atk-common 1.3.0__py3-none-any.whl → 1.5.0__py3-none-any.whl

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/__init__.py CHANGED
@@ -2,10 +2,11 @@
2
2
  from atk_common.datetime_utils import date_time_utc, get_utc_date_time
3
3
  from atk_common.env_utils import get_env_value
4
4
  from atk_common.error_utils import get_message, get_error_entity, handle_error, get_response_error, get_error_type
5
- from atk_common.http_utils import is_status_code_ok
5
+ 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 get_image_version, get_current_container_info
9
10
 
10
11
  __all__ = [
11
12
  'date_time_utc',
@@ -17,8 +18,11 @@ __all__ = [
17
18
  'get_response_error',
18
19
  'get_error_type',
19
20
  'is_status_code_ok',
21
+ 'get_test_response',
20
22
  'add_log_item',
21
23
  'add_log_item_http',
22
24
  'RabbitMQConsumer',
23
- 'create_save_resp'
25
+ 'create_save_resp',
26
+ 'get_image_version',
27
+ 'get_current_container_info'
24
28
  ]
@@ -0,0 +1,65 @@
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
atk_common/env_utils.py CHANGED
@@ -3,6 +3,8 @@ from atk_common.log_utils import add_log_item
3
3
 
4
4
  def get_env_value(key, default_value):
5
5
  try:
6
+ if default_value is None:
7
+ return None
6
8
  val = os.environ[key]
7
9
  add_log_item(key + ':' + val)
8
10
  return val
atk_common/error_utils.py CHANGED
@@ -1,6 +1,7 @@
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
4
5
  from atk_common.log_utils import add_log_item
5
6
  from atk_common.response_utils import create_save_resp
6
7
 
@@ -18,6 +19,7 @@ def get_error_entity(app, error, component, method, error_type, status_code):
18
19
  data['message'] = get_message(error)
19
20
  data['component'] = component
20
21
  data['method'] = method
22
+ data['imageVersion'] = get_image_version(component)
21
23
  data['timestamp'] = get_utc_date_time()
22
24
  return app.response_class(
23
25
  response=json.dumps(data),
@@ -41,7 +43,7 @@ def get_response_error(resp):
41
43
 
42
44
  # Return values:
43
45
  # 1 - Connection error
44
- # 2 - Database error
46
+ # 2 - Internal error
45
47
  def get_error_type(conn):
46
48
  if conn is None:
47
49
  return 1
atk_common/http_utils.py CHANGED
@@ -1,2 +1,24 @@
1
+ import json
2
+ from atk_common.datetime_utils import get_utc_date_time
3
+ from atk_common.docker_utils import get_current_container_info, get_image_version
4
+ from atk_common.env_utils import get_env_value
5
+
1
6
  def is_status_code_ok(status_code):
2
7
  return status_code >= 200 and status_code < 300
8
+
9
+ def get_test_response(default_value=None):
10
+ data = {}
11
+ container_info = get_current_container_info()
12
+ if container_info is not None:
13
+ data['containerName'] = container_info.name
14
+ data['containerPorts'] = container_info.ports
15
+ else:
16
+ data['containerName'] = None
17
+ 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
+ data['timestamp'] = get_utc_date_time()
24
+ return data
@@ -3,12 +3,11 @@ import socket
3
3
  import time
4
4
 
5
5
  class RabbitMQConsumer:
6
- def __init__(self, queue_name, user, pwd, host, vhost, dlx, dlq, encoding, message_handler, log):
6
+ def __init__(self, queue_name, user, pwd, host, vhost, dlx, dlq, content_type, message_handler, log):
7
7
 
8
8
  rabbit_url = 'amqp://' + user + ':' + pwd + '@' + host + '/' + vhost
9
9
 
10
10
  self.connection = Connection(rabbit_url, heartbeat=10)
11
- queue = None
12
11
  if dlx is not None and dlq is not None:
13
12
  queue = Queue(name=queue_name,
14
13
  queue_arguments={
@@ -17,7 +16,10 @@ class RabbitMQConsumer:
17
16
  )
18
17
  else:
19
18
  queue = Queue(name=queue_name)
20
- self.consumer = Consumer(self.connection, queues=queue, callbacks=[message_handler], accept=[encoding])
19
+ if content_type is not None:
20
+ self.consumer = Consumer(self.connection, queues=queue, callbacks=[message_handler], accept=[content_type])
21
+ else:
22
+ self.consumer = Consumer(self.connection, queues=queue, callbacks=[message_handler], accept=None)
21
23
  self.consumer.consume()
22
24
 
23
25
  self.message_handler = message_handler # Custom message handler
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: atk_common
3
- Version: 1.3.0
3
+ Version: 1.5.0
4
4
  Summary: ATK common methods
5
5
  Home-page: https://github.com/pypa/atk_common
6
6
  Author: Roger
@@ -1,10 +1,11 @@
1
- atk_common/__init__.py,sha256=EceDa352WFHTdC_LxYANXf_TS9pGzK3MCx87bwAKTvg,811
1
+ atk_common/__init__.py,sha256=u4ue9lkuw1ImDdWClEB1z6k_8eHjfm-1wvJipZV5JnM,1000
2
2
  atk_common/datetime_utils.py,sha256=qsVF7l90P1-xukG2tV_jLqG9J_Yfl5wTpyfrdPBlyMo,239
3
- atk_common/env_utils.py,sha256=bXOrxM3fZUslqfmZt75iphbEJHbG4riJa8XOVzPwIII,313
4
- atk_common/error_utils.py,sha256=cBDe5zOzOOuzRCs5WHo55knx74-ZUnDvaqrQtXBuZKw,1421
5
- atk_common/http_utils.py,sha256=5PjGYy-TiBbtNOElEDhsCs3BPDTfqg_aMQBNCuHfcrQ,90
3
+ atk_common/docker_utils.py,sha256=BuulyBYuUoNS-iV4ZsknRifx5L2KWcKrrHDRqg6zqaY,2194
4
+ atk_common/env_utils.py,sha256=66v-S0vlkJNaun6ay1XcOGRtYJb4Ce4odJsWxuaHsQc,373
5
+ atk_common/error_utils.py,sha256=5OiL5nMBEkDc193m2cVMD2Bs-fwRvPeSlI7PwnqB0o4,1533
6
+ atk_common/http_utils.py,sha256=t2XmuDMBjUAH071qwarZ6hcTBzpO_4a9x10E8MU2EMc,924
6
7
  atk_common/log_utils.py,sha256=DGGwXjD5jvpMZJNW1QBoJRdDaTY1e1Z69j0LSOOzsWA,419
7
- atk_common/rabbitmq_consumer.py,sha256=wmZQieeONkfXB5RmM6yiNkwl0_QK4oWWDBSfiD47CdA,1753
8
+ atk_common/rabbitmq_consumer.py,sha256=4MhuwZs47Jt1fX4sUxr1MKRe7o2QRbPe9_utXEsa8QE,1907
8
9
  atk_common/response_utils.py,sha256=ezqTSNdGwueFXzijKB7CKPWxZml39bqjAZOeIMWMntk,197
9
10
  atk_common/enums/__init__.py,sha256=oW0aVnwewfH6sNu14jjZ5_uP4iRN4pRmGdTNC204rHo,236
10
11
  atk_common/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
@@ -21,8 +22,8 @@ atk_package/enums/__init__.py,sha256=oW0aVnwewfH6sNu14jjZ5_uP4iRN4pRmGdTNC204rHo
21
22
  atk_package/enums/command_status_enum.py,sha256=M2Nln27a_DbzI07-gfytWQk2X087JhkU6Fmard5qVHs,127
22
23
  atk_package/enums/speed_control_status_enum.py,sha256=qpURh0K1L1tSpbrzVnckoe4hUn1illIkbo7k4mLfzIM,182
23
24
  shared_python_atk_enforcement/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
- atk_common-1.3.0.dist-info/METADATA,sha256=XAqiKPtEPWZ2NmEwxPp0BWwxo2zzhHeDzNscV3VdoCE,1193
25
- atk_common-1.3.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
26
- atk_common-1.3.0.dist-info/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
27
- atk_common-1.3.0.dist-info/top_level.txt,sha256=4CwRjkLnheIdI4jQwc4tK3dbRc58WqUmoqjkdDTWlME,41
28
- atk_common-1.3.0.dist-info/RECORD,,
25
+ atk_common-1.5.0.dist-info/METADATA,sha256=Qa-31_dqBulVz-F3gzWuvkalmUrfWjHo1PGsQhEFCRw,1193
26
+ atk_common-1.5.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
27
+ atk_common-1.5.0.dist-info/license.txt,sha256=_0O6fWM00-wTurDjnZhUP_N5QiwGhItaQZqHq5eqadA,1063
28
+ atk_common-1.5.0.dist-info/top_level.txt,sha256=4CwRjkLnheIdI4jQwc4tK3dbRc58WqUmoqjkdDTWlME,41
29
+ atk_common-1.5.0.dist-info/RECORD,,