Version Control

14 Nov 2020 - Pentti

Had a brief look to couple Git commands (log, diff, blame, reset) and then installed Vim including theme, configuration ja plugins. Tested everything first locally and then to a virtual machine using Salt.

Git

In this assignment I’m having a simple repository created inside of hyperthread folder. Inside of the folder I also have file called importantfile which I have edited couple of times and made commits afterwards.

Git log

Git log shows detailed information of made commits to the current repository. Example below shows all the commits I have made.

pena@webserver:~/hyperthread$ git log
commit fea2b1c867ba3d7beeecaecc0f04c90ed4c04a85 (HEAD -> master)
Author: peon <penonized@gmail.com>
Date:   Thu Nov 12 17:11:24 2020 +0000

    added a

commit c49c81e72d726a8d5746363370832abbfb698336
Author: peon <penonized@gmail.com>
Date:   Thu Nov 12 17:10:04 2020 +0000

    added n

commit b346201b5e35df6fe92fca002d19220a90ce24b6
Author: peon <penonized@gmail.com>
Date:   Thu Nov 12 17:08:17 2020 +0000

    added e

commit a52b2c069b7ca374c39fc978a6ec0f586bdebad6
Author: peon <penonized@gmail.com>
Date:   Thu Nov 12 17:06:52 2020 +0000

    added p

Git diff

Git diff is used to see what has been modified before commit. In this example I erased contents of the important file and added text “this is the modified important file which is not commited yet”. Every row marked with - refers to the last made commit file and every row marked with + refers to the content of the modified file.

$ git diff 
diff --git a/importantfile b/importantfile
index de59436..37ec64f 100644
--- a/importantfile
+++ b/importantfile
@@ -1,8 +1,3 @@
  This is very important file

- foofoofoofoo   foofoo  foo   foo  foofoofoo
-    foo   foo   foo     foofo foo  foo   foo
-    foofoofoo   foofoo  foo foofo  foofoofoo
-    foo         foo     foo  foof  foo   foo
- foofoofoof     foofoo  foo   foo  foo   foofoo
+ This is the modified important file which is not commited yet.

Git blame

With Git blame it is possible to have a loglist kind of view of the commits of a certain repository. Example below demostrates how you can see which author changed what line and when the change was made. You can also see that there is still the previous example’s commit waiting to be committed.

$ git blame importantfile
^a52b2c0 (peon 2020-11-12 17:06:52 +0000 1) ## This is very important file
^a52b2c0 (peon 2020-11-12 17:06:52 +0000 2)
00000000 (Not Committed Yet 2020-11-12 18:16:53 +0000 3) This is the important file which is not commited yet.

Git reset –hard

Git reset --hard command returns to the last committed state, which is the same as seen as in the log in the git log example. This can be confirmed by comparing the given id from the reset command to the one in the log, which starts with fea2b1c.

$ git reset --hard
HEAD is now at fea2b1c added a

This means that the importantfile should now have the same content as marked with - characters in the second example.

$ cat importantfile
## This is very important file

foofoofoofoo   foofoo  foo   foo  foofoofoo
   foo   foo   foo     foofo foo  foo   foo
   foofoofoo   foofoo  foo foofo  foofoofoo
   foo         foo     foo  foof  foo   foo
foofoofoof     foofoo  foo   foo  foo   foofoo

Salting Vim

Most of the Linux powered servers have vim installed by default, but usually its users want to include their own configuration and plugins to the program. I’m not that hardcore vim user myself, but I still have some simple plugins and configuration to ease my day to day life.

Testing locally

I started by checking that vim is definitely installed to my system:

$ vim -version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 15 2020 06:40:31)

Nextly I wanted to have latest version of the package, which is was 8.2. Since I had already updated my system, I knew that 8.2 version of vim is not not available from Ubuntu’s default repositories. I followed these instructions to get the repository and the latest package.

$ sudo add-apt-repository ppa:jonathonf/vim
$ sudo apt update -y
$ sudo apt install vim -y
...
$ vim -version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Oct 24 2020 16:26:37)

After having installed the latest package, it was time to set up plugins and thme theme I’m fond of. For installing plugins there was two options: Vundle or Pathogen. I chose to use Pathogen because it seemed easier to configure in the eyes of setting up salt configuration. Vundle would have needed external commands to be executed in the command line to put plugins into use but with Pathogen plugins can be just included in a folder and they will be ready to be used.

To set up Pathogen I did following:

$ mkdir -p ~/.vim/autoload ~/.vim/bundle
$ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

and included line execute pathogen#infect() to my ~/.vimrc file.

$ cat ~/.vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on

I encountered backspace problem after creating the .vimrc file, but it was fixed by adding command set backspace=indent,eol,start to the file.

Now I was ready to test that the plugins actually work by adding vim-closetag plugin to the ~/.vim/bundle folder.


~/.vim/bundle$ git clone https://github.com/alvan/vim-closetag.git
~/.vim/bundle$ tree 
bundle
└── vim-closetag
    ├── README.md
    └── plugin
        └── closetag.vim

Next I tested that the plugin works by creating a html file vim test.html and assuring that when I write html tags the plugin closes them automatically.

<html></html>
<body></body>
<h1></h1>

Last step of this manual installation was to apply a theme to the vim. This was a very simple task and I just downloaded a theme I like and in this case it was mushroom. Then I just put it in to the .vim/colors folder and included line colo mushroom to my .vimrc file.

colors
└── mushroom.vim

However the theme didn’t work just like that. After doing some searching I found out that I have to force vim to use 256 colors using line set t_Co=256 in the .vimrc configuration file.

