-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
116 lines (96 loc) · 3.21 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
# Copyright (c) 2019 Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import yaml
import math
# from os import listdir
# from os.path import isfile, join
import os
from glob import glob
from lxml import etree
from multiprocessing import Process
import subprocess
def read_ids_from_file(id_file):
with open(id_file, 'r') as fimage:
list_ids = fimage.readlines()
#print list_ids
fimage.closed
ids = []
for i in list_ids:
if i and i.strip():
uuid = i.split()[0]
#print uuid
ids.append(uuid)
return ids
def read_ids_with_hyps_file(id_file):
with open(id_file, 'r') as fimage:
list_ids = fimage.readlines()
fimage.closed
ids = []
for i in list_ids:
if i and i.strip():
uuid = i.split()[0]
hyp = i.split()[1]
pair = {'uuid': uuid, 'hyp': hyp}
ids.append(pair)
return ids
def load(filename):
"""Load a dictionary from a yaml file.
Expects the file at filename to be a yaml file.
Returns the parsed configuration as a dictionary.
:param filename: Name of the file
:type filename: String
:return: Loaded configuration
:rtype: Dict
"""
with open(filename, 'r') as f:
return yaml.safe_load(f)
def get_nfs_location(dir, volume_id):
# for f in listdir(dir):
# if isfile(join(dir, f)):
# print f
# else:
# for root, d_names, f_names in os.walk(dir):
# print root, d_names, f_names
fname = []
for root, d_names, f_names in os.walk(dir):
for f in f_names:
if volume_id == f:
#print os.path.join(root, f)
# print root
# print f
return root
#will round up to the nearest gb:
#base 1024
def convert_B_to_GB(bytes):
gig = math.pow(1024, 3)
convert_gb = math.ceil(bytes / gig)
return int(convert_gb)
# Get macs from libvirt file. Needed for proper nic ordering on boot.
def get_macs_from_libvirt(full_path):
print "libvirt in: " + full_path
tree = etree.parse(full_path + '/libvirt.xml')
root = tree.getroot()
interfaces = root.findall('./devices/interface/mac')
macs = []
for interface in interfaces:
macs.append(interface.get('address'))
# print macs
return macs
def copy_file(full_path, file_name, new_name):
print "Copying " + full_path + file_name + " to " + full_path + new_name
# copy2(full_path + file_name, full_path + new_name)
subprocess.call(["cp", "--sparse=always", full_path + file_name, full_path + new_name])
def start_copy_process(full_path, file_name, new_name):
p = Process(target=copy_file, args=(full_path, file_name, new_name))
p.start()