~zulcss/openstack-charm-testing/lxd-updates-16Jun

« back to all changes in this revision

Viewing changes to bin/post-deploy-config

  • Committer: Ryan Beisner
  • Date: 2016-06-10 13:18:06 UTC
  • mfrom: (214.1.2 openstack-charm-testing)
  • Revision ID: ryan.beisner@canonical.com-20160610131806-6mnzwcn9b7c0ccr1
[james-page, r=1chb1n] Rework ext-port tooling to use openstack clients directly and set ext-port based on mac addresses

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import os
6
6
import sys
7
7
 
8
 
cmd_out = subprocess.check_output(['neutron', 'net-list'])
9
 
 
10
 
net_id = os.environ.get('NET_ID')
11
 
 
12
 
if net_id:
13
 
    # Use OSCI / Jenkins environment variable if defined.
14
 
    print 'Using NET_ID environment variable: {}'.format(net_id)
15
 
else:
16
 
    # Preserve existing default behavior (eg. manual testing)
17
 
    net_name = os.environ['OS_USERNAME'] + '_admin_net'
18
 
    print('Using default network name: {}'.format(net_name))
19
 
    for line in cmd_out.split('\n'):
20
 
        if net_name in line:
21
 
            net_id = line.split('|')[1].replace(' ', '')
22
 
 
23
 
service = sys.argv[1]
24
 
 
25
 
ext_port = 'eth1'
26
 
if len(sys.argv) >= 3:
27
 
    ext_port = sys.argv[2]
28
 
 
29
 
service_config = yaml.load(
30
 
    subprocess.check_output(['juju', 'status', service])
31
 
)
32
 
 
33
 
uuids = []
34
 
for machine in service_config['machines']:
35
 
    uuids.append(service_config['machines'][machine]['instance-id'])
36
 
 
37
 
if len(uuids) > 0:
38
 
    for uuid in uuids:
39
 
        print("Attaching interface to instance {}".format(uuid))
40
 
        subprocess.check_call(
41
 
            ['nova', 'interface-attach', '--net-id', net_id, uuid]
42
 
        )
43
 
 
44
 
print("Setting ext-port configuration on neutron-gateway to {}".format(ext_port))
45
 
subprocess.check_call(['juju', 'set', service, 'ext-port={}'.format(ext_port)])
 
8
from neutronclient.v2_0 import client as ne_client
 
9
from novaclient import client as no_client
 
10
 
 
11
if __name__ == '__main__':
 
12
    neutron = ne_client.Client(username=os.environ['OS_USERNAME'],
 
13
                               password=os.environ['OS_PASSWORD'],
 
14
                               tenant_name=os.environ['OS_TENANT_NAME'],
 
15
                               auth_url=os.environ['OS_AUTH_URL'],
 
16
                               region_name=os.environ['OS_REGION_NAME'])
 
17
    nova = no_client.Client('2',
 
18
                            os.environ['OS_USERNAME'],
 
19
                            os.environ['OS_PASSWORD'],
 
20
                            os.environ['OS_TENANT_NAME'],
 
21
                            os.environ['OS_AUTH_URL'],
 
22
                            os.environ['OS_REGION_NAME'])
 
23
 
 
24
    net_id = os.environ.get('NET_ID')
 
25
    if net_id:
 
26
        # Use OSCI / Jenkins environment variable if defined.
 
27
        print('Using NET_ID environment variable: {}'.format(net_id))
 
28
    else:
 
29
        # Preserve existing default behavior (eg. manual testing)
 
30
        net_name = os.environ['OS_USERNAME'] + '_admin_net'
 
31
        print('Using default network name: {}'.format(net_name))
 
32
        try:
 
33
            network = neutron.list_networks(name=net_name)['networks'][0]
 
34
            net_id = network['id']
 
35
        except IndexError:
 
36
            print('Unable to find local network {}'.format(net_name))
 
37
            raise ValueError('Unable to find local network {}'.format(net_name))
 
38
 
 
39
    service = sys.argv[1]
 
40
 
 
41
    service_config = yaml.load(
 
42
        subprocess.check_output(['juju', 'status', service])
 
43
    )
 
44
 
 
45
    uuids = []
 
46
    for machine in service_config['machines']:
 
47
        uuids.append(service_config['machines'][machine]['instance-id'])
 
48
 
 
49
    ext_port = []
 
50
    if len(sys.argv) >= 3:
 
51
        ext_port = [sys.argv[2]]
 
52
 
 
53
    if len(uuids) > 0:
 
54
        for uuid in uuids:
 
55
            print("Attaching interface to instance {}".format(uuid))
 
56
            server = nova.servers.get(uuid)
 
57
            result = server.interface_attach(port_id=None,
 
58
                                             net_id=net_id,
 
59
                                             fixed_ip=None).to_dict()
 
60
            ext_port.append(result['mac_addr'])
 
61
 
 
62
    ports = " ".join(ext_port)
 
63
    print("Setting ext-port configuration on {} to {}".format(service, ports))
 
64
    subprocess.check_call(['juju', 'set', service, 'ext-port={}'.format(ports)])
 
65