272 lines
9.7 KiB
Python
Executable File
272 lines
9.7 KiB
Python
Executable File
import subprocess
|
|
import local
|
|
import paramiko
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
def check_ssh_credentials(hostname, username, password):
|
|
try:
|
|
client = paramiko.SSHClient()
|
|
client.load_system_host_keys()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
|
|
client.connect(hostname, username=username, password=password)
|
|
return True
|
|
|
|
except paramiko.AuthenticationException:
|
|
print("Authentication failed when connecting to %s" % hostname)
|
|
return False
|
|
except paramiko.SSHException as e:
|
|
return False
|
|
except Exception as e:
|
|
return False
|
|
|
|
def execSshCommand(hostname, username, password, command, verbose=False):
|
|
client = paramiko.SSHClient()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
|
|
try:
|
|
client.connect(hostname, username=username, password=password)
|
|
stdin, stdout, stderr = client.exec_command(command)
|
|
if verbose:
|
|
print(stdout.read().decode('utf-8'))
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
def execScpCommand(local_path, hostname, username, password, remote_path):
|
|
try:
|
|
transport = paramiko.Transport((hostname, 22))
|
|
transport.connect(username=username, password=password)
|
|
sftp = paramiko.SFTPClient.from_transport(transport)
|
|
sftp.put(local_path, remote_path)
|
|
transport.close()
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
def is_reachable(hostname, verbose=False):
|
|
try:
|
|
if verbose:
|
|
subprocess.check_output(["ping", "-c", "1", hostname])
|
|
else:
|
|
subprocess.check_output(["ping", "-c", "1", hostname], stderr=subprocess.DEVNULL)
|
|
|
|
return True
|
|
except subprocess.CalledProcessError:
|
|
return False
|
|
|
|
|
|
def editLtsccConf(hostname, username, password, id, new_content):
|
|
try:
|
|
client = paramiko.SSHClient()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
client.connect(hostname, username=username, password=password)
|
|
_, stdout, _ = client.exec_command(f"cat /etc/lts-cc.conf")
|
|
content = stdout.read().decode('utf-8')
|
|
new_content = content.replace(id + '=', f'{id}={new_content};')
|
|
with client.open_sftp().file("/etc/lts-cc.conf", 'w') as remote_file:
|
|
remote_file.write(new_content)
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
def checkLtsccConf(hostname, username, password, config_id):
|
|
try:
|
|
client = paramiko.SSHClient()
|
|
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
client.connect(hostname, username=username, password=password)
|
|
_, stdout, _ = client.exec_command(f"cat /etc/lts-cc.conf")
|
|
|
|
content = stdout.read().decode('utf-8')
|
|
lines = content.splitlines()
|
|
|
|
return local.getConfigParam(lines, config_id)
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
finally:
|
|
client.close()
|
|
|
|
|
|
def check_proc_running(hostname, username, password, process_name, verbose=False):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname, username=username, password=password)
|
|
|
|
stdin, stdout, stderr = ssh.exec_command(f"ps aux | grep {process_name} | grep -v grep")
|
|
output = stdout.read().decode('utf-8')
|
|
ssh.close()
|
|
return process_name in output
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
return False
|
|
|
|
|
|
def checkDatabases(hostname, username, password):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname, username=username, password=password)
|
|
stdin, stdout, stderr = ssh.exec_command('psql -l')
|
|
stdout_str = stdout.read().decode('utf-8')
|
|
stderr_str = stderr.read().decode('utf-8')
|
|
ssh.close()
|
|
return local.extractDatabases(output=stdout_str)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
def check_proc_args(hostname, username, password, process_name, verbose=False):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname=hostname, username=username, password=password)
|
|
|
|
stdin, stdout, stderr = ssh.exec_command(f"ps aux | grep {process_name} | grep -v grep")
|
|
output = stdout.read().decode('utf-8')
|
|
ssh.close()
|
|
lines = output.split("-")[1:]
|
|
if lines:
|
|
return lines
|
|
else:
|
|
return None
|
|
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
return None
|
|
|
|
|
|
def checkDiskSpace(hostname, username, password):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname, username=username, password=password)
|
|
stdin, stdout, stderr = ssh.exec_command("df -h")
|
|
output = stdout.read().decode().strip()
|
|
ssh.close()
|
|
disk_usage = local.parseDiskUsage(output)
|
|
return disk_usage
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
|
|
|
|
def list_branches(hostname, username, password):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname, username=username, password=password)
|
|
stdin, stdout, stderr = ssh.exec_command(f'ls -d /shd/CTC/TOOLS/TDS/*')
|
|
output = stdout.read().decode('utf-8').splitlines()
|
|
directories = [dire.strip('/') for dire in output]
|
|
ssh.close()
|
|
return directories
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
|
|
def add_branch(hostname, username, password, svn_url):
|
|
try:
|
|
branch_name = svn_url.rstrip('/').split('/')[-1]
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname, username=username, password=password)
|
|
ssh.exec_command(f'mkdir /shd/CTC/TOOLS/TDS/source-{branch_name}')
|
|
ssh.exec_command(f'mkdir /shd/CTC/TOOLS/TDS/build-{branch_name}')
|
|
ssh.exec_command(f'svn co {svn_url} /shd/CTC/TOOLS/TDS/source-{branch_name}')
|
|
ssh.close()
|
|
return branch_name
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
|
|
def build_branch(hostname, username, password, branch_name, project_name, guez_mode, build_ws, build_sv, build_hla):
|
|
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname, username=username, password=password)
|
|
command = f'cmake -G Ninja /shd/CTC/TOOLS/TDS/source-{branch_name} -DGUEZ_MODE='
|
|
if guez_mode:
|
|
command+= "ON"
|
|
else:
|
|
command+= "OFF"
|
|
command+= " -DBUILD_TOOLSSV=ON -DBUILD_WLS=ON -DBUILD_TOOLSWS=ON -DBUILD_TESTING=ON -DBUILD_WS="
|
|
if build_ws:
|
|
command+= "ON"
|
|
else:
|
|
command+= "OFF"
|
|
|
|
command+= " -DBUILD_AAR=OFF -DBUILD_SVC_HLA="
|
|
if build_hla:
|
|
command += "ON"
|
|
else:
|
|
command += "OFF"
|
|
command+= " -DBUILD_SV="
|
|
|
|
if build_sv:
|
|
command += "ON"
|
|
else:
|
|
command += "OFF"
|
|
|
|
command+= " -D_pname="
|
|
command+= project_name
|
|
|
|
command += " -DCMAKE_BUILD_TYPE=RelWithDebInfo"
|
|
command += f" -B /shd/CTC/TOOLS/TDS/build-{branch_name}"
|
|
|
|
stderr = ssh.exec_command(command)
|
|
stderr = ssh.exec_command(f'cd /shd/CTC/TOOLS/TDS/build-{branch_name}/ && ninja')
|
|
|
|
|
|
ssh.close()
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
|
|
def build_test_db(hostname, username, password, branch_name):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname=hostname, username=username, password=password)
|
|
ssh.exec_command(f'cd /shd/CTC/TOOLS/TDS/build-{branch_name}/ && ninja test-db')
|
|
ssh.close()
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
|
|
def save_db(dbs,db_user, db_pass, db_name, verbose):
|
|
command = f"pg_dump {db_name} > {db_name}_save" + str(datetime.date)
|
|
execSshCommand(dbs, db_user, db_pass, command, verbose)
|
|
|
|
def execExternalApp(host, user, password, path, params):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname=host, username=user, password=password)
|
|
transport = ssh.get_transport()
|
|
session = transport.open_session()
|
|
session.request_x11()
|
|
command = path + " " + params
|
|
session.exec_command(command)
|
|
session.recv_exit_status()
|
|
ssh.close()
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server")
|
|
|
|
def restart_server(hostname, username, password):
|
|
try:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
|
|
ssh.connect(hostname=hostname, username="root", password="!Mout11de")
|
|
transport = ssh.get_transport()
|
|
session =transport.open_session()
|
|
session.request_x11()
|
|
ssh.exec_command(f'systemctl restart lts_sv')
|
|
session.recv_exit_status()
|
|
ssh.close()
|
|
except (paramiko.AuthenticationException, paramiko.SSHException) as e:
|
|
print("Fehler bei Verbindung zum Server") |