summaryrefslogtreecommitdiffstats
path: root/jenkins_jobs/cli/subcommand/delete_all.py
blob: bee23cf860f673ca729ff0d9e92a0a2359cd234a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# Copyright (C) 2015 Wayne Warren
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


import logging
import sys

from jenkins_jobs import utils
from jenkins_jobs.builder import JenkinsManager
from jenkins_jobs.errors import JenkinsJobsException
import jenkins_jobs.cli.subcommand.base as base


logger = logging.getLogger(__name__)


class DeleteAllSubCommand(base.BaseSubCommand):
    def parse_args(self, subparser):
        delete_all = subparser.add_parser(
            "delete-all",
            help="delete *ALL* jobs from Jenkins server, including "
            "those not managed by Jenkins Job Builder.",
        )

        self.parse_option_recursive_exclude(delete_all)

        delete_all.add_argument(
            "-j",
            "--jobs-only",
            action="store_true",
            dest="del_jobs",
            default=False,
            help="delete only jobs",
        )
        delete_all.add_argument(
            "-v",
            "--views-only",
            action="store_true",
            dest="del_views",
            default=False,
            help="delete only views",
        )

    def execute(self, options, jjb_config):
        builder = JenkinsManager(jjb_config)

        reach = set()
        if options.del_jobs and options.del_views:
            raise JenkinsJobsException(
                '"--views-only" and "--jobs-only" cannot be used together.'
            )
        elif options.del_jobs and not options.del_views:
            reach.add("jobs")
        elif options.del_views and not options.del_jobs:
            reach.add("views")
        else:
            reach.update(("jobs", "views"))

        if not utils.confirm(
            "Sure you want to delete *ALL* {} from Jenkins "
            "server?\n(including those not managed by Jenkins "
            "Job Builder)".format(" AND ".join(reach))
        ):
            sys.exit("Aborted")

        if "jobs" in reach:
            logger.info("Deleting all jobs")
            builder.delete_all_jobs()

        if "views" in reach:
            logger.info("Deleting all views")
            builder.delete_all_views()