summaryrefslogtreecommitdiff
path: root/trove/cmd/manage.py
blob: c6e3c6757dfdbafebb163a74b84742f35fbdb1ec (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# Copyright 2011 OpenStack Foundation
# All Rights Reserved.
#
#    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 inspect
import sys

from oslo_log import log as logging

from trove.common import cfg
from trove.common import exception
from trove.common.i18n import _
from trove.common import utils
from trove.configuration import models as config_models
from trove.datastore import models as datastore_models
from trove.db import get_db_api


CONF = cfg.CONF


class Commands(object):

    def __init__(self):
        self.db_api = get_db_api()

    def db_sync(self, repo_path=None):
        self.db_api.db_sync(CONF, repo_path=repo_path)

    def db_upgrade(self, version=None, repo_path=None):
        self.db_api.db_upgrade(CONF, version, repo_path=repo_path)

    def db_downgrade(self, version, repo_path=None):
        raise SystemExit(_("Database downgrade is no longer supported."))

    def execute(self):
        exec_method = getattr(self, CONF.action.name)
        args = inspect.getargspec(exec_method)
        args.args.remove('self')
        kwargs = {}
        for arg in args.args:
            kwargs[arg] = getattr(CONF.action, arg)
        exec_method(**kwargs)

    def datastore_update(self, datastore_name, default_version):
        try:
            datastore_models.update_datastore(datastore_name,
                                              default_version)
            print("Datastore '%s' updated." % datastore_name)
        except exception.DatastoreVersionNotFound as e:
            print(e)

    def datastore_version_update(self, datastore, version_name, manager,
                                 image_id, packages, active, image_tags=None,
                                 version=None):
        try:
            datastore_models.update_datastore_version(datastore,
                                                      version_name,
                                                      manager,
                                                      image_id,
                                                      image_tags,
                                                      packages, active,
                                                      version=version)
            print("Datastore version '%s(%s)' updated." %
                  (version_name, version or version_name))
        except exception.DatastoreNotFound as e:
            print(e)

    def db_recreate(self, repo_path):
        """Drops the database and recreates it."""
        self.db_api.drop_db(CONF)
        self.db_sync(repo_path)

    def db_load_datastore_config_parameters(self,
                                            datastore,
                                            datastore_version_name,
                                            config_file_location,
                                            version=None):
        print("Loading config parameters for datastore (%s) version (%s)"
              % (datastore, datastore_version_name))
        config_models.load_datastore_configuration_parameters(
            datastore, datastore_version_name, config_file_location,
            version_number=version)

    def db_remove_datastore_config_parameters(self, datastore,
                                              datastore_version_name,
                                              version=None):
        print("Removing config parameters for datastore (%s) version (%s)"
              % (datastore, datastore_version_name))
        config_models.remove_datastore_configuration_parameters(
            datastore, datastore_version_name, version_number=version)

    def datastore_version_flavor_add(self, datastore_name,
                                     datastore_version_name, flavor_ids,
                                     version=None):
        """Adds flavors for a given datastore version id."""
        dsmetadata = datastore_models.DatastoreVersionMetadata
        try:
            datastore_version_id = dsmetadata.datastore_version_find(
                datastore_name,
                datastore_version_name,
                version_number=version)

            dsmetadata.add_datastore_version_flavor_association(
                datastore_version_id, flavor_ids.split(","))
            print("Added flavors '%s' to the '%s' '%s'."
                  % (flavor_ids, datastore_name, datastore_version_name))
        except Exception as e:
            print(e)

    def datastore_version_flavor_delete(self, datastore_name,
                                        datastore_version_name, flavor_id,
                                        version=None):
        """Deletes a flavor's association with a given datastore."""
        try:
            dsmetadata = datastore_models.DatastoreVersionMetadata

            datastore_version_id = dsmetadata.datastore_version_find(
                datastore_name,
                datastore_version_name,
                version_number=version)

            dsmetadata.delete_datastore_version_flavor_association(
                datastore_version_id, flavor_id)
            print("Deleted flavor '%s' from '%s' '%s'."
                  % (flavor_id, datastore_name, datastore_version_name))
        except Exception as e:
            print(e)

    def datastore_version_volume_type_add(self, datastore_name,
                                          datastore_version_name,
                                          volume_type_ids, version=None):
        """Adds volume type assiciation for a given datastore version id."""
        try:
            dsmetadata = datastore_models.DatastoreVersionMetadata

            datastore_version_id = dsmetadata.datastore_version_find(
                datastore_name,
                datastore_version_name,
                version_number=version)

            dsmetadata.add_datastore_version_volume_type_association(
                datastore_version_id,
                volume_type_ids.split(","))
            print("Added volume type '%s' to the '%s' '%s'."
                  % (volume_type_ids, datastore_name, datastore_version_name))
        except Exception as e:
            print(e)

    def datastore_version_volume_type_delete(self, datastore_name,
                                             datastore_version_name,
                                             volume_type_id, version=None):
        """Deletes a volume type association with a given datastore."""
        try:
            dsmetadata = datastore_models.DatastoreVersionMetadata

            datastore_version_id = dsmetadata.datastore_version_find(
                datastore_name,
                datastore_version_name,
                version_number=version)

            dsmetadata.delete_datastore_version_volume_type_association(
                datastore_version_id, volume_type_id)
            print("Deleted volume type '%s' from '%s' '%s'."
                  % (volume_type_id, datastore_name, datastore_version_name))
        except Exception as e:
            print(e)

    def datastore_version_volume_type_list(self, datastore_name,
                                           datastore_version_name,
                                           version=None):
        """Lists volume type association with a given datastore."""
        try:
            dsmetadata = datastore_models.DatastoreVersionMetadata

            datastore_version_id = dsmetadata.datastore_version_find(
                datastore_name,
                datastore_version_name,
                version_number=version)

            vtlist = dsmetadata. \
                list_datastore_version_volume_type_associations(
                    datastore_version_id)
            if vtlist.count() > 0:
                for volume_type in vtlist:
                    print("Datastore: %s, Version: %s, Volume Type: %s" %
                          (datastore_name, datastore_version_name,
                           volume_type.value))
            else:
                print("No Volume Type Associations found for Datastore: %s, "
                      "Version: %s." %
                      (datastore_name, datastore_version_name))
        except Exception as e:
            print(e)

    def params_of(self, command_name):
        if Commands.has(command_name):
            return utils.MethodInspector(getattr(self, command_name))


def main():

    def actions(subparser):
        repo_path_help = 'SQLAlchemy Migrate repository path.'

        parser = subparser.add_parser(
            'db_sync', description='Populate the database structure')
        parser.add_argument('--repo_path', help=repo_path_help)

        parser = subparser.add_parser(
            'db_upgrade', description='Upgrade the database to the '
            'specified version.')
        parser.add_argument(
            '--version', help='Target version. Defaults to the '
            'latest version.')
        parser.add_argument('--repo_path', help=repo_path_help)

        parser = subparser.add_parser(
            'datastore_update', description='Add or update a datastore. '
            'If the datastore already exists, the default version will be '
            'updated.')
        parser.add_argument(
            'datastore_name', help='Name of the datastore.')
        parser.add_argument(
            'default_version', help='Name or ID of an existing datastore '
            'version to set as the default. When adding a new datastore, use '
            'an empty string.')

        parser = subparser.add_parser(
            'datastore_version_update', description='Add or update a '
            'datastore version. If the datastore version already exists, all '
            'values except the datastore name and version will be updated.')
        parser.add_argument('datastore', help='Name of the datastore.')
        parser.add_argument(
            'version_name', help='Name of the datastore version.')
        parser.add_argument(
            'manager', help='Name of the manager that will administer the '
            'datastore version.')
        parser.add_argument(
            'image_id',
            help='ID of the image used to create an instance of '
                 'the datastore version.')
        parser.add_argument(
            'packages', help='Packages required by the datastore version that '
            'are installed on the guest image.')
        parser.add_argument(
            'active', type=int,
            help='Whether the datastore version is active or not. '
            'Accepted values are 0 and 1.')
        parser.add_argument(
            '--image-tags',
            help='List of image tags separated by comma used for getting '
                 'guest image.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <version_name> as default value.')

        parser = subparser.add_parser(
            'db_recreate', description='Drop the database and recreate it.')
        parser.add_argument('--repo_path', help=repo_path_help)

        parser = subparser.add_parser(
            'db_load_datastore_config_parameters',
            description='Loads configuration group parameter validation rules '
            'for a datastore version into the database.')
        parser.add_argument(
            'datastore',
            help='Name of the datastore.')
        parser.add_argument(
            'datastore_version_name',
            help='Name of the datastore version.')
        parser.add_argument(
            'config_file_location',
            help='Fully qualified file path to the configuration group '
            'parameter validation rules.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

        parser = subparser.add_parser(
            'db_remove_datastore_config_parameters',
            description='Remove configuration group parameter validation '
                        'rules for a datastore version from the database.')
        parser.add_argument(
            'datastore',
            help='Name of the datastore.')
        parser.add_argument(
            'datastore_version_name',
            help='Name of the datastore version.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

        parser = subparser.add_parser(
            'datastore_version_flavor_add',
            help='Adds flavor association to a given datastore and datastore '
                 'version.')
        parser.add_argument('datastore_name', help='Name of the datastore.')
        parser.add_argument('datastore_version_name', help='Name of the '
                            'datastore version.')
        parser.add_argument('flavor_ids', help='Comma separated list of '
                            'flavor ids.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

        parser = subparser.add_parser(
            'datastore_version_flavor_delete',
            help='Deletes a flavor associated with a given datastore and '
                 'datastore version.')
        parser.add_argument('datastore_name', help='Name of the datastore.')
        parser.add_argument('datastore_version_name', help='Name of the '
                            'datastore version.')
        parser.add_argument('flavor_id', help='The flavor to be deleted for '
                            'a given datastore and datastore version.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

        parser = subparser.add_parser(
            'datastore_version_volume_type_add',
            help='Adds volume_type association to a given datastore and '
                 'datastore version.')
        parser.add_argument('datastore_name', help='Name of the datastore.')
        parser.add_argument('datastore_version_name', help='Name of the '
                            'datastore version.')
        parser.add_argument('volume_type_ids', help='Comma separated list of '
                            'volume_type ids.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

        parser = subparser.add_parser(
            'datastore_version_volume_type_delete',
            help='Deletes a volume_type '
                 'associated with a given datastore and datastore version.')
        parser.add_argument('datastore_name', help='Name of the datastore.')
        parser.add_argument('datastore_version_name', help='Name of the '
                            'datastore version.')
        parser.add_argument('volume_type_id', help='The volume_type to be '
                            'deleted for a given datastore and datastore '
                            'version.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

        parser = subparser.add_parser(
            'datastore_version_volume_type_list',
            help='Lists the volume_types '
                 'associated with a given datastore and datastore version.')
        parser.add_argument('datastore_name', help='Name of the datastore.')
        parser.add_argument('datastore_version_name', help='Name of the '
                            'datastore version.')
        parser.add_argument(
            '--version',
            help='The version number of the datastore version, e.g. 5.7.30. '
                 'If not specified, use <datastore_version_name> as default '
                 'value.')

    cfg.custom_parser('action', actions)
    cfg.parse_args(sys.argv)

    try:
        logging.setup(CONF, None)

        Commands().execute()
        sys.exit(0)
    except TypeError as e:
        print(_("Possible wrong number of arguments supplied %s.") % e)
        sys.exit(2)
    except Exception:
        print(_("Command failed, please check log for more info."))
        raise