Commit 9b07ed06901a584d4928c46b622eac9fed81ab61

Authored by Dmitriy Zaporozhets
2 parents 2a3f5dae 1acff973

Merge pull request #2323 from riyad/rename-tasks

Rename tasks
doc/raketasks/backup_restore.md
@@ -4,7 +4,7 @@ Creates a backup archive of the database and all repositories. This archive will @@ -4,7 +4,7 @@ Creates a backup archive of the database and all repositories. This archive will
4 The filename will be `[TIMESTAMP]_gitlab_backup.tar`. This timestamp can be used to restore an specific backup. 4 The filename will be `[TIMESTAMP]_gitlab_backup.tar`. This timestamp can be used to restore an specific backup.
5 5
6 ``` 6 ```
7 -bundle exec rake gitlab:app:backup_create 7 +bundle exec rake gitlab:backup:create
8 ``` 8 ```
9 9
10 Example output: 10 Example output:
@@ -40,7 +40,7 @@ Deleting old backups... [SKIPPING] @@ -40,7 +40,7 @@ Deleting old backups... [SKIPPING]
40 ### Restore a previously created backup 40 ### Restore a previously created backup
41 41
42 ``` 42 ```
43 -bundle exec rake gitlab:app:backup_restore 43 +bundle exec rake gitlab:backup:restore
44 ``` 44 ```
45 45
46 Options: 46 Options:
doc/raketasks/features.md
1 ### Enable usernames and namespaces for user projects 1 ### Enable usernames and namespaces for user projects
2 2
3 -This command will enable the namespace feature introduced in v4.0. It will move every project in its namespace folder. 3 +This command will enable the namespaces feature introduced in v4.0. It will move every project in its namespace folder.
4 4
5 Note: 5 Note:
6 6
@@ -13,7 +13,7 @@ Old path: `git@example.org:myrepo.git` @@ -13,7 +13,7 @@ Old path: `git@example.org:myrepo.git`
13 New path: `git@example.org:username/myrepo.git` or `git@example.org:groupname/myrepo.git` 13 New path: `git@example.org:username/myrepo.git` or `git@example.org:groupname/myrepo.git`
14 14
15 ``` 15 ```
16 -bundle exec rake gitlab:activate_namespaces 16 +bundle exec rake gitlab:enable_namespaces
17 ``` 17 ```
18 18
19 19
@@ -22,7 +22,7 @@ bundle exec rake gitlab:activate_namespaces @@ -22,7 +22,7 @@ bundle exec rake gitlab:activate_namespaces
22 This command will enable the auto merge feature. After this you will be able to **merge a merge request** via GitLab and use the **online editor**. 22 This command will enable the auto merge feature. After this you will be able to **merge a merge request** via GitLab and use the **online editor**.
23 23
24 ``` 24 ```
25 -bundle exec rake gitlab:app:enable_automerge 25 +bundle exec rake gitlab:enable_automerge
26 ``` 26 ```
27 27
28 Example output: 28 Example output:
doc/raketasks/maintenance.md
@@ -139,6 +139,16 @@ Checking GitLab ... Finished @@ -139,6 +139,16 @@ Checking GitLab ... Finished
139 ``` 139 ```
140 140
141 141
  142 +### (Re-)Create satellite repos
  143 +
  144 +This will create satellite repos for all your projects.
  145 +If necessary, remove the `tmp/repo_satellites` directory and rerun the command below.
  146 +
  147 +```
  148 +bundle exec rake gitlab:satellites:create
  149 +```
  150 +
  151 +
