diff --git a/proxstar/tasks.py b/proxstar/tasks.py index 2f1a608..fcf3d52 100644 --- a/proxstar/tasks.py +++ b/proxstar/tasks.py @@ -1,5 +1,7 @@ import os import paramiko +from rq import Queue +from redis import Redis from flask import Flask from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker @@ -19,6 +21,9 @@ else: config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py") app.config.from_pyfile(config) +redis_conn = Redis(app.config['REDIS_HOST'], app.config['REDIS_PORT']) +q = Queue(connection=redis_conn) + def connect_db(): engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI']) @@ -106,56 +111,48 @@ def generate_pool_cache_task(): def setup_template(template_id, name, user, password, cores, memory): with app.app_context(): + q = Queue('ssh', connection=redis_conn) proxmox = connect_proxmox() starrs = connect_starrs() db = connect_db() template = get_template(db, template_id) - print('clone') vmid, mac = clone_vm(proxmox, template_id, name, user) - print('starrs') ip = get_next_ip(starrs, app.config['STARRS_IP_RANGE']) register_starrs(starrs, name, app.config['STARRS_USER'], mac, ip) get_vm_expire(db, vmid, app.config['VM_EXPIRE_MONTHS']) - print('set cpu mem') change_vm_cpu(proxmox, vmid, cores) change_vm_mem(proxmox, vmid, memory) - print('wait to start') time.sleep(90) - print('start') change_vm_power(proxmox, vmid, 'start') - print('wait after start') time.sleep(20) - client = paramiko.SSHClient() - client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - retry = 0 - while retry < 30: - try: - print('try ssh') - print(ip) - print(template['username']) - print(template['password']) - client.connect( - ip, - username=template['username'], - password=template['password']) - break - except: - print('ssh failed') - retry += 1 - time.sleep(3) - print('start commands') - stdin, stdout, stderr = client.exec_command("useradd {}".format(user)) - exit_status = stdout.channel.recv_exit_status() - root_password = gen_password(32) - stdin, stdout, stderr = client.exec_command( - "echo '{}' | passwd root --stdin".format(root_password)) - exit_status = stdout.channel.recv_exit_status() - stdin, stdout, stderr = client.exec_command( - "echo '{}' | passwd '{}' --stdin".format(password, user)) - exit_status = stdout.channel.recv_exit_status() - stdin, stdout, stderr = client.exec_command( - "echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format( - user)) - exit_status = stdout.channel.recv_exit_status() - print('done') - client.close() + q.enqueue(setup_template_ssh, ip, template, user, password) + + +def setup_template_ssh(ip, template, user, password): + client = paramiko.SSHClient() + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + retry = 0 + while retry < 30: + try: + client.connect( + ip, + username=template['username'], + password=template['password']) + break + except: + retry += 1 + time.sleep(3) + stdin, stdout, stderr = client.exec_command("useradd {}".format(user)) + exit_status = stdout.channel.recv_exit_status() + root_password = gen_password(32) + stdin, stdout, stderr = client.exec_command( + "echo '{}' | passwd root --stdin".format(root_password)) + exit_status = stdout.channel.recv_exit_status() + stdin, stdout, stderr = client.exec_command( + "echo '{}' | passwd '{}' -e --stdin".format(password, user)) + exit_status = stdout.channel.recv_exit_status() + stdin, stdout, stderr = client.exec_command( + "echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format( + user)) + exit_status = stdout.channel.recv_exit_status() + client.close() diff --git a/start_worker_ssh.sh b/start_worker_ssh.sh new file mode 100755 index 0000000..80a1027 --- /dev/null +++ b/start_worker_ssh.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +/opt/app-root/bin/rq worker -u "$PROXSTAR_REDIS_URL" --sentry-dsn "$PROXSTAR_SENTRY_DSN" ssh