aboutsummaryrefslogtreecommitdiffstats
path: root/meta-openstack/Documentation/testsystem/launch.py
blob: e1777734a0ed0f8cbbb12a40c1b07f953e16b8d4 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python

import sys
import grp
import pwd
import os
import libvirt
import ConfigParser
import subprocess
import shutil
import distutils.spawn
import platform

# this does a very basic test to see if the required packages
# are installed, extend list as required
def checkPackages():
        sys_ok = True
        check_apps = [ "virsh", "qemu-system-x86_64", "libvirtd" ]
        for app in check_apps:
                if distutils.spawn.find_executable(app) == None:
                        print( "Missing: " + app)
                        sys_ok = False
        if not sys_ok:
                print("The required libvirt/qemu packages are missing...")
                distro = platform.dist()[0]
                if distro == "debian" or distro == "Ubuntu":
                        print( "This appears to be a Debian/Ubuntu distribution\nPlease install " +
                                "packages like libvirt-bin, qemu-system-x86,..." )
                elif distro == "redhat" or distro == "fedora":
                        print( "This appears to be a Redhat/Fedora distribution\nPlease install " +
                                "packages like libvirt-client, libvirt-daemon, qemu-system-x86, ..." )
                exit(1)
        return

def networkInterfaces():
        ifaces = []
        for line in open('/proc/net/dev', 'r'):
                if_info = line.split(":", 1)
                if len(if_info) > 1:
                        ifaces.append( if_info[0].strip() )
        return ifaces

def destroyNetwork(conn, network_name):
        networks = conn.listNetworks() + conn.listDefinedNetworks()
        if network_name in networks:
                try:
                        nw = conn.networkLookupByName(network_name)
                        if nw.isActive():
                                nw.destroy()
                        nw.undefine()
                except:
                        print( "Failed to destroy network: %s" % network_name )
                        exit( 1 )

def restartDomain(conn, domain_name):
        try:
                domain = conn.lookupByName(domain_name)
        except:
                print( "restartDomain: Warning domain " + domain_name + " doesn't exist." )
                return
        if domain.isActive():
                domain.reboot()

def destroyDomain(conn, auto_destroy, domain_name):
        try:
                domain = conn.lookupByName(domain_name)
        except:
                return
        if domain.isActive():
                if auto_destroy:
                        print( "Auto destroy enabled, destroying old instance of domain %s" % domain_name )
                        domain.destroy()
                else:
                        print( "Domain %s is active, abort..." % domain_name )
                        print( "To stop:  virsh -c %s destroy %s " % ( uri , domain_name ) )
                        exit(0)
        domain.undefine()

def startDomain(conn, auto_destroy, domain_name, xml_desc):
        print( "Starting %s...\n%s" % ( domain_name, xml_desc ) )
        destroyDomain(conn, auto_destroy, domain_name)
        try:
                conn.defineXML(xml_desc)
                domain = conn.lookupByName(domain_name)
                domain.create()
                print( "Starting domain %s..." % domain_name )
                print( "To connect to the console:  virsh -c %s console %s" % ( uri, domain_name ) )
                print( "To stop:  virsh -c %s destroy %s" % ( uri, domain_name ) )
        except Exception as e:
                print( e )
                exit(1)

def make_nw_spec(network_name, bridge_nw_interface, network, auto_assign_ip):
        spec = '<network>'
        spec += '<name>' + network_name + '</name>'
        spec += '<bridge name="' + bridge_nw_interface + '"/>'
        spec += '<forward/>'
        spec += '<ip address="' + network + '" netmask="255.255.255.0">'
        if auto_assign_ip:
                nw_parts = network.split('.')
                nw_parts[-1] = "2"
                start_dhcp = '.'.join(nw_parts)
                nw_parts[-1] = "254"
                end_dhcp = '.'.join(nw_parts)
                spec += '<dhcp>'
                spec += '<range start="' + start_dhcp + '" end="' + end_dhcp + '"/>'
                spec += '</dhcp>'
        spec += '</ip>'
        spec += '</network>'
        return spec

