~justin-fathomdb/nova/schedule-compute-near-volume

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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# 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.

# Interactive shell based on Django:
#
# Copyright (c) 2005, the Lawrence Journal-World
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#     1. Redistributions of source code must retain the above copyright notice,
#        this list of conditions and the following disclaimer.
#
#     2. Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#
#     3. Neither the name of Django nor the names of its contributors may be
#        used to endorse or promote products derived from this software without
#        specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


"""
  CLI interface for nova management.
"""

import datetime
import gettext
import os
import re
import sys
import time

import IPy

# If ../nova/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python...
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(possible_topdir, 'nova', '__init__.py')):
    sys.path.insert(0, possible_topdir)

gettext.install('nova', unicode=1)

from nova import context
from nova import crypto
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova import quota
from nova import rpc
from nova import utils
from nova.api.ec2.cloud import ec2_id_to_id
from nova.auth import manager
from nova.cloudpipe import pipelib
from nova.compute import instance_types
from nova.db import migration

FLAGS = flags.FLAGS
flags.DECLARE('fixed_range', 'nova.network.manager')
flags.DECLARE('num_networks', 'nova.network.manager')
flags.DECLARE('network_size', 'nova.network.manager')
flags.DECLARE('vlan_start', 'nova.network.manager')
flags.DECLARE('vpn_start', 'nova.network.manager')
flags.DECLARE('fixed_range_v6', 'nova.network.manager')
flags.DEFINE_flag(flags.HelpFlag())
flags.DEFINE_flag(flags.HelpshortFlag())
flags.DEFINE_flag(flags.HelpXMLFlag())


def param2id(object_id):
    """Helper function to convert various id types to internal id.
    args: [object_id], e.g. 'vol-0000000a' or 'volume-0000000a' or '10'
    """
    if '-' in object_id:
        return ec2_id_to_id(object_id)
    else:
        return int(object_id)


class VpnCommands(object):
    """Class for managing VPNs."""

    def __init__(self):
        self.manager = manager.AuthManager()
        self.pipe = pipelib.CloudPipe()

    def list(self, project=None):
        """Print a listing of the VPN data for one or all projects.

        args: [project=all]"""
        print "%-12s\t" % 'project',
        print "%-20s\t" % 'ip:port',
        print "%-20s\t" % 'private_ip',
        print "%s" % 'state'
        if project:
            projects = [self.manager.get_project(project)]
        else:
            projects = self.manager.get_projects()
            # NOTE(vish): This hits the database a lot.  We could optimize
            #             by getting all networks in one query and all vpns
            #             in aother query, then doing lookups by project
        for project in projects:
            print "%-12s\t" % project.name,
            ipport = "%s:%s" % (project.vpn_ip, project.vpn_port)
            print "%-20s\t" % ipport,
            ctxt = context.get_admin_context()
            vpn = db.instance_get_project_vpn(ctxt, project.id)
            if vpn:
                address = None
                state = 'down'
                if vpn.get('fixed_ip', None):
                    address = vpn['fixed_ip']['address']
                if project.vpn_ip and utils.vpn_ping(project.vpn_ip,
                                                     project.vpn_port):
                    state = 'up'
                print address,
                print vpn['host'],
                print vpn['ec2_id'],
                print vpn['state_description'],
                print state
            else:
                print None

    def spawn(self):
        """Run all VPNs."""
        for p in reversed(self.manager.get_projects()):
            if not self._vpn_for(p.id):
                print 'spawning %s' % p.id
                self.pipe.launch_vpn_instance(p.id)
                time.sleep(10)

    def run(self, project_id):
        """Start the VPN for a given project."""
        self.pipe.launch_vpn_instance(project_id)

    def change(self, project_id, ip, port):
        """Change the ip and port for a vpn.

        args: project, ip, port"""
        project = self.manager.get_project(project_id)
        if not project:
            print 'No project %s' % (project_id)
            return
        admin = context.get_admin_context()
        network_ref = db.project_get_network(admin, project_id)
        db.network_update(admin,
                          network_ref['id'],
                          {'vpn_public_address': ip,
                           'vpn_public_port': int(port)})


