Skip to content

Commit 9b05d1f

Browse files
committed
Add volume/iso9660 implementation
1 parent 3fa35c5 commit 9b05d1f

4 files changed

Lines changed: 400 additions & 1 deletion

File tree

cloudify_libvirt/iso9660_tasks.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
########
2+
# Copyright (c) 2016-2018 GigaSpaces Technologies Ltd. All rights reserved
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import libvirt
17+
import pycdlib
18+
import os
19+
import re
20+
from io import BytesIO
21+
22+
from cloudify import ctx
23+
from cloudify.decorators import operation
24+
from cloudify import exceptions as cfy_exc
25+
import cloudify_libvirt.common as common
26+
27+
28+
def _joliet_name(name):
29+
if name[0] == "/":
30+
name = name[1:]
31+
return "/{}".format(name[:64])
32+
33+
34+
def _name_cleanup(name):
35+
return re.sub('[^A-Z0-9_]{1}', r'_', name.upper())
36+
37+
38+
def _iso_name(name):
39+
if name[0] == "/":
40+
name = name[1:]
41+
42+
name_splited = name.split('.')
43+
if len(name_splited[-1]) <= 3 and len(name_splited) > 1:
44+
return "/{}.{};1".format(
45+
_name_cleanup("_".join(name_splited[:-1])[:8]),
46+
_name_cleanup(name_splited[-1]))
47+
else:
48+
return "/{}.;1".format(_name_cleanup(name[:8]))
49+
50+
51+
@operation
52+
def create(**kwargs):
53+
ctx.logger.info("Creating new iso image.")
54+
55+
libvirt_auth, template_params = common.get_libvirt_params(**kwargs)
56+
conn = libvirt.open(libvirt_auth)
57+
if conn is None:
58+
raise cfy_exc.NonRecoverableError(
59+
'Failed to open connection to the hypervisor'
60+
)
61+
62+
try:
63+
# lookup the default volume by name
64+
try:
65+
pool = conn.storagePoolLookupByName(template_params["pool"])
66+
volume = pool.storageVolLookupByName(template_params["volume"])
67+
except libvirt.libvirtError as e:
68+
raise cfy_exc.NonRecoverableError(
69+
'Failed to find the volume: {}'.format(repr(e))
70+
)
71+
72+
iso = pycdlib.PyCdlib()
73+
iso.new(vol_ident='cidata', joliet=3, rock_ridge='1.09')
74+
75+
fstree = template_params.get('files', {})
76+
for name in fstree:
77+
file_bufer = BytesIO()
78+
file_bufer.write(fstree[name].encode())
79+
iso.add_fp(file_bufer, len(fstree[name]),
80+
_iso_name(name), rr_name=name,
81+
joliet_path=_joliet_name(name))
82+
83+
outiso = BytesIO()
84+
iso.write_fp(outiso)
85+
outiso.seek(0, os.SEEK_END)
86+
iso_size = outiso.tell()
87+
iso.close()
88+
89+
ctx.logger.info("ISO size: {}".format(repr(iso_size)))
90+
91+
stream = conn.newStream(0)
92+
volume.upload(stream, 0, iso_size, 0)
93+
outiso.seek(0, os.SEEK_SET)
94+
95+
read_size = iso_size
96+
while read_size > 0:
97+
buffer = outiso.read(read_size)
98+
read_size -= len(buffer)
99+
stream.send(buffer)
100+
stream.finish()
101+
102+
finally:
103+
conn.close()

cloudify_libvirt/pool_tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def stop(**kwargs):
202202
ctx.logger.info("Looks as not active.")
203203
break
204204

205-
ctx.logger.info("Tring to stop vm {}/10".format(i))
205+
ctx.logger.info("Tring to stop pool {}/10".format(i))
206206
if pool.destroy() < 0:
207207
raise cfy_exc.NonRecoverableError(
208208
'Can not destroy pool.'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<volume>
2+
<name>{{ name }}</name>
3+
<uuid>{{ instance_uuid }}</uuid>
4+
<allocation unit='MiB'>{{ allocation }}</allocation>
5+
<capacity unit='MiB'>{{ capacity }}</capacity>
6+
<target>
7+
<permissions>
8+
<owner>107</owner>
9+
<group>107</group>
10+
<mode>0744</mode>
11+
<label>virt_image_t</label>
12+
</permissions>
13+
</target>
14+
</volume>

0 commit comments

Comments
 (0)