Recently I moved all my environments to a private OpenStack cloud. It is probably the best technology decision I have made recently. Not only is it fast but also convenient when I visit clients and prospects on my travels. I simply VPN and there I am with my Puppet Master and a variety of VMs that range from Windows to Debian to CoreOS.

At the time, I was relying on Vagrant to provision the VMs and rebuild any (or all) servers that I needed to work on. While I love Vagrant, sometimes it can be a little bit slow and I still needed to track a lot of yaml files to do the stuff. So, I took it back old school! Text files and bash. Why? Because I could.

On this exercise, we will compose a set of files and a shell script that will create 3 OpenStack VMs and bind them to a Puppet Master to deliver 3 apache web servers and be config managed by Puppet.

Why wait? Let’s get started.

Setup

You must have an OpenStack project out there. I assume you do.

Pick up the OpenStack Python client and use it on a python virtual environment:

Create a folder to host all these items:

mkdir ~/.ostack_env

Install virtualenv:

sudo pip install virtualenv

Change directory and launch virtualenv:

cd ~ && virtualenv .ostack_env

Activate the environment:

source bin/activate

Install OpenStack client:

pip install python-openstackclient

Finally, you can source your Openstack RC file and test the client with a command:

source /path/to/your/openstack/rc/file
openstack usage show

At this time, you should be set.

Procedure

On the same directory if you wish or another one create the following three files:

instances: Simple text files with instance names
ostack_provision.sh: Bash script to provision the VMs
user_data.sh: The cloud bootstrap file for OpenStack

Edit instances and populate with some instance names:

xuxo_web001.puppet.xuxo
xuxo_web002.puppet.xuxo
xuxo_web003.puppet.xuxo

Edit ostack_provision.sh as below and replace values with yours. The comments describe what the script is doing:

#!/bin/sh
# shell script to demo the deployment of an openstack instance
# get creative, you can loop through a file to create multiple instances,etc.
# Uses accompanying user_data script to provision Puppet agent
# jesus at puppet.com

# --change variables or source from file.
# Replace with your OpenStack environment data. You can use
# Private side IP instead of OStack's associated floating one

export master_ip="192.x.x.x"
export user_data_script="user_data.sh"
export key="<your_key_name>"
export img="centos_7_x86_64"
export flavor="d1.small"
export sec_group="<your_sec_group>"
# v where we installed the openstack client
export ostack_dir="/Users/jgarcia/.ostack_env/bin"

export OS_AUTH_URL="https://<your_ostack_provider>:5000/v2.0"
export OS_TENANT_ID="9e7fd724f0764461ac5cbe874d8fb5e5"
export OS_TENANT_NAME="<your_tenant_name>"
export OS_PROJECT_NAME="<your_project_name>"
export OS_USERNAME="<your_username>"

if [ -z "$OS_REGION_NAME" ];
 then unset OS_REGION_NAME
fi

# function definition
function provisionVM(){

echo "Please enter your OpenStack Password: "
 read -sr OS_PASSWORD_INPUT
 export OS_PASSWORD=$OS_PASSWORD_INPUT

# Here we will loop through instances and create each VM
# We will also associate a floating IP to each one

for i in `cat instances`;
 do
 echo "Creating VM $i"
 ${ostack_dir}/openstack server create --user-data ${user_data_script} --image ${img} --flavor ${flavor} --security-group ${sec_group} --key-name ${key} $i
 echo "Assigning floating IP"
 float_ip=`${ostack_dir}/openstack ip floating list | grep None | awk -F '|' '{print $3}' | head -1`
 ${ostack_dir}/openstack ip floating add ${float_ip} $i
 echo "Floating IP assigned"
 ${ostack_dir}/openstack server show $i
 done
}

#main
provisionVM

Now edit user_data.sh. This is the cloud injection file that will provide some initial configuration to our VMs. On ours, it will add some basic host info and provision Puppet’s agent to get to the master and retrieve catalogs:

#!/bin/sh
PHOST=$(curl http://169.254.169.254/latest/meta-data/public-hostname)
SHOST=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname|awk -F '.' '{print $1}')
PIPV4=$(ifconfig -a|grep -w inet|awk '{print $2}'|grep 192)
HSET=0
grep -i $PHOST /etc/hosts || HSET=1
grep -i $PIPV4 /etc/hosts || HSET=1

# Replace with your master's info
echo "192.x.x.x master.puppet.xuxo master" >> /etc/hosts
#echo "$PIPV4 $PHOST $PHOST.puppet.xuxo" >> /etc/hosts
hostname $PHOST
sleep 10
curl -k https://<your master>:8140/packages/current/install.bash | bash

Ok!, we’re good to go! Open a terminal and run our provisioner:

bash ostack_provision.sh

The client will be going through the list of instances and giving you a message each time one is built. Wait a couple of minutes and check the Master’s console:

ostack

And each one has it’s own small apache server with web page:

webpage

As an idea, you can put all these behind a load balancer and you have yourself a little VM scaling setup!

Thanks for reading.

Advertisements

One thought on “Bash, OpenStack, and Puppet: A Quick tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s