class ShellCommands(object):
    def bpython(self):
        """Runs a bpython shell.

        Falls back to Ipython/python shell if unavailable"""
        self.run('bpython')

    def ipython(self):
        """Runs an Ipython shell.

        Falls back to Python shell if unavailable"""
        self.run('ipython')

    def python(self):
        """Runs a python shell.

        Falls back to Python shell if unavailable"""
        self.run('python')

    def run(self, shell=None):
        """Runs a Python interactive interpreter.

        args: [shell=bpython]"""
        if not shell:
            shell = 'bpython'

        if shell == 'bpython':
            try:
                import bpython
                bpython.embed()
            except ImportError:
                shell = 'ipython'
        if shell == 'ipython':
            try:
                import IPython
                # Explicitly pass an empty list as arguments, because
                # otherwise IPython would use sys.argv from this script.
                shell = IPython.Shell.IPShell(argv=[])
                shell.mainloop()
            except ImportError:
                shell = 'python'

        if shell == 'python':
            import code
            try:
                # Try activating rlcompleter, because it's handy.
                import readline
            except ImportError:
                pass
            else:
                # We don't have to wrap the following import in a 'try',
                # because we already know 'readline' was imported successfully.
                import rlcompleter
                readline.parse_and_bind("tab:complete")
            code.interact()

    def script(self, path):
        """Runs the script from the specifed path with flags set properly.
        arguments: path"""
        exec(compile(open(path).read(), path, 'exec'), locals(), globals())


class RoleCommands(object):
    """Class for managing roles."""

    def __init__(self):
        self.manager = manager.AuthManager()

    def add(self, user, role, project=None):
        """adds role to user
        if project is specified, adds project specific role
        arguments: user, role [project]"""
        self.manager.add_role(user, role, project)

    def has(self, user, role, project=None):
        """checks to see if user has role
        if project is specified, returns True if user has
        the global role and the project role
        arguments: user, role [project]"""
        print self.manager.has_role(user, role, project)

    def remove(self, user, role, project=None):
        """removes role from user
        if project is specified, removes project specific role
        arguments: user, role [project]"""
        self.manager.remove_role(user, role, project)


def _db_error(caught_exception):
    print caught_exception
    print _("The above error may show that the database has not "
            "been created.\nPlease create a database using "
            "nova-manage sync db before running this command.")
    exit(1)


class UserCommands(object):
    """Class for managing users."""

    @staticmethod
    def _print_export(user):
        """Print export variables to use with API."""
        print 'export EC2_ACCESS_KEY=%s' % user.access
        print 'export EC2_SECRET_KEY=%s' % user.secret

    def __init__(self):
        self.manager = manager.AuthManager()

    def admin(self, name, access=None, secret=None):
        """creates a new admin and prints exports
        arguments: name [access] [secret]"""
        try:
            user = self.manager.create_user(name, access, secret, True)
        except exception.DBError, e:
            _db_error(e)
        self._print_export(user)

    def create(self, name, access=None, secret=None):
        """creates a new user and prints exports
        arguments: name [access] [secret]"""
        try:
            user = self.manager.create_user(name, access, secret, False)
        except exception.DBError, e:
            _db_error(e)
        self._print_export(user)

    def delete(self, name):
        """deletes an existing user
        arguments: name"""
        self.manager.delete_user(name)

    def exports(self, name):
        """prints access and secrets for user in export format
        arguments: name"""
        user = self.manager.get_user(name)
        if user:
            self._print_export(user)
        else:
            print "User %s doesn't exist" % name

    def list(self):
        """lists all users
        arguments: <none>"""
        for user in self.manager.get_users():
            print user.name

    def modify(self, name, access_key, secret_key, is_admin):
        """update a users keys & admin flag
        arguments: accesskey secretkey admin
        leave any field blank to ignore it, admin should be 'T', 'F', or blank
        """
        if not is_admin:
            is_admin = None
        elif is_admin.upper()[0] == 'T':
            is_admin = True
        else:
            is_admin = False
        self.manager.modify_user(name, access_key, secret_key, is_admin)

    def revoke(self, user_id, project_id=None):
        """revoke certs for a user
        arguments: user_id [project_id]"""
        if project_id:
            crypto.revoke_certs_by_user_and_project(user_id, project_id)
        else:
            crypto.revoke_certs_by_user(user_id)


