예제 #1
0
def install_haproxy(serv, **kwargs):
    import sql
    script = "install_haproxy.sh"
    haproxy_sock_port = sql.get_setting('haproxy_sock_port')
    stats_port = sql.get_setting('stats_port')
    server_state_file = sql.get_setting('server_state_file')
    stats_user = sql.get_setting('stats_user')
    stats_password = sql.get_setting('stats_password')
    proxy = sql.get_setting('proxy')
    hapver = kwargs.get('hapver')
    fullpath = get_config_var('main', 'fullpath')
    ssh_enable = ''
    ssh_port = ''
    ssh_user_name = ''
    ssh_user_password = ''

    for sshs in sql.select_ssh(serv=serv):
        ssh_enable = sshs[3]
        ssh_user_name = sshs[4]
        ssh_user_password = sshs[5]
        ssh_key_name = fullpath + '/keys/%s.pem' % sshs[2]

    os.system("cp scripts/%s ." % script)

    if hapver is None:
        hapver = '2.0.7-1'

    proxy_serv = proxy if proxy is not None else ''
    syn_flood_protect = '1' if kwargs.get('syn_flood') == "1" else ''

    commands = [
        "chmod +x " + script + " &&  ./" + script + " PROXY=" + proxy_serv +
        " SOCK_PORT=" + haproxy_sock_port + " STAT_PORT=" + stats_port +
        " STAT_FILE=" + server_state_file + " STATS_USER="******" STATS_PASS="******" HAPVER=" + hapver + " SYN_FLOOD=" +
        syn_flood_protect + " HOST=" + serv + " USER="******" PASS="******" KEY=" + ssh_key_name
    ]

    output, error = subprocess_execute(commands[0])

    if error:
        logging('localhost', error, haproxywi=1)
        print('error: ' + error)
    else:
        for l in output:
            if "msg" in l or "FAILED" in l:
                try:
                    l = l.split(':')[1]
                    l = l.split('"')[1]
                    print(l + "<br>")
                    break
                except:
                    print(output)
                    break
        else:
            print('success: HAProxy was installed<br>')

    os.system("rm -f %s" % script)
예제 #2
0
def ssh_connect(serv, **kwargs):
    import paramiko
    from paramiko import SSHClient
    import sql
    fullpath = get_config_var('main', 'fullpath')
    ssh_enable = ''
    ssh_port = ''
    ssh_user_name = ''
    ssh_user_password = ''

    for sshs in sql.select_ssh(serv=serv):
        ssh_enable = sshs[3]
        ssh_user_name = sshs[4]
        ssh_user_password = sshs[5]
        ssh_key_name = fullpath + '/keys/%s.pem' % sshs[2]

    servers = sql.select_servers(server=serv)
    for server in servers:
        ssh_port = server[10]

    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        if ssh_enable == 1:
            k = paramiko.RSAKey.from_private_key_file(ssh_key_name)
            ssh.connect(hostname=serv,
                        port=ssh_port,
                        username=ssh_user_name,
                        pkey=k)
        else:
            ssh.connect(hostname=serv,
                        port=ssh_port,
                        username=ssh_user_name,
                        password=ssh_user_password)
        return ssh
    except paramiko.AuthenticationException:
        return 'Authentication failed, please verify your credentials'
        pass
    except paramiko.SSHException as sshException:
        return 'Unable to establish SSH connection: %s ' % sshException
        pass
    except paramiko.BadHostKeyException as badHostKeyException:
        return 'Unable to verify server\'s host key: %s ' % badHostKeyException
        pass
    except Exception as e:
        if e == "No such file or directory":
            return '%s. Check ssh key' % e
            pass
        elif e == "Invalid argument":
            error = 'Check the IP of the server'
            pass
        else:
            error = e
            pass
        return str(error)
