Exemplo n.º 1
0
    def clean_up(self):
        """
        Clean up the system after scan is done.
        This cleans up any unused/dangling images and volumes.
        This is using `docker image prune -f` and `docker volume prune -f`
        commands under the hood. The `-f` option will help avoid the prompt
        for confirmation.
        """
        command = "docker rmi -f `docker images -f dangling=true -q`"
        self.logger.debug("Removing unused/dangling images..")
        try:
            run_cmd(command, shell=True)
        except Exception as e:
            self.logger.critical("Failing to remove dangling images.")
            self.logger.critical("Error: %s", str(e))
        else:
            self.logger.debug("Cleaned unsued images post scan.")

        command = "docker volume rm `docker volume ls -f dangling=true -q`"
        self.logger.debug("Removing unused/dangling volumes..")
        try:
            run_cmd(command, shell=True)
        except Exception as e:
            self.logger.critical("Failing to remove dangling volume.")
            self.logger.critical("Error: %s", str(e))
        else:
            self.logger.debug("Cleaned unsued volumes post scan.")
 def clean_project(self, project):
     try:
         run_cmd('oc delete build,bc,is -n {project} {suffix}'.format(
             project=project, suffix=self.oc_cmd_suffix))
     except subprocess.CalledProcessError as e:
         self.logger.error('Error during cleaning project {}: {}'.format(
             project, e))
    def clean_up(self):
        """
        Clean up the system after scan is done.
        This cleans up any unused/dangling images and volumes.
        This is using `docker image prune -f` and `docker volume prune -f`
        commands under the hood. The `-f` option will help avoid the prompt
        for confirmation.
        """
        command = "docker rmi -f `docker images -f dangling=true -q`"
        self.logger.debug("Removing unused/dangling images..")
        try:
            run_cmd(command, shell=True)
        except Exception as e:
            self.logger.critical("Failing to remove dangling images.")
            self.logger.critical("Error: %s", str(e))
        else:
            self.logger.debug("Cleaned unsued images post scan.")

        command = "docker volume rm `docker volume ls -f dangling=true -q`"
        self.logger.debug("Removing unused/dangling volumes..")
        try:
            run_cmd(command, shell=True)
        except Exception as e:
            self.logger.critical("Failing to remove dangling volume.")
            self.logger.critical("Error: %s", str(e))
        else:
            self.logger.debug("Cleaned unsued volumes post scan.")
 def clean_project(self, project):
     try:
         run_cmd(
             'oc delete build,bc,is -n {project} {suffix}'
             .format(project=project, suffix=self.oc_cmd_suffix))
     except subprocess.CalledProcessError as e:
         self.logger.error('Error during cleaning project {}: {}'.format(
             project, e))
 def delete(self, project):
     self.logger.debug('Delete openshift project: {}'.format(project))
     try:
         run_cmd('oc delete project {project} {suffix}'.format(
             project=project, suffix=self.oc_cmd_suffix))
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Error during deleting openshift project {}: {}'.format(
                 project, e))
 def delete(self, project):
     self.logger.debug('Delete openshift project: {}'.format(project))
     try:
         run_cmd(
             'oc delete project {project} {suffix}'
             .format(project=project, suffix=self.oc_cmd_suffix))
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Error during deleting openshift project {}: {}'.format(
                 project, e))
 def create(self, project):
     self.logger.debug('Create openshift project: {}'.format(project))
     try:
         run_cmd(
             'oc new-project {project} --display-name {project} {suffix}'
             .format(project=project, suffix=self.oc_cmd_suffix))
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Error during creating openshift project {}: {}'.format(
                 project, e))
 def create(self, project):
     self.logger.debug('Create openshift project: {}'.format(project))
     try:
         run_cmd(
             'oc new-project {project} --display-name {project} {suffix}'.
             format(project=project, suffix=self.oc_cmd_suffix))
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Error during creating openshift project {}: {}'.format(
                 project, e))
 def delete_pods(self, project, build_id):
     """
     Deletes the pods from OpenShift for the provided project and build_id.
     Mainly used by the delivery worker.
     """
     try:
         self.logger.debug("Deleting pods for project build: {}/{}".format(
             project, build_id))
         run_cmd('oc delete pods --namespace {project} build/{build_id}'
                 ' {suffix}'.format(project=project,
                                    build_id=build_id,
                                    suffix=self.oc_cmd_suffix))
         self.logger.info("Deleted pods for project build: {}/{}".format(
             project, build_id))
     except subprocess.CalledProcessError as e:
         self.logger.error('Could not delete pods for project build: '
                           '{}/{}\n{}'.format(project, build_id, e))
 def delete_pods(self, project, build_id):
     """
     Deletes the pods from OpenShift for the provided project and build_id.
     Mainly used by the delivery worker.
     """
     try:
         self.logger.debug("Deleting pods for project build: {}/{}"
                           .format(project, build_id))
         run_cmd(
             'oc delete pods --namespace {project} build/{build_id}'
             ' {suffix}'.format(project=project, build_id=build_id,
                                suffix=self.oc_cmd_suffix))
         self.logger.info("Deleted pods for project build: {}/{}"
                          .format(project, build_id))
     except subprocess.CalledProcessError as e:
         self.logger.error(
             'Could not delete pods for project build: '
             '{}/{}\n{}'.format(project, build_id, e)
         )
 def upload_template(self, project, template_path, template_data):
     """Upload processed template for project from template path."""
     self.logger.debug('Uploading template data: {} for project: {} from '
                       'template: {}'.format(
                           template_data, project, template_path))
     tmpl_params_str = ' '.join(
         ['-p {k}={v}'.format(k=k, v=v) for k, v in template_data.items()])
     try:
         run_cmd(
             'oc process -n {project} -f {tmpl_path} {tmpl_params} '
             '{suffix} | '
             'oc {suffix} -n {project} create -f -'.format(
                 project=project, tmpl_path=template_path,
                 tmpl_params=tmpl_params_str, suffix=self.oc_cmd_suffix),
             shell=True)
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Error during uploading processed template for project {}: {}'
             .format(project, e))
