robot tcs, test charts, robot container added
[ta/cloudtaf.git] / resources / scripts / prepare_robot_bm.py
1 #!/usr/bin/python
2
3 import paramiko
4 import os
5 import sys
6
7 WORK_DIR = os.getenv('WORKDIR')
8 sys.path.append(os.path.join(WORK_DIR, 'libraries', 'common'))
9 from users import *  # noqa
10
11 REG = os.getenv('REG')
12 REG_PORT = os.getenv('REG_PORT')
13 REG_PATH = os.getenv('REG_PATH')
14
15
16 IP = os.getenv('SUT_IP')
17 CONTAINERS_DIR = os.path.join(WORK_DIR, 'resources', 'test_containers')
18 CHARTS_DIR = os.path.join(WORK_DIR, 'resources', 'test_charts')
19
20
21 def open_connection(host, user, password):
22     print"Open paramiko connection to {} with user: {} pass: {}".format(host, user, password)
23     client = paramiko.SSHClient()
24     client.load_system_host_keys()
25     client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
26     client.connect(host, username=user, password=password)
27     return client
28
29
30 def create_remote_dir(client, remote_dir):
31     execute_command(client, "rm -rf {}".format(remote_dir))
32     execute_command(client, "mkdir -p {}".format(remote_dir))
33
34
35 def delete_remote_dir(client, remote_dir):
36     execute_command(client, "rm -rf {}".format(remote_dir))
37
38
39 def execute_command(client, command):
40     _, stdout, stderr = client.exec_command(command)
41     print"The following command executed on remote: {}".format(command)
42     stdout = stdout.read()
43     print('stdout:', stdout)
44     err = stderr.read()
45     if err:
46         raise Exception("The following error occured: {}".format(err))
47     else:
48         return stdout
49
50
51 def get_all_files_in_local_dir(local_dir, extension=""):
52     all_files = list()
53     if os.path.exists(local_dir):
54         files = os.listdir(local_dir)
55         for f in files:
56             _, ext = os.path.splitext(f)
57             if extension in ext:
58                 filepath = os.path.join(local_dir, f)
59                 print "filename:" + filepath
60                 if os.path.isdir(filepath):
61                     all_files.extend(get_all_files_in_local_dir(filepath))
62                 else:
63                     all_files.append(filepath)
64     else:
65         print '{} folder does not exist'.format(local_dir)
66     return all_files
67
68
69 def upload_resources(client, local, remote):
70     sftp = client.open_sftp()
71     for f in local:
72         remote_path = os.path.join("{}{}".format(remote, f.split(remote.split('/')[-1])[1]))
73         remote_dir = remote_path.rsplit('/', 1)[0]
74         execute_command(client, "mkdir -p {}".format(remote_dir))
75         sftp.put(f, remote_path)
76     print"Upload {} from robot container to the SUT {}".format(local, remote)
77
78
79 def load_docker_images_from_directory(client, remote_dir):
80     command = "ls {}".format(remote_dir)
81     docker_images = execute_command(client, command).splitlines()
82     for image in docker_images:
83         command = "docker load -i {}/{}".format(remote_dir, image)
84         execute_command(client, command)
85         image_name = image.rsplit('.tar')[0]
86         print image_name
87         command = "docker push {}:{}/{}/{}".format(REG, REG_PORT, REG_PATH, image_name)
88         execute_command(client, command)
89
90
91 def create_helm_packages(client, remote_dir):
92     command = "helm repo list"
93     stdout = execute_command(client, command)
94     chart_repo = stdout.splitlines()[1].split()[1]
95     command = "ls {}".format(remote_dir)
96     helm_charts = execute_command(client, command).splitlines()
97     for chart in helm_charts:
98         command = "helm package {}/{}".format(remote_dir, chart)
99         helm_package_path = execute_command(client, command)
100         helm_package = helm_package_path.split(cloudadmin['username'] + '/')[1].rstrip()
101         print helm_package
102         command = "curl -sS -XPOST -H 'Content-Type: application/gzip' --data-binary @{} {}/charts/{}".format(
103             helm_package, chart_repo, helm_package)
104         execute_command(client, command)
105         command = "rm -f {}".format(helm_package_path)
106         execute_command(client, command)
107     command = "helm repo update"
108     execute_command(client, command)
109
110
111 def main():
112
113     paramiko_client = open_connection(IP, cloudadmin['username'], cloudadmin['password'])
114     remote_containers_dir = os.path.join("/home/{}/resources/test_containers".format(cloudadmin['username']))
115     container_images = get_all_files_in_local_dir(CONTAINERS_DIR, "tar")
116     remote_test_charts_dir = os.path.join("/home/{}/resources/test_charts".format(cloudadmin['username']))
117     test_charts = get_all_files_in_local_dir(CHARTS_DIR)
118
119     try:
120         create_remote_dir(paramiko_client, remote_containers_dir)
121         create_remote_dir(paramiko_client, remote_test_charts_dir)
122         upload_resources(paramiko_client, container_images, remote_containers_dir)
123         upload_resources(paramiko_client, test_charts, remote_test_charts_dir)
124         load_docker_images_from_directory(paramiko_client, remote_containers_dir)
125         create_helm_packages(paramiko_client, remote_test_charts_dir)
126         delete_remote_dir(paramiko_client, remote_test_charts_dir)
127         delete_remote_dir(paramiko_client, remote_containers_dir)
128
129     finally:
130         paramiko_client.close()
131
132
133 if __name__ == "__main__":
134     main()