-
Notifications
You must be signed in to change notification settings - Fork 3
/
multipass.py
136 lines (134 loc) · 4.9 KB
/
multipass.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
import subprocess
from haikunator import Haikunator
import os
import json
class MultipassVM:
def __init__(self, vm_name, multipass_cmd):
self.cmd = multipass_cmd
self.vm_name = vm_name
def info(self):
cmd = [self.cmd, "info", self.vm_name, "--format", "json"]
out = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
exitcode = out.wait()
if(not exitcode == 0):
raise Exception("Multipass info command failed: {0}".format(stderr))
return json.loads(stdout)
def delete(self):
cmd = [self.cmd, "delete", self.vm_name]
try:
subprocess.check_output(cmd)
except:
raise Exception("Error deleting Multipass VM {0}".format(vm_name))
def shell(self):
raise Exception("The shell command is not supported in the Multipass SDK. Consider using exec.")
def exec(self, cmd_to_execute):
cmd = [self.cmd, "exec", self.vm_name]
cmd += cmd_to_execute.split(" ")
out = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
exitcode = out.wait()
if(not exitcode == 0):
raise Exception("Multipass exec command failed: {0}".format(stderr))
return stdout, stderr
def stop(self):
cmd = [self.cmd, "stop", self.vm_name]
try:
subprocess.check_output(cmd)
except:
raise Exception("Error stopping Multipass VM {0}".format(vm_name))
def start(self):
cmd = [self.cmd, "start", self.vm_name]
try:
subprocess.check_output(cmd)
except:
raise Exception("Error starting Multipass VM {0}".format(vm_name))
def restart(self):
cmd = [self.cmd, "restart", self.vm_name]
try:
subprocess.check_output(cmd)
except:
raise Exception("Error restarting Multipass VM {0}".format(vm_name))
class MultipassClient:
"""
Multipass client
"""
def __init__(self, multipass_cmd="multipass"):
self.cmd = multipass_cmd
def launch(self, vm_name=None, cpu=1, disk="5G", mem="1G", image=None, cloud_init=None):
if(not vm_name):
# similar to Multipass's VM name generator
vm_name = Haikunator().haikunate(token_length=0)
cmd = [self.cmd, "launch", "-c", str(cpu), "-d", disk, "-n", vm_name, "-m", mem]
if(cloud_init):
cmd.append("--cloud-init")
cmd.append(cloud_init)
if(image and not image == "ubuntu-lts"):
cmd.append(image)
try:
subprocess.check_output(cmd)
except:
raise Exception("Error launching Multipass VM {0}".format(vm_name))
return MultipassVM(vm_name, self.cmd)
def transfer(self, src, dest):
cmd = [self.cmd, "transfer", src, dest]
try:
subprocess.check_output(cmd)
except:
raise Exception("Multipass transfer command failed.")
def get_vm(self, vm_name):
return MultipassVM(vm_name, self.cmd)
def purge(self):
cmd = [self.cmd, "purge"]
try:
subprocess.check_output(cmd)
except:
raise Exception("Purge command failed.")
def list(self):
cmd = [self.cmd, "list", "--format", "json"]
out = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
exitcode = out.wait()
if(not exitcode == 0):
raise Exception("Multipass list command failed: {0}".format(stderr))
return json.loads(stdout)
def find(self):
cmd = [self.cmd, "find", "--format", "json"]
out = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = out.communicate()
exitcode = out.wait()
if(not exitcode == 0):
raise Exception("Multipass find command failed: {0}".format(stderr))
return json.loads(stdout)
def mount(self, src, target):
cmd = [self.cmd, "mount", src, target]
try:
subprocess.check_output(cmd)
except:
raise Exception("Multipass mount command failed.")
def unmount(self, mount):
cmd = [self.cmd, "unmount", mount]
try:
subprocess.check_output(cmd)
except:
raise Exception("Multipass unmount command failed.")
def recover(self, vm_name):
cmd = [self.cmd, "recover", vm_name]
try:
subprocess.check_output(cmd)
except:
raise Exception("Multipass recover command failed.")
def suspend(self):
cmd = [self.cmd, "suspend"]
try:
subprocess.check_output(cmd)
except:
raise Exception("Multipass suspend command failed.")