142 ### Rebuild each key at gitolite config 152 ### Rebuild each key at gitolite config
143 153
144 This will send all users ssh public keys to gitolite and grant them access (based on their permission) to their projects. 154 This will send all users ssh public keys to gitolite and grant them access (based on their permission) to their projects.
doc/raketasks/user_management.md
1 ### Add user to as a developer to all projects 1 ### Add user to as a developer to all projects
2 2
3 ``` 3 ```
4 -bundle exec rake add_user_to_project_teams[username@domain.tld] 4 +bundle exec rake gitlab:import:user_to_projects[username@domain.tld]
5 ``` 5 ```
6 6
7 7
@@ -12,5 +12,5 @@ Notes: @@ -12,5 +12,5 @@ Notes:
12 * admin users are added as masters 12 * admin users are added as masters
13 13
14 ``` 14 ```
15 -bundle exec rake add_users_to_project_teams 15 +bundle exec rake gitlab:import:all_users_to_all_projects
16 ``` 16 ```
lib/tasks/bulk_add_permission.rake
@@ -1,20 +0,0 @@ @@ -1,20 +0,0 @@
1 -desc "Add all users to all projects (admin users are added as masters)"  
2 -task :add_users_to_project_teams => :environment do |t, args|  
3 - user_ids = User.where(:admin => false).pluck(:id)  
4 - admin_ids = User.where(:admin => true).pluck(:id)  
5 -  
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)  
11 - end  
12 -end  
13 -  
14 -desc "Add user to as a developer to all projects"  
15 -task :add_user_to_project_teams, [:email] => :environment do |t, args|  
16 - user = User.find_by_email args.email  
17 - project_ids = Project.pluck(:id)  
18 -  
19 - UsersProject.user_bulk_import(user, project_ids, UsersProject::DEVELOPER)  
20 -end  
lib/tasks/gitlab/activate_namespaces.rake
@@ -1,67 +0,0 @@ @@ -1,67 +0,0 @@
1 -namespace :gitlab do  
2 - desc "GITLAB | Enable usernames and namespaces for user projects"  
3 - task activate_namespaces: :environment do  
4 - print "\nUsernames for users:".yellow  
5 -  
6 - User.find_each(batch_size: 500) do |user|  
7 - next if user.namespace  
8 -  
9 - User.transaction do  
10 - username = user.email.match(/^[^@]*/)[0]  
11 - if user.update_attributes!(username: username)  
12 - print '.'.green  
13 - else  
14 - print 'F'.red  
15 - end  
16 - end  
17 - end  
18 -  
19 - print "\n\nDirs for groups:".yellow  
20 -  
21 - Group.find_each(batch_size: 500) do |group|  
22 - if group.ensure_dir_exist  
23 - print '.'.green  
24 - else  
25 - print 'F'.red  
26 - end  
27 - end  
28 -  
29 - print "\n\nMove projects from groups under groups dirs:".yellow  
30 - git_path = Gitlab.config.gitolite.repos_path  
31 -  
32 - Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project|  
33 - next unless project.group  
34 -  
35 - group = project.group  
36 -  
37 - puts "\n"  
38 - print " * #{project.name}: "  
39 -  
40 - new_path = File.join(git_path, project.path_with_namespace + '.git')  
41 -  
42 - if File.exists?(new_path)  
43 - print "ok. already at #{new_path}".cyan  
44 - next  
45 - end  
46 -  
47 - old_path = File.join(git_path, project.path + '.git')  
48 -  
49 - unless File.exists?(old_path)  
50 - print "missing. not found at #{old_path}".red  
51 - next  
52 - end  
53 -  
54 - begin  
55 - Gitlab::ProjectMover.new(project, '', group.path).execute  
56 - print "ok. Moved to #{new_path}".green  
57 - rescue  
58 - print "Failed moving to #{new_path}".red  
59 - end  
60 - end  
61 -  
62 - print "\n\nRebuild gitolite:".yellow  
63 - gitolite = Gitlab::Gitolite.new  
64 - gitolite.update_repositories(Project.where('namespace_id IS NOT NULL'))  
65 - puts "\n"  
66 - end  
67 -end  
lib/tasks/gitlab/backup.rake
1 require 'active_record/fixtures' 1 require 'active_record/fixtures'
2 2
3 namespace :gitlab do 3 namespace :gitlab do
4 - namespace :app do 4 + namespace :backup do
5 # Create backup of GitLab system 5 # Create backup of GitLab system
6 desc "GITLAB | Create a backup of the GitLab system" 6 desc "GITLAB | Create a backup of the GitLab system"
7 - task :backup_create => :environment do  
8 - Rake::Task["gitlab:app:db_dump"].invoke  
9 - Rake::Task["gitlab:app:repo_dump"].invoke 7 + task :create => :environment do
  8 + Rake::Task["gitlab:backup:db:create"].invoke
  9 + Rake::Task["gitlab:backup:repo:create"].invoke
