Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions controllers/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def __pause(vm):
def __reboot(vm):
vm.reboot()


def __delete(vm):
vm.delete()

Expand Down Expand Up @@ -118,16 +119,28 @@ def __power_off(vm):


def __restore_snapshot(vmid):
auth = b64encode(dumps(dict(u=session.username, p=session.password)))
snapshot_id = request.vars.snapshot_id
pvars = dict(auth=auth, vmid=vmid, snapshot_id=snapshot_id)
scheduler.queue_task(task_restore_snapshot, timeout=600,
pvars=pvars)


def __delete_snapshot(vmid):
auth = b64encode(dumps(dict(u=session.username, p=session.password)))
snapshot_id = request.vars.snapshot_id
pvars = dict(auth=auth, vmid=vmid, snapshot_id=snapshot_id)
logger.debug(pvars)
scheduler.queue_task(task_delete_snapshot, timeout=600, pvars=pvars)


def __get_console_url(vm):
console_type = config.get('misc', 'console_type')
consoleurl = vm.get_console_url(console_type=console_type)
return '<a target="_blank" href="{0}">{0}</a>'.format(consoleurl)
if request.vars.urlonly:
return consoleurl
else:
return '<a target="_blank" href="{0}">{0}</a>'.format(consoleurl)


def __migrate(vmid):
Expand Down Expand Up @@ -167,6 +180,8 @@ def index():
message = __power_off(vm)
elif action == 'restore-snapshot':
message = __restore_snapshot(vmid)
elif action == 'delete-snapshot':
message = __delete_snapshot(vmid)
elif action == 'migrate':
message = __migrate(vmid)
elif action == 'add-virtual-disk':
Expand All @@ -184,7 +199,6 @@ def __add_virtual_disk(vmid, size):
try:
db.virtual_disk_requests.insert(user=session.username, vmid=vmid,
disk_size=int(size), status=0)
return jsonify()
except Exception as e:
logger.exception(e.message or str(e.__class__))
return jsonify(status='fail', message=e.message or str(e.__class__))
Expand Down Expand Up @@ -451,13 +465,21 @@ def __attach_vm_public_ip():
@auth.requires(user_is_project_admin)
def handle_clone_request():
try:
conn = Baadal.Connection(_authurl, _tenant, session.username,
session.password)
row = db(db.clone_requests.id == request.vars.id).select()[0]
vm = conn.find_baadal_vm(id=row.vm_id)
vm.clone()
row.update_record(status=1)
db.commit()
if request.vars.action == 'approve':
row = db(db.clone_requests.id == request.vars.id).select()[0]
row.update_record(status=REQUEST_STATUS_PROCESSING)
auth = b64encode(dumps(dict(u=session.username,
p=session.password)))
scheduler.queue_task(task_clone_vm, timeout=600,
pvars={'reqid': row.id, 'auth': auth})
db.commit()
return jsonify(message='Request successfully approved')
elif request.vars.action == 'reject':
db(db.clone_requests.id == request.vars.id).delete()
db.commit()
return jsonify(message='Request successfully deleted')
else:
raise HTTP(400)
except Exception as e:
message = e.message or str(e.__class__)
logger.error(message)
Expand Down
19 changes: 13 additions & 6 deletions controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@ def all_vms():
vm_properties = vm.properties()
image_id = vm_properties['image']['id']
if not images.has_key(image_id):
image = conn.find_image(id=image_id)
meta = image.metadata
images[image_id] = ' '.join([meta['os_name'],
meta['os_version'], meta['os_arch'],
meta['os_edition'], meta['disk_size']])
try:
image = conn.find_image(id=image_id)
meta = image.metadata
images[image_id] = ' '.join([meta['os_name'],
meta['os_version'], meta['os_arch'],
meta['os_edition'], meta['disk_size']])
except NotFound:
images[image_id] = 'Image not found'
vm_properties['image']['info'] = images[image_id]
# snapshots = vm.properties()['snapshots']
# STR = 'created'
Expand Down Expand Up @@ -281,14 +284,18 @@ def disk_requests():
elif request.extension == 'json':
response = []
spurious_requests = []
cache = {}
try:
rows = db(db.virtual_disk_requests.status == 0).select()
conn = Baadal.Connection(_authurl, _tenant, session.username,
session.password)
for row in rows:
try:
cr = {}
vm = conn.find_baadal_vm(id=row.vmid)
if not cache.has_key(row.vmid):
vm = conn.find_baadal_vm(id=row.vmid)
cache[row.vmid] = vm.name
cr['vm_name'] = cache[row.vmid]
cr['id'] = row.id
cr['request_time'] = str(row.request_time)
cr['vm_name'] = vm.name
Expand Down
7 changes: 4 additions & 3 deletions controllers/post_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ def new_vm():
request_time=int(time.time()),
state=vm_state
)
user_email = ldap.fetch_user_info(session.username)['user_email']
context = Storage()
context.username = session.username
user_info = ldap.fetch_user_info(session.username)
context.username = user_info['user_name']
user_email = user_info['user_email']
context.user_email = user_email
context.vm_name = request.vars.vm_name
context.support_email = mail_support
context.mail_support = mail_support

