Skip to content

Commit

Permalink
Make a way to change both the vm name and hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
frybin committed Feb 18, 2019
1 parent 12b0f37 commit 78379f1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
16 changes: 16 additions & 0 deletions proxstar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ def vm_disk(vmid, disk, size):
return '', 403


@app.route("/starrs/<string:vmid>/hostname/<string:old_name>/<string:new_name>", methods=['POST'])
@auth.oidc_auth
def vm_disk(vmid, old_name, new_name):
user = User(session['userinfo']['preferred_username'])
proxmox = connect_proxmox()
if user.rtp or int(vmid) in user.allowed_vms:
valid, available = check_hostname(starrs, new_name)
if valid and available:
vm = VM(vmid)
vm.rename_vm(new_name)
change_hostname(starrs, old_name, new_name)
return '', 200
else:
return '', 403


@app.route("/vm/<string:vmid>/renew", methods=['POST'])
@auth.oidc_auth
def vm_renew(vmid):
Expand Down
13 changes: 13 additions & 0 deletions proxstar/starrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ def delete_starrs(starrs, name):
finally:
c.close()
return results

def change_hostname(starrs, old_name, new_name):
c = starrs.cursor()
try:
c.execute("BEGIN")
c.callproc("api.initialize", ('root', ))
c.callproc("api.modify_dns_cname",
(old_name, 'csh.rit.edu', 'hostname',new_name))
results = c.fetchall()
c.execute("COMMIT")
finally:
c.close()
return results
54 changes: 54 additions & 0 deletions proxstar/static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,57 @@ $(document).on('focus click', "[id^=boot-order-]", function() {
}
});
});

$(".rename-vm").click(function(){
const vmid = $(this).data('vmid');
const old_name = $(this).data('old_name');
swal({
title: 'Enter what you would like this VM to be renamed to:',
content: {
element: 'input',
attributes: {
type: 'string',
},
},
buttons: {
cancel: {
text: "Cancel",
visible: true,
closeModal: true,
className: "",
},
confirm: {
text: "Select",
closeModal: false,
}
},
})
.then((new_name) => {
if (new_name) {
fetch(`/starrs/${vmid}/hostname/${old_name}/${new_name}`, {
credentials: 'same-origin',
method: 'post'
}).then((response) => {
return swal(`VM Name has been changes!`, {
icon: "success",
buttons: {
ok: {
text: "OK",
closeModal: true,
className: "",
}
}
});
}).then(() => {
window.location = `/vm/${vmid}`;
});
}
}).catch(err => {
if (err) {
swal("Uh oh...", `Unable to change VM Name. Please try again later.`, "error");
} else {
swal.stopLoading();
swal.close();
}
});
});
7 changes: 6 additions & 1 deletion proxstar/templates/vm_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ <h3 class="card-title">VM Details</h3>
<div class="card-body">
<dl class="dl-horizontal">
<dt>Name</dt>
<dd>{{ vm.name }}</dd>
<dd>
{{ vm.name }}
<button class="btn btn-default proxstar-vmbtn" id="rename-vm" data-vmid="{{ vm.id }}" old_name="{{ vm.name }}">
<i class="fas fa-cog"></i>
</button>
</dd>
<dt>DNS Name</dt>
<dd>{{ vm.name }}.csh.rit.edu</dd>
<dt>ID</dt>
Expand Down
5 changes: 5 additions & 0 deletions proxstar/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ def resize_disk(self, disk, size):
proxmox.nodes(self.node).qemu(self.id).resize.put(
disk=disk, size="+{}G".format(size))

@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
def rename_vm(self, name):
proxmox = connect_proxmox()
proxmox.nodes(self.node).qemu(self.id).config.put(name=name)

@lazy_property
def expire(self):
return get_vm_expire(db, self.id, app.config['VM_EXPIRE_MONTHS'])
Expand Down

0 comments on commit 78379f1

Please sign in to comment.