Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / common.py @ bd40abfa

History | View | Annotate | Download (5.8 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from django.core.management import CommandError
35
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor
36

    
37
from snf_django.lib.api import faults
38
from synnefo.api.util import get_image as backend_get_image
39
from synnefo.api.util import validate_network_params
40
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient
41
from synnefo.logic.utils import (id_from_instance_name,
42
                                 id_from_network_name)
43

    
44
import logging
45
log = logging.getLogger(__name__)
46

    
47

    
48
def format_vm_state(vm):
49
    if vm.operstate == "BUILD":
50
        return "BUILD(" + str(vm.buildpercentage) + "%)"
51
    else:
52
        return vm.operstate
53

    
54

    
55
def validate_network_info(options):
56
    subnet = options['subnet']
57
    gateway = options['gateway']
58
    subnet6 = options['subnet6']
59
    gateway6 = options['gateway6']
60

    
61
    try:
62
        validate_network_params(subnet, gateway)
63
    except (faults.BadRequest, faults.OverLimit) as e:
64
        raise CommandError(e)
65

    
66
    return subnet, gateway, subnet6, gateway6
67

    
68

    
69
def get_backend(backend_id):
70
    try:
71
        backend_id = int(backend_id)
72
        return Backend.objects.get(id=backend_id)
73
    except ValueError:
74
        raise CommandError("Invalid Backend ID: %s" % backend_id)
75
    except Backend.DoesNotExist:
76
        raise CommandError("Backend with ID %s not found in DB. "
77
                           " Use snf-manage backend-list to find"
78
                           " out available backend IDs." % backend_id)
79

    
80

    
81
def get_image(image_id, user_id):
82
    if image_id:
83
        try:
84
            return backend_get_image(image_id, user_id)
85
        except faults.ItemNotFound:
86
            raise CommandError("Image with ID %s not found."
87
                               " Use snf-manage image-list to find"
88
                               " out available image IDs." % image_id)
89
    else:
90
        raise CommandError("image-id is mandatory")
91

    
92

    
93
def get_vm(server_id):
94
    """Get a VirtualMachine object by its ID.
95

96
    @type server_id: int or string
97
    @param server_id: The server's DB id or the Ganeti name
98

99
    """
100
    try:
101
        server_id = int(server_id)
102
    except (ValueError, TypeError):
103
        try:
104
            server_id = id_from_instance_name(server_id)
105
        except VirtualMachine.InvalidBackendIdError:
106
            raise CommandError("Invalid server ID: %s" % server_id)
107

    
108
    try:
109
        return VirtualMachine.objects.get(id=server_id)
110
    except VirtualMachine.DoesNotExist:
111
        raise CommandError("Server with ID %s not found in DB."
112
                           " Use snf-manage server-list to find out"
113
                           " available server IDs." % server_id)
114

    
115

    
116
def get_network(network_id):
117
    """Get a Network object by its ID.
118

119
    @type network_id: int or string
120
    @param network_id: The networks DB id or the Ganeti name
121

122
    """
123

    
124
    try:
125
        network_id = int(network_id)
126
    except (ValueError, TypeError):
127
        try:
128
            network_id = id_from_network_name(network_id)
129
        except Network.InvalidBackendIdError:
130
            raise CommandError("Invalid network ID: %s" % network_id)
131

    
132
    try:
133
        return Network.objects.get(id=network_id)
134
    except Network.DoesNotExist:
135
        raise CommandError("Network with ID %s not found in DB."
136
                           " Use snf-manage network-list to find out"
137
                           " available network IDs." % network_id)
138

    
139

    
140
def get_flavor(flavor_id):
141
    try:
142
        flavor_id = int(flavor_id)
143
        return Flavor.objects.get(id=flavor_id)
144
    except ValueError:
145
        raise CommandError("Invalid flavor ID: %s", flavor_id)
146
    except Flavor.DoesNotExist:
147
        raise CommandError("Flavor with ID %s not found in DB."
148
                           " Use snf-manage flavor-list to find out"
149
                           " available flavor IDs." % flavor_id)
150

    
151

    
152
def check_backend_credentials(clustername, port, username, password):
153
    try:
154
        client = GanetiRapiClient(clustername, port, username, password)
155
        # This command will raise an exception if there is no
156
        # write-access
157
        client.ModifyCluster()
158
    except GanetiApiError as e:
159
        raise CommandError(e)
160

    
161
    info = client.GetInfo()
162
    info_name = info['name']
163
    if info_name != clustername:
164
        raise CommandError("Invalid clustername value. Please use the"
165
                           " Ganeti Cluster name: %s" % info_name)
166

    
167

    
168
class Omit(object):
169
    pass