File: minion_pools.py

package info (click to toggle)
python-coriolisclient 1.0.9-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 596 kB
  • sloc: python: 5,614; makefile: 23; sh: 2
file content (321 lines) | stat: -rw-r--r-- 12,949 bytes parent folder | download
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright (c) 2020 Cloudbase Solutions Srl
#
# 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.

"""
Command-line interface sub-commands related to minion_pools.
"""

import os

from cliff import command
from cliff import lister
from cliff import show

from coriolisclient.cli import formatter
from coriolisclient.cli import utils as cli_utils


class MinionPoolFormatter(formatter.EntityFormatter):

    columns = ("ID",
               "Pool Name",
               "Endpoint",
               "Pool Platform",
               "OS Type",
               "Notes",
               "Pool Status",
               "Minions (Min - Max)")

    def _get_sorted_list(self, obj_list):
        return sorted(obj_list, key=lambda o: o.created_at)

    def _get_formatted_data(self, obj):
        data = (obj.id,
                obj.name,
                obj.endpoint_id,
                obj.platform,
                obj.os_type,
                obj.notes,
                obj.status,
                "%s - %s" % (
                    obj.minimum_minions, obj.maximum_minions))

        return data


class MinionPoolDetailFormatter(formatter.EntityFormatter):

    columns = ("ID",
               "Pool Name",
               "Endpoint",
               "Pool Platform",
               "OS Type",
               "Pool Status",
               "Notes",
               "Created At",
               "Updated At",
               "Maintenance Trust ID",
               "Minimum Minions",
               "Maximum Minions",
               "Minion Max Idle Time",
               "Minion Retention Strategy",
               "Environment Options",
               "Shared Resources",
               "Events",
               "Progress Updates",
               "Minion Machines")

    def _get_sorted_list(self, obj_list):
        return sorted(obj_list, key=lambda o: o.created_at)

    def _format_pool_event(self, event):
        return (
            "%(level)s %(created_at)s %(message)s" % event)

    def _format_pool_events(self, events):
        return ("%(ls)s" % {"ls": os.linesep}).join(
            [self._format_pool_event(e) for e in
             sorted(events, key=lambda e: e["index"])])

    def _format_progress_updates(self, progress_updates):
        return ("%(ls)s" % {"ls": os.linesep}).join(
            [self._format_progress_update(p) for p in
             sorted(progress_updates,
                    key=lambda p: (p.get('index', 0), p["created_at"]))])

    def _get_formatted_data(self, obj):
        data = (obj.id,
                obj.name,
                obj.endpoint_id,
                obj.platform,
                obj.os_type,
                obj.status,
                obj.notes,
                obj.created_at,
                obj.updated_at,
                obj.maintenance_trust_id,
                obj.minimum_minions,
                obj.maximum_minions,
                obj.minion_max_idle_time,
                obj.minion_retention_strategy,
                cli_utils.format_json_for_object_property(
                    obj, prop_name="environment_options"),
                cli_utils.format_json_for_object_property(
                    obj, prop_name="shared_resources"),
                self._format_pool_events(obj.events),
                self._format_progress_updates(obj.progress_updates),
                cli_utils.format_json_for_object_property(
                    obj, prop_name="minion_machines"))
        return data


class CreateMinionPool(show.ShowOne):
    """ Creates a new minion pool. """
    def get_parser(self, prog_name):
        parser = super(CreateMinionPool, self).get_parser(prog_name)

        parser.add_argument('name',
                            help='A name for the new minion pool.')
        parser.add_argument('--os-type', required=True,
                            help='The OS type for the minions of the pool.')
        parser.add_argument('--platform', required=True,
                            help='The type of the minion pool ("source" or '
                                 "destination"').')
        parser.add_argument('--notes', dest='notes',
                            help='Notes about the minion pool.')
        parser.add_argument('--endpoint-id', required=True,
                            help='ID/Name of the Coriolis Endpoint to create '
                                 'the pool for.')
        parser.add_argument('--minimum-minions', type=int, default=None,
                            help='Minimum number of minions machines '
                                 'for the minion pool.')
        parser.add_argument('--maximum-minions', type=int, default=None,
                            help='Maximum number of minions machines '
                                 'for the minion pool.')
        parser.add_argument('--minion-max-idle-time', type=int, default=None,
                            help='Number of idle seconds for minions before '
                                 'being shelved based on the selected '
                                 '--minion-retention-strategy')
        parser.add_argument('--minion-retention-strategy', default='delete',
                            help='Action to take when scaling down the number '
                                 'machines within the pool.')
        parser.add_argument('--skip-allocation', action='store_true',
                            default=False,
                            help="Whether or not to skip allocating the minion"
                                 " pool's resources upfront. The pool "
                                 "allocation will need to be manually "
                                 "triggered before the pool can be used.")

        cli_utils.add_args_for_json_option_to_parser(
            parser, "environment-options")
        return parser

    def take_action(self, args):
        endpoints = self.app.client_manager.coriolis.endpoints
        endpoint_id = endpoints.get_endpoint_id_for_name(args.endpoint_id)
        environment_options = cli_utils.get_option_value_from_args(
            args, 'environment-options', error_on_no_value=True)
        minion_pool = self.app.client_manager.coriolis.minion_pools.create(
            args.name, endpoint_id, args.platform, args.os_type,
            environment_options, minimum_minions=args.minimum_minions,
            maximum_minions=args.maximum_minions,
            minion_max_idle_time=args.minion_max_idle_time,
            minion_retention_strategy=args.minion_retention_strategy,
            notes=args.notes, skip_allocation=args.skip_allocation)

        return MinionPoolDetailFormatter().get_formatted_entity(minion_pool)