if mailer.send(mailer.MailTypes.VMRequest, user_email, context):
db.commit()
Expand Down
16 changes: 11 additions & 5 deletions controllers/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from novaclient.exceptions import NotFound


@auth.requires_login()
def index():
return dict()
Expand Down Expand Up @@ -25,11 +28,14 @@ def my_vms():
vm_properties = vm.properties()
image_id = vm_properties['image']['id']
if not images.has_key(image_id):
image = conn.find_image(id=image_id)
meta = image.metadata
images[image_id] = ' '.join([meta['os_name'],
meta['os_version'], meta['os_arch'],
meta['os_edition'], meta['disk_size']])
try:
image = conn.find_image(id=image_id)
meta = image.metadata
images[image_id] = ' '.join([meta['os_name'],
meta['os_version'], meta['os_arch'],
meta['os_edition'], meta['disk_size']])
except NotFound:
images[image_id] = 'Image not found'
vm_properties['image']['info'] = images[image_id]
response.append(vm_properties)
# snapshots = vm.properties()['snapshots']
Expand Down
3 changes: 1 addition & 2 deletions models/2_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@
Field('id', 'integer'),
Field('user', 'string', required=True),
Field('vmid', 'string', required=True),
Field('request_time', 'integer',
requires=IS_INT_IN_RANGE(0, 1)),
Field('request_time', 'datetime'),
Field('status', 'integer')
)

Expand Down
28 changes: 20 additions & 8 deletions models/action/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def task_create_vm(reqid, auth):
try:
req.update_record(state=REQUEST_STATUS_APPROVED)
context = Storage()
context.username = auth.u
context.vm_name = name
context.mail_support = mail_support
user_info = ldap.fetch_user_info(auth.u)
context.username = user_info['user_name']
context.user_email = user_info['user_email']
context.vm_name = name
context.mail_support = mail_support
context.gateway_server = gateway_server
context.req_time = seconds_to_localtime(req.request_time)
context.request_time = seconds_to_localtime(req.request_time)
logger.info('sending mail')
mailer.send(mailer.MailTypes.VMCreated, context.user_email,
context)
Expand Down Expand Up @@ -98,15 +98,18 @@ def task_restore_snapshot(auth, vmid, snapshot_id):
logger.exception(e)


def task_clone_vm(auth, vmid):
def task_clone_vm(auth, reqid):
auth = Storage(loads(b64decode(auth)))
req = db(db.clone_requests.id == reqid).select()[0]
try:
conn = Baadal.Connection(_authurl, _tenant, auth.u, auth.p)
vm = conn.find_baadal_vm(id=vmid)
vm = conn.find_baadal_vm(id=req.vm_id)
clone = vm.clone()
logger.info('VM Cloned: VMID %s, clone_id %s' % (vmid, clone))
logger.info('VM Cloned: VMID %s, clone_id %s' % (vm_id, clone))
req.update_record(status=REQUEST_STATUS_APPROVED)
except Exception as e:
logger.exception(e)
req.update_record(status=REQUEST_STATUS_POSTED)

def task_resize_vm(auth, reqid):
auth = Storage(loads(b64decode(auth)))
Expand All @@ -128,4 +131,13 @@ def task_resize_vm(auth, reqid):
conn.close()



