Andy Bavier | 8d51c6c | 2015-04-01 11:40:22 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import subprocess |
| 4 | import json |
| 5 | import time |
| 6 | |
| 7 | jujuconfig="/home/ubuntu/openstack.cfg" |
| 8 | |
| 9 | services = { |
| 10 | "nova-compute" : "--config=%s cs:~andybavier/trusty/nova-compute" % jujuconfig, |
| 11 | } |
| 12 | |
| 13 | def get_free_machines(status): |
| 14 | for (service, svcinfo) in status['services'].iteritems(): |
| 15 | if 'units' in svcinfo: |
| 16 | for (unit, unitinfo) in svcinfo['units'].iteritems(): |
| 17 | if 'machine' in unitinfo: |
| 18 | machine = unitinfo['machine'] |
| 19 | status['machines'][machine]['unit'] = unit |
| 20 | |
| 21 | free = {} |
| 22 | for (machine, mchinfo) in status['machines'].iteritems(): |
| 23 | if machine == "0": |
| 24 | continue |
| 25 | |
| 26 | if 'unit' not in mchinfo: |
| 27 | # print "%s: %s" % (machine, mchinfo['dns-name']) |
| 28 | free[machine] = mchinfo |
| 29 | |
| 30 | return free |
| 31 | |
| 32 | |
| 33 | def deploy(status, service, cmd): |
| 34 | # Deploy nova-compute to all free machines |
| 35 | machines = get_free_machines(status) |
| 36 | |
| 37 | for (machine, mchinfo) in machines.iteritems(): |
| 38 | if service in status['services']: |
| 39 | print "Adding unit %s on %s" % (service, mchinfo['dns-name']) |
| 40 | subprocess.check_call("juju add-unit --to=%s %s" % (machine, service), shell=True) |
| 41 | else: |
| 42 | print "Deploying service %s on %s" % (service, mchinfo['dns-name']) |
| 43 | subprocess.check_call("juju deploy --to=%s %s" % (machine, cmd), shell=True) |
| 44 | status['services'][service] = "installed" |
| 45 | time.sleep(10) |
| 46 | |
| 47 | def get_juju_status(): |
| 48 | output = subprocess.check_output("juju status --format=json", shell=True) |
| 49 | status = json.loads(output) |
| 50 | return status |
| 51 | |
| 52 | def addservices(): |
| 53 | status = get_juju_status() |
| 54 | |
| 55 | for service, cmd in services.iteritems(): |
| 56 | try: |
| 57 | deploy(status, service, cmd) |
| 58 | except: |
| 59 | pass |
| 60 | |
| 61 | def main(): |
| 62 | addservices() |
| 63 | |
| 64 | if __name__ =='__main__': |
| 65 | main() |