BigW Consortium Gitlab

cleanup.rake 2.67 KB
Newer Older
1 2
namespace :gitlab do
  namespace :cleanup do
3
    desc "GitLab | Cleanup | Clean namespaces"
4
    task dirs: :environment  do
5 6 7 8 9
      warn_user_is_not_gitlab
      remove_flag = ENV['REMOVE']


      namespaces = Namespace.pluck(:path)
10
      git_base_path = Gitlab.config.gitlab_shell.repos_path
11 12
      all_dirs = Dir.glob(git_base_path + '/*')

13
      puts git_base_path.color(:yellow)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
      puts "Looking for directories to remove... "

      all_dirs.reject! do |dir|
        # skip if git repo
        dir =~ /.git$/
      end

      all_dirs.reject! do |dir|
        dir_name = File.basename dir

        # skip if namespace present
        namespaces.include?(dir_name)
      end

      all_dirs.each do |dir_path|

        if remove_flag
          if FileUtils.rm_rf dir_path
32
            puts "Removed...#{dir_path}".color(:red)
33
          else
34
            puts "Cannot remove #{dir_path}".color(:red)
35 36
          end
        else
37
          puts "Can be removed: #{dir_path}".color(:red)
38 39 40 41
        end
      end

      unless remove_flag
42
        puts "To cleanup this directories run this command with REMOVE=true".color(:yellow)
43 44 45
      end
    end

46
    desc "GitLab | Cleanup | Clean repositories"
47
    task repos: :environment  do
48 49
      warn_user_is_not_gitlab

50 51 52 53 54 55
      move_suffix = "+orphaned+#{Time.now.to_i}"
      repo_root = Gitlab.config.gitlab_shell.repos_path
      # Look for global repos (legacy, depth 1) and normal repos (depth 2)
      IO.popen(%W(find #{repo_root} -mindepth 1 -maxdepth 2 -name *.git)) do |find|
        find.each_line do |path|
          path.chomp!
56 57 58 59 60
          repo_with_namespace = path.
            sub(repo_root, '').
            sub(%r{^/*}, '').
            chomp('.git').
            chomp('.wiki')
61 62 63 64
          next if Project.find_with_namespace(repo_with_namespace)
          new_path = path + move_suffix
          puts path.inspect + ' -> ' + new_path.inspect
          File.rename(path, new_path)
65 66 67
        end
      end
    end
68

69
    desc "GitLab | Cleanup | Block users that have been removed in LDAP"
70 71 72 73
    task block_removed_ldap_users: :environment  do
      warn_user_is_not_gitlab
      block_flag = ENV['BLOCK']

74 75 76 77
      User.find_each do |user|
        next unless user.ldap_user?
        print "#{user.name} (#{user.ldap_identity.extern_uid}) ..."
        if Gitlab::LDAP::Access.allowed?(user)
78
          puts " [OK]".color(:green)
79 80
        else
          if block_flag
81
            user.block! unless user.blocked?
82
            puts " [BLOCKED]".color(:red)
83
          else
84
            puts " [NOT IN LDAP]".color(:yellow)
85 86 87 88 89
          end
        end
      end

      unless block_flag
90
        puts "To block these users run this command with BLOCK=true".color(:yellow)
91 92
      end
    end
93 94
  end
end