-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloud_manager.py
252 lines (227 loc) · 8.18 KB
/
cloud_manager.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from flask import Flask, render_template, request, redirect
from os.path import isfile, join
from sys import platform, argv
from threading import Thread
from sqlite3 import Error
import urllib.parse
import webbrowser
import threading
import requests
import datetime
import unittest
import sqlite3
import math
import json
import time
import csv
import sys
import os
import re
import virtualbox
from virtualbox import library
app = Flask(__name__, template_folder='./front-end', static_folder='./front-end', static_url_path='')
vbox = virtualbox.VirtualBox()
sessions = []
ips = []
def getVM(name=''):
if name:
return vbox.find_machine(name)
else:
return vbox.machines
def getState(x):
return {
'FirstOnline': True,
'Restoring': True,
'Starting': True,
'Saved': False,
'Stopping': False,
'PoweredOff': False,
'Aborted': False
}[x]
def prepareVMs():
vms = []
allVms = getVM()
for vm in allVms:
state = 'none'
# print(str(vm.state))
state = getState(str(vm.state))
uid_name = str(vm.name).replace(' ', '_').replace('(', '').replace(')', '')
cpu_count = str(vm.cpu_count)
memory_size = str(vm.memory_size)
memory_usage = int(memory_size) * 100 / 16000
vms.append([vm.name, state, uid_name, memory_size, cpu_count, memory_usage])
return vms
def runSsh(session):
try:
print('starting ssh service ...')
guest_session = session.console.guest.create_session("ubuntu1", "1234")
proc, stdout, stderr = guest_session.execute("/home/ubuntu1/.local/bin/wssh")
except:
pass
def retriveIpAddress(name, session):
try:
print('retriving ip address ...')
guest_session = session.console.guest.create_session("ubuntu1", "1234")
proc, stdout, stderr = guest_session.execute("/sbin/ifconfig")
print('stdout: ', stdout.decode('ascii'))
print('stderr: ', stderr.decode('ascii'))
ip = re.findall( r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", stdout.decode('ascii'))[0]
print('ip: ', ip)
ips.append([name, ip])
except:
pass
def getSession(name, remove = False):
if name:
for session in sessions:
if session[0] == name:
if remove:
session[0] = ''
return session[1]
def getIpAddress(name, remove = False):
if name:
for ip in ips:
if ip[0] == name:
if remove:
ip[0] = ''
return ip[1]
def cloneVM(origin_vm, snapshot_name_or_id=None,
mode=library.CloneMode.machine_state,
options=None, name=None,
uuid=None, groups=None, basefolder='', register=True):
"""Clone this Machine
Options:
snapshot_name_or_id - value can be either ISnapshot, name, or id
mode - set the CloneMode value
options - define the CloneOptions options
name - define a name of the new VM
uuid - set the uuid of the new VM
groups - specify which groups the new VM will exist under
basefolder - specify which folder to set the VM up under
register - register this VM with the server
Note: Default values create a linked clone from the current machine state
Return a IMachine object for the newly cloned vm
"""
if options is None:
# options = [library.CloneOptions.keep_disk_names]
options = [library.CloneOptions.link]
# options = [library.CloneOptions.keep_natma_cs]
if groups is None:
groups = []
if snapshot_name_or_id is not None:
if isinstance(snapshot_name_or_id, basestring):
snapshot = origin_vm.find_snapshot(snapshot_name_or_id)
else:
snapshot = snapshot_name_or_id
vm = snapshot.machine
else:
# linked clone can only be created from a snapshot...
# try grabbing the current_snapshot
if library.CloneOptions.link in options:
vm = origin_vm.current_snapshot.machine
else:
vm = origin_vm
if name is None:
name = "%s Clone" % vm.name
# Build the settings file
create_flags = ''
if uuid is not None:
create_flags = "UUID=%s" % uuid
primary_group = ''
if groups:
primary_group = groups[0]
# Make sure this settings file does not already exist
test_name = name
settings_file = ''
for i in range(1, 1000):
settings_file = vbox.compose_machine_filename(test_name,
primary_group,
create_flags,
basefolder)
if not os.path.exists(os.path.dirname(settings_file)):
break
test_name = "%s (%s)" % (name, i)
name = test_name
# Create the new machine and clone it!
vm_clone = vbox.create_machine(settings_file, name, groups, '', create_flags)
progress = vm.clone_to(vm_clone, mode, options)
progress.wait_for_completion(-1)
if register:
vbox.register_machine(vm_clone)
return vm_clone
@app.route('/')
def indexPage():
vms = prepareVMs()
return render_template('/index.html', vms=vms)
@app.route('/start', methods=['GET'])
def start():
name = str(request.args.get('name'))
session = virtualbox.Session()
progress = getVM(name).launch_vm_process(session, "gui", [])
progress.wait_for_completion(-1)
sessions.append([name, session])
return redirect('/')
@app.route('/stop', methods=['GET'])
def stop():
name = str(request.args.get('name'))
session = virtualbox.Session()
getVM(name).lock_machine(session, library.LockType.shared)
session.console.power_down()
return redirect('/')
@app.route('/clone', methods=['GET'])
def clone():
name = str(request.args.get('name'))
clone_name = str(request.args.get('clone_name'))
cloneVM(getVM(name), name = clone_name)
return redirect('/')
@app.route('/terminal', methods=['GET'])
def terminal():
name = str(request.args.get('name'))
session = virtualbox.Session()
getVM(name).lock_machine(session, library.LockType.shared)
retriveIpAddress(name, session)
t1 = threading.Thread(target=runSsh, args=(session,))
t1.start()
# session = getSession(name)
# t2 = threading.Thread(target=retriveIpAddress, args=(name, session,))
# t2.start()
ip = getIpAddress(name)
print('gathered ip: ', str(ip))
terminal_address = 'http://' + str(ip) + ':8888/?hostname=ubuntu1-VirtualBox&username=ubuntu1&password=MTIzNA=='
session.unlock_machine()
return terminal_address
@app.route('/setting', methods=['GET'])
def setting():
name = str(request.args.get('name'))
memory = str(request.args.get('memory'))
cpu = str(request.args.get('cpu'))
session = virtualbox.Session()
getVM(name).lock_machine(session, library.LockType.shared)
vm = session.machine
vm.memory_size = int(memory)
vm.cpu_count = int(cpu)
vm.save_settings()
session.unlock_machine()
return redirect('/')
@app.route('/remove', methods=['GET'])
def remove():
name = str(request.args.get('name'))
vm = getVM(name)
if vm.state >= library.MachineState.running:
session = virtualbox.Session()
vm.lock_machine(session, library.LockType.shared)
try:
progress = session.console.power_down()
progress.wait_for_completion(-1)
except Exception:
print("Error powering off machine", file=sys.stderr)
session.unlock_machine()
time.sleep(0.5) # TODO figure out how to ensure session is really unlocked...
option = library.CleanupMode.full
media = vm.unregister(option)
progress = vm.delete_config(media)
progress.wait_for_completion(-1)
media = []
return redirect('/')
if __name__ == "__main__":
webbrowser.open('http://localhost:3333')
app.run(host="0.0.0.0", port=3333, threaded=True)