-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_bhyve_profiles
executable file
·203 lines (169 loc) · 5.39 KB
/
compile_bhyve_profiles
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
#!/usr/bin/env python3.9
#v11.0.7
import json
import os
import argparse
import subprocess
from subprocess import check_output
import sys
RCCONF_FILE = "/etc/rc.conf"
def get_rcconf_var(var):
result = check_output(
". {file}; echo ${var_name}".format(
file=RCCONF_FILE,
var_name=var), shell=True).decode().rstrip()
return result
def get_vm_info(path,vm_filter):
if os.path.exists(path):
cmd = subprocess.Popen(['/usr/bin/egrep', '-E', vm_filter, path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return (cmd.communicate()[0])
else:
return ''
def get_profile_list(workdir):
if not workdir:
workdir = get_rcconf_var("cbsd_workdir")
if not workdir:
print("Empty workdir")
sys.exit(0)
cmd = subprocess.Popen(['/usr/bin/find',workdir + '/etc/defaults/','-maxdepth','1','-depth','1','-type','f','-name','vm\-*\.conf' ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return (cmd.communicate()[0])
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_args():
parser = argparse.ArgumentParser(description='show bhyve profiles')
parser.add_argument('--cloud', type=str2bool, nargs='?', const=True, help='show cloud profiles')
parser.add_argument('--workdir', type=str, nargs='?', const=True, help='CBSD workdir')
parser.add_argument('--is_clonos', type=str2bool, nargs='?', const=True, help='show templates marked for ClonOS')
parser.add_argument('--template', type=str2bool, nargs='?', const=True, help='show template')
parser.add_argument('--vm', type=str2bool, nargs='?', const=True, help='show generic vm')
return parser.parse_args()
def human_readable_to_bytes(size):
"""Given a human-readable byte string (e.g. 2G, 10GB, 30MB, 20KB),
return the number of bytes. Will return 0 if the argument has
unexpected form.
"""
if (size[-1] == 'B'):
size = size[:-1]
if (size.isdigit()):
bytes = int(size)
else:
bytes = size[:-1]
unit = size[-1]
if (bytes.isdigit()):
bytes = int(bytes)
if (unit == 'G'):
bytes *= 1073741824
elif (unit == 'M'):
bytes *= 1048576
elif (unit == 'K'):
bytes *= 1024
else:
bytes = 0
else:
bytes = 0
return bytes
def main(args):
_locals = locals()
profile_list = get_profile_list(args.workdir)
# check for variable exist
try:
profile_list
except NameError:
print ("No profiles")
exit
vm_filter=""
clonos=0
if(args.cloud):
vm_filter="(^long_description=|^vm_profile=|^vm_os_type=|^clonos_active=|^is_template=|^is_cloud=|^imgsize=|^imgsize_min=|^default_jailname=)"
tpl=0
cloud=1
vm=0
if(args.is_clonos):
clonos=1
if(args.template):
vm_filter="(^long_description=|^vm_profile=|^vm_os_type=|^clonos_active=|^is_template=|^imgsize=|^imgsize_min=|^default_jailname=)"
tpl=1
cloud=0
vm=0
if(args.vm):
vm_filter="(^long_description=|^vm_profile=|^vm_os_type=|^clonos_active=|^is_template=|^imgsize=|^imgsize_min=|^default_jailname=)"
tpl=0
cloud=0
vm=1
if ( vm_filter=="" ):
print("specify --cloud, --vm or --template [--is_clonos get image marked for ClonOS]")
sys.exit(0)
arr_all_profiles=[]
for myvm in profile_list.split():
_locals['is_template']=0
_locals['is_cloud']=0
_locals['clonos_active']=0
_locals['vm_os_type']=""
_locals['vm_profile']=""
_locals['vm_description']=""
_locals['imgsize']=0
_locals['imgsize_min']=0
_locals['default_jailname']="vm"
clonos_active=0
is_template=0
is_cloud=0
vm_profile=""
vm_os_type=""
imgsize=0
imgsize_min=0
long_description=""
temp_vm_os=""
arr_profile=[]
temp_vm_os = get_vm_info(myvm,vm_filter).decode().rstrip()
if (temp_vm_os):
exec(temp_vm_os, globals(), _locals )
try:
clonos_active=_locals['clonos_active']
except:
continue
if ( clonos==1 and clonos_active != 1 ):
continue
try:
long_description = _locals['long_description']
vm_profile = _locals['vm_profile']
vm_os_type = _locals['vm_os_type']
imgsize = _locals['imgsize']
imgsize_min = _locals['imgsize_min']
default_jailname = _locals['default_jailname']
is_template = _locals['is_template']
except:
continue
if ( tpl==1 and is_template==0 ):
continue
if ( cloud==1 and is_cloud==0 ):
continue
if ( vm==1 and is_template==1 ):
continue
if ( type(imgsize) != int ):
imgsize = human_readable_to_bytes(imgsize)
if ( type(imgsize) != int ):
imgsize = 0
if ( type(imgsize_min) != int ):
imgsize_min = human_readable_to_bytes(imgsize_min)
if ( type(imgsize_min) != int ):
imgsize_min = 0
# if (int(imgsize_min) > 0 ):
# arr_profile['imgsize_min']=int(imgsize_min)
arr_profile = {
"name": long_description,
"profile": vm_profile,
"type": vm_os_type,
"default_name": default_jailname,
"imgsize": imgsize,
"imgsize_min": imgsize_min
}
arr_all_profiles.append(arr_profile)
arr_all_profiles = sorted(arr_all_profiles, key=lambda k: k['name'], reverse=False)
print(json.dumps(arr_all_profiles,sort_keys=False))
if __name__ == '__main__':
main(get_args())