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 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()

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