Setup local Linux git repo to push to GitLab using SSH

If you or your organisation are using GitLab then here are a few steps that you might find useful when creating a repo and pushing it to GitLab.

Firstly create your repo on GitLab from the screen below:

git-gitlab-1

After that your remote repo will be created. We now need to setup git on your dev machine.

The steps below are just for linux as this tutorial is for Linux, windows is covered here(to be completed!).

Setup a git user

git config –global user.name “your name
git config –global user.email “you@youremail.com

Create Empty Repository

If you have an existing repo skip this stage.

SSH URLs are strongly recommended.
You will need to add an SSH key for each platform on which you will use git.
cd to your directory where you wish to store you git repo. Create a directory for this project.

mkdir your-project

cd your-project
git init
touch README
git add README
git commit -m ‘first commit’
git remote add origin git@git.domain:group/your-project.git

Existing Git Repo?

cd existing_git_repo
git remote add origin git@git.domain:group/your-project.git
git push -u origin master

 

Adding SSH key

Before generating an SSH key, check if your system already has one by running:

cat ~/.ssh/id_rsa.pub

If your see a long string starting with ssh-rsa or ssh-dsa, you can skip the ssh-keygen step.

To generate a new SSH key just open your terminal and use code below. The ssh-keygen command prompts you for a location and filename to store the key pair and for a password. When prompted for the location and filename you can press enter to use the default. It is a best practice to use a password for an SSH key but it is not required and you can skip creating a password by pressing enter. Note that the password you choose here can’t be altered or retrieved.

ssh-keygen -t rsa -C "you@youremail.com"

Use the code below to show your public key.

cat ~/.ssh/id_rsa.pub

Copy-paste the key (copy the complete key starting with ssh- and ending with your username and host).

 

Navigate to your SSH keys section in your profile on GitLab. Select your profile icon, then the SSH tab  and then Add Key, as highlighted below:

git-gitlab-2

Paste the key into the key text field that appears when you click “Add Key”

Your first push

Now go back to your local git repo directory and run the git push command on the command line. It will ask for your password that you used to create the key.

git push -u origin master

You should now be able to check on GitLab that a commit and push has been completed.

Get previous or next HTML element with a certain class

If you want to get the previous or next element with a certain class you can use JQuery and a CSS selector. This may be useful in certain circumstances, I found myself in that position today and thought I’d share it here.

HTML

An example markup.

<div class="AnElementYouWant">Some beginning text</div>
<div id="ElementAfter">More text</div>
<div class="AnElementYouWant">Some ending text</div>

Javascript

Given the above markup we can use this Javascript to select the divs around the centre div.

var first = $(this).prevAll("AnElementYouWant:first");
var last = $(this).nextAll("AnElementYouWant:first");

So console.log(first.text()) would output “Some beginning text” and console.log(last.text()) would output “Some ending text”.

Hope someone finds that useful.

Robot Prototype 1 chassis complete

I’m building a robot which will have sonar and IR ranging units for navigation and 2 cameras on-board allowing for stereoscopic vision. Here is my first go at putting together a prototype wheeled robot chassis.

 

robot-p1-top-view
As  you can probably see here it will be controlled by an Arduiono at the very lowest level. Controlling the Arduino will be an embedded Linux system, e.g. a raspberry pi or banana pi, having a radio / wirless link for accessing the Linux system.

 

robot-p1-bottom-view

I’ll post the results of how it drove on various surfaces.


robot-p1-top-view2

Here’s one with the cameras on-board and the old wheels which i replaced with the above omni-wheels.

 

 

robot-p1-front-view

 

Edit: The robot is having difficulty turning on very thick fluffy carpet. It looks like more power is required. I am currently working on a prototype 2 chassis that I will post here.

TreeView context menus

Recently I wanted to add a right click context menu to a list of items in a TreeView control in C# and winforms.

It was relatively simple but there is one small thing that isn’t as obvious. That is that it is good to allow users to be able to right click an item and open it, delete it, run it or whatever you action is on the your context menu, without having to left click select it first. Less clicks == good.

So first create your TreeView, you can do this by dragging the control onto your form or custom control.

Now add a ContextMenuStrip item from the toolbox in the same way.

