Commit 37e39e4176757019fb6e9d437b86229497e63b57

Authored by Dmitriy Zaporozhets
1 parent 36dbac2f

Added bare import script. Removed old unworking script

lib/tasks/bulk_import.rake
... ... @@ -1,83 +0,0 @@
1   -desc "Imports existing Git repos from a directory into new projects in git_base_path"
2   -task :import_projects, [:directory,:email] => :environment do |t, args|
3   - user_email, import_directory = args.email, args.directory
4   - repos_to_import = Dir.glob("#{import_directory}/*")
5   - git_base_path = Gitlab.config.git_base_path
6   - imported_count, skipped_count, failed_count = 0
7   -
8   - puts "Found #{repos_to_import.size} repos to import"
9   -
10   - repos_to_import.each do |repo_path|
11   - repo_name = File.basename repo_path
12   - clone_path = "#{git_base_path}#{repo_name}.git"
13   -
14   - puts " Processing #{repo_name}"
15   -
16   - if Dir.exists? clone_path
17   - if Project.find_by_code(repo_name)
18   - puts " INFO: #{clone_path} already exists in repositories directory, skipping."
19   - skipped_count += 1
20   - next
21   - else
22   - puts " INFO: Project doesn't exist for #{repo_name} (but the repo does)."
23   - end
24   - else
25   - # Clone the repo
26   - unless clone_bare_repo_as_git(repo_path, clone_path)
27   - failed_count += 1
28   - next
29   - end
30   - end
31   -
32   - # Create the project and repo
33   - if create_repo_project(repo_name, user_email)
34   - imported_count += 1
35   - else
36   - failed_count += 1
37   - end
38   - end
39   -
40   - puts "Finished importing #{imported_count} projects (skipped #{skipped_count}, failed #{failed_count})."
41   -end
42   -
43   -# Clones a repo as bare git repo using the git_user
44   -def clone_bare_repo_as_git(existing_path, new_path)
45   - git_user = Gitlab.config.ssh_user
46   - begin
47   - sh "sudo -u #{git_user} -i git clone --bare '#{existing_path}' #{new_path}"
48   - rescue Exception => msg
49   - puts " ERROR: Failed to clone #{existing_path} to #{new_path}"
50   - puts " Make sure #{git_user} can reach #{existing_path}"
51   - puts " Exception-MSG: #{msg}"
52   - end
53   -end
54   -
55   -# Creates a project in GitLab given a `project_name` to use
56   -# (for name, web url, and code url) and a `user_email` that will be
57   -# assigned as the owner of the project.
58   -def create_repo_project(project_name, user_email)
59   - if user = User.find_by_email(user_email)
60   - # Using find_by_code since that's the most important identifer to be unique
61   - if Project.find_by_code(project_name)
62   - puts " INFO: Project #{project_name} already exists in Gitlab, skipping."
63   - else
64   - project = Project.create(
65   - name: project_name,
66   - code: project_name,
67   - path: project_name,
68   - owner: user,
69   - description: "Automatically created from 'import_projects' rake task on #{Time.now}"
70   - )
71   -
72   - if project.valid?
73   - # Add user as admin for project
74   - project.users_projects.create!(:project_access => UsersProject::MASTER, :user => user)
75   - project.update_repository
76   - else
77   - puts " ERROR: Failed to create project #{project} because #{project.errors.first}"
78   - end
79   - end
80   - else
81   - puts " ERROR: user with #{user_email} not found, skipping"
82   - end
83   -end
lib/tasks/gitlab/import.rake 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +namespace :gitlab do
  2 + namespace :import do
  3 + # How to use:
  4 + #
  5 + # 1. copy your bare repos under git base_path
  6 + # 2. run bundle exec rake gitlab:import:repos RAILS_ENV=production
  7 + #
  8 + # Notes:
  9 + # * project owner will be a first admin
  10 + # * existing projects will be skipped
  11 + #
  12 + desc "GITLAB | Import bare repositories from git_host -> base_path into GitLab project instance"
  13 + task :repos => :environment do
  14 +
  15 + git_base_path = Gitlab.config.git_base_path
  16 + repos_to_import = Dir.glob(git_base_path + '/*')
  17 +
  18 + repos_to_import.each do |repo_path|
  19 + repo_name = File.basename repo_path
  20 +
  21 + # skip gitolite admin
  22 + next if repo_name == 'gitolite-admin.git'
  23 +
  24 + path = repo_name.sub(/\.git$/, '')
  25 +
  26 + project = Project.find_by_path(path)
  27 +
  28 + puts "Processing #{repo_name}".yellow
  29 +
  30 + if project
  31 + puts " * #{project.name} (#{repo_name}) exists"
  32 + else
  33 + user = User.admins.first
  34 +
  35 + project_params = {
  36 + :name => path,
  37 + :code => path,
  38 + :path => path,
  39 + }
  40 +
  41 + project = Project.create_by_user(project_params, user)
  42 +
  43 + if project.valid?
  44 + puts " * Created #{project.name} (#{repo_name})".green
  45 + else
  46 + puts " * Failed trying to create #{project.name} (#{repo_name})".red
  47 + end
  48 + end
  49 + end
  50 +
  51 + puts "Done!".green
  52 + end
  53 + end
  54 +end
... ...