1 # ============LICENSE_START=======================================================
2 # csit-dcaegen2-collectors-hv-ves
3 # ================================================================================
4 # Copyright (C) 2018 NOKIA
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
20 from robot.api import logger
23 from io import BytesIO
24 from os.path import basename
25 from tarfile import TarFile, TarInfo
27 LOCALHOST = "localhost"
30 class VesHvContainersUtilsLibrary:
32 def get_consul_api_access_url(self, method, image_name, port):
33 return self.create_url(
35 self.get_instance_address(image_name, port)
38 def get_xnf_sim_api_access_url(self, method, host):
39 if is_running_inside_docker():
40 return self.create_url(method, host)
42 logger.info("File `/.dockerenv` not found. Assuming local environment and using localhost.")
43 port_from_container_name = str(host)[-4:]
44 return self.create_url(method, LOCALHOST + ":" + port_from_container_name)
46 def get_dcae_app_api_access_url(self, method, image_name, port):
47 return self.create_url(
49 self.get_instance_address(image_name, port)
52 def get_instance_address(self, image_name, port):
53 if is_running_inside_docker():
54 return image_name + ":" + port
56 logger.info("File `/.dockerenv` not found. Assuming local environment and using localhost.")
57 return LOCALHOST + ":" + port
59 def create_url(self, method, host_address):
60 return method + host_address
62 def is_running_inside_docker():
63 return os.path.isfile("/.dockerenv")
65 def copy_to_container(container_id, filepaths, path='/etc/ves-hv'):
66 with create_archive(filepaths) as archive:
67 docker.APIClient('unix:///var/run/docker.sock') \
68 .put_archive(container=container_id, path=(path), data=archive)
71 def create_archive(filepaths):
73 tarfile = TarFile(fileobj=tarstream, mode='w')
74 for filepath in filepaths:
75 file = open(filepath, 'r')
76 file_data = file.read()
78 tarinfo = TarInfo(name=basename(file.name))
79 tarinfo.size = len(file_data)
80 tarinfo.mtime = time()
82 tarfile.addfile(tarinfo, BytesIO(file_data))