def clean_resources(ctx, post_behavior, user, test_id, logdir, dry_run, backend): """Clean cloud resources. There are different options how to run clean up: - To clean resources for the latest run according to post behavior $ hydra clean-resources --post-behavior - The same as above but with altered logdir $ hydra clean-resources --post-behavior --logdir /path/to/logdir - To clean resources for some Test ID according to post behavior (test run status extracted from logdir) $ hydra clean-resources --post-behavior --test-id TESTID - The same as above but with altered logdir $ hydra clean-resources --post-behavior --test-id TESTID --logdir /path/to/logdir - To clean resources for the latest run ignoring post behavior $ hydra clean-resources - The same as above but with altered logdir $ hydra clean-resources --logdir /path/to/logdir - To clean all resources belong to some Test ID: $ hydra clean-resources --test-id TESTID - To clean all resources belong to some user: $ hydra clean-resources --user vasya.pupkin Also you can add --dry-run option to see what should be cleaned. """ add_file_logger() user_param = {"RunByUser": user} if user else {} if not post_behavior and user and not test_id and not logdir: click.echo(f"Clean all resources belong to user `{user}'") params = (user_param, ) else: if not logdir and (post_behavior or not test_id): logdir = Setup.base_logdir() if not test_id and (latest_test_id := search_test_id_in_latest(logdir)): click.echo(f"Latest TestId in {logdir} is {latest_test_id}") test_id = (latest_test_id, ) if not test_id: click.echo(clean_resources.get_help(ctx)) return if post_behavior: click.echo( f"Clean resources according to post behavior for following Test IDs: {test_id}" ) else: click.echo( f"Clean all resources for following Test IDs: {test_id}") params = ({"TestId": tid, **user_param} for tid in test_id)
def clean_resources(ctx, user, test_id, logdir, config_file, backend): # pylint: disable=too-many-arguments,too-many-branches params = dict() if config_file or logdir: if not logdir: logdir = os.path.expandvars("$HOME/sct-results") if logdir and not test_id: test_id = (search_test_id_in_latest(logdir), ) if not logdir or not all(test_id): click.echo(clean_resources.get_help(ctx)) return if not backend: backend = "aws" if not os.environ.get('SCT_CLUSTER_BACKEND', None): os.environ['SCT_CLUSTER_BACKEND'] = backend if config_file: os.environ['SCT_CONFIG_FILES'] = str(list(config_file)) config = SCTConfiguration() for _test_id in test_id: params['TestId'] = _test_id if 'aws' in backend: clean_aws_instances_according_post_behavior( params, config, logdir) if 'gce' in backend: clean_gce_instances_according_post_behavior( params, config, logdir) else: if not (user or test_id): click.echo(clean_resources.get_help(ctx)) return if user: params['RunByUser'] = user if test_id: for _test_id in test_id: params['TestId'] = _test_id clean_cloud_instances(params) click.echo('cleaned instances for {}'.format(params)) else: clean_cloud_instances(params) click.echo('cleaned instances for {}'.format(params))
def clean_resources(ctx, user, test_id, logdir, config_file): # pylint: disable=too-many-arguments,too-many-branches add_file_logger() params = dict() if config_file or logdir: if not logdir: logdir = Setup.base_logdir() if logdir and not test_id: test_id = (search_test_id_in_latest(logdir), ) if not logdir or not all(test_id): click.echo(clean_resources.get_help(ctx)) return # need to pass SCTConfigration verification, # but not affect on result os.environ['SCT_CLUSTER_BACKEND'] = "aws" if config_file: os.environ['SCT_CONFIG_FILES'] = str(list(config_file)) config = SCTConfiguration() for _test_id in test_id: params['TestId'] = _test_id clean_resources_according_post_behavior(params, config, logdir) else: if not (user or test_id): click.echo(clean_resources.get_help(ctx)) return if user: params['RunByUser'] = user if test_id: for _test_id in test_id: params['TestId'] = _test_id clean_cloud_resources(params) click.echo('cleaned instances for {}'.format(params)) else: clean_cloud_resources(params) click.echo('cleaned instances for {}'.format(params))
def define_test_id(self): if not self._test_id: self._test_id = search_test_id_in_latest(self.sct_result_dir)