lib/subtrac.rb in ktec-subtrac-0.1.28 vs lib/subtrac.rb in ktec-subtrac-0.1.33

- old
+ new

@@ -24,26 +24,23 @@ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# You may need these... -# -# sudo apt-get install ruby rubygems -# sudo gem sources -a http://gems.github.com -# sudo gem install visionmedia-commander -# sudo gem install yaml erb fileutils -# require 'yaml' require 'erb' require 'fileutils' require 'subtrac/version' require 'subtrac/commands' +require 'subtrac/client' +require 'subtrac/project' +require 'subtrac/apache' +require 'subtrac/svn' +require 'subtrac/trac' +require 'subtrac/core_ext' - module Subtrac SUBTRAC_ROOT = "#{File.dirname(__FILE__)}/" unless defined?(SUBTRAC_ROOT) SUBTRAC_ENV = (ENV['SUBTRAC_ENV'] || 'development').dup unless defined?(SUBTRAC_ENV) USER_CONFIG_FILE = 'config/user.yml' @@ -72,23 +69,31 @@ end def default_client=(name) @APP_CONFIG[:default_client] = @default_client = name end - + def default_project @default_project ||= @APP_CONFIG[:default_project] end def default_project=(name) puts "Updating default_project to #{name}" @APP_CONFIG[:default_project] = @default_project = name end + def default_project_type + @default_project_type ||= @APP_CONFIG[:default_project_type] + end - # Filesystem directories + def default_project_type=(name) + puts "Updating default_project_type to #{name}" + @APP_CONFIG[:default_project_type] = @default_project_type = name + end + + # Filesystem directories def root Pathname.new(SUBTRAC_ROOT) if defined?(SUBTRAC_ROOT) end def public_path @@ -143,17 +148,19 @@ def locations_dir @locations_dir ||= File.join(install_dir,@APP_CONFIG[:dirs][:locations]) end # Urls - def svn_url - @svn_url ||= "http://#{server_hostname}#{@APP_CONFIG[:urls][:svn]}" + @svn_url ||= @APP_CONFIG[:urls][:svn] end - + + def trac_permissions + @trac_permissions ||= @APP_CONFIG[:trac][:permissions] + end + # LDAP - def enable_ldap @enable_ldap ||= @APP_CONFIG[:ldap][:enable] end def enable_ldap=(value) @@ -183,11 +190,11 @@ def ldap_bind_host=(value) @APP_CONFIG[:ldap][:bind_host] = @ldap_bind_password = value end end - + # Loads the configuration YML file def self.load_config # TODO: We need to refactor this code so it will load the default configuration only if one has not been created puts "\n==== Loading configuration file ====" # load configuration file @@ -209,11 +216,19 @@ raise StandardError, "config/config.yml does not exist." end say("Configuration loaded successfully...") end + # Write the changes configuration to disk + def self.save_config + # save the things that might have changed + file_path = File.join(subtrac_path, USER_CONFIG_FILE) + user_config = {SUBTRAC_ENV => @APP_CONFIG} + open(file_path, 'w') {|f| YAML.dump(user_config, f)} + end + # Install def self.install(args,options) puts "\n==== Installing development server files ====" if options.defaults then @@ -247,92 +262,79 @@ server name: #{server_name} server hostname: #{server_hostname} default client: #{default_client} default project: #{default_project}") - confirm = agree("Is this ok? [y/n]") + confirm = agree("Is this ok? [y/n]") exit 0 if !confirm create_environment_directories(overwrite) - create_virtual_host() + #create_virtual_host() + apache = Apache.new(binding) + apache.create_virtual_host() + install_common_files() - install_default_theme() configure_admin_user() - create_project(default_project,default_client) + + # create default project + project = create_project(default_project,default_client) + # get these out for binding...we'll tidy up later + client = project.client + + # create the apache configuration + apache.create_project(project) + + # make sure apache can operate on these files + `sudo chown -R www-data:www-data #{install_dir}` + + # store any user preferences for later use save_config() end + + def self.create_project(project,client) + + # create default project + project = Project.new(project,client,default_project_type) + + # get these out for binding...we'll tidy up later + client = project.client + # create the svn repo + svn = Svn.new(svn_dir, binding) + svn.create_project(project) + + # create the trac site + trac = Trac.new(trac_dir, binding) + trac.create_project(project) + + # return the project + project + + end + private # confirms the current value with the user or accepts a new value if required def self.confirm_or_replace_value(prop, name) method(prop).call # initialise the property current_value = instance_variable_get("@#{prop}") - accept_default = agree("The default value for #{prop} is \"#{current_value}\". Is this ok? [y/n]") + accept_default = agree("The default value for #{prop} @#{prop} is \"#{current_value}\". Is this ok? [y/n]") if !accept_default then answer = ask("What would you like to change it to?") send("#{prop}=", answer) #instance_variable_set(prop, answer) end end - # Write the changes configuration to disk - def self.save_config - # save the things that might have changed - file_path = File.join(subtrac_path, USER_CONFIG_FILE) - user_config = {SUBTRAC_ENV => @APP_CONFIG} - open(file_path, 'w') {|f| YAML.dump(user_config, f)} - end - - # creates a directory if it does not exist - def self.create_if_missing(*names) - names.each do |name| - unless File.directory?(name) - puts "Creating directory called #{names}..." - FileUtils.mkdir_p(name) - end - end - end - - # publishes an erb template - def self.parse_template(infile,outfile,binding) - file = File.open(outfile, 'w+') - if file - template = ERB.new(IO.read(infile)) - if template - file.syswrite(template.result(binding)) - else - raise "Could not read template. file #{infile}" - end - else - raise "Unable to open file for writing. file #{outfile}" - end - end - - # creates a new virtual host and reloads apache and enables the new virtual host - def self.create_virtual_host - puts "\n==== Creating new virtual host ====" - vhost_template = File.join(subtrac_path, @APP_CONFIG[:templates][:virtual_host]) - puts "group apache virtual host template: #{vhost_template}" - # TODO: Copy this to the conf folder and map a file with an Include to it. - # this way the vhost file will get backed up as well. - new_vhost = File.join(apache_conf_dir,server_hostname) - puts "group apache file: #{new_vhost}" - parse_template(vhost_template,new_vhost,binding) if SUBTRAC_ENV != 'test' - # reload apache configuration - `/etc/init.d/apache2 force-reload` if SUBTRAC_ENV != 'test' - `a2ensite #{server_hostname}` if SUBTRAC_ENV != 'test' - end - def self.install_common_files puts "\n==== Installing common files ====" # TODO: implement a mask for .svn folders # TODO: refactor /common to the app config FileUtils.cp_r(Dir.glob(File.join(subtrac_path, "common/.")),docs_dir) - FileUtils.cp_r(Dir.glob(File.join(subtrac_path, "shared/.")),create_if_missing(File.join(trac_dir, ".shared"))) + FileUtils.cp_r(Dir.glob(File.join(subtrac_path, "shared/.")),File.create_if_missing(File.join(trac_dir, ".shared"))) end def self.configure_admin_user puts "\n==== Configure admin user ====" # create admin user @@ -352,140 +354,17 @@ end def self.create_environment_directories(overwrite=false) puts "\n==== Creating new environment directories ====" FileUtils.rm_rf install_dir if overwrite - create_if_missing install_dir + File.create_if_missing install_dir # create the environment directories @APP_CONFIG[:dirs].each do |key, value| dir = File.join(install_dir,value) - create_if_missing dir + File.create_if_missing dir end - end - - def self.create_client(name) - puts "\n==== Create a new client called #{name} ====" - client_name = name.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase } - client_path = name.downcase - # create apache configuration - puts "subtrac_path: #{subtrac_path}" - puts "templates_location: #{@APP_CONFIG[:templates][:location]}" - location_template = File.join(subtrac_path, @APP_CONFIG[:templates][:location]) - puts "location_template: #{location_template}" - location_conf = File.join(locations_dir,"#{client_path}.conf") - puts "location_conf: #{location_conf}" - parse_template(location_template,location_conf,binding) - `/etc/init.d/apache2 force-reload` if SUBTRAC_ENV != 'test' - - # create svn+trac directory - create_if_missing File.join(svn_dir,client_path) - create_if_missing File.join(trac_dir,client_path) - end - - def self.install_default_theme - puts "\n==== Create a new client called #{name} ====" - # create a project for this clients trac theme - #create_project("trac_theme", client_path,@APP_CONFIG[:default_theme_template]) - - # check the theme project out - #client_theme_dir = File.join(trac_dir,client_path,".theme") - #create_if_missing(client_theme_dir) - #FileUtils.chown_R('www-data', 'www-data', client_theme_dir, :verbose => true) if SUBTRAC_ENV != 'test' - #puts "Attempting checkout of theme project..." - - #`sudo svn co --username #{@APP_CONFIG[:admin_user]} --password #{@APP_CONFIG[:admin_pass]} \ - # file://#{svn_dir}/#{client_path}/trac_theme/trunk #{client_theme_dir}` if SUBTRAC_ENV != 'test' - # TODO: Need to find a way to test before trying - template_dir = File.join(subtrac_path, "templates","trac","themes","subtractheme") - say(`sudo easy_install #{template_dir}`) - end - - def self.create_project(project, client, project_type=@APP_CONFIG[:default_project_template]) - puts "\n==== Create a new project called #{project} for #{client} ====" - - client_name = client.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase } - project_name = project.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase } - # need to make sure this is a valid path - swap spaces for underscores etc - client_path = client.downcase - project_path = project.downcase - - # create client directory if needed - create_client(client_path) if (!File.directory? File.join(svn_dir,client_path)) - - project_svn_dir = File.join(svn_dir,client_path,project_path) - project_trac_dir = File.join(trac_dir,client_path,project_path) - - # TODO: Need to handle this exception... - if (File.directory? project_svn_dir) then - raise StandardError, "A project called #{project} already exists in the #{client} repository. Would you like to replace it?" - end - - # create new project directories - say("Create project directories...") - create_if_missing project_svn_dir - create_if_missing project_trac_dir - - project_template = File.join(subtrac_path, @APP_CONFIG[:templates][:projects],project_type) - - # copy template svn project to a temp folder, then svn import it into the repo - say("Create temporary project directory and copy template files...") - svn_template_dir = File.join(project_template,"svn") - project_temp_dir = File.join(temp_dir,project_path) - FileUtils.cp_r(svn_template_dir,project_temp_dir) - - # create a new subversion repository - say("Creating a new subversion repository...") - `svnadmin create #{project_svn_dir}` if SUBTRAC_ENV != 'test' - - # import into svn - say("Importing temporary project into the new subversion repository...") - `svn import #{project_temp_dir} file:///#{project_svn_dir} --message "initial import"` if SUBTRAC_ENV != 'test' - # delete the temporary directory - FileUtils.rm_r(project_temp_dir, :force => true) - - # create a new trac site - say("Creating a new trac site...") - result = `trac-admin #{project_trac_dir} initenv #{project_path} sqlite:#{project_trac_dir}/db/trac.db svn #{project_svn_dir}` if SUBTRAC_ENV != 'test' - FileUtils.chown_R('www-data', 'www-data', project_trac_dir, :verbose => false) if SUBTRAC_ENV != 'test' - FileUtils.mkdir_p("#{project_trac_dir}/conf") if SUBTRAC_ENV == 'test' # fake the folder for tests - - say("Installing trac configuration...") - # install shared trac.ini - trac_ini_template = File.join(subtrac_path, @APP_CONFIG[:templates][:trac]) - parse_template(trac_ini_template,File.join(project_trac_dir,"/conf/trac.ini"),binding) - - # remove custom templates directory so trac uses the shared location (while we wait for trac patch) - #FileUtils.rm_rf("#{project_trac_dir}/templates") - say("Setting up default trac permissions...") - # set up trac permissions - @APP_CONFIG[:trac][:permissions].each do |key, value| - `sudo trac-admin #{project_trac_dir} permission add #{key} #{value}` if SUBTRAC_ENV != 'test' - end - - say("Adding default trac wiki pages...") - # loop through the directory and import all pages - Dir.foreach("#{project_template}/trac/wiki/.") do |file| - # do something with the file here - unless ['.', '..','.svn'].include? file - temp_file = File.join(temp_dir,file) - puts = binding - parse_template(File.join(project_template,"trac/wiki",file), temp_file, binding) - `trac-admin #{project_trac_dir} wiki import #{file} #{temp_file}` if SUBTRAC_ENV != 'test' - FileUtils.rm(temp_file) - end - end - - # chown the whole directory - `sudo chown -R www-data:www-data #{install_dir}` - #FileUtils.chown_R('www-data', 'www-data', File.join(install_dir,"*"), :verbose => true) if SUBTRAC_ENV != 'test' - - # run trac upgrade - say("Upgrading the trac installation...") - `trac-admin #{project_trac_dir} upgrade` if SUBTRAC_ENV != 'test' - - end end