-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_vm_packages
executable file
·66 lines (56 loc) · 1.49 KB
/
get_vm_packages
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
#!/usr/bin/env python3.9
#v11.0.7
# http://www.i-programmer.info/programming/python/3942-arrays-in-python.html?start=1
import re
import json
import os
import subprocess
import sys
def parse_vm_packages(stdout):
"""
Pasre cbsd vm-packages output
"""
arr_all_vm_packages = []
vm_packages = []
for line in stdout.splitlines():
name=""
vm_cpus=""
vm_ram=""
vm_disk=""
name, vm_cpus, vm_ram, vm_disk = line.split(' ')
if name != "" and vm_cpus != "" and vm_ram != "" and vm_disk != "":
vm_packages = {
"name": name,
"vm_cpus": vm_cpus,
"vm_ram": vm_ram,
"vm_disk": vm_disk,
}
arr_all_vm_packages.append(vm_packages)
return arr_all_vm_packages
def get_vm_info():
"""
Run: cbsd vm-packages header=0 display=name,pkg_vm_cpus,pkg_vm_ram,pkg_vm_disk
Example: small1 1 1g 10g
"""
command = [
'env',
'NOCOLOR=1',
'/usr/local/bin/cbsd',
'vm-packages',
'header=0',
'display=name,pkg_vm_cpus,pkg_vm_ram,pkg_vm_disk',
'human=0',
]
process = subprocess.Popen(
command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
output = process.communicate()[0].decode('ascii').strip()
if process.returncode not in [0, 1]:
raise RuntimeError('cbsd vm-packages error {0}'.format(process.returncode))
output = re.sub("\s\s+", " ", output)
vm_packages=parse_vm_packages(output)
print(json.dumps(vm_packages))
get_vm_info()