Skip to content

Commit 9ab1426

Browse files
committed
Make a way to change both the vm name and hostname
1 parent 12b0f37 commit 9ab1426

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

proxstar/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ def vm_disk(vmid, disk, size):
306306
return '', 403
307307

308308

309+
@app.route("/starrs/<string:vmid>/hostname/<string:old_name>/<string:new_name>", methods=['POST'])
310+
@auth.oidc_auth
311+
def vm_disk(vmid, old_name, new_name):
312+
user = User(session['userinfo']['preferred_username'])
313+
proxmox = connect_proxmox()
314+
if user.rtp or int(vmid) in user.allowed_vms:
315+
valid, available = check_hostname(starrs, new_name)
316+
if valid and available:
317+
vm = VM(vmid)
318+
vm.rename_vm(new_name)
319+
change_hostname(starrs, old_name, new_name)
320+
return '', 200
321+
else:
322+
return '', 403
323+
324+
309325
@app.route("/vm/<string:vmid>/renew", methods=['POST'])
310326
@auth.oidc_auth
311327
def vm_renew(vmid):

proxstar/starrs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,16 @@ def delete_starrs(starrs, name):
106106
finally:
107107
c.close()
108108
return results
109+
110+
def change_hostname(starrs, old_name, new_name):
111+
c = starrs.cursor()
112+
try:
113+
c.execute("BEGIN")
114+
c.callproc("api.initialize", ('root', ))
115+
c.callproc("api.modify_dns_cname",
116+
(old_name, 'csh.rit.edu', 'hostname',new_name))
117+
results = c.fetchall()
118+
c.execute("COMMIT")
119+
finally:
120+
c.close()
121+
return results

proxstar/static/js/script.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,57 @@ $(document).on('focus click', "[id^=boot-order-]", function() {
891891
}
892892
});
893893
});
894+
895+
$(".rename-vm").click(function(){
896+
const vmid = $(this).data('vmid');
897+
const old_name = $(this).data('old_name');
898+
swal({
899+
title: 'Enter what you would like this VM to be renamed to:',
900+
content: {
901+
element: 'input',
902+
attributes: {
903+
type: 'string',
904+
},
905+
},
906+
buttons: {
907+
cancel: {
908+
text: "Cancel",
909+
visible: true,
910+
closeModal: true,
911+
className: "",
912+
},
913+
confirm: {
914+
text: "Select",
915+
closeModal: false,
916+
}
917+
},
918+
})
919+
.then((new_name) => {
920+
if (new_name) {
921+
fetch(`/starrs/${vmid}/hostname/${old_name}/${new_name}`, {
922+
credentials: 'same-origin',
923+
method: 'post'
924+
}).then((response) => {
925+
return swal(`VM Name has been changes!`, {
926+
icon: "success",
927+
buttons: {
928+
ok: {
929+
text: "OK",
930+
closeModal: true,
931+
className: "",
932+
}
933+
}
934+
});
935+
}).then(() => {
936+
window.location = `/vm/${vmid}`;
937+
});
938+
}
939+
}).catch(err => {
940+
if (err) {
941+
swal("Uh oh...", `Unable to change VM Name. Please try again later.`, "error");
942+
} else {
943+
swal.stopLoading();
944+
swal.close();
945+
}
946+
});
947+
});

proxstar/templates/vm_details.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ <h3 class="card-title">VM Details</h3>
5454
<div class="card-body">
5555
<dl class="dl-horizontal">
5656
<dt>Name</dt>
57-
<dd>{{ vm.name }}</dd>
57+
<dd>
58+
{{ vm.name }}
59+
<button class="btn btn-default proxstar-vmbtn" id="rename-vm" data-vmid="{{ vm.id }}" old_name="{{ vm.name }}">
60+
<i class="fas fa-cog"></i>
61+
</button>
62+
</dd>
5863
<dt>DNS Name</dt>
5964
<dd>{{ vm.name }}.csh.rit.edu</dd>
6065
<dt>ID</dt>

proxstar/vm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ def resize_disk(self, disk, size):
227227
proxmox.nodes(self.node).qemu(self.id).resize.put(
228228
disk=disk, size="+{}G".format(size))
229229

230+
@retry(wait=wait_fixed(2), stop=stop_after_attempt(5))
231+
def rename_vm(self, name):
232+
proxmox = connect_proxmox()
233+
proxmox.nodes(self.node).qemu(self.id).config.put(name=name)
234+
230235
@lazy_property
231236
def expire(self):
232237
return get_vm_expire(db, self.id, app.config['VM_EXPIRE_MONTHS'])

0 commit comments

Comments
 (0)