Skip to content

Commit 0f3de68

Browse files
authored
Merge pull request #109 from AlmaLinux/fix-stop-env
Fix stopping environments in Opennebula
2 parents 1535a22 + ddea345 commit 0f3de68

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

alts/worker/runners/base.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,25 @@ def write_to_file(file_base_name: str, artifacts_section: dict):
14791479
except UploadError as e:
14801480
raise PublishArtifactsError from e
14811481

1482+
def _stop_env(self):
1483+
if not os.path.exists(self._work_dir):
1484+
return 0, '', f'Working directory {self._work_dir} does not exist'
1485+
self._logger.info(
1486+
'Destroying the environment %s...',
1487+
self.env_name,
1488+
)
1489+
self._logger.debug(
1490+
'Running "terraform destroy --auto-approve" command'
1491+
)
1492+
cmd_args = ['destroy', '--auto-approve', '-no-color']
1493+
if self.TF_VARIABLES_FILE:
1494+
cmd_args.extend(['--var-file', self.TF_VARIABLES_FILE])
1495+
return local['terraform'].with_cwd(self._work_dir).run(
1496+
args=cmd_args,
1497+
retcode=None,
1498+
timeout=CONFIG.provision_timeout,
1499+
)
1500+
14821501
# After: install_package and run_tests
14831502
@command_decorator(
14841503
'stop_environment',
@@ -1487,35 +1506,23 @@ def write_to_file(file_base_name: str, artifacts_section: dict):
14871506
is_abortable=False,
14881507
)
14891508
def stop_env(self):
1490-
if os.path.exists(self._work_dir):
1491-
self._logger.info(
1492-
'Destroying the environment %s...',
1493-
self.env_name,
1494-
)
1495-
self._logger.debug(
1496-
'Running "terraform destroy --auto-approve" command'
1497-
)
1498-
cmd_args = ['destroy', '--auto-approve', '-no-color']
1499-
if self.TF_VARIABLES_FILE:
1500-
cmd_args.extend(['--var-file', self.TF_VARIABLES_FILE])
1501-
return local['terraform'].with_cwd(self._work_dir).run(
1502-
args=cmd_args,
1503-
retcode=None,
1504-
timeout=CONFIG.provision_timeout,
1505-
)
1509+
return self._stop_env()
15061510

15071511
def erase_work_dir(self):
1508-
if self._work_dir and os.path.exists(self._work_dir):
1509-
self._logger.info('Erasing working directory...')
1510-
try:
1511-
shutil.rmtree(self._work_dir)
1512-
except Exception as e:
1513-
self._logger.error(
1514-
'Error while erasing working directory: %s',
1515-
e,
1516-
)
1517-
else:
1518-
self._logger.info('Working directory was successfully removed')
1512+
if not self._work_dir:
1513+
return
1514+
if self._work_dir and not os.path.exists(self._work_dir):
1515+
return
1516+
self._logger.info('Erasing working directory...')
1517+
try:
1518+
shutil.rmtree(self._work_dir)
1519+
except Exception as e:
1520+
self._logger.error(
1521+
'Error while erasing working directory: %s',
1522+
e,
1523+
)
1524+
else:
1525+
self._logger.info('Working directory was successfully removed')
15191526

15201527
def setup(self, skip_provision: bool = False):
15211528
self._stats['started_at'] = datetime.datetime.utcnow().isoformat()

alts/worker/runners/docker.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from alts.shared.exceptions import (
2121
PackageIntegrityTestsError,
2222
ProvisionError,
23-
StopEnvironmentError,
2423
ThirdPartyTestError,
2524
)
2625
from alts.shared.uploaders.base import BaseLogsUploader
@@ -351,20 +350,14 @@ def clone_third_party_repo(
351350
])
352351
return test_repo_path
353352

354-
@command_decorator(
355-
'stop_environment',
356-
'Cannot destroy environment',
357-
exception_class=StopEnvironmentError,
358-
)
359-
def stop_env(self):
353+
def _stop_env(self):
360354
_, container_id, _ = local['terraform'].with_cwd(
361355
self._work_dir).run(
362356
args=('output', '-raw', '-no-color', 'container_id'),
363357
retcode=None,
364358
timeout=CONFIG.provision_timeout,
365359
)
366-
try:
367-
return super().stop_env()
368-
except StopEnvironmentError:
369-
# Attempt to delete environment via plain docker command
360+
exit_code, out, err = super()._stop_env()
361+
if exit_code != 0:
370362
return self.exec_command('rm', '-f', container_id)
363+
return exit_code, out, err

alts/worker/runners/opennebula.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
from alts.shared.exceptions import (
2222
OpennebulaVMStopError,
2323
VMImageNotFound,
24-
StopEnvironmentError,
2524
)
2625
from alts.shared.uploaders.base import BaseLogsUploader
2726
from alts.worker import CONFIG
28-
from alts.worker.runners.base import GenericVMRunner, command_decorator
27+
from alts.worker.runners.base import GenericVMRunner
2928

3029
__all__ = ['OpennebulaRunner']
3130

@@ -242,24 +241,24 @@ def recover_delete():
242241
)
243242
recover_delete()
244243

245-
@command_decorator(
246-
'stop_environment',
247-
'Cannot destroy environment',
248-
exception_class=StopEnvironmentError,
249-
is_abortable=False,
250-
)
251-
def stop_env(self):
244+
def _stop_env(self):
245+
stop_exit_code, stop_out, stop_err = super()._stop_env()
246+
if stop_exit_code == 0:
247+
return stop_exit_code, stop_out, stop_err
248+
249+
self._logger.warning(
250+
'Cannot stop VM conventionally. Output:\n%s\nStderr:\n%s',
251+
stop_out, stop_err
252+
)
252253
id_exit_code, vm_id, id_stderr = local['terraform'].with_cwd(
253254
self._work_dir).run(
254255
args=('output', '-raw', '-no-color', 'vm_id'),
255256
retcode=None,
256257
timeout=CONFIG.provision_timeout,
257258
)
258-
if id_exit_code != 0:
259+
self._logger.debug('VM ID: %s', vm_id)
260+
if id_exit_code != 0 or not vm_id:
259261
self._logger.warning('Cannot get VM ID: %s', id_stderr)
260-
try:
261-
return super().stop_env()
262-
except StopEnvironmentError:
263-
if vm_id:
264-
self.destroy_vm_via_api(int(vm_id.strip()))
265-
return 0, f'{vm_id} is destroyed via API', ''
262+
return id_exit_code, 'Cannot get VM ID', id_stderr
263+
self.destroy_vm_via_api(int(vm_id.strip()))
264+
return 0, f'{vm_id} is destroyed via API', ''

0 commit comments

Comments
 (0)