shell.rake 4.44 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
      target_dir = Gitlab.config.gitlab_shell.path
16 17 18

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

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

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

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

46 47 48 49 50 51 52 53 54
        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

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

        # Launch installation process
59
        system(*%W(bin/install) + repository_storage_paths_args)
60 61

        # (Re)create hooks
62
        system(*%W(bin/create-hooks) + repository_storage_paths_args)
63 64 65 66 67 68 69 70 71 72 73
      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
74 75

      Gitlab::Shell.new.generate_and_link_secret_token
76 77
    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
        if File.exists?(path_to_repo)
          print '-'
        else
90 91
          if Gitlab::Shell.new.add_repository(project.repository_storage_path,
                                              project.path_with_namespace)
92 93 94 95 96 97 98
            print '.'
          else
            print 'F'
          end
        end
      end
    end
99 100 101 102 103
  end

  def setup
    warn_user_is_not_gitlab

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

111
    Gitlab::Shell.new.remove_all_keys
112

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

121
    unless $?.success?
122
      puts "Failed to add keys...".color(:red)
123 124 125
      exit 1
    end

126
  rescue Gitlab::TaskAbortedByUserError
127
    puts "Quitting...".color(:red)
128 129
    exit 1
  end
130 131

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

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

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