-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui_server_vmware.py
executable file
·218 lines (188 loc) · 7.42 KB
/
ui_server_vmware.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
#!/usr/bin/env python
## Copyright (c) 2014 Citrix Systems, Inc. All Rights Reserved.
## You may only reproduce, distribute, perform, display, or prepare derivative works of this file pursuant to a valid license from Citrix.
## ----------------------
## INSTALL DEPENDANCIES
## ----------------------
## $ pip install bottle
## $ pip install rocket or cherrypy
##
## Author: Will Stevens
import hashlib
import json
import logging
import logging.handlers
import pprint
from pysphere import VIServer
from ui_common import *
from ConfigParser import ConfigParser
conf = ConfigParser()
conf.add_section('VMWARE')
conf.set('VMWARE', 'log_file', './logs/vmware_api.log')
conf.add_section('WEBSERVER')
conf.set('WEBSERVER', 'debug', 'False')
conf.set('WEBSERVER', 'port', '8787')
conf.add_section('STATE') # STATE config section to maintain state of the running process
conf.set('STATE', 'active_migration', 'False')
conf.add_section('DEBUG')
conf.set('DEBUG', 'ui_test', 'False')
# read in config files if they exist
conf.read(['./settings.conf', './running.conf'])
# require the vmware endpoint to be configured to start the server
if not conf.has_option('VMWARE', 'endpoint'):
sys.exit("Config required in settings.conf: [VMWARE] -> endpoint")
if not conf.has_option('VMWARE', 'username'):
sys.exit("Config required in settings.conf: [VMWARE] -> username")
if not conf.has_option('VMWARE', 'password'):
sys.exit("Config required in settings.conf: [VMWARE] -> password")
# make sure we have an nfs mount point
if not conf.has_section('FILESERVER') or not conf.has_option('FILESERVER', 'files_path'):
sys.exit("Config required in settings.conf: [FILESERVER] -> files_path")
# add server logging
log_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
log = logging.getLogger()
if conf.getboolean('WEBSERVER', 'debug'):
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
logging.basicConfig(format='%(asctime)s %(message)s')
log_file_handler = logging.handlers.TimedRotatingFileHandler('server.log', when='midnight', interval=1, backupCount=30)
log_file_handler.setFormatter(log_formatter)
log.addHandler(log_file_handler)
def discover_src_vms():
conf.read(['./running.conf'])
if conf.has_option('STATE', 'vms'):
vms = json.loads(conf.get('STATE', 'vms'))
else:
vms = {}
if conf.has_option('STATE', 'vm_order'):
order = json.loads(conf.get('STATE', 'vm_order'))
else:
order = []
vmware = VIServer()
try:
vmware.connect(
conf.get('VMWARE', 'endpoint'),
conf.get('VMWARE', 'username'),
conf.get('VMWARE', 'password')
)
except:
print("")
print("UNABLE TO CONNECT TO VMWARE...")
print("")
bottle.abort(500, "Unable to connect to VMware...")
with open(conf.get('VMWARE', 'log_file'), 'a') as f:
f.write('\n\nDISCOVERING VMWARE...\n')
discovered = [] # vms of this discovery. we will remove the vm's from 'vms' later if they are not in this array.
datacenters = vmware.get_datacenters()
for dc_key, dc_name in datacenters.iteritems():
src_vm_list = vmware.get_registered_vms(datacenter=dc_name, advanced_filters={'config.template':False})
for vm_path in src_vm_list:
try:
src_vm = vmware.get_vm_by_path(vm_path)
except:
continue
properties = src_vm.get_properties()
vm_id = hashlib.sha1(properties['name']+"|"+properties['path']).hexdigest()
if vm_id not in order:
order.append(vm_id)
if vm_id not in vms:
vms[vm_id] = {}
vms[vm_id]['id'] = vm_id
vms[vm_id]['src_dc'] = dc_name
vms[vm_id]['src_name'] = properties['name']
vms[vm_id]['src_path'] = properties['path']
vms[vm_id]['src_memory'] = properties['memory_mb']
vms[vm_id]['src_cpus'] = properties['num_cpu']
vms[vm_id]['src_type'] = properties['guest_full_name']
vms[vm_id]['src_status'] = src_vm.get_status(basic_status=True)
if 'state' not in vms[vm_id]:
vms[vm_id]['state'] = ''
if 'src_disks' not in vms[vm_id] or (
'src_disks' in vms[vm_id] and len(vms[vm_id]['src_disks']) != len(properties['disks'])):
vms[vm_id]['state'] = ''
vms[vm_id]['src_disks'] = []
for disk in properties['disks']:
vms[vm_id]['src_disks'].append({
'label':disk['label'],
'path':disk['descriptor'],
'type':disk['device']['type'],
'size':disk['capacity']
})
if '64-bit' in vms[vm_id]['src_type'].lower():
vms[vm_id]['src_os_arch'] = 64
elif '32-bit' in vms[vm_id]['src_type'].lower():
vms[vm_id]['src_os_arch'] = 32
discovered.append(vm_id)
with open(conf.get('VMWARE', 'log_file'), 'a') as f:
f.write('VM: %s (%s)\n' % (vms[vm_id]['src_name'], vm_id))
f.write(pprint.pformat(properties))
f.write('\n\n')
# loop through the 'vms' and remove any that were not discovered in this pass...
for vm_id in vms.keys():
if vm_id not in discovered:
del vms[vm_id] # no longer a valid VM, so remove it...
if vm_id in order: # remove the vm from the order list as well if it exists...
order.remove(vm_id)
### Update the running.conf file
conf.set('STATE', 'vms', json.dumps(vms))
conf.set('STATE', 'vm_order', json.dumps(order))
with open('running.conf', 'wb') as f:
conf.write(f) # update the file to include the changes we have made
return vms, order
# migration page
@bottle.route('/')
@bottle.view('index')
def index():
variables = {}
conf.read(['./running.conf'])
if not conf.getboolean('STATE', 'active_migration'):
open(conf.get('CLOUDSTACK', 'log_file'), 'w').close() # refresh the cs_request.log on reloads
open(conf.get('VMWARE', 'log_file'), 'w').close() # refresh the vmware_api.log on reloads
variables['cs_objs'] = json.dumps(cs_discover_accounts())
if conf.getboolean('DEBUG', 'ui_test'):
vms = json.loads(conf.get('STATE', 'vms'))
order = json.loads(conf.get('STATE', 'vm_order'))
else:
vms, order = discover_src_vms()
variables['vms'] = json.dumps(vms)
variables['vm_order'] = json.dumps(order)
variables['active_migration'] = conf.get('STATE', 'active_migration').lower()
else:
variables['cs_objs'] = json.dumps(json.loads(conf.get('STATE', 'cs_objs')))
variables['vms'] = vms = json.dumps(json.loads(conf.get('STATE', 'vms')))
variables['vm_order'] = json.dumps(json.loads(conf.get('STATE', 'vm_order')))
variables['active_migration'] = conf.get('STATE', 'active_migration').lower()
variables['log_list'] = get_log_list()
return dict(variables)
# start the migration
@bottle.route('/migration/start', method='POST')
def start_migration():
if bottle.request.params.migrate:
conf.read(['./running.conf'])
conf.set('STATE', 'migrate', bottle.request.params.migrate)
with open('running.conf', 'wb') as f:
conf.write(f) # update the file to include the changes we have made
if not conf.getboolean('DEBUG', 'ui_test'):
subprocess.Popen(['python', 'migrate_vmware.py'])
return 'ok'
else:
return bottle.abort(500, 'Could not start the migration...')
# get the migration log
@bottle.route('/migration/log')
def get_migration_log():
output = ''
conf.read(['./running.conf'])
try:
with open(conf.get('VMWARE', 'migration_log_file'), 'r') as f:
output = f.read()
except:
output = 'Log does not exist yet...'
return output
# start the server
bottle.run(
server='cherrypy',
host='0.0.0.0',
port=conf.getint('WEBSERVER', 'port'),
reloader=False,
debug=conf.getboolean('WEBSERVER', 'debug'))