def run(dry_run): jenkins_instances = queries.get_jenkins_instances() settings = queries.get_app_interface_settings() for instance in jenkins_instances: if instance.get('deleteMethod') != 'manual': continue managed_projects = instance.get('managedProjects') if not managed_projects: continue instance_name = instance['name'] token = instance['token'] jenkins = JenkinsApi(token, ssl_verify=False, settings=settings) all_job_names = jenkins.get_job_names() managed_job_names = \ get_managed_job_names(all_job_names, managed_projects) desired_job_names = get_desired_job_names(instance_name) delete_job_names = [ j for j in managed_job_names if j not in desired_job_names ] for job_name in delete_job_names: logging.info(['delete_job', instance_name, job_name]) if not dry_run: jenkins.delete_job(job_name)
def run(dry_run): jenkins_instances = queries.get_jenkins_instances() settings = queries.get_app_interface_settings() for instance in jenkins_instances: instance_cleanup_rules = instance.get("buildsCleanupRules", []) if not instance_cleanup_rules: # Skip instance if no cleanup rules defined continue # Process cleanup rules, pre-compile as regexes cleanup_rules = [] for rule in instance_cleanup_rules: cleanup_rules.append({ "name": rule["name"], "name_re": re.compile(rule["name"]), "keep_hours": rule["keep_hours"], "keep_ms": hours_to_ms(rule["keep_hours"]), }) token = instance["token"] jenkins = JenkinsApi(token, ssl_verify=False, settings=settings) all_job_names = jenkins.get_job_names() builds_todel = find_builds(jenkins, all_job_names, cleanup_rules) logging.info(f"{len(builds_todel)} builds will be deleted") delete_builds(jenkins, builds_todel, dry_run) logging.info("deletion completed")
def run(dry_run): jenkins_instances = queries.get_jenkins_instances() settings = queries.get_app_interface_settings() for instance in jenkins_instances: instance_cleanup_rules = instance.get('buildsCleanupRules', []) if not instance_cleanup_rules: # Skip instance if no cleanup rules defined continue # Process cleanup rules, pre-compile as regexes cleanup_rules = [] for rule in instance_cleanup_rules: cleanup_rules.append({ 'name': rule['name'], 'name_re': re.compile(rule['name']), 'keep_hours': rule['keep_hours'], 'keep_ms': hours_to_ms(rule['keep_hours']) }) token = instance['token'] jenkins = JenkinsApi(token, ssl_verify=False, settings=settings) all_job_names = jenkins.get_job_names() builds_todel = find_builds(jenkins, all_job_names, cleanup_rules) logging.info(f'{len(builds_todel)} builds will be deleted') delete_builds(jenkins, builds_todel, dry_run) logging.info('deletion completed')
def get_jenkins_map(plugins_only=False, desired_instances=None): gqlapi = gql.get_api() jenkins_instances = gqlapi.query(INSTANCES_QUERY)['instances'] settings = queries.get_app_interface_settings() jenkins_map = {} for instance in jenkins_instances: instance_name = instance['name'] if desired_instances and instance_name not in desired_instances: continue if instance_name in jenkins_map: continue if plugins_only and not instance['plugins']: continue token = instance['token'] jenkins = JenkinsApi(token, ssl_verify=False, settings=settings) jenkins_map[instance_name] = jenkins return jenkins_map
def get_jenkins_map(): gqlapi = gql.get_api() permissions = gqlapi.query(PERMISSIONS_QUERY)['permissions'] settings = queries.get_app_interface_settings() jenkins_permissions = \ [p for p in permissions if p['service'] == 'jenkins-role'] jenkins_map = {} for jp in jenkins_permissions: instance = jp['instance'] instance_name = instance['name'] if instance_name in jenkins_map: continue token = instance['token'] jenkins = JenkinsApi(token, ssl_verify=False, settings=settings) jenkins_map[instance_name] = jenkins return jenkins_map