Skip to content

Instantly share code, notes, and snippets.

@linuxsimba
Last active July 4, 2016 08:55
Show Gist options
  • Select an option

  • Save linuxsimba/1057b92769187c853e5fd22a1ee7e008 to your computer and use it in GitHub Desktop.

Select an option

Save linuxsimba/1057b92769187c853e5fd22a1ee7e008 to your computer and use it in GitHub Desktop.
Vagrantfile for Openstack Ansible
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# This file has lots of comments. If you are using VIM,
# this quick liner will remove all comments, if you so wish.
#
# :g/\v^(#|$)/d
#
#
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
# Configure Default settings if not configured via user's shell
# environment variables
#
# Extend String class with blank? function. Its from RubyOnRails Code
class String
def blank?
respond_to?(:empty?) ? !!empty? : !self
end
end
# Name of the vagrant box.
ENV["AIO_VAGRANT_BOX"] ||= "aio"
# What Type of Hypervisor are you using. Defaults to using
# Virtualbox.
ENV["AIO_HYPERVISOR"] ||= "virtualbox"
# CPU and Memory Settings
ENV["AIO_CPUS"] ||= "2"
ENV["AIO_MEMORY"] ||= "4096"
# What Release to Load. If AIO_TAG is mentioned then use that
# the tag instead of the release as the starting point for the repo
ENV["AIO_OPENSTACK_RELEASE"] ||= ""
ENV["AIO_TAG"] ||= ""
# Use AIO_OPENSTACK_RELEASE env var as the default branch name
# if AIO_TAG is set, use that instead.
# Just build the Vagrant VM before running run-playbooks.sh
ENV["AIO_STOP_AFTER_BOOTSTRAP"] ||= "0"
# Check for vagrant plugins that you might want to use.
# Example:
# export AIO_VAGRANT_PLUGINS "vagrant-sahara vagrant-notify"
# By default AIO_VAGRANT_PLUGINS is empty
# Default: Not defined.
ENV["AIO_VAGRANT_PLUGINS"] ||= ""
# Install the vagrant plugins needed. Takes a list of vagrant plugins.
# Example
# export AIO_VAGRANT_PLUGINS "vagrant-sahara vagrant-notify"
# Default: empty string
ENV["AIO_VAGRANT_PLUGINS"] = ""
vagrant_plugins = ENV["AIO_VAGRANT_PLUGINS"].split(%r{,|\s+}).map {|a| a.strip}.reject(&:empty?)
# vagrant-triggers is used to run code that checks
# that the branch or tag mentioned exists before building the
# VM. Can save a little time and frustration with those with
# fat fingers.
vagrant_plugins.push('vagrant-triggers')
# The virtualbox plugin is installed by default.
# Only need to check for the libvirt plugin if the
# user selected AIO_HYPERVISOR = 'libvirt'
#
if ENV["AIO_HYPERVISOR"] == 'libvirt'
vagrant_plugins.push("vagrant-libvirt")
end
# Add additional bootstrap options to the bootstrap script
ENV["AIO_BOOTSTRAP_OPTS"] ||= ""
# Check that all the required vagrant plugins are installed.
# Otherwise quit explaining which plugins are missing.
exit unless vagrant_plugins.all? do |named_plugin|
Vagrant.has_plugin?(named_plugin) || (
puts "The #{named_plugin} plugin is required. Install it with:"
puts "vagrant plugin install #{named_plugin}"
false
)
end
# Function to determine whether to clone at a specific release
# or tag, depending on whether AIO_TAG env variable is set
$release_or_tag = ENV["AIO_OPENSTACK_RELEASE"]
def branch_or_tag_exists?
if ENV["AIO_TAG"].empty? and ENV["AIO_OPENSTACK_RELEASE"].empty?
puts "AIO_TAG and AIO_OPENSTACK_RELEASE environment variables are empty"
puts "Set AIO_TAG or AIO_OPENSTACK_RELEASE environment variable"
puts "Example:"
puts " export AIO_OPENSTACK_RELEASE=stable/mitaka"
puts " vagrant up"
exit
end
if not ENV["AIO_TAG"].empty? and not ENV["AIO_OPENSTACK_RELEASE"].empty?
puts "AIO_TAG(#{ENV['AIO_TAG']}) and " + \
"AIO_OPENSTACK_RELEASE(#{ENV['AIO_OPENSTACK_RELEASE']}) " + \
"env variables are configured"
puts "Unset the undesired environment variable"
exit
end
if not ENV["AIO_TAG"].empty?
$release_or_tag = ENV["AIO_TAG"]
branch_exists = `git ls-remote --tags https://github.com/openstack/openstack-ansible #{$release_or_tag}`
if branch_exists.blank?
msg = 'check environment variable AIO_TAG. ' + \
'The tag: ' + $release_or_tag + ' is not in the' + \
'Openstack Ansible Git Repo'
puts msg
exit
else
puts "Git clone Openstack Ansible at Tag: '#{$release_or_tag}'"
end
else
branch_exists = `git ls-remote --heads https://github.com/openstack/openstack-ansible #{$release_or_tag}`
if branch_exists.blank?
msg = 'check environment variable AIO_OPENSTACK_RELEASE. ' + \
'The branch: ' + $release_or_tag + ' is not in the' + \
'Openstack Ansible Git Repo'
puts msg
exit
else
puts "Git clone Openstack Ansible at Release: '#{$release_or_tag}'"
end
end
end
openstack_ansible_dir = '/opt/openstack-ansible'
# Simple script to setup AIO environment.
$create_opt_openstack_dir = <<SCRIPT
git_installed=`dpkg -l | egrep "ii[ ]+git[ ]+" | wc -l`
if [ "$git_installed" == "0" ]; then
apt-get -y install git
echo "** Install git"
fi
if [ ! -d #{openstack_ansible_dir} ]; then
mkdir #{openstack_ansible_dir}
echo "** Created #{openstack_ansible_dir}"
fi
chown vagrant #{openstack_ansible_dir}
SCRIPT
$bootstrap_script = <<SCRIPT
bootstrap_opts=$1
apt-get -y dist-upgrade
git clone https://github.com/openstack/openstack-ansible \
--branch #{$release_or_tag} #{openstack_ansible_dir}
cd #{openstack_ansible_dir}
sudo BOOTSTRAP_OPTS="${bootstrap_opts}" ./scripts/bootstrap-ansible.sh
sudo BOOTSTRAP_OPTS="${bootstrap_opts}" ./scripts/bootstrap-aio.sh
SCRIPT
# Run the playbooks
$run_playbook_script = <<SCRIPT
bootstrap_opts=$1
cd #{openstack_ansible_dir}
sudo BOOTSTRAP_OPTS="${bootstrap_opts}" ./scripts/run-playbooks.sh
SCRIPT
# When logging via vagrant ssh, set the first login directory
# to /opt/openstack-ansible
#
$change_login_start_dir = <<SCRIPT
username=vagrant
homedir=/home/${username}
echo "cd #{openstack_ansible_dir}" >> ${homedir}/.bashrc
chown ${username} ${homedir}/.bashrc
echo "** On first login, ${username} will log into #{openstack_ansible_dir}"
SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = ENV["AIO_VAGRANT_BOX"]
config.trigger.before :up do
branch_or_tag_exists?
end
config.vm.provider :virtualbox do |vb|
vb.memory = ENV["AIO_MEMORY"]
vb.cpus = ENV["AIO_CPUS"]
end
config.vm.provider :libvirt do |lv|
lv.memory = ENV["AIO_MEMORY"]
lv.cpus = ENV["AIO_CPUS"]
end
config.vm.provision :shell, inline: $create_opt_openstack_dir
config.vm.provision :shell do |s|
s.inline = $bootstrap_script
s.privileged = false
s.args = [ENV["AIO_BOOTSTRAP_OPTS"]]
end
if ENV["AIO_STOP_AFTER_BOOTSTRAP"] != "0"
config.vm.provision :shell do |s|
s.inline = $run_playbook_script
s.privileged = false
s.args = [ENV["AIO_BOOTSTRAP_OPTS"]]
end
end
config.vm.provision :shell, inline: $change_login_start_dir
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment