remove extra logging, add separate queue for ssh tasks for python2

This commit is contained in:
Jordan Rodgers 2018-02-09 22:05:48 -05:00
parent 7514293fff
commit 615549502e
2 changed files with 40 additions and 40 deletions

View file

@ -1,5 +1,7 @@
import os import os
import paramiko import paramiko
from rq import Queue
from redis import Redis
from flask import Flask from flask import Flask
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
@ -19,6 +21,9 @@ else:
config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py") config = os.path.join(app.config.get('ROOT_DIR', os.getcwd()), "config.py")
app.config.from_pyfile(config) app.config.from_pyfile(config)
redis_conn = Redis(app.config['REDIS_HOST'], app.config['REDIS_PORT'])
q = Queue(connection=redis_conn)
def connect_db(): def connect_db():
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI']) 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): def setup_template(template_id, name, user, password, cores, memory):
with app.app_context(): with app.app_context():
q = Queue('ssh', connection=redis_conn)
proxmox = connect_proxmox() proxmox = connect_proxmox()
starrs = connect_starrs() starrs = connect_starrs()
db = connect_db() db = connect_db()
template = get_template(db, template_id) template = get_template(db, template_id)
print('clone')
vmid, mac = clone_vm(proxmox, template_id, name, user) vmid, mac = clone_vm(proxmox, template_id, name, user)
print('starrs')
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('set cpu mem')
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('wait to start')
time.sleep(90) time.sleep(90)
print('start')
change_vm_power(proxmox, vmid, 'start') change_vm_power(proxmox, vmid, 'start')
print('wait after start')
time.sleep(20) time.sleep(20)
client = paramiko.SSHClient() q.enqueue(setup_template_ssh, ip, template, user, password)
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
retry = 0
while retry < 30: def setup_template_ssh(ip, template, user, password):
try: client = paramiko.SSHClient()
print('try ssh') client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print(ip) retry = 0
print(template['username']) while retry < 30:
print(template['password']) try:
client.connect( client.connect(
ip, ip,
username=template['username'], username=template['username'],
password=template['password']) password=template['password'])
break break
except: except:
print('ssh failed') retry += 1
retry += 1 time.sleep(3)
time.sleep(3) stdin, stdout, stderr = client.exec_command("useradd {}".format(user))
print('start commands') exit_status = stdout.channel.recv_exit_status()
stdin, stdout, stderr = client.exec_command("useradd {}".format(user)) root_password = gen_password(32)
exit_status = stdout.channel.recv_exit_status() stdin, stdout, stderr = client.exec_command(
root_password = gen_password(32) "echo '{}' | passwd root --stdin".format(root_password))
stdin, stdout, stderr = client.exec_command( exit_status = stdout.channel.recv_exit_status()
"echo '{}' | passwd root --stdin".format(root_password)) stdin, stdout, stderr = client.exec_command(
exit_status = stdout.channel.recv_exit_status() "echo '{}' | passwd '{}' -e --stdin".format(password, user))
stdin, stdout, stderr = client.exec_command( exit_status = stdout.channel.recv_exit_status()
"echo '{}' | passwd '{}' --stdin".format(password, user)) stdin, stdout, stderr = client.exec_command(
exit_status = stdout.channel.recv_exit_status() "echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format(
stdin, stdout, stderr = client.exec_command( user))
"echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format( exit_status = stdout.channel.recv_exit_status()
user)) client.close()
exit_status = stdout.channel.recv_exit_status()
print('done')
client.close()

3
start_worker_ssh.sh Executable file
View file

@ -0,0 +1,3 @@
#!/bin/sh
/opt/app-root/bin/rq worker -u "$PROXSTAR_REDIS_URL" --sentry-dsn "$PROXSTAR_SENTRY_DSN" ssh