cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
__init__.py
Go to the documentation of this file.
1 # _*_ coding: utf_8 _*_
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010_2013] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE_2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 from cm.models.vm import VM
20 from cm.utils import log
21 from cm.utils.decorators import ec2ctx_log
22 from cm.utils.exception import CMException
23 
24 from cm.views.ec2ctx.helpers.getters import get_exposed_methods, get_submethods
25 from cm.views.ec2ctx.helpers.request import get_vm_by_ip
26 from django.conf.urls import patterns, include, url
27 from time import sleep
28 import base64
29 import os
30 import sys
31 import inspect
32 
33 ##
34 # @package src.cm.views.ec2ctx.latest.metadata
35 #
36 # @copyright Copyright (c) 2013 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
37 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
38 #
39 
40 
41 @ec2ctx_log(log=True)
42 def ami_id(request):
43  vm_ip = request.META.get('REMOTE_ADDR')
44  vm = VM.get_by_ip(vm_ip)
45 
46  ami = 'ami-' + str(vm.dict['image_id'])
47 
48  return ami
49 ami_id.func_dict['meta_data'] = True
50 
51 
52 @ec2ctx_log(log=True)
53 def ami_launch_index(request):
54  vm_ip = request.META.get('REMOTE_ADDR')
55  vm = VM.get_by_ip(vm_ip)
56 
57  instance_id = vm.dict['vm_id']
58  reservation_id = vm.long_dict['reservation_id']
59 
60  launch_index = reservation_id - instance_id + 1
61 
62  return launch_index
63 ami_launch_index.func_dict['meta_data'] = True
64 
65 
66 @ec2ctx_log(log=True)
67 def ami_manifest_path(request):
68  return 'Not implemented' # chyba nie ma odpowiednika
69 ami_manifest_path.func_dict['meta_data'] = True
70 
71 
72 @ec2ctx_log(log=True)
73 def block_device_mapping(request):
74  return 'Not implemented'
75 block_device_mapping.func_dict['meta_data'] = True
76 
77 
78 @ec2ctx_log(log=True)
79 def hostname(request):
80 # vm_ip = request.META.get('REMOTE_ADDR')
81 #
82 # vm = VM.get_by_ip( vm_ip )
83 #
84 # instance = vm.dict['name']
85 # instance = instance.replace(" " , "_")
86 #
87 #
88 # return instance
89  return "" # TODO !!!
90 hostname.func_dict['meta_data'] = True
91 
92 
93 @ec2ctx_log(log=True)
94 def instance_action(self):
95  return 'Not implemented'
96 instance_action.func_dict['meta_data'] = True
97 
98 
99 @ec2ctx_log(log=True)
100 def instance_id(request):
101  vm_ip = request.META.get('REMOTE_ADDR')
102 
103  vm = VM.get_by_ip(vm_ip)
104 
105  vm_id = 'i-' + str(vm.dict['vm_id'])
106  return vm_id
107 instance_id.func_dict['meta_data'] = True
108 
109 
110 @ec2ctx_log(log=True)
111 def instance_type(request):
112  vm_ip = request.META.get('REMOTE_ADDR')
113  vm = VM.get_by_ip(vm_ip)
114 
115  template_name = str(vm.dict['template_name'])
116  return template_name
117 instance_type.func_dict['meta_data'] = True
118 
119 
120 @ec2ctx_log(log=True)
121 def kernel_id(request):
122  return 'Not implemented'
123 kernel_id.func_dict['meta_data'] = True
124 
125 
126 @ec2ctx_log(log=True)
127 def local_hostname(request):
128  return '' # TODO
129 local_hostname.func_dict['meta_data'] = True
130 
131 
132 @ec2ctx_log(log=True)
133 def local_ipv4(request):
134  vm_ip = request.META.get('REMOTE_ADDR')
135  vm = VM.get_by_ip(vm_ip)
136 
137  leases = vm.dict['leases']
138  ip = leases
139  for ip in leases:
140  if ip.get('address'):
141  ip = ip.get('address')
142  else:
143  ip = ""
144 
145  return ip
146 local_ipv4.func_dict['meta_data'] = True
147 
148 
149 @ec2ctx_log(log=True)
150 def mac(request):
151  return 'Not implemented'
152 mac.func_dict['meta_data'] = True
153 
154 
155 @ec2ctx_log(log=True)
156 def network(request):
157  return 'Not implemented'
158 network.func_dict['meta_data'] = True
159 
160 
161 @ec2ctx_log(log=True)
162 def placement(request):
163  return 'Not implemented'
164 placement.func_dict['meta_data'] = True
165 
166 
167 @ec2ctx_log(log=True)
168 def public_hostname(request):
169  return '' # TODO
170 public_hostname.func_dict['meta_data'] = True
171 
172 
173 @ec2ctx_log(log=True)
174 def public_ipv4(request):
175  vm_ip = request.META.get('REMOTE_ADDR')
176  vm = VM.get_by_ip(vm_ip)
177 
178  leases = vm.dict['leases']
179  ip = leases
180  for ip in leases:
181  if ip.get('public_ip'):
182  ip = ip.get('public_ip')
183  else:
184  ip = ""
185 
186  return ip
187 public_ipv4.func_dict['meta_data'] = True
188 
189 
190 @ec2ctx_log(log=True)
191 def public_keys(request, number=None, key_type=None):
192  vm_ip = request.META.get('REMOTE_ADDR')
193 
194  vm = VM.get_by_ip(vm_ip)
195 
196  if number == "0" and vm.long_dict.get('ssh_key'):
197  if key_type == "openssh-key": # HARDCODED VALUE, only one key can be assigned
198  return vm.long_dict.get('ssh_key')
199  else:
200  if key_type:
201  pass # todo error
202  else:
203  return "openssh-key"
204  else:
205  if number or key_type:
206  pass # todo error
207 
208  if vm.long_dict.get('ssh_key'):
209  return '0=public-key'
210 public_keys.func_dict['meta_data'] = True
211 
212 
213 @ec2ctx_log(log=True)
214 def reservation_id(request):
215  vm_ip = request.META.get('REMOTE_ADDR')
216  vm = VM.get_by_ip(vm_ip)
217 
218  reservation_id = vm.long_dict['reservation_id']
219 
220  return 'r-' + str(reservation_id)
221 reservation_id.func_dict['meta_data'] = True
222 
223 
224 @ec2ctx_log(log=True)
225 def security_groups(request):
226  return 'default'
227 security_groups.func_dict['meta_data'] = True
228 
229 
230 @ec2ctx_log(log=True)
231 def meta_data(self):
232  funcs = inspect.getmembers(sys.modules[__name__], inspect.isfunction)
233  funcs = [fun[0].replace('_', '-') for fun in funcs if fun[1].func_dict.get('meta_data')]
234  return '\n'.join(funcs)
235 
236 
237 meta_data_patterns = patterns('',
238  url(r'^$', meta_data),
239  url(r'^ami-id/?$', ami_id),
240  url(r'^ami-manifest-path/?$', ami_manifest_path),
241  url(r'^ami-launch-index/?$', ami_launch_index),
242  url(r'^block-device-mapping/?$', block_device_mapping),
243  url(r'^hostname/?$', hostname),
244  url(r'^instance-action/?$', instance_action),
245  url(r'^instance-id/?$', instance_id),
246  url(r'^instance-type/?$', instance_type),
247  url(r'^kernel-id/?$', kernel_id),
248  url(r'^local-hostname/?$', local_hostname),
249  url(r'^local-ipv4/?$', local_ipv4),
250  url(r'^mac/?$', mac),
251  url(r'^network/?$', network),
252  url(r'^placement/?$', placement),
253  url(r'^public-hostname/?$', public_hostname),
254  url(r'^public-ipv4/?$', public_ipv4),
255  url(r'^public-keys/?$', public_keys),
256  url(r'^public-keys/(?P<number>[0-9]+)/?$', public_keys),
257  url(r'^public-keys/(?P<number>[0-9]+)/(?P<key_type>[^/]+)/?$', public_keys),
258  url(r'^reservation-id/?$', reservation_id),
259  url(r'^security-groups/?$', security_groups))
260