10 10
11 Dir.chdir(Gitlab.config.backup.path) 11 Dir.chdir(Gitlab.config.backup.path)
12 12
@@ -54,7 +54,7 @@ namespace :gitlab do @@ -54,7 +54,7 @@ namespace :gitlab do
54 54
55 # Restore backup of GitLab system 55 # Restore backup of GitLab system
56 desc "GITLAB | Restore a previously created backup" 56 desc "GITLAB | Restore a previously created backup"
57 - task :backup_restore => :environment do 57 + task :restore => :environment do
58 Dir.chdir(Gitlab.config.backup.path) 58 Dir.chdir(Gitlab.config.backup.path)
59 59
60 # check for existing backups in the backup dir 60 # check for existing backups in the backup dir
@@ -62,7 +62,7 @@ namespace :gitlab do @@ -62,7 +62,7 @@ namespace :gitlab do
62 puts "no backups found" if file_list.count == 0 62 puts "no backups found" if file_list.count == 0
63 if file_list.count > 1 && ENV["BACKUP"].nil? 63 if file_list.count > 1 && ENV["BACKUP"].nil?
64 puts "Found more than one backup, please specify which one you want to restore:" 64 puts "Found more than one backup, please specify which one you want to restore:"
65 - puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup" 65 + puts "rake gitlab:backup:restore BACKUP=timestamp_of_backup"
66 exit 1; 66 exit 1;
67 end 67 end
68 68
@@ -93,8 +93,8 @@ namespace :gitlab do @@ -93,8 +93,8 @@ namespace :gitlab do
93 exit 1 93 exit 1
94 end 94 end
95 95
96 - Rake::Task["gitlab:app:db_restore"].invoke  
97 - Rake::Task["gitlab:app:repo_restore"].invoke 96 + Rake::Task["gitlab:backup:db:restore"].invoke
  97 + Rake::Task["gitlab:backup:repo:restore"].invoke
98 98
99 # cleanup: remove tmp files 99 # cleanup: remove tmp files
100 print "Deleting tmp directories..." 100 print "Deleting tmp directories..."
@@ -110,82 +110,86 @@ namespace :gitlab do @@ -110,82 +110,86 @@ namespace :gitlab do
110 110
111 ################################# REPOSITORIES ################################# 111 ################################# REPOSITORIES #################################
112 112
113 - task :repo_dump => :environment do  
114 - backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")  
115 - FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)  
116 - puts "Dumping repositories:"  
117 - project = Project.all.map { |n| [n.path, n.path_to_repo] }  
118 - project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]  
119 - project.each do |project|  
120 - print "- Dumping repository #{project.first}... "  
121 - if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1")  
122 - puts "[DONE]".green  
123 - else  
124 - puts "[FAILED]".red 113 + namespace :repo do
  114 + task :create => :environment do
  115 + backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
  116 + FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
  117 + puts "Dumping repositories:"
  118 + project = Project.all.map { |n| [n.path, n.path_to_repo] }
  119 + project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
  120 + project.each do |project|
  121 + print "- Dumping repository #{project.first}... "
  122 + if Kernel.system("cd #{project.second} > /dev/null 2>&1 && git bundle create #{backup_path_repo}/#{project.first}.bundle --all > /dev/null 2>&1")
  123 + puts "[DONE]".green
  124 + else
  125 + puts "[FAILED]".red
  126 + end
125 end 127 end
126 end 128 end
127 - end  
128 129
129 - task :repo_restore => :environment do  
130 - backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")  
131 - puts "Restoring repositories:"  
132 - project = Project.all.map { |n| [n.path, n.path_to_repo] }  
133 - project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]  
134 - project.each do |project|  
135 - print "- Restoring repository #{project.first}... "  
136 - FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff  
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")  
138 - permission_commands = [  
139 - "sudo chmod -R g+rwX #{Gitlab.config.gitolite.repos_path}",  
140 - "sudo chown -R #{Gitlab.config.gitolite.ssh_user}:#{Gitlab.config.gitolite.ssh_user} #{Gitlab.config.gitolite.repos_path}"  
141 - ]  
142 - permission_commands.each { |command| Kernel.system(command) }  
143 - puts "[DONE]".green  
144 - else  
145 - puts "[FAILED]".red 130 + task :restore => :environment do
  131 + backup_path_repo = File.join(Gitlab.config.backup.path, "repositories")
  132 + puts "Restoring repositories:"
  133 + project = Project.all.map { |n| [n.path, n.path_to_repo] }
  134 + project << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
  135 + project.each do |project|
  136 + print "- Restoring repository #{project.first}... "
  137 + FileUtils.rm_rf(project.second) if File.dirname(project.second) # delete old stuff
  138 + 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")
  139 + permission_commands = [
  140 + "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}",
  141 + "sudo chown -R #{Gitlab.config.ssh_user}:#{Gitlab.config.ssh_user} #{Gitlab.config.git_base_path}"
  142 + ]
  143 + permission_commands.each { |command| Kernel.system(command) }
  144 + puts "[DONE]".green
  145 + else
  146 + puts "[FAILED]".red
  147 + end
