|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import winrm |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import List |
| 7 | +from quali_api_helper import QualiAPISession |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class ConfigExecutionServer: |
| 12 | + es_name: str # name of es in cloudshell |
| 13 | + user: str |
| 14 | + password: str |
| 15 | + address: str |
| 16 | + |
| 17 | + |
| 18 | +def get_cli_server_list() -> List[str]: |
| 19 | + CLI = argparse.ArgumentParser() |
| 20 | + CLI.add_argument("--servers", nargs="*", type=str) |
| 21 | + args = CLI.parse_args() |
| 22 | + return args.servers |
| 23 | + |
| 24 | + |
| 25 | +def get_target_config_servers(target_servers: List[str], config_servers: List[ConfigExecutionServer]): |
| 26 | + target_config_servers = [] |
| 27 | + conf_server_names = [x.es_name for x in config_servers] |
| 28 | + conf_servers_dict = {x.es_name: x for x in config_servers} |
| 29 | + for server in target_servers: |
| 30 | + if server not in conf_server_names: |
| 31 | + raise ValueError(f"can't find target server {server} in config server json") |
| 32 | + target_config_servers.append(conf_servers_dict[server]) |
| 33 | + return target_config_servers |
| 34 | + |
| 35 | + |
| 36 | +def validate_servers_not_running(api: QualiAPISession, target_servers: List[str]): |
| 37 | + for server in target_servers: |
| 38 | + server_details = api.get_execution_server_details(server) |
| 39 | + running_jobs = server_details["running"] |
| 40 | + if running_jobs > 0: |
| 41 | + raise ValueError(f"Server {server} has {running_jobs} running jobs. Stopping") |
| 42 | + |
| 43 | + |
| 44 | +def run_git_flow(config_servers: List[ConfigExecutionServer], remote_repo_path: str): |
| 45 | + for server in config_servers: |
| 46 | + session = winrm.Session(target=server.address, auth=(server.user, server.password)) |
| 47 | + session.run_ps(f"Test-NetConnection -Computername {server.address} -Port 5985") |
| 48 | + session.run_ps(f"Test-WSMAN {server.address}") |
| 49 | + session.run_ps(f"Set-Location {remote_repo_path};git pull") |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == "__main__": |
| 53 | + api = QualiAPISession("localhost", "admin", "admin") |
| 54 | + REMOTE_REPO_PATH = "C:/testshell-repo/Tests" |
| 55 | + with open("servers.json") as f: |
| 56 | + data = json.load(f) |
| 57 | + config_servers = [ConfigExecutionServer(x["hostname"], x["user"], x["password"], x["address"]) for x in data] |
| 58 | + target_servers = get_cli_server_list() |
| 59 | + target_config_servers = get_target_config_servers(target_servers, config_servers) |
| 60 | + validate_servers_not_running(api, target_servers) |
| 61 | + run_git_flow(target_config_servers, REMOTE_REPO_PATH) |
0 commit comments