Commit 83696b127b7822482957a894816b0e3c2daea0b5

Authored by Nihad Abbasov
1 parent 30ee5362

cleanup rake tasks

lib/tasks/bulk_add_permission.rake
1   -desc "Add all users to all projects, system administratos are added as masters"
  1 +desc "Add all users to all projects (admin users are added as masters)"
2 2 task :add_users_to_project_teams => :environment do |t, args|
3   - users = User.find_all_by_admin(false, :select => 'id').map(&:id)
4   - admins = User.find_all_by_admin(true, :select => 'id').map(&:id)
  3 + user_ids = User.where(:admin => false).pluck(:id)
  4 + admin_ids = User.where(:admin => true).pluck(:id)
5 5  
6   - users.each do |user|
7   - puts "#{user}"
8   - end
9   -
10   - Project.all.each do |project|
11   - puts "Importing #{users.length} users into #{project.path}"
12   - UsersProject.bulk_import(project, users, UsersProject::DEVELOPER)
13   - puts "Importing #{admins.length} admins into #{project.path}"
14   - UsersProject.bulk_import(project, admins, UsersProject::MASTER)
  6 + Project.find_each do |project|
  7 + puts "Importing #{user_ids.size} users into #{project.code}"
  8 + UsersProject.bulk_import(project, user_ids, UsersProject::DEVELOPER)
  9 + puts "Importing #{admin_ids.size} admins into #{project.code}"
  10 + UsersProject.bulk_import(project, admin_ids, UsersProject::MASTER)
15 11 end
16 12 end
17 13  
18 14 desc "Add user to as a developer to all projects"
19 15 task :add_user_to_project_teams, [:email] => :environment do |t, args|
20   - user_email = args.email
21   - user = User.find_by_email(user_email)
22   -
23   - project_ids = Project.all.map(&:id)
  16 + user = User.find_by_email args.email
  17 + project_ids = Project.pluck(:id)
24 18  
25   - UsersProject.user_bulk_import(user,project_ids,UsersProject::DEVELOPER)
  19 + UsersProject.user_bulk_import(user, project_ids, UsersProject::DEVELOPER)
26 20 end
... ...
lib/tasks/bulk_import.rake
1   -
2 1 desc "Imports existing Git repos from a directory into new projects in git_base_path"
3 2 task :import_projects, [:directory,:email] => :environment do |t, args|
4   - user_email = args.email
5   - import_directory = args.directory
  3 + user_email, import_directory = args.email, args.directory
6 4 repos_to_import = Dir.glob("#{import_directory}/*")
7 5 git_base_path = Gitlab.config.git_base_path
8   - puts "Found #{repos_to_import.length} repos to import"
  6 + imported_count, skipped_count, failed_count = 0
  7 +
  8 + puts "Found #{repos_to_import.size} repos to import"
9 9  
10   - imported_count = 0
11   - skipped_count = 0
12   - failed_count = 0
13 10 repos_to_import.each do |repo_path|
14 11 repo_name = File.basename repo_path
  12 + clone_path = "#{git_base_path}#{repo_name}.git"
15 13  
16 14 puts " Processing #{repo_name}"
17   - clone_path = "#{git_base_path}#{repo_name}.git"
18 15  
19 16 if Dir.exists? clone_path
20 17 if Project.find_by_code(repo_name)
... ... @@ -38,7 +35,6 @@ task :import_projects, [:directory,:email] => :environment do |t, args|
38 35 else
39 36 failed_count += 1
40 37 end
41   -
42 38 end
43 39  
44 40 puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})."
... ... @@ -49,63 +45,39 @@ def clone_bare_repo_as_git(existing_path, new_path)
49 45 git_user = Gitlab.config.ssh_user
50 46 begin
51 47 sh "sudo -u #{git_user} -i git clone --bare '#{existing_path}' #{new_path}"
52   - true
53   - rescue Exception=> msg
54   - puts " ERROR: Faild to clone #{existing_path} to #{new_path}"
55   - puts " Make sure #{git_user} can reach #{existing_path}"
56   - puts " Exception-MSG: #{msg}"
57   - false
  48 + rescue Exception => msg
  49 + puts " ERROR: Failed to clone #{existing_path} to #{new_path}"
  50 + puts " Make sure #{git_user} can reach #{existing_path}"
  51 + puts " Exception-MSG: #{msg}"