예제 #3
0
def return_ssh_keys_path(serv, **kwargs):
    import sql
    full_path = get_config_var('main', 'fullpath')
    ssh_enable = ''
    ssh_user_name = ''
    ssh_user_password = ''

    if kwargs.get('id'):
        for sshs in sql.select_ssh(id=kwargs.get('id')):
            ssh_enable = sshs[2]
            ssh_user_name = sshs[3]
            ssh_user_password = sshs[4]
            ssh_key_name = full_path + '/keys/%s.pem' % sshs[1]
    else:
        for sshs in sql.select_ssh(serv=serv):
            ssh_enable = sshs[3]
            ssh_user_name = sshs[4]
            ssh_user_password = sshs[5]
            ssh_key_name = full_path + '/keys/%s.pem' % sshs[2]

    return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name
예제 #4
0
def return_ssh_keys_path(serv):
    import sql
    fullpath = get_config_var('main', 'fullpath')
    ssh_enable = ''
    ssh_port = ''
    ssh_user_name = ''
    ssh_user_password = ''

    for sshs in sql.select_ssh(serv=serv):
        ssh_enable = sshs[3]
        ssh_user_name = sshs[4]
        ssh_user_password = sshs[5]
        ssh_key_name = fullpath + '/keys/%s.pem' % sshs[2]

    return ssh_enable, ssh_user_name, ssh_user_password, ssh_key_name
예제 #5
0
파일: users.py 프로젝트: wmsonmx/haproxy-wi
funct.check_login()
funct.page_for_admin()
try:
    cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
    user_id = cookie.get('uuid')
    user = sql.get_user_name_by_uuid(user_id.value)
    users = sql.select_users()
    servers = sql.get_dick_permit()
    token = sql.get_token(user_id.value)
    settings = sql.get_setting('', all=1)
    ldap_enable = sql.get_setting('ldap_enable')
except:
    pass

template = template.render(title="Admin area: users manage",
                           role=sql.get_user_role_by_uuid(user_id.value),
                           user=user,
                           users=users,
                           groups=sql.select_groups(),
                           servers=sql.select_servers(full=1),
                           roles=sql.select_roles(),
                           masters=sql.select_servers(get_master_servers=1),
                           sshs=sql.select_ssh(),
                           telegrams=sql.select_telegram(),
                           token=token,
                           versions=funct.versions(),
                           settings=settings,
                           ldap_enable=ldap_enable)
print(template)
예제 #6
0
def ssh_connect(serv, **kwargs):
    import sql
    fullpath = get_config_var('main', 'fullpath')
    for sshs in sql.select_ssh(serv=serv):
        ssh_enable = sshs[3]
        ssh_user_name = sshs[4]
        ssh_user_password = sshs[5]
        ssh_key_name = fullpath + '/keys/%s.pem' % sshs[2]
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        if ssh_enable == 1:
            k = paramiko.RSAKey.from_private_key_file(ssh_key_name)
            ssh.connect(hostname=serv, username=ssh_user_name, pkey=k)
        else:
            ssh.connect(hostname=serv,
                        username=ssh_user_name,
                        password=ssh_user_password)
        if kwargs.get('check'):
            return True
        else:
            return ssh
    except paramiko.AuthenticationException:
        if kwargs.get('check'):
            print(
                '<div class="alert alert-danger">Authentication failed, please verify your credentials</div>'
            )
            return False
        else:
            return 'Authentication failed, please verify your credentials'
            pass
    except paramiko.SSHException as sshException:
        if kwargs.get('check'):
            print(
                '<div class="alert alert-danger">Unable to establish SSH connection: %s </div>'
                % sshException)
            return False
        else:
            return 'Unable to establish SSH connection: %s ' % sshException
            pass
    except paramiko.BadHostKeyException as badHostKeyException:
        if kwargs.get('check'):
            print(
                '<div class="alert alert-danger">Unable to verify server\'s host key: %s </div>'
                % badHostKeyException)
            return False
        else:
            return 'Unable to verify server\'s host key: %s ' % badHostKeyException
            pass
    except Exception as e:
        if e.args[1] == "No such file or directory":
            if kwargs.get('check'):
                print(
                    '<div class="alert alert-danger">{}. Check ssh key</div>'.
                    format(e.args[1]))
            else:
                return '{}. Check ssh key'.format(e.args[1])
                pass
        elif e.args[1] == "Invalid argument":
            if kwargs.get('check'):
                print(
                    '<div class="alert alert-danger">Check the IP of the new server</div>'
                )
            else:
                error = 'Check the IP of the new server'
                pass
        else:
            if kwargs.get('check'):
                print('<div class="alert alert-danger">{}</div>'.format(
                    e.args[1]))
            else:
                error = e.args[1]
                pass
        if kwargs.get('check'):
            return False
        else:
            return error
