Exemplo n.º 1
0
    def exec(self, command: Union[str, List[str]], **kwargs) -> Dict[str, Union[bytes, Dict, int]]:
        """
        Args:
            command: the command to run inside the docker container
            **kwargs: extra args to pass to the docker container (according to the docker API)

        Returns:
            a dict contains 3 elements:
                1. (Outputs) the container stdout
                2. (StatusCode) the command exit code
                3. (Error) the container stderr
        """
        # one important thing that you need to know about docker is that a dead container is basically an image.
        # and that is the reason why do we need to commit to create the image.
        # so we can build a new container on the basis of this dead container.
        if self._container_obj is not None:
            self._image_name = self.container.commit().id
            self.remove_container()
        kwargs['detach'] = True
        self._create_container(command=command, **kwargs)
        try:
            self.container.start()
        except Exception as error:
            logger.debug(error)
        return {**self.container.wait(), 'Outputs': self.container.logs()}
    def _search_gitlab_repo(self, gitlab_hostname: str, repo_name: Optional[str] = None,
                            project_id: Optional[int] = None) -> \
            Optional[Tuple[str, int]]:
        """
        Searches the gitlab API for the repo.
        One of `repo_name` or `project_id` is mandatory.
        Args:
            gitlab_hostname: hostname of gitlab.
            repo_name: The repo name to search.
            project_id: The project id to search

        Returns:
            If found - A tuple of the gitlab hostname and the gitlab id.
            If not found - `None`.

        """
        if not gitlab_hostname or \
                gitlab_hostname == GitContentConfig.GITHUB_USER_CONTENT or \
                gitlab_hostname == 'github.com':
            return None
        try:
            if project_id:
                res = requests.get(
                    f"https://{gitlab_hostname}/api/v4/projects/{project_id}",
                    headers={'PRIVATE-TOKEN': self.credentials.gitlab_token},
                    timeout=10,
                    verify=False)
                if res.ok:
                    return gitlab_hostname, project_id

            if repo_name:
                res = requests.get(
                    f"https://{gitlab_hostname}/api/v4/projects",
                    params={'search': repo_name},
                    headers={'PRIVATE-TOKEN': self.credentials.gitlab_token},
                    timeout=10,
                    verify=False)
                if not res.ok:
                    return None
                search_results = res.json()
                assert search_results and isinstance(
                    search_results, list) and isinstance(
                        search_results[0], dict)
                gitlab_id = search_results[0].get('id')
                if gitlab_id is None:
                    return None
                return gitlab_hostname, gitlab_id
            logger.debug(
                'Could not access GitLab api in `_search_gitlab_repo`')
            return None

        except (requests.exceptions.ConnectionError, json.JSONDecodeError,
                AssertionError) as e:
            logger.debug(str(e), exc_info=True)
            return None
    def _search_github_repo(self, github_hostname,
                            repo_name) -> Optional[Tuple[str, str]]:
        """
        Searches the github API for the repo
        Args:
            github_hostname: hostname of github.
            repo_name: repository name in this structure: "<org_name>/<repo_name>".

        Returns:
            If found -  a tuple of the github hostname and the repo name that was found.
            If not found - 'None`
        """
        if not github_hostname or not repo_name:
            return None
        api_host = github_hostname if github_hostname != GitContentConfig.GITHUB_USER_CONTENT else 'github.com'
        if api_host.lower() == 'github.com':
            github_hostname = GitContentConfig.GITHUB_USER_CONTENT
        try:
            r = requests.get(f'https://api.{api_host}/repos/{repo_name}',
                             headers={
                                 'Authorization':
                                 f"Bearer {self.credentials.github_token}"
                                 if self.credentials.github_token else None,
                                 'Accept':
                                 'application/vnd.github.VERSION.raw'
                             },
                             verify=False,
                             timeout=10)
            if r.ok:
                return github_hostname, repo_name
            r = requests.get(f'https://api.{api_host}/repos/{repo_name}',
                             verify=False,
                             params={'token': self.credentials.github_token},
                             timeout=10)
            if r.ok:
                return github_hostname, repo_name
            logger.debug(
                'Could not access GitHub api in `_search_github_repo`')
            return None
        except requests.exceptions.ConnectionError as e:
            logger.debug(str(e), exc_info=True)
            return None
Exemplo n.º 4
0
    def _search_github_repo(github_hostname: str, repo_name: str) -> Optional[Tuple[str, str]]:
        """
        Searches the github API for the repo
        Args:
            github_hostname: hostname of github.
            repo_name: repository name in this structure: "<org_name>/<repo_name>".

        Returns:
            If found -  a tuple of the github hostname and the repo name that was found.
            If not found - 'None`
        """
        if not github_hostname or not repo_name:
            return None
        api_host = GitContentConfig.USERCONTENT_TO_GITHUB.get(github_hostname, github_hostname).lower()
        github_hostname = GitContentConfig.GITHUB_TO_USERCONTENT.get(github_hostname, github_hostname)
        if (api_host, repo_name) in GitContentConfig.ALLOWED_REPOS:
            return github_hostname, repo_name
        github_hostname = GitContentConfig.GITHUB_TO_USERCONTENT.get(api_host, api_host)
        try:
            r = requests.get(f'https://api.{api_host}/repos/{repo_name}',
                             headers={
                                 'Authorization': f"Bearer {GitContentConfig.CREDENTIALS.github_token}"
                                 if GitContentConfig.CREDENTIALS.github_token else None,
                                 'Accept': 'application/vnd.github.VERSION.raw'},
                             verify=False,
                             timeout=10)
            if r.ok:
                return github_hostname, repo_name
            r = requests.get(f'https://api.{api_host}/repos/{repo_name}',
                             verify=False,
                             params={'token': GitContentConfig.CREDENTIALS.github_token},
                             timeout=10)
            if r.ok:
                return github_hostname, repo_name
            logger.debug(
                f'Could not access GitHub api in `_search_github_repo`. status code={r.status_code}, reason={r.reason}')
            return None
        except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) as e:
            logger.debug(str(e), exc_info=True)
            return None