58 52 end
59 53 end
60 54  
61   -# Creats a project in Gitlag given a @project_name@ to use (for name, web url, and code
62   -# url) and a @user_email@ that will be assigned as the owner of the project.
  55 +# Creates a project in GitLab given a `project_name` to use
  56 +# (for name, web url, and code url) and a `user_email` that will be
  57 +# assigned as the owner of the project.
63 58 def create_repo_project(project_name, user_email)
64   - user = User.find_by_email(user_email)
65   - if user
  59 + if user = User.find_by_email(user_email)
66 60 # Using find_by_code since that's the most important identifer to be unique
67 61 if Project.find_by_code(project_name)
68 62 puts " INFO: Project #{project_name} already exists in Gitlab, skipping."
69   - false
70 63 else
71   - project = nil
72   - if Project.find_by_code(project_name)
73   - puts " ERROR: Project already exists #{project_name}"
74   - return false
75   - project = Project.find_by_code(project_name)
76   - else
77   - project = Project.create(
78   - name: project_name,
79   - code: project_name,
80   - path: project_name,
81   - owner: user,
82   - description: "Automatically created from Rake on #{Time.now.to_s}"
83   - )
84   - end
85   -
86   - unless project.valid?
87   - puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
88   - return false
89   - end
90   -
91   - # Add user as admin for project
92   - project.users_projects.create!(
93   - :project_access => UsersProject::MASTER,
94   - :user => user
  64 + project = Project.create(
  65 + name: project_name,
  66 + code: project_name,
  67 + path: project_name,
  68 + owner: user,
  69 + description: "Automatically created from 'import_projects' rake task on #{Time.now}"
95 70 )
96 71  
97   - # Per projects_controller.rb#37
98   - project.update_repository
99   -
100 72 if project.valid?
101   - true
  73 + # Add user as admin for project
  74 + project.users_projects.create!(:project_access => UsersProject::MASTER, :user => user)
  75 + project.update_repository
102 76 else
103 77 puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
104   - false
105 78 end
106 79 end
107 80 else
108   - puts " ERROR: #{user_email} not found, skipping"
109   - false
  81 + puts " ERROR: user with #{user_email} not found, skipping"
110 82 end
111 83 end
... ...
lib/tasks/gitlab/backup.rake
... ... @@ -2,22 +2,20 @@ require 'active_record/fixtures'
2 2  
3 3 namespace :gitlab do
4 4 namespace :app do
5   -
6   - # Create backup of gitlab system
7   - desc "GITLAB | Create a backup of the gitlab system"
  5 + # Create backup of GitLab system
  6 + desc "GITLAB | Create a backup of the GitLab system"
8 7 task :backup_create => :environment do
9   -
10 8 Rake::Task["gitlab:app:db_dump"].invoke
11 9 Rake::Task["gitlab:app:repo_dump"].invoke
12 10  
13 11 Dir.chdir(Gitlab.config.backup_path)
14 12  
15 13 # saving additional informations
16   - s = Hash.new
17   - s["db_version"] = "#{ActiveRecord::Migrator.current_version}"
18   - s["backup_created_at"] = "#{Time.now}"
19   - s["gitlab_version"] = %x{git rev-parse HEAD}.gsub(/\n/,"")
20   - s["tar_version"] = %x{tar --version | head -1}.gsub(/\n/,"")
  14 + s = {}
  15 + s[:db_version] = "#{ActiveRecord::Migrator.current_version}"
  16 + s[:backup_created_at] = "#{Time.now}"
  17 + s[:gitlab_version] = %x{git rev-parse HEAD}.gsub(/\n/,"")
  18 + s[:tar_version] = %x{tar --version | head -1}.gsub(/\n/,"")
21 19  
22 20 File.open("#{Gitlab.config.backup_path}/backup_information.yml", "w+") do |file|
23 21 file << s.to_yaml.gsub(/^---\n/,'')
... ... @@ -32,7 +30,7 @@ namespace :gitlab do
32 30 end
33 31  
34 32 # cleanup: remove tmp files
35   - print "Deletion of tmp directories..."
  33 + print "Deleting tmp directories..."
36 34 if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
37 35 puts "[DONE]".green
38 36 else
... ... @@ -52,26 +50,23 @@ namespace :gitlab do
52 50 else
53 51 puts "[SKIPPING]".yellow
54 52 end
55   -
56 53 end
57 54  
58   -
59   - # Restore backup of gitlab system
  55 + # Restore backup of GitLab system
60 56 desc "GITLAB | Restore a previously created backup"
61 57 task :backup_restore => :environment do
62   -
63 58 Dir.chdir(Gitlab.config.backup_path)
64 59  
65 60 # check for existing backups in the backup dir
66 61 file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i }
67   - puts "no backup found" if file_list.count == 0
  62 + puts "no backups found" if file_list.count == 0