$cat .vimrc
execute pathogen#infect()
syntax enable
filetype plugin indent on
set t_Co=256
colo mushroom
set backspace=indent,eol,start

Now I had succesfully set up vim with basic configuration, theme and one plugin and it was time to move to use Salt.

Testing locally with Salt

To get started I created folder and init.sls for my vim salt state module.

$ sudo mkdir -p /srv/salt/vim
$ touch /srv/salt/vim/init.sls

At this point I just wanted to test that the module actually works by creating simple cmd.run function.

vimtest:
  cmd.run:
    - name: "echo working"
$ sudo salt-call state.apply vim --local

ID: vimtest
Function: cmd.run
Name: echo working
Result: True
Comment: Command "echo working" run
...
         stdout:
             working

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0

Then I wanted Salt to check that the repository I’m going to use is up to date. Also, at this point I wanted to add that package vim will depend on this this statement. For that I used require_in statement.

...
checkvimrepo:
   pkgrepo.managed:
     - name: ppa:jonathonf/vim
     - refresh_db: True
     - require_in:
       - pkg: vim
$ sudo salt-call state.apply vim --local
...
ID: checkvimrepo
Function: pkgrepo.managed
Name: ppa:jonathonf/vim 
Result: True
Comment: Configured package repo 'ppa:jonathonf/vim'
...
         repo:
             ppa:jonathonf/vim

Summary for local
------------
Succeeded: 2 (changed=2)
Failed:    0

Now I was ready to add in the vim installation state. I used pkg.latest command to ensure getting the latest version of the package.

...
viminstall:
  pkg.latest:
    - name: vim
$ sudo salt-call state.apply vim --local
...
ID: installvim 
Function: pkg.latest
Name: vim 
Result: True
Comment: The following packages were successfully installed/upgraded: vim
...
         vim:
             ----------
             new:
                 2:8.2.1897-0york0~20.04
             old:
...
Summary for local
------------
Succeeded: 3 (changed=3)
Failed:    0

I had now succesfully installed the latest version of vim and it was time to send the previously created .vimrc file using file.managed state which I have included in the /etc/salt/vim/ folder. I have also moved the whole ~/./vim/ folder to the same folder because it is already configured and I have just to move it to the target computer in order to use it.

vim
├── .vim
├── .vimrc
├── mushroom.vim
└── init.sls

I started by delivering the .vimrc .vimrc file with file.managed. Salt is using root when executing commands so I had to include username for the path, otherwise the file would go directly to the root home directory. Since I’m handling multiple files that include including username, I decided to set a variable on top of the file so it can be changed depending on a situation.


{% set user = <setuserhere> %}
...
~{{ user }}/.vimrc:
   file.managed:
     - source: salt://vim/.vimrc

$ sudo salt-call state.apply vim --local
...
ID: ~/pena/.vimrc 
Function: file.managed
Result: True
Comment: File /home/pena/.vimrc updated
...
	 diff:
             New file
...
Summary for local
------------
Succeeded: 4 (changed=2)
Failed:    0

By restarting vim I could tell that it is using the .vimrc file because of the errors.

$ vim testi.html
Error detected while processing /home/pena/.vimrc:
line    6:
E117: Unknown function: pathogen#infect
line    8:
E185: Cannot find color scheme 'mushroom'
Press ENTER or type command to continue

Final step for local testing was to recursively deliver the .vim folder to the minion. I also wanted to define access permissions because otherwise moved items would have root access permissions instead and it could be problematic later on.


...
~{{ user }}/.vim:
  file.recurse:
    - source: salt://vim/.vim
    - user: {{ user }}
    - group: {{ user }} 
    - file_mode: 755
    - dir_mode: 755

Tree structure after state.apply:

$ tree ~/.vim/
/home/pena/.vim/
├── autoload
│   └── pathogen.vim
├── bundle
│   └── vim-closetag
│       ├── README.md
│       └── plugin
│           └── closetag.vim
└── colors
    └── mushroom.vim

I wanted to test the configuration once again by removing everything vim related.

$ sudo apt-get autoremove --purge vim vim-common
$ sudo rm -rf /etc/vim /usr/share/vim ~/.vim ~/.vimrc /etc/apt/sources.list.d/jonathonf-ubuntu-vim-focal.list
$ sudo salt-call state.apply testi --local
Comment: Command "echo working" run
Comment: Configured package repo 'ppa:jonathonf/vim'
Comment: The following packages were successfully installed/upgraded: vim
Comment: File /home/pena/.vimrc updated
Comment: Recursively updated /home/pena/.vim
Summary for local
------------
Succeeded: 5 (changed=5)
Failed:    0

And everything works as should!

Testing the configuration with Vagrant instance

Now the configurement is ready for testing with real salt-minion. For this purpose I have booted one VM instance with vagrant and it’s ready to use.

Firstly I wanted to make sure that the connection worked

$ sudo salt '*' cmd.run 'pwd'
slavec3869540:
    /root

Then I changed the user from the init.sls file to vagrant, because it is the default user.

{$ set user = vagrant $}

And to test the state:

$ sudo salt '*' state.apply vim

Comment: Command "echo working" run
Comment: Configured package repo 'ppa:jonathonf/vim'
Comment: The following packages were successfully installed/upgraded: vim
Comment: File /home/vagrant/.vimrc updated
Comment: Recursively updated /home/vagrant/.vim
Summary for local
------------
Succeeded: 5 (changed=5)
Failed:    0

Everything works the same as locally!