class ProjectCommands(object):
    """Class for managing projects."""

    def __init__(self):
        self.manager = manager.AuthManager()

    def add(self, project_id, user_id):
        """Adds user to project
        arguments: project_id user_id"""
        self.manager.add_to_project(user_id, project_id)

    def create(self, name, project_manager, description=None):
        """Creates a new project
        arguments: name project_manager [description]"""
        self.manager.create_project(name, project_manager, description)

    def modify(self, name, project_manager, description=None):
        """Modifies a project
        arguments: name project_manager [description]"""
        self.manager.modify_project(name, project_manager, description)

    def delete(self, name):
        """Deletes an existing project
        arguments: name"""
        self.manager.delete_project(name)

    def environment(self, project_id, user_id, filename='novarc'):
        """Exports environment variables to an sourcable file
        arguments: project_id user_id [filename='novarc]"""
        rc = self.manager.get_environment_rc(user_id, project_id)
        with open(filename, 'w') as f:
            f.write(rc)

    def list(self):
        """Lists all projects
        arguments: <none>"""
        for project in self.manager.get_projects():
            print project.name

    def quota(self, project_id, key=None, value=None):
        """Set or display quotas for project
        arguments: project_id [key] [value]"""
        ctxt = context.get_admin_context()
        if key:
            quo = {'project_id': project_id, key: value}
            try:
                db.quota_update(ctxt, project_id, quo)
            except exception.NotFound:
                db.quota_create(ctxt, quo)
        project_quota = quota.get_quota(ctxt, project_id)
        for key, value in project_quota.iteritems():
            print '%s: %s' % (key, value)

    def remove(self, project_id, user_id):
        """Removes user from project
        arguments: project_id user_id"""
        self.manager.remove_from_project(user_id, project_id)

    def scrub(self, project_id):
        """Deletes data associated with project
        arguments: project_id"""
        ctxt = context.get_admin_context()
        network_ref = db.project_get_network(ctxt, project_id)
        db.network_disassociate(ctxt, network_ref['id'])
        groups = db.security_group_get_by_project(ctxt, project_id)
        for group in groups:
            db.security_group_destroy(ctxt, group['id'])

    def zipfile(self, project_id, user_id, filename='nova.zip'):
        """Exports credentials for project to a zip file
        arguments: project_id user_id [filename='nova.zip]"""
        try:
            zip_file = self.manager.get_credentials(user_id, project_id)
            with open(filename, 'w') as f:
                f.write(zip_file)
        except db.api.NoMoreNetworks:
            print _('No more networks available. If this is a new '
                    'installation, you need\nto call something like this:\n\n'
                    '    nova-manage network create 10.0.0.0/8 10 64\n\n')
        except exception.ProcessExecutionError, e:
            print e
            print _("The above error may show that the certificate db has not "
                    "been created.\nPlease create a database by running a "
                    "nova-api server on this host.")


class FixedIpCommands(object):
    """Class for managing fixed ip."""

    def list(self, host=None):
        """Lists all fixed ips (optionally by host) arguments: [host]"""
        ctxt = context.get_admin_context()
        if host == None:
            fixed_ips = db.fixed_ip_get_all(ctxt)
        else:
            fixed_ips = db.fixed_ip_get_all_by_host(ctxt, host)

        print "%-18s\t%-15s\t%-17s\t%-15s\t%s" % (_('network'),
                                                  _('IP address'),
                                                  _('MAC address'),
                                                  _('hostname'),
                                                  _('host'))
        for fixed_ip in fixed_ips:
            hostname = None
            host = None
            mac_address = None
            if fixed_ip['instance']:
                instance = fixed_ip['instance']
                hostname = instance['hostname']
                host = instance['host']
                mac_address = instance['mac_address']
            print "%-18s\t%-15s\t%-17s\t%-15s\t%s" % (
                                  fixed_ip['network']['cidr'],
                                  fixed_ip['address'],
                                  mac_address, hostname, host)


