Go back to the main page

Create your own git server easily with Chef and the Vagrant AWS plugin: Part 1.

This article is over 2 years old. Proceed with caution.

Regards ♨ – Minimul

 

Free private git server with unlimited repositories?

Remove the enslavement of having a private Github account. Gitolite is an open source git server in which you may have as many repositories as you desire. In this series of articles I will demonstrate how to create a git server on EC2 that is automatically backed up to S3. If you are a new signup to Amazon Web Services (AWS), a micro EC2 instance and up to 5 GBs of S3 storage are free for the first 12 months.

Why use Vagrant?

With the introduction of the Vagrant AWS plugin (and hopefully many more provider plugins to follow), Vagrant is no longer just for creating and managing development environments. Here are some reasons why Vagrant is useful managing non-development environments:

  • You don't have to manually rsync provisioner changes to the production server as Vagrant does that automatically.
  • You can easily integrate ENV variables with the provisioning tool.
  • Handy vagrant commands such as vagrant ssh, vagrant provision, and vagrant destroy just work.
  • Encourages a faster work flow where you first develop your provisioner recipes on a local provider (Virtualbox, VMWare) and then deploy to the production provider (AWS, Rackspace, etc).
  • With respect to the Vagrant AWS plugin you can manage multiple machines in different regions.

