atk-common 1.6.0__tar.gz → 1.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {atk_common-1.6.0 → atk_common-1.8.0}/PKG-INFO +1 -1
- {atk_common-1.6.0 → atk_common-1.8.0}/setup.py +1 -1
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/__init__.py +1 -2
- atk_common-1.8.0/src/atk_common/docker_utils.py +57 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/error_utils.py +22 -9
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/http_utils.py +8 -9
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common.egg-info/PKG-INFO +1 -1
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common.egg-info/SOURCES.txt +1 -0
- atk_common-1.8.0/tests/test_docker_utils.py +12 -0
- atk_common-1.8.0/tests/test_error_utils.py +21 -0
- atk_common-1.8.0/tests/test_http_utils.py +12 -0
- atk_common-1.6.0/src/atk_common/docker_utils.py +0 -65
- atk_common-1.6.0/tests/test_docker_utils.py +0 -20
- atk_common-1.6.0/tests/test_http_utils.py +0 -20
- {atk_common-1.6.0 → atk_common-1.8.0}/README.md +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/license.txt +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/pyproject.toml +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/setup.cfg +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/datetime_utils.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/enums/__init__.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/enums/command_status_enum.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/enums/speed_control_status_enum.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/env_utils.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/log_utils.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/rabbitmq_consumer.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common/response_utils.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common.egg-info/dependency_links.txt +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common.egg-info/requires.txt +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/atk_common.egg-info/top_level.txt +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/src/shared_python_atk_enforcement/__init__.py +0 -0
- {atk_common-1.6.0 → atk_common-1.8.0}/tests/test_log_utils.py +0 -0
@@ -36,7 +36,7 @@ setup(
|
|
36
36
|
# For a discussion on single-sourcing the version across setup.py and the
|
37
37
|
# project code, see
|
38
38
|
# https://packaging.python.org/guides/single-sourcing-package-version/
|
39
|
-
version="1.
|
39
|
+
version="1.8.0", # Required
|
40
40
|
# This is a one-line description or tagline of what your project does. This
|
41
41
|
# corresponds to the "Summary" metadata field:
|
42
42
|
# https://packaging.python.org/specifications/core-metadata/#summary
|
@@ -6,7 +6,7 @@ from atk_common.http_utils import is_status_code_ok, get_test_response
|
|
6
6
|
from atk_common.log_utils import add_log_item, add_log_item_http
|
7
7
|
from atk_common.rabbitmq_consumer import RabbitMQConsumer
|
8
8
|
from atk_common.response_utils import create_save_resp
|
9
|
-
from atk_common.docker_utils import
|
9
|
+
from atk_common.docker_utils import get_current_container_info
|
10
10
|
|
11
11
|
__all__ = [
|
12
12
|
'date_time_utc',
|
@@ -23,6 +23,5 @@ __all__ = [
|
|
23
23
|
'add_log_item_http',
|
24
24
|
'RabbitMQConsumer',
|
25
25
|
'create_save_resp',
|
26
|
-
'get_image_version',
|
27
26
|
'get_current_container_info',
|
28
27
|
]
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import docker
|
2
|
+
import socket
|
3
|
+
from atk_common.log_utils import add_log_item
|
4
|
+
|
5
|
+
def get_image_name_and_version(tags):
|
6
|
+
if tags:
|
7
|
+
# Use the first tag (usually only one)
|
8
|
+
full_tag = tags[0] # e.g., 'bo-crypto-wrapper-api:latest'
|
9
|
+
|
10
|
+
if ":" in full_tag:
|
11
|
+
image_name, image_version = full_tag.split(":", 1)
|
12
|
+
else:
|
13
|
+
image_name = full_tag
|
14
|
+
image_version = "<none>"
|
15
|
+
return image_name, image_version
|
16
|
+
else:
|
17
|
+
return None, None
|
18
|
+
|
19
|
+
def create_port_item(port, binding):
|
20
|
+
data = {}
|
21
|
+
data['port'] = port
|
22
|
+
data['binding'] = binding
|
23
|
+
return data
|
24
|
+
|
25
|
+
def create_container_log(container_data):
|
26
|
+
log_str = 'Container name: ' + container_data['containerName'] + ', image name: ' + container_data['imageName'] + ', image version: ' + container_data['imageVersion']
|
27
|
+
add_log_item(log_str)
|
28
|
+
|
29
|
+
def get_current_container_info():
|
30
|
+
try:
|
31
|
+
data = {}
|
32
|
+
client = docker.from_env()
|
33
|
+
|
34
|
+
# Get current container's hostname (usually the container ID)
|
35
|
+
container_id = socket.gethostname()
|
36
|
+
|
37
|
+
# Fetch container object using partial ID
|
38
|
+
container = client.containers.get(container_id)
|
39
|
+
|
40
|
+
tags_info = get_image_name_and_version(container.image.tags)
|
41
|
+
data['imageName'] = tags_info[0]
|
42
|
+
data['imageVersion'] = tags_info[1]
|
43
|
+
data['containerName'] = container.name
|
44
|
+
ports = container.attrs['NetworkSettings']['Ports']
|
45
|
+
data['ports'] = []
|
46
|
+
if ports:
|
47
|
+
for container_port, host_bindings in ports.items():
|
48
|
+
if host_bindings:
|
49
|
+
for binding in host_bindings:
|
50
|
+
data['ports'].append(create_port_item(container_port, f"{binding['HostIp']}:{binding['HostPort']}"))
|
51
|
+
else:
|
52
|
+
data['ports'].append(create_port_item(container_port, None))
|
53
|
+
create_container_log(data)
|
54
|
+
return data
|
55
|
+
except Exception as e:
|
56
|
+
add_log_item("Error getting container data:" + str(e))
|
57
|
+
return None
|
@@ -1,7 +1,6 @@
|
|
1
1
|
from datetime import datetime
|
2
2
|
import json
|
3
3
|
from atk_common.datetime_utils import get_utc_date_time
|
4
|
-
from atk_common.docker_utils import get_image_version
|
5
4
|
from atk_common.log_utils import add_log_item
|
6
5
|
from atk_common.response_utils import create_save_resp
|
7
6
|
|
@@ -10,22 +9,36 @@ def get_message(error):
|
|
10
9
|
return str(error.message)
|
11
10
|
else:
|
12
11
|
return str(error)
|
12
|
+
|
13
|
+
def create_error_log(data):
|
14
|
+
err_str = data['message'] + ', statusCode: ' + str(data['statusCode']) + ', method: ' + data['method']
|
15
|
+
if data['containerInfo'] is not None:
|
16
|
+
err_str += ', imageName: ' + data['containerInfo']['imageName']
|
17
|
+
err_str += ', imageVersion: ' + data['containerInfo']['imageVersion']
|
18
|
+
err_str += ', containerName: ' + data['containerInfo']['containerName']
|
19
|
+
else:
|
20
|
+
err_str += ', imageName: <none>'
|
21
|
+
err_str += ', imageVersion: <none>'
|
22
|
+
err_str += ', containerName: <none>'
|
23
|
+
add_log_item(err_str)
|
13
24
|
|
14
|
-
def get_error_entity(app, error,
|
25
|
+
def get_error_entity(app, error, method, error_type, status_code, container_info):
|
15
26
|
data = {}
|
16
27
|
data['statusCode'] = status_code
|
17
28
|
data['exceptionType'] = str(type(error))
|
18
29
|
data['errorType'] = error_type
|
19
30
|
data['message'] = get_message(error)
|
20
|
-
data['component'] = component
|
21
31
|
data['method'] = method
|
22
|
-
data['imageVersion'] = get_image_version(component)
|
23
32
|
data['timestamp'] = get_utc_date_time()
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
33
|
+
data['containerInfo'] = container_info
|
34
|
+
create_error_log(data)
|
35
|
+
if app is not None:
|
36
|
+
return app.response_class(
|
37
|
+
response=json.dumps(data),
|
38
|
+
status=500,
|
39
|
+
mimetype='application/json'
|
40
|
+
)
|
41
|
+
return None
|
29
42
|
|
30
43
|
def handle_error(resp, status):
|
31
44
|
if resp.status_code == 500:
|
@@ -1,24 +1,23 @@
|
|
1
1
|
import json
|
2
2
|
from atk_common.datetime_utils import get_utc_date_time
|
3
|
-
from atk_common.docker_utils import get_current_container_info
|
3
|
+
from atk_common.docker_utils import get_current_container_info
|
4
4
|
from atk_common.env_utils import get_env_value
|
5
5
|
|
6
6
|
def is_status_code_ok(status_code):
|
7
7
|
return status_code >= 200 and status_code < 300
|
8
8
|
|
9
|
-
def get_test_response(
|
9
|
+
def get_test_response():
|
10
10
|
data = {}
|
11
11
|
container_info = get_current_container_info()
|
12
12
|
if container_info is not None:
|
13
|
-
data['
|
14
|
-
data['
|
13
|
+
data['imageName'] = container_info['imageName']
|
14
|
+
data['imageVersion'] = container_info['imageVersion']
|
15
|
+
data['containerName'] = container_info['containerName']
|
16
|
+
data['containerPorts'] = container_info['ports']
|
15
17
|
else:
|
18
|
+
data['imageName'] = None
|
19
|
+
data['imageVersion'] = None
|
16
20
|
data['containerName'] = None
|
17
21
|
data['containerPorts'] = None
|
18
|
-
data['imageName'] = get_env_value('DOCKER_IMAGE_NAME', default_value)
|
19
|
-
if data['imageName'] is None:
|
20
|
-
data['imageVersion'] = None
|
21
|
-
else:
|
22
|
-
data['imageVersion'] = get_image_version(data['imageName'])
|
23
22
|
data['timestamp'] = get_utc_date_time()
|
24
23
|
return data
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import unittest
|
2
|
+
import sys, os
|
3
|
+
from atk_common.docker_utils import get_current_container_info
|
4
|
+
|
5
|
+
class TestDockerUtils(unittest.TestCase):
|
6
|
+
|
7
|
+
def test_get_container_info(self):
|
8
|
+
info = get_current_container_info()
|
9
|
+
self.assertIsNone(info)
|
10
|
+
|
11
|
+
if __name__ == "__main__":
|
12
|
+
unittest.main()
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import unittest
|
2
|
+
import sys, os
|
3
|
+
from atk_common.error_utils import get_error_entity
|
4
|
+
|
5
|
+
class TestErrorUtils(unittest.TestCase):
|
6
|
+
|
7
|
+
def create_dummy_container_info(self):
|
8
|
+
data = {}
|
9
|
+
data['imageName'] = 'bo-test-api'
|
10
|
+
data['imageVersion'] = '1.0.0'
|
11
|
+
data['containerName'] = 'bo-test-api'
|
12
|
+
data['ports'] = []
|
13
|
+
data['ports'].append({'port': 8080, 'binding': 8080})
|
14
|
+
return data
|
15
|
+
|
16
|
+
def test_get_error_entity(self):
|
17
|
+
resp_json = get_error_entity(None, 'An new error occured', 'get-configuration', 1, 404, self.create_dummy_container_info())
|
18
|
+
self.assertIsNone(resp_json)
|
19
|
+
|
20
|
+
if __name__ == "__main__":
|
21
|
+
unittest.main()
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import unittest
|
2
|
+
import sys, os
|
3
|
+
from atk_common.http_utils import get_test_response
|
4
|
+
|
5
|
+
class TestHttpUtils(unittest.TestCase):
|
6
|
+
|
7
|
+
def test_get_test_response_none(self):
|
8
|
+
resp_json = get_test_response()
|
9
|
+
self.assertIsNotNone(resp_json)
|
10
|
+
|
11
|
+
if __name__ == "__main__":
|
12
|
+
unittest.main()
|
@@ -1,65 +0,0 @@
|
|
1
|
-
import docker
|
2
|
-
import os
|
3
|
-
import socket
|
4
|
-
from atk_common.env_utils import get_env_value
|
5
|
-
|
6
|
-
def find_image_prop_list_by_name(image_list, image_name):
|
7
|
-
for image in image_list:
|
8
|
-
if image.tags and len(image.tags) > 0:
|
9
|
-
# Check if the image name is in the first tag
|
10
|
-
if image_name in image.tags[0]:
|
11
|
-
return image.tags[0]
|
12
|
-
return None
|
13
|
-
|
14
|
-
def get_image_version(default_value=None):
|
15
|
-
"""
|
16
|
-
Get the version of a Docker image.
|
17
|
-
|
18
|
-
:param image_name: Name of the Docker image
|
19
|
-
:return: Version of the Docker image
|
20
|
-
"""
|
21
|
-
try:
|
22
|
-
image_name = get_env_value('DOCKER_IMAGE_NAME', default_value)
|
23
|
-
if image_name is None:
|
24
|
-
return None
|
25
|
-
client = docker.from_env()
|
26
|
-
image_list = client.images.list()
|
27
|
-
image_prop_list = find_image_prop_list_by_name(image_list, image_name)
|
28
|
-
if image_prop_list is None:
|
29
|
-
return None
|
30
|
-
image_props = image_prop_list.split(':')
|
31
|
-
if len(image_props) < 2:
|
32
|
-
return None
|
33
|
-
return image_props[1]
|
34
|
-
except docker.errors.ImageNotFound:
|
35
|
-
return None
|
36
|
-
|
37
|
-
def get_current_container_info():
|
38
|
-
try:
|
39
|
-
client = docker.from_env()
|
40
|
-
|
41
|
-
# Get current container's hostname (usually the container ID)
|
42
|
-
container_id = socket.gethostname()
|
43
|
-
|
44
|
-
# Fetch container object using partial ID
|
45
|
-
container = client.containers.get(container_id)
|
46
|
-
|
47
|
-
name = container.name
|
48
|
-
ports = container.attrs['NetworkSettings']['Ports']
|
49
|
-
|
50
|
-
print(f"🔍 Container name: {name}")
|
51
|
-
print("🌐 Port mappings:")
|
52
|
-
if ports:
|
53
|
-
for container_port, host_bindings in ports.items():
|
54
|
-
if host_bindings:
|
55
|
-
for binding in host_bindings:
|
56
|
-
print(f" {container_port} -> {binding['HostIp']}:{binding['HostPort']}")
|
57
|
-
else:
|
58
|
-
print(f" {container_port} -> Not bound to host")
|
59
|
-
else:
|
60
|
-
print("No exposed ports found.")
|
61
|
-
return None
|
62
|
-
|
63
|
-
except Exception as e:
|
64
|
-
print("❌ Error getting container info:", e)
|
65
|
-
return None
|
@@ -1,20 +0,0 @@
|
|
1
|
-
import unittest
|
2
|
-
import sys, os
|
3
|
-
from atk_common.docker_utils import get_image_version, list_all_images
|
4
|
-
|
5
|
-
class TestDockerUtils(unittest.TestCase):
|
6
|
-
|
7
|
-
def test_get_image_version_none(self):
|
8
|
-
ver = get_image_version()
|
9
|
-
self.assertIsNone(ver)
|
10
|
-
|
11
|
-
def test_get_image_version_not_none_1(self):
|
12
|
-
ver = get_image_version('bo-config-db-api')
|
13
|
-
self.assertIsNotNone(ver)
|
14
|
-
|
15
|
-
def test_get_image_version_not_none_2(self):
|
16
|
-
ver = get_image_version('bo-case-web')
|
17
|
-
self.assertIsNotNone(ver)
|
18
|
-
|
19
|
-
if __name__ == "__main__":
|
20
|
-
unittest.main()
|
@@ -1,20 +0,0 @@
|
|
1
|
-
import unittest
|
2
|
-
import sys, os
|
3
|
-
from atk_common.http_utils import get_test_response
|
4
|
-
|
5
|
-
class TestHttpUtils(unittest.TestCase):
|
6
|
-
|
7
|
-
def test_get_test_response_none(self):
|
8
|
-
resp_json = get_test_response()
|
9
|
-
self.assertIsNotNone(resp_json)
|
10
|
-
|
11
|
-
def test_get_image_version_not_none_1(self):
|
12
|
-
resp_json = get_test_response('bo-config-db-api')
|
13
|
-
self.assertIsNotNone(resp_json)
|
14
|
-
|
15
|
-
def test_get_image_version_not_none_2(self):
|
16
|
-
resp_json = get_test_response('bo-case-web')
|
17
|
-
self.assertIsNotNone(resp_json)
|
18
|
-
|
19
|
-
if __name__ == "__main__":
|
20
|
-
unittest.main()
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|