Add Git Repository support to TRAC
[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 project_dir = self.options['site-dir'].strip()
58 trac_admin = self.options['trac-admin'].strip()
59 admin = self.options['admin-user'].strip()
60 passwd = self.options['admin-password'].strip()
61 config = os.path.join(project_dir, 'conf/trac.ini')
62 filestat = self.options['file-status'].strip()
63 self.logger.info("Checking if trac project is not installed...")
64 if os.path.exists(filestat):
65 os.unlink(filestat)
66 if not os.path.exists(project_dir):
67 self.logger.info("Starting trac project installation at %s" % project_dir)
68 trac_args = [trac_admin, project_dir, 'initenv']
69 db_string = "mysql://%s:%s@%s:%s/%s" % (
70 self.options['mysql-username'].strip(),
71 self.options['mysql-password'].strip(),
72 self.options['mysql-host'].strip(),
73 self.options['mysql-port'].strip(),
74 self.options['mysql-database'].strip()
75 )
76 process_install = subprocess.Popen(trac_args, stdout=subprocess.PIPE,
77 stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
78 process_install.stdin.write('%s\n%s\n' % (self.options['project'].strip(),
79 db_string))
80 result = process_install.communicate()[0]
81 process_install.stdin.close()
82 if process_install.returncode is None or process_install.returncode != 0:
83 if os.path.exists(project_dir):
84 shutil.rmtree(project_dir)
85 self.logger.error("Failed to initialize Trac.\nThe error was: %s" % result)
86 return []
87 os.mkdir(self.options['wsgi-dir'])
88 os.mkdir(self.options['git-dir'])
89 os.mkdir(self.options['svn-dir'])
90 os.unlink(config)
91 shutil.copy(self.options['trac-ini'].strip(), config)
92 shutil.copy(self.options['trac-wsgi'].strip(),
93 os.path.join(self.options['wsgi-dir'], 'trac.wsgi'))
94 else:
95 self.logger.info("The directory %s already exist, skip project installation"
96 % project_dir)
97 trac_args = [trac_admin, project_dir, 'upgrade']
98 process_upgrade = subprocess.Popen(trac_args, stdout=subprocess.PIPE,
99 stderr=subprocess.STDOUT)
100 result = process_upgrade.communicate()[0]
101 if process_upgrade.returncode is None or process_upgrade.returncode != 0:
102 self.logger.error("Failed to upgrade Trac.\nThe error was: %s" % result)
103 return []
104
105 #Add All grant to admin user
106 self.logger.info("Granting admin rights to the admin user.")
107 trac_grant = [trac_admin, project_dir, "permission add %s TRAC_ADMIN" % admin]
108 process = subprocess.Popen(trac_grant, stdout=subprocess.PIPE,
109 stderr=subprocess.STDOUT)
110 result = process.communicate()[0]
111 if process.returncode is None or process.returncode != 0:
112 raise Exception("Failed to execute Trac-admin.\nThe error was: %s" % result)
113
114 self.logger.info("Copying additional plugins into plugins directory")
115 plugins_dir = os.path.join(project_dir, "plugins")
116 for item in os.listdir(self.options['plugins-egg-dir'].strip()):
117 source = os.path.join(self.options['plugins-egg-dir'].strip(), item)
118 destination = os.path.join(plugins_dir, item)
119 if not os.path.exists(destination):
120 shutil.copytree(source, destination)
121
122 svn_list = json.loads(self.options.get('svn-project-list', '{}'))
123 for svn_repo in svn_list:
124 svn_repo_path = os.path.join(self.options['svn-dir'], svn_repo)
125 if not os.path.exists(svn_repo_path):
126 self.logger.info("Initializing %s SVN repository..." % svn_repo)
127 svn_args = [self.options['svn-repo-script'], project_dir,
128 svn_repo, svn_list[svn_repo]]
129 process = subprocess.Popen(svn_args, stdout=subprocess.PIPE,
130 stderr=subprocess.STDOUT)
131 result = process.communicate()[0]
132 if process.returncode is None or process.returncode != 0:
133 shutil.rmtree(svn_repo_path)
134 raise Exception("Failed to create repository.\nThe error was: %s" % result)
135 shutil.copy(self.options['trac-svn-hook'].strip(),
136 os.path.join(svn_repo_path, 'hooks/post-commit'))
137 shutil.copy(self.options['post-revprop-change'].strip(),
138 os.path.join(svn_repo_path, 'hooks/post-revprop-change'))
139 self.logger.info("Finished initializing %s reposiroty" % svn_repo)
140
141 repolist = json.loads(self.options.get('git-project-list', '{}'))
142 for repo, desc in repolist.iteritems():
143 absolute_path = os.path.join(self.options['git-dir'], '%s.git' % repo)
144 if not os.path.exists(absolute_path):
145 self.logger.info("Initializing %s GIT repository..." % repo)
146 subprocess.check_call([self.options['git-binary'], 'init',
147 '--bare', absolute_path])
148 subprocess.check_call([trac_admin, project_dir, 'repository',
149 'add', repo, absolute_path, 'git'])
150 subprocess.check_call([trac_admin, project_dir, 'repository',
151 'resync', repo])
152 # XXX: Hardcoded path
153 shutil.copy(self.options['trac-git-hook'].strip(),
154 os.path.join(absolute_path, 'hooks/post-commit'))
155 description_filename = os.path.join(absolute_path, 'description')
156 with open(description_filename, 'w') as description_file:
157 description_file.write(desc)
158
159 user_list = json.loads(self.options.get('user-list', '{}'))
160 fd = open(os.path.join(project_dir, 'svnpasswd'), 'w')
161 fd.write("[users]\n%s = %s" % (admin, passwd))
162 os.system("%s -cb %s %s %s" % (self.options['htpasswd'],
163 self.options['passwd-file'],
164 admin, passwd)
165 )
166 for user in user_list:
167 self.logger.info("Creating or updating user %s ..." % user)
168 user_args = [self.options['htpasswd'], '-b', self.options['passwd-file'],
169 user, user_list[user]]
170 process = subprocess.Popen(user_args, stdout=subprocess.PIPE,
171 stderr=subprocess.STDOUT)
172 result = process.communicate()[0]
173 if process.returncode is None or process.returncode != 0:
174 raise Exception("Failed to create user %s.\nThe error was: %s" %
175 (user, result))
176 fd.write("\n%s = %s" % (user, user_list[user]))
177 fd.close()
178 open(filestat, "w").write("done.")
179
180 return install_path
181