-
Notifications
You must be signed in to change notification settings - Fork 364
/
utils.py
317 lines (273 loc) · 10 KB
/
utils.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#
# Project Kimchi
#
# Copyright IBM Corp, 2015-2016
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
import contextlib
import json
import os
import re
import sqlite3
import stat
import time
import urllib
from http.client import HTTPConnection
from http.client import HTTPException
from wok.exception import InvalidParameter
from wok.exception import OperationFailed
from wok.plugins.kimchi import config
from wok.plugins.kimchi.osinfo import get_template_default
from wok.stringutils import encode_value
from wok.utils import run_command
from wok.utils import wok_log
from wok.xmlutils.utils import xpath_get_text
MAX_REDIRECTION_ALLOWED = 5
def _uri_to_name(collection, uri):
expr = '/plugins/kimchi/%s/(.*?)$' % collection
m = re.match(expr, uri)
if not m:
raise InvalidParameter('WOKUTILS0001E', {'uri': uri})
return m.group(1)
def template_name_from_uri(uri):
return _uri_to_name('templates', uri)
def pool_name_from_uri(uri):
return _uri_to_name('storagepools', uri)
def check_url_path(path, redirected=0):
if redirected > MAX_REDIRECTION_ALLOWED:
return False
try:
parse_result = urllib.parse.urlparse(path)
server_name = parse_result.netloc
urlpath = parse_result.path
if not urlpath:
# Just a server, as with a repo.
with contextlib.closing(urllib.request.urlopen(path)) as res:
code = res.getcode()
else:
# socket.gaierror could be raised,
# which is a child class of IOError
conn = HTTPConnection(server_name, timeout=15)
# Don't try to get the whole file:
conn.request('HEAD', path)
response = conn.getresponse()
code = response.status
conn.close()
if code == 200:
return True
elif code == 301 or code == 302:
for header in response.getheaders():
if header[0] == 'location':
return check_url_path(header[1], redirected + 1)
else:
return False
except (urllib.error.URLError, HTTPException, IOError, ValueError):
return False
return True
def upgrade_objectstore_data(item, old_uri, new_uri):
"""
Upgrade the value of a given JSON's item of all Template and VM entries
of the objectstore from old_uri to new_uri.
"""
total = 0
try:
conn = sqlite3.connect(config.get_object_store(), timeout=10)
cursor = conn.cursor()
sql = "SELECT id, json FROM objects WHERE type='template' OR type='vm'"
cursor.execute(sql)
for row in cursor.fetchall():
# execute update here
template = json.loads(row[1])
path = template[item] if item in template else 'none'
if path.startswith(old_uri):
template[item] = new_uri + path
sql = 'UPDATE objects SET json=?, version=? WHERE id=?'
cursor.execute(
sql, (json.dumps(template),
config.get_kimchi_version(), row[0])
)
conn.commit()
total += 1
except sqlite3.Error as e:
if conn:
conn.rollback()
wok_log.error('Error while upgrading objectstore data: %s', e.args[0])
raise OperationFailed('KCHUTILS0006E')
finally:
if conn:
conn.close()
wok_log.info("%d '%s' entries upgraded in objectstore.", total, item)
def upgrade_objectstore_template_disks(libv_conn):
"""
Upgrade the value of a given JSON's item of all Templates.
Removes 'storagepool' entry and adds
'pool: { name: ..., type: ... }'
"""
total = 0
try:
conn = sqlite3.connect(config.get_object_store(), timeout=10)
cursor = conn.cursor()
sql = "SELECT id, json FROM objects WHERE type='template'"
cursor.execute(sql)
for row in cursor.fetchall():
template = json.loads(row[1])
# Get pool info
pool_uri = template['storagepool']
pool_name = pool_name_from_uri(pool_uri)
pool = libv_conn.get().storagePoolLookupByName(pool_name.encode('utf-8'))
pool_type = xpath_get_text(pool.XMLDesc(0), '/pool/@type')[0]
# Update json
new_disks = []
for disk in template['disks']:
disk['pool'] = {'name': pool_uri, 'type': pool_type}
new_disks.append(disk)
template['disks'] = new_disks
del template['storagepool']
sql = 'UPDATE objects SET json=? WHERE id=?'
cursor.execute(sql, (json.dumps(template), row[0]))
conn.commit()
total += 1
except sqlite3.Error as e:
if conn:
conn.rollback()
wok_log.error('Error while upgrading objectstore data: %s', e.args[0])
raise OperationFailed('KCHUTILS0006E')
finally:
if conn:
conn.close()
wok_log.info("%d 'template' entries upgraded in objectstore.", total)
def upgrade_objectstore_memory():
"""
Upgrade the value of a given JSON's item of all Templates.
Changes 'memory': XXX by 'memory': {'current': XXXX,
'maxmemory': XXXX}
"""
total = 0
try:
conn = sqlite3.connect(config.get_object_store(), timeout=10)
cursor = conn.cursor()
sql = "SELECT id,json FROM objects WHERE type='template'"
cursor.execute(sql)
for row in cursor.fetchall():
template = json.loads(row[1])
# Get memory info
memory = template['memory']
# New memory is a dictionary with 'current' and 'maxmemory'
if type(memory) is not dict:
maxmem = get_template_default(
'modern', 'memory').get('maxmemory')
if maxmem < memory:
maxmem = memory
template['memory'] = {'current': memory, 'maxmemory': maxmem}
else:
continue
sql = 'UPDATE objects SET json=? WHERE id=?'
cursor.execute(sql, (json.dumps(template), row[0]))
conn.commit()
total += 1
except sqlite3.Error as e:
if conn:
conn.rollback()
wok_log.error('Error while upgrading objectstore data: %s', e.args[0])
raise OperationFailed('KCHUTILS0006E')
finally:
if conn:
conn.close()
if total > 0:
wok_log.info(
"%d 'template' memory entries upgraded in objectstore.", total)
def get_next_clone_name(all_names, basename, name_suffix='', ts=False):
"""Find the next available name for a cloned resource.
If any resource named "<basename>-clone-<number><name_suffix>" is found
in "all_names", use the maximum "number" + 1; else, use 1.
Arguments:
all_names -- All existing names for the resource type. This list will
be used to make sure the new name won't conflict with
existing names.
basename -- The name of the original resource.
name_suffix -- The resource name suffix (optional). This parameter
exist so that a resource named "foo.img" gets the name
"foo-clone-1.img" instead of "foo.img-clone-1". If this parameter
is used, the suffix should not be present in "basename".
ts -- use timestamp, instead of incremental numbers
Return:
A UTF-8 string in the format "<basename>-clone-<number><name_suffix>".
"""
re_group_num = 'num'
# Use timestamps or increase clone number
if ts:
# Microsecond precision
ts_suffix = int(time.time() * 1000000)
new_name = u'%s-clone-%d' % (basename, ts_suffix)
else:
re_expr = u'%s-clone-(?P<%s>\\d+)' % (basename, re_group_num)
if name_suffix != '':
re_expr = u'%s%s' % (re_expr, name_suffix)
max_num = 0
re_compiled = re.compile(re_expr)
for n in all_names:
match = re_compiled.match(n)
if match is not None:
max_num = max(max_num, int(match.group(re_group_num)))
# increments the maximum "clone number" found
new_name = u'%s-clone-%d' % (basename, max_num + 1)
if name_suffix != '':
new_name = new_name + name_suffix
return new_name
def is_libvirtd_up():
"""
Checks if libvirt is up.
"""
path = os.path.join(config.get_libvirt_path(), 'libvirt-sock')
try:
mode = os.stat(path).st_mode
if stat.S_ISSOCK(mode):
return True
except Exception:
pass
return False
def is_s390x():
"""
Check if current arch is 's390x'
Returns:
"""
if os.uname()[4] == 's390x':
return True
return False
def create_disk_image(format_type, path, capacity):
"""
Create a disk image for the Guest
Args:
format: Format of the storage. e.g. qcow2
path: Path where the virtual disk will be created
capacity: Capacity of the virtual disk in GBs
Returns:
"""
out, err, rc = run_command(
[
'/usr/bin/qemu-img',
'create',
'-f',
format_type,
'-o',
'preallocation=metadata',
path,
encode_value(capacity) + 'G',
]
)
if rc != 0:
raise OperationFailed('KCHTMPL0041E', {'err': err})
return