| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (C) 2015 Wayne Warren |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import logging |
| 17 |
import sys |
| 18 |
|
| 19 |
from jenkins_jobs.errors import JenkinsJobsException |
| 20 |
import jenkins_jobs.cli.subcommand.base as base |
| 21 |
|
| 22 |
|
| 23 |
logger = logging.getLogger(__name__) |
| 24 |
|
| 25 |
|
| 26 |
class UpdateSubCommand(base.JobsSubCommand): |
| 27 |
def parse_arg_path(self, parser): |
| 28 |
parser.add_argument( |
| 29 |
"path", |
| 30 |
nargs="?", |
| 31 |
default=sys.stdin, |
| 32 |
help="colon-separated list of paths to YAML files " "or directories", |
| 33 |
) |
| 34 |
|
| 35 |
def parse_arg_names(self, parser): |
| 36 |
parser.add_argument("names", help="name(s) of job(s)", nargs="*") |
| 37 |
|
| 38 |
def parse_args(self, subparser): |
| 39 |
update = subparser.add_parser("update") |
| 40 |
|
| 41 |
self.parse_option_recursive_exclude(update) |
| 42 |
|
| 43 |
self.parse_arg_path(update) |
| 44 |
self.parse_arg_names(update) |
| 45 |
|
| 46 |
update.add_argument( |
| 47 |
"--delete-old", |
| 48 |
action="store_true", |
| 49 |
dest="delete_old", |
| 50 |
default=False, |
| 51 |
help="delete obsolete jobs", |
| 52 |
) |
| 53 |
update.add_argument( |
| 54 |
"-p", |
| 55 |
"--plugin-info", |
| 56 |
dest="plugins_info_path", |
| 57 |
default=None, |
| 58 |
help="path to plugin info YAML file. Can be used to provide " |
| 59 |
"previously retrieved plugins info when connecting credentials " |
| 60 |
"don't have permissions to query.", |
| 61 |
) |
| 62 |
update.add_argument( |
| 63 |
"--workers", |
| 64 |
type=int, |
| 65 |
default=1, |
| 66 |
dest="n_workers", |
| 67 |
help="number of workers to use, 0 for autodetection and 1 " |
| 68 |
"for just one worker.", |
| 69 |
) |
| 70 |
update.add_argument( |
| 71 |
"--existing-only", |
| 72 |
action="store_true", |
| 73 |
default=False, |
| 74 |
dest="existing_only", |
| 75 |
help="update existing jobs only", |
| 76 |
) |
| 77 |
|
| 78 |
update.add_argument( |
| 79 |
"--enabled-only", |
| 80 |
action="store_true", |
| 81 |
default=False, |
| 82 |
dest="enabled_only", |
| 83 |
help="update enabled jobs only", |
| 84 |
) |
| 85 |
|
| 86 |
update_type = update.add_mutually_exclusive_group() |
| 87 |
update_type.add_argument( |
| 88 |
"-j", |
| 89 |
"--jobs-only", |
| 90 |
action="store_const", |
| 91 |
dest="update", |
| 92 |
const="jobs", |
| 93 |
help="update only jobs", |
| 94 |
) |
| 95 |
update_type.add_argument( |
| 96 |
"-v", |
| 97 |
"--views-only", |
| 98 |
action="store_const", |
| 99 |
dest="update", |
| 100 |
const="views", |
| 101 |
help="update only views", |
| 102 |
) |
| 103 |
|
| 104 |
def execute(self, options, jjb_config): |
| 105 |
if options.n_workers < 0: |
| 106 |
raise JenkinsJobsException( |
| 107 |
"Number of workers must be equal or greater than 0" |
| 108 |
) |
| 109 |
|
| 110 |
builder, xml_jobs, xml_views = self.make_jobs_and_views_xml( |
| 111 |
jjb_config, options.path, options.names |
| 112 |
) |
| 113 |
|
| 114 |
if options.enabled_only: |
| 115 |
# filter out jobs which are disabled |
| 116 |
xml_jobs_filtered = [] |
| 117 |
for xml_job in xml_jobs: |
| 118 |
el = xml_job.xml.find("./disabled") |
| 119 |
if el is not None: |
| 120 |
if el.text == "true": |
| 121 |
continue |
| 122 |
xml_jobs_filtered.append(xml_job) |
| 123 |
|
| 124 |
logging.info( |
| 125 |
"Will only deploy enabled jobs " |
| 126 |
"(skipping {} disabled jobs)".format( |
| 127 |
len(xml_jobs) - len(xml_jobs_filtered) |
| 128 |
) |
| 129 |
) |
| 130 |
xml_jobs = xml_jobs_filtered |
| 131 |
|
| 132 |
if options.update in {"jobs", "all"}: |
| 133 |
jobs, num_updated_jobs = builder.update_jobs( |
| 134 |
xml_jobs, |
| 135 |
n_workers=options.n_workers, |
| 136 |
existing_only=options.existing_only, |
| 137 |
) |
| 138 |
logger.info("Number of jobs updated: %d", num_updated_jobs) |
| 139 |
if options.update in {"views", "all"}: |
| 140 |
views, num_updated_views = builder.update_views( |
| 141 |
xml_views, |
| 142 |
n_workers=options.n_workers, |
| 143 |
existing_only=options.existing_only, |
| 144 |
) |
| 145 |
logger.info("Number of views updated: %d", num_updated_views) |
| 146 |
|
| 147 |
if options.delete_old: |
| 148 |
if options.update in {"jobs", "all"}: |
| 149 |
keep_jobs = [job.name for job in xml_jobs] |
| 150 |
n = builder.delete_old_managed_jobs(keep=keep_jobs) |
| 151 |
logger.info("Number of jobs deleted: %d", n) |
| 152 |
if options.update in {"views", "all"}: |
| 153 |
keep_views = [view.name for view in xml_views] |
| 154 |
n = builder.delete_old_managed_views(keep=keep_views) |
| 155 |
logger.info("Number of views deleted: %d", n) |