check hostname for invalid characters

This commit is contained in:
Jordan Rodgers 2017-12-10 17:02:49 -05:00
parent 29873e2055
commit f3d69381c1
4 changed files with 29 additions and 11 deletions

9
app.py
View file

@ -46,8 +46,13 @@ def hostname(name):
starrs = connect_starrs(
app.config['STARRS_DB_NAME'], app.config['STARRS_DB_USER'],
app.config['STARRS_DB_HOST'], app.config['STARRS_DB_PASS'])
result = check_hostname(starrs, name)
return str(result)
valid, available = check_hostname(starrs, name)
if not valid:
return 'invalid'
if not available:
return 'taken'
else:
return 'ok'
@app.route("/vm/<string:vmid>")

View file

@ -140,7 +140,7 @@ def get_user_usage(proxmox, user):
usage['cpu'] += int(config['cores'] * config.get('sockets', 1))
usage['mem'] += (int(config['memory']) // 1024)
for disk in get_vm_disks(proxmox, vm['vmid'], config):
usage['disk'] += int(disk[1][:-1])
usage['disk'] += int(disk[1])
return usage

View file

@ -1,6 +1,5 @@
import psycopg2
def connect_starrs(db, user, host, password):
try:
starrs = psycopg2.connect(
@ -43,12 +42,24 @@ def check_hostname(starrs, hostname):
try:
c.execute("BEGIN")
c.callproc("api.initialize", ('root', ))
c.callproc("api.check_dns_hostname", (hostname, 'csh.rit.edu'))
results = c.fetchall()
c.callproc("api.validate_name", (hostname,))
valid = False
if hostname == c.fetchall()[0][0]:
valid = True
c.execute("COMMIT")
c.execute("BEGIN")
c.callproc("api.initialize", ('root', ))
c.callproc("api.check_dns_hostname", (hostname, 'csh.rit.edu'))
available = False
if not c.fetchall()[0][0]:
available = True
c.execute("COMMIT")
except(psycopg2.InternalError):
valid = False
available = False
finally:
c.close()
return results[0][0]
return valid, available
def register_starrs(starrs, name, owner, mac, addr):

View file

@ -367,7 +367,7 @@ $("#renew-vm").click(function(){
credentials: 'same-origin',
method: 'post'
}).then((response) => {
return swal(`${vmname} is now renewing!`, {
return swal(`${vmname} has been renewed!`, {
icon: "success",
});
}).then(() => {
@ -394,7 +394,7 @@ $("#create-vm").click(function(){
}).then((response) => {
return response.text()
}).then((text) => {
if (text == "False") {
if (text == 'ok') {
var loader = document.createElement('div');
loader.setAttribute('class', 'loader');
var info = document.createElement('span');
@ -431,8 +431,10 @@ $("#create-vm").click(function(){
});
}
});
} else {
swal("Uh oh...", `That name is already taken! Please try another name.`, "error");
} else if (text == 'invalid') {
swal("Uh oh...", `That name is not a valid name! Please try another name.`, "error");
} else if (text == 'taken') {
swal("Uh oh...", `That name is not available! Please try another name.`, "error");
}
}).catch(err => {
if (err) {