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