Revision f66d8b04

b/snf-cyclades-app/synnefo/api/management/commands/_common.py
48 48
        return timesince(d) + ' ago'
49 49
    else:
50 50
        return 'in ' + timeuntil(d)
51

  
52

  
53
def format_vm_state(vm):
54
    if vm.operstate == "BUILD":
55
        return "BUILD(" + str(vm.buildpercentage) + "%)"
56
    else:
57
        return vm.operstate
b/snf-cyclades-app/synnefo/api/management/commands/server-inspect.py
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
from datetime import datetime
36
from optparse import make_option
37
from django.core.management.base import BaseCommand, CommandError
38

  
39
from synnefo.api.util import get_image
40
from synnefo.lib.utils import merge_time
41
from synnefo.db.models import VirtualMachine
42
from synnefo.logic.rapi import GanetiApiError
43
from ._common import format_vm_state
44

  
45

  
46
# Fields to print from a gnt-instance info
47
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
48
                          'pnode', 'snode', 'network_port', 'disk_template',
49
                          'disk_usage', 'oper_ram', 'oper_vcpus', 'mtime',
50
                          'nic.ips', 'nic.macs', 'nic.networks', 'nic.modes')
51

  
52
# Fields to print from a gnt-job info
53
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
54
                     'oplog', 'start_ts', 'end_ts')
55

  
56

  
57
class Command(BaseCommand):
58
    help = "Inspect a server on DB and Ganeti"
59

  
60
    option_list = BaseCommand.option_list + (
61
        make_option('--jobs', action='store_true',
62
                    dest='jobs', default=False,
63
                    help="Show non-archived jobs concerning server."
64
            ),
65
    )
66

  
67
    def handle(self, *args, **options):
68
        if len(args) != 1:
69
            raise CommandError("Please provide a server ID")
70

  
71
        try:
72
            vm_id = int(args[0])
73
        except ValueError:
74
            raise CommandError("Invalid server ID")
75

  
76
        try:
77
            vm = VirtualMachine.objects.get(id=vm_id)
78
        except VirtualMachine.DoesNotExist:
79
            raise CommandError("Server not found in DB")
80

  
81
        try:
82
            image = get_image(vm.imageid, vm.userid)['name']
83
        except:
84
            image = vm.imageid
85

  
86
        sep = '-' * 80 + '\n'
87
        labels = ('name', 'owner', 'flavor', 'image', 'state', 'backend',
88
                  'deleted', 'action', 'backendjobid', 'backendopcode',
89
                  'backendjobstatus', 'backend_time')
90
        fields = (vm.name, vm.userid, vm.flavor.name, image,
91
                  format_vm_state(vm), str(vm.backend), str(vm.deleted),
92
                  str(vm.action), str(vm.backendjobid), str(vm.backendopcode),
93
                  str(vm.backendjobstatus), str(vm.backendtime))
94

  
95
        self.stdout.write(sep)
96
        self.stdout.write('State of Server in DB\n')
97
        self.stdout.write(sep)
98
        for l, f in zip(labels, fields):
99
            self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n')
100
        self.stdout.write('\n')
101
        for nic in vm.nics.all():
102
            self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s,  Network: %s\n"\
103
                              % (nic.index, nic.ipv4, nic.mac, nic.ipv6,  nic.network))
104

  
105
        client = vm.get_client()
106
        try:
107
            g_vm = client.GetInstance(vm.backend_vm_id)
108
        except GanetiApiError as e:
109
            if e.code == 404:
110
                self.stdout.write('Server does not exist in backend %s\n' %
111
                                  vm.backend.clustername)
112
                return
113
            else:
114
                raise e
115

  
116
        self.stdout.write('\n')
117
        self.stdout.write(sep)
118
        self.stdout.write('State of Server in Ganeti\n')
119
        self.stdout.write(sep)
120
        for i in GANETI_INSTANCE_FIELDS:
121
            try:
122
                value = g_vm[i]
123
                if i.find('time') != -1:
124
                    value = datetime.fromtimestamp(value)
125
                self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n')
126
            except KeyError:
127
                pass
128

  
129
        if not options['jobs']:
130
            return
131

  
132
        self.stdout.write('\n')
133
        self.stdout.write(sep)
134
        self.stdout.write('Non-archived jobs concerning Server in Ganeti\n')
135
        self.stdout.write(sep)
136
        jobs = client.GetJobs()
137
        for j in jobs:
138
            info = client.GetJobStatus(j)
139
            summary = ' '.join(info['summary'])
140
            if summary.startswith("INSTANCE") and \
141
               summary.find(vm.backend_vm_id) != -1:
142
                for i in GANETI_JOB_FIELDS:
143
                    value = info[i]
144
                    if i.find('_ts') != -1:
145
                        value = merge_time(value)
146
                    try:
147
                        self.stdout.write(i.ljust(14) + ': ' + str(value) +\
148
                                          '\n')
149
                    except KeyError:
150
                        pass
151
                self.stdout.write('\n' + sep)
152
        # Return the RAPI client to pool
153
        vm.put_client(client)
b/snf-cyclades-app/synnefo/api/management/commands/server-list.py
37 37

  
38 38
from synnefo.api.util import get_image
39 39
from synnefo.db.models import VirtualMachine, Backend
40
from ._common import format_vm_state
40 41

  
41 42

  
42 43
class Command(BaseCommand):
......
101 102
                image = cache.get_image(server.imageid, server.userid)['name']
102 103
            except:
103 104
                image = server.imageid
