Create git server on AWS cloud

Posted on Wednesday, January 23, 2013

I am new to the git world,  I have been using subversion for many of my past projects, so bear with me if I seem to cover rudimentary git in this document.

The goal of this document is to show you how to use cloud-init with ubuntu 12.10 to easily create a git server in the aws cloud.

Ubuntu 12.10

I would prefer to use 12.04 LTS for its longer support, however 12.04 does not have a new enough version of cloud-init installed for the features we will be using.

A list Ubuntu’s virtual machines, or AMIs in amazons language, can be found at [1]

For this example I will be using an EBS backed AMI in the east-1 region.  This AMIs id is ami-7539b41c


AWS allows you to pass in script as a file or text when instantiating a server.  This script will then be run during the creation process.   You can for example send it a bash script and it will execute it just fine.

However, rather than using bash,  there is new script language in town cloud-init  [2]  One part of this is the cloud-config script.   This idea is still in development, but it works well now,  I believe their goal is to have a script more focused on “what would you do on initialization” .   Focusing more on things like adding users,  getting ssh keys set up etc.

I am not going to go into great depth on how to use cloud init, I am mostly just going to show you a script that works and explain that script a little.  To see more examples you can go to the /usr/share/doc/cloud-init/examples folder on your ubuntu install and read the examples there.

Here is the cloud-config script I came up with to help create a git server in the AWS cloud.  (replace the ssh keys and password with your own

# This cloud-config file creates a git server
apt_update: true
apt_upgrade: true

# Add the git package
 - git-core

# Add groups to the system
# The following example adds the ubuntu group with members foo and bar and
# the group cloud-users.
  - git
  - patman

# Add users to the system. Users are added after groups are added.
  - default
  - name: git
    shell: /usr/bin/git-shell
    gecos: git user
    primary-group: git
    lock-passwd: true
    ssh-import-id: None
# Add SSh keys for every user of the git repository
      - ssh-rsa AAAAB3NzXXXX
      - ssh-rsa AAAAB3NzXXXX
# This is not "needed" I am just adding myself to the system as a user who can ssh into the box
  - name: patman
    shell: /bin/bash
    gecos: Patrick Bailey
    primary-group: patman
    groups: admin
    passwd: $6$aFXXXXXX
    ssh-import-id: None
      - ssh-rsa AAAABXXXX
      - ssh-rsa AAAABXXXX

# This is a work around to a but
# The users password is locked upon creation and should not be
# This just unlocks it
  - [passwd, patman, '-u']

This script
·         installs the git tool
·         Create the git user
·         Sets the git default shell to usr/bin/git-shell, this will allow git programs to work via ssh but not allow a shell to ssh into for users.
·         Puts all the public SSH keys into the gits aurhorized_keys file
·         Create the user “patman” with ssh access and a set password for the machine (the password is the encrypted password as seen in /etc/shadow

Save this file off as a script called

Create the EC2 machine

The following assumes you have installed and are familiar with the AWS command line tools and have them installed on their system.

This also assumes that your default security group has port 22 open

Run the following command to instantiate this machine.

  > ec2-run-instances ami-7539b41c -b /dev/sda1=:8:true -k pats-keypair -t t1.micro --availability-zone us-east-1a -f cloud-config-git.txt

The instance id should be displayed in this instance it is i-d3bd8da2

  >   ec2-describe-instances i-d3bd8da2

Copy the address

Now since I created the patman user and gave it my ssh keys I can ssh like this

  >   ssh

Instead of doing this (using the ubuntu user)

  >   ssh -i .work_ec2/pats-keypair.pem

Create a location to put the git repositories

From the aws server run the following commands

  >   sudo mkdir -p /opt/git
  >   sudo chown git:git /opt/git

Now place a “bare project” in this folder for each of projects you want this server to act as a origin master for.  More information can be found here on how to do this [3]

Assuming you have a git project called my_git_project.git

  >   sudo su -s /bin/bash git
  >   cd /opt/git
  >   mkdir my_git_project.git
  >   cd my_git_project.git
  >  git --bare init

From my understanding the --bare init creates a shell git project for you to later push to.

This git “origin master” server should be ready to go!

Push your git project to this master

Now from your local machine upload your project.

If you do not have a local project here is a quick few commands to create one.

  >   git init my_git_project.git

  >   cd my_git_project
  >  touch test_file.txt
  >   git add *
  >   git commit -m "initial commit"

Set the origin

  >   git remote add origin

Now view the remote connection to make sure it is correct.

  >   git remote -v


In case you need to edit this you can remove the origin by running the command  “git remote rm origin”  Oh, and origin is not a special name you could use bob instead if you like, or whatever makes sense for you.

Before uploading this repository to the git server you can check to see if anything needs to be checked in

  >   git status

Now push this project up to the master

  >   git push origin master



Now for some testing

I will try a few different test setups to download from this master server and to upload to it.

Command line:  clone the master repository

From your local machine, in another directory run the following commands.

  >   git clone  my_git_project.git


Now go into this and add a new file add and merge it to the local repository then upload it to the master git repository

  >   cd my_git_project.git/
  >   touch new_file.txt
  >   git add new_file.txt
  >   git commit –m “added new_file.txt”

If you run

  >   git remote -v

You will see that its master is already set

To push the new data up to the server run the following commands

  >   git push origin master

Command line: get updates from the master

Back to the original project you made on your system that does not have the new_file.txt

Run the following command

  >   git pull origin master

Enough Tests for not that seems to work just fine 

[1]  Ubuntus image releases
       Visited 1/2013
[2]  CloudInit
       Visited 1/2013
[3]  4.2 Git on the Server - Getting Git on a Server
       Visited 1/2013

No comments:

Post a Comment