class FloatingIpCommands(object):
    """Class for managing floating ip."""

    def create(self, host, range):
        """Creates floating ips for host by range
        arguments: host ip_range"""
        for address in IPy.IP(range):
            db.floating_ip_create(context.get_admin_context(),
                                  {'address': str(address),
                                   'host': host})

    def delete(self, ip_range):
        """Deletes floating ips by range
        arguments: range"""
        for address in IPy.IP(ip_range):
            db.floating_ip_destroy(context.get_admin_context(),
                                   str(address))

    def list(self, host=None):
        """Lists all floating ips (optionally by host)
        arguments: [host]"""
        ctxt = context.get_admin_context()
        if host == None:
            floating_ips = db.floating_ip_get_all(ctxt)
        else:
            floating_ips = db.floating_ip_get_all_by_host(ctxt, host)
        for floating_ip in floating_ips:
            instance = None
            if floating_ip['fixed_ip']:
                instance = floating_ip['fixed_ip']['instance']['ec2_id']
            print "%s\t%s\t%s" % (floating_ip['host'],
                                  floating_ip['address'],
                                  instance)


class NetworkCommands(object):
    """Class for managing networks."""

    def create(self, fixed_range=None, num_networks=None,
               network_size=None, vlan_start=None,
               vpn_start=None, fixed_range_v6=None, label='public'):
        """Creates fixed ips for host by range
        arguments: [fixed_range=FLAG], [num_networks=FLAG],
                   [network_size=FLAG], [vlan_start=FLAG],
                   [vpn_start=FLAG], [fixed_range_v6=FLAG]"""
        if not fixed_range:
            fixed_range = FLAGS.fixed_range
        if not num_networks:
            num_networks = FLAGS.num_networks
        if not network_size:
            network_size = FLAGS.network_size
        if not vlan_start:
            vlan_start = FLAGS.vlan_start
        if not vpn_start:
            vpn_start = FLAGS.vpn_start
        if not fixed_range_v6:
            fixed_range_v6 = FLAGS.fixed_range_v6
        net_manager = utils.import_object(FLAGS.network_manager)
        net_manager.create_networks(context.get_admin_context(),
                                    cidr=fixed_range,
                                    num_networks=int(num_networks),
                                    network_size=int(network_size),
                                    vlan_start=int(vlan_start),
                                    vpn_start=int(vpn_start),
                                    cidr_v6=fixed_range_v6,
                                    label=label)

    def list(self):
        """List all created networks"""
        print "%-18s\t%-15s\t%-15s\t%-15s" % (_('network'),
                                              _('netmask'),
                                              _('start address'),
                                              'DNS')
        for network in db.network_get_all(context.get_admin_context()):
            print "%-18s\t%-15s\t%-15s\t%-15s" % (network.cidr,
                                network.netmask,
                                network.dhcp_start,
                                network.dns)


class ServiceCommands(object):
    """Enable and disable running services"""

    def list(self, host=None, service=None):
        """Show a list of all running services. Filter by host & service name.
        args: [host] [service]"""
        ctxt = context.get_admin_context()
        now = datetime.datetime.utcnow()
        services = db.service_get_all(ctxt) + db.service_get_all(ctxt, True)
        if host:
            services = [s for s in services if s['host'] == host]
        if service:
            services = [s for s in services if s['binary'] == service]
        for svc in services:
            delta = now - (svc['updated_at'] or svc['created_at'])
            alive = (delta.seconds <= 15)
            art = (alive and ":-)") or "XXX"
            active = 'enabled'
            if svc['disabled']:
                active = 'disabled'
            print "%-10s %-10s %-8s %s %s" % (svc['host'], svc['binary'],
                                              active, art,
                                              svc['updated_at'])

    def enable(self, host, service):
        """Enable scheduling for a service
        args: host service"""
        ctxt = context.get_admin_context()
        svc = db.service_get_by_args(ctxt, host, service)
        if not svc:
            print "Unable to find service"
            return
        db.service_update(ctxt, svc['id'], {'disabled': False})

    def disable(self, host, service):
        """Disable scheduling for a service
        args: host service"""
        ctxt = context.get_admin_context()
        svc = db.service_get_by_args(ctxt, host, service)
        if not svc:
            print "Unable to find service"
            return
        db.service_update(ctxt, svc['id'], {'disabled': True})