def make_spec(name, network, kernel, disk, bridge_nw_interface, emulator, auto_assign_ip, ip):
        if not os.path.exists(kernel):
                print( "Kernel image %s does not exist!" % kernel )
                exit(1)
        if not os.path.exists(disk):
                print( "Disk %s does not exist!" % disk )
                exit(1)
        if auto_assign_ip:
                ip_spec = 'dhcp'
        else:
                ip_spec = ip + '::' + network + ':255.255.255.0:' + name + ':eth0:off'
        spec = '<domain type=\'kvm\'>'
        spec += '   <name>' + name + '</name>'
        spec += '   <memory>4096000</memory>'
        spec += '   <currentMemory>4096000</currentMemory>'
        spec += '   <vcpu cpuset=\'1\'>1</vcpu>'
        spec += '   <cpu>'
        spec += '     <model>kvm64</model>'
        spec += '   </cpu>'
        spec += '   <os>'
        spec += '     <type arch=\'x86_64\' machine=\'pc\'>hvm</type>'
        spec += '     <kernel>' + kernel + '</kernel>'
        spec += '     <boot dev=\'hd\'/>'
        spec += '     <cmdline>root=/dev/vda rw console=ttyS0 ip=' + ip_spec + '</cmdline>'
        spec += '   </os>'
        spec += '   <features>'
        spec += '     <acpi/>'
        spec += '     <apic/>'
        spec += '     <pae/>'
        spec += '   </features>'
        spec += '   <clock offset=\'utc\'/>'
        spec += '   <on_poweroff>destroy</on_poweroff>'
        # spec += '   <on_reboot>destroy</on_reboot>'
        spec += '   <on_crash>destroy</on_crash>'
        spec += '   <devices>'
        spec += '     <emulator>' + emulator + '</emulator>'
        spec += '     <disk type=\'file\' device=\'disk\'>'
        spec += '       <source file=\'' + disk + '\'/>'
        spec += '       <target dev=\'vda\' bus=\'virtio\'/>'
        spec += '     </disk>'
        spec += '     <interface type=\'bridge\'>'
        spec += '       <source bridge=\'' + bridge_nw_interface + '\'/>'
        spec += '       <model type=\'virtio\' />'
        spec += '     </interface>'
        spec += '     <serial type=\'pty\'>'
        spec += '      <target port=\'0\'/>'
        spec += '      <alias name=\'serial0\'/>'
        spec += '     </serial>'
        spec += '    <console type=\'pty\'>'
        spec += '      <target type=\'serial\' port=\'0\'/>'
        spec += '      <alias name=\'serial0\'/>'
        spec += '    </console>'
        spec += '   </devices>'
        spec += '</domain>'
        return spec

def getConfig(config, section, key):
        try:
                return os.path.expandvars(config.get(section, key))
        except:
                print( "Configuration file error! Missing item (section: %s, key: %s)" % ( section, key ) )
                exit(1)

# does the user have access to libvirt?
eligible_groups = [ "libvirt", "libvirtd" ]
eligible_user = False
euid = os.geteuid()
if euid == 0:
        eligible_user = True
else:
        username = pwd.getpwuid(euid)[0]
        groups = [g.gr_name for g in grp.getgrall() if username in g.gr_mem]
        for v in eligible_groups:
                if v in groups:
                        eligible_user = True

checkPackages()

if not eligible_user:
        sys.stderr.write("You need to be the 'root' user or in group [" + '|'.join(eligible_groups) + "] to run this script.\n")
        exit(1)

if len(sys.argv) != 3:
        sys.stderr.write("Usage: "+sys.argv[0]+" [config file] [start|stop|restart]\n")
        sys.exit(1)

