File: replica_executions.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 (182 lines) | stat: -rw-r--r-- 6,178 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
# Copyright (c) 2016 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 replicas.
"""
import os

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

from coriolisclient.cli import formatter


class ReplicaExecutionFormatter(formatter.EntityFormatter):

    columns = ("Replica ID",
               "ID",
               "Status",
               "Created",
               )

    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.action_id,
                obj.id,
                obj.status,
                obj.created_at,
                )
        return data


class ReplicaExecutionDetailFormatter(formatter.EntityFormatter):

    columns = ("id",
               "replica_id",
               "status",
               "created",
               "last_updated",
               "instances",
               "tasks",
               )

    def _format_instances(self, obj):
        return os.linesep.join(sorted(set([t.instance for t in obj.tasks])))

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

    def _format_task(self, task):
        d = task.to_dict()
        d["depends_on"] = ", ".join(d.get("depends_on") or [])

        progress_updates_format = "progress_updates:"
        progress_updates = self._format_progress_updates(d)
        if progress_updates:
            progress_updates_format += os.linesep
            progress_updates_format += progress_updates

        return os.linesep.join(
            ["%s: %s" % (k, d.get(k) or "") for k in
                ['id',
                 'task_type',
                 'instance',
                 'status',
                 'depends_on',
                 'exception_details']] +
            [progress_updates_format])

    def _format_tasks(self, obj):
        return ("%(ls)s%(ls)s" % {"ls": os.linesep}).join(
            [self._format_task(t) for t in obj.tasks])

    def _get_formatted_data(self, obj):
        data = (obj.id,
                obj.action_id,
                obj.status,
                obj.created_at,
                obj.updated_at,
                self._format_instances(obj),
                self._format_tasks(obj),
                )
        return data


class CreateReplicaExecution(show.ShowOne):
    """Start a replica execution"""
    def get_parser(self, prog_name):
        parser = super(CreateReplicaExecution, self).get_parser(prog_name)
        parser.add_argument('replica',
                            help='The ID of the replica to execute')
        parser.add_argument('--shutdown-instances',
                            help='Shutdown instances before executing the '
                            'replica', action='store_true',
                            default=False)
        return parser

    def take_action(self, args):
        execution = self.app.client_manager.coriolis.replica_executions.create(
            args.replica, args.shutdown_instances)

        return ReplicaExecutionDetailFormatter().get_formatted_entity(
            execution)


class ShowReplicaExecution(show.ShowOne):
    """Show a replica execution"""

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

    def take_action(self, args):
        execution = self.app.client_manager.coriolis.replica_executions.get(
            args.replica, args.id)
        return ReplicaExecutionDetailFormatter().get_formatted_entity(
            execution)


class CancelReplicaExecution(command.Command):
    """Cancel a replica execution"""

    def get_parser(self, prog_name):
        parser = super(CancelReplicaExecution, self).get_parser(prog_name)
        parser.add_argument('replica', help='The replica\'s id')
        parser.add_argument('id', help='The replica execution\'s id')
        parser.add_argument('--force',
                            help='Perform a forced termination of running '
                            'tasks', action='store_true',
                            default=False)
        return parser

    def take_action(self, args):
        self.app.client_manager.coriolis.replica_executions.cancel(
            args.replica, args.id, args.force)


class DeleteReplicaExecution(command.Command):
    """Delete a replica execution"""

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

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


class ListReplicaExecution(lister.Lister):
    """List replica executions"""

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

    def take_action(self, args):
        obj_list = self.app.client_manager.coriolis.replica_executions.list(
            args.replica)
        return ReplicaExecutionFormatter().list_objects(obj_list)