Davstorage+ajaxplorer software release v1
[slapos.git] / slapos / recipe / davstorage / __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 subprocess
28 import httplib
29 import base64
30 import os
31 import shutil
32
33 from slapos.recipe.librecipe import GenericBaseRecipe
34
35 class Recipe(GenericBaseRecipe):
36
37 def _options(self, options):
38 options['password'] = self.generatePassword()
39
40 def install(self):
41 path_list = []
42
43 htpasswd_file = self.options['htpasswd-file']
44 # Create or empty the file
45 open(htpasswd_file, 'w').close()
46 path_list.append(htpasswd_file)
47
48 user = self.options['user']
49 password = self.options['password']
50 subprocess.check_call([self.options['apache-htpasswd'],
51 '-bc', htpasswd_file,
52 user, password
53 ])
54
55 htdocs_location = self.options['htdocs']
56 if not (os.path.exists(htdocs_location) and os.listdir(htdocs_location)):
57 try:
58 os.rmdir(htdocs_location)
59 except:
60 pass
61 shutil.copytree(self.options['source'], htdocs_location)
62
63 # Install php.ini
64 php_ini = self.createFile(os.path.join(self.options['php-ini-dir'],
65 'php.ini'),
66 self.substituteTemplate(self.getTemplateFilename('php.ini.in'),
67 dict(tmp_directory=self.options['tmp-dir']))
68 )
69 path_list.append(php_ini)
70
71 apache_config = dict(
72 pid_file=self.options['pid-file'],
73 lock_file=self.options['lock-file'],
74 davlock_db=self.options['davdb-lock'],
75 ip=self.options['ip'],
76 port_webdav=self.options['port_webdav'],
77 port_ajax=self.options['port_ajax'],
78 error_log=self.options['error-log'],
79 access_log=self.options['access-log'],
80 document_root=self.options['htdocs'],
81 modules_dir=self.options['apache-modules-dir'],
82 mime_types=self.options['apache-mime-file'],
83 server_root=self.options['root'],
84 email_address=self.options['email-address'],
85 htpasswd_file=htpasswd_file,
86 ssl_certificate=self.options['cert-file'],
87 ssl_key=self.options['key-file'],
88 php_ini_dir=self.options['php-ini-dir']
89 )
90
91 # Create logfiles
92 for log in [self.options['error-log'], self.options['access-log']]:
93 open(log, 'a').close()
94
95 config_file = self.createFile(self.options['conf-file'],
96 self.substituteTemplate(self.getTemplateFilename('httpd.conf.in'),
97 apache_config)
98 )
99 path_list.append(config_file)
100
101 wrapper = self.createPythonScript(self.options['wrapper'],
102 'slapos.recipe.librecipe.execute.execute',
103 [self.options['apache-binary'], '-f', config_file, '-DFOREGROUND'])
104 path_list.append(wrapper)
105
106 promise = self.createPythonScript(self.options['promise'],
107 __name__ + '.promise',
108 dict(host=self.options['ip'], port=int(self.options['port_webdav']),
109 user=self.options['user'], password=self.options['password'])
110 )
111 path_list.append(promise)
112
113 return path_list
114
115 def promise(args):
116 host = args['host']
117 port = args['port']
118 user = args['user']
119 password = args['password']
120
121 connection = httplib.HTTPSConnection(host, port)
122 auth = base64.b64encode('%s:%s' % (user, password))
123 connection.request('OPTIONS', '/',
124 headers=dict(
125 Authorization='Basic %s' % auth,
126 )
127 )
128 connection.getresponse()
129
130 return 0