| 
 | 1 | +import os  | 
 | 2 | +import subprocess  | 
 | 3 | +import argparse  | 
 | 4 | + | 
 | 5 | + | 
 | 6 | +def handle_args():  | 
 | 7 | +    parser = argparse.ArgumentParser()  | 
 | 8 | +    parser.add_argument('--tagbase-server-home', type=str)  | 
 | 9 | +    parser.add_argument('--installation-user', type=str)  | 
 | 10 | +    parser.add_argument('--prod-servers', type=str, nargs='*')  | 
 | 11 | +    parser.add_argument('--dev-servers', type=str, nargs='*')  | 
 | 12 | +    parser.add_argument('--dev-servers-sshkey-path', type=str)  | 
 | 13 | +    parser.add_argument('--prod-servers-sshkey-path', type=str)  | 
 | 14 | +    return vars(parser.parse_args())  | 
 | 15 | + | 
 | 16 | + | 
 | 17 | +def log_tb(msg):  | 
 | 18 | +    print("[TAGBASE] {}".format(msg))  | 
 | 19 | + | 
 | 20 | + | 
 | 21 | +def create_ansible_servers_file(inventory_file_path, prod_servers, dev_servers, installation_user, dev_servers_sshkey_path, prod_servers_sshkey_path):  | 
 | 22 | +    with open(inventory_file_path, 'w') as inv_file:  | 
 | 23 | +        if prod_servers != None:  | 
 | 24 | +            inv_file.write("[prod_servers]\n")  | 
 | 25 | +        for prod_server in prod_servers:  | 
 | 26 | +            inv_file.write("{}\n".format(prod_server))  | 
 | 27 | +        inv_file.write("[prod_servers:vars]\n")  | 
 | 28 | +        inv_file.write("ansible_user={}\n".format(installation_user))  | 
 | 29 | +        inv_file.write("ansible_ssh_private_key_file={}\n".format(prod_servers_sshkey_path))  | 
 | 30 | + | 
 | 31 | +        if dev_servers != None:  | 
 | 32 | +            inv_file.write("[dev_servers]\n")  | 
 | 33 | +        for dev_server in dev_servers:  | 
 | 34 | +            inv_file.write("{}\n".format(dev_server))  | 
 | 35 | +        inv_file.write("[dev_servers:vars]\n")  | 
 | 36 | +        inv_file.write("ansible_user={}\n".format(installation_user))  | 
 | 37 | +        inv_file.write("ansible_ssh_private_key_file={}\n".format(dev_servers_sshkey_path))  | 
 | 38 | + | 
 | 39 | +    log_tb("Completed writing {}".format(inventory_file_path))  | 
 | 40 | + | 
 | 41 | + | 
 | 42 | +def run_ansible_inventory_file(tagserver_home, inventory_file_path):  | 
 | 43 | +    playbook_file_path = "{}/tagserver_playbook.yaml".format(tagserver_home)  | 
 | 44 | +    ansible_cmd = "ansible-playbook -i {} {} -v".format(inventory_file_path, playbook_file_path)  | 
 | 45 | +    subprocess.run(ansible_cmd, shell=True)  | 
 | 46 | +    log_tb("Completed configuring servers")  | 
 | 47 | + | 
 | 48 | + | 
 | 49 | +if __name__ == "__main__":  | 
 | 50 | +    args = handle_args()  | 
 | 51 | +    tagbase_server_home = args['tagbase_server_home']  | 
 | 52 | +    tagbase_server_devops_home = '{}/{}'.format(tagbase_server_home, 'tagbase-server/devops')  | 
 | 53 | +    inventory_file_path = '{}/inventory_2'.format(tagbase_server_devops_home)  | 
 | 54 | +    create_ansible_servers_file(inventory_file_path, args['prod_servers'], args['dev_servers'], args['installation_user'], args['dev_servers_sshkey_path'], args['prod_servers_sshkey_path'])  | 
 | 55 | +    #run_ansible_inventory_file(tagserver_home, inventory_file_path)  | 
0 commit comments