Add nginx-reverse-proxy Software Release.
[slapos.git] / slapos / recipe / reverse_proxy_nginx / __init__.py
1 ##############################################################################
2 #
3 # Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 ##############################################################################
27 import operator
28 from slapos.recipe.librecipe import GenericSlapRecipe
29 import zc.buildout
30
31 class Recipe(GenericSlapRecipe):
32 def _install(self):
33 path_list = []
34
35 # Check for mandatory arguments
36 domain_name = self.options['domain']
37 if not domain_name:
38 raise zc.buildout.UserError('No domain name specified. Please define '
39 'the "domain" instance parameter.')
40
41 # XXX: add HTTP support
42 #https_port_number = self.options['https-port']
43 #http_port_number = self.options['http-port']
44
45 # Parse list of slaves
46 slave_instance_list = sorted(self.options['slave-instance-list'],
47 key=operator.itemgetter('slave_reference'))
48
49 # Now, we only take first instance and only use this one.
50 # XXX: TODO real implementation of slaves
51 zimbra_slave_instance = slave_instance_list[0]
52
53 # Generate Nginx configuration
54 nginx_configuration_dict = {
55 'listen-local-ipv4': self.options['ipv4'],
56 'listen-global-ipv6': '[%s]' % self.options['ipv6'],
57 'domain-name': domain_name,
58 'smtp-port-number': self.options['smtp-port'],
59 'error-log': self.options['error-log'],
60 'access-log': self.options['access-log'],
61 'htdocs': self.options['htdocs'],
62 'smtp-upstream-host': zimbra_slave_instance['smtp-upstream-host'],
63 'smtp-upstream-port': zimbra_slave_instance['smtp-upstream-port'],
64 }
65 nginx_configuration_file = self.createFile(
66 self.options['configuration-file'],
67 self.substituteTemplate(self.getTemplateFilename('nginx.conf.in'),
68 nginx_configuration_dict)
69 )
70 path_list.append(nginx_configuration_file)
71
72 # Generate Nginx wrapper
73 wrapper = self.createWrapper(
74 name=self.options['wrapper'],
75 command=self.options['nginx-executable'],
76 parameters=[
77 '-c', self.options['configuration-file'],
78 '-p', self.options['home-directory']
79 ]
80 )
81
82 # TODO: reload configuration or have feature like apache_map
83
84 # Send connection informations about each slave
85 for slave_instance in slave_instance_list:
86 reference = slave_instance.get("slave_reference")
87 self.logger.debug('Sending connection parameters of slave '
88 'instance: %s' % reference)
89 try:
90 connection_dict = {
91 'listening-ipv6': self.options['ipv6'],
92 # Arbitrary, as the instance doesn't know its public IP.
93 'listening-ipv4': self.options['public-ipv4'],
94 # XXX-TODO
95 #'site_url': url,
96 }
97 self.setConnectionDict(connection_dict, reference)
98 except:
99 self.logger.fatal("Error while sending slave %s informations: %s",
100 reference, traceback.format_exc())
101
102 return path_list