Commit d03964d6ec4351be76ec978fc0481f24c3b623ec
1 parent
d991ce63
Exists in
master
and in
4 other branches
Fixed and improved enable_naamespace migration task
Showing
3 changed files
with
71 additions
and
22 deletions
Show diff stats
app/models/namespace.rb
@@ -51,8 +51,12 @@ class Namespace < ActiveRecord::Base | @@ -51,8 +51,12 @@ class Namespace < ActiveRecord::Base | ||
51 | end | 51 | end |
52 | 52 | ||
53 | def ensure_dir_exist | 53 | def ensure_dir_exist |
54 | + dir_exists? || system("mkdir -m 770 #{namespace_dir_path}") | ||
55 | + end | ||
56 | + | ||
57 | + def dir_exists? | ||
54 | namespace_dir_path = File.join(Gitlab.config.gitolite.repos_path, path) | 58 | namespace_dir_path = File.join(Gitlab.config.gitolite.repos_path, path) |
55 | - system("mkdir -m 770 #{namespace_dir_path}") unless File.exists?(namespace_dir_path) | 59 | + File.exists?(namespace_dir_path) |
56 | end | 60 | end |
57 | 61 | ||
58 | def move_dir | 62 | def move_dir |
app/observers/user_observer.rb
@@ -14,7 +14,7 @@ class UserObserver < ActiveRecord::Observer | @@ -14,7 +14,7 @@ class UserObserver < ActiveRecord::Observer | ||
14 | if user.namespace | 14 | if user.namespace |
15 | user.namespace.update_attributes(path: user.username) | 15 | user.namespace.update_attributes(path: user.username) |
16 | else | 16 | else |
17 | - user.create_namespace!(path: user.username, name: user.name) | 17 | + user.create_namespace!(path: user.username, name: user.username) |
18 | end | 18 | end |
19 | end | 19 | end |
20 | end | 20 | end |
lib/tasks/gitlab/enable_namespaces.rake
@@ -3,36 +3,85 @@ namespace :gitlab do | @@ -3,36 +3,85 @@ namespace :gitlab do | ||
3 | task enable_namespaces: :environment do | 3 | task enable_namespaces: :environment do |
4 | warn_user_is_not_gitlab | 4 | warn_user_is_not_gitlab |
5 | 5 | ||
6 | - print "Generate usernames for users without one: " | 6 | + migrate_user_namespaces |
7 | + migrate_groups | ||
8 | + migrate_projects | ||
7 | 9 | ||
10 | + puts "Rebuild Gitolite ... " | ||
11 | + gitolite = Gitlab::Gitolite.new | ||
12 | + gitolite.update_repositories(Project.where('namespace_id IS NOT NULL')) | ||
13 | + puts "... #{"done".green}" | ||
14 | + end | ||
15 | + | ||
16 | + def migrate_user_namespaces | ||
17 | + puts "\nGenerate usernames for users without one: ".blue | ||
8 | User.find_each(batch_size: 500) do |user| | 18 | User.find_each(batch_size: 500) do |user| |
9 | - next if user.namespace | 19 | + if user.namespace |
20 | + print '-'.cyan | ||
21 | + next | ||
22 | + end | ||
10 | 23 | ||
11 | - User.transaction do | ||
12 | - username = user.email.match(/^[^@]*/)[0] | ||
13 | - if user.update_attributes!(username: username) | 24 | + username = if user.username.present? |
25 | + # if user already has username filled | ||
26 | + user.username | ||
27 | + else | ||
28 | + build_username(user) | ||
29 | + end | ||
30 | + | ||
31 | + begin | ||
32 | + User.transaction do | ||
33 | + user.update_attributes!(username: username) | ||
14 | print '.'.green | 34 | print '.'.green |
15 | - else | ||
16 | - print 'F'.red | ||
17 | end | 35 | end |
36 | + rescue | ||
37 | + print 'F'.red | ||
18 | end | 38 | end |
19 | end | 39 | end |
40 | + puts "\nDone" | ||
41 | + end | ||
42 | + | ||
43 | + def build_username(user) | ||
44 | + username = nil | ||
45 | + | ||
46 | + # generate username | ||
47 | + username = user.email.match(/^[^@]*/)[0] | ||
48 | + username.gsub!("+", ".") | ||
49 | + | ||
50 | + # return username if no mathes | ||
51 | + return username unless User.find_by_username(username) | ||
52 | + | ||
53 | + # look for same username | ||
54 | + (1..10).each do |i| | ||
55 | + suffixed_username = "#{username}#{i}" | ||
56 | + | ||
57 | + return suffixed_username unless User.find_by_username(suffixed_username) | ||
58 | + end | ||
59 | + end | ||
20 | 60 | ||
21 | - puts "" | ||
22 | - print "Create directories for groups: " | 61 | + def migrate_groups |
62 | + puts "\nCreate directories for groups: ".blue | ||
23 | 63 | ||
24 | Group.find_each(batch_size: 500) do |group| | 64 | Group.find_each(batch_size: 500) do |group| |
25 | - if group.ensure_dir_exist | ||
26 | - print '.'.green | ||
27 | - else | 65 | + begin |
66 | + if group.dir_exists? | ||
67 | + print '-'.cyan | ||
68 | + else | ||
69 | + if group.ensure_dir_exist | ||
70 | + print '.'.green | ||
71 | + else | ||
72 | + print 'F'.red | ||
73 | + end | ||
74 | + end | ||
75 | + rescue | ||
28 | print 'F'.red | 76 | print 'F'.red |
29 | end | 77 | end |
30 | end | 78 | end |
31 | - puts "" | 79 | + puts "\nDone" |
80 | + end | ||
32 | 81 | ||
82 | + def migrate_projects | ||
33 | git_path = Gitlab.config.gitolite.repos_path | 83 | git_path = Gitlab.config.gitolite.repos_path |
34 | - puts "" | ||
35 | - puts "Move projects in groups into respective directories ... " | 84 | + puts "\nMove projects in groups into respective directories ... ".blue |
36 | Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project| | 85 | Project.where('namespace_id IS NOT NULL').find_each(batch_size: 500) do |project| |
37 | next unless project.group | 86 | next unless project.group |
38 | 87 | ||
@@ -62,10 +111,6 @@ namespace :gitlab do | @@ -62,10 +111,6 @@ namespace :gitlab do | ||
62 | end | 111 | end |
63 | end | 112 | end |
64 | 113 | ||
65 | - puts "" | ||
66 | - puts "Rebuild Gitolite ... " | ||
67 | - gitolite = Gitlab::Gitolite.new | ||
68 | - gitolite.update_repositories(Project.where('namespace_id IS NOT NULL')) | ||
69 | - puts "... #{"done".green}" | 114 | + puts "\nDone" |
70 | end | 115 | end |
71 | end | 116 | end |