Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a way to change both the vm name and hostname #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably shouldn't be named vm_disk

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a function to rename the name of the system in STARRS? Proxstar tracks systems by their system name, so changing everything but that will cause it to no longer be able to match up a STARRS record with a VM.

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}`, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to do something like /vm/${vmid}/rename and include the new name in the POST data

credentials: 'same-origin',
method: 'post'
}).then((response) => {
return swal(`VM Name has been changes!`, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowercase name and changed

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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowercase name

} 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