如何激活GitLab EE

技术渣 2022年09月18日

动机

由于需要使用镜像仓库拉取等高级功能,需要使用到GitLab EE版本。

  • 当前Gitlab版本:15.3.3-ee

步骤

  1. 安装ruby 2.3以上(2.3部分子版本报错,可以考虑直接上最新的版本),我使用的是2.7。

  2. 添加gitlab-license库。

    gem install gitlab-license
    
  3. 创建rb文件license.rb.

    require "openssl"
    require "gitlab/license"
     
    key_pair = OpenSSL::PKey::RSA.generate(2048)
    File.open("license_key", "w") { |f| f.write(key_pair.to_pem) }
     
    public_key = key_pair.public_key
    File.open("license_key.pub", "w") { |f| f.write(public_key.to_pem) }
     
    private_key = OpenSSL::PKey::RSA.new File.read("license_key")
    Gitlab::License.encryption_key = private_key
     
    license = Gitlab::License.new
    license.licensee = {
      "Name" => "none",
      "Company" => "none",
      "Email" => "[email protected]",
    }
    license.starts_at = Date.new(2020, 1, 1) # 开始时间
    license.expires_at = Date.new(2050, 1, 1) # 结束时间
    license.notify_admins_at = Date.new(2049, 12, 1)
    license.notify_users_at = Date.new(2049, 12, 1)
    license.block_changes_at = Date.new(2050, 1, 1)
    license.restrictions = {
      active_user_count: 10000,
    }
     
    puts "License:"
    puts license
     
    data = license.export
    puts "Exported license:"
    puts data
    File.open("GitLabBV.gitlab-license", "w") { |f| f.write(data) }
     
    public_key = OpenSSL::PKey::RSA.new File.read("license_key.pub")
    Gitlab::License.encryption_key = public_key
     
    data = File.read("GitLabBV.gitlab-license")
    $license = Gitlab::License.import(data)
     
    puts "Imported license:"
    puts $license
     
    unless $license
      raise "The license is invalid."
    end
     
    if $license.restricted?(:active_user_count)
      active_user_count = 10000
      if active_user_count > $license.restrictions[:active_user_count]
        raise "The active user count exceeds the allowed amount!"
      end
    end
     
    if $license.notify_admins?
      puts "The license is due to expire on #{$license.expires_at}."
    end
     
    if $license.notify_users?
      puts "The license is due to expire on #{$license.expires_at}."
    end
     
    module Gitlab
      class GitAccess
        def check(cmd, changes = nil)
          if $license.block_changes?
            return build_status_object(false, "License expired")
          end
        end
      end
    end
     
    puts "This instance of GitLab Enterprise Edition is licensed to:"
    $license.licensee.each do |key, value|
      puts "#{key}: #{value}"
    end
     
    if $license.expired?
      puts "The license expired on #{$license.expires_at}"
    elsif $license.will_expire?
      puts "The license will expire on #{$license.expires_at}"
    else
      puts "The license will never expire."
    end
    
  4. 运行。

    ruby license.rb
    
  5. 正常情况下,该目录会多出三个文件,分别是GitLabBV.gitlab-license, license_key, license_key.pub.

  6. 覆盖公钥。

    cp license_key.pub /opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub
    
  7. 服务器断外网,这个很重要,试了好几次,终于在断网成功了。然后在设置->通用->添加许可证处上传GitLabBV.gitlab-license文件或贴上文件的内容。

  8. 重启Gitlab

    gitlab-ctl restart
    
  9. 修改等级。

    vim /opt/gitlab/embedded/service/gitlab-rails/ee/app/models/license.rb
    
    -    restricted_attr(:plan).presence || STARTER_PLAN
    +    restricted_attr(:plan).presence || ULTIMATE_PLAN
    
  10. 重启Gitlab

    gitlab-ctl restart
    
  11. Enjoy~

补充

更新后订阅会失效,再次替换文件重启即可。

cp license_key.pub /opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub
gitlab-ctl restart

参考

gitlab-license