PROJECT_MOVED -> https://lab.nexedi.com/nexedi/slapos
[slapos.git] / slapos / recipe / trac.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2010 Vifib SARL and Contributors. All Rights Reserved.
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsibility of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # guarantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 3
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #
27 ##############################################################################
28 import os
29 import sys
30 import subprocess
31 import shutil
32 import json
33
34 from slapos.recipe.librecipe import GenericBaseRecipe
35
36 class Recipe(GenericBaseRecipe):
37
38 def __init__(self, buildout, name, options):
39
40 pythonPath = ""
41
42 for eggs in options['eggs-dirs'].splitlines():
43 if eggs:
44 for item in os.listdir(eggs):
45 path = os.path.join(eggs, item)
46 pythonPath = path + ":" + pythonPath
47
48 options['python_path'] = pythonPath
49 options['wsgi-dir'] = os.path.join(options['site-dir'].strip(), 'apache')
50 options['git-dir'] = os.path.join(options['site-dir'].strip(), 'git')
51 options['svn-dir'] = os.path.join(options['site-dir'].strip(), 'svn')
52 return GenericBaseRecipe.__init__(self, buildout, name, options)
53
54 def install(self):
55 install_path = []
56
57 env = os.environ
58 env['LD_LIBRARY_PATH'] = self.options['python-lib']
59 project_dir = self.options['site-dir'].strip()
60 trac_admin = self.options['trac-admin'].strip()
61 admin = self.options['admin-user'].strip()
62 passwd = self.options['admin-password'].strip()
63 config = os.path.join(project_dir, 'conf/trac.ini')
64 filestat = self.options['file-status'].strip()
65 self.logger.info("Checking if trac project is not installed...")
66 if os.path.exists(filestat):
67 os.unlink(filestat)
68 if not os.path.exists(project_dir):
69 self.logger.info("Starting trac project installation at %s" % project_dir)
70 trac_args = [trac_admin, project_dir, 'initenv']
71 db_string = "mysql://%s:%s@%s:%s/%s" % (
72 self.options['mysql-username'].strip(),
73 self.options['mysql-password'].strip(),
74 self.options['mysql-host'].strip(),
75 self.options['mysql-port'].strip(),
76 self.options['mysql-database'].strip()
77 )
78 process_install = subprocess.Popen(trac_args, stdout=subprocess.PIPE,
79 stdin=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
80 process_install.stdin.write('%s\n%s\n' % (self.options['project'].strip(),
81 db_string))
82 result = process_install.communicate()[0]
83 process_install.stdin.close()
84 if process_install.returncode is None or process_install.returncode != 0:
85 if os.path.exists(project_dir):
86 shutil.rmtree(project_dir)
87 self.logger.error("Failed to initialize Trac.\nThe error was: %s" % result)
88 return []
89 os.mkdir(self.options['git-dir'])
90 os.mkdir(self.options['svn-dir'])
91 os.mkdir(self.options['wsgi-dir'])
92 os.unlink(config)
93 shutil.copy(self.options['trac-ini'].strip(), config)
94 shutil.copy(self.options['trac-wsgi'].strip(),
95 os.path.join(self.options['wsgi-dir'], 'trac.wsgi'))
96 else:
97 self.logger.info("The directory %s already exist, skip project installation"
98 % project_dir)
99 trac_args = [trac_admin, project_dir, 'repository', 'list']
100 process_upgrade = subprocess.Popen(trac_args, stdout=subprocess.PIPE,
101 stderr=subprocess.STDOUT)
102 result = process_upgrade.communicate()[0]
103 if process_upgrade.returncode is None or process_upgrade.returncode != 0:
104 self.logger.error("Failed to run Trac.\nThe error was: %s" % result)
105 return []
106
107 #Add All grant to admin user
108 self.logger.info("Granting admin rights to the admin user.")
109 trac_grant = [trac_admin, project_dir, "permission add %s TRAC_ADMIN" % admin]
110 process = subprocess.Popen(trac_grant, stdout=subprocess.PIPE,
111 stderr=subprocess.STDOUT, env=env)
112 result = process.communicate()[0]
113 if process.returncode is None or process.returncode != 0:
114 raise Exception("Failed to execute Trac-admin.\nThe error was: %s" % result)
115
116 self.logger.info("Copying additional plugins into plugins directory")
117 plugins_dir = os.path.join(project_dir, "plugins")
118 for item in os.listdir(self.options['plugins-egg-dir'].strip()):
119 source = os.path.join(self.options['plugins-egg-dir'].strip(), item)
120 destination = os.path.join(plugins_dir, item)
121 if not os.path.exists(destination):
122 shutil.copytree(source, destination)
123
124 svn_list = json.loads(self.options.get('svn-project-list', '{}'))
125 for svn_repo in svn_list:
126 svn_repo_path = os.path.join(self.options['svn-dir'], svn_repo)
127 if not os.path.exists(svn_repo_path):
128 self.logger.info("Initializing %s SVN repository..." % svn_repo)
129 svn_args = [self.options['svn-repo-script'], project_dir,
130 svn_repo, svn_list[svn_repo]]
131 process = subprocess.Popen(svn_args, stdout=subprocess.PIPE,
132 stderr=subprocess.STDOUT)
133 result = process.communicate()[0]
134 if process.returncode is None or process.returncode != 0:
135 shutil.rmtree(svn_repo_path)
136 raise Exception("Failed to create repository.\nThe error was: %s" % result)
137 shutil.copy(self.options['trac-svn-hook'].strip(),
138 os.path.join(svn_repo_path, 'hooks/post-commit'))
139 shutil.copy(self.options['post-revprop-change'].strip(),
140 os.path.join(svn_repo_path, 'hooks/post-revprop-change'))
141 self.logger.info("Finished initializing %s reposiroty" % svn_repo)
142
143 repolist = json.loads(self.options.get('git-project-list', '{}'))
144 for repo, desc in repolist.iteritems():
145 absolute_path = os.path.join(self.options['git-dir'], '%s.git' % repo)
146 if not os.path.exists(absolute_path):
147 self.logger.info("Initializing %s GIT repository..." % repo)
148 subprocess.check_call([self.options['git-binary'], 'init',
149 '--bare', absolute_path])
150 subprocess.check_call([trac_admin, project_dir, 'repository',
151 'add', repo, absolute_path, 'git'], env=env)
152 subprocess.check_call([trac_admin, project_dir, 'repository',
153 'resync', repo], env=env)
154 # XXX: Hardcoded path
155 shutil.copy(self.options['trac-git-hook'].strip(),
156 os.path.join(absolute_path, 'hooks/post-commit'))
157 description_filename = os.path.join(absolute_path, 'description')
158 with open(description_filename, 'w') as description_file:
159 description_file.write(desc)
160
161 user_list = json.loads(self.options.get('user-list', '{}'))
162 fd = open(os.path.join(project_dir, 'svnpasswd'), 'w')
163 fd.write("[users]\n%s = %s" % (admin, passwd))
164 os.system("%s -cb %s %s %s" % (self.options['htpasswd'],
165 self.options['passwd-file'],
166 admin, passwd)
167 )
168 for user in user_list:
169 self.logger.info("Creating or updating user %s ..." % user)
170 user_args = [self.options['htpasswd'], '-b', self.options['passwd-file'],
171 user, user_list[user]]
172 process = subprocess.Popen(user_args, stdout=subprocess.PIPE,
173 stderr=subprocess.STDOUT)
174 result = process.communicate()[0]
175 if process.returncode is None or process.returncode != 0:
176 raise Exception("Failed to create user %s.\nThe error was: %s" %
177 (user, result))
178 fd.write("\n%s = %s" % (user, user_list[user]))
179 fd.close()
180 open(filestat, "w").write("done.")
181
182 return install_path
183