diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 8c90f5aee26cb84f8c7053009eddc1fe8623e33b..c37d13e3245cb53fdf5c78da0bd0b1d2b398ef2a 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 diff --git a/lib/tasks/gitlab/backup.rake b/lib/tasks/gitlab/backup.rake index 44da6d671e0d1de27b02807b0705043f3ee6e65f..e3fbf93707807cb12b6f75a29da39ed9c073c392 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 diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake index baa706d2beef05ff8d9c668a32900d16048be81d..a2cdfba4421a587bf2e1ea2dc085347b7861f9ff 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 @@ -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/enable_automerge.rake b/lib/tasks/gitlab/enable_automerge.rake index ed3d6368a998d1aae98f67ecd75983838adb578b..83ba58517a36ba124876d11d8adec51a8a9d9196 100644 --- a/lib/tasks/gitlab/enable_automerge.rake +++ b/lib/tasks/gitlab/enable_automerge.rake @@ -1,20 +1,74 @@ namespace :gitlab do desc "GITLAB | Enable auto merge" task :enable_automerge => :environment do - Gitlab::Gitolite.new.enable_automerge - - Project.find_each do |project| - if project.repo_exists? && !project.satellite.exists? - puts "Creating satellite for #{project.name}...".green - project.satellite.create - end - end - - puts "Done!".green + 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}" + 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} ... " + + 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 + rescue Gitlab::TaskAbortedByUserError + puts "Quitting...".red + exit 1 + end end diff --git a/lib/tasks/gitlab/enable_namespaces.rake b/lib/tasks/gitlab/enable_namespaces.rake index 1be9ba6469d29860c9a3f62df7002d25136fa0e1..dcdb617efcee6ba8f35f9b162ef137c2eb6edddb 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 diff --git a/lib/tasks/gitlab/gitolite_rebuild.rake b/lib/tasks/gitlab/gitolite_rebuild.rake index fce10eb5b692a4fc3e230a67be1bc87695b353f1..c24d1270ee3a34d653d79be112b5d9e87f3a021a 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 diff --git a/lib/tasks/gitlab/info.rake b/lib/tasks/gitlab/info.rake index 85458fe2c43fdd53888671878d2a7afdb64c15f5..3fbedda77219a5a54735a7d5bfc783244ffae156 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/setup.rake b/lib/tasks/gitlab/setup.rake index 572a22aa1f6028f1add82ee6b230cc3dd1fadc85..ec67ea56e5d2f8e074d4b93b9a80f4b83871f59a 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 diff --git a/lib/tasks/gitlab/task_helpers.rake b/lib/tasks/gitlab/task_helpers.rake new file mode 100644 index 0000000000000000000000000000000000000000..2778badc909d12a3b07d6df82491ca758d2be133 --- /dev/null +++ b/lib/tasks/gitlab/task_helpers.rake @@ -0,0 +1,66 @@ +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 + # 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