This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nova.py
292 lines (244 loc) · 9.54 KB
/
nova.py
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
# coding: utf-8
import time
from fabkit import sudo, env, filer, api
from fablib.base import SimpleBase
import re
import utils
class Nova(SimpleBase):
def __init__(self, enable_services=['.*']):
self.data_key = 'nova'
self.data = {
'user': 'root',
'sudoers_cmd': 'ALL',
'debug': 'true',
'verbose': 'true',
'timeout_nbd': 1,
'is_nova-api': False,
'is_nova-compute': False,
'is_master': False,
}
default_services = [
'nova-api-uwsgi',
'nova-metadata-api-uwsgi',
'nova-placement-api-uwsgi',
'nova-scheduler',
'nova-conductor',
'nova-consoleauth',
'nova-novncproxy',
'nova-compute',
]
self.enable_nginx = False
self.packages = {
'CentOS Linux 7.*': [],
'Ubuntu 16.*': [],
}
self.services = []
for service in default_services:
for enable_service in enable_services:
if re.search(enable_service, service):
if service == 'nova-placement-api-uwsgi':
self.services.append('nginx')
self.enable_nginx = True
self.services.append(service)
break
if 'nova-api' in self.services:
self.data['is_nova-api'] = True
if 'nova-api-uwsgi' in self.services:
self.data['is_nova-api'] = True
if 'nova-novncproxy' in self.services:
self.packages['CentOS Linux 7.*'].append('novnc')
self.packages['Ubuntu 16.*'].append('novnc')
if 'nova-compute' in self.services:
self.data['is_nova-compute'] = True
self.services.extend([
'virtlogd',
'libvirtd',
'messagebus',
'nova-compute',
])
self.packages['CentOS Linux 7.*'].extend([
'vde2-2.3.2',
'qemu-2.9.0',
'libvirt-3.6.0',
'libvirt-python-3.6.0',
'sysfsutils',
'dbus',
'genisoimage',
])
self.packages['Ubuntu 16.*'].extend([
'qemu',
'libvirt-bin',
'python-libvirt',
'sysfsutils',
'dbus',
'genisoimage',
])
def init_before(self):
self.version = env.cluster[self.data_key]['version']
if self.version == 'master':
self.packages['CentOS Linux 7.*'].extend([
'nova-18.0.0.0b1',
])
self.packages['Ubuntu 16.*'].extend([
'nova-18.0.0.0b1',
])
elif self.version == 'pike':
self.packages['CentOS Linux 7.*'].extend([
'nova-16.0.0',
])
self.packages['Ubuntu 16.*'].extend([
'nova=16.0.0',
])
def init_after(self):
self.data.update({
'timeout_nbd': 1,
'sudoers_cmd': '/usr/bin/nova-rootwrap /etc/nova/rootwrap.conf *',
'lock_path': '/var/lock/subsys/nova',
'my_ip': env.node['ip']['default_dev']['ip'],
'vncserver_listen': env.node['ip']['default_dev']['ip'],
'vncserver_proxyclient_address': env.node['ip']['default_dev']['ip'],
'keystone': env.cluster['keystone'],
})
if env.host == env.hosts[0] and self.data['is_nova-api']:
self.data['is_master'] = True
else:
self.data['is_master'] = False
def setup(self):
data = self.init()
if self.is_tag('package'):
self.install_packages()
# for cinder
if not filer.exists('/usr/bin/scsi_id'):
sudo('ln -s /lib/udev/scsi_id /usr/bin/')
# if not filer.exists('/usr/bin/privsep-helper'):
# sudo('ln -s /opt/nova/bin/privsep-helper /usr/bin/')
# if not filer.exists('/usr/bin/nova-rootwrap'):
# sudo('ln -s /opt/nova/bin/nova-rootwrap /usr/bin/')
filer.template('/usr/lib/systemd/system/nova-api-uwsgi.service')
filer.template('/usr/lib/systemd/system/nova-metadata-api-uwsgi.service')
filer.template('/usr/lib/systemd/system/nova-placement-api-uwsgi.service')
sudo('systemctl daemon-reload')
if self.is_tag('conf'):
filer.template('/etc/sudoers.d/nova', data=data, src='sudoers')
self.setup_api_conf()
self.setup_metadata_api_conf()
self.setup_placement_api_conf()
if filer.template(
'/etc/nova/nova.conf',
src='{0}/nova/nova.conf'.format(data['version']),
data=data):
self.handlers['restart_nova'] = True
if self.data['is_nova-compute']:
if filer.template(
'/etc/libvirt/qemu.conf',
data=data):
self.handlers['restart_libvirtd'] = True
if self.is_tag('data') and env.host == env.hosts[0]:
if data['is_master']:
sudo('/opt/nova/bin/nova-manage db sync')
sudo('/opt/nova/bin/nova-manage api_db sync')
# cetup cell0
sudo('/opt/nova/bin/nova-manage cell_v2 list_cells | grep cell0 || /opt/nova/bin/nova-manage cell_v2 map_cell0')
sudo('/opt/nova/bin/nova-manage db sync')
sudo('/opt/nova/bin/nova-manage cell_v2 list_cells | grep cell1 || /opt/nova/bin/nova-manage cell_v2 create_cell --name cell1')
if self.is_tag('service'):
self.enable_services().start_services(pty=False)
self.exec_handlers()
if self.is_tag('data') and env.host == env.hosts[0]:
if data['is_master']:
self.sync_flavors()
return 0
def setup_api_conf(self):
data = self.data
data.update({
'httpd_port': data['api_port'],
'uwsgi_socket': '/var/run/nova-api-uwsgi.sock',
})
if filer.template(
'/etc/nova/nova-api-uwsgi.ini',
data=data,
):
self.handlers['restart_nova-api-uwsgi'] = True
if self.enable_nginx:
if filer.template(
'/etc/nginx/conf.d/nova-api-uwsgi.conf',
src='uwsgi-nginx.conf',
data=data,
):
self.handlers['restart_nginx'] = True
def setup_metadata_api_conf(self):
data = self.data
data.update({
'httpd_port': data['metadata_port'],
'uwsgi_socket': '/var/run/nova-metadata-api-uwsgi.sock',
})
if filer.template(
'/etc/nova/nova-metadata-api-uwsgi.ini',
data=data,
):
self.handlers['restart_nova-metadata-api-uwsgi'] = True
if self.enable_nginx:
if filer.template(
'/etc/nginx/conf.d/nova-metadata-api-uwsgi.conf',
src='uwsgi-nginx.conf',
data=data,
):
self.handlers['restart_nginx'] = True
def setup_placement_api_conf(self):
data = self.data
data.update({
'httpd_port': data['placement_port'],
'uwsgi_socket': '/var/run/nova-placement-api-uwsgi.sock',
})
if filer.template(
'/etc/nova/nova-placement-api-uwsgi.ini',
data=data,
):
self.handlers['restart_nova-placement-api-uwsgi'] = True
if self.enable_nginx:
if filer.template(
'/etc/nginx/conf.d/nova-placement-api-uwsgi.conf',
src='uwsgi-nginx.conf',
data=data,
):
self.handlers['restart_nginx'] = True
def cmd(self, cmd):
self.init()
return utils.oscmd('openstack {0}'.format(cmd))
def check(self):
self.nova_api.status()
self.nova_cert.status()
self.nova_consoleauth.status()
self.nova_scheduler.status()
self.nova_conductor.status()
self.nova_novncproxy.status()
def enable_nova_services(self):
return
result = sudo("/opt/nova/bin/nova-manage service list 2>/dev/null | grep disabled | awk '{print $1,$2}'")
services = result.split('\r\n')
services = map(lambda s: s.split(' '), services)
for service in services:
sudo("/opt/nova/bin/nova-manage service enable --service {0} --host {1}".format(
service[0], service[1]))
def sync_flavors(self):
data = self.init()
with api.warn_only():
ttl = 3
while True:
result = self.cmd("flavor list")
if result.return_code == 0:
break
time.sleep(10)
ttl -= 1
if ttl == 0:
raise Exception("nova is not available.")
result = self.cmd("flavor list 2>/dev/null | grep '| ' | grep -v '| ID' | awk '{print $4}'")
flavor_list = result.split('\r\n')
sub_set = set(flavor_list) - set(data['flavors'].keys())
for flavor_name in sub_set:
if len(flavor_name) == 0:
continue
self.cmd("flavor delete {0}".format(flavor_name))
for flavor_name, flavor in data['flavors'].items():
if flavor_name not in flavor_list:
self.cmd("flavor create {0} {1}".format(flavor['options'], flavor_name))