Class: Project

Inherits:
Object
  • Object
show all
Defined in:
lib/projektbauer.rb

Instance Method Summary collapse

Constructor Details

#initializeProject

The class constructor



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/projektbauer.rb', line 61

def initialize

  @server_port_nossl = nil
  @server_port_ssl = nil
  @server_root = "xxx"


  @project_name        = nil  # name of project
  @virtual_host        = nil  # set of the project
  @project_realm       = nil  # realm for authorization
  @project_admin_user  = nil  # admin - user
  @server_admin        = nil  # email of server admin
  @smtp_enabled        = false # email is not supported by default

  @trac_admin          = "trac-admin"  # the command to trac-admin

end

Instance Method Details

#create_allNil

calling this method after setting all attributes does the entire generation job

Returns:

  • (Nil)

    no return



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/projektbauer.rb', line 339

def create_all
  init_filenames
  create_folders

  create_index_html

  save_virtual_host_httpd_conf
  save_project_location_httpd_conf
  update_virtual_host_locations_httpd_conf
  update_include_virtual_hosts_httpd_conf

  create_htpasswd
  create_svn_authz_file
  create_svn

  create_trac
end

#create_foldersnil

Creates the folders in the file system. If a folder already exists, nothing happens.

Returns:

  • (nil)

    no return



116
117
118
119
120
121
122
123
124
# File 'lib/projektbauer.rb', line 116

def create_folders
  #todo: double check if path exists
  FileUtils.mkdir_p @_project_svn_dir
  FileUtils.mkdir_p @_project_trac_dir
  FileUtils.mkdir_p @_document_root_dir
  #todo: ensure that folders are not listable
  #todo: htaccess - file
  nil
end

#create_htpasswdNil

This creates the htdigest file

Returns:

  • (Nil)

    no return



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/projektbauer.rb', line 287

def create_htpasswd

  allusers = @project_users.map{|k,v| v}.flatten
  pwdhashes = {}
  passwords = {}
  allusers.each{|user|
    password = (0...10).map{ ('a'..'z').to_a[rand(26)] }.join
    pwdhashes[user] = _create_passwdentry(@project_realm, user, password)
    passwords[user] = password
  }

  if File.exists?(@_project_auth_user_file) then
    oldcontents = File.open(@_project_auth_user_file).readlines
    oldcontents = oldcontents.map{|i| i.strip }.sort.uniq

    oldentries={}
    oldcontents.each{|entry|

      record = entry.split(":")
      if pwdhashes.has_key?(record.first) then
        pwdhashes[record.first] = entry
        passwords[record.first] = "<password unchanged>"
      else
        passwords[record.first] = "<user deleted>"
      end
    }
  else
    oldcontents=[]
  end

  newcontents=pwdhashes.map{|k,v| v }.sort.uniq
  unless oldcontents==newcontents
    File.open(@_project_auth_user_file, "w"){|f|
      f.puts(newcontents.join("\n"))
    }
  end

  File.open(@_project_auth_user_file+".txt", "a"){|f|
    f.puts "generated passwords for #{@project_realm}"
    f.puts ""
    f.puts(passwords.map{|k,v| "#{k} => #{v}"}.sort.join("\n"))
  }

end

#create_index_htmlNil

Creates a minimalistic index.html in the virtual host. In particular this avoids that the files can be listed.

Returns:

  • (Nil)

    no return



182
183
184
185
186
187
# File 'lib/projektbauer.rb', line 182

def create_index_html
  config = expand_erb("index_html.erb")
  File.open(@_index_html_file, "w") {|f|
    f.puts config
  }
end

#create_svntype

This creates the svn repository if it does not yet exist. Note that this is tested by investigating the format folder in the svn repository.

todo: improve the detection

Returns:

  • (type)
    description


269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/projektbauer.rb', line 269

def create_svn
  unless File.exists?(@_project_svn_dir + "/format")
    cmd = "svnadmin create \"#{@_project_svn_dir}\""
    puts cmd
    `#{cmd}`
    initialfolders= ["tags", "branches", "trunk"]
    cmd = ["svn mkdir",
           initialfolders.map{|d| "file://\"#{@_project_svn_dir}\"/#{d}"},
           "-m \"Repository created by Projektbauer\""].flatten.join(" ")
    puts cmd
    `#{cmd}`
  end
end

#create_svn_authz_fileNil

creates the Authorization file for svn. Thereby it creates an observer and a contributor group. the project_admin_user is entered as contributor

Returns:

  • (Nil)

    no return



166
167
168
169
170
171
172
173
# File 'lib/projektbauer.rb', line 166

def create_svn_authz_file
  config =expand_erb("svn_authz_file.erb")

  File.open(@_project_svn_authz_file, "w") {|f|
    f.puts config
  }
  nil
end

#create_tractype

This creates the trac environment

Returns:

  • (type)
    description


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/projektbauer.rb', line 222

