-
Notifications
You must be signed in to change notification settings - Fork 0
/
msl.py
executable file
·158 lines (117 loc) · 5.84 KB
/
msl.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
#!/usr/bin/env python3
import argparse
import yaml
import subprocess
import os
def read_yaml():
if os.path.exists('multipass.yaml'):
with open(r'multipass.yaml') as file:
instances = yaml.load(file, Loader=yaml.FullLoader)
return instances['instances']
else:
print("Can't find multipass.yaml in current directory. Please run msl init")
def current_instances():
return list(yaml.safe_load(subprocess.check_output("multipass info --all --format yaml", shell=True)).keys())
def virtual_instances(virtual_machines, config):
if len(virtual_machines) > 0:
virtual_machines = virtual_machines
else:
virtual_machines = list(config.keys())
return virtual_machines
def launch(current, config, virtual_machines):
launch_instances = []
for vm in virtual_machines:
if vm not in current and vm in config.keys():
cmd = f"multipass launch --name {vm} {' '.join([f'--{key} {value}' for key, value in config[vm].items() if key != 'mounts'])}"
if 'mounts' in config.keys():
mount_cmd = f" {' '.join([f'--mount {key}:{value}' for key, value in config[vm]['mounts'].items()])}"
launch_instances.append(cmd + mount_cmd)
else:
launch_instances.append(cmd)
else:
print(f'{vm} is not defined in multipass.yaml or exists already')
procs = [subprocess.Popen(instance, shell=True) for instance in launch_instances]
for p in procs:
p.wait()
def current_mounts(vm):
current = yaml.safe_load(subprocess.check_output("multipass info --format yaml --all", shell=True))
return {v['source_path']: k for k, v in current[vm][0]['mounts'].items()}
def config_mounts(vm, config):
if 'mounts' in config.keys():
return config[vm]['mounts']
else:
return {}
def mount(virtual_machines, config, current):
for vm in virtual_machines:
if vm in current:
mounted = current_mounts(vm)
mounts = config_mounts(vm, config)
for key, value in mounts.items():
if key in list(mounted.keys()):
if mounted[key] != value:
print(f'umount {mounted[key]} from {vm}')
subprocess.Popen(f'multipass umount {vm}:{mounted[key]}', shell=True)
print(f'mount {key} to {value} in to {vm}')
subprocess.Popen(f'multipass mount {key} {vm}:{value}', shell=True)
else:
if os.path.exists(key):
print(f'mount {key} to {value} in to {vm}')
subprocess.Popen(f'multipass mount {key} {vm}:{value}', shell=True)
else:
print(f'host directory {key} does not exists')
def init():
init_config = {"instances": {"primary": {"cpus": 1, "memory": "1G", "disk": "10G"}}}
if not os.path.exists('multipass.yaml'):
with open('multipass.yaml', 'w') as file:
yaml.dump(init_config, file)
else:
print('multipass.yaml already exists')
def stop(virtual_machines, current, stop_all):
if stop_all:
cmd = f"multipass stop --all"
else:
cmd = f"multipass stop {' '.join([f'{vm}' for vm in virtual_machines if vm in current])}"
subprocess.Popen(cmd, shell=True)
def delete(virtual_machines, current, delete_all):
if delete_all:
cmd = f"multipass delete --all"
else:
cmd = f"multipass delete {' '.join([f'{vm}' for vm in virtual_machines if vm in current])}"
subprocess.Popen(cmd, shell=True)
def parser():
arg_parser = argparse.ArgumentParser(description='msl: Multipass Subsystem Linux')
command_parser = arg_parser.add_subparsers(dest='command', help='sub-command help')
launch_parser = command_parser.add_parser('launch', help='Launch multiple Multipass Instances')
launch_parser.add_argument('launch', nargs='*', help='Instances to launch')
delete_parser = command_parser.add_parser('delete', help='Delete multiple Multipass Instances')
delete_parser.add_argument('--all', action='store_true', default=False,
help='switch to delete all virtual machines')
delete_parser.add_argument('delete', nargs='*', help='Instances to delete')
stop_parser = command_parser.add_parser('stop', help='Stop multiple Multipass Instances')
stop_parser.add_argument('--all', action='store_true', default=False, help='switch to stop all virtual machines')
stop_parser.add_argument('stop', nargs='*', help='Instances to stop')
mount_parser = command_parser.add_parser('mount', help='mount dictionaries to configured Multipass Instances')
mount_parser.add_argument('mount', nargs='*', help='Instances to stop')
init_parser = command_parser.add_parser('init', help='create multipass.yaml file in current directory')
args = arg_parser.parse_args()
return args
def main():
args = parser()
if args.command == "launch":
vms = virtual_instances(config=read_yaml(), virtual_machines=args.launch)
launch(current=current_instances(), config=read_yaml(), virtual_machines=vms)
elif args.command == "stop":
vms = virtual_instances(config=read_yaml(), virtual_machines=args.stop)
stop(virtual_machines=vms, current=current_instances(), stop_all=args.all)
elif args.command == "delete":
vms = virtual_instances(config=read_yaml(), virtual_machines=args.delete)
delete(virtual_machines=vms, current=current_instances(), delete_all=args.all)
elif args.command == "mount":
vms = virtual_instances(config=read_yaml(), virtual_machines=args.mount)
mount(current=current_instances(), config=read_yaml(), virtual_machines=vms)
elif args.command == "init":
init()
else:
print('Invalid command')
if __name__ == '__main__':
main()