Commit bd18aa43 authored by David Fernandez's avatar David Fernandez

Add seed for the license file

Use a common rake task between the development and production seed
parent e2b86d1d
# frozen_string_literal: true
Gitlab::Seeder.quiet do
Rake::Task['gitlab:license:load'].invoke('verbose')
end
# frozen_string_literal: true
default_license_file = Settings.source.dirname + 'Gitlab.gitlab-license'
license_file = Pathname.new(ENV.fetch('GITLAB_LICENSE_FILE', default_license_file))
# Do not fail if the license was not specified in configuration or a file
# placed at the expected locations.
if license_file.exist?
if ::License.new(data: license_file.read).save
puts "License Added:\n\nFilePath: #{license_file}".color(:green)
else
puts "License Invalid:\n\nFilePath: #{license_file}".color(:red)
raise "License Invalid"
end
elsif !ENV['GITLAB_LICENSE_FILE'].blank?
puts "License File Missing:\n\nFilePath: #{license_file}".color(:red)
raise "License File Missing"
end
Rake::Task['gitlab:license:load'].invoke
......@@ -12,5 +12,32 @@ namespace :gitlab do
puts "License valid from: #{license[:license_starts_at]} to #{license[:license_expires_at]}"
puts "Email associated with license: #{license[:licensee]['Email']}"
end
task :load, [:mode] => :environment do |_, args|
args.with_defaults(mode: 'default')
verbose = args[:mode] == 'verbose'
flag = 'GITLAB_LICENSE_FILE'
if ENV[flag].blank? && verbose
puts "Skipped. Use the `#{flag}` environment variable to seed the License file of the given path."
next
end
default_license_file = Settings.source.dirname + 'Gitlab.gitlab-license'
license_file = ENV.fetch(flag, default_license_file)
if File.file?(license_file)
if ::License.create(data: File.read(license_file))
puts "License Added:\n\nFilePath: #{license_file}".color(:green)
else
puts "License Invalid:\n\nFilePath: #{license_file}".color(:red)
raise "License Invalid"
end
elsif !ENV[flag].blank?
puts "License File Missing:\n\nFilePath: #{license_file}".color(:red)
raise "License File Missing"
end
end
end
end
......@@ -4,52 +4,9 @@ require 'spec_helper'
describe 'Automated License Installation' do
subject { load Rails.root.join('ee', 'db', 'fixtures', 'production', '010_license.rb') }
it 'works when no license to be installed' do
expect { subject }.not_to raise_error
end
context 'when GITLAB_LICENSE_FILE env variable is set' do
let(:license_path) { 'arbitrary_file_name' }
before do
stub_env('GITLAB_LICENSE_FILE', license_path)
end
it 'fails when the file does not exist' do
license_file = double('Pathname', exist?: false)
allow(Pathname).to receive(:new).and_call_original
expect(Pathname).to receive(:new).with(license_path).and_return(license_file)
expect { subject }.to raise_error(RuntimeError, "License File Missing")
end
context 'when the file does exist' do
before do
license_file = double('Pathname', exist?: true, read: license_file_contents)
allow(Pathname).to receive(:new).and_call_original
expect(Pathname).to receive(:new).with(license_path).and_return(license_file)
end
context 'and contains a valid license' do
let(:license_file_contents) { 'valid contents' }
it 'succeeds in adding the license' do
license = double('License', save: true)
expect(License).to receive(:new).with(data: license_file_contents).and_return(license)
expect { subject }.not_to raise_error
end
end
context 'but does not contain a valid license' do
let(:license_file_contents) { 'invalid contents' }
it 'fails to add the license' do
license = double('License', save: false)
expect(License).to receive(:new).with(data: license_file_contents).and_return(license)
it 'executes the gitlab:license:load task' do
expect(Rake::Task).to receive(:[]).with('gitlab:license:load').and_return(OpenStruct.new(invoke: true))
expect { subject }.to raise_error(RuntimeError, "License Invalid")
end
end
end
subject
end
end
# frozen_string_literal: true
require 'rake_helper'
describe 'gitlab:license namespace rake tasks' do
before do
Rake.application.rake_require 'tasks/gitlab/license'
end
describe 'load' do
let_it_be(:license_path) { 'arbitrary_file_name' }
let(:mode) { 'default' }
subject { run_rake_task 'gitlab:license:load', [mode] }
it 'works when no license to be installed' do
expect { subject }.not_to raise_error
end
context 'when GITLAB_LICENSE_FILE env variable is set' do
before do
stub_env('GITLAB_LICENSE_FILE', license_path)
end
it 'fails when the file does not exist' do
expect(File).to receive(:file?).with(license_path).and_return(false)
expect { subject }.to raise_error(RuntimeError, "License File Missing")
end
context 'when the file does exist' do
before do
expect(File).to receive(:file?).with(license_path).and_return(true)
end
context 'and contains a valid license' do
let(:license_file_contents) { 'valid contents' }
it 'succeeds in adding the license' do
expect(File).to receive(:read).with(license_path).and_return(license_file_contents)
expect(License).to receive(:create).with(data: license_file_contents).and_return(true)
expect { subject }.not_to raise_error
end
end
context 'but does not contain a valid license' do
let(:license_file_contents) { 'invalid contents' }
it 'fails to add the license' do
expect(File).to receive(:read).with(license_path).and_return(license_file_contents)
expect(License).to receive(:create).with(data: license_file_contents).and_return(false)
expect { subject }.to raise_error(RuntimeError, "License Invalid")
end
end
end
end
context 'running in mode verbose' do
let(:mode) { 'verbose' }
it 'outputs a the help message' do
expect { subject }.to output(/environment variable to seed the License file of the given path/).to_stdout
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment