This is an assignment h5 - Palvelinten hallinta ICT4TN022-5 in Haaga-Helia University of Applied Sciences

Applying Salt states from Git repository can be very useful if you want to test different states that you find, for example, from GitHub. I’ll demonstrate this with new Git repository where I build my own state, but this also works for any already working Salt state that also happens to be a Git repository.

Creating a Git repository

I’ll start by making a directory for my Git repository:

$ mkdir gitsalt

Then I make it a Git repository with following command:

$ cd gitsalt
$ git init

Now the directory gitsalt is Git repository. So I just start building some sample directory structure for my Salt state.

$ mkdir emacs
$ emacs emacs/init.sls

And lastly I make some short for this repository:

$ emacs
# salt-emacs

Simple SaltStack state for installing Emacs, the One True Editor!

Applying the state locally

After that is done I test applying this state locally with:

$ sudo salt-call --local --file-root=$(pwd) state.apply emacs
          ID: emacs
    Function: pkg.installed
      Result: True
     Comment: Package emacs is already installed
     Started: 21:07:01.367076
    Duration: 303.542 ms

Summary for local
Succeeded: 1
Failed:    0
Total states run:     1
Total run time: 303.542 ms

In the previous command I indicated that I work locally with command salt-call and option --local. I also indicated that that the current working directory is also the file root for Salt, so I don’t need to move these files to my master’s /srv/salt directory. Also which is impossible for me currently, since I’m mostly working offline.

Commit your changes

Since I’m working offline, I’ll demonstrate committing changes to your Git repository with my own local Git server.

Creating your own Git server

I start making my own Git server by making a new user called git:

$ sudo adduser git
Adding user `git' ...
Adding new group `git' (1001) ...
Adding new user `git' (1001) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
	Full Name []: Topi Kettunen Git
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] 

Then I try logging in to that user with ssh:

While logged in to that user I make a new bare Git repository with:

(git)$ mkdir gitsalt.git
(git)$ cd gitsalt.git
(git)$ git init --bare

Now there is an empty repository in /home/git/gitsalt.git. After that I can close the ssh session with CTRL-d

Commit your changes to your new Git server

I cannot just yet to commit and push my changes, since Git doesn’t yet know where to push these changes. There is (at least) two solutions for this. We can either clone the empty repository from /home/git/gitsalt.git and move the files from my regular user’s gitsalt directory to that cloned or we set new remote URL for our already existing repository so that it’ll point to our Git server.

Cloning bare repository

I make a new directory for the new cloned repository so it doesn’t mix with our already made repository

$ mkdir git
$ cd git
$ git clone ssh://[email protected]:/home/git/gitsalt.git
Cloning into 'gitsalt'...
[email protected]'s password: 
warning: You appear to have cloned an empty repository.
$ cp -r ../gitsalt/emacs ../gitsalt/ gitsalt/
$ git add .
$ git commit -m "Initial commit"
[master (root-commit) 4fccb71] Initial commit
 2 files changed, 5 insertions(+)
 create mode 100644
 create mode 100644 emacs/init.sls
$ git push
[email protected]'s password: 
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 401 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To ssh://localhost:/home/git/gitsalt.git
 * [new branch]      master -> master

Adding new remote URL

This also works when you’re working with already existing repository and you might want to push your changes to different Git server.

$ cd
$ cd gitsalt
$ git add .
$ git commit -m "Initial commit"
[master (root-commit) 0d5fa58] Initial commit
 2 files changed, 5 insertions(+)
 create mode 100644
 create mode 100644 emacs/init.sls
$ git remote add origin ssh://[email protected]:/home/git/gitsalt.git
$ git push origin master

You can set that the master branch in your repository is also the upstream for it with the command:

$ git push --set-upstream origin master

So in the future you can push your changes with simply git push. This is only recommended when you’re working on your own private projects, since changes to upstream can easily change a lot of thing in bigger projects.