From 5321a456a5a7691c5d8cfd43133a0c7aeb14096f Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 01:33:58 +0100 Subject: [PATCH 1/9] Fix check.rake --- lib/tasks/gitlab/check.rake | 64 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake index baa706d2bee..2de9dbd9df7 100644 --- a/lib/tasks/gitlab/check.rake +++ b/lib/tasks/gitlab/check.rake @@ -48,7 +48,7 @@ namespace :gitlab do see_database_guide, "http://guides.rubyonrails.org/getting_started.html#configuring-a-database" ) - check_failed + fix_and_rerun end end @@ -65,7 +65,7 @@ namespace :gitlab do "https://github.com/gitlabhq/gitlabhq/wiki/Migrate-from-SQLite-to-MySQL", see_database_guide ) - check_failed + fix_and_rerun end end @@ -85,7 +85,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "GitLab" ) - check_failed + fix_and_rerun end end @@ -110,7 +110,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "GitLab" ) - check_failed + fix_and_rerun end end @@ -129,7 +129,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Install Init Script" ) - check_failed + fix_and_rerun end end @@ -155,7 +155,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Install Init Script" ) - check_failed + fix_and_rerun end end @@ -171,7 +171,7 @@ namespace :gitlab do try_fixing_it( "sudo -u gitlab -H bundle exec rake db:migrate" ) - check_failed + fix_and_rerun end end @@ -199,7 +199,7 @@ namespace :gitlab do for_more_information( "doc/raketasks/maintenance.md " ) - check_failed + fix_and_rerun end end end @@ -220,7 +220,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "GitLab" ) - check_failed + fix_and_rerun end end @@ -240,7 +240,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "GitLab" ) - check_failed + fix_and_rerun end end end @@ -288,7 +288,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "GitLab" ) - check_failed + fix_and_rerun end end @@ -306,7 +306,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "System Users" ) - check_failed + fix_and_rerun end end @@ -330,7 +330,7 @@ namespace :gitlab do see_installation_guide_section("Gitolite"), "https://github.com/gitlabhq/gitlabhq/issues/1059" ) - check_failed + fix_and_rerun end end @@ -350,7 +350,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Packages / Dependencies" ) - check_failed + fix_and_rerun end end @@ -376,7 +376,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Packages / Dependencies" ) - check_failed + fix_and_rerun end end end @@ -432,7 +432,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end # assumes #check_can_clone_gitolite_admin has been run before @@ -464,7 +464,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun ensure FileUtils.rm_rf("/tmp/gitolite_gitlab_test") end @@ -486,7 +486,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -510,7 +510,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -536,7 +536,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -581,7 +581,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -610,7 +610,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -634,7 +634,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Setup GitLab Hooks" ) - check_failed + fix_and_rerun end end @@ -665,7 +665,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Setup GitLab Hooks" ) - check_failed + fix_and_rerun end end @@ -687,7 +687,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -711,7 +711,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -737,7 +737,7 @@ namespace :gitlab do for_more_information( see_installation_guide_section "Gitolite" ) - check_failed + fix_and_rerun end end @@ -771,7 +771,7 @@ namespace :gitlab do for_more_information( "doc/raketasks/maintenance.md" ) - check_failed + fix_and_rerun end end end @@ -807,7 +807,7 @@ namespace :gitlab do for_more_information( "lib/support/rewrite-hooks.sh" ) - check_failed + fix_and_rerun next end @@ -821,7 +821,7 @@ namespace :gitlab do for_more_information( "lib/support/rewrite-hooks.sh" ) - check_failed + fix_and_rerun end end end @@ -879,7 +879,7 @@ namespace :gitlab do see_installation_guide_section("Install Init Script"), "see log/resque.log for possible errors" ) - check_failed + fix_and_rerun end end end @@ -888,7 +888,7 @@ namespace :gitlab do # Helper methods ########################## - def check_failed + def fix_and_rerun puts " Please #{"fix the error above"} and rerun the checks.".red end -- GitLab From 80af104bb7412ec37209bba3f0474b7e8fc68619 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 03:20:13 +0100 Subject: [PATCH 2/9] Extract task helper methods --- lib/tasks/gitlab/check.rake | 37 ---------------------------- lib/tasks/gitlab/info.rake | 26 -------------------- lib/tasks/gitlab/task_helpers.rake | 39 ++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 63 deletions(-) create mode 100644 lib/tasks/gitlab/task_helpers.rake diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake index 2de9dbd9df7..a2cdfba4421 100644 --- a/lib/tasks/gitlab/check.rake +++ b/lib/tasks/gitlab/check.rake @@ -907,29 +907,6 @@ namespace :gitlab do puts "" end - # Runs the given command - # - # Returns nil if the command was not found - # Returns the output of the command otherwise - # - # see also #run_and_match - def run(command) - unless `#{command} 2>/dev/null`.blank? - `#{command}` - end - end - - # Runs the given command and matches the output agains the given pattern - # - # Returns nil if nothing matched - # Retunrs the MatchData if the pattern matched - # - # see also #run - # see also String#match - def run_and_match(command, pattern) - run(command).try(:match, pattern) - end - def see_database_guide "doc/install/databases.md" end @@ -951,18 +928,4 @@ namespace :gitlab do puts " #{step}" end end - - def warn_user_is_not_gitlab - unless @warned_user_not_gitlab - current_user = run("whoami").chomp - unless current_user == "gitlab" - puts "#{Colored.color(:black)+Colored.color(:on_yellow)} Warning #{Colored.extra(:clear)}" - puts " You are running as user #{current_user.magenta}, we hope you know what you are doing." - puts " Some tests may pass\/fail for the wrong reason." - puts " For meaningful results you should run this as user #{"gitlab".magenta}." - puts "" - end - @warned_user_not_gitlab = true - end - end end diff --git a/lib/tasks/gitlab/info.rake b/lib/tasks/gitlab/info.rake index 85458fe2c43..3fbedda7721 100644 --- a/lib/tasks/gitlab/info.rake +++ b/lib/tasks/gitlab/info.rake @@ -80,31 +80,5 @@ namespace :gitlab do puts "Git:\t\t#{Gitlab.config.git.bin_path}" end - - - # Helper methods - - # Runs the given command and matches the output agains the given pattern - # - # Returns nil if nothing matched - # Retunrs the MatchData if the pattern matched - # - # see also #run - # see also String#match - def run_and_match(command, regexp) - run(command).try(:match, regexp) - end - - # Runs the given command - # - # Returns nil if the command was not found - # Returns the output of the command otherwise - # - # see also #run_and_match - def run(command) - unless `#{command} 2>/dev/null`.blank? - `#{command}` - end - end end end diff --git a/lib/tasks/gitlab/task_helpers.rake b/lib/tasks/gitlab/task_helpers.rake new file mode 100644 index 00000000000..c9635f058ee --- /dev/null +++ b/lib/tasks/gitlab/task_helpers.rake @@ -0,0 +1,39 @@ +namespace :gitlab do + + # Runs the given command and matches the output agains the given pattern + # + # Returns nil if nothing matched + # Retunrs the MatchData if the pattern matched + # + # see also #run + # see also String#match + def run_and_match(command, regexp) + run(command).try(:match, regexp) + end + + # Runs the given command + # + # Returns nil if the command was not found + # Returns the output of the command otherwise + # + # see also #run_and_match + def run(command) + unless `#{command} 2>/dev/null`.blank? + `#{command}` + end + end + + def warn_user_is_not_gitlab + unless @warned_user_not_gitlab + current_user = run("whoami").chomp + unless current_user == "gitlab" + puts "#{Colored.color(:black)+Colored.color(:on_yellow)} Warning #{Colored.extra(:clear)}" + puts " You are running as user #{current_user.magenta}, we hope you know what you are doing." + puts " Things may work\/fail for the wrong reasons." + puts " For correct results you should run this as user #{"gitlab".magenta}." + puts "" + end + @warned_user_not_gitlab = true + end + end +end -- GitLab From 0aaef732b359fabd145a00d7d4a3ed88a0b131a2 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 03:02:56 +0100 Subject: [PATCH 3/9] Update gitlab:enable_automerge --- lib/tasks/gitlab/enable_automerge.rake | 36 ++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/tasks/gitlab/enable_automerge.rake b/lib/tasks/gitlab/enable_automerge.rake index ed3d6368a99..d412f8b3837 100644 --- a/lib/tasks/gitlab/enable_automerge.rake +++ b/lib/tasks/gitlab/enable_automerge.rake @@ -1,16 +1,42 @@ namespace :gitlab do desc "GITLAB | Enable auto merge" task :enable_automerge => :environment do + warn_user_is_not_gitlab + + puts "Updating repo permissions ..." Gitlab::Gitolite.new.enable_automerge + puts "... #{"done".green}" + puts "" + + print "Creating satellites for ..." + unless Project.count > 0 + puts "skipping, because you have no projects".magenta + return + end + puts "" + + Project.find_each(batch_size: 100) do |project| + print "#{project.name_with_namespace.yellow} ... " - Project.find_each do |project| - if project.repo_exists? && !project.satellite.exists? - puts "Creating satellite for #{project.name}...".green + unless project.repo_exists? + puts "skipping, because the repo is empty".magenta + next + end + + if project.satellite.exists? + puts "exists already".green + else + puts "" project.satellite.create + + print "... " + if $?.success? + puts "created".green + else + puts "error".red + end end end - - puts "Done!".green end namespace :satellites do -- GitLab From a0e0be1e745cd1eb803f4266e2bac675ad167cb8 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 03:30:39 +0100 Subject: [PATCH 4/9] Prompt user in gitlab:enable_automerge --- lib/tasks/gitlab/enable_automerge.rake | 38 ++++++++++++++++++++++---- lib/tasks/gitlab/task_helpers.rake | 27 ++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/lib/tasks/gitlab/enable_automerge.rake b/lib/tasks/gitlab/enable_automerge.rake index d412f8b3837..83ba58517a3 100644 --- a/lib/tasks/gitlab/enable_automerge.rake +++ b/lib/tasks/gitlab/enable_automerge.rake @@ -1,8 +1,38 @@ namespace :gitlab do desc "GITLAB | Enable auto merge" task :enable_automerge => :environment do + enable_automerge + end + + namespace :satellites do + desc "GITLAB | Create satellite repos" + task create: 'gitlab:enable_automerge' + end + + + # Task methods + ########################## + + def enable_automerge warn_user_is_not_gitlab + puts "This will update the repository permissions in Gitolite and" + puts "(re-)create satellite repos for your projects." + ask_to_continue + puts "" + + satelite_base_path = Rails.root.join("tmp", "repo_satellites").to_s + if File.exists?(satelite_base_path) + puts "#{satelite_base_path.yellow} conaining satellite repos exists already." + answer = prompt("#{"Do you want to remove it and recreate ".blue}#{"all".blue.underline}#{" satellites (y/n)? ".blue}", %w{y n}) + if answer == "y" + print "Removing #{satelite_base_path.yellow} ... " + Kernel.system("rm -rf #{satelite_base_path}") + puts "done".green + end + puts "" + end + puts "Updating repo permissions ..." Gitlab::Gitolite.new.enable_automerge puts "... #{"done".green}" @@ -37,10 +67,8 @@ namespace :gitlab do end end end - end - - namespace :satellites do - desc "GITLAB | Create satellite repos" - task create: 'gitlab:enable_automerge' + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 end end diff --git a/lib/tasks/gitlab/task_helpers.rake b/lib/tasks/gitlab/task_helpers.rake index c9635f058ee..2778badc909 100644 --- a/lib/tasks/gitlab/task_helpers.rake +++ b/lib/tasks/gitlab/task_helpers.rake @@ -1,5 +1,32 @@ +module Gitlab + class TaskAbortedByUserError < StandardError; end +end + namespace :gitlab do + # Ask if the user wants to continue + # + # Returns "yes" the user chose to continue + # Raises Gitlab::TaskAbortedByUserError if the user chose *not* to continue + def ask_to_continue + answer = prompt("Do you want to continue (yes/no)? ".blue, %w{yes no}) + raise Gitlab::TaskAbortedByUserError unless answer == "yes" + end + + # Prompt the user to input something + # + # message - the message to display before input + # choices - array of strings of acceptible answers or nil for any answer + # + # Returns the user's answer + def prompt(message, choices = nil) + begin + print(message) + answer = STDIN.gets.chomp + end while choices.present? && !choices.include?(answer) + answer + end + # Runs the given command and matches the output agains the given pattern # # Returns nil if nothing matched -- GitLab From 88a4bbc36072597945c629835bf440c2a87f3537 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 04:01:56 +0100 Subject: [PATCH 5/9] Add prompt to gitlab:backup:resore --- lib/tasks/gitlab/backup.rake | 227 ++++++++++++++++++++--------------- 1 file changed, 130 insertions(+), 97 deletions(-) diff --git a/lib/tasks/gitlab/backup.rake b/lib/tasks/gitlab/backup.rake index 44da6d671e0..e3fbf937078 100644 --- a/lib/tasks/gitlab/backup.rake +++ b/lib/tasks/gitlab/backup.rake @@ -5,6 +5,42 @@ namespace :gitlab do # Create backup of GitLab system desc "GITLAB | Create a backup of the GitLab system" task :create => :environment do + backup + end + + # Restore backup of GitLab system + desc "GITLAB | Restore a previously created backup" + task :restore => :environment do + restore + end + + namespace :db do + task :create => :environment do + backup_dbs + end + + task :restore=> :environment do + restore_dbs + end + end + + namespace :repo do + task :create => :environment do + backup_repos + end + + task :restore => :environment do + restore_repos + end + end + + + # Task methods + ######################## + + def backup + warn_user_is_not_gitlab + Rake::Task["gitlab:backup:db:create"].invoke Rake::Task["gitlab:backup:repo:create"].invoke @@ -22,23 +58,23 @@ namespace :gitlab do end # create archive - print "Creating backup archive: #{Time.now.to_i}_gitlab_backup.tar " + print "Creating backup archive: #{Time.now.to_i}_gitlab_backup.tar ... " if Kernel.system("tar -cf #{Time.now.to_i}_gitlab_backup.tar repositories/ db/ backup_information.yml") - puts "[DONE]".green + puts "done".green else - puts "[FAILED]".red + puts "failed".red end # cleanup: remove tmp files - print "Deleting tmp directories..." + print "Deleting tmp directories ... " if Kernel.system("rm -rf repositories/ db/ backup_information.yml") - puts "[DONE]".green + puts "done".green else - puts "[FAILED]".red + puts "failed".red end # delete backups - print "Deleting old backups... " + print "Deleting old backups ... " if Gitlab.config.backup.keep_time > 0 file_list = Dir.glob("*_gitlab_backup.tar").map { |f| f.split(/_/).first.to_i } file_list.sort.each do |timestamp| @@ -46,15 +82,51 @@ namespace :gitlab do %x{rm #{timestamp}_gitlab_backup.tar} end end - puts "[DONE]".green + puts "done".green else - puts "[SKIPPING]".yellow + puts "skipping".yellow end end - # Restore backup of GitLab system - desc "GITLAB | Restore a previously created backup" - task :restore => :environment do + def backup_dbs + backup_path_db = File.join(Gitlab.config.backup.path, "db") + FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) + + puts "Dumping database tables ... " + ActiveRecord::Base.connection.tables.each do |tbl| + print "#{tbl.yellow} ... " + count = 1 + File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| + ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| + line.delete_if{|k,v| v.blank?} + output = {tbl + '_' + count.to_s => line} + file << output.to_yaml.gsub(/^---\n/,'') + "\n" + count += 1 + end + puts "done".green + end + end + end + + def backup_repos + backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") + FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) + puts "Dumping repositories ..." + project = Project.all.map { |n| [n.path, n.path_to_repo] } + project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")] + project.each do |project| + print "#{project.first.yellow} ... " + if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") + puts "done".green + else + puts "failed".red + end + end + end + + def restore + warn_user_is_not_gitlab + Dir.chdir(Gitlab.config.backup.path) # check for existing backups in the backup dir @@ -63,22 +135,26 @@ namespace :gitlab do if file_list.count > 1 && ENV["BACKUP"].nil? puts "Found more than one backup, please specify which one you want to restore:" puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup" - exit 1; + exit 1 end + puts "This will overwrite your database and repositories with backuped data." + ask_to_continue + puts "" + tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") unless File.exists?(tar_file) puts "The specified backup doesn't exist!" - exit 1; + exit 1 end - print "Unpacking backup... " + print "Unpacking backup ... " unless Kernel.system("tar -xf #{tar_file}") - puts "[FAILED]".red + puts "failed".red exit 1 else - puts "[DONE]".green + puts "done".green end settings = YAML.load_file("backup_information.yml") @@ -86,7 +162,7 @@ namespace :gitlab do # restoring mismatching backups can lead to unexpected problems if settings[:gitlab_version] != %x{git rev-parse HEAD}.gsub(/\n/,"") - puts "gitlab_version mismatch:".red + puts "GitLab version mismatch:".red puts " Your current HEAD differs from the HEAD in the backup!".red puts " Please switch to the following revision and try again:".red puts " revision: #{settings[:gitlab_version]}".red @@ -97,96 +173,53 @@ namespace :gitlab do Rake::Task["gitlab:backup:repo:restore"].invoke # cleanup: remove tmp files - print "Deleting tmp directories..." + print "Deleting tmp directories ... " if Kernel.system("rm -rf repositories/ db/ backup_information.yml") - puts "[DONE]".green + puts "done".green else - puts "[FAILED]".red + puts "failed".red end - end - ################################################################################ - ################################# invoked tasks ################################ + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 + end - ################################# REPOSITORIES ################################# + def restore_dbs + backup_path_db = File.join(Gitlab.config.backup.path, "db") - namespace :repo do - task :create => :environment do - backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") - FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) - puts "Dumping repositories:" - project = Project.all.map { |n| [n.path, n.path_to_repo] } - project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")] - project.each do |project| - print "- Dumping repository #{project.first}... " - if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1") - puts "[DONE]".green - else - puts "[FAILED]".red - end - end - end + puts "Restoring database tables (loading fixtures) ... " + Rake::Task["db:reset"].invoke - task :restore => :environment do - backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") - puts "Restoring repositories:" - project = Project.all.map { |n| [n.path, n.path_to_repo] } - project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")] - project.each do |project| - print "- Restoring repository #{project.first}... " - FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff - if Kernel.system("cd #{File.dirname(project.second)} > /dev/null 2>&1 && git clone --bare #{backup_path_repo}/#{project.first}.bundle #{project.first}.git > /dev/null 2>&1") - permission_commands = [ - "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}", - "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}" - ] - permission_commands.each { |command| Kernel.system(command) } - puts "[DONE]".green - else - puts "[FAILED]".red - end + Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| + fixture_file = File.basename(dir, ".*" ) + print "#{fixture_file.yellow} ... " + if File.size(dir) > 0 + ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) + puts "done".green + else + puts "skipping".yellow end end end - ###################################### DB ###################################### - - namespace :db do - task :create => :environment do - backup_path_db = File.join(Gitlab.config.backup.path, "db") - FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db) - - puts "Dumping database tables:" - ActiveRecord::Base.connection.tables.each do |tbl| - print "- Dumping table #{tbl}... " - count = 1 - File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file| - ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line| - line.delete_if{|k,v| v.blank?} - output = {tbl + '_' + count.to_s => line} - file << output.to_yaml.gsub(/^---\n/,'') + "\n" - count += 1 - end - puts "[DONE]".green - end - end - end - - task :restore=> :environment do - backup_path_db = File.join(Gitlab.config.backup.path, "db") - - puts "Restoring database tables:" - Rake::Task["db:reset"].invoke - - Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| - fixture_file = File.basename(dir, ".*" ) - print "- Loading fixture #{fixture_file}..." - if File.size(dir) > 0 - ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file) - puts "[DONE]".green - else - puts "[SKIPPING]".yellow - end + def restore_repos + backup_path_repo = File.join(Gitlab.config.backup.path, "repositories") + puts "Restoring repositories ... " + project = Project.all.map { |n| [n.path, n.path_to_repo] } + project << ["gitolite-admin.git", File.join(Gitlab.config.git_base_path, "gitolite-admin.git")] + project.each do |project| + print "#{project.first.yellow} ... " + FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff + if Kernel.system("cd #{File.dirname(project.second)} > /dev/null 2>&1 && git clone --bare #{backup_path_repo}/#{project.first}.bundle #{project.first}.git > /dev/null 2>&1") + permission_commands = [ + "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}", + "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}" + ] + permission_commands.each { |command| Kernel.system(command) } + puts "done".green + else + puts "failed".red end end end -- GitLab From b2cb651dddbac59b0ed1678c60c39d6e5b3de165 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 04:23:50 +0100 Subject: [PATCH 6/9] Rename gitlab:app:setup to gitlab:setup Call gitlab:enable_namespaces Add prompt --- lib/tasks/gitlab/setup.rake | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/tasks/gitlab/setup.rake b/lib/tasks/gitlab/setup.rake index 572a22aa1f6..ec67ea56e5d 100644 --- a/lib/tasks/gitlab/setup.rake +++ b/lib/tasks/gitlab/setup.rake @@ -1,10 +1,23 @@ namespace :gitlab do - namespace :app do - desc "GITLAB | Setup production application" - task :setup => [ - 'db:setup', - 'db:seed_fu', - 'gitlab:enable_automerge' - ] + desc "GITLAB | Setup production application" + task :setup => :environment do + setup + end + + def setup + warn_user_is_not_gitlab + + puts "This will create the necessary database tables and seed the database." + puts "It will overwrite data you already have." + ask_to_continue + puts "" + + Rake::Task["db:setup"].invoke + Rake::Task["db:seed_fu"].invoke + Rake::Task["gitlab:enable_automerge"].invoke + Rake::Task["gitlab:enable_namespaces"].invoke + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 end end -- GitLab From 0a096c44e95aade26db15c875ebda7388f925ee9 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 04:47:21 +0100 Subject: [PATCH 7/9] Fix Namespace#ensure_dir_exist --- app/models/namespace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 8c90f5aee26..c37d13e3245 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -49,7 +49,7 @@ class Namespace < ActiveRecord::Base def ensure_dir_exist namespace_dir_path = File.join(Gitlab.config.gitolite.repos_path, path) - system("mkdir -m 770 #{namespace_dir_path}") unless File.exists?(namespace_dir_path) + File.exists?(namespace_dir_path) || system("mkdir -m 770 #{namespace_dir_path}") end def move_dir -- GitLab From 7f08ff93d68e0c7f7c1c45d933fbd88240b5da2b Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 04:47:53 +0100 Subject: [PATCH 8/9] Add prompt to gitlab:enable_namespaces --- lib/tasks/gitlab/enable_namespaces.rake | 41 +++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/tasks/gitlab/enable_namespaces.rake b/lib/tasks/gitlab/enable_namespaces.rake index 1be9ba6469d..dcdb617efce 100644 --- a/lib/tasks/gitlab/enable_namespaces.rake +++ b/lib/tasks/gitlab/enable_namespaces.rake @@ -1,7 +1,19 @@ namespace :gitlab do desc "GITLAB | Enable usernames and namespaces for user projects" task enable_namespaces: :environment do - print "\nUsernames for users:".yellow + enable_namespaces + end + + def enable_namespaces + warn_user_is_not_gitlab + + puts "This will update your GitLab to be able to use namespaces for projects." + puts "It will move projects in groups into appropriately named directories." + puts "This will change their project and repo URLs." + ask_to_continue + puts "" + + print "Generate usernames for users without one: " User.find_each(batch_size: 500) do |user| next if user.namespace @@ -16,7 +28,8 @@ namespace :gitlab do end end - print "\n\nDirs for groups:".yellow + puts "" + print "Create directories for groups: " Group.find_each(batch_size: 500) do |group| if group.ensure_dir_exist @@ -25,43 +38,47 @@ namespace :gitlab do print 'F'.red end end + puts "" - print "\n\nMove projects from groups under groups dirs:".yellow git_path = Gitlab.config.gitolite.repos_path - + puts "" + puts "Move projects in groups into respective directories ... " Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project| next unless project.group group = project.group - puts "\n" - print " * #{project.name}: " + print "#{project.name_with_namespace.yellow} ... " new_path = File.join(git_path, project.path_with_namespace + '.git') if File.exists?(new_path) - print "ok. already at #{new_path}".cyan + puts "already at #{new_path}".green next end old_path = File.join(git_path, project.path + '.git') unless File.exists?(old_path) - print "missing. not found at #{old_path}".red + puts "couldn't find it at #{old_path}".red next end begin Gitlab::ProjectMover.new(project, '', group.path).execute - print "ok. Moved to #{new_path}".green + puts "moved to #{new_path}".green rescue - print "Failed moving to #{new_path}".red + puts "failed moving to #{new_path}".red end end - print "\n\nRebuild gitolite:".yellow + puts "" + puts "Rebuild Gitolite ... " gitolite = Gitlab::Gitolite.new gitolite.update_repositories(Project.where('namespace_id IS NOT NULL')) - puts "\n" + puts "... #{"done".green}" + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 end end -- GitLab From a1814a50100916666b38c56c88f74ddc8a04b707 Mon Sep 17 00:00:00 2001 From: Riyad Preukschas Date: Sun, 23 Dec 2012 05:03:12 +0100 Subject: [PATCH 9/9] Add prompt to gitlab:gitolite:update_* --- lib/tasks/gitlab/gitolite_rebuild.rake | 55 ++++++++++++++++++++------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/tasks/gitlab/gitolite_rebuild.rake b/lib/tasks/gitlab/gitolite_rebuild.rake index fce10eb5b69..c24d1270ee3 100644 --- a/lib/tasks/gitlab/gitolite_rebuild.rake +++ b/lib/tasks/gitlab/gitolite_rebuild.rake @@ -1,24 +1,53 @@ namespace :gitlab do namespace :gitolite do - desc "GITLAB | Rebuild each project at gitolite config" + desc "GITLAB | Rebuild each user key in Gitolite config" + task :update_keys => :environment do + update_keys + end + + desc "GITLAB | Rebuild each project in Gitolite config" task :update_repos => :environment do - puts "Starting Projects" - Project.find_each(:batch_size => 100) do |project| - puts "\n=== #{project.name}" - project.update_repository - puts - end - puts "Done with projects" + update_repos end - desc "GITLAB | Rebuild each key at gitolite config" - task :update_keys => :environment do - puts "Starting Key" + + # Task methods + ######################## + + def update_keys + warn_user_is_not_gitlab + + puts "This will rebuild and replace the user SSH keys configuration in Gitolite." + ask_to_continue + puts "" + + puts "Rebuilding keys ... " Key.find_each(:batch_size => 100) do |key| + puts "#{key.identifier.yellow} ... " Gitlab::Gitolite.new.set_key(key.identifier, key.key, key.projects) - print '.' + puts "... #{"done".green}" + end + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 + end + + def update_repos + warn_user_is_not_gitlab + + puts "This will rebuild and relpace the projects configuration in Gitolite." + ask_to_continue + puts "" + + puts "Rebuilding projects ... " + Project.find_each(:batch_size => 100) do |project| + puts "#{project.name_with_namespace.yellow} ... " + project.update_repository + puts "... #{"done".green}" end - puts "Done with keys" + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 end end end -- GitLab