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( starrs = connect_starrs(
app.config['STARRS_DB_NAME'], app.config['STARRS_DB_USER'], app.config['STARRS_DB_NAME'], app.config['STARRS_DB_USER'],
app.config['STARRS_DB_HOST'], app.config['STARRS_DB_PASS']) app.config['STARRS_DB_HOST'], app.config['STARRS_DB_PASS'])
result = check_hostname(starrs, name) valid, available = check_hostname(starrs, name)
return str(result) if not valid:
return 'invalid'
if not available:
return 'taken'
else:
return 'ok'
@app.route("/vm/<string:vmid>") @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['cpu'] += int(config['cores'] * config.get('sockets', 1))
usage['mem'] += (int(config['memory']) // 1024) usage['mem'] += (int(config['memory']) // 1024)
for disk in get_vm_disks(proxmox, vm['vmid'], config): for disk in get_vm_disks(proxmox, vm['vmid'], config):
usage['disk'] += int(disk[1][:-1]) usage['disk'] += int(disk[1])
return usage return usage

View file

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

View file

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