Automated Project Creation
Here is the script I mentioned the other day. For the most part this is my first Ruby script. I have done a few little tests to see how some things work but nothing more than 5 lines. For that reason please don’t be too harsh if I did something incorrect, but I would like some input.
Basically if you have this script named ‘project.rb’ you would enter:
project testproject "Test Project" htdigest
What that is doing is creating a repository called ‘testproject’ and a trac environment called ‘testproject’ in the directory you specify at the top of the script. It will set the Trac project name to ‘Test Project’ and if the htdigest argument is present it will add the users in the script to the HTDigest file you are using for trac. This script will add the users you specify to the trac environment along with the permissions you think they should have. It will also add those users to your repository passwd file and alter your svnserve.conf file.
One thing it is not doing is altering the trac.ini file. I debate implementing it, and I will need to look into the ways you process INI files in Ruby before I do.
I must also note that with this configuration I am using one virtual host and location for trac environments in Apache. Since this is my local stuff I don’t have a lot of users to be accessing and they will not be changing much from project to project. Below is a screenshot of how my project list looks using the index.tmpl you can download here. You can also download the RB file for this script from here. Also for some reason the highlighting is all funky, but it is still readable.

-
#!/usr/bin/env ruby
-
#
-
# Usage
-
#
-
# script <dir> <project name> <htdigest>
-
#
-
# Example:
-
# —————————
-
# script test "Test Project" htdigest
-
#
-
# - This will create a directory named test in both
-
# your repository and trac directories. The htdigest
-
# parameter tells it to write to your htdigest file.
-
#
-
-
require ‘digest/md5′
-
user = Hash.new
-
perm = Hash.new
-
-
#
-
# Changes these locations to fit your environment
-
#
-
repo_loc = ‘C:/DEV/repo/’ # Directory for the SVN Repo
-
trac_loc = ‘C:/DEV/trac/’ # Directory for the Trac Project
-
svn_loc = ‘E:/Subversion/bin/’ # Subversion Bin Directory
-
py_loc = ‘C:/Programs/Python24/’ # Python Directory
-
htd_loc = ‘D:/DEV/trac/htdigest’ # HTDigest File Location
-
tmpl_loc = py_loc + ’share/trac/templates’ # Template Directory
-
-
#
-
# Repository Configuration
-
#
-
conf = <<eof
-
[general]
-
anon-access = none
-
auth-access = write
-
password-db = passwd
-
# authz-db = authz
-
# realm = My First Repository
-
EOF
-
-
#
-
# Trac and Apache users and permissions
-
#
-
user[‘manis’] = ‘test’ # Key is username, value is password
-
perm[‘manis’] = ‘TRAC_ADMIN’ # Permission in Trac for user
-
-
#
-
# In most cases these will not need to be changed
-
#
-
realm = ‘trac’ # Realm for HTDigest
-
tracdb = ’sqlite:db/trac.db’ # Trac Database Location
-
repostype = ’svn’ # Type of Repository for Trac
-
x_py = py_loc + ‘python ‘ # Python Executable
-
x_svn = svn_loc + ’svnadmin create ‘ # Subversion admin + create command
-
x_trac = py_loc + ‘Scripts/trac-admin ‘ # Trac Admin script
-
initstring = ” # Initializing (Leave Empty)
-
htdigest = ” # Initializing (Leave Empty)
-
passwd = "[users]\n" # Initializing (Leave Empty)
-
-
#
-
# User Input
-
#
-
projpath = ARGV[0] # Name of Directory for Trac/SVN
-
projname = ARGV[1] # Project Name for Trac
-
-
#
-
# Build Paths, Commands & Trac Environment Initialization String
-
#
-
repospath = repo_loc + projpath
-
tracspath = trac_loc + projpath
-
templatepath = tmpl_loc
-
initstring += ‘"’ + projname + ‘"’ + ‘ ‘
-
initstring += ‘"’ + tracdb + ‘"’ + ‘ ‘
-
initstring += ‘"’ + repostype + ‘"’ + ‘ ‘
-
initstring += ‘"’ + repospath + ‘"’ + ‘ ‘
-
initstring += ‘"’ + templatepath + ‘"’
-
svn_x = x_svn + repospath
-
trac_x = x_py + x_trac + tracspath + ‘ initenv ‘ + initstring
-
-
#
-
# Execute Commands
-
#
-
# This block will check if the svn or trac location already exists and cancel
-
# if it does. After executing each command it will loop through the user hash
-
# and add each user with linked permissions to the trac environment. If you
-
# included the parameter ‘htdigest’ after the project name the string created
-
# during the loop through users will add the new users to the HTDigest file you
-
# specified above.
-
#
-
if (File::exists?(repospath) || File::exists?(tracspath))
-
puts ‘Project Exists’
-
else
-
svnb = system(svn_x + ‘ > project.log’)
-
tracb = system(trac_x + ‘ > project.log’)
-
if svnb && tracb
-
puts "Repository Created"
-
puts "Trac Environment Created"
-
user.each_pair do |k1, v1|
-
system(x_py + x_trac + tracspath + ‘ permission add ‘ + k1 + ‘ ‘ + perm[k1].to_s)
-
htdigest += k1 + ‘:’ + realm + ‘:’ + Digest::MD5.new.hexdigest(k1 + ‘:’ + realm + ‘:’ + v1) + "\n"
-
passwd += k1 + ‘ = ‘ + v1
-
end
-
if (ARGV[2] && ARGV[2] == ‘htdigest’)
-
open(htd_loc, "a") { |f| f.puts htdigest }
-
end
-
open(repospath+‘/conf/passwd’, "w") { |f| f.puts passwd }
-
open(repospath+‘/conf/svnserve.conf’, "w") { |f| f.puts conf }
-
else
-
puts "An Error Occurred"
-
end
-
end
Here is my apache config for Trac
-
<virtualHost 10.0.10.22>
-
SetHandler mod_python
-
PythonHandler trac.web.modpython_frontend
-
PythonOption TracEnvParentDir D:/DEV/trac/
-
PythonOption TracUriRoot /
-
PythonOption TracEnvIndexTemplate D:/DEV/trac/index.tmpl
-
ServerName trac.myserver.com
-
ErrorLog D:/DEV/logs/apache.trac.errors.log
-
CustomLog D:/DEV/logs/apache.trac.referer.log referer
-
CustomLog D:/DEV/logs/apache.trac.agent.log agent
-
CustomLog D:/DEV/logs/apache.trac.access.log combinedio
-
CustomLog D:/DEV/logs/apache.trac.deflate.log deflate
-
<locationMatch "/[^/]+/login">
-
AllowOverride All
-
AuthType Digest
-
AuthName "trac"
-
AuthDigestFile "D:/DEV/trac/htdigest"
-
Require valid-user
-
</locationMatch>
-
</virtualHost>





