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|
users = User.find_all_by_admin(false, :select => 'id').map(&:id)
admins = User.find_all_by_admin(true, :select => 'id').map(&:id)
user_ids = User.where(:admin => false).pluck(:id)
admin_ids = User.where(:admin => true).pluck(:id)
users.each do |user|
puts "#{user}"
end
Project.all.each do |project|
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)
Project.find_each do |project|
puts "Importing #{user_ids.size} users into #{project.code}"
UsersProject.bulk_import(project, user_ids, UsersProject::DEVELOPER)
puts "Importing #{admin_ids.size} admins into #{project.code}"
UsersProject.bulk_import(project, admin_ids, UsersProject::MASTER)
end
end
desc "Add user to as a developer to all projects"
task :add_user_to_project_teams, [:email] => :environment do |t, args|
user_email = args.email
user = User.find_by_email(user_email)
project_ids = Project.all.map(&:id)
user = User.find_by_email args.email
project_ids = Project.pluck(:id)
UsersProject.user_bulk_import(user,project_ids,UsersProject::DEVELOPER)
UsersProject.user_bulk_import(user, project_ids, UsersProject::DEVELOPER)
end
desc "Imports existing Git repos from a directory into new projects in git_base_path"
task :import_projects, [:directory,:email] => :environment do |t, args|
user_email = args.email
import_directory = args.directory
user_email, import_directory = args.email, args.directory
repos_to_import = Dir.glob("#{import_directory}/*")
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|
repo_name = File.basename repo_path
clone_path = "#{git_base_path}#{repo_name}.git"
puts " Processing #{repo_name}"
clone_path = "#{git_base_path}#{repo_name}.git"
if Dir.exists? clone_path
if Project.find_by_code(repo_name)
......@@ -38,7 +35,6 @@ task :import_projects, [:directory,:email] => :environment do |t, args|
else
failed_count += 1
end
end
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)
git_user = Gitlab.config.ssh_user
begin
sh "sudo -u #{git_user} -i git clone --bare '#{existing_path}' #{new_path}"
true
rescue Exception=> msg
puts " ERROR: Faild to clone #{existing_path} to #{new_path}"
puts " Make sure #{git_user} can reach #{existing_path}"
puts " Exception-MSG: #{msg}"
false
rescue Exception => msg
puts " ERROR: Failed to clone #{existing_path} to #{new_path}"
puts " Make sure #{git_user} can reach #{existing_path}"
puts " Exception-MSG: #{msg}"
end
end
# Creats a project in Gitlag given a @project_name@ to use (for name, web url, and code
# url) and a @user_email@ that will be assigned as the owner of the project.
# Creates a project in GitLab given a `project_name` to use
# (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)
user = User.find_by_email(user_email)
if user
if user = User.find_by_email(user_email)
# Using find_by_code since that's the most important identifer to be unique
if Project.find_by_code(project_name)
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
project = Project.create(
name: project_name,
code: project_name,
path: project_name,
owner: user,
description: "Automatically created from Rake on #{Time.now.to_s}"
)
end
unless project.valid?
puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
return false
end
# Add user as admin for project
project.users_projects.create!(
:project_access => UsersProject::MASTER,
:user => user
project = Project.create(
name: project_name,
code: project_name,
path: project_name,
owner: user,
description: "Automatically created from 'import_projects' rake task on #{Time.now}"
)
# Per projects_controller.rb#37
project.update_repository
if project.valid?
true
# Add user as admin for project
project.users_projects.create!(:project_access => UsersProject::MASTER, :user => user)
project.update_repository
else
puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
false
end
end
else
puts " ERROR: #{user_email} not found, skipping"
false
puts " ERROR: user with #{user_email} not found, skipping"
end
end
......@@ -2,22 +2,20 @@ require 'active_record/fixtures'
namespace :gitlab do
namespace :app do
# Create backup of gitlab system
desc "GITLAB | Create a backup of the gitlab system"
# Create backup of GitLab system
desc "GITLAB | Create a backup of the GitLab system"
task :backup_create => :environment do
Rake::Task["gitlab:app:db_dump"].invoke
Rake::Task["gitlab:app:repo_dump"].invoke
Dir.chdir(Gitlab.config.backup_path)
# saving additional informations
s = Hash.new
s["db_version"] = "#{ActiveRecord::Migrator.current_version}"
s["backup_created_at"] = "#{Time.now}"
s["gitlab_version"] = %x{git rev-parse HEAD}.gsub(/\n/,"")
s["tar_version"] = %x{tar --version | head -1}.gsub(/\n/,"")
s = {}
s[:db_version] = "#{ActiveRecord::Migrator.current_version}"
s[:backup_created_at] = "#{Time.now}"
s[:gitlab_version] = %x{git rev-parse HEAD}.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 << s.to_yaml.gsub(/^---\n/,'')
......@@ -32,7 +30,7 @@ namespace :gitlab do
end
# cleanup: remove tmp files
print "Deletion of tmp directories..."
print "Deleting tmp directories..."
if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
puts "[DONE]".green
else
......@@ -52,26 +50,23 @@ namespace :gitlab do
else
puts "[SKIPPING]".yellow
end
end
# Restore backup of gitlab system
# Restore backup of GitLab system
desc "GITLAB | Restore a previously created backup"
task :backup_restore => :environment do
Dir.chdir(Gitlab.config.backup_path)
# check for existing backups in the backup dir
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?
puts "Found more than one backup, please specify which one you want to restore:"
puts "rake gitlab:app:backup_restore BACKUP=timestamp_of_backup"
exit 1;
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)
puts "The specified backup doesn't exist!"
......@@ -102,16 +97,14 @@ namespace :gitlab do
Rake::Task["gitlab:app:repo_restore"].invoke
# cleanup: remove tmp files
print "Deletion of tmp directories..."
print "Deleting tmp directories..."
if Kernel.system("rm -rf repositories/ db/ backup_information.yml")
puts "[DONE]".green
else
puts "[FAILED]".red
end
end
################################################################################
################################# invoked tasks ################################
......@@ -121,7 +114,7 @@ namespace :gitlab 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 = 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 "- Dumping repository #{project.first}... "
......@@ -136,11 +129,11 @@ namespace :gitlab do
task :repo_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 = 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) # 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")
permission_commands = [
"sudo chmod -R g+rwX #{Gitlab.config.git_base_path}",
......@@ -157,8 +150,9 @@ namespace :gitlab do
###################################### DB ######################################
task :db_dump => :environment do
backup_path_db = File.join(Gitlab.config.backup_path, "db")
FileUtils.mkdir_p(backup_path_db) until Dir.exists?(backup_path_db)
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}... "
......@@ -176,9 +170,11 @@ namespace :gitlab do
end
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:"
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}..."
......
namespace :gitlab do
namespace :app do
desc "GITLAB | Enable auto merge"
task :enable_automerge => :environment do
task :enable_automerge => :environment do
Gitlab::Gitolite.new.enable_automerge
Project.find_each do |project|
......
namespace :gitlab do
namespace :gitolite do
desc "GITLAB | Rebuild each project at gitolite config"
task :update_repos => :environment do
task :update_repos => :environment do
puts "Starting Projects"
Project.find_each(:batch_size => 100) do |project|
puts
puts "=== #{project.name}"
puts "\n=== #{project.name}"
project.update_repository
puts
end
......
......@@ -4,8 +4,7 @@ namespace :gitlab do
task :setup => [
'db:setup',
'db:seed_fu',
'gitlab:app:enable_automerge'
'gitlab:app:enable_automerge'
]
end
end
namespace :gitlab do
namespace :app do
desc "GITLAB | Check gitlab installation status"
desc "GITLAB | Check GitLab installation status"
task :status => :environment do
puts "Starting diagnostic".yellow
puts "Starting diagnostics".yellow
git_base_path = Gitlab.config.git_base_path
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
else
else
puts "missing".red
return
end
print "config/gitlab.yml............"
if File.exists?(File.join Rails.root, "config", "gitlab.yml")
puts "exists".green
if File.exists?(Rails.root.join "config", "gitlab.yml")
puts "exists".green
else
puts "missing".red
return
end
print "#{git_base_path}............"
if File.exists?(git_base_path)
puts "exists".green
else
if File.exists?(git_base_path)
puts "exists".green
else
puts "missing".red
return
end
print "#{git_base_path} is writable?............"
if File.stat(git_base_path).writable?
puts "YES".green
puts "YES".green
else
puts "NO".red
return
......@@ -41,16 +41,16 @@ namespace :gitlab do
`git clone #{Gitlab.config.gitolite_admin_uri} /tmp/gitolite_gitlab_test`
FileUtils.rm_rf("/tmp/gitolite_gitlab_test")
print "Can clone gitolite-admin?............"
puts "YES".green
rescue
puts "YES".green
rescue
print "Can clone gitolite-admin?............"
puts "NO".red
return
end
print "UMASK for .gitolite.rc is 0007? ............"
unless open("#{git_base_path}/../.gitolite.rc").grep(/UMASK([ \t]*)=([ \t>]*)0007/).empty?
puts "YES".green
if open("#{git_base_path}/../.gitolite.rc").grep(/UMASK([ \t]*)=([ \t>]*)0007/).any?
puts "YES".green
else
puts "NO".red
return
......@@ -69,16 +69,15 @@ namespace :gitlab do
end
end
if Project.count > 0
if Project.count > 0
puts "Validating projects repositories:".yellow
Project.find_each(:batch_size => 100) do |project|
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)
puts "post-receive file missing".red
next
puts "post-receive file missing".red
return
end
puts "post-receive file ok".green
......
......@@ -4,7 +4,6 @@ namespace :gitlab do
task :write_hooks => :environment do
gitolite_hooks_path = File.join(Gitlab.config.git_hooks_path, "common")
gitlab_hooks_path = Rails.root.join("lib", "hooks")
gitlab_hook_files = ['post-receive']
gitlab_hook_files.each do |file_name|
......@@ -20,4 +19,3 @@ namespace :gitlab do
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