summaryrefslogtreecommitdiffstats
path: root/deploy/post/execute.py
blob: 7b0293a246b8d0e5b09444dd27f940b2fef105e5 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
##############################################################################
# Copyright (c) 2017 ZTE Coreporation and others.
# feng.xiaowei@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import os

import glance
import argparse
import yaml
import neutron
import nova
from deploy.config.network import NetworkConfig
from deploy.utils import err_exit


def _config_kolla_admin_openrc(kolla_config_path):
    with open('%s/globals.yml' % kolla_config_path, 'r') as f:
        kolla_config = yaml.safe_load(f.read())
    if kolla_config.get('enable_opendaylight', None) == 'yes':
        openrc_file = file('%s/admin-openrc.sh' % kolla_config_path, 'a')

        v = kolla_config.get('kolla_internal_vip_address')
        line = 'export SDN_CONTROLLER_IP=' + v + '\n'
        openrc_file.write(line)

        v = kolla_config.get('opendaylight_haproxy_restconf_port_backup')
        line = 'export SDN_CONTROLLER_WEBPORT=' + v + '\n'
        openrc_file.write(line)

        v = kolla_config.get('opendaylight_haproxy_restconf_port')
        line = 'export SDN_CONTROLLER_RESTCONFPORT=' + v + '\n'
        openrc_file.write(line)

        openrc_file.close()


def _config_external_network(ext_name, physnet):
    body = {
        'network': {
            'name': ext_name,
            'admin_state_up': True,
            'shared': False,
            'provider:network_type': 'flat',
            'provider:physical_network': physnet,
            'router:external': True
        }
    }

    return body


def _config_external_subnet(ext_id, network_conf):
    return {
        'subnets': [
            {
                'name': '{}_subnet'.format(network_conf.ext_network_name),
                'cidr': network_conf.ext_cidr,
                'ip_version': 4,
                'network_id': ext_id,
                'gateway_ip': network_conf.ext_gateway,
                'allocation_pools': network_conf.ext_ip_ranges,
                'enable_dhcp': False
            }
        ]
    }


def _create_external_network(network_file, openrc=None):
    network_conf = NetworkConfig(network_file=network_file)
    ext_name = network_conf.ext_network_name
    physnet = network_conf.ext_mapping if hasattr(network_conf, 'ext_mapping') else 'physnet1'
    neutronclient = neutron.Neutron(openrc=openrc)
    ext_id = neutronclient.create_network(ext_name,
                                          _config_external_network(ext_name, physnet))
    if ext_id:
        neutronclient.create_subnet(_config_external_subnet(ext_id, network_conf))


def _create_flavor_m1_micro(openrc=None):
    name = 'm1.micro'
    novaclient = nova.Nova(openrc=openrc)
    if not novaclient.get_flavor_by_name(name):
        try:
            return novaclient.create_flavor(name, ram=64, vcpus=1, disk=0)
        except Exception as error:
            print ('_create_flavor_m1_micro failed: {}'.format(str(error)))
    else:
        print ('Use existing m1.micro flavor')


def _prepare_cirros():
    url = 'https://download.cirros-cloud.net'
    version = '0.3.5'
    name = 'cirros-{}-x86_64-disk.img'.format(version)
    imgpath = "/var/lib/daisy/images"
    img = os.path.join(imgpath, name)
    imgmd5 = os.path.join(imgpath, "cirros.md5")

    if not os.path.isfile(img):
        cmd = "curl -sSL %(url)s/%(version)s/%(name)s -o %(path)s" % {
            'url': url,
            'version': version,
            'name': name,
            'path': img}
        print ('Downloading cirros: {}'.format(cmd))
        os.system(cmd)

        cmd = "curl -sSL %(url)s/%(version)s/MD5SUMS -o %(md5path)s" % {
            'url': url,
            'version': version,
            'md5path': imgmd5}
        print ('Downloading MD5SUM for cirros: {}'.format(cmd))
        os.system(cmd)

        cmd = "cd %(path)s && cat %(md5path)s | grep %(name)s | md5sum -c" % {
            'path': imgpath,
            'md5path': imgmd5,
            'name': name}
        print ('md5sum check cirros: {}'.format(cmd))
        ret = os.system(cmd)
        if ret != 0:
            img = None

    return img


def _create_image_TestVM(openrc=None):
    glanceclient = glance.Glance(openrc=openrc)
    image = 'TestVM'
    if not glanceclient.get_by_name(image):
        try:
            img = _prepare_cirros()
            if img:
                glanceclient.create(image, img)
            else:
                err_exit("Test image preparation failed")
        except Exception as error:
            err_exit('Create image failed: {}'.format(str(error)))
    else:
        print ('Use existing TestVM image')


def _config_icmp_security_group_rule(security_group_id):
    body = {
        'security_group_rule': {
            'direction': 'ingress',
            'ethertype': 'IPv4',
            'protocol': 'icmp',
            'remote_ip_prefix': '0.0.0.0/0',
            'security_group_id': security_group_id
        }
    }
    return body


def _config_ssh_security_group_rule(security_group_id):
    body = {
        'security_group_rule': {
            'direction': 'ingress',
            'ethertype': 'IPv4',
            'protocol': 'tcp',
            'port_range_min': 22,
            'port_range_max': 22,
            'remote_ip_prefix': '0.0.0.0/0',
            'security_group_id': security_group_id
        }
    }
    return body


def _create_security_group_rules(openrc=None):
    neutronclient = neutron.Neutron(openrc=openrc)
    try:
        security_group_name = 'default'
        security_group = neutronclient.get_security_group_by_name(security_group_name)
        security_group_id = security_group['id']
    except Exception:
        print('Cannot find security group by name %s' % security_group_name)
        return

    neutronclient.create_security_group_rule(security_group,
                                             _config_icmp_security_group_rule(security_group_id))
    neutronclient.create_security_group_rule(security_group,
                                             _config_ssh_security_group_rule(security_group_id))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-nw', '--network-file',
                        type=str,
                        required=True,
                        help='network configuration file')
    args = parser.parse_args()
    _create_external_network(args.network_file)
    _create_flavor_m1_micro()
    _create_image_TestVM()
    _create_security_group_rules()
    _config_kolla_admin_openrc('/etc/kolla/')


if __name__ == '__main__':
    main()