Error   test

Automated Project Creation

Posted June 29, 2007 at 05:06pm in Programming

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.

  1. #!/usr/bin/env ruby
  2. #
  3. # Usage
  4. #
  5. # script <dir> <project name> <htdigest>
  6. #
  7. # Example:
  8. # —————————
  9. # script test "Test Project" htdigest
  10. #
  11. #       - This will create a directory named test in both
  12. #         your repository and trac directories.  The htdigest
  13. #         parameter tells it to write to your htdigest file.
  14. #
  15.  
  16. require ‘digest/md5′
  17. user = Hash.new
  18. perm = Hash.new
  19.  
  20. #
  21. # Changes these locations to fit your environment
  22. #
  23. repo_loc = ‘C:/DEV/repo/’                   # Directory for the SVN Repo
  24. trac_loc = ‘C:/DEV/trac/’                   # Directory for the Trac Project
  25. svn_loc = ‘E:/Subversion/bin/’              # Subversion Bin Directory
  26. py_loc = ‘C:/Programs/Python24/’            # Python Directory
  27. htd_loc = ‘D:/DEV/trac/htdigest’            # HTDigest File Location
  28. tmpl_loc = py_loc + ’share/trac/templates’  # Template Directory
  29.  
  30. #
  31. # Repository Configuration
  32. #
  33. conf = <<eof
  34. [general]
  35. anon-access = none
  36. auth-access = write
  37. password-db = passwd
  38. # authz-db = authz
  39. # realm = My First Repository
  40. EOF
  41.  
  42. #
  43. # Trac and Apache users and permissions
  44. #
  45. user[‘manis’] = ‘test’                      # Key is username, value is password
  46. perm[‘manis’] = ‘TRAC_ADMIN’                # Permission in Trac for user
  47.  
  48. #
  49. # In most cases these will not need to be changed
  50. #
  51. realm = ‘trac’                              # Realm for HTDigest
  52. tracdb = ’sqlite:db/trac.db’                # Trac Database Location
  53. repostype = ’svn’                           # Type of Repository for Trac
  54. x_py = py_loc + ‘python ‘                   # Python Executable
  55. x_svn = svn_loc + ’svnadmin create ‘        # Subversion admin + create command
  56. x_trac = py_loc + ‘Scripts/trac-admin ‘     # Trac Admin script
  57. initstring =                              # Initializing (Leave Empty)
  58. htdigest =                                # Initializing (Leave Empty)
  59. passwd = "[users]\\n"                        # Initializing (Leave Empty)
  60.  
  61. #
  62. # User Input
  63. #
  64. projpath = ARGV[0]                          # Name of Directory for Trac/SVN
  65. projname = ARGV[1]                          # Project Name for Trac
  66.  
  67. #
  68. # Build Paths, Commands & Trac Environment Initialization String
  69. #
  70. repospath = repo_loc + projpath
  71. tracspath = trac_loc + projpath
  72. templatepath = tmpl_loc
  73. initstring += ‘"’ + projname + ‘"’ + ‘ ‘
  74. initstring += ‘"’ + tracdb + ‘"’ + ‘ ‘
  75. initstring += ‘"’ + repostype + ‘"’ + ‘ ‘
  76. initstring += ‘"’ + repospath + ‘"’ + ‘ ‘
  77. initstring += ‘"’ + templatepath + ‘"’
  78. svn_x = x_svn + repospath
  79. trac_x = x_py + x_trac + tracspath + ‘ initenv ‘ + initstring
  80.  
  81. #
  82. # Execute Commands
  83. #
  84. # This block will check if the svn or trac location already exists and cancel
  85. # if it does.  After executing each command it will loop through the user hash
  86. # and add each user with linked permissions to the trac environment.  If you
  87. # included the parameter ‘htdigest’ after the project name the string created
  88. # during the loop through users will add the new users to the HTDigest file you
  89. # specified above.
  90. #
  91. if (File::exists?(repospath) || File::exists?(tracspath))
  92.     puts ‘Project Exists’
  93. else
  94.     svnb = system(svn_x + ‘ > project.log’)
  95.     tracb = system(trac_x + ‘ > project.log’)
  96.     if svnb && tracb
  97.         puts "Repository Created"
  98.         puts "Trac Environment Created"
  99.         user.each_pair do |k1, v1|
  100.             system(x_py + x_trac + tracspath + ‘ permission add ‘ + k1 + ‘ ‘ + perm[k1].to_s)
  101.             htdigest += k1 + ‘:’ + realm + ‘:’ + Digest::MD5.new.hexdigest(k1 + ‘:’ + realm + ‘:’ + v1) + "\\n"
  102.             passwd += k1 + ‘ = ‘ + v1
  103.         end
  104.         if (ARGV[2] && ARGV[2] == ‘htdigest’)
  105.             open(htd_loc, "a") { |f| f.puts htdigest }
  106.         end
  107.         open(repospath+‘/conf/passwd’, "w") { |f| f.puts passwd }
  108.         open(repospath+‘/conf/svnserve.conf’, "w") { |f| f.puts conf }
  109.     else
  110.         puts "An Error Occurred"
  111.     end
  112. end

Here is my apache config for Trac

Apache [Show Plain Code]:
  1. <virtualHost 10.0.10.22>
  2.   SetHandler mod_python
  3.   PythonHandler trac.web.modpython_frontend
  4.   PythonOption TracEnvParentDir D:/DEV/trac/
  5.   PythonOption TracUriRoot /
  6.         PythonOption TracEnvIndexTemplate D:/DEV/trac/index.tmpl
  7.         ServerName trac.myserver.com
  8.         ErrorLog D:/DEV/logs/apache.trac.errors.log
  9.         CustomLog D:/DEV/logs/apache.trac.referer.log referer
  10.         CustomLog D:/DEV/logs/apache.trac.agent.log agent
  11.         CustomLog D:/DEV/logs/apache.trac.access.log combinedio
  12.         CustomLog D:/DEV/logs/apache.trac.deflate.log deflate
  13.         <locationMatch "/[^/]+/login">
  14.           AllowOverride All
  15.           AuthType Digest
  16.           AuthName "trac"
  17.           AuthDigestFile "D:/DEV/trac/htdigest"
  18.           Require valid-user
  19.         </locationMatch>
  20. </virtualHost>
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Google
  • del.icio.us
  • Digg
  • e-mail
  • Spurl
  • Facebook


Leave a Reply