146 end 148 end
147 end 149 end
148 end 150 end
149 151
150 ###################################### DB ###################################### 152 ###################################### DB ######################################
151 153
152 - task :db_dump => :environment do  
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 -  
156 - puts "Dumping database tables:"  
157 - ActiveRecord::Base.connection.tables.each do |tbl|  
158 - print "- Dumping table #{tbl}... "  
159 - count = 1  
160 - File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file|  
161 - ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line|  
162 - line.delete_if{|k,v| v.blank?}  
163 - output = {tbl + '_' + count.to_s => line}  
164 - file << output.to_yaml.gsub(/^---\n/,'') + "\n"  
165 - count += 1 154 + namespace :db do
  155 + task :create => :environment do
  156 + backup_path_db = File.join(Gitlab.config.backup.path, "db")
  157 + FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db)
  158 +
  159 + puts "Dumping database tables:"
  160 + ActiveRecord::Base.connection.tables.each do |tbl|
  161 + print "- Dumping table #{tbl}... "
  162 + count = 1
  163 + File.open(File.join(backup_path_db, tbl + ".yml"), "w+") do |file|
  164 + ActiveRecord::Base.connection.select_all("SELECT * FROM `#{tbl}`").each do |line|
  165 + line.delete_if{|k,v| v.blank?}
  166 + output = {tbl + '_' + count.to_s => line}
  167 + file << output.to_yaml.gsub(/^---\n/,'') + "\n"
  168 + count += 1
  169 + end
  170 + puts "[DONE]".green
166 end 171 end
167 - puts "[DONE]".green  
168 end 172 end
169 end 173 end
170 - end  
171 174
172 - task :db_restore=> :environment do  
173 - backup_path_db = File.join(Gitlab.config.backup.path, "db") 175 + task :restore=> :environment do
  176 + backup_path_db = File.join(Gitlab.config.backup.path, "db")
174 177
175 - puts "Restoring database tables:"  
176 - Rake::Task["db:reset"].invoke 178 + puts "Restoring database tables:"
  179 + Rake::Task["db:reset"].invoke
177 180
178 - Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|  
179 - fixture_file = File.basename(dir, ".*" )  
180 - print "- Loading fixture #{fixture_file}..."  
181 - if File.size(dir) > 0  
182 - ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file)  
183 - puts "[DONE]".green  
184 - else  
185 - puts "[SKIPPING]".yellow 181 + Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
  182 + fixture_file = File.basename(dir, ".*" )
  183 + print "- Loading fixture #{fixture_file}..."
  184 + if File.size(dir) > 0
  185 + ActiveRecord::Fixtures.create_fixtures(backup_path_db, fixture_file)
  186 + puts "[DONE]".green
  187 + else
  188 + puts "[SKIPPING]".yellow
  189 + end
186 end 190 end
187 end 191 end
188 end 192 end
189 193
190 - end # namespace end: app 194 + end # namespace end: backup
191 end # namespace end: gitlab 195 end # namespace end: gitlab
lib/tasks/gitlab/bulk_add_permission.rake 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +namespace :gitlab do
  2 + namespace :import do
  3 + desc "GITLAB | Add all users to all projects (admin users are added as masters)"
  4 + task :all_users_to_all_projects => :environment do |t, args|
  5 + user_ids = User.where(:admin => false).pluck(:id)
  6 + admin_ids = User.where(:admin => true).pluck(:id)
  7 +
  8 + Project.find_each do |project|
  9 + puts "Importing #{user_ids.size} users into #{project.code}"
  10 + UsersProject.bulk_import(project, user_ids, UsersProject::DEVELOPER)
  11 + puts "Importing #{admin_ids.size} admins into #{project.code}"
  12 + UsersProject.bulk_import(project, admin_ids, UsersProject::MASTER)
  13 + end
  14 + end
  15 +
  16 + desc "GITLAB | Add a specific user to all projects (as a developer)"
  17 + task :user_to_projects, [:email] => :environment do |t, args|
  18 + user = User.find_by_email args.email
  19 + project_ids = Project.pluck(:id)
  20 +
  21 + UsersProject.user_bulk_import(user, project_ids, UsersProject::DEVELOPER)
  22 + end
  23 + end
  24 +end
0 \ No newline at end of file 25 \ No newline at end of file
lib/tasks/gitlab/check.rake
@@ -192,7 +192,9 @@ namespace :gitlab do @@ -192,7 +192,9 @@ namespace :gitlab do
192 else 192 else
193 puts "no".red 193 puts "no".red
194 try_fixing_it( 194 try_fixing_it(
195 - "sudo -u gitlab -H bundle exec rake gitlab:app:enable_automerge" 195 + "sudo -u gitlab -H bundle exec rake gitlab:satellites:create",
  196 + "If necessary, remove the tmp/repo_satellites directory ...",
  197 + "... and rerun the above command"
196 ) 198 )
197 for_more_information( 199 for_more_information(
198 "doc/raketasks/maintenance.md " 200 "doc/raketasks/maintenance.md "
lib/tasks/gitlab/enable_automerge.rake
1 namespace :gitlab do 1 namespace :gitlab do
2 - namespace :app do  
3 - desc "GITLAB | Enable auto merge"  
4 - task :enable_automerge => :environment do  
5 - Gitlab::Gitolite.new.enable_automerge 2 + desc "GITLAB | Enable auto merge"
  3 + task :enable_automerge => :environment do
  4 + Gitlab::Gitolite.new.enable_automerge
6 5
7 - Project.find_each do |project|  
8 - if project.repo_exists? && !project.satellite.exists?  
9 - puts "Creating satellite for #{project.name}...".green  
10 - project.satellite.create  
11 - end 6 + Project.find_each do |project|
  7 + if project.repo_exists? && !project.satellite.exists?
  8 + puts "Creating satellite for #{project.name}...".green
  9 + project.satellite.create
12 end 10 end
13 -  
14 - puts "Done!".green  
15 end 11 end
  12 +
  13 + puts "Done!".green
  14 + end
  15 +
  16 + namespace :satellites do
  17 + desc "GITLAB | Create satellite repos"
  18 + task create: 'gitlab:enable_automerge'
16 end 19 end
17 end 20 end
lib/tasks/gitlab/enable_namespaces.rake 0 → 100644
@@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
  1 +namespace :gitlab do
  2 + desc "GITLAB | Enable usernames and namespaces for user projects"
  3 + task enable_namespaces: :environment do
  4 + print "\nUsernames for users:".yellow
  5 +
  6 + User.find_each(batch_size: 500) do |user|
  7 + next if user.namespace
  8 +
  9 + User.transaction do
  10 + username = user.email.match(/^[^@]*/)[0]
  11 + if user.update_attributes!(username: username)
  12 + print '.'.green
  13 + else
  14 + print 'F'.red
  15 + end
  16 + end
  17 + end
  18 +
  19 + print "\n\nDirs for groups:".yellow
  20 +
  21 + Group.find_each(batch_size: 500) do |group|
  22 + if group.ensure_dir_exist
  23 + print '.'.green
  24 + else
  25 + print 'F'.red
  26 + end
  27 + end
  28 +
  29 + print "\n\nMove projects from groups under groups dirs:".yellow
  30 + git_path = Gitlab.config.gitolite.repos_path
  31 +
  32 + Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project|
  33 + next unless project.group
  34 +
  35 + group = project.group
  36 +
  37 + puts "\n"
  38 + print " * #{project.name}: "
  39 +
  40 + new_path = File.join(git_path, project.path_with_namespace + '.git')
  41 +
  42 + if File.exists?(new_path)
  43 + print "ok. already at #{new_path}".cyan
  44 + next
  45 + end
  46 +
  47 + old_path = File.join(git_path, project.path + '.git')
  48 +
  49 + unless File.exists?(old_path)
  50 + print "missing. not found at #{old_path}".red
  51 + next
  52 + end
  53 +
  54 + begin
  55 + Gitlab::ProjectMover.new(project, '', group.path).execute
  56 + print "ok. Moved to #{new_path}".green
  57 + rescue
  58 + print "Failed moving to #{new_path}".red
  59 + end
  60 + end
  61 +
  62 + print "\n\nRebuild gitolite:".yellow
  63 + gitolite = Gitlab::Gitolite.new
  64 + gitolite.update_repositories(Project.where('namespace_id IS NOT NULL'))
  65 + puts "\n"
  66 + end
  67 +end
lib/tasks/gitlab/setup.rake
@@ -4,7 +4,7 @@ namespace :gitlab do @@ -4,7 +4,7 @@ namespace :gitlab do
4 task :setup => [ 4 task :setup => [
5 'db:setup', 5 'db:setup',
6 'db:seed_fu', 6 'db:seed_fu',
7 - 'gitlab:app:enable_automerge' 7 + 'gitlab:enable_automerge'
8 ] 8 ]
9 end 9 end
10 end 10 end