def create_trac
  quoted_project_trac_dir = "\"#{@_project_trac_dir}\""

  unless File.exists?("#{@_project_trac_dir}/conf")
    FileUtils.mkdir_p(@_project_trac_dir)
    _trac_admin "#{quoted_project_trac_dir} initenv \"#{@project_name}\" sqlite:\"#{@_project_trac_dir}/db/trac.db\""
    FileUtils.rm(@_trac_ini_file)  # remove this such that it gets regenerated
  end

  _update_trac_ini

  # create group for project.admins
  _trac_admin "#{quoted_project_trac_dir} permission add project.admins TRAC_ADMIN TICKET_ADMIN WIKI_ADMIN"


  @project_users[:admin].each{|u|
    _trac_admin "#{quoted_project_trac_dir} permission add #{u} project.admins"
  }
  _trac_admin "#{quoted_project_trac_dir} permission add project.contributors TRAC_ADMIN TICKET_ADMIN WIKI_ADMIN"
  anonymous_revoke="BROWSER_VIEW CHANGESET_VIEW FILE_VIEW LOG_VIEW MILESTONE_VIEW REPORT_SQL_VIEW"
  anonymous_revoke <<" REPORT_VIEW ROADMAP_VIEW SEARCH_VIEW TICKET_VIEW TIMELINE_VIEW WIKI_VIEW"

  _trac_admin "#{quoted_project_trac_dir} permission remove anonymous #{anonymous_revoke}"
  _trac_admin "#{quoted_project_trac_dir} permission add authenticated #{anonymous_revoke}"

  # crate include-file for webserver
  #
  _trac_admin "#{quoted_project_trac_dir} deploy #{quoted_project_trac_dir}"

  cmd = "chmod +x #{@_project_trac_dir}/cgi-bin/*"
  `#{cmd}`
  # link to the svn repository
  # 
  _trac_admin "#{quoted_project_trac_dir} repository add \"(default)\" \"#{@_project_svn_dir}\" svn"

  _create_post_commit
end

#init_filenamesnil

Inititalizes filenames etc. Needs to be called whenever one or more of the attributes are changed.

Returns:

  • (nil)

    no return



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/projektbauer.rb', line 85

def init_filenames

  #todo add checks for the attributes
  # no spaces in project_name
  #

  @_virtual_host_home_dir               = "#{@server_root}/#{@virtual_host}"
  @_include_virtual_hosts_httpd_conf    = "#{@server_root}/include_virtual_hosts.httpd.conf"
  @_project_home                        = "#{@_virtual_host_home_dir}/#{@project_name}"
  @_virtual_host_httpd_conf             = "#{@_virtual_host_home_dir}/virtual_host.httpd.conf"
  @_virtual_host_locations_httpd_conf   = "#{@_virtual_host_home_dir}/include_locations.httpd.conf"
  @_project_location_httpd_conf         = "#{@_project_home}/#{@project_name}.httpd.conf"
  @_project_svn_dir                     = "#{@_project_home}/svn"
  @_project_trac_dir                    = "#{@_project_home}/trac"
  @_project_auth_user_file              = "#{@_project_home}/#{@project_realm}.htdigest"
  @_project_svn_authz_file              = "#{@_project_home}/#{@project_name}.dav_svn_authz"
  @_document_root_dir                   = "#{@_virtual_host_home_dir}/www"
  @_index_html_file                     = "#{@_document_root_dir}/index.html"
  @_trac_admin                          = "#{@trac_admin}"
  @_trac_ini_file                       = "#{@_project_trac_dir}/conf/trac.ini"
  nil
end

#save_project_location_httpd_confNil

create the apache configuration for the <location> It corresponds to a project.

Returns:

  • (Nil)

    no return



149
150
151
152
153
154
155
156
157
# File 'lib/projektbauer.rb', line 149

def save_project_location_httpd_conf
  config = expand_erb("project_location_httpd_conf.erb")

  File.open(@_project_location_httpd_conf,"w"){|f|
    f.puts config
  }

  nil
end

#save_virtual_host_httpd_confNil

This basically creates the configuration of the virtual host

Returns:

  • (Nil)

    no return



133
134
135
136
137
138
139
140
141
# File 'lib/projektbauer.rb', line 133

def save_virtual_host_httpd_conf
  config = expand_erb("virtual_host_httpd_conf.erb")

  File.open(@_virtual_host_httpd_conf,"w"){|f|
    f.puts config
  }

  nil
end

#update_include_virtual_hosts_httpd_confNil

Updates the include file with the virtual hosts. This file needs to be added manually to ‘/etc/apache2/apache2.conf`

Returns:

  • (Nil)

    no return



209
210
211
212
213
214
# File 'lib/projektbauer.rb', line 209

def update_include_virtual_hosts_httpd_conf
  _maintain_include_file(@_include_virtual_hosts_httpd_conf,
                         @_project_location_httpd_conf
                         )
  nil
end

#update_virtual_host_locations_httpd_confNil

Updates the include file with the locations. This file is subsequently included in the virtual hosts.

Duplicate entries are removed.

Returns:

  • (Nil)

    no return



197
198
199
200
201
# File 'lib/projektbauer.rb', line 197

def update_virtual_host_locations_httpd_conf
  _maintain_include_file(@_virtual_host_locations_httpd_conf,
                         @_project_location_httpd_conf
                         )
end