forked from lambdacrash/xenadm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
77 lines (69 loc) · 2.33 KB
/
Server.py
File metadata and controls
77 lines (69 loc) · 2.33 KB
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
import paramiko
from VM import VM
class Server:
id = ""
ip = ""
user = "root"
password = "secret"
vms = []
def __init__(self, id="0", ip="0", vms=[], user="root", password="secret"):
self.id = id
self.ip = ip
self.vms = vms
self.user = user
self.password = password
def start_vm(self, id=-1):
if id < 0:
return
for vm in self.vms:
if vm.id == id:
if vm.status!="halted":
return "Unable to start a running VM"
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(self.ip, username=self.user, password=self.password)
stdin, stdout, stderr = ssh.exec_command('xe vm-start uuid="'+vm.id+'"')
ret = stdout.read()
ssh.close()
return ret
def stop_vm(self, id=-1):
if id < 0:
return
for vm in self.vms:
if vm.id == id:
if vm.status!="running":
return "Unable to stop an halted VM"
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(self.ip, username=self.user, password=self.password)
stdin, stdout, stderr = ssh.exec_command('xe vm-shutdown uuid="'+vm.id+'"')
ret = stdout.read()
ssh.close()
return ret
# for vm in self.vms:
# if vm.id == id:
def snap_vm(self, id=-1):
if id < 0:
return
# for vm in self.vms:
# if vm.id == id:
def list_vm(self):
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(self.ip, username=self.user, password=self.password)
stdin, stdout, stderr = ssh.exec_command('xe vm-list tags="xenadm"')
ret = stdout.read()
ssh.close()
self.vms = []
name = ""
id=""
status=""
for l in ret.split("\n"):
if "uuid" in l:
id = l.split(" : ")[-1]
elif "name-label" in l:
name = l.split(": ")[-1]
elif "power-state" in l:
status = l.split(": ")[-1]
self.vms.append(VM(id=id, name=name, status=status))
return self.vms