Skip to content
Snippets Groups Projects
Commit 708a0d42 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Fixed and improved enable_naamespace migration task

parent 779e95b5
No related branches found
No related tags found
4 merge requests!104094 0 stable,!2553Wiki grammar patch,!2439Documentation: don't setuid the repositories on installation,!2422updated instructions fixed up references of mysql and postgresql
This commit is part of merge request !10409. Comments created here will be created in the context of that merge request.
Loading
Loading
@@ -51,8 +51,17 @@ class Namespace < ActiveRecord::Base
end
 
def ensure_dir_exist
namespace_dir_path = File.join(Gitlab.config.gitolite.repos_path, path)
system("mkdir -m 770 #{namespace_dir_path}") unless File.exists?(namespace_dir_path)
unless dir_exists?
system("mkdir -m 770 #{namespace_full_path}")
end
end
def dir_exists?
File.exists?(namespace_full_path)
end
def namespace_full_path
@namespace_full_path ||= File.join(Gitlab.config.gitolite.repos_path, path)
end
 
def move_dir
Loading
Loading
Loading
Loading
@@ -14,7 +14,7 @@ class UserObserver < ActiveRecord::Observer
if user.namespace
user.namespace.update_attributes(path: user.username)
else
user.create_namespace!(path: user.username, name: user.name)
user.create_namespace!(path: user.username, name: user.username)
end
end
end
Loading
Loading
namespace :gitlab do
desc "GITLAB | Enable usernames and namespaces for user projects"
task enable_namespaces: :environment do
print "\nUsernames for users:".yellow
warn_user_is_not_gitlab
 
migrate_user_namespaces
migrate_groups
migrate_projects
puts "Rebuild Gitolite ... "
gitolite = Gitlab::Gitolite.new
gitolite.update_repositories(Project.where('namespace_id IS NOT NULL'))
puts "... #{"done".green}"
end
def migrate_user_namespaces
puts "\nGenerate usernames for users without one: ".blue
User.find_each(batch_size: 500) do |user|
next if user.namespace
if user.namespace
print '-'.cyan
next
end
 
User.transaction do
username = user.email.match(/^[^@]*/)[0]
if user.update_attributes!(username: username)
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)
print '.'.green
else
print 'F'.red
end
rescue
print 'F'.red
end
end
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
 
print "\n\nDirs for groups:".yellow
def migrate_groups
puts "\nCreate directories for groups: ".blue
 
Group.find_each(batch_size: 500) do |group|
if group.ensure_dir_exist
print '.'.green
else
begin
if group.dir_exists?
print '-'.cyan
else
if group.ensure_dir_exist
print '.'.green
else
print 'F'.red
end
end
rescue
print 'F'.red
end
end
puts "\nDone"
end
 
print "\n\nMove projects from groups under groups dirs:".yellow
def migrate_projects
git_path = Gitlab.config.gitolite.repos_path
puts "\nMove projects in groups into respective directories ... ".blue
Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project|
next unless project.group
 
Loading
Loading
@@ -59,9 +112,6 @@ namespace :gitlab do
end
end
 
print "\n\nRebuild gitolite:".yellow
gitolite = Gitlab::Gitolite.new
gitolite.update_repositories(Project.where('namespace_id IS NOT NULL'))
puts "\n"
puts "\nDone"
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment