Skip to content

Instantly share code, notes, and snippets.

@UtahDave
Forked from rgbkrk/fabfile.py
Created October 18, 2013 23:49
Show Gist options
  • Save UtahDave/7049950 to your computer and use it in GitHub Desktop.
Save UtahDave/7049950 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Bootstrapping the salt minions on Rackspace (using fabric rather than
salt-cloud).
This is not the most effective way to do this, but it gets it done.
For instance, we could be bootstrapping servers while others are still being
built by Rackspace. In the end, it doesn't matter much.
To get up and running, use
$ fab minions_up saltstrap_minions
minions_up will set env.hosts (for other fabric calls).
If you want to use saltstrap_minion or restart_minion, set the hosts when calling them
$ fab -H 67.207.156.15,67.207.156.147,67.207.155.4 restart_minion
'''
import os
import fabric.api
from fabric.api import env, run
from fabric.contrib import files
from fabric.context_managers import settings
import pyrax
env.user = 'root'
env.key_filename = os.path.expanduser("~/.ssh/id_rsa")
def minions_up():
'''
Creates a specific build of machines and bootstraps salt.
'''
# Authenticate with Rackspace, use credential file
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credential_file(os.path.expanduser("~/.rackspace_cloud_credentials"))
# Shorthand
cs = pyrax.cloudservers
# Building Ubuntu 12.04 boxes with 512 MB RAM
flavor_512 = [flavor for flavor in cs.flavors.list() if flavor.ram == 512][0]
ubu_image = [img for img in cs.images.list() if "Ubuntu 12.04" in img.name][0]
# Grab the SSH key labeled 'mac'
keys = cs.keypairs.list()
kp = cs.keypairs.get('mac')
names = ("qa01.minion.saltdev", "qa02.minion.saltdev", "qa03.minion.saltdev",
"prod01.minion.saltdev", "prod02.minion.saltdev", "prod03.minion.saltdev")
# Create homogenous minions (with names as above)
# This hardcodes the key name to the one I'm using right now.
minions = map(lambda name: cs.servers.create(name, ubu_image.id, flavor_512, key_name="mac"), names)
# Make sure all the minions are done before we move on to fabric runs
minions = [pyrax.utils.wait_for_build(minion, verbose=True) for minion in minions]
env.hosts = [str(minion.accessIPv4) for minion in minions]
print(env.hosts)
def saltstrap_minion():
'''
Install saltstack, set master, restart the salt-minion daemon
'''
run("curl -L http://bootstrap.saltstack.org | sh -s -- git develop")
run("echo 'master: 162.242.160.207' > /etc/salt/minion")
run("service salt-minion restart")
with settings(warn_only=True):
run("cat /etc/salt/pki/minion/minion.pub")
#67.207.156.15,67.207.156.147,67.207.155.4,67.207.156.44,67.207.155.79,67.207.156.158
def restart_minion():
run("service salt-minion restart")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment