root / snf-cyclades-app / synnefo / admin-interface / templatetags / helpdesk_tags.py @ c204fcff
History | View | Annotate | Download (4.2 kB)
1 | c204fcff | Alex Pyrgiotis | # Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | c204fcff | Alex Pyrgiotis | #
|
3 | c204fcff | Alex Pyrgiotis | # Redistribution and use in source and binary forms, with or without
|
4 | c204fcff | Alex Pyrgiotis | # modification, are permitted provided that the following conditions
|
5 | c204fcff | Alex Pyrgiotis | # are met:
|
6 | c204fcff | Alex Pyrgiotis | #
|
7 | c204fcff | Alex Pyrgiotis | # 1. Redistributions of source code must retain the above copyright
|
8 | c204fcff | Alex Pyrgiotis | # notice, this list of conditions and the following disclaimer.
|
9 | c204fcff | Alex Pyrgiotis | #
|
10 | c204fcff | Alex Pyrgiotis | # 2. Redistributions in binary form must reproduce the above copyright
|
11 | c204fcff | Alex Pyrgiotis | # notice, this list of conditions and the following disclaimer in the
|
12 | c204fcff | Alex Pyrgiotis | # documentation and/or other materials provided with the distribution.
|
13 | c204fcff | Alex Pyrgiotis | #
|
14 | c204fcff | Alex Pyrgiotis | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 | c204fcff | Alex Pyrgiotis | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 | c204fcff | Alex Pyrgiotis | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 | c204fcff | Alex Pyrgiotis | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 | c204fcff | Alex Pyrgiotis | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 | c204fcff | Alex Pyrgiotis | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 | c204fcff | Alex Pyrgiotis | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 | c204fcff | Alex Pyrgiotis | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 | c204fcff | Alex Pyrgiotis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 | c204fcff | Alex Pyrgiotis | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 | c204fcff | Alex Pyrgiotis | # SUCH DAMAGE.
|
25 | c204fcff | Alex Pyrgiotis | #
|
26 | c204fcff | Alex Pyrgiotis | # The views and conclusions contained in the software and documentation are
|
27 | c204fcff | Alex Pyrgiotis | # those of the authors and should not be interpreted as representing official
|
28 | c204fcff | Alex Pyrgiotis | # policies, either expressed or implied, of GRNET S.A.
|
29 | c204fcff | Alex Pyrgiotis | |
30 | c204fcff | Alex Pyrgiotis | from django import template |
31 | c204fcff | Alex Pyrgiotis | |
32 | c204fcff | Alex Pyrgiotis | register = template.Library() |
33 | c204fcff | Alex Pyrgiotis | |
34 | c204fcff | Alex Pyrgiotis | |
35 | c204fcff | Alex Pyrgiotis | @register.filter(name="vm_public_ip") |
36 | c204fcff | Alex Pyrgiotis | def vm_public_ip(vm): |
37 | c204fcff | Alex Pyrgiotis | """
|
38 | c204fcff | Alex Pyrgiotis | Identify if vm is connected to ``public`` network and return the ipv4
|
39 | c204fcff | Alex Pyrgiotis | address
|
40 | c204fcff | Alex Pyrgiotis | """
|
41 | c204fcff | Alex Pyrgiotis | try:
|
42 | c204fcff | Alex Pyrgiotis | return vm.nics.filter(network__public=True)[0].ipv4_address |
43 | c204fcff | Alex Pyrgiotis | except IndexError: |
44 | c204fcff | Alex Pyrgiotis | return "No public ip" |
45 | c204fcff | Alex Pyrgiotis | |
46 | c204fcff | Alex Pyrgiotis | |
47 | c204fcff | Alex Pyrgiotis | VM_STATE_CSS_MAP = { |
48 | c204fcff | Alex Pyrgiotis | 'BUILD': 'warning', |
49 | c204fcff | Alex Pyrgiotis | 'PENDING': 'warning', |
50 | c204fcff | Alex Pyrgiotis | 'ERROR': 'important', |
51 | c204fcff | Alex Pyrgiotis | 'STOPPED': 'notice', |
52 | c204fcff | Alex Pyrgiotis | 'STARTED': 'success', |
53 | c204fcff | Alex Pyrgiotis | 'ACTIVE': 'success', |
54 | c204fcff | Alex Pyrgiotis | 'DESTROYED': 'inverse' |
55 | c204fcff | Alex Pyrgiotis | } |
56 | c204fcff | Alex Pyrgiotis | |
57 | c204fcff | Alex Pyrgiotis | |
58 | c204fcff | Alex Pyrgiotis | @register.filter(name="object_status_badge", is_safe=True) |
59 | c204fcff | Alex Pyrgiotis | def object_status_badge(vm_or_net): |
60 | c204fcff | Alex Pyrgiotis | """
|
61 | c204fcff | Alex Pyrgiotis | Return a span badge styled based on the vm current status
|
62 | c204fcff | Alex Pyrgiotis | """
|
63 | c204fcff | Alex Pyrgiotis | state = vm_or_net.operstate if hasattr(vm_or_net, 'operstate') else \ |
64 | c204fcff | Alex Pyrgiotis | vm_or_net.state |
65 | c204fcff | Alex Pyrgiotis | state_cls = VM_STATE_CSS_MAP.get(state, 'notice')
|
66 | c204fcff | Alex Pyrgiotis | badge_cls = "badge badge-%s" % state_cls
|
67 | c204fcff | Alex Pyrgiotis | |
68 | c204fcff | Alex Pyrgiotis | deleted_badge = ""
|
69 | c204fcff | Alex Pyrgiotis | if vm_or_net.deleted:
|
70 | c204fcff | Alex Pyrgiotis | deleted_badge = '<span class="badge badge-important">Deleted</span>'
|
71 | c204fcff | Alex Pyrgiotis | return '%s\n<span class="%s">%s</span>' % (deleted_badge, badge_cls, state) |
72 | c204fcff | Alex Pyrgiotis | |
73 | c204fcff | Alex Pyrgiotis | |
74 | c204fcff | Alex Pyrgiotis | @register.filter(name="network_deleted_badge", is_safe=True) |
75 | c204fcff | Alex Pyrgiotis | def network_deleted_badge(network): |
76 | c204fcff | Alex Pyrgiotis | """
|
77 | c204fcff | Alex Pyrgiotis | Return a span badge styled based on the vm current status
|
78 | c204fcff | Alex Pyrgiotis | """
|
79 | c204fcff | Alex Pyrgiotis | deleted_badge = ""
|
80 | c204fcff | Alex Pyrgiotis | if network.deleted:
|
81 | c204fcff | Alex Pyrgiotis | deleted_badge = '<span class="badge badge-important">Deleted</span>'
|
82 | c204fcff | Alex Pyrgiotis | return deleted_badge
|
83 | c204fcff | Alex Pyrgiotis | |
84 | c204fcff | Alex Pyrgiotis | |
85 | c204fcff | Alex Pyrgiotis | @register.filter(name="get_os", is_safe=True) |
86 | c204fcff | Alex Pyrgiotis | def get_os(vm): |
87 | c204fcff | Alex Pyrgiotis | try:
|
88 | c204fcff | Alex Pyrgiotis | return vm.metadata.filter(meta_key="OS").get().meta_value |
89 | c204fcff | Alex Pyrgiotis | except:
|
90 | c204fcff | Alex Pyrgiotis | return "unknown" |
91 | c204fcff | Alex Pyrgiotis | |
92 | c204fcff | Alex Pyrgiotis | |
93 | c204fcff | Alex Pyrgiotis | @register.filter(name="network_vms", is_safe=True) |
94 | c204fcff | Alex Pyrgiotis | def network_vms(network, account, show_deleted=False): |
95 | c204fcff | Alex Pyrgiotis | vms = [] |
96 | c204fcff | Alex Pyrgiotis | nics = network.nics.filter(machine__userid=account) |
97 | c204fcff | Alex Pyrgiotis | if not show_deleted: |
98 | c204fcff | Alex Pyrgiotis | nics = nics.filter(machine__deleted=False).distinct()
|
99 | c204fcff | Alex Pyrgiotis | for nic in nics: |
100 | c204fcff | Alex Pyrgiotis | vms.append(nic.machine) |
101 | c204fcff | Alex Pyrgiotis | return vms
|
102 | c204fcff | Alex Pyrgiotis | |
103 | c204fcff | Alex Pyrgiotis | |
104 | c204fcff | Alex Pyrgiotis | @register.filter(name="network_nics") |
105 | c204fcff | Alex Pyrgiotis | def network_nics(network, account, show_deleted=False): |
106 | c204fcff | Alex Pyrgiotis | vms = [] |
107 | c204fcff | Alex Pyrgiotis | nics = network.nics.filter(machine__userid=account) |
108 | c204fcff | Alex Pyrgiotis | if not show_deleted: |
109 | c204fcff | Alex Pyrgiotis | nics = nics.filter(machine__deleted=False).distinct()
|
110 | c204fcff | Alex Pyrgiotis | return nics
|
111 | c204fcff | Alex Pyrgiotis | |
112 | c204fcff | Alex Pyrgiotis | |
113 | c204fcff | Alex Pyrgiotis | @register.filter(name="backend_info", is_safe=True) |
114 | c204fcff | Alex Pyrgiotis | def backend_info(vm): |
115 | c204fcff | Alex Pyrgiotis | content = ""
|
116 | c204fcff | Alex Pyrgiotis | backend = vm.backend |
117 | c204fcff | Alex Pyrgiotis | excluded = ['password_hash', 'hash', 'username'] |
118 | c204fcff | Alex Pyrgiotis | if not vm.backend: |
119 | c204fcff | Alex Pyrgiotis | content = "No backend"
|
120 | c204fcff | Alex Pyrgiotis | return content
|
121 | c204fcff | Alex Pyrgiotis | |
122 | c204fcff | Alex Pyrgiotis | for field in vm.backend._meta.fields: |
123 | c204fcff | Alex Pyrgiotis | if field.name in excluded: |
124 | c204fcff | Alex Pyrgiotis | continue
|
125 | c204fcff | Alex Pyrgiotis | content += '<dt>Backend ' + field.name + '</dt><dd>' + \ |
126 | c204fcff | Alex Pyrgiotis | str(getattr(backend, field.name)) + '</dd>' |
127 | c204fcff | Alex Pyrgiotis | return content |