68 63 if file_list.count > 1 && ENV["BACKUP"].nil?
69 64 puts "Found more than one backup, please specify which one you want to restore:"
70 65 puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup"
71 66 exit 1;
72 67 end
73 68  
74   - tar_file = ENV["BACKUP"].nil? ? File.join(file_list.first.to_s + "_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar")
  69 + tar_file = ENV["BACKUP"].nil? ? File.join("#{file_list.first}_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar")
75 70  
76 71 unless File.exists?(tar_file)
77 72 puts "The specified backup doesn't exist!"
... ... @@ -102,16 +97,14 @@ namespace :gitlab do
102 97 Rake::Task["gitlab:app:repo_restore"].invoke
103 98  
104 99 # cleanup: remove tmp files
105   - print "Deletion of tmp directories..."
  100 + print "Deleting tmp directories..."
106 101 if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
107 102 puts "[DONE]".green
108 103 else
109 104 puts "[FAILED]".red
110 105 end
111   -
112 106 end
113 107  
114   -
115 108 ################################################################################
116 109 ################################# invoked tasks ################################
117 110  
... ... @@ -121,7 +114,7 @@ namespace :gitlab do
121 114 backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
122 115 FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
123 116 puts "Dumping repositories:"
124   - project = Project.all.map { |n| [n.path,n.path_to_repo] }
  117 + project = Project.all.map { |n| [n.path, n.path_to_repo] }
125 118 project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
126 119 project.each do |project|
127 120 print "- Dumping repository #{project.first}... "
... ... @@ -136,11 +129,11 @@ namespace :gitlab do
136 129 task :repo_restore => :environment do
137 130 backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
138 131 puts "Restoring repositories:"
139   - project = Project.all.map { |n| [n.path,n.path_to_repo] }
  132 + project = Project.all.map { |n| [n.path, n.path_to_repo] }
140 133 project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
141 134 project.each do |project|
142 135 print "- Restoring repository #{project.first}... "
143   - FileUtils.rm_rf(project.second) if File.dirname(project.second) # delet old stuff
  136 + FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff
144 137 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")
145 138 permission_commands = [
146 139 "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}",
... ... @@ -157,8 +150,9 @@ namespace :gitlab do
157 150 ###################################### DB ######################################
158 151  
159 152 task :db_dump => :environment do
160   - backup_path_db = File.join(Gitlab.config.backup_path, "db")
161   - FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db)
  153 + backup_path_db = File.join(Gitlab.config.backup_path, "db")
  154 + FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db)
  155 +
162 156 puts "Dumping database tables:"
163 157 ActiveRecord::Base.connection.tables.each do |tbl|
164 158 print "- Dumping table #{tbl}... "
... ... @@ -176,9 +170,11 @@ namespace :gitlab do
176 170 end
177 171  
178 172 task :db_restore=> :environment do
179   - backup_path_db = File.join(Gitlab.config.backup_path, "db")
  173 + backup_path_db = File.join(Gitlab.config.backup_path, "db")
  174 +
180 175 puts "Restoring database tables:"
181 176 Rake::Task["db:reset"].invoke
  177 +
182 178 Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
183 179 fixture_file = File.basename(dir, ".*" )
184 180 print "- Loading fixture #{fixture_file}..."
... ...
lib/tasks/gitlab/enable_automerge.rake
1 1 namespace :gitlab do
2 2 namespace :app do
3 3 desc "GITLAB | Enable auto merge"
4   - task :enable_automerge => :environment do
  4 + task :enable_automerge => :environment do
5 5 Gitlab::Gitolite.new.enable_automerge
6 6  
7 7 Project.find_each do |project|
... ...
lib/tasks/gitlab/gitolite_rebuild.rake
1 1 namespace :gitlab do
2 2 namespace :gitolite do
3 3 desc "GITLAB | Rebuild each project at gitolite config"
4   - task :update_repos => :environment do
  4 + task :update_repos => :environment do
5 5 puts "Starting Projects"
6 6 Project.find_each(:batch_size => 100) do |project|
7   - puts
8   - puts "=== #{project.name}"
  7 + puts "\n=== #{project.name}"
