From 78379f1195233a17df4bbe0a8fed4d838dfc614e Mon Sep 17 00:00:00 2001 From: frybin Date: Sun, 17 Feb 2019 23:08:07 -0500 Subject: [PATCH] Make a way to change both the vm name and hostname --- proxstar/__init__.py | 16 +++++++++ proxstar/starrs.py | 13 +++++++ proxstar/static/js/script.js | 54 ++++++++++++++++++++++++++++++ proxstar/templates/vm_details.html | 7 +++- proxstar/vm.py | 5 +++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/proxstar/__init__.py b/proxstar/__init__.py index c877ca2..99a856b 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -306,6 +306,22 @@ def vm_disk(vmid, disk, size): return '', 403 +@app.route("/starrs//hostname//", 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//renew", methods=['POST']) @auth.oidc_auth def vm_renew(vmid): diff --git a/proxstar/starrs.py b/proxstar/starrs.py index fa0d0a7..c45fc3c 100644 --- a/proxstar/starrs.py +++ b/proxstar/starrs.py @@ -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 diff --git a/proxstar/static/js/script.js b/proxstar/static/js/script.js index 154bcb1..3bbd2b2 100644 --- a/proxstar/static/js/script.js +++ b/proxstar/static/js/script.js @@ -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(); + } + }); +}); diff --git a/proxstar/templates/vm_details.html b/proxstar/templates/vm_details.html index bfcc95b..52383f6 100644 --- a/proxstar/templates/vm_details.html +++ b/proxstar/templates/vm_details.html @@ -54,7 +54,12 @@

VM Details

Name
-
{{ vm.name }}
+
+ {{ vm.name }} + +
DNS Name
{{ vm.name }}.csh.rit.edu
ID
diff --git a/proxstar/vm.py b/proxstar/vm.py index 4ba709e..7705891 100644 --- a/proxstar/vm.py +++ b/proxstar/vm.py @@ -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'])