mirror of
https://github.com/ComputerScienceHouse/proxstar.git
synced 2025-02-14 22:11:51 +00:00
enabling VM stop on expire day, add logging for template provisioning
This commit is contained in:
parent
c2c91b9f37
commit
451f992d8f
2 changed files with 21 additions and 3 deletions
|
@ -90,8 +90,7 @@ def process_expiring_vms_task():
|
||||||
name = get_vm_config(proxmox, vmid)['name']
|
name = get_vm_config(proxmox, vmid)['name']
|
||||||
expiring_vms.append([name, days])
|
expiring_vms.append([name, days])
|
||||||
if days == 0:
|
if days == 0:
|
||||||
#change_vm_power(proxmox, vmid, 'stop')
|
change_vm_power(proxmox, vmid, 'stop')
|
||||||
pass
|
|
||||||
if expiring_vms:
|
if expiring_vms:
|
||||||
send_vm_expire_email('com6056', expiring_vms)
|
send_vm_expire_email('com6056', expiring_vms)
|
||||||
|
|
||||||
|
@ -109,16 +108,27 @@ def setup_template(template_id, name, user, password, cores, memory):
|
||||||
proxmox = connect_proxmox()
|
proxmox = connect_proxmox()
|
||||||
starrs = connect_starrs()
|
starrs = connect_starrs()
|
||||||
db = connect_db()
|
db = connect_db()
|
||||||
|
print("[{}] Retrieving template info for template {}.".format(
|
||||||
|
name, template_id))
|
||||||
template = get_template(db, template_id)
|
template = get_template(db, template_id)
|
||||||
|
print("[{}] Cloning template {}.".format(name, template_id))
|
||||||
vmid, mac = clone_vm(proxmox, template_id, name, user)
|
vmid, mac = clone_vm(proxmox, template_id, name, user)
|
||||||
|
print("[{}] Registering in STARRS.".format(name))
|
||||||
ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE'])
|
ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE'])
|
||||||
register_starrs(starrs, name, app.config['STARRS_USER'], mac, ip)
|
register_starrs(starrs, name, app.config['STARRS_USER'], mac, ip)
|
||||||
get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
|
get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS'])
|
||||||
|
print("[{}] Setting CPU and memory.".format(name))
|
||||||
change_vm_cpu(proxmox, vmid, cores)
|
change_vm_cpu(proxmox, vmid, cores)
|
||||||
change_vm_mem(proxmox, vmid, memory)
|
change_vm_mem(proxmox, vmid, memory)
|
||||||
|
print(
|
||||||
|
"[{}] Waiting for STARRS to propogate before starting VM.".format(
|
||||||
|
name))
|
||||||
time.sleep(90)
|
time.sleep(90)
|
||||||
|
print("[{}] Starting VM.".format(name))
|
||||||
change_vm_power(proxmox, vmid, 'start')
|
change_vm_power(proxmox, vmid, 'start')
|
||||||
|
print("[{}] Waiting for VM to start before SSHing.".format(name))
|
||||||
time.sleep(20)
|
time.sleep(20)
|
||||||
|
print("[{}] Creating SSH session.".format(name))
|
||||||
client = paramiko.SSHClient()
|
client = paramiko.SSHClient()
|
||||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
retry = 0
|
retry = 0
|
||||||
|
@ -132,17 +142,22 @@ def setup_template(template_id, name, user, password, cores, memory):
|
||||||
except:
|
except:
|
||||||
retry += 1
|
retry += 1
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
print("[{}] Running user creation commands.".format(name))
|
||||||
stdin, stdout, stderr = client.exec_command("useradd {}".format(user))
|
stdin, stdout, stderr = client.exec_command("useradd {}".format(user))
|
||||||
exit_status = stdout.channel.recv_exit_status()
|
exit_status = stdout.channel.recv_exit_status()
|
||||||
|
print(exit_status)
|
||||||
root_password = gen_password(32)
|
root_password = gen_password(32)
|
||||||
stdin, stdout, stderr = client.exec_command(
|
stdin, stdout, stderr = client.exec_command(
|
||||||
"echo '{}' | passwd root --stdin".format(root_password))
|
"echo '{}' | passwd root --stdin".format(root_password))
|
||||||
exit_status = stdout.channel.recv_exit_status()
|
exit_status = stdout.channel.recv_exit_status()
|
||||||
|
print(exit_status)
|
||||||
stdin, stdout, stderr = client.exec_command(
|
stdin, stdout, stderr = client.exec_command(
|
||||||
"echo '{}' | passwd '{}' -e --stdin".format(password, user))
|
"echo '{}' | passwd '{}' -e --stdin".format(password, user))
|
||||||
exit_status = stdout.channel.recv_exit_status()
|
exit_status = stdout.channel.recv_exit_status()
|
||||||
|
print(exit_status)
|
||||||
stdin, stdout, stderr = client.exec_command(
|
stdin, stdout, stderr = client.exec_command(
|
||||||
"echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format(
|
"echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format(
|
||||||
user))
|
user))
|
||||||
exit_status = stdout.channel.recv_exit_status()
|
exit_status = stdout.channel.recv_exit_status()
|
||||||
client.close()
|
client.close()
|
||||||
|
print("[{}] Template successfully provisioned.".format(name))
|
||||||
|
|
|
@ -2,6 +2,9 @@ import string
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def gen_password(length, charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"):
|
def gen_password(
|
||||||
|
length,
|
||||||
|
charset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
|
||||||
|
):
|
||||||
# use secrets module once this works in python 3.6
|
# use secrets module once this works in python 3.6
|
||||||
return ''.join(random.choice(charset) for x in range(length))
|
return ''.join(random.choice(charset) for x in range(length))
|
||||||
|
|
Loading…
Reference in a new issue