Skip to content

ark_exec_action

ArkExecAction

Bases: ArkAction

Source code in ark_sdk_python/actions/ark_exec_action.py
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class ArkExecAction(ArkAction):
    def _serialize_output(self, output: Optional[Union[List, Dict, ArkModel, Generator, Tuple, Any]]) -> str:
        if output is None:
            return ''
        if isinstance(output, Generator):
            return self._serialize_output(list(itertools.chain.from_iterable([p.items for p in output])))
        if isinstance(output, list):
            return json.dumps(
                [json.loads(a.json(by_alias=False, exclude={'poll_progress_callback'})) for a in output if a is not None], indent=4
            )
        elif isinstance(output, tuple):
            return json.dumps(
                [json.loads(a.json(by_alias=False, exclude={'poll_progress_callback'})) for a in output if a is not None], indent=4
            )
        elif isinstance(output, dict):
            return json.dumps(
                {
                    k: json.loads(v.json(indent=4, by_alias=False, exclude={'poll_progress_callback'}))
                    for k, v in output.items()
                    if k is not None and v is not None
                },
                indent=4,
            )
        elif issubclass(type(output), ArkModel):
            return output.json(indent=4, by_alias=False, exclude={'poll_progress_callback'})
        elif issubclass(type(output), ArkAsyncRequest):
            return output.async_task.json(indent=4, by_alias=False)
        return str(output)

    def _write_output_to_file(self, output_path: str, serialized_output: str) -> None:
        output_path = os.path.abspath(output_path)
        if not os.path.exists(os.path.dirname(output_path)):
            os.makedirs(os.path.dirname(output_path), exist_ok=True)
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(serialized_output)

    def _run_async_action(
        self, service: ArkService, schemas_map: Dict[str, Optional[Type[ArkModel]]], action: str, args: argparse.Namespace
    ) -> None:
        try:
            model_type: Type[ArkPollableModel] = schemas_map[action.replace('_', '-')]
            model: ArkPollableModel = model_type.parse_obj(ArkPydanticArgparse.argparse_to_schema(model_type.schema(), args))
            model.poll_progress_callback = ArkPollers.default_poller()
            output = getattr(service, action.replace('-', '_'))(model)
            async_req = None
            if issubclass(type(output), ArkAsyncRequest):
                async_req = output
            elif isinstance(output, tuple):
                for a in output:
                    if issubclass(type(a), ArkAsyncRequest):
                        async_req = a
                        break
            if async_req is not None:
                if args.output_path:
                    self._write_output_to_file(
                        args.output_path, async_req.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})
                    )
                if async_req.task_failed():
                    if async_req.task_timeout():
                        ArkArgsFormatter.print_warning(
                            f'Failed to execute async command due to timeout, error:\n{async_req.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                        )
                        raise ArkException(
                            f'Failed to execute async command due to timeout, error:\n{async_req.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                        )
                    else:
                        ArkArgsFormatter.print_failure(
                            f'Failed to execute async command, error:\n{async_req.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                        )
                        raise ArkException(
                            f'Failed to execute async command, error:\n{async_req.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                        )
                else:
                    ArkArgsFormatter.print_success(self._serialize_output(output))
            elif isinstance(output, list) and all(issubclass(type(ar), ArkAsyncRequest) for ar in output):
                if args.output_path:
                    self._write_output_to_file(
                        args.output_path,
                        json.dumps([ar.async_task.dict(indent=4, by_alias=False, exclude={"poll_progress_callback"}) for ar in output]),
                    )
                for ar in output:
                    if ar.task_failed():
                        if ar.task_timeout():
                            ArkArgsFormatter.print_warning(
                                f'Failed to execute async command due to timeout, error:\n{ar.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                            )
                            raise ArkException(
                                f'Failed to execute async command due to timeout, error:\n{ar.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                            )
                        else:
                            ArkArgsFormatter.print_failure(
                                f'Failed to execute async command, error:\n{ar.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                            )
                            raise ArkException(
                                f'Failed to execute async command, error:\n{ar.async_task.json(indent=4, by_alias=False, exclude={"poll_progress_callback"})}',
                            )
                    else:
                        ArkArgsFormatter.print_success(self._serialize_output(ar))
            elif output is not None:
                ArkArgsFormatter.print_success(self._serialize_output(output))
        except Exception as ex:
            self._logger.exception(f'Failed running async command {action}')
            ArkArgsFormatter.print_failure(f'Failed to execute async command, error:\n{str(ex)}')
            self._logger.debug(traceback.format_exc())
            raise ex

    def _run_sync_action(
        self, service: ArkService, schemas_map: Dict[str, Optional[Type[ArkModel]]], action: str, args: argparse.Namespace
    ) -> None:
        try:
            model_type: Type[ArkPollableModel] = schemas_map[action.replace('_', '-')]
            if model_type:
                model: ArkModel = model_type.parse_obj(ArkPydanticArgparse.argparse_to_schema(model_type.schema(), args))
                output = getattr(service, action.replace('-', '_'))(model)
            else:
                output = getattr(service, action.replace('-', '_'))()
            if output is not None:
                serialized_output: str = self._serialize_output(output)
                if args.output_path:
                    self._write_output_to_file(args.output_path, serialized_output)
                ArkArgsFormatter.print_success(serialized_output)
            else:
                ArkArgsFormatter.print_success(f'{action.replace("-", " ").title()} finished successfully')
        except Exception as ex:
            self._logger.exception(f'Failed running command {action}')
            ArkArgsFormatter.print_failure(f'Failed to execute command, error:\n{str(ex)}')
            self._logger.debug(traceback.format_exc())
            raise ex

    def _define_actions_by_schemas(
        self,
        subparsers: argparse._SubParsersAction,
        schemas_map: Dict[str, Optional[Type[ArkModel]]],
        defaults_map: Optional[Dict[str, Dict[str, Any]]] = None,
    ):
        for action, schema in schemas_map.items():
            parser = subparsers.add_parser(action)
            if schema:
                ArkPydanticArgparse.schema_to_argparse(
                    schema.schema(), parser, defaults=defaults_map.get(action, None) if defaults_map else None
                )

    @overrides
    def define_action(self, subparsers: argparse._SubParsersAction) -> None:
        """
        Defines the CLI `exec` action, with its subparsers (args) for the service.

        Args:
            subparsers (argparse._SubParsersAction): _description_
        """
        exec_parser = None
        exec_subparsers = None
        # Check if the exec subparser already exists from previous definitions of a service
        if 'exec' in subparsers._name_parser_map.keys():  # pylint: disable=protected-access
            # Retrieve the existing exec parser
            exec_parser = subparsers._name_parser_map['exec']  # pylint: disable=protected-access
            exec_subparsers = exec_parser._subparsers._group_actions[0]  # pylint: disable=protected-access
        else:
            # Create a new exec parser
            exec_parser = subparsers.add_parser('exec')
            self._common_actions_configuration(exec_parser)
            exec_parser.add_argument('-pn', '--profile-name', default=ArkProfileLoader.default_profile_name(), help='Profile name to load')
            exec_parser.add_argument('-op', '--output-path', help='Output file to write data to')
            exec_parser.add_argument('-rf', '--request-file', help='Request file containing the parameters for the exec action')
            exec_parser.add_argument('-rc', '--retry-count', type=int, help='Retry count for execution', default=1)
            exec_parser.add_argument(
                '-ra',
                '--refresh-auth',
                action='store_true',
                help='If possible, will try to refresh the active authentication before running the actual command',
            )
            exec_subparsers = exec_parser.add_subparsers(dest="command")
            exec_subparsers.required = True
        self.define_exec_action(exec_subparsers)

    @overrides
    def run_action(self, args: argparse.Namespace) -> None:
        """
        Runs the exec action.
        Loads the authenticators from the cache and connects to the API using the loaded authenticators.
        Each service is created from the API, based on the given authenticators, and then
        runs the exec action using the API.

        Args:
            args (argparse.Namespace): _description_

        Raises:
            ArkException: _description_
            ArkException: _description_
        """
        self._common_actions_execution(args)
        profile = ArkProfileLoader.load_profile(ArkProfileLoader.deduce_profile_name(args.profile_name))
        if not profile:
            raise ArkException('Please configure a profile and login before trying to exec')

        # Load token from cache for each auth profile
        authenticators: List[ArkAuth] = []
        for authenticator_name in profile.auth_profiles.keys():
            authenticator = SUPPORTED_AUTHENTICATORS[authenticator_name]()
            if not authenticator.load_authentication(profile, args.refresh_auth):
                continue
            authenticators.append(authenticator)

        if len(authenticators) == 0:
            raise ArkException(
                'Failed to load authenticators, tokens are either expired or authenticators are not logged in, please login first'
            )
        if len(authenticators) != len(profile.auth_profiles) and ArkSystemConfig.is_interactive():
            ArkArgsFormatter.print_colored('Not all authenticators are logged in, some of the functionality will be disabled')

        # Create the CLI API with the authenticators
        api = ArkCLIAPI(authenticators, profile)

        # Run the actual exec fitting action with the api
        # Run it with retries as per defined by user
        retry_call(
            self.run_exec_action,
            fargs=[api, args],
            tries=args.retry_count,
            delay=1,
            logger=namedtuple("logger", ("warning"))(
                warning=lambda _1, _2, delay: ArkArgsFormatter.print_failure(f"Retrying in {delay} seconds")
            ),
        )

    @overrides
    def can_run_action(self, action_name: str, args: argparse.Namespace) -> bool:
        """
        Asserts the action is `exec`.

        Args:
            action_name (str): _description_
            args (argparse.Namespace): _description_

        Returns:
            bool: _description_
        """
        return action_name == 'exec' and self.can_run_exec_action(args.command, args)

    @abstractmethod
    def define_exec_action(self, exec_subparsers: argparse._SubParsersAction) -> None:
        """
        Defines an exec action, and its specified configurations and args, for a service.

        Args:
            exec_subparsers (argparse._SubParsersAction): _description_
        """

    @abstractmethod
    def run_exec_action(self, api: ArkCLIAPI, args: argparse.Namespace) -> None:
        """
        Runs the exec action for a service with the specified arguments and API.

        Args:
            api (ArkCLIAPI): _description_
            args (argparse.Namespace): _description_
        """

    @abstractmethod
    def can_run_exec_action(self, command_name: str, args: argparse.Namespace) -> bool:
        """
        Checks whether the specified exec service action can be run.

        Args:
            command_name (str): _description_
            args (argparse.Namespace): _description_

        Returns:
            bool: _description_
        """

can_run_action(action_name, args)

Asserts the action is exec.

Parameters:

Name Type Description Default
action_name str

description

required
args Namespace

description

required

Returns:

Name Type Description
bool bool

description

Source code in ark_sdk_python/actions/ark_exec_action.py
250
251
252
253
254
255
256
257
258
259
260
261
262
@overrides
def can_run_action(self, action_name: str, args: argparse.Namespace) -> bool:
    """
    Asserts the action is `exec`.

    Args:
        action_name (str): _description_
        args (argparse.Namespace): _description_

    Returns:
        bool: _description_
    """
    return action_name == 'exec' and self.can_run_exec_action(args.command, args)

can_run_exec_action(command_name, args) abstractmethod

Checks whether the specified exec service action can be run.

Parameters:

Name Type Description Default
command_name str

description

required
args Namespace

description

required

Returns:

Name Type Description
bool bool

description

Source code in ark_sdk_python/actions/ark_exec_action.py
283
284
285
286
287
288
289
290
291
292
293
294
@abstractmethod
def can_run_exec_action(self, command_name: str, args: argparse.Namespace) -> bool:
    """
    Checks whether the specified exec service action can be run.

    Args:
        command_name (str): _description_
        args (argparse.Namespace): _description_

    Returns:
        bool: _description_
    """

define_action(subparsers)

Defines the CLI exec action, with its subparsers (args) for the service.

Parameters:

Name Type Description Default
subparsers _SubParsersAction

description

required
Source code in ark_sdk_python/actions/ark_exec_action.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
@overrides
def define_action(self, subparsers: argparse._SubParsersAction) -> None:
    """
    Defines the CLI `exec` action, with its subparsers (args) for the service.

    Args:
        subparsers (argparse._SubParsersAction): _description_
    """
    exec_parser = None
    exec_subparsers = None
    # Check if the exec subparser already exists from previous definitions of a service
    if 'exec' in subparsers._name_parser_map.keys():  # pylint: disable=protected-access
        # Retrieve the existing exec parser
        exec_parser = subparsers._name_parser_map['exec']  # pylint: disable=protected-access
        exec_subparsers = exec_parser._subparsers._group_actions[0]  # pylint: disable=protected-access
    else:
        # Create a new exec parser
        exec_parser = subparsers.add_parser('exec')
        self._common_actions_configuration(exec_parser)
        exec_parser.add_argument('-pn', '--profile-name', default=ArkProfileLoader.default_profile_name(), help='Profile name to load')
        exec_parser.add_argument('-op', '--output-path', help='Output file to write data to')
        exec_parser.add_argument('-rf', '--request-file', help='Request file containing the parameters for the exec action')
        exec_parser.add_argument('-rc', '--retry-count', type=int, help='Retry count for execution', default=1)
        exec_parser.add_argument(
            '-ra',
            '--refresh-auth',
            action='store_true',
            help='If possible, will try to refresh the active authentication before running the actual command',
        )
        exec_subparsers = exec_parser.add_subparsers(dest="command")
        exec_subparsers.required = True
    self.define_exec_action(exec_subparsers)

define_exec_action(exec_subparsers) abstractmethod

Defines an exec action, and its specified configurations and args, for a service.

Parameters:

Name Type Description Default
exec_subparsers _SubParsersAction

description

required
Source code in ark_sdk_python/actions/ark_exec_action.py
264
265
266
267
268
269
270
271
@abstractmethod
def define_exec_action(self, exec_subparsers: argparse._SubParsersAction) -> None:
    """
    Defines an exec action, and its specified configurations and args, for a service.

    Args:
        exec_subparsers (argparse._SubParsersAction): _description_
    """

run_action(args)

Runs the exec action. Loads the authenticators from the cache and connects to the API using the loaded authenticators. Each service is created from the API, based on the given authenticators, and then runs the exec action using the API.

Parameters:

Name Type Description Default
args Namespace

description

required

Raises:

Type Description
ArkException

description

ArkException

description

Source code in ark_sdk_python/actions/ark_exec_action.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@overrides
def run_action(self, args: argparse.Namespace) -> None:
    """
    Runs the exec action.
    Loads the authenticators from the cache and connects to the API using the loaded authenticators.
    Each service is created from the API, based on the given authenticators, and then
    runs the exec action using the API.

    Args:
        args (argparse.Namespace): _description_

    Raises:
        ArkException: _description_
        ArkException: _description_
    """
    self._common_actions_execution(args)
    profile = ArkProfileLoader.load_profile(ArkProfileLoader.deduce_profile_name(args.profile_name))
    if not profile:
        raise ArkException('Please configure a profile and login before trying to exec')

    # Load token from cache for each auth profile
    authenticators: List[ArkAuth] = []
    for authenticator_name in profile.auth_profiles.keys():
        authenticator = SUPPORTED_AUTHENTICATORS[authenticator_name]()
        if not authenticator.load_authentication(profile, args.refresh_auth):
            continue
        authenticators.append(authenticator)

    if len(authenticators) == 0:
        raise ArkException(
            'Failed to load authenticators, tokens are either expired or authenticators are not logged in, please login first'
        )
    if len(authenticators) != len(profile.auth_profiles) and ArkSystemConfig.is_interactive():
        ArkArgsFormatter.print_colored('Not all authenticators are logged in, some of the functionality will be disabled')

    # Create the CLI API with the authenticators
    api = ArkCLIAPI(authenticators, profile)

    # Run the actual exec fitting action with the api
    # Run it with retries as per defined by user
    retry_call(
        self.run_exec_action,
        fargs=[api, args],
        tries=args.retry_count,
        delay=1,
        logger=namedtuple("logger", ("warning"))(
            warning=lambda _1, _2, delay: ArkArgsFormatter.print_failure(f"Retrying in {delay} seconds")
        ),
    )

run_exec_action(api, args) abstractmethod

Runs the exec action for a service with the specified arguments and API.

Parameters:

Name Type Description Default
api ArkCLIAPI

description

required
args Namespace

description

required
Source code in ark_sdk_python/actions/ark_exec_action.py
273
274
275
276
277
278
279
280
281
@abstractmethod
def run_exec_action(self, api: ArkCLIAPI, args: argparse.Namespace) -> None:
    """
    Runs the exec action for a service with the specified arguments and API.

    Args:
        api (ArkCLIAPI): _description_
        args (argparse.Namespace): _description_
    """