Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.4 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

    
35
import ipaddr
36
from datetime import datetime
37

    
38
from django.utils.timesince import timesince, timeuntil
39

    
40
from django.core.management import CommandError
41
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor
42
from synnefo.api.util import get_image as backend_get_image
43
from synnefo.api.faults import ItemNotFound
44

    
45
from synnefo.api.util import validate_network_size
46
from synnefo.settings import MAX_CIDR_BLOCK
47

    
48

    
49
def format_bool(b):
50
    return 'YES' if b else 'NO'
51

    
52

    
53
def format_date(d):
54
    if not d:
55
        return ''
56

    
57
    if d < datetime.now():
58
        return timesince(d) + ' ago'
59
    else:
60
        return 'in ' + timeuntil(d)
61

    
62

    
63
def format_vm_state(vm):
64
    if vm.operstate == "BUILD":
65
        return "BUILD(" + str(vm.buildpercentage) + "%)"
66
    else:
67
        return vm.operstate
68

    
69

    
70
def validate_network_info(options):
71
    subnet = options['subnet']
72
    gateway = options['gateway']
73
    subnet6 = options['subnet6']
74
    gateway6 = options['gateway6']
75

    
76
    try:
77
        net = ipaddr.IPv4Network(subnet)
78
        prefix = net.prefixlen
79
        if not validate_network_size(prefix):
80
            raise CommandError("Unsupport network mask %d."
81
                               " Must be in range (%s,29] "
82
                               % (prefix, MAX_CIDR_BLOCK))
83
    except ValueError:
84
        raise CommandError('Malformed subnet')
85
    try:
86
        gateway and ipaddr.IPv4Address(gateway) or None
87
    except ValueError:
88
        raise CommandError('Malformed gateway')
89

    
90
    try:
91
        subnet6 and ipaddr.IPv6Network(subnet6) or None
92
    except ValueError:
93
        raise CommandError('Malformed subnet6')
94

    
95
    try:
96
        gateway6 and ipaddr.IPv6Address(gateway6) or None
97
    except ValueError:
98
        raise CommandError('Malformed gateway6')
99

    
100
    return subnet, gateway, subnet6, gateway6
101

    
102

    
103
def get_backend(backend_id):
104
    try:
105
        backend_id = int(backend_id)
106
        return Backend.objects.get(id=backend_id)
107
    except ValueError:
108
        raise CommandError("Invalid Backend ID: %s" % backend_id)
109
    except Backend.DoesNotExist:
110
        raise CommandError("Backend with ID %s not found in DB. "
111
                           " Use snf-manage backend-list to find"
112
                           " out available backend IDs." % backend_id)
113

    
114

    
115
def get_image(image_id, user_id):
116
    if image_id:
117
        try:
118
            return backend_get_image(image_id, user_id)
119
        except ItemNotFound:
120
            raise CommandError("Image with ID %s not found."
121
                               " Use snf-manage image-list to find"
122
                               " out available image IDs." % image_id)
123
    else:
124
        raise CommandError("image-id is mandatory")
125

    
126

    
127
def get_vm(server_id):
128
    try:
129
        server_id = int(server_id)
130
        return VirtualMachine.objects.get(id=server_id)
131
    except ValueError:
132
        raise CommandError("Invalid server ID: %s", server_id)
133
    except VirtualMachine.DoesNotExist:
134
        raise CommandError("Server with ID %s not found in DB."
135
                           " Use snf-manage server-list to find out"
136
                           " available server IDs." % server_id)
137

    
138

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

    
150

    
151
def get_flavor(flavor_id):
152
    try:
153
        flavor_id = int(flavor_id)
154
        return Flavor.objects.get(id=flavor_id)
155
    except ValueError:
156
        raise CommandError("Invalid flavor ID: %s", flavor_id)
157
    except Flavor.DoesNotExist:
158
        raise CommandError("Flavor with ID %s not found in DB."
159
                           " Use snf-manage flavor-list to find out"
160
                           " available flavor IDs." % flavor_id)