Skip to content

ark_sia_access_service

ArkSIAAccessService

Bases: ArkService

Source code in ark_sdk_python/services/sia/access/ark_sia_access_service.py
 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
class ArkSIAAccessService(ArkService):
    def __init__(self, isp_auth: ArkISPAuth) -> None:
        super().__init__(isp_auth)
        self.__isp_auth = isp_auth
        self.__client: ArkISPServiceClient = ArkISPServiceClient.from_isp_auth(
            isp_auth=self.__isp_auth,
            service_name='dpa',
            refresh_connection_callback=self.__refresh_sia_auth,
        )

    def __refresh_sia_auth(self, client: ArkISPServiceClient) -> None:
        ArkISPServiceClient.refresh_client(client, self.__isp_auth)

    def __create_connection(
        self,
        os_type: ArkOsType,
        target_machine: str,
        username: str,
        password: Optional[str] = None,
        private_key_path: Optional[str] = None,
        private_key_contents: Optional[str] = None,
    ) -> Tuple[ArkConnection, Dict[str, str]]:
        if os_type == ArkOsType.WINDOWS:
            connection = ArkWinRMConnection()
            connection_details = ArkConnectionDetails(
                address=target_machine,
                port=WINRM_HTTPS_PORT,
                connection_type=ArkConnectionType.WinRM,
                credentials=ArkConnectionCredentials(user=username, password=password),
                connection_data=ArkWinRMConnectionData(),
            )
        else:
            connection = ArkSSHConnection()
            connection_details = ArkConnectionDetails(
                address=target_machine,
                port=SSH_PORT,
                connection_type=ArkConnectionType.SSH,
                credentials=ArkConnectionCredentials(
                    user=username, password=password, private_key_filepath=private_key_path, private_key_contents=private_key_contents
                ),
                connection_data=ArkSSHConnectionData(),
            )
        connection.connect(connection_details)
        return connection, CONNECTOR_CMDSET[os_type]

    def __install_connector_on_machine(
        self,
        install_script: str,
        os_type: ArkOsType,
        target_machine: str,
        username: str,
        password: Optional[str] = None,
        private_key_path: Optional[str] = None,
        private_key_contents: Optional[str] = None,
    ) -> str:
        connection, cmdset = self.__create_connection(os_type, target_machine, username, password, private_key_path, private_key_contents)
        connection.run_command(ArkConnectionCommand(command=cmdset['stop-connector-service'], raise_on_error=False))
        connection.run_command(ArkConnectionCommand(command=cmdset['remove-connector-service'], raise_on_error=False))
        connection.run_command(ArkConnectionCommand(command=cmdset['remove-connector-files'], raise_on_error=False))
        if os_type == ArkOsType.WINDOWS:
            connection.run_command(ArkConnectionCommand(command=install_script, extra_command_data={'force_command_split': True}))
        else:
            connection.run_command(ArkConnectionCommand(command=install_script))
        retry_count = CONNECTOR_READY_RETRY_COUNT
        while True:
            try:
                connection.run_command(ArkConnectionCommand(command=cmdset['connector-active']))
                break
            except ArkException as ex:
                self._logger.exception(f'Failed to check whether a connector is active [{str(ex)}]')
                if retry_count > 0:
                    retry_count = retry_count - 1
                    self._logger.info(
                        f'Retrying to check if connector is active, sleeping for '
                        f'[{CONNECTOR_RETRY_TICK_SECONDS}] and retrying, retries left [{retry_count}]'
                    )
                    time.sleep(CONNECTOR_RETRY_TICK_SECONDS)
                    continue
                raise
        result = connection.run_command(ArkConnectionCommand(command=cmdset['read-connector-config']))
        connector_config = json.loads(str(result.stdout).strip())
        return connector_config['Id']

    def __uninstall_connector_on_machine(
        self,
        os_type: ArkOsType,
        target_machine: str,
        username: str,
        password: Optional[str] = None,
        private_key_path: Optional[str] = None,
        private_key_contents: Optional[str] = None,
    ) -> None:
        connection, cmdset = self.__create_connection(os_type, target_machine, username, password, private_key_path, private_key_contents)
        connection.run_command(ArkConnectionCommand(command=cmdset['stop-connector-service']))
        connection.run_command(ArkConnectionCommand(command=cmdset['remove-connector-service']))
        connection.run_command(ArkConnectionCommand(command=cmdset['remove-connector-files']))

    def connector_setup_script(self, get_connector_setup_script: ArkSIAGetConnectorSetupScript) -> ArkSIAConnectorSetupScript:
        """
        Retrieves a new connector installation setup script

        Args:
            get_connector_setup_script (ArkSIAGetConnectorSetupScript): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkSIAConnectorSetupScript: _description_
        """
        self._logger.info('Retrieving new connector setup script')
        get_connector_setup_script_dict = get_connector_setup_script.model_dump(exclude_none=True)
        get_connector_setup_script_dict['connector_type'] = serialize_access_workspace_type(get_connector_setup_script.connector_type)
        resp: Response = self.__client.post(CONNECTORS_SETUP_SCRIPT_API, json=get_connector_setup_script_dict)
        if resp.status_code == HTTPStatus.CREATED:
            try:
                return ArkSIAConnectorSetupScript.model_validate(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse connector setup script response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse connector setup script response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to retrieve connector setup script [{resp.text}] - [{resp.status_code}]')

    def install_connector(self, install_connector: ArkSIAInstallConnector) -> str:
        """
        Gets a connector installation script
        Afterwards, installs a connector on the remote machine based on the given parameters
        Uses WinRM on windows os type
        Uses SSH on linux / darwin os type

        Args:
            install_connector (ArkSIAInstallConnector): _description_

        Returns:
            str: _description_
        """
        self._logger.info(
            f'Installing connector on machine [{install_connector.target_machine}] of type [{install_connector.connector_os}]'
        )
        installation_script = self.connector_setup_script(
            ArkSIAGetConnectorSetupScript(
                connector_os=install_connector.connector_os,
                connector_pool_id=install_connector.connector_pool_id,
                connector_type=install_connector.connector_type,
            )
        )
        return self.__install_connector_on_machine(
            install_script=installation_script.bash_cmd,
            os_type=install_connector.connector_os,
            target_machine=install_connector.target_machine,
            username=install_connector.username,
            password=install_connector.password.get_secret_value() if install_connector.password else None,
            private_key_path=install_connector.private_key_path,
            private_key_contents=(
                install_connector.private_key_contents.get_secret_value() if install_connector.private_key_contents else None
            ),
        )

    def uninstall_connector(self, uninstall_connector: ArkSIAUninstallConnector) -> None:
        """
        Uninstalls a connector on the remote machine based on the given parameters
        Uses WinRM on windows os type
        Uses SSH on linux / darwin os type

        Args:
            uninstall_connector (ArkSIAUninstallConnector): _description_
        """
        self._logger.info(f'Uninstalling connector [{uninstall_connector.connector_id}] from machine')
        self.__uninstall_connector_on_machine(
            uninstall_connector.connector_os,
            uninstall_connector.target_machine,
            uninstall_connector.username,
            uninstall_connector.password.get_secret_value() if uninstall_connector.password else None,
            uninstall_connector.private_key_path,
            uninstall_connector.private_key_contents,
        )

    @staticmethod
    @overrides
    def service_config() -> ArkServiceConfig:
        return SERVICE_CONFIG

connector_setup_script(get_connector_setup_script)

Retrieves a new connector installation setup script

Parameters:

Name Type Description Default
get_connector_setup_script ArkSIAGetConnectorSetupScript

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkSIAConnectorSetupScript ArkSIAConnectorSetupScript

description

Source code in ark_sdk_python/services/sia/access/ark_sia_access_service.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def connector_setup_script(self, get_connector_setup_script: ArkSIAGetConnectorSetupScript) -> ArkSIAConnectorSetupScript:
    """
    Retrieves a new connector installation setup script

    Args:
        get_connector_setup_script (ArkSIAGetConnectorSetupScript): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkSIAConnectorSetupScript: _description_
    """
    self._logger.info('Retrieving new connector setup script')
    get_connector_setup_script_dict = get_connector_setup_script.model_dump(exclude_none=True)
    get_connector_setup_script_dict['connector_type'] = serialize_access_workspace_type(get_connector_setup_script.connector_type)
    resp: Response = self.__client.post(CONNECTORS_SETUP_SCRIPT_API, json=get_connector_setup_script_dict)
    if resp.status_code == HTTPStatus.CREATED:
        try:
            return ArkSIAConnectorSetupScript.model_validate(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse connector setup script response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse connector setup script response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to retrieve connector setup script [{resp.text}] - [{resp.status_code}]')

install_connector(install_connector)

Gets a connector installation script Afterwards, installs a connector on the remote machine based on the given parameters Uses WinRM on windows os type Uses SSH on linux / darwin os type

Parameters:

Name Type Description Default
install_connector ArkSIAInstallConnector

description

required

Returns:

Name Type Description
str str

description

Source code in ark_sdk_python/services/sia/access/ark_sia_access_service.py
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
def install_connector(self, install_connector: ArkSIAInstallConnector) -> str:
    """
    Gets a connector installation script
    Afterwards, installs a connector on the remote machine based on the given parameters
    Uses WinRM on windows os type
    Uses SSH on linux / darwin os type

    Args:
        install_connector (ArkSIAInstallConnector): _description_

    Returns:
        str: _description_
    """
    self._logger.info(
        f'Installing connector on machine [{install_connector.target_machine}] of type [{install_connector.connector_os}]'
    )
    installation_script = self.connector_setup_script(
        ArkSIAGetConnectorSetupScript(
            connector_os=install_connector.connector_os,
            connector_pool_id=install_connector.connector_pool_id,
            connector_type=install_connector.connector_type,
        )
    )
    return self.__install_connector_on_machine(
        install_script=installation_script.bash_cmd,
        os_type=install_connector.connector_os,
        target_machine=install_connector.target_machine,
        username=install_connector.username,
        password=install_connector.password.get_secret_value() if install_connector.password else None,
        private_key_path=install_connector.private_key_path,
        private_key_contents=(
            install_connector.private_key_contents.get_secret_value() if install_connector.private_key_contents else None
        ),
    )

uninstall_connector(uninstall_connector)

Uninstalls a connector on the remote machine based on the given parameters Uses WinRM on windows os type Uses SSH on linux / darwin os type

Parameters:

Name Type Description Default
uninstall_connector ArkSIAUninstallConnector

description

required
Source code in ark_sdk_python/services/sia/access/ark_sia_access_service.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def uninstall_connector(self, uninstall_connector: ArkSIAUninstallConnector) -> None:
    """
    Uninstalls a connector on the remote machine based on the given parameters
    Uses WinRM on windows os type
    Uses SSH on linux / darwin os type

    Args:
        uninstall_connector (ArkSIAUninstallConnector): _description_
    """
    self._logger.info(f'Uninstalling connector [{uninstall_connector.connector_id}] from machine')
    self.__uninstall_connector_on_machine(
        uninstall_connector.connector_os,
        uninstall_connector.target_machine,
        uninstall_connector.username,
        uninstall_connector.password.get_secret_value() if uninstall_connector.password else None,
        uninstall_connector.private_key_path,
        uninstall_connector.private_key_contents,
    )