← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~hloeung/canonical-mojo-specs/update-add-floating-ip into lp:~canonical-launchpad-branches/canonical-mojo-specs/trunk

 

Haw Loeung has proposed merging lp:~hloeung/canonical-mojo-specs/update-add-floating-ip into lp:~canonical-launchpad-branches/canonical-mojo-specs/trunk.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~hloeung/canonical-mojo-specs/update-add-floating-ip/+merge/325208

Update add-floating-ip as it allows overriding floating IP pool via MOJO_FLOATING_IP_POOL environment variable.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~hloeung/canonical-mojo-specs/update-add-floating-ip into lp:~canonical-launchpad-branches/canonical-mojo-specs/trunk.
=== modified file 'scripts/utils/add-floating-ip'
--- scripts/utils/add-floating-ip	2016-03-02 07:27:31 +0000
+++ scripts/utils/add-floating-ip	2017-06-07 08:01:31 +0000
@@ -5,17 +5,20 @@
 # NOTE:         $MOJO_PROJECT and $MOJO_STAGE must be set before calling this script.
 #
 
-import jujuclient
-import novaclient.client
+import json
 import os
 import subprocess
 
+import novaclient.client
+
+
 SECRETS_DIR = "/srv/mojo/LOCAL/{MOJO_PROJECT}/{MOJO_STAGE}/".format(**os.environ)
 
-juju_env = jujuclient.Environment.connect(
-    subprocess.check_output(
-        ['juju', 'switch']).rstrip()
-)
+status = json.loads(
+    subprocess.check_output(['juju', 'status', '--format=json']))
+services = status.get("applications")
+if services is None:
+    services = status["services"]
 
 nova_tenant = novaclient.client.Client(os.environ['NOVA_VERSION'],
                                        os.environ['OS_USERNAME'],
@@ -23,26 +26,28 @@
                                        os.environ['OS_TENANT_NAME'],
                                        os.environ['OS_AUTH_URL'])
 
+
 def get_ip_pool():
+    pool = os.environ.get('MOJO_FLOATING_IP_POOL')
+    if pool is not None:
+        return pool
+
     if os.environ["OS_USERNAME"].startswith("stg-"):
         return "staging_ext_net"
     return "ext_net"
 
 
 def units_in_service(service_name):
-    status = juju_env.status(service_name)
-    units = status["Services"][service_name]["Units"]
+    units = services[service_name]["units"]
     return units.keys()
 
 
 def machine_of_unit(unit_name):
     service_name, _ = unit_name.split('/', 1)
 
-    # I have opinions about this API.
-    status = juju_env.status(service_name)
-    unit = status["Services"][service_name]["Units"][unit_name]
-    machine_no = unit["Machine"]
-    machine = status["Machines"][machine_no]
+    unit = services[service_name]["units"][unit_name]
+    machine_no = unit["machine"]
+    machine = status["machines"][machine_no]
 
     return machine
 
@@ -50,7 +55,7 @@
 def get_unit_floating_ip(unit_name):
     machine = machine_of_unit(unit_name)
 
-    server = nova_tenant.servers.find(id=machine['InstanceId'])
+    server = nova_tenant.servers.find(id=machine["instance-id"])
 
     # If we already have a floating IP associated, use that.
     try:
@@ -88,12 +93,15 @@
         except:
             # If this happens you're going to need to either get that back in the list,
             # or blow away the state file so it gets a new IP.
-            raise(RuntimeError, "Desired IP {} not in floating ips list!".format(myip))
+            raise(RuntimeError("Desired IP {} not in floating ips list!".format(myip)))
 
     if fip.instance_id:
         # If it's already associated, ensure it's associated to us
-        if (fip.instance_id != machine['Id']):
-            raise(RuntimeError, "IP {} is associated, but not to {}!".format(myip, unit_name))
+        machine_id = machine.get('Id')
+        if machine_id is None:
+            machine_id = machine['instance-id']
+        if (fip.instance_id != machine_id):
+            raise(RuntimeError("IP {} is associated, but not to {}!".format(myip, unit_name)))
         return myip
 
     # Go associate it now
@@ -113,16 +121,24 @@
 
 # Add floating IPs to all units in the haproxy and squid services:
 add-floating-ip haproxy squid
+
+# Add floating IPs to the apache2/0 and apache2/1 units:
+export targets="apache2/0 apache2/1"
+add-floating-ip
 """.format("add-floating-ip"))
 
 
 def main():
     import sys
 
-    if len(sys.argv) < 2:
+    if len(sys.argv) >= 2:
+        args = sys.argv[1:]
+    elif 'targets' in os.environ:
+        args = os.environ['targets'].split()
+    else:
         return usage()
 
-    for arg in sys.argv[1:]:
+    for arg in args:
         if "/" not in arg:
             service_name = arg
             print("{}:".format(service_name))


Follow ups