Debian Codegraph @

jenkins_jobs/cli/subcommand/delete_all.py

raw
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 import logging
18 import sys
19
20 from jenkins_jobs import utils
21 from jenkins_jobs.builder import JenkinsManager
22 from jenkins_jobs.errors import JenkinsJobsException
23 import jenkins_jobs.cli.subcommand.base as base
24
25
26 logger = logging.getLogger(__name__)
27
28
29 class DeleteAllSubCommand(base.BaseSubCommand):
30 def parse_args(self, subparser):
31 delete_all = subparser.add_parser(
32 "delete-all",
33 help="delete *ALL* jobs from Jenkins server, including "
34 "those not managed by Jenkins Job Builder.",
35 )
36
37 self.parse_option_recursive_exclude(delete_all)
38
39 delete_all.add_argument(
40 "-j",
41 "--jobs-only",
42 action="store_true",
43 dest="del_jobs",
44 default=False,
45 help="delete only jobs",
46 )
47 delete_all.add_argument(
48 "-v",
49 "--views-only",
50 action="store_true",
51 dest="del_views",
52 default=False,
53 help="delete only views",
54 )
55 delete_all.add_argument(
56 "-f",
57 "--force",
58 action="store_true",
59 help="Do not ask interactively for confirmation. Ba cautious!",
60 )
61
62 def execute(self, options, jjb_config):
63 builder = JenkinsManager(jjb_config)
64
65 reach = set()
66 if options.del_jobs and options.del_views:
67 raise JenkinsJobsException(
68 '"--views-only" and "--jobs-only" cannot be used together.'
69 )
70 elif options.del_jobs and not options.del_views:
71 reach.add("jobs")
72 elif options.del_views and not options.del_jobs:
73 reach.add("views")
74 else:
75 reach.update(("jobs", "views"))
76
77 if not options.force and not utils.confirm(
78 "Sure you want to delete *ALL* {} from Jenkins "
79 "server?\n(including those not managed by Jenkins "
80 "Job Builder)".format(" AND ".join(reach))
81 ):
82 sys.exit("Aborted")
83
84 if "jobs" in reach:
85 logger.info("Deleting all jobs")
86 builder.delete_all_jobs()
87
88 if "views" in reach:
89 logger.info("Deleting all views")
90 builder.delete_all_views()