blob: 3e7d48bdf9c1a9205283d79d498ca52cec796760 [file] [log] [blame]
Filip Krzywka66e2ad62019-03-20 14:03:02 +01001# ============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
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
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=========================================================
Gary Wu9abb61c2018-09-27 10:38:50 -070018from time import time
19
20from robot.api import logger
21import os.path
22import docker
23from io import BytesIO
24from os.path import basename
25from tarfile import TarFile, TarInfo
26
27LOCALHOST = "localhost"
28
29
30class VesHvContainersUtilsLibrary:
31
32 def get_consul_api_access_url(self, method, image_name, port):
33 return self.create_url(
34 method,
35 self.get_instance_address(image_name, port)
36 )
37
38 def get_xnf_sim_api_access_url(self, method, host):
39 if is_running_inside_docker():
40 return self.create_url(method, host)
41 else:
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)
45
46 def get_dcae_app_api_access_url(self, method, image_name, port):
47 return self.create_url(
48 method,
49 self.get_instance_address(image_name, port)
50 )
51
52 def get_instance_address(self, image_name, port):
53 if is_running_inside_docker():
54 return image_name + ":" + port
55 else:
56 logger.info("File `/.dockerenv` not found. Assuming local environment and using localhost.")
57 return LOCALHOST + ":" + port
58
59 def create_url(self, method, host_address):
60 return method + host_address
61
62def is_running_inside_docker():
63 return os.path.isfile("/.dockerenv")
64
65def 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)
69
70
71def create_archive(filepaths):
72 tarstream = BytesIO()
73 tarfile = TarFile(fileobj=tarstream, mode='w')
74 for filepath in filepaths:
75 file = open(filepath, 'r')
76 file_data = file.read()
77
78 tarinfo = TarInfo(name=basename(file.name))
79 tarinfo.size = len(file_data)
80 tarinfo.mtime = time()
81
82 tarfile.addfile(tarinfo, BytesIO(file_data))
83
84 tarfile.close()
85 tarstream.seek(0)
86 return tarstream