104
            fields = (id, name, server.userid, flavor, image, server.operstate,
105

  
106
            state = format_vm_state(server)
107
            fields = (id, name, server.userid, flavor, image, state,
105 108
                      str(server.backend))
106 109

  
107 110
            if options['csv']:
b/snf-cyclades-app/synnefo/api/management/commands/server-show.py
36 36
from synnefo.api.util import get_image
37 37
from synnefo.db.models import VirtualMachine
38 38

  
39
from ._common import format_bool, format_date
39
from ._common import format_bool, format_date, format_vm_state
40 40

  
41 41

  
42 42
class Command(BaseCommand):
......
68 68
            'flavor': flavor,
69 69
            'deleted': format_bool(server.deleted),
70 70
            'suspended': format_bool(server.suspended),
71
            'state': server.operstate
71
            'state': format_vm_state(server)
72 72
        }
73 73
        
74 74
        for key, val in sorted(kv.items()):
/dev/null
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
from datetime import datetime
36
from optparse import make_option
37
from django.core.management.base import BaseCommand, CommandError
38

  
39
from synnefo.api.util import get_image
40
from synnefo.lib.utils import merge_time
41
from synnefo.db.models import VirtualMachine
42
from synnefo.logic.rapi import GanetiApiError
43

  
44

  
45
# Fields to print from a gnt-instance info
46
GANETI_INSTANCE_FIELDS = ('name', 'oper_state', 'admin_state', 'status',
47
                          'pnode', 'snode', 'network_port', 'disk_template',
48
                          'disk_usage', 'oper_ram', 'oper_vcpus', 'mtime',
49
                          'nic.ips', 'nic.macs', 'nic.networks', 'nic.modes')
50

  
51
# Fields to print from a gnt-job info
52
GANETI_JOB_FIELDS = ('id', 'status', 'summary', 'opresult', 'opstatus',
53
                     'oplog', 'start_ts', 'end_ts')
54

  
55

  
56
class Command(BaseCommand):
57
    help = "Inspect a server on DB and Ganeti"
58

  
59
    option_list = BaseCommand.option_list + (
60
        make_option('--jobs', action='store_true',
61
                    dest='jobs', default=False,
62
                    help="Show non-archived jobs concerning server."
63
            ),
64
    )
65

  
66
    def handle(self, *args, **options):
67
        if len(args) != 1:
68
            raise CommandError("Please provide a server ID")
69

  
70
        try:
71
            vm_id = int(args[0])
72
        except ValueError:
73
            raise CommandError("Invalid server ID")
74

  
75
        try:
76
            vm = VirtualMachine.objects.get(id=vm_id)
77
        except VirtualMachine.DoesNotExist:
78
            raise CommandError("Server not found in DB")
79

  
80
        try:
81
            image = get_image(vm.imageid, vm.userid)['name']
82
        except:
83
            image = vm.imageid
84

  
85
        sep = '-' * 80 + '\n'
86
        labels = ('name', 'owner', 'flavor', 'image', 'state', 'backend',
87
                  'deleted', 'action', 'backendjobid', 'backendopcodoe',
88
                  'backendjobstatus', 'backend_time')
89
        fields = (vm.name, vm.userid, vm.flavor.name, image, vm.operstate,
90
                  str(vm.backend), str(vm.deleted), str(vm.action),
91
                  str(vm.backendjobid), str(vm.backendopcode),
92
                  str(vm.backendjobstatus), str(vm.backendtime))
93

  
94
        self.stdout.write(sep)
95
        self.stdout.write('State of Server in DB\n')
96
        self.stdout.write(sep)
97
        for l, f in zip(labels, fields):
98
            self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n')
99
        self.stdout.write('\n')
100
        for nic in vm.nics.all():
101
            self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s,  Network: %s\n"\
102
                              % (nic.index, nic.ipv4, nic.mac, nic.ipv6,  nic.network))
103

  
104
        client = vm.get_client()
105
        try:
106
            g_vm = client.GetInstance(vm.backend_vm_id)
107
        except GanetiApiError as e:
108
            if e.code == 404:
109
                self.stdout.write('Server does not exist in backend %s\n' %
110
                                  vm.backend.clustername)
111
                return
112
            else:
113
                raise e
114

  
115
        self.stdout.write('\n')
116
        self.stdout.write(sep)
117
        self.stdout.write('State of Server in Ganeti\n')
118
        self.stdout.write(sep)
119
        for i in GANETI_INSTANCE_FIELDS:
120
            try:
121
                value = g_vm[i]
122
                if i.find('time') != -1:
123
                    value = datetime.fromtimestamp(value)
124
                self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n')
125
            except KeyError:
126
                pass
127

  
128
        if not options['jobs']:
129
            return
130

  
131
        self.stdout.write('\n')
132
        self.stdout.write(sep)
133
        self.stdout.write('Non-archived jobs concerning Server in Ganeti\n')
134
        self.stdout.write(sep)
135
        jobs = client.GetJobs()
136
        for j in jobs:
137
            info = client.GetJobStatus(j)
138
            summary = ' '.join(info['summary'])
139
            if summary.startswith("INSTANCE") and \
140
               summary.find(vm.backend_vm_id) != -1:
141
                for i in GANETI_JOB_FIELDS:
142
                    value = info[i]
143
                    if i.find('_ts') != -1:
144
                        value = merge_time(value)
145
                    try:
146
                        self.stdout.write(i.ljust(14) + ': ' + str(value) +\
147
                                          '\n')
148
                    except KeyError:
149
                        pass
150
                self.stdout.write('\n' + sep)
151
        # Return the RAPI client to pool
152
        vm.put_client(client)

Also available in: Unified diff