Go to the properties of the TreeView and set the ContextMenuStrip property to be the one which you just created.

Now add items to your context menu by click the small arrow drop down at the top right corner of it, see the image below:

contextMenuAddItems

You will then be able to click Edit Items:

contextMenuAddItems2

From you can add menu items, so add the ones you want and name them appropriately and change the menu text for them as well.

Now for the right clicking to actually work without a user selecting the item first we need to add this line of code somewhere sensible, i’ve put mine just after the InitializeComponent(); line in the form.

yourTreeView.NodeMouseClick += (sender, args) => yourTreeView.SelectedNode = args.Node;

This line of code means that the node you right click on will be selected as you right click, this is very important as in most cases you will want the actions in the context menu to be applicable to the item you have right clicked.

So now if you click your ContextMenuStrip control that you added your items to you should see your menu at the top of the form (while in design mode the context menu is just at the top of the form). Double click one of your items to add some functionaility.

One of my right click menu items is called openFile the click handler is below, this is where it is important to be able to have your SelectedNode set to the one you right clicked on.

// Code executed when a context menu item is clicked
private void openFile_Click(object sender, EventArgs e) {
    
    // Here we can now reference yourTreeView.SelectedNode
    DoSomething(yourTreeView.SelectedNode.Text);
}

 

If we did not have that first line of code ( the NodeMouseClick event handler ) the above code would throw an exception as the SelectedNode would be null (unless the user did a left click first).

Happy right clicking!

Gedit settings not saving

The Problem

(Just skip to the solution for the commands you need to run to fix this)

I use Gedit over SHH with Xming / X11 running quite regularly and I was not able to save my setting in Gedit. I am running a Red Hat based OS ( such as CentOS and Scientific Linux )

The first error I got was:

GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.

This tells us that Gedit cannot use the normal GSettings backend to save our settings to disk and is just going to save them to (volatile) memory so when we exit the applicaiton our settings are lost. Not a very clear message at all if you actually want to fix this bug as it gives you no clues that it really wants you to install various other packages (one of them is dconf as we’ll see below).

After searching around I found out i needed to install dconf. After which I got a new error message:

dconf-WARNING **: failed to commit changes to dconf: Failed to execute child process "dbus-launch" (No such file or directory)

Again not very clear… but dconf wants us to install dbus, so I did that and found it was already installed! So I searched Yum and saw a package named dbus-x11. I installed this package and my settings now save.

The solution steps are below.

The Solution

yum install dconf
yum install dconf-editor
yum install dbus
yum install dbus-x11

I hope this helps someone!

 

Setup FTP on Red Hat based OSs

In this post I’ll go through the very basic steps of how to setup your FTP server on Red Hat based OSs such as CentOS and Scientific Linux.

yum install -y vsftpd
systemctl start vsftpd
systemctl enable vsftpd

Now we need to edit the configuration file here:

/etc/vsftpd/vsftpd.conf

Here is my file, the changes I have made are in red and bold.

# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
ascii_upload_enable=YES
ascii_download_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=Your banner text here.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
#chroot_local_user=YES
#chroot_list_enable=YES
# (default follows)
#chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
use_localtime=YES

Now restart the FTP service:

systemctl restart vsftpd

Create a user for FTP access:

useradd ftpuser
passwd ftpuser

Now you should be able to connect to your FTP server using the above user account (root probably wont’ work, hence the new user). You will need your server IP, to get this you can run ifconfig.

I use FileZilla as an FTP client.

service command depreciated in RHEL based OSs

service

The service command is no longer valid in red hat based systems such as CentOS and Scientific Linux. You will probably see the message “Redirecting to /bin/systemctl” when you try and start / stop / status a service. So now we have to do it as below:

For example:

service httpd start

Would now be run as below:

systemctl start httpd

 

chkconfig

chkconfig has also been changed, for example:

chkconfig httpd on

becomes

systemctl enable httpd

… and …

chkconfig httpd off

becomes

systemctl disable httpd

… and …

chkconfig httpd

becomes

systemctl is-enabled httpd

… and …

The below command shows what services are enabled (chkconfig –list):

systemctl list-unit-files --type=service

 

Hope this helps someone!