Part 1 Goal. Get the latest Chef on your EC2 target.

  1. Sign up with AWS and create your keypair and security group. Follow this article to the tee. For the security group you only have to enable port 22 for SSH access. Make sure you download your AWS security credentials and private key.
  2. Fill in the below environmental variables in ~/.profile — or other place you prefer — with the AWS credentials and private key from Step 1. Also add a path to your local public ssh key as this is going to be used later when setting up the git server.
  3. # Just an EXAMPLE
    export AWS_ACCESS_KEY_ID="********************"
    export AWS_SECRET_ACCESS_KEY="****************"
    export AWS_KEYPAIR_NAME="my-key-pair-here" 
    export MY_PUBLIC_SSH_KEY_PATH=$HOME/.ssh/id_rsa.pub
    export MY_PRIVATE_AWS_SSH_KEY_PATH=$HOME/.ssh/aws.pem
    
    IMPORTANT: After updating the file make sure you run source ~/.profile in the command window/pane you intend to the run vagrant up --provider=aws (see below).
  4. Make a directory for Vagrant workspaces.
  5. Personally, I use a directory called WorkSpaces.
    $ mkdir ~/WorkSpaces
    
    NOTE: These steps were done within a local Ruby environment of 1.9.3
  6. Make sure you have the latest Vagrant version, which at the time of this writing is 1.3.3.
  7. I don't guarantee anything in this article unless at least Vagrant 1.3.x is used.
  8. Install Berkshelf. Berkshelf aids in managing and creating Chef cookbooks. Version at the time of this writing is 2.0.10.
  9. $ gem install berkshelf
    
  10. Install Vagrant plugins.
  11. $ vagrant plugin install vagrant-aws # (0.3.0)
    $ vagrant plugin install vagrant-berkshelf # (1.3.3)
    $ vagrant plugin install vagrant-omnibus # (1.1.1)
    
    DISCUSSION: The vagrant-aws plugin handles communication with an EC2 or VPC instance. The vagrant-berkshelf plugin integrates Berkshelf with Vagrant, and the vagrant-omnibus plugin handles installing the latest Chef on your target.
  12. Install a Berkshelf cookbook boilerplate.
  13. $ cd ~/WorkSpaces
    $ berks cookbook git-server
    $ cd git-server
    $ bundle install
    
          create  git-server/files/default
          create  git-server/templates/default
          create  git-server/attributes
          create  git-server/definitions
          create  git-server/libraries
          create  git-server/providers
          create  git-server/recipes
          create  git-server/resources
          create  git-server/recipes/default.rb
          create  git-server/metadata.rb
          create  git-server/LICENSE
          create  git-server/README.md
          create  git-server/Berksfile
          create  git-server/Thorfile
          create  git-server/chefignore
          create  git-server/.gitignore
             run  git init from "./git-server"
          create  git-server/Gemfile
          create  git-server/Vagrantfile
    
    DISCUSSION: The berks cookbook command will install a bunch of files. Don't be too concerned with what each individual file is for but just know that it creates an orthodox layout for Chef cookbooks and Vagrant projects.
  14. Install a Vagrant box set with an AWS provider.
  15. $ vagrant box add aws-basic https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
    Downloading or copying the box...
    Extracting box...e: 0/s, Estimated time remaining: --:--:--)
    Successfully added box 'aws-basic' with provider 'aws'!
    
  16. Remove the Berkshelf generated Vagrantfile with this one.
  17. Vagrant.configure("2") do |config|
      config.vm.hostname = "git-server"
      config.vm.box = "aws-basic"
      config.vm.boot_timeout   = 120
      config.omnibus.chef_version = :latest
    
      config.vm.provider :aws do |aws, override|
        aws.access_key_id = ENV['AWS_ACCESS_KEY_ID']
        aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
        aws.keypair_name = ENV['AWS_KEYPAIR_NAME']
        aws.security_groups = ['your-security-group-here']
        aws.instance_type = "t1.micro"
        aws.ami = "ami-d0f89fb9"
        override.ssh.username = "ubuntu"
        override.ssh.private_key_path = ENV['MY_PRIVATE_AWS_SSH_KEY_PATH']
      end
    end
    
    DISCUSSION: The only setting you will have to manually edit is the security_groups value. The config.vm.boot_timeout = 120 gives SSH connections a little more time. The AMI, ami-d 0f89fb9, is an Ubuntu 12.04 64-bit image for the us-east-1a zone that supports micro instances, which again are free for 12 months if you are a new AWS signup. You can choose a different AMI id here but be forewarned as some of the AMI ids I tested did not work as advertised.
  18. Ready to vagrant up! Actually, it is vagrant up --provider=aws and don't forget the --provider=aws.
  19. $ vagrant up --provider=aws
    Bringing machine 'default' up with 'aws' provider...
    [default] Warning! The AWS provider doesn't support any of the Vagrant
    high-level network configurations (`config.vm.network`). They
    will be silently ignored.
    [default] Launching an instance with the following settings...
    [default]  -- Type: t1.micro
    [default]  -- AMI: ami-c30360aa
    [default]  -- Region: us-east-1
    [default]  -- Keypair: <your key pair name>
    [default]  -- Security Groups: ["your-security-group-here"]
    [default]  -- Block Device Mapping: []
    [default]  -- Terminate On Shutdown: false
    [default] Waiting for instance to become "ready"...
    [default] Waiting for SSH to become available...
    [default] Machine is booted and ready for use!
    [default] Rsyncing folder: /Users/christian/WorkSpaces/git-server/ => /vagrant
    [default] Installing Chef 11.6.0 Omnibus package...
    
    DISCUSSION: The vagrant up --provider=aws will create a new EC2 instance and then install the latest Chef on it via the Vagrantfile config.omnibus.chef_version = :latest setting.
    IMPORTANT: If you get a "Cannot find box named aws-basic" it was because you just ran vagrant up and not vagrant up --provider=aws.
    NOTE: After the initial vagrant up --provider=aws run in which an EC2 instance was successfully created you don't need to provide the --provider=aws switch to commands such as vagrant ssh, vagrant provision, and vagrant destroy.
    IMPORTANT: If the vagrant up --provider=aws process hangs at the Waiting for SSH to become available... you either didn't set a security group that has SSH access or your PATH to the AWS private key is incorrect.
  20. Last, test the assertions made in the previous step.
  21. vagrant ssh # should login successfully and get the Ubuntu 12.04 banner
    $ chef-client -v # should output "Chef: 11.6.0" (latest at the time of this writing)
    

Stay tuned as in part 2 I will build on these steps revealing a drop-dead simple Git server installation.

Comment on this article?