class UpdateMinionPool(show.ShowOne):
    """ Updates a minion_pool. """
    def get_parser(self, prog_name):
        parser = super(UpdateMinionPool, self).get_parser(prog_name)

        parser.add_argument('id', help='The Minion Pools\'s ID.')
        parser.add_argument('--name',
                            help='New name for the new minion pool.')
        parser.add_argument('--os-type',
                            help='The OS type for the minions of the pool.')
        parser.add_argument('--notes', dest='notes',
                            help='Notes about the minion pool.')
        parser.add_argument('--minimum-minions', type=int, default=None,
                            help='Minimum number of minions machines '
                                 'for the minion pool.')
        parser.add_argument('--maximum-minions', type=int, default=None,
                            help='Maximum number of minions machines '
                                 'for the minion pool.')
        parser.add_argument('--minion-max-idle-time', type=int,
                            help='Number of idle seconds for minions before '
                                 'being shelved based on the selected '
                                 '--minion-retention-strategy')
        parser.add_argument('--minion-retention-strategy',
                            help='Action to take when scaling down the number '
                                 'machines within the pool.')
        cli_utils.add_args_for_json_option_to_parser(
            parser, "environment-options")

        return parser

    def take_action(self, args):
        updated_values = {}
        if args.name:
            updated_values["name"] = args.name
        if args.os_type:
            updated_values["os_type"] = args.os_type
        if args.minimum_minions is not None:
            updated_values["minimum_minions"] = args.minimum_minions
        if args.maximum_minions is not None:
            updated_values["maximum_minions"] = args.maximum_minions
        if args.minion_max_idle_time is not None:
            updated_values["minion_max_idle_time"] = args.minion_max_idle_time
        if args.minion_retention_strategy is not None:
            updated_values["minion_retention_strategy"] = (
                args.minion_retention_strategy)
        if args.notes:
            updated_values["notes"] = args.notes
        environment_options = cli_utils.get_option_value_from_args(
            args, 'environment-options', error_on_no_value=False)
        if environment_options:
            updated_values["environment_options"] = environment_options

        minion_pool = self.app.client_manager.coriolis.minion_pools.update(
            args.id, updated_values)

        return MinionPoolDetailFormatter().get_formatted_entity(minion_pool)


class ShowMinionPool(show.ShowOne):
    """ Show details for a Minion Pool. """

    def get_parser(self, prog_name):
        parser = super(ShowMinionPool, self).get_parser(prog_name)
        parser.add_argument('id', help='The minion_pool\'s id.')
        return parser

    def take_action(self, args):
        minion_pool = self.app.client_manager.coriolis.minion_pools.get(
            args.id)
        return MinionPoolDetailFormatter().get_formatted_entity(minion_pool)


class DeleteMinionPool(command.Command):
    """ Delete a Minion Pool. """

    def get_parser(self, prog_name):
        parser = super(DeleteMinionPool, self).get_parser(prog_name)
        parser.add_argument('id', help='The minion_pool\'s id.')
        return parser

    def take_action(self, args):
        self.app.client_manager.coriolis.minion_pools.delete(args.id)


class ListMinionPools(lister.Lister):
    """ List all Minion Pools. """

    def get_parser(self, prog_name):
        parser = super(ListMinionPools, self).get_parser(prog_name)
        return parser

    def take_action(self, args):
        minion_pool_list = self.app.client_manager.coriolis.minion_pools.list()
        return MinionPoolFormatter().list_objects(minion_pool_list)


class AllocateMinionPool(show.ShowOne):
    """ Allocates a Minion Pool. """

    def get_parser(self, prog_name):
        parser = super(AllocateMinionPool, self).get_parser(prog_name)
        parser.add_argument('id', help='The minion pool\'s id.')
        return parser

    def take_action(self, args):
        mps = self.app.client_manager.coriolis.minion_pools
        minion_pool = mps.allocate_minion_pool(args.id)
        return MinionPoolDetailFormatter().get_formatted_entity(minion_pool)


class RefreshMinionPool(show.ShowOne):
    """ Refreshs a Minion Pool. """

    def get_parser(self, prog_name):
        parser = super(RefreshMinionPool, self).get_parser(prog_name)
        parser.add_argument('id', help='The minion pool\'s id.')
        return parser

    def take_action(self, args):
        mps = self.app.client_manager.coriolis.minion_pools
        minion_pool = mps.refresh_minion_pool(args.id)
        return MinionPoolDetailFormatter().get_formatted_entity(minion_pool)


class DeallocateMinionPool(show.ShowOne):
    """ Deallocates a Minion Pool. """

    def get_parser(self, prog_name):
        parser = super(DeallocateMinionPool, self).get_parser(
            prog_name)
        parser.add_argument('id', help='The minion pool\'s id.')
        parser.add_argument('--force', action='store_true', default=False,
                            help='Perform a forced deallocation of the minion '
                                 'pool.')
        return parser

    def take_action(self, args):
        mps = self.app.client_manager.coriolis.minion_pools
        minion_pool = mps.deallocate_minion_pool(args.id, force=args.force)

        return MinionPoolDetailFormatter().get_formatted_entity(minion_pool)