Installing Chef Server on AWS using Python

This post is about how to install chef server on a AWS EC2 instance using fabric python library.

Create EC2 Instance

In this link you will find all the steps required to create and start an EC2 instance using python

Installing Chef server using Fabric Library

Fabric is a python library and command-line tool that makes easy the use of SSH for application deployment or systems administration tasks.

Fabric installation

    pip install fabric

First Steps with fabric

First at all we must create a file named we will add the code below to this file in order to check the linux box type:

    from fabric.api import run

    def host_type():
        run('uname -s')

We can run fab to see what happens:

    $fab show_host_type  -i ~/.ssh/my_key.pem --host -u ubuntu
    [] Executing task 'show_host_type'
    [] run: uname -s
    [] out: Linux
    [] out: 


Encrypting EC2 Volume

Chef components such as recipes, databags, etc, usually store sensitive data about our infraestructure, for that reason we will encrypt the block storage that we attached on the first step when we created the ec2 instance with python.

In our example we will encrypt /dev/xvdf device using luks library, the first step is setting up the encryption on the device, to do that, we add another task on the fabfile:

    def setup_luks(device):
        cmd  = 'sudo cryptsetup -y --cipher blowfish luksFormat {}'.format(device)

fab setup_luks:'/dev/xvdf' -i ~/.ssh/my_key.pem --host -u ubuntu

[] Executing task 'setup_luks'
[] run: sudo cryptsetup -y --cipher blowfish luksFormat /dev/xvdf
[] out: 
[] out: WARNING!
[] out: ========
[] out: This will overwrite data on /dev/xvdf irrevocably.
[] out: 
[] out: Are you sure? (Type uppercase yes): YES
[] out: Enter passphrase: 
[] out: Verify passphrase: 
[] out: 

Disconnecting from done.

The next step is verify the device, in order to do this we will add another task to the fabfile:

    def verify_luks_device(device):
        cmd = 'sudo file -s {0}'.format(device)
    fab verify_luks_device:'/dev/xvdf' -i ~/.ssh/my_key.pem --host -u ubuntu

[] Executing task 'verify_luks_device'
[] run: sudo file -s /dev/xvdf
[] out: /dev/xvdf: LUKS encrypted file, ver 1 [blowfish, cbc-plain, sha1] UUID: e20d71a4-0a4a-43ba-84cc-1ab3470e845f

Now We now that We have a valid luks encryted device, the next is open the device, to do that we add another task to the fabfile:

    def open_luks_device(device, storedev):
        cmd = 'sudo cryptsetup luksOpen {} {}'.format(device, storedev)

    fab open_luks_device:'/dev/xvdf,mydevice' -i ~/.ssh/my_key.pem --host -u ubuntu

[] Executing task 'open_luks_device'
[] run: sudo cryptsetup luksOpen /dev/xvdf myvol
[] out: Enter passphrase for /dev/xvdf: 
[] out: 

Now we have the device mapped in /dev/mapper/mydevice, the next step is format the device:

    def format_luks_device(storedev):
        run('sudo mkfs.ext4 -m 0 /dev/mapper/{}'.format(storedev))
    fab format_luks_device:'mydevice' -i ~/.ssh/my_key.pem --host -u ubuntu

[] Executing task 'format_luks_device'
[] run: sudo mkfs.ext4 -m 0 /dev/mapper/mydevice
[] out: mke2fs 1.42.9 (4-Feb-2014)
[] out: Filesystem label=
[] out: OS type: Linux
[] out: Block size=4096 (log=2)
[] out: Fragment size=4096 (log=2)
[] out: Stride=0 blocks, Stripe width=0 blocks
[] out: 655360 inodes, 2620928 blocks
[] out: 0 blocks (0.00%) reserved for the super user
[] out: First data block=0
[] out: Maximum filesystem blocks=2684354560
[] out: 80 block groups
[] out: 32768 blocks per group, 32768 fragments per group
[] out: 8192 inodes per group
[] out: Superblock backups stored on blocks: 
[] out:         32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
[] out: 
[] out: Allocating group tables: done                            
[] out: Writing inode tables: done                            
[] out: Creating journal (32768 blocks): done
[] out: Writing superblocks and filesystem accounting information: done 
[] out: 
[] out: 

The next step is backup the luks header and store it on our local box, We can use the task below:

    def backup_luks_header_device(device):
        run('sudo cryptsetup luksHeaderBackup --header-backup-file /home/ubuntu/luksbackup {}'.format(device))
        run('sudo chown ubuntu /home/ubuntu/luksbackup')
        get('/home/ubuntu/luksbackup', './')
        run('sudo rm /home/ubuntu/luksbackup')
fab backup_luks_header_device:'/dev/xvdf' -i ~/.ssh/mykey.pem --host -u ubuntu                                                                                                                                                                         
[] Executing task 'backup_luks_header_device'                                                                                                   
[] run: sudo cryptsetup luksHeaderBackup --header-backup-file /home/ubuntu/luksbackup /dev/xvdf                                                 
[] run: sudo chown ubuntu /home/ubuntu/luksbackup                                                                                               
[] download: /home/manuel/src/blog/2015/01/chef-aws-python/luksbackup <- /home/ubuntu/luksbackup                                                

[] run: rm /home/ubuntu/luksbackup

The last step is mount the volume:

    def mount_luks_device(storedev, mount_point, owner):
        run('sudo mkdir -p {}'.format(mount_point))
        run('sudo mount /dev/mapper/{} {}'.format(storedev, mount_point))
        run('sudo chown {} {}'.format(owner, mount_point))
fab mount_luks_device:'mydevice,/secure,ubuntu' -i ~/.ssh/mykey.pem --host -u ubuntu

[] Executing task 'mount_luks_device'
[] run: sudo mkdir -p /secure
[] run: sudo mount /dev/mapper/mydevice /secure
[] run: sudo chown ubuntu /secure

If you want to unmount and close the device you must execute the task below:

    def unmount_luks_device(storedev, mount_point):
        run('sudo umount {}'.format(mount_point))
        run('sudo cryptsetup luksClose {}'.format(storedev))
    fab unmount_luks_device:'mydevice,/secure' -i ~/.ssh/mykey.pem --host -u ubuntu

[] Executing task 'unmount_luks_device'
[] run: sudo umount /secure
[] run: sudo cryptsetup luksClose mydevice

Chef Server Installation

The objective is automatize the steps required to install chef server

    def install_chef_server():
        run('sudo apt-get install ruby')
        run(' wget')
        run('sudo dpkg -i /home/ubuntu/chef-server-core_12.0.1-1_amd64.deb')
        run('sudo chef-server-ctl reconfigure')

The can be found at this link