~1chb1n/charms/trusty/nova-cloud-controller/15.10-stable-flip-tests-helper-syncs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python

try:
  from quantumclient.v2_0 import client
except ImportError:
  from neutronclient.v2_0 import client

from keystoneclient.v2_0 import client as ks_client
import optparse
import os
import sys
import logging

usage = """Usage: %prog [options] name cidr

For example:

  %prog -t admin -r provider-router admin_net 10.5.5.0/24

will create a new network for the admin tenant called 'admin_net' with a
default gateway of 10.5.5.1 and a dhcp allocation range of
10.5.5.2->10.5.5.254
"""

if __name__ == '__main__':
    parser = optparse.OptionParser(usage)
    parser.add_option('-t', '--tenant',
                      help='Tenant name to create network for',
                      dest='tenant', action='store',
                      default=None)
    parser.add_option('-s', '--shared',
                      help='Create a shared rather than private network',
                      dest='shared', action='store_true', default=False)
    parser.add_option('-r', '--router',
                      help='Router to plug new network into',
                      dest='router', action='store', default=None)
    parser.add_option("-d", "--debug",
                      help="Enable debug logging",
                      dest="debug", action="store_true",  default=False)
    parser.add_option("-D", "--disable-dhcp",
                      help="Disable dhcp on network",
                      dest="dhcp", action="store_false",  default=True)
    parser.add_option("-N", "--dns-nameservers",
                      help="Comma separated list of dns servers to use.",
                      dest="dns_servers", action="store",  default=None)
    (opts, args) = parser.parse_args()

    if len(args) != 2:
        parser.print_help()
        sys.exit(1)

    if opts.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    net_name = args[0]
    subnet_name = "{}_subnet".format(net_name)
    cidr = args[1]

    keystone = ks_client.Client(username=os.environ['OS_USERNAME'],
                                password=os.environ['OS_PASSWORD'],
                                tenant_name=os.environ['OS_TENANT_NAME'],
                                auth_url=os.environ['OS_AUTH_URL'],
                                region_name=os.environ['OS_REGION_NAME'])
    quantum = client.Client(username=os.environ['OS_USERNAME'],
                            password=os.environ['OS_PASSWORD'],
                            tenant_name=os.environ['OS_TENANT_NAME'],
                            auth_url=os.environ['OS_AUTH_URL'],
                            region_name=os.environ['OS_REGION_NAME'])

    # Resolve tenant id
    tenant_id = None
    for tenant in [t._info for t in keystone.tenants.list()]:
        if (tenant['name'] ==
            (opts.tenant or os.environ['OS_TENANT_NAME'])):
            tenant_id = tenant['id']
            break  # Tenant ID found - stop looking
    if not tenant_id:
        logging.error("Unable to locate tenant id for %s.", opts.tenant)
        sys.exit(1)

    # Create network
    networks = quantum.list_networks(name=net_name)
    if len(networks['networks']) == 0:
        logging.info('Creating network: %s',
                     net_name)
        network_msg = {
            'network': {
                'name': net_name,
                'shared': opts.shared,
                'tenant_id': tenant_id
            }
        }
        network = quantum.create_network(network_msg)['network']
    else:
        logging.warning('Network %s already exists.', net_name)
        network = networks['networks'][0]

    # Create subnet
    subnets = quantum.list_subnets(name=subnet_name)
    if len(subnets['subnets']) == 0:
        logging.info('Creating subnet for %s',
                     net_name)
        subnet_msg = {
            'subnet': {
                'name': subnet_name,
                'network_id': network['id'],
                'enable_dhcp': opts.dhcp,
                'cidr': cidr,
                'ip_version': 4,
                'tenant_id': tenant_id
            }
        }
        subnet = quantum.create_subnet(subnet_msg)['subnet']
    else:
        logging.warning('Subnet %s already exists.', subnet_name)
        subnet = subnets['subnets'][0]

    # Update dns_nameservers
    if opts.dns_servers:
        msg = {
            'subnet': {
                'dns_nameservers': opts.dns_servers.split(',')
            }
        }
        logging.info('Updating dns_nameservers (%s) for subnet %s',
                     opts.dns_servers,
                     subnet_name)
        quantum.update_subnet(subnet['id'], msg)

    # Plug subnet into router if provided
    if opts.router:
        routers = quantum.list_routers(name=opts.router)
        if len(routers['routers']) == 0:
            logging.error('Unable to locate provider router %s', opts.router)
            sys.exit(1)
        else:
            # Check to see if subnet already plugged into router
            ports = quantum.list_ports(device_owner='network:router_interface',
                                       network_id=network['id'])
            if len(ports['ports']) == 0:
                logging.info('Adding interface from %s to %s',
                             opts.router, subnet_name)
                router = routers['routers'][0]
                quantum.add_interface_router(router['id'],
                                             {'subnet_id': subnet['id']})
            else:
                logging.warning('Router already connected to subnet')