PHP : add configuration options to php.ini
[slapos.git] / slapos / recipe / lamp / __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 from slapos.recipe.librecipe import BaseSlapRecipe
28 import os
29 import shutil
30 import pkg_resources
31 import zc.buildout
32 import sys
33 import zc.recipe.egg
34
35 class BaseRecipe(BaseSlapRecipe):
36 def installMysqlServer(self, ip=None, port=None):
37 if ip is None:
38 ip = self.getLocalIPv4Address()
39 if port is None:
40 port = '3306'
41 mysql_conf = dict(
42 ip=ip,
43 data_directory=os.path.join(self.data_root_directory,
44 'mysql'),
45 tcp_port=port,
46 pid_file=os.path.join(self.run_directory, 'mysqld.pid'),
47 socket=os.path.join(self.run_directory, 'mysqld.sock'),
48 error_log=os.path.join(self.log_directory, 'mysqld.log'),
49 slow_query_log=os.path.join(self.log_directory,
50 'mysql-slow.log'),
51 database='appdb',
52 user='appuser',
53 password=self.generatePassword(),
54 )
55 self._createDirectory(mysql_conf['data_directory'])
56
57 mysql_conf_path = self.createConfigurationFile("my.cnf",
58 self.substituteTemplate(pkg_resources.resource_filename(__name__, 'template/my.cnf.in'),
59 mysql_conf))
60
61 mysql_script = pkg_resources.resource_string(__name__,
62 'template/mysqlinit.sql.in') % mysql_conf
63 self.path_list.extend(zc.buildout.easy_install.scripts([('mysql_update',
64 __name__ + '.mysql', 'updateMysql')], self.ws,
65 sys.executable, self.wrapper_directory, arguments=[dict(
66 mysql_script=mysql_script,
67 mysql_binary=self.options['mysql_binary'].strip(),
68 mysql_upgrade_binary=self.options['mysql_upgrade_binary'].strip(),
69 socket=mysql_conf['socket'],
70 )]))
71 self.path_list.extend(zc.buildout.easy_install.scripts([('mysqld',
72 __name__ + '.mysql', 'runMysql')], self.ws,
73 sys.executable, self.wrapper_directory, arguments=[dict(
74 mysql_install_binary=self.options['mysql_install_binary'].strip(),
75 mysqld_binary=self.options['mysqld_binary'].strip(),
76 data_directory=mysql_conf['data_directory'].strip(),
77 mysql_binary=self.options['mysql_binary'].strip(),
78 socket=mysql_conf['socket'].strip(),
79 configuration_file=mysql_conf_path,
80 )]))
81 self.path_list.extend([mysql_conf_path])
82 return dict(
83 mysql_host=mysql_conf['ip'],
84 mysql_port=mysql_conf['tcp_port'],
85 mysql_user=mysql_conf['user'],
86 mysql_password=mysql_conf['password'],
87 mysql_database=mysql_conf['database'],
88 )
89
90 def createHtdocs(self, source, document_root):
91 source = self.options['source'].strip()
92 document_root = self.createDataDirectory('htdocs')
93 for p in os.listdir(document_root):
94 path = os.path.join(document_root, p)
95 if os.path.isdir(path):
96 shutil.rmtree(path)
97 else:
98 os.unlink(path)
99 for p in os.listdir(source):
100 path = os.path.join(source, p)
101 if os.path.isdir(path):
102 shutil.copytree(path, os.path.join(document_root, p))
103 else:
104 shutil.copy2(path, os.path.join(document_root, p))
105
106 def installApache(self, document_root, ip=None, port=None):
107 if ip is None:
108 ip=self.getGlobalIPv6Address()
109 if port is None:
110 port = '9080'
111 apache_config = dict(
112 pid_file=os.path.join(self.run_directory, 'httpd.pid'),
113 lock_file=os.path.join(self.run_directory, 'httpd.lock'),
114 ip=ip,
115 port=port,
116 error_log=os.path.join(self.log_directory, 'httpd-error.log'),
117 access_log=os.path.join(self.log_directory, 'httpd-access.log'),
118 document_root=document_root,
119 php_ini_dir=self.etc_directory
120 )
121 config_file = self.createConfigurationFile('httpd.conf',
122 self.substituteTemplate(pkg_resources.resource_filename(__name__,
123 'template/apache.in'), apache_config))
124 self.path_list.append(config_file)
125 self.path_list.append(self.createConfigurationFile('php.ini',
126 self.substituteTemplate(pkg_resources.resource_filename(__name__,
127 'template/php.ini.in'), dict(tmp_directory=self.tmp_directory))))
128 self.path_list.extend(zc.buildout.easy_install.scripts([(
129 'httpd',
130 __name__ + '.apache', 'runApache')], self.ws,
131 sys.executable, self.wrapper_directory, arguments=[
132 dict(
133 required_path_list=[],
134 binary=self.options['httpd_binary'],
135 config=config_file
136 )
137 ]))
138 return 'http://[%s]:%s' % (ip, port)
139
140 def createConfiguration(self, template, document_root, destination, d):
141 directory = os.path.dirname(destination)
142 file = os.path.basename(destination)
143 path = document_root
144 if directory:
145 path = os.path.join(document_root, directory)
146 if not os.path.exists(path):
147 os.makedirs(path)
148 destination = os.path.join(path, file)
149 open(destination, 'w').write(open(template, 'r').read() % d)
150
151 class Static(BaseRecipe):
152 def _install(self):
153 self.path_list = []
154 self.requirements, self.ws = self.egg.working_set()
155 document_root = self.createDataDirectory('htdocs')
156 self.createHtdocs(self.options['source'].strip(), document_root)
157 url = self.installApache(document_root)
158 self.setConnectionDict(dict(url = url))
159 return self.path_list
160
161 class Simple(BaseRecipe):
162 def _install(self):
163 self.path_list = []
164 self.requirements, self.ws = self.egg.working_set()
165 document_root = self.createDataDirectory('htdocs')
166 self.createHtdocs(self.options['source'].strip(), document_root)
167 mysql_conf = self.installMysqlServer()
168 url = self.installApache(document_root)
169 self.setConnectionDict(dict(
170 url=url,
171 **mysql_conf
172 ))
173 self.createConfiguration(self.options['template'], document_root,
174 self.options['configuration'], mysql_conf)
175 return self.path_list
176
177 class Request(BaseRecipe):
178 def _install(self):
179 self.path_list = []
180 self.requirements, self.ws = self.egg.working_set()
181 software_type = self.parameter_dict['slap_software_type']
182 if software_type == 'RootSoftwareInstance':
183 document_root = self.createDataDirectory('htdocs')
184 self.createHtdocs(self.options['source'].strip(), document_root)
185 mysql = self.request(self.software_release_url, 'MySQL Server', 'mysql')
186 mysql_conf = dict(
187 mysql_host=mysql.getConnectionParameter('mysql_host'),
188 mysql_port=mysql.getConnectionParameter('mysql_port'),
189 mysql_user=mysql.getConnectionParameter('mysql_user'),
190 mysql_password=mysql.getConnectionParameter('mysql_password'),
191 mysql_database=mysql.getConnectionParameter('mysql_database'),
192 )
193 url = self.installApache(document_root)
194 self.setConnectionDict(dict(
195 url=url,
196 ))
197 self.createConfiguration(self.options['template'], document_root,
198 self.options['configuration'], mysql_conf)
199 elif software_type == 'MySQL Server':
200 mysql_conf = self.installMysqlServer()
201 self.setConnectionDict(dict(
202 **mysql_conf
203 ))
204 else:
205 raise zc.buildout.UserError('Uknown software type %r' % software_type)
206 return self.path_list