class LogCommands(object):
    def request(self, request_id, logfile='/var/log/nova.log'):
        """Show all fields in the log for the given request.  Assumes you
        haven't changed the log format too much.
        ARGS: request_id [logfile]"""
        lines = utils.execute("cat %s | grep '\[%s '" % (logfile, request_id))
        print re.sub('#012', "\n", "\n".join(lines))


class DbCommands(object):
    """Class for managing the database."""

    def __init__(self):
        pass

    def sync(self, version=None):
        """Sync the database up to the most recent version."""
        return migration.db_sync(version)

    def version(self):
        """Print the current database version."""
        print migration.db_version()


class VolumeCommands(object):
    """Methods for dealing with a cloud in an odd state"""

    def delete(self, volume_id):
        """Delete a volume, bypassing the check that it
        must be available.
        args: volume_id_id"""
        ctxt = context.get_admin_context()
        volume = db.volume_get(ctxt, param2id(volume_id))
        host = volume['host']

        if not host:
            print "Volume not yet assigned to host."
            print "Deleting volume from database and skipping rpc."
            db.volume_destroy(ctxt, param2id(volume_id))
            return

        if volume['status'] == 'in-use':
            print "Volume is in-use."
            print "Detach volume from instance and then try again."
            return

        rpc.cast(ctxt,
                 db.queue_get_for(ctxt, FLAGS.volume_topic, host),
                 {"method": "delete_volume",
                  "args": {"volume_id": volume['id']}})

    def reattach(self, volume_id):
        """Re-attach a volume that has previously been attached
        to an instance.  Typically called after a compute host
        has been rebooted.
        args: volume_id_id"""
        ctxt = context.get_admin_context()
        volume = db.volume_get(ctxt, param2id(volume_id))
        if not volume['instance_id']:
            print "volume is not attached to an instance"
            return
        instance = db.instance_get(ctxt, volume['instance_id'])
        host = instance['host']
        rpc.cast(ctxt,
                 db.queue_get_for(ctxt, FLAGS.compute_topic, host),
                 {"method": "attach_volume",
                  "args": {"instance_id": instance['id'],
                           "volume_id": volume['id'],
                           "mountpoint": volume['mountpoint']}})