예제 #7
0
    master = form.getvalue('master')
    slave = form.getvalue('slave')
    ETH = form.getvalue('interface')
    IP = form.getvalue('vrrpip')
    syn_flood = form.getvalue('syn_flood')
    script = "install_keepalived.sh"
    fullpath = funct.get_config_var('main', 'fullpath')
    proxy = sql.get_setting('proxy')
    ssh_enable = ''
    ssh_port = ''
    ssh_user_name = ''
    ssh_user_password = ''

    proxy_serv = proxy if proxy is not None else ""

    for sshs in sql.select_ssh(serv=master):
        ssh_enable = sshs[3]
        ssh_user_name = sshs[4]
        ssh_user_password = sshs[5]
        ssh_key_name = fullpath + '/keys/%s.pem' % sshs[2]

    os.system("cp scripts/%s ." % script)

    if form.getvalue('hap') == "1":
        funct.install_haproxy(master)
        funct.install_haproxy(slave)

    commands = [
        "chmod +x " + script + " &&  ./" + script + " PROXY=" + proxy_serv +
        " ETH=" + ETH + " IP=" + str(IP) + " MASTER=MASTER" + " SYN_FLOOD=" +
        syn_flood + " HOST=" + str(master) + " USER=" + str(ssh_user_name) +
예제 #8
0
funct.check_login()
funct.page_for_admin(level=2)
try:
    user, user_id, role, token, servers = funct.get_users_params()
    ldap_enable = sql.get_setting('ldap_enable')
    user_group = funct.get_user_group(id=1)
    settings = sql.get_setting('', all=1)
    geoip_country_codes = sql.select_geoip_country_codes()

except Exception as e:
    pass

output_from_parsed_template = template.render(
    title="Servers: ",
    role=role,
    user=user,
    users=sql.select_users(group=user_group),
    groups=sql.select_groups(),
    servers=sql.get_dick_permit(virt=1, disable=0, only_group=1),
    roles=sql.select_roles(),
    masters=sql.select_servers(get_master_servers=1, uuid=user_id.value),
    group=user_group,
    sshs=sql.select_ssh(group=user_group),
    token=token,
    settings=settings,
    backups=sql.select_backups(),
    page="servers.py",
    geoip_country_codes=geoip_country_codes,
    ldap_enable=ldap_enable)
print(output_from_parsed_template)
예제 #9
0
					l = l.split(':')[1]
					l = l.split('"')[1]
					print(l+"<br>")
					break
				except:
					print(output)
					break
		else:			
			if deljob == '' and update == '':
				if sql.insert_backup_job(server, rserver, rpath, type, time, cred, description):
					funct.logging('backup ', ' has created a new backup job for server '+server , haproxywi=1, login=1)
					import http.cookies
					from jinja2 import Environment, FileSystemLoader
					env = Environment(loader=FileSystemLoader('templates/ajax'))
					template = env.get_template('new_backup.html')
					template = template.render(backups=sql.select_backups(server=server, rserver=rserver), sshs=sql.select_ssh())											
					print(template)
					print('success: Backup job has created<br>')
				else:
					print('error: Cannot add job into DB<br>')
			elif deljob:
				sql.delete_backups(deljob)
				print('Ok')
				funct.logging('backup ', ' has deleted a backup job for server '+server, haproxywi=1, login=1)
			elif update:
				sql.update_backup(server, rserver, rpath, type, time, cred, description, update)
				print('Ok')
				funct.logging('backup ', ' has updated a backup job for server '+server, haproxywi=1, login=1)
				
				
if form.getvalue('install_nginx'):