Managing Source Code
Source code management (SCM) is an essential element in a DevOps environment. Think about it: If you will be turning your infrastructure into code, it is important that there is a way to review any changes and go back to different versions of a file in case new changes introduce problems (for instance, periodic instabilities in the best case, outages in the worst case). Some might think the “easy” way would be to make multiple copies of a file each with a unique name (Vagrantfile1, Vagrantfile2, Vagrantfile01012015, and so on), but then you have to deal with the hassle of renaming the file when you want to use it and trying to remember what was different about all the files.
The various teams in the development organization are most likely using some SCM system already to manage their work (for example, software developers storing their source code, QA teams managing their test scripts). As you begin using SCM technologies, it would be worthwhile discussing best practices with these other groups.
There are many SCM solutions, including SVN, Mercurial, and so on. Git happens to be one of the more popular SCM systems in the DevOps community. So, for this book, we use Git.
Git is a distributed version control system, which means that you make a local copy of the central repository instead of just checking out individual files. Local commits can be synchronized back to the central server so that there is consistency in the environment, and users can always pull the latest version of the source from the central repository. This architecture differs from traditional source code management systems, in which only the central server ever has a complete copy of the repository.
As you work through the examples in this book, we recommend using one of the freely available online Git repositories, such as Bitbucket, GitHub, and Gitorious, as your central location for storing your code. Each site has its own unique features. For example, BitBucket allows unlimited free private repositories. GitHub is the online repository system that this book’s authors used for their code. However, feel free to use whichever system meets your needs, as the methods by which you obtain code (clone/pull) and store code (push) are universal across any Git system.
Creating Your First Git Repository
First, install Git using your favorite package manager (for example, homebrew or macports on Mac OS X, apt-get on Ubuntu/Debian, yum on Red Hat/CentOS/Fedora). For Windows users, the popular online repositories like GitHub and BitBucket offer software clients that make it easy to interact with their online repository system. Alternatively, the http://git-scm.com site maintains a standalone install of the Git binary for Windows.
If you are using Linux or Mac OS X, you can open a terminal window to work with the following examples. Windows users must use the special shell that gets installed with whichever Git client that you use. The example syntax will be Linux/Mac-based, but the commands should be equivalent for the Windows platform.
Before we start writing any code, we need to set a couple global variables so that Git knows who we are. It’s not as critical for local copies of the repository, but when we start pushing our code to a remote server, it will be very critical. The two global variables are your email address and username:
git config --global user.email "firstname.lastname@example.org" git config --global user.name "Your Name"
As a matter of fact, if you try using Git and making your first commit without setting these variables, Git will prompt you to set them before you can continue.
If you followed along with the earlier Vagrant examples, you already have a directory of content that we can work with. Otherwise, create a new directory and create a text file in it. From here on out, make sure that you are in that directory on your command-line prompt.
First, let’s initialize this directory to have a Git repository:
If you do a listing of your directory with the option to show hidden files (ls -a on Linux/Mac or dir /A:H on Windows), you’ll see that there is a hidden directory called .git. This directory contains your repository’s files and settings specific to this repository. These local settings are combined with the global settings that we set earlier, and we can confirm this by using the following command:
git config –l
If you want to see the state of the files in your directory (has the file been added to the repository? Are there any changes since the last command? and so on), you can type git status and see output similar to what is shown in Listing 3-5.
Listing 3-5 Git Repository Status
git-test $ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) .vagrant/ Vagrantfile nothing added to commit but untracked files present (use "git add" to track)
The last line is most important; it tells us that our files need to be tracked for the repository to manage it. This is important to take note of as putting files into the directory does not automatically get it tracked by the SCM tool. This feature prevents us from tracking junk files in the repository and wasting space.
Let’s tell Git to track our Vagrantfile:
git add Vagrantfile
However, the .vagrant/ directory is not essential to be tracked because it only contains temporary files that Vagrant uses to set up your VM. We can explicitly tell Git to ignore this directory by creating a .gitignore file. Use your favorite text editor and create your .gitignore file with a single entry:
Alternatively, you could use a simple echo command to accomplish the same thing. (Windows users will need to use the special Git Shell binary that is included with their Git install for this to work properly.)
echo '.vagrant/' > .gitignore
If you run the git status command again, you’ll see that Git informs us about the .gitignore file as well. What gives? Remember that Git needs to be told what to do with any files or directories that the repository can see including the .gitignore file. Well, there are two ways to deal with your .gitignore file:
- Add the .gitignore file itself to the list of files and directories to ignore.
- Tell Git to track the .gitignore file as well.
I will use the second option so that anyone else who may use my repository will be able to ignore the appropriate files as well:
git add .gitignore
Now, if we check the status of the repository again, we should see output similar to what is shown in Listing 3-6.
Listing 3-6 Updated Repository Status
git-test $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: .gitignore new file: Vagrantfile
All the files in our directory are either ready to be committed or added to the .gitignore list of nonessential files and directories. So, all that’s left to do is to commit the repository changes:
A file editor will automatically be opened so that you can enter details about the files that you are committing. (By default, vi is used.) See Listing 3-7.
Listing 3-7 Your Commit Message
git-test $ git commit 1 This is my first commit. 2 # Please enter the commit message for your changes. Lines starting 3 # with '#' will be ignored, and an empty message aborts the commit. 4 # On branch master 5 # 6 # Initial commit 7 # 8 # Changes to be committed: 9 # new file: .gitignore 10 # new file: Vagrantfile 11 #
You must enter a message; otherwise, the commit will be canceled. If you are not familiar with vi, press I, type some text, press the Escape key, and then type :wq and press Enter. If you don’t want to deal with the text editor, you can use the short form of the git commit command with the -m option to enter your commit message on the same line:
git commit -m "This is my first commit."
If the commit is successful, you should see the following output:
[master (root-commit) d962cd6] This is my first commit. 2 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 Vagrantfile
Working with a Central Git Server (a.k.a. A Remote)
If you have opened an account on either GitHub, BitBucket, or whatever public Git repository site that you prefer, you will need to provide your computer’s SSH public key to the site so that it can verify who you are. Each site may have a different way of doing this. So, consult the documentation for the appropriate steps. For Windows users, this is typically handled for you automatically by installing the client software for the site. Mac and Linux users must generate an SSH public key by using the ssh-keygen command.
Once your SSH public key is properly configured on your remote, it’s time to create the repository on the remote site that you will be storing your files into: a process known as pushing. When you create your remote repository on the website, you should skip the automatic generation of the README file. After the repository is created, the site will provide you with a link that you can use to tell your local repository what is the location of your remote server, often labeled as origin.
In my setup, I gave the same name to my repository as I did to my local repository. This is optional, and the names can differ. I can use the git remote command with the link that GitHub gave me to update my local repository settings:
git remote add origin email@example.com:DevOpsForVMwareAdministrators/git
If I use the git config -l command, I will see new data about the location of my remote server:
I can now push the files from my local repository to my remote repository:
git push origin master
I should see output similar to what is shown in Listing 3-8.
Listing 3-8 Git Remote Push Results
git-test $ git push origin master Warning: Permanently added the RSA host key for IP address '188.8.131.52' to the list of known hosts. Counting objects: 4, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 2.13 KiB | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To firstname.lastname@example.org:DevOpsForVMwareAdministrators/git-test.git * [new branch] master -> master
Figure 3-1 shows what the repository looks like on GitHub after I push the first commit to the remote server.
Figure 3-1 Remote Git repository
If there is a remote repository that you want to use, like the samples we’re providing for this book, you can either use CLI-based methods or GUI-based methods to clone the remote repository to your local machine. Your remote repository system should have options similar to what is pictured on the lower-right corner of Figure 3-1.
The GUI-based methods will differ according to the site you are using. However, on GitHub, you can either use the Clone in Desktop button if you are using its Mac or Windows client, or you can use the Download Zip button, which will be a simple zip file that contains all the source code of the repository. If you are using the Windows or Mac platform, the Clone in Desktop option is recommended because it will create your Git remote link to the remote repository automatically.
The CLI-based method involves taking the SSH or HTTP clone URL and using the git clone command, similar to the following:
git clone https://github.com/DevOpsForVMwareAdministrators/git-test.git
After executing this command, Git will create a directory with the name of the repository (in this case, git-test) and copy the contents of the repository into that directory. You can begin using and updating the code, and the Git remote link will be created for you automatically just like with the Clone in Desktop button mentioned earlier. If you have permission to make changes to the remote repository, you can perform a Git push to forward any local changes to the remote repository. Requesting to make changes to others’ repositories is not within the scope of this book; but if you are interested in the topic, you can research repository forks and pull requests. The implementation of these features may differ between the various public Git repository sites. So, check the appropriate documentation for the site that you are using.
If you are having issues working with the remote repository setup, an alternate workflow consists of the following:
- Before you begin your work, create an empty repository on your remote Git site of choice (for instance, GitHub).
- Clone the empty repository to your local machine using the GUI or CLI methods discussed earlier.
- Begin working in the cloned directory and perform your commits there. You can then use the same git push command introduced earlier to forward your source code commits to the remote repository.