Skip to content

Commit 948aea9

Browse files
authored
Merge branch 'main' into aap-20044
2 parents 89c1d20 + 4f69f6a commit 948aea9

File tree

4 files changed

+11
-68
lines changed

4 files changed

+11
-68
lines changed

src/aap_eda/services/activation/engine/podman.py

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import base64
16-
import json
1715
import logging
1816
import os
1917

@@ -31,7 +29,6 @@
3129
ContainerEngine,
3230
ContainerRequest,
3331
ContainerStatus,
34-
Credential,
3532
LogHandler,
3633
)
3734

@@ -73,7 +70,6 @@ def __init__(
7370
self.client = get_podman_client()
7471
LOGGER.debug(self.client.version())
7572

76-
self.auth_file = None
7773
except APIError as e:
7874
LOGGER.error(f"Failed to initialize podman engine: f{e}")
7975
raise exceptions.ContainerEngineInitError(str(e))
@@ -100,6 +96,11 @@ def cleanup(self, container_id: str, log_handler: LogHandler) -> None:
10096
# ContainerCleanupError handled by the manager
10197
except APIError as e:
10298
raise exceptions.ContainerCleanupError(str(e)) from e
99+
finally:
100+
# Ensure volumes are purged due to a bug in podman
101+
# ref: https://github.com/containers/podman-py/issues/328
102+
pruned_volumes = self.client.volumes.prune()
103+
LOGGER.info(f"Pruned volumes: {pruned_volumes}")
103104

104105
def _image_exists(self, image_url: str) -> bool:
105106
try:
@@ -113,7 +114,6 @@ def start(self, request: ContainerRequest, log_handler: LogHandler) -> str:
113114
raise exceptions.ContainerStartError("Missing image url")
114115

115116
try:
116-
self._set_auth_json_file()
117117
self._login(request)
118118
LOGGER.info(f"Image URL is {request.image_url}")
119119
if request.pull_policy == "Always" or not self._image_exists(
@@ -326,44 +326,6 @@ def _login(self, request: ContainerRequest) -> None:
326326
LOGGER.exception("Login failed: f{e}")
327327
raise exceptions.ContainerStartError(str(e))
328328

329-
def _write_auth_json(self, request: ContainerRequest) -> None:
330-
if not self.auth_file:
331-
LOGGER.debug("No auth file to create")
332-
return
333-
334-
auth_dict = {}
335-
if os.path.exists(self.auth_file):
336-
with open(self.auth_file, encoding="utf-8") as f:
337-
auth_dict = json.load(f)
338-
339-
if "auths" not in auth_dict:
340-
auth_dict["auths"] = {}
341-
registry = request.image_url.split("/")[0]
342-
auth_dict["auths"][registry] = self._create_auth_key(
343-
request.credential
344-
)
345-
346-
with open(self.auth_file, mode="w", encoding="utf-8") as f:
347-
json.dump(auth_dict, f, indent=6)
348-
349-
def _create_auth_key(self, credential: Credential) -> dict:
350-
data = f"{credential.username}:{credential.secret}"
351-
encoded_data = data.encode("ascii")
352-
return {"auth": base64.b64encode(encoded_data).decode("ascii")}
353-
354-
def _set_auth_json_file(self) -> None:
355-
xdg_runtime_dir = os.getenv(
356-
"XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}"
357-
)
358-
auth_file = f"{xdg_runtime_dir}/containers/auth.json"
359-
dir_name = os.path.dirname(auth_file)
360-
if os.path.exists(dir_name):
361-
self.auth_file = auth_file
362-
LOGGER.debug("Will use auth file %s", auth_file)
363-
else:
364-
self.auth_file = None
365-
LOGGER.debug("Will not use auth file")
366-
367329
def _pull_image(
368330
self, request: ContainerRequest, log_handler: LogHandler
369331
) -> Image:
@@ -376,7 +338,6 @@ def _pull_image(
376338
"username": request.credential.username,
377339
"password": request.credential.secret,
378340
}
379-
self._write_auth_json(request)
380341
image = self.client.images.pull(request.image_url, **kwargs)
381342

382343
# https://github.com/containers/podman-py/issues/301

src/aap_eda/services/activation/manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,13 @@ def _get_container_request(self) -> ContainerRequest:
10751075
@staticmethod
10761076
def check_new_process_allowed(parent_type: str, parent_id: int) -> bool:
10771077
"""Check if a new process is allowed."""
1078+
if settings.MAX_RUNNING_ACTIVATIONS < 0:
1079+
return True
1080+
10781081
num_running_processes = models.RulebookProcess.objects.filter(
10791082
status__in=[ActivationStatus.RUNNING, ActivationStatus.STARTING],
10801083
).count()
1084+
10811085
if num_running_processes >= settings.MAX_RUNNING_ACTIVATIONS:
10821086
LOGGER.info(
10831087
"No capacity to start a new rulebook process. "

src/aap_eda/settings/default.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ def _get_secret_key() -> str:
447447
ACTIVATION_MAX_RESTARTS_ON_FAILURE = int(
448448
settings.get("ACTIVATION_MAX_RESTARTS_ON_FAILURE", 5)
449449
)
450+
451+
# -1 means no limit
450452
MAX_RUNNING_ACTIVATIONS = int(settings.get("MAX_RUNNING_ACTIVATIONS", 5))
451453

452454
# ---------------------------------------------------------

tests/integration/services/activation/engine/test_podman.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -587,27 +587,3 @@ def raise_error(*args, **kwargs):
587587

588588
with pytest.raises(ContainerUpdateLogsError, match="Not found"):
589589
engine.update_logs("100", log_handler)
590-
591-
592-
@pytest.mark.django_db
593-
def test_set_auth_json(podman_engine):
594-
engine = podman_engine
595-
596-
with mock.patch("os.path.dirname"):
597-
engine._set_auth_json_file()
598-
599-
xdg_runtime_dir = os.getenv(
600-
"XDG_RUNTIME_DIR", f"/run/user/{os.getuid()}"
601-
)
602-
603-
assert engine.auth_file == f"{xdg_runtime_dir}/containers/auth.json"
604-
605-
606-
@pytest.mark.django_db
607-
def test_write_auth_json(init_data, podman_engine):
608-
engine = podman_engine
609-
engine.auth_file = f"{DATA_DIR}/auth.json"
610-
request = get_request_with_credential(init_data)
611-
612-
engine._write_auth_json(request)
613-
assert engine.auth_file is not None

0 commit comments

Comments
 (0)