allomov
4/13/2014 - 7:43 PM

Script to deploy single node Cloud Foundry setup using juju charms.

Script to deploy single node Cloud Foundry setup using juju charms.

#!/usr/bin/env python
import os
import yaml
import commands
import time


def run(command):
    print "Executing: %s" % command
    os.system(command)


def get_status_hash():
    return yaml.load(commands.getoutput('juju status'))


def wait_for_machine(machine_number,
                     required_state='started',
                     retries=40,
                     timeout=2):
    while True:
        status_hash = get_status_hash()
        current_state = status_hash['machines'][machine_number]['agent-state']
        retries = retries - 1
        print "retries: %s" % retries
        print "current_state: %s" % current_state
        if (current_state == required_state) or (retries == 0):
            if retries == 0:
                raise BaseException("Machine %s is not "
                                    "available." % machine_number)
            else:
                break
        time.sleep(timeout)

environment_name = commands.getoutput('juju switch')
print("!!! Destroying %s environment" % environment_name)
run('juju destroy-environment %s' % environment_name)

print("!!! Bootstraping %s environment" % environment_name)
run('juju bootstrap')

print("!!! Creating machine for deploy.")
run('juju add-machine --constraints arch=amd64 --constraints mem=2G '
    '--series trusty')

juju_status_hash = yaml.load(commands.getoutput('juju status'))
machine_number = juju_status_hash['machines'].keys()[-1]
wait_for_machine(machine_number)

charms = {'cf-nats': 'nats',
          'cf-go-router': 'router',
          'cf-cloud-controller': 'cc',
          'cf-dea': 'dea',
          'cf-uaa': 'uaa'}

for charm_name in charms.keys():
    service_name = charms[charm_name]
    os.chdir(charm_name)
    run('juju deploy --to %s --repository=../../. '
        'local:trusty/%s %s '
        '--show-log' % (machine_number, charm_name, service_name))
    os.chdir('..')

for service_name in ['router', 'cc', 'dea', 'uaa']:
    run('juju add-relations nats %s' % service_name)

for service_name in ['cc', 'dea', 'uaa']:
    run('juju add-relations router %s' % service_name)