7 WORK_DIR = os.getenv('WORKDIR')
8 sys.path.append(os.path.join(WORK_DIR, 'libraries', 'common'))
9 from users import * # noqa
11 REG = os.getenv('REG')
12 REG_PORT = os.getenv('REG_PORT')
13 REG_PATH = os.getenv('REG_PATH')
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')
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)
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))
35 def delete_remote_dir(client, remote_dir):
36 execute_command(client, "rm -rf {}".format(remote_dir))
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)
46 raise Exception("The following error occured: {}".format(err))
51 def get_all_files_in_local_dir(local_dir, extension=""):
53 if os.path.exists(local_dir):
54 files = os.listdir(local_dir)
56 _, ext = os.path.splitext(f)
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))
63 all_files.append(filepath)
65 print '{} folder does not exist'.format(local_dir)
69 def upload_resources(client, local, remote):
70 sftp = client.open_sftp()
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)
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]
87 command = "docker push {}:{}/{}/{}".format(REG, REG_PORT, REG_PATH, image_name)
88 execute_command(client, command)
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()
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)
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)
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)
130 paramiko_client.close()
133 if __name__ == "__main__":