| 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 |
|
| 17 |
from jenkins_jobs.builder import JenkinsManager |
| 18 |
from jenkins_jobs.errors import JenkinsJobsException |
| 19 |
import jenkins_jobs.cli.subcommand.base as base |
| 20 |
|
| 21 |
|
| 22 |
class DeleteSubCommand(base.JobsSubCommand): |
| 23 |
def parse_args(self, subparser): |
| 24 |
delete = subparser.add_parser("delete") |
| 25 |
|
| 26 |
self.parse_option_recursive_exclude(delete) |
| 27 |
|
| 28 |
delete.add_argument("name", help="name of job", nargs="+") |
| 29 |
delete.add_argument( |
| 30 |
"-p", |
| 31 |
"--path", |
| 32 |
default=None, |
| 33 |
help="colon-separated list of paths to YAML files " "or directories", |
| 34 |
) |
| 35 |
delete.add_argument( |
| 36 |
"-j", |
| 37 |
"--jobs-only", |
| 38 |
action="store_true", |
| 39 |
dest="del_jobs", |
| 40 |
default=False, |
| 41 |
help="delete only jobs", |
| 42 |
) |
| 43 |
delete.add_argument( |
| 44 |
"-v", |
| 45 |
"--views-only", |
| 46 |
action="store_true", |
| 47 |
dest="del_views", |
| 48 |
default=False, |
| 49 |
help="delete only views", |
| 50 |
) |
| 51 |
|
| 52 |
def execute(self, options, jjb_config): |
| 53 |
builder = JenkinsManager(jjb_config) |
| 54 |
|
| 55 |
if options.del_jobs and options.del_views: |
| 56 |
raise JenkinsJobsException( |
| 57 |
'"--views-only" and "--jobs-only" cannot be used together.' |
| 58 |
) |
| 59 |
|
| 60 |
if options.path: |
| 61 |
roots = self.load_roots(jjb_config, options.path) |
| 62 |
jobs = base.filter_matching(roots.generate_jobs(), options.name) |
| 63 |
views = base.filter_matching(roots.generate_views(), options.name) |
| 64 |
job_names = [j.name for j in jobs] |
| 65 |
view_names = [v.name for v in views] |
| 66 |
else: |
| 67 |
job_names = options.name |
| 68 |
view_names = options.name |
| 69 |
|
| 70 |
if options.del_jobs: |
| 71 |
builder.delete_jobs(job_names) |
| 72 |
elif options.del_views: |
| 73 |
builder.delete_views(view_names) |
| 74 |
else: |
| 75 |
builder.delete_jobs(job_names) |
| 76 |
builder.delete_views(view_names) |