Exemplo n.º 12
0
 def upload_template(self, project, template_path, template_data):
     """Upload processed template for project from template path."""
     self.logger.debug('Uploading template data: {} for project: {} from '
                       'template: {}'.format(template_data, project,
                                             template_path))
     tmpl_params_str = ' '.join(
         ['-p {k}={v}'.format(k=k, v=v) for k, v in template_data.items()])
     try:
         run_cmd('oc process -n {project} -f {tmpl_path} {tmpl_params} '
                 '{suffix} | '
                 'oc {suffix} -n {project} create -f -'.format(
                     project=project,
                     tmpl_path=template_path,
                     tmpl_params=tmpl_params_str,
                     suffix=self.oc_cmd_suffix),
                 shell=True)
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Error during uploading processed template for project {}: {}'.
             format(project, e))
Exemplo n.º 13
0
 def login(self, user=None, password=None):
     """Login to openshift"""
     user = user or self.user
     password = password or self.password
     try:
         self.logger.debug('Login to openshift:\n{}'.format(
             run_cmd('oc login {endpoint} -u {user} -p {password} {suffix}'.
                     format(endpoint=self.endpoint,
                            user=user,
                            password=password,
                            suffix=self.oc_login_suffix))))
     except subprocess.CalledProcessError as e:
         raise OpenshiftError('Openshift login error: {}'.format(e))
Exemplo n.º 14
0
 def get_project(self, project):
     self.logger.debug(
         'Check openshift project: {} existing or not'.format(project))
     is_existing = False
     try:
         output = run_cmd(
             'oc get projects {suffix}'.format(suffix=self.oc_cmd_suffix))
         if project in output.strip():
             is_existing = True
     except subprocess.CalledProcessError as e:
         self.logger.debug('Error during fetching details for \
                           openshift project  {}: {}'.format(project, e))
     return is_existing
 def get_project(self, project):
     self.logger.debug(
         'Check openshift project: {} existing or not'.format(project))
     is_existing = False
     try:
         output = run_cmd(
             'oc get projects {suffix}'.format(suffix=self.oc_cmd_suffix))
         if project in output.strip():
             is_existing = True
     except subprocess.CalledProcessError as e:
         self.logger.debug('Error during fetching details for \
                           openshift project  {}: {}'.format(project, e))
     return is_existing
 def get_build_logs(self, project, build_id, build_type="build"):
     try:
         output = run_cmd(
             'oc logs --namespace {project} build/{build_id} {suffix}'
             .format(
                 project=project, build_id=build_id,
                 suffix=self.oc_cmd_suffix))
         self.logger.debug('Build logs for project build: {}/{}\n{}'.format(
             project, build_id, output))
     except subprocess.CalledProcessError as e:
         self.logger.error(
             'Could not retrieve {} phase logs for project build: '
             '{}/{}\n{}'.format(build_type, project, build_id, e))
         output = 'Could not retrieve %s phase logs.' % build_type
     return output