if not os.path.exists(sys.argv[1]):
        sys.stderr.write("Error: config file \"" + sys.argv[1] + "\" was not found!\n")
        sys.exit(1)

command = sys.argv[2]
command_options = ["start", "stop", "restart"]
if not command in command_options:
        sys.stderr.write("Usage: "+sys.argv[0]+" [config file] [start|stop|restart]\n")
        sys.exit(1)

Config = ConfigParser.ConfigParser()
Config.read(sys.argv[1])

network_addr = getConfig(Config, "main", "network")
getConfig(Config, "main", "auto_destroy")
auto_destroy = Config.getboolean("main", "auto_destroy")
getConfig(Config, "main", "auto_assign_ip")
auto_assign_ip = Config.getboolean("main", "auto_assign_ip")
network_name = 'ops_default'
uri = 'qemu:///system'

# Connect to libvirt
conn = libvirt.open(uri)
if conn is None:
        print( "Failed to open connection to the hypervisor" )
        exit(1)

if command == "start":
        destroyNetwork(conn, network_name)

        # Change the default bridge device from virbr0 to virbr%d.
        # This will make libvirt try virbr0, virbr1, etc. until it finds a free one.
        cnt = 0
        ifaces = networkInterfaces()
        found_virbr = False
        while found_virbr == False:
                if cnt > 254:
                        print( "Giving up on looking for a free virbr network interface!" )
                        exit(1)
                bridge_nw_interface = 'virbr' + str(cnt)
                if bridge_nw_interface not in ifaces:
                        print( "bridge_nw_interface: %s" % bridge_nw_interface )
                        network_spec = make_nw_spec(network_name, bridge_nw_interface, network_addr, auto_assign_ip)
                        try:
                                conn.networkDefineXML(network_spec)
                                nw = conn.networkLookupByName(network_name)
                                nw.create()
                                found_virbr = True
                        except:
                                print( "Network Name: %s" % network_name )
                                destroyNetwork( conn, network_name )
                                print( "Error creating network interface" )
                cnt += 1
else:
        # verify network exists
        try:
                nw = conn.networkLookupByName(network_name)
        except:
                print( "Error! Virtual network " + network_name + " is not defined!" )
                exit(1)
        if not nw.isActive():
                print( "Error! Virtual network " + network_name + " is not running!" )
                exit(1)

emulator = getConfig(Config, "main", "emulator")
if not os.path.exists(emulator):
        print( "Emulator %s does not exist!" % emulator )
        exit(1)

controller_name = 'controller'
if command == "start":
        # Define the controller xml
        controller_kernel = getConfig(Config, "controller", "kernel")
        controller_disk = getConfig(Config, "controller", "disk")

        controller_ip = None
        if not auto_assign_ip:
                controller_ip = getConfig(Config, "controller", "ip")
        controller_spec = make_spec(controller_name, network_addr, controller_kernel,
                                controller_disk, bridge_nw_interface, emulator,
                                auto_assign_ip, controller_ip)

        # Now that network is setup let's actually run the virtual images
        startDomain(conn, auto_destroy, controller_name, controller_spec)
elif command == "stop":
        destroyDomain(conn, True, controller_name)
elif command == "restart":
        restartDomain(conn, controller_name)

for i in Config.sections():
        if i.startswith("compute"):
                if command == "start":
                        # Define the compute xml
                        kernel = getConfig(Config, i, "kernel")
                        disk = getConfig(Config, i, "disk")
                        compute_ip = None
                        if not auto_assign_ip:
                                compute_ip = getConfig(Config, i, "ip")
                        spec = make_spec(i, network_addr, kernel, disk, bridge_nw_interface,
                                        emulator, auto_assign_ip, compute_ip)
                        startDomain(conn, auto_destroy, i, spec)
                elif command == "stop":
                        destroyDomain(conn, True, i)
                elif command == "restart":
                        restartDomain(conn, i)

conn.close()