cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
main.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] 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 
20 ##
21 # @package src.ec2.upload
22 # WSGI application for publishing system images for CLM/CM to download
23 #
24 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
25 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
26 #
27 import logging
28 import os
29 import traceback
30 import urlparse
31 from ec2.settings import UPLOAD_IMAGES_PATH
32 import sys
33 
34 DEBUG = True
35 
36 QUERY_IMAGE_NAME = 'image_name'
37 BLOCK_SIZE = 65536
38 
39 logging.basicConfig(stream=sys.stdout)
40 logging.basicConfig(stream=sys.stderr)
41 
42 
43 ##
44 # Extract EC2 parameters from GET/POST request.
45 #
46 # Args:
47 # environ <> WSGI's environment variable
48 #
49 # Returns:
50 # <dict> EC2 action's parameters
51 #
52 def _environ_to_parameters(environ):
53  query_string = environ['QUERY_STRING']
54 
55  query_string_params = urlparse.parse_qs(query_string)
56  if len(query_string_params) != 1:
57  pass # ERROR
58 
59  parameters = {}
60  parameters['image_name'] = query_string_params['image_name'][0]
61 
62  # we keep blank values to capture commands for S3
63 
64  print 15 * '=', 'PARAMETERS', 15 * '='
65  for u, v in parameters.iteritems():
66  print u, ":", v
67  print 42 * '='
68 
69  return parameters
70 
71 
72 def application(environ, start_response):
73  print 15 * '=', 'EC2-CM-UPLOAD', 15 * '='
74  try:
75  # return _application(environ, start_response)
76  parameters = _environ_to_parameters(environ)
77 
78  image_name = parameters[QUERY_IMAGE_NAME]
79  file_path = os.path.join(UPLOAD_IMAGES_PATH, image_name)
80 
81  image_file = open(file_path, 'r')
82  start_response('200 OK', [])
83  if 'wsgi.file_wrapper' in environ:
84  return environ['wsgi.file_wrapper'](image_file, BLOCK_SIZE)
85  else:
86  return iter(lambda: image_file.read(BLOCK_SIZE), '')
87 
88  except Exception, error:
89  response = handle_500(environ, error)
90  start_response('500 Internal Server Error', [('Content-Type', 'text/plain')])
91  return response
92 
93 
94 def handle_500(environ, error):
95  if DEBUG:
96  response = traceback.format_exc()
97  print response
98  else:
99  response = '500 Internal Server Error'
100  return response
101