def task_delete_snapshot(auth, vmid, snapshot_id):
auth = Storage(loads(b64decode(auth)))
try:
conn = Baadal.Connection(_authurl, _tenant, auth.u, auth.p)
image = conn.find_image(id=snapshot_id)
status = image.delete()
logger.info('Snapshot deleted: VMID %s, snapshot_id %s' % \
(vmid, snapshot_id))
except Exception as e:
logger.exception(e)
50 changes: 21 additions & 29 deletions modules/Baadal.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,43 +130,35 @@ def clone(self, clone_name=None, full=False):
defaults to Full (optional)
:return:
"""
# create a snapshot of the machine
# create a new vm using the newly created snapshot
# delete the snapshot

clone_name = clone_name or self.server.name + '_clone'
flavor_id = self.server.flavor['id']
networks = self.get_networks().keys()
network_ids = [
self.__conn.neutron.list_networks(name=net)['networks'][0]['id']
for net in networks]
nics = [{'net-id': netid for netid in network_ids}]
snapshot_id = self.server.create_image("temp")
image = self.__conn.nova.images.find(id=snapshot_id)
while image.status != 'ACTIVE':
image = self.__conn.nova.images.find(id=snapshot_id)
pass
image = self.__conn.nova.images.find(id=self.server.image['id'])
flavor = self.__conn.nova.flavors.find(id=flavor_id)
clone = self.server.manager.create(clone_name, image,
flavor, nics=nics,
security_groups=networks,
meta=self.server.metadata)
while clone.status != 'ACTIVE':
clone = clone.manager.find(id=clone.id)
else:
flavor = self.__conn.nova.flavors.find(id=flavor_id)
clone = self.server.manager.create(clone_name, image,
flavor, nics=nics,
security_groups=networks,
meta=self.server.metadata)
while clone.status != 'ACTIVE':
clone = clone.manager.find(id=clone.id)
else:
image.delete()
attached_disks = self.get_attached_disks()
for i in attached_disks:
volid = i['id']
if full:
volume_clone = self.__conn.cinder.volumes.create(
i['size'], source_volid=i['id'])
volid = volume_clone.id
while volume_clone.status != 'available':
volume_clone = self.__conn.cinder.volumes.get(
volume_clone.id)
self.__conn.nova.volumes.create_server_volume(
clone.id, volid, i['path'])
attached_disks = self.get_attached_disks()
for i in attached_disks:
volid = i['id']
if full:
volume_clone = self.__conn.cinder.volumes.create(
i['size'], source_volid=i['id'])
volid = volume_clone.id
while volume_clone.status != 'available':
volume_clone = self.__conn.cinder.volumes.get(
volume_clone.id)
self.__conn.nova.volumes.create_server_volume(
clone.id, volid, i['path'])
return clone

def create_snapshot(self, snapshot_name=None):
Expand Down
20 changes: 9 additions & 11 deletions modules/BaadalMailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class VMRequest(object):

class ApprovalReminder(object):
subject = 'Request waiting for your approval'
mailbody = "Dear {0[approverName]},\n\n{0[userName]} has made a "\
mailbody = "Dear {0[approver]},\n\n{0[user]} has made a "\
"'{0[requestType]}' request on {0[requestTime]}. It is "\
"waiting for your approval."

Expand All @@ -37,16 +37,14 @@ class IPRequest(object):

class VMCreated(object):
subject = 'Your BaadalVM has been created'
mailbody = "Dear {0[username]},\n\nThe VM {0[vm_name]}, requested"\
" at {0[request_time]} is successfully created and is now" \
" available for use. Following operations are allowed on" \
" VM:\n 1. Start\n2. Stop\n3. Pause\n4. Resume\n5. Destroy" \
" \n6. Delete\n\n Default credentials for VM is as follows:" \
" \nUsername:root/baadalservervm/baadaldesktopvm\n" \
" Password:baadal\n\n To access VM using assigned private" \
" IP; SSH to baadal gateway machine using your GCL" \
" credential .\n username@{0[gateway_server]}\n For other"\
" details, Please login to Baadal web interface."
mailbody = ("Dear {0[username]},\n\nThe VM {0[vm_name]}, requested"
" at {0[request_time]} is successfully created and is now"
" available for use. To access your VM from within the web"
" browser, login to Baadal using your credentials and click"
" on the open console icon, which looks like >_\n"
" Default credentials for VM is as follows:"
" \nUsername:root/baadalservervm/baadaldesktopvm\n"
" Password:baadal\n\n")

class TaskComplete(object):
subject = '{0[taskType]} task successful'
Expand Down
48 changes: 45 additions & 3 deletions static/css/baadal-custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,51 @@ textarea {
}

#vm-settings-modal .panel-footer>span {
display: none;
display: none;
}

#disk-size-input input {
display: inline-block;
}
display: inline-block;
}
/* Sticky footer styles
-------------------------------------------------- */
html {
position: relative;
min-height: 100%;
}
body {
/* Margin bottom by footer height */
margin-bottom: 60px;
}
.footer {
position: absolute;
bottom: 0;
width: 100%;
/* Set the fixed height of the footer here */
height: 60px;
background-color: #222;
}


/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */

body > .container {
padding: 60px 15px 0;
}
.container .text-muted {
margin: 20px 0;
}

.footer > .container {
padding-right: 15px;
padding-left: 15px;
text-align: center;
color: #9d9d9d;
}

code {
font-size: 80%;
}

Loading