mirror of
				https://github.com/ComputerScienceHouse/proxstar.git
				synced 2025-03-09 15:40:09 +00:00 
			
		
		
		
	remove ssh queue
This commit is contained in:
		
							parent
							
								
									f5a36d6f1e
								
							
						
					
					
						commit
						4a2df4d979
					
				
					 2 changed files with 27 additions and 40 deletions
				
			
		| 
						 | 
					@ -1,7 +1,5 @@
 | 
				
			||||||
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
 | 
				
			||||||
| 
						 | 
					@ -21,9 +19,6 @@ 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'])
 | 
				
			||||||
| 
						 | 
					@ -111,7 +106,6 @@ 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()
 | 
				
			||||||
| 
						 | 
					@ -125,34 +119,30 @@ def setup_template(template_id, name, user, password, cores, memory):
 | 
				
			||||||
        time.sleep(90)
 | 
					        time.sleep(90)
 | 
				
			||||||
        change_vm_power(proxmox, vmid, 'start')
 | 
					        change_vm_power(proxmox, vmid, 'start')
 | 
				
			||||||
        time.sleep(20)
 | 
					        time.sleep(20)
 | 
				
			||||||
        q.enqueue(setup_template_ssh, ip, template, user, password)
 | 
					        client = paramiko.SSHClient()
 | 
				
			||||||
 | 
					        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 | 
				
			||||||
 | 
					        retry = 0
 | 
				
			||||||
def setup_template_ssh(ip, template, user, password):
 | 
					        while retry < 30:
 | 
				
			||||||
    client = paramiko.SSHClient()
 | 
					            try:
 | 
				
			||||||
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 | 
					                client.connect(
 | 
				
			||||||
    retry = 0
 | 
					                    ip,
 | 
				
			||||||
    while retry < 30:
 | 
					                    username=template['username'],
 | 
				
			||||||
        try:
 | 
					                    password=template['password'])
 | 
				
			||||||
            client.connect(
 | 
					                break
 | 
				
			||||||
                ip,
 | 
					            except:
 | 
				
			||||||
                username=template['username'],
 | 
					                retry += 1
 | 
				
			||||||
                password=template['password'])
 | 
					                time.sleep(3)
 | 
				
			||||||
            break
 | 
					        stdin, stdout, stderr = client.exec_command("useradd {}".format(user))
 | 
				
			||||||
        except:
 | 
					        exit_status = stdout.channel.recv_exit_status()
 | 
				
			||||||
            retry += 1
 | 
					        root_password = gen_password(32)
 | 
				
			||||||
            time.sleep(3)
 | 
					        stdin, stdout, stderr = client.exec_command(
 | 
				
			||||||
    stdin, stdout, stderr = client.exec_command("useradd {}".format(user))
 | 
					            "echo '{}' | passwd root --stdin".format(root_password))
 | 
				
			||||||
    exit_status = stdout.channel.recv_exit_status()
 | 
					        exit_status = stdout.channel.recv_exit_status()
 | 
				
			||||||
    root_password = gen_password(32)
 | 
					        stdin, stdout, stderr = client.exec_command(
 | 
				
			||||||
    stdin, stdout, stderr = client.exec_command(
 | 
					            "echo '{}' | passwd '{}' -e --stdin".format(password, user))
 | 
				
			||||||
        "echo '{}' | passwd root --stdin".format(root_password))
 | 
					        exit_status = stdout.channel.recv_exit_status()
 | 
				
			||||||
    exit_status = stdout.channel.recv_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 '{}' | passwd '{}' -e --stdin".format(password, user))
 | 
					                user))
 | 
				
			||||||
    exit_status = stdout.channel.recv_exit_status()
 | 
					        exit_status = stdout.channel.recv_exit_status()
 | 
				
			||||||
    stdin, stdout, stderr = client.exec_command(
 | 
					        client.close()
 | 
				
			||||||
        "echo '{} ALL=(ALL:ALL) ALL' | sudo EDITOR='tee -a' visudo".format(
 | 
					 | 
				
			||||||
            user))
 | 
					 | 
				
			||||||
    exit_status = stdout.channel.recv_exit_status()
 | 
					 | 
				
			||||||
    client.close()
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +0,0 @@
 | 
				
			||||||
#!/bin/sh
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/opt/app-root/bin/rq worker -u "$PROXSTAR_REDIS_URL" --sentry-dsn "$PROXSTAR_SENTRY_DSN" ssh
 | 
					 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue