2012-11-23 04:34:09 +01:00
|
|
|
namespace :gitlab do
|
|
|
|
desc "GITLAB | Enable usernames and namespaces for user projects"
|
2012-12-20 19:31:29 +01:00
|
|
|
task enable_namespaces: :environment do
|
2012-12-23 21:16:08 +01:00
|
|
|
warn_user_is_not_gitlab
|
|
|
|
|
2012-12-28 04:14:05 +01:00
|
|
|
migrate_user_namespaces
|
|
|
|
migrate_groups
|
|
|
|
migrate_projects
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_user_namespaces
|
|
|
|
puts "\nGenerate usernames for users without one: ".blue
|
2012-11-23 04:34:09 +01:00
|
|
|
User.find_each(batch_size: 500) do |user|
|
2012-12-28 04:14:05 +01:00
|
|
|
if user.namespace
|
|
|
|
print '-'.cyan
|
|
|
|
next
|
|
|
|
end
|
2012-11-24 11:25:04 +01:00
|
|
|
|
2012-12-28 04:14:05 +01:00
|
|
|
username = if user.username.present?
|
|
|
|
# if user already has username filled
|
|
|
|
user.username
|
|
|
|
else
|
|
|
|
build_username(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
User.transaction do
|
|
|
|
user.update_attributes!(username: username)
|
2012-11-24 11:25:04 +01:00
|
|
|
print '.'.green
|
|
|
|
end
|
2012-12-28 04:14:05 +01:00
|
|
|
rescue
|
|
|
|
print 'F'.red
|
2012-11-23 04:34:09 +01:00
|
|
|
end
|
|
|
|
end
|
2012-12-28 04:14:05 +01:00
|
|
|
puts "\nDone"
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_username(user)
|
|
|
|
username = nil
|
|
|
|
|
|
|
|
# generate username
|
|
|
|
username = user.email.match(/^[^@]*/)[0]
|
|
|
|
username.gsub!("+", ".")
|
|
|
|
|
|
|
|
# return username if no mathes
|
|
|
|
return username unless User.find_by_username(username)
|
|
|
|
|
|
|
|
# look for same username
|
|
|
|
(1..10).each do |i|
|
|
|
|
suffixed_username = "#{username}#{i}"
|
|
|
|
|
|
|
|
return suffixed_username unless User.find_by_username(suffixed_username)
|
|
|
|
end
|
|
|
|
end
|
2012-11-25 16:58:06 +01:00
|
|
|
|
2012-12-28 04:14:05 +01:00
|
|
|
def migrate_groups
|
|
|
|
puts "\nCreate directories for groups: ".blue
|
2012-11-25 17:24:27 +01:00
|
|
|
|
2012-11-25 16:58:06 +01:00
|
|
|
Group.find_each(batch_size: 500) do |group|
|
2012-12-28 04:14:05 +01:00
|
|
|
begin
|
|
|
|
if group.dir_exists?
|
|
|
|
print '-'.cyan
|
|
|
|
else
|
|
|
|
if group.ensure_dir_exist
|
|
|
|
print '.'.green
|
|
|
|
else
|
|
|
|
print 'F'.red
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
2012-11-25 16:58:06 +01:00
|
|
|
print 'F'.red
|
|
|
|
end
|
|
|
|
end
|
2012-12-28 04:14:05 +01:00
|
|
|
puts "\nDone"
|
|
|
|
end
|
2012-11-25 16:58:06 +01:00
|
|
|
|
2012-12-28 04:14:05 +01:00
|
|
|
def migrate_projects
|
2013-02-11 18:16:59 +01:00
|
|
|
git_path = Gitlab.config.gitlab_shell.repos_path
|
2012-12-28 04:14:05 +01:00
|
|
|
puts "\nMove projects in groups into respective directories ... ".blue
|
2012-11-25 16:58:06 +01:00
|
|
|
Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project|
|
|
|
|
next unless project.group
|
|
|
|
|
|
|
|
group = project.group
|
|
|
|
|
2012-12-23 21:16:08 +01:00
|
|
|
print "#{project.name_with_namespace.yellow} ... "
|
2012-11-25 17:24:27 +01:00
|
|
|
|
|
|
|
new_path = File.join(git_path, project.path_with_namespace + '.git')
|
|
|
|
|
|
|
|
if File.exists?(new_path)
|
2012-12-23 21:16:08 +01:00
|
|
|
puts "already at #{new_path}".green
|
2012-11-25 17:24:27 +01:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
old_path = File.join(git_path, project.path + '.git')
|
2012-11-25 16:58:06 +01:00
|
|
|
|
2012-11-25 17:24:27 +01:00
|
|
|
unless File.exists?(old_path)
|
2012-12-23 21:16:08 +01:00
|
|
|
puts "couldn't find it at #{old_path}".red
|
2012-11-25 17:24:27 +01:00
|
|
|
next
|
|
|
|
end
|
2012-11-25 16:58:06 +01:00
|
|
|
|
|
|
|
begin
|
|
|
|
Gitlab::ProjectMover.new(project, '', group.path).execute
|
2012-12-23 21:16:08 +01:00
|
|
|
puts "moved to #{new_path}".green
|
2012-11-25 16:58:06 +01:00
|
|
|
rescue
|
2012-12-23 21:16:08 +01:00
|
|
|
puts "failed moving to #{new_path}".red
|
2012-11-25 16:58:06 +01:00
|
|
|
end
|
|
|
|
end
|
2012-11-25 17:24:27 +01:00
|
|
|
|
2012-12-28 04:14:05 +01:00
|
|
|
puts "\nDone"
|
2012-11-23 04:34:09 +01:00
|
|
|
end
|
|
|
|
end
|