class InstanceTypeCommands(object):
    """Class for managing instance types / flavors."""

    def _print_instance_types(self, n, val):
        deleted = ('', ', inactive')[val["deleted"] == 1]
        print ("%s: Memory: %sMB, VCPUS: %s, Storage: %sGB, FlavorID: %s, "
            "Swap: %sGB, RXTX Quota: %sGB, RXTX Cap: %sMB%s") % (
            n, val["memory_mb"], val["vcpus"], val["local_gb"],
            val["flavorid"], val["swap"], val["rxtx_quota"],
            val["rxtx_cap"], deleted)

    def create(self, name, memory, vcpus, local_gb, flavorid,
               swap=0, rxtx_quota=0, rxtx_cap=0):
        """Creates instance types / flavors
           arguments: name memory vcpus local_gb flavorid [swap] [rxtx_quota]
                      [rxtx_cap]
        """
        try:
            instance_types.create(name, memory, vcpus, local_gb,
                                  flavorid, swap, rxtx_quota, rxtx_cap)
        except exception.InvalidInputException:
            print "Must supply valid parameters to create instance type"
            print e
            sys.exit(1)
        except exception.DBError, e:
            print "DB Error: %s" % e
            sys.exit(2)
        except:
            print "Unknown error"
            sys.exit(3)
        else:
            print "%s created" % name

    def delete(self, name, purge=None):
        """Marks instance types / flavors as deleted
        arguments: name"""
        try:
            if purge == "--purge":
                instance_types.purge(name)
                verb = "purged"
            else:
                instance_types.destroy(name)
                verb = "deleted"
        except exception.ApiError:
            print "Valid instance type name is required"
            sys.exit(1)
        except exception.DBError, e:
            print "DB Error: %s" % e
            sys.exit(2)
        except:
            sys.exit(3)
        else:
            print "%s %s" % (name, verb)

    def list(self, name=None):
        """Lists all active or specific instance types / flavors
        arguments: [name]"""
        try:
            if name == None:
                inst_types = instance_types.get_all_types()
            elif name == "--all":
                inst_types = instance_types.get_all_types(1)
            else:
                inst_types = instance_types.get_instance_type(name)
        except exception.DBError, e:
            _db_error(e)
        if isinstance(inst_types.values()[0], dict):
            for k, v in inst_types.iteritems():
                self._print_instance_types(k, v)
        else:
            self._print_instance_types(name, inst_types)


CATEGORIES = [
    ('user', UserCommands),
    ('project', ProjectCommands),
    ('role', RoleCommands),
    ('shell', ShellCommands),
    ('vpn', VpnCommands),
    ('fixed', FixedIpCommands),
    ('floating', FloatingIpCommands),
    ('network', NetworkCommands),
    ('service', ServiceCommands),
    ('log', LogCommands),
    ('db', DbCommands),
    ('volume', VolumeCommands),
    ('instance_type', InstanceTypeCommands),
    ('flavor', InstanceTypeCommands)]


def lazy_match(name, key_value_tuples):
    """Finds all objects that have a key that case insensitively contains
    [name] key_value_tuples is a list of tuples of the form (key, value)
    returns a list of tuples of the form (key, value)"""
    result = []
    for (k, v) in key_value_tuples:
        if k.lower().find(name.lower()) == 0:
            result.append((k, v))
    if len(result) == 0:
        print "%s does not match any options:" % name
        for k, _v in key_value_tuples:
            print "\t%s" % k
        sys.exit(2)
    if len(result) > 1:
        print "%s matched multiple options:" % name
        for k, _v in result:
            print "\t%s" % k
        sys.exit(2)
    return result


def methods_of(obj):
    """Get all callable methods of an object that don't start with underscore
    returns a list of tuples of the form (method_name, method)"""
    result = []
    for i in dir(obj):
        if callable(getattr(obj, i)) and not i.startswith('_'):
            result.append((i, getattr(obj, i)))
    return result


def main():
    """Parse options and call the appropriate class/method."""
    utils.default_flagfile()
    argv = FLAGS(sys.argv)
    logging.setup()

    script_name = argv.pop(0)
    if len(argv) < 1:
        print script_name + " category action [<args>]"
        print "Available categories:"
        for k, _ in CATEGORIES:
            print "\t%s" % k
        sys.exit(2)
    category = argv.pop(0)
    matches = lazy_match(category, CATEGORIES)
    # instantiate the command group object
    category, fn = matches[0]
    command_object = fn()
    actions = methods_of(command_object)
    if len(argv) < 1:
        print script_name + " category action [<args>]"
        print "Available actions for %s category:" % category
        for k, _v in actions:
            print "\t%s" % k
        sys.exit(2)
    action = argv.pop(0)
    matches = lazy_match(action, actions)
    action, fn = matches[0]
    # call the action with the remaining arguments
    try:
        fn(*argv)
        sys.exit(0)
    except TypeError:
        print "Possible wrong number of arguments supplied"
        print "%s %s: %s" % (category, action, fn.__doc__)
        raise

if __name__ == '__main__':
    main()