Exemplo n.º 17
0
 def get_build_logs(self, project, build_id, build_type="build"):
     try:
         output = run_cmd(
             'oc logs --namespace {project} build/{build_id} {suffix}'.
             format(project=project,
                    build_id=build_id,
                    suffix=self.oc_cmd_suffix))
         self.logger.debug('Build logs for project build: {}/{}\n{}'.format(
             project, build_id, output))
     except subprocess.CalledProcessError as e:
         self.logger.error(
             'Could not retrieve {} phase logs for project build: '
             '{}/{}\n{}'.format(build_type, project, build_id, e))
         output = 'Could not retrieve %s phase logs.' % build_type
     return output
Exemplo n.º 18
0
 def get_build_status(self, project, build_id, status_index=3):
     """Get status of an openshift project build"""
     try:
         output = run_cmd(
             'oc get --namespace {project} build/{build_id} {suffix} | '
             'grep -v STATUS'.format(project=project,
                                     build_id=build_id,
                                     suffix=self.oc_cmd_suffix),
             shell=True)
         self.logger.info('Openshift build status for: {}/{}\n{}'.format(
             project, build_id, output))
         return output.split()[status_index]
     except subprocess.CalledProcessError as e:
         self.logger.error(
             'Openshift build status fetch error for {}/{}: {}'.format(
                 project, build_id, e))
         return ""
 def build(self, project, build):
     """Run build for a project"""
     self.logger.debug('Run openshift project build: {}/{}'
                       .format(build, project))
     try:
         output = run_cmd(
             'oc --namespace {project} start-build {build} '
             '{suffix}'.format(
                 project=project, build=build, suffix=self.oc_cmd_suffix)
         )
         self.logger.info('Openshift project build run output: {}/{}\n{}'
                          .format(project, build, output))
         build_id = output.split('"')[1].rstrip()
         return build_id
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Unable to run build project build: {}/{}\nError: {}'
             .format(project, build, e))
 def get_build_status(self, project, build_id, status_index=3):
     """Get status of an openshift project build"""
     try:
         output = run_cmd(
             'oc get --namespace {project} build/{build_id} {suffix} | '
             'grep -v STATUS'.format(
                 project=project, build_id=build_id,
                 suffix=self.oc_cmd_suffix),
             shell=True
         )
         self.logger.info('Openshift build status for: {}/{}\n{}'
                          .format(project, build_id, output))
         return output.split()[status_index]
     except subprocess.CalledProcessError as e:
         self.logger.error(
             'Openshift build status fetch error for {}/{}: {}'
             .format(project, build_id, e))
         return ""
 def login(self, user=None, password=None):
     """Login to openshift"""
     user = user or self.user
     password = password or self.password
     try:
         self.logger.debug(
             'Login to openshift:\n{}'.format(
                 run_cmd(
                     'oc login {endpoint} -u {user} -p {password} {suffix}'
                     .format(
                         endpoint=self.endpoint, user=user,
                         password=password, suffix=self.oc_login_suffix
                     ))
             )
         )
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Openshift login error: {}'.format(e))
Exemplo n.º 22
0
 def build(self, project, build):
     """Run build for a project"""
     self.logger.debug('Run openshift project build: {}/{}'.format(
         build, project))
     try:
         output = run_cmd('oc --namespace {project} start-build {build} '
                          '{suffix}'.format(project=project,
                                            build=build,
                                            suffix=self.oc_cmd_suffix))
         self.logger.info(
             'Openshift project build run output: {}/{}\n{}'.format(
                 project, build, output))
         build_id = output.split('"')[1].rstrip()
         return build_id
     except subprocess.CalledProcessError as e:
         raise OpenshiftError(
             'Unable to run build project build: {}/{}\nError: {}'.format(
                 project, build, e))