9 8 project.update_repository
10 9 puts
11 10 end
... ...
lib/tasks/gitlab/setup.rake
... ... @@ -4,8 +4,7 @@ namespace :gitlab do
4 4 task :setup => [
5 5 'db:setup',
6 6 'db:seed_fu',
7   - 'gitlab:app:enable_automerge'
  7 + 'gitlab:app:enable_automerge'
8 8 ]
9 9 end
10 10 end
11   -
... ...
lib/tasks/gitlab/status.rake
1 1 namespace :gitlab do
2 2 namespace :app do
3   - desc "GITLAB | Check gitlab installation status"
  3 + desc "GITLAB | Check GitLab installation status"
4 4 task :status => :environment do
5   - puts "Starting diagnostic".yellow
  5 + puts "Starting diagnostics".yellow
6 6 git_base_path = Gitlab.config.git_base_path
7 7  
8 8 print "config/database.yml............"
9   - if File.exists?(File.join Rails.root, "config", "database.yml")
  9 + if File.exists?(Rails.root.join "config", "database.yml")
10 10 puts "exists".green
11   - else
  11 + else
12 12 puts "missing".red
13 13 return
14 14 end
15 15  
16 16 print "config/gitlab.yml............"
17   - if File.exists?(File.join Rails.root, "config", "gitlab.yml")
18   - puts "exists".green
  17 + if File.exists?(Rails.root.join "config", "gitlab.yml")
  18 + puts "exists".green
19 19 else
20 20 puts "missing".red
21 21 return
22 22 end
23 23  
24 24 print "#{git_base_path}............"
25   - if File.exists?(git_base_path)
26   - puts "exists".green
27   - else
  25 + if File.exists?(git_base_path)
  26 + puts "exists".green
  27 + else
28 28 puts "missing".red
29 29 return
30 30 end
31 31  
32 32 print "#{git_base_path} is writable?............"
33 33 if File.stat(git_base_path).writable?
34   - puts "YES".green
  34 + puts "YES".green
35 35 else
36 36 puts "NO".red
37 37 return
... ... @@ -41,16 +41,16 @@ namespace :gitlab do
41 41 `git clone #{Gitlab.config.gitolite_admin_uri} /tmp/gitolite_gitlab_test`
42 42 FileUtils.rm_rf("/tmp/gitolite_gitlab_test")
43 43 print "Can clone gitolite-admin?............"
44   - puts "YES".green
45   - rescue
  44 + puts "YES".green
  45 + rescue
46 46 print "Can clone gitolite-admin?............"
47 47 puts "NO".red
48 48 return
49 49 end
50 50  
51 51 print "UMASK for .gitolite.rc is 0007? ............"
52   - unless open("#{git_base_path}/../.gitolite.rc").grep(/UMASK([ \t]*)=([ \t>]*)0007/).empty?
53   - puts "YES".green
  52 + if open("#{git_base_path}/../.gitolite.rc").grep(/UMASK([ \t]*)=([ \t>]*)0007/).any?
  53 + puts "YES".green
54 54 else
55 55 puts "NO".red
56 56 return
... ... @@ -69,16 +69,15 @@ namespace :gitlab do
69 69 end
70 70 end
71 71  
72   -
73   - if Project.count > 0
  72 + if Project.count > 0
74 73 puts "Validating projects repositories:".yellow
75 74 Project.find_each(:batch_size => 100) do |project|
76 75 print "#{project.name}....."
77   - hook_file = File.join(project.path_to_repo, 'hooks','post-receive')
  76 + hook_file = File.join(project.path_to_repo, 'hooks', 'post-receive')
78 77  
79 78 unless File.exists?(hook_file)
80   - puts "post-receive file missing".red
81   - next
  79 + puts "post-receive file missing".red
  80 + return
82 81 end
83 82  
84 83 puts "post-receive file ok".green
... ...
lib/tasks/gitlab/write_hook.rake
... ... @@ -4,7 +4,6 @@ namespace :gitlab do
4 4 task :write_hooks => :environment do
5 5 gitolite_hooks_path = File.join(Gitlab.config.git_hooks_path, "common")
6 6 gitlab_hooks_path = Rails.root.join("lib", "hooks")
7   -
8 7 gitlab_hook_files = ['post-receive']
9 8  
10 9 gitlab_hook_files.each do |file_name|
... ... @@ -20,4 +19,3 @@ namespace :gitlab do
20 19 end
21 20 end
22 21 end
23   -
... ...