BigW Consortium Gitlab

shell.rake 4.32 KB
Newer Older
1 2
namespace :gitlab do
  namespace :shell do
3
    desc "GitLab | Install or upgrade gitlab-shell"
4 5 6
    task :install, [:tag, :repo] => :environment do |t, args|
      warn_user_is_not_gitlab

7
      default_version = Gitlab::Shell.version_required
8
      args.with_defaults(tag: 'v' + default_version, repo: "https://gitlab.com/gitlab-org/gitlab-shell.git")
9

10 11 12
      user = Gitlab.config.gitlab.user
      home_dir = Rails.env.test? ? Rails.root.join('tmp/tests') : Gitlab.config.gitlab.user_home
      gitlab_url = Gitlab.config.gitlab.url
13
      # gitlab-shell requires a / at the end of the url
14
      gitlab_url += '/' unless gitlab_url.end_with?('/')
15 16
      repos_path = Gitlab.config.gitlab_shell.repos_path
      target_dir = Gitlab.config.gitlab_shell.path
17 18 19

      # Clone if needed
      unless File.directory?(target_dir)
20
        system(*%W(#{Gitlab.config.git.bin_path} clone -- #{args.repo} #{target_dir}))
21 22 23 24
      end

      # Make sure we're on the right tag
      Dir.chdir(target_dir) do
25 26
        # First try to checkout without fetching
        # to avoid stalling tests if the Internet is down.
27
        reseted = reset_to_commit(args)
28

29
        unless reseted
30
          system(*%W(#{Gitlab.config.git.bin_path} fetch origin))
31 32
          reset_to_commit(args)
        end
33 34 35 36

        config = {
          user: user,
          gitlab_url: gitlab_url,
37
          http_settings: {self_signed_cert: false}.stringify_keys,
38
          repos_path: repos_path,
39 40 41 42
          auth_file: File.join(home_dir, ".ssh", "authorized_keys"),
          redis: {
            bin: %x{which redis-cli}.chomp,
            namespace: "resque:gitlab"
43
          }.stringify_keys,
44
          log_level: "INFO",
45
          audit_usernames: false
46
        }.stringify_keys
47

48 49 50 51 52 53 54 55 56
        redis_url = URI.parse(ENV['REDIS_URL'] || "redis://localhost:6379")

        if redis_url.scheme == 'unix'
          config['redis']['socket'] = redis_url.path
        else
          config['redis']['host'] = redis_url.host
          config['redis']['port'] = redis_url.port
        end

57 58 59 60
        # Generate config.yml based on existing gitlab settings
        File.open("config.yml", "w+") {|f| f.puts config.to_yaml}

        # Launch installation process
Marin Jankovski committed
61
        system(*%W(bin/install))
62 63 64

        # (Re)create hooks
        system(*%W(bin/create-hooks))
65 66 67 68 69 70 71 72 73 74 75 76 77
      end

      # Required for debian packaging with PKGR: Setup .ssh/environment with
      # the current PATH, so that the correct ruby version gets loaded
      # Requires to set "PermitUserEnvironment yes" in sshd config (should not
      # be an issue since it is more than likely that there are no "normal"
      # user accounts on a gitlab server). The alternative is for the admin to
      # install a ruby (1.9.3+) in the global path.
      File.open(File.join(home_dir, ".ssh", "environment"), "w+") do |f|
        f.puts "PATH=#{ENV['PATH']}"
      end
    end

78
    desc "GitLab | Setup gitlab-shell"
79
    task setup: :environment do
80 81
      setup
    end
82

83
    desc "GitLab | Build missing projects"
84 85
    task build_missing_projects: :environment do
      Project.find_each(batch_size: 1000) do |project|
86
        path_to_repo = project.repository.path_to_repo
87 88 89 90 91 92 93 94 95 96 97
        if File.exists?(path_to_repo)
          print '-'
        else
          if Gitlab::Shell.new.add_repository(project.path_with_namespace)
            print '.'
          else
            print 'F'
          end
        end
      end
    end
98 99 100 101 102
  end

  def setup
    warn_user_is_not_gitlab

103 104
    unless ENV['force'] == 'yes'
      puts "This will rebuild an authorized_keys file."
105
      puts "You will lose any data stored in authorized_keys file."
106 107 108
      ask_to_continue
      puts ""
    end
109

110
    Gitlab::Shell.new.remove_all_keys
111

112 113 114
    Gitlab::Shell.new.batch_add_keys do |adder|
      Key.find_each(batch_size: 1000) do |key|
        adder.add_key(key.shell_id, key.key)
115 116 117
        print '.'
      end
    end
118
    puts ""
119

120 121 122 123 124
    unless $?.success?
      puts "Failed to add keys...".red
      exit 1
    end

125 126 127 128
  rescue Gitlab::TaskAbortedByUserError
    puts "Quitting...".red
    exit 1
  end
129 130

  def reset_to_commit(args)
131
    tag, status = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} describe -- #{args.tag}))
132

133
    unless status.zero?
134
      tag, status = Gitlab::Popen.popen(%W(#{Gitlab.config.git.bin_path} describe -- origin/#{args.tag}))
135 136 137
    end

    tag = tag.strip
138
    system(*%W(#{Gitlab.config.git.bin_path} reset --hard #{tag}))
139
  end
140 141
end