Commit 83696b12 authored by Nihad Abbasov's avatar Nihad Abbasov

cleanup rake tasks

parent 30ee5362
desc "Add all users to all projects, system administratos are added as masters" desc "Add all users to all projects (admin users are added as masters)"
task :add_users_to_project_teams => :environment do |t, args| task :add_users_to_project_teams => :environment do |t, args|
users = User.find_all_by_admin(false, :select => 'id').map(&:id) user_ids = User.where(:admin => false).pluck(:id)
admins = User.find_all_by_admin(true, :select => 'id').map(&:id) admin_ids = User.where(:admin => true).pluck(:id)
users.each do |user| Project.find_each do |project|
puts "#{user}" puts "Importing #{user_ids.size} users into #{project.code}"
end UsersProject.bulk_import(project, user_ids, UsersProject::DEVELOPER)
puts "Importing #{admin_ids.size} admins into #{project.code}"
Project.all.each do |project| UsersProject.bulk_import(project, admin_ids, UsersProject::MASTER)
puts "Importing #{users.length} users into #{project.path}"
UsersProject.bulk_import(project, users, UsersProject::DEVELOPER)
puts "Importing #{admins.length} admins into #{project.path}"
UsersProject.bulk_import(project, admins, UsersProject::MASTER)
end end
end end
desc "Add user to as a developer to all projects" desc "Add user to as a developer to all projects"
task :add_user_to_project_teams, [:email] => :environment do |t, args| task :add_user_to_project_teams, [:email] => :environment do |t, args|
user_email = args.email user = User.find_by_email args.email
user = User.find_by_email(user_email) project_ids = Project.pluck(:id)
project_ids = Project.all.map(&:id)
UsersProject.user_bulk_import(user,project_ids,UsersProject::DEVELOPER) UsersProject.user_bulk_import(user, project_ids, UsersProject::DEVELOPER)
end end
desc "Imports existing Git repos from a directory into new projects in git_base_path" desc "Imports existing Git repos from a directory into new projects in git_base_path"
task :import_projects, [:directory,:email] => :environment do |t, args| task :import_projects, [:directory,:email] => :environment do |t, args|
user_email = args.email user_email, import_directory = args.email, args.directory
import_directory = args.directory
repos_to_import = Dir.glob("#{import_directory}/*") repos_to_import = Dir.glob("#{import_directory}/*")
git_base_path = Gitlab.config.git_base_path git_base_path = Gitlab.config.git_base_path
puts "Found #{repos_to_import.length} repos to import" imported_count, skipped_count, failed_count = 0
puts "Found #{repos_to_import.size} repos to import"
imported_count = 0
skipped_count = 0
failed_count = 0
repos_to_import.each do |repo_path| repos_to_import.each do |repo_path|
repo_name = File.basename repo_path repo_name = File.basename repo_path
clone_path = "#{git_base_path}#{repo_name}.git"
puts " Processing #{repo_name}" puts " Processing #{repo_name}"
clone_path = "#{git_base_path}#{repo_name}.git"
if Dir.exists? clone_path if Dir.exists? clone_path
if Project.find_by_code(repo_name) if Project.find_by_code(repo_name)
...@@ -38,7 +35,6 @@ task :import_projects, [:directory,:email] => :environment do |t, args| ...@@ -38,7 +35,6 @@ task :import_projects, [:directory,:email] => :environment do |t, args|
else else
failed_count += 1 failed_count += 1
end end
end end
puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})." 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,63 +45,39 @@ def clone_bare_repo_as_git(existing_path, new_path)
git_user = Gitlab.config.ssh_user git_user = Gitlab.config.ssh_user
begin begin
sh "sudo -u #{git_user} -i git clone --bare '#{existing_path}' #{new_path}" sh "sudo -u #{git_user} -i git clone --bare '#{existing_path}' #{new_path}"
true rescue Exception => msg
rescue Exception=> msg puts " ERROR: Failed to clone #{existing_path} to #{new_path}"
puts " ERROR: Faild to clone #{existing_path} to #{new_path}"
puts " Make sure #{git_user} can reach #{existing_path}" puts " Make sure #{git_user} can reach #{existing_path}"
puts " Exception-MSG: #{msg}" puts " Exception-MSG: #{msg}"
false
end end
end end
# Creats a project in Gitlag given a @project_name@ to use (for name, web url, and code # Creates a project in GitLab given a `project_name` to use
# url) and a @user_email@ that will be assigned as the owner of the project. # (for name, web url, and code url) and a `user_email` that will be
# assigned as the owner of the project.
def create_repo_project(project_name, user_email) def create_repo_project(project_name, user_email)
user = User.find_by_email(user_email) if user = User.find_by_email(user_email)
if user
# Using find_by_code since that's the most important identifer to be unique # Using find_by_code since that's the most important identifer to be unique
if Project.find_by_code(project_name) if Project.find_by_code(project_name)
puts " INFO: Project #{project_name} already exists in Gitlab, skipping." puts " INFO: Project #{project_name} already exists in Gitlab, skipping."
false
else
project = nil
if Project.find_by_code(project_name)
puts " ERROR: Project already exists #{project_name}"
return false
project = Project.find_by_code(project_name)
else else
project = Project.create( project = Project.create(
name: project_name, name: project_name,
code: project_name, code: project_name,
path: project_name, path: project_name,
owner: user, owner: user,
description: "Automatically created from Rake on #{Time.now.to_s}" description: "Automatically created from 'import_projects' rake task on #{Time.now}"
) )
end
unless project.valid?
puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
return false
end
if project.valid?
# Add user as admin for project # Add user as admin for project
project.users_projects.create!( project.users_projects.create!(:project_access => UsersProject::MASTER, :user => user)
:project_access => UsersProject::MASTER,
:user => user
)
# Per projects_controller.rb#37
project.update_repository project.update_repository
if project.valid?
true
else else
puts " ERROR: Failed to create project #{project} because #{project.errors.first}" puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
false
end end
end end
else else
puts " ERROR: #{user_email} not found, skipping" puts " ERROR: user with #{user_email} not found, skipping"
false
end end
end end
...@@ -2,22 +2,20 @@ require 'active_record/fixtures' ...@@ -2,22 +2,20 @@ require 'active_record/fixtures'
namespace :gitlab do namespace :gitlab do
namespace :app do namespace :app do
# Create backup of GitLab system
# Create backup of gitlab system desc "GITLAB | Create a backup of the GitLab system"
desc "GITLAB | Create a backup of the gitlab system"
task :backup_create => :environment do task :backup_create => :environment do
Rake::Task["gitlab:app:db_dump"].invoke Rake::Task["gitlab:app:db_dump"].invoke
Rake::Task["gitlab:app:repo_dump"].invoke Rake::Task["gitlab:app:repo_dump"].invoke
Dir.chdir(Gitlab.config.backup_path) Dir.chdir(Gitlab.config.backup_path)
# saving additional informations # saving additional informations
s = Hash.new s = {}
s["db_version"] = "#{ActiveRecord::Migrator.current_version}" s[:db_version] = "#{ActiveRecord::Migrator.current_version}"
s["backup_created_at"] = "#{Time.now}" s[:backup_created_at] = "#{Time.now}"
s["gitlab_version"] = %x{git rev-parse HEAD}.gsub(/\n/,"") s[:gitlab_version] = %x{git rev-parse HEAD}.gsub(/\n/,"")
s["tar_version"] = %x{tar --version | head -1}.gsub(/\n/,"") s[:tar_version] = %x{tar --version | head -1}.gsub(/\n/,"")
File.open("#{Gitlab.config.backup_path}/backup_information.yml", "w+") do |file| File.open("#{Gitlab.config.backup_path}/backup_information.yml", "w+") do |file|
file << s.to_yaml.gsub(/^---\n/,'') file << s.to_yaml.gsub(/^---\n/,'')
...@@ -32,7 +30,7 @@ namespace :gitlab do ...@@ -32,7 +30,7 @@ namespace :gitlab do
end end
# cleanup: remove tmp files # cleanup: remove tmp files
print "Deletion of tmp directories..." print "Deleting tmp directories..."
if Kernel.system("rm -rf repositories/ db/ backup_information.yml") if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
puts "[DONE]".green puts "[DONE]".green
else else
...@@ -52,26 +50,23 @@ namespace :gitlab do ...@@ -52,26 +50,23 @@ namespace :gitlab do
else else
puts "[SKIPPING]".yellow puts "[SKIPPING]".yellow
end end
end end
# Restore backup of GitLab system
# Restore backup of gitlab system
desc "GITLAB | Restore a previously created backup" desc "GITLAB | Restore a previously created backup"
task :backup_restore => :environment do task :backup_restore => :environment do
Dir.chdir(Gitlab.config.backup_path) Dir.chdir(Gitlab.config.backup_path)
# check for existing backups in the backup dir # check for existing backups in the backup dir
file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i } file_list = Dir.glob("*_gitlab_backup.tar").each.map { |f| f.split(/_/).first.to_i }
puts "no backup found" if file_list.count == 0 puts "no backups found" if file_list.count == 0
if file_list.count > 1 && ENV["BACKUP"].nil? if file_list.count > 1 && ENV["BACKUP"].nil?
puts "Found more than one backup, please specify which one you want to restore:" puts "Found more than one backup, please specify which one you want to restore:"
puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup" puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup"
exit 1; exit 1;
end end
tar_file = ENV["BACKUP"].nil? ? File.join(file_list.first.to_s + "_gitlab_backup.tar") : File.join(ENV["BACKUP"] + "_gitlab_backup.tar") 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) unless File.exists?(tar_file)
puts "The specified backup doesn't exist!" puts "The specified backup doesn't exist!"
...@@ -102,16 +97,14 @@ namespace :gitlab do ...@@ -102,16 +97,14 @@ namespace :gitlab do
Rake::Task["gitlab:app:repo_restore"].invoke Rake::Task["gitlab:app:repo_restore"].invoke
# cleanup: remove tmp files # cleanup: remove tmp files
print "Deletion of tmp directories..." print "Deleting tmp directories..."
if Kernel.system("rm -rf repositories/ db/ backup_information.yml") if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
puts "[DONE]".green puts "[DONE]".green
else else
puts "[FAILED]".red puts "[FAILED]".red
end end
end end
################################################################################ ################################################################################
################################# invoked tasks ################################ ################################# invoked tasks ################################
...@@ -121,7 +114,7 @@ namespace :gitlab do ...@@ -121,7 +114,7 @@ namespace :gitlab do
backup_path_repo = File.join(Gitlab.config.backup_path, "repositories") backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo) FileUtils.mkdir_p(backup_path_repo) until Dir.exists?(backup_path_repo)
puts "Dumping repositories:" puts "Dumping repositories:"
project = Project.all.map { |n| [n.path,n.path_to_repo] } 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 << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
project.each do |project| project.each do |project|
print "- Dumping repository #{project.first}... " print "- Dumping repository #{project.first}... "
...@@ -136,11 +129,11 @@ namespace :gitlab do ...@@ -136,11 +129,11 @@ namespace :gitlab do
task :repo_restore => :environment do task :repo_restore => :environment do
backup_path_repo = File.join(Gitlab.config.backup_path, "repositories") backup_path_repo = File.join(Gitlab.config.backup_path, "repositories")
puts "Restoring repositories:" puts "Restoring repositories:"
project = Project.all.map { |n| [n.path,n.path_to_repo] } 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 << ["gitolite-admin.git", File.join(File.dirname(project.first.second), "gitolite-admin.git")]
project.each do |project| project.each do |project|
print "- Restoring repository #{project.first}... " print "- Restoring repository #{project.first}... "
FileUtils.rm_rf(project.second) if File.dirname(project.second) # delet old stuff 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") 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 = [ permission_commands = [
"sudo chmod -R g+rwX #{Gitlab.config.git_base_path}", "sudo chmod -R g+rwX #{Gitlab.config.git_base_path}",
...@@ -158,7 +151,8 @@ namespace :gitlab do ...@@ -158,7 +151,8 @@ namespace :gitlab do
task :db_dump => :environment do task :db_dump => :environment do
backup_path_db = File.join(Gitlab.config.backup_path, "db") backup_path_db = File.join(Gitlab.config.backup_path, "db")
FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db) FileUtils.mkdir_p(backup_path_db) unless Dir.exists?(backup_path_db)
puts "Dumping database tables:" puts "Dumping database tables:"
ActiveRecord::Base.connection.tables.each do |tbl| ActiveRecord::Base.connection.tables.each do |tbl|
print "- Dumping table #{tbl}... " print "- Dumping table #{tbl}... "
...@@ -177,8 +171,10 @@ namespace :gitlab do ...@@ -177,8 +171,10 @@ namespace :gitlab do
task :db_restore=> :environment do task :db_restore=> :environment do
backup_path_db = File.join(Gitlab.config.backup_path, "db") backup_path_db = File.join(Gitlab.config.backup_path, "db")
puts "Restoring database tables:" puts "Restoring database tables:"
Rake::Task["db:reset"].invoke Rake::Task["db:reset"].invoke
Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir| Dir.glob(File.join(backup_path_db, "*.yml") ).each do |dir|
fixture_file = File.basename(dir, ".*" ) fixture_file = File.basename(dir, ".*" )
print "- Loading fixture #{fixture_file}..." print "- Loading fixture #{fixture_file}..."
......
...@@ -4,8 +4,7 @@ namespace :gitlab do ...@@ -4,8 +4,7 @@ namespace :gitlab do
task :update_repos => :environment do task :update_repos => :environment do
puts "Starting Projects" puts "Starting Projects"
Project.find_each(:batch_size => 100) do |project| Project.find_each(:batch_size => 100) do |project|
puts puts "\n=== #{project.name}"
puts "=== #{project.name}"
project.update_repository project.update_repository
puts puts
end end
......
...@@ -8,4 +8,3 @@ namespace :gitlab do ...@@ -8,4 +8,3 @@ namespace :gitlab do
] ]
end end
end end
namespace :gitlab do namespace :gitlab do
namespace :app do namespace :app do
desc "GITLAB | Check gitlab installation status" desc "GITLAB | Check GitLab installation status"
task :status => :environment do task :status => :environment do
puts "Starting diagnostic".yellow puts "Starting diagnostics".yellow
git_base_path = Gitlab.config.git_base_path git_base_path = Gitlab.config.git_base_path
print "config/database.yml............" print "config/database.yml............"
if File.exists?(File.join Rails.root, "config", "database.yml") if File.exists?(Rails.root.join "config", "database.yml")
puts "exists".green puts "exists".green
else else
puts "missing".red puts "missing".red
...@@ -14,7 +14,7 @@ namespace :gitlab do ...@@ -14,7 +14,7 @@ namespace :gitlab do
end end
print "config/gitlab.yml............" print "config/gitlab.yml............"
if File.exists?(File.join Rails.root, "config", "gitlab.yml") if File.exists?(Rails.root.join "config", "gitlab.yml")
puts "exists".green puts "exists".green
else else
puts "missing".red puts "missing".red
...@@ -49,7 +49,7 @@ namespace :gitlab do ...@@ -49,7 +49,7 @@ namespace :gitlab do
end end
print "UMASK for .gitolite.rc is 0007? ............" print "UMASK for .gitolite.rc is 0007? ............"
unless open("#{git_base_path}/../.gitolite.rc").grep(/UMASK([ \t]*)=([ \t>]*)0007/).empty? if open("#{git_base_path}/../.gitolite.rc").grep(/UMASK([ \t]*)=([ \t>]*)0007/).any?
puts "YES".green puts "YES".green
else else
puts "NO".red puts "NO".red
...@@ -69,16 +69,15 @@ namespace :gitlab do ...@@ -69,16 +69,15 @@ namespace :gitlab do
end end
end end
if Project.count > 0 if Project.count > 0
puts "Validating projects repositories:".yellow puts "Validating projects repositories:".yellow
Project.find_each(:batch_size => 100) do |project| Project.find_each(:batch_size => 100) do |project|
print "#{project.name}....." print "#{project.name}....."
hook_file = File.join(project.path_to_repo, 'hooks','post-receive') hook_file = File.join(project.path_to_repo, 'hooks', 'post-receive')
unless File.exists?(hook_file) unless File.exists?(hook_file)
puts "post-receive file missing".red puts "post-receive file missing".red
next return
end end
puts "post-receive file ok".green puts "post-receive file ok".green
......
...@@ -4,7 +4,6 @@ namespace :gitlab do ...@@ -4,7 +4,6 @@ namespace :gitlab do
task :write_hooks => :environment do task :write_hooks => :environment do
gitolite_hooks_path = File.join(Gitlab.config.git_hooks_path, "common") gitolite_hooks_path = File.join(Gitlab.config.git_hooks_path, "common")
gitlab_hooks_path = Rails.root.join("lib", "hooks") gitlab_hooks_path = Rails.root.join("lib", "hooks")
gitlab_hook_files = ['post-receive'] gitlab_hook_files = ['post-receive']
gitlab_hook_files.each do |file_name| gitlab_hook_files.each do |file_name|
...@@ -20,4 +19,3 @@ namespace :gitlab do ...@@ -20,4 +19,3 @@ namespace :gitlab do
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment