Skip to content
Snippets Groups Projects
Commit b56c3c1e authored by Ian Baum's avatar Ian Baum
Browse files

Merge branch 'delete-ami-deletion-cruft' into 'master'

Build AMIs for all tags except RC and auto-deploy ones

See merge request gitlab-org/omnibus-gitlab!4036
parents 0d372d21 d72b2049
No related branches found
No related tags found
No related merge requests found
---
title: Build AMIs for all tags except RC and auto-deploy ones
merge_request: 4036
author:
type: other
......@@ -3,8 +3,6 @@
require_relative 'util.rb'
class AWSHelper
VERSION_REGEX = /\A(?<version>\d+\.\d+\.\d+)-?(?<type>(ee|ce))?\z/.freeze
def initialize(version, type)
# version specifies the GitLab version being processed
# type specifies whether it is CE or EE being processed
......@@ -17,7 +15,6 @@
@type = "ee-#{release_type}"
@license_file = "AWS_#{release_type}_LICENSE_FILE".upcase
end
@clients = {}
@download_url = Build::Info.package_download_url
end
......@@ -21,85 +18,6 @@
@download_url = Build::Info.package_download_url
end
def ec2_client(region)
@clients[region] ||= Aws::EC2::Client.new(region: region)
end
def image_version(image)
tag = image.tags.find { |tag| tag.key == "Version" }
tag.value
end
def image_filter
{
dry_run: false,
filters: [
{
name: "tag-key",
values: ["Type"],
},
{
name: "tag-value",
values: [category]
}
],
}
end
def list_regions
# AWS API mandatorily requires a region to be set for every client. Hence
# using us-east-1 as a default region
client = ec2_client('us-east-1')
client.describe_regions.regions.map(&:region_name)
end
def category
if @type == "ce"
"GitLab Community Edition"
elsif @type == "ee"
"GitLab Enterprise Edition"
elsif @type == "ee-ultimate"
"GitLab Enterprise Edition Ultimate"
elsif @type == "ee-premium"
"GitLab Enterprise Edition Premium"
end
end
def list_images(region)
client = ec2_client(region)
resp = client.describe_images(image_filter)
resp.images
end
def delete_smaller(region)
# client = ec2_client(region)
images = list_images(region)
images.each do |image|
next unless Gem::Version.new(image_version(image)) < Gem::Version.new(@version)
puts "\t#{image.image_id} - #{image.name} - #{image_version(image)}"
# Commenting out actual deregister code temporarily for first few releases
# client.deregister_image({
# dry_run: false,
# image_id: image.image_id
# })
end
end
def search_for_greater
puts "Checking if greater version already exists"
flag = false
images = list_images('us-east-1')
images.each do |image|
if Gem::Version.new(image_version(image)) >= Gem::Version.new(@version)
flag = true
break
end
end
flag
end
def create_ami
system(*%W[support/packer/packer_ami.sh #{@version} #{@type} #{@download_url} #{@license_file}])
end
......@@ -103,21 +21,4 @@
def create_ami
system(*%W[support/packer/packer_ami.sh #{@version} #{@type} #{@download_url} #{@license_file}])
end
def process
puts "Finding existing images of #{category}"
greater_exist = search_for_greater
if greater_exist
puts "Greater version already exists. Skipping"
else
puts "No greater version exists. Creating AMI"
status = create_ami
if status
list_regions.each do |region|
puts "Deleting smaller images from #{region}"
delete_smaller(region)
end
end
end
end
end
......@@ -6,9 +6,11 @@
namespace :aws do
desc "Perform operations related to AWS AMI"
task :process do
if Build::Check.is_latest_stable_tag?
Omnibus.load_configuration('omnibus.rb')
AWSHelper.new(Omnibus::BuildVersion.semver, Build::Info.edition).process
end
next unless Build::Check.on_tag?
next if Build::Check.is_auto_deploy? || Build::Check.is_rc_tag?
Omnibus.load_configuration('omnibus.rb')
AWSHelper.new(Omnibus::BuildVersion.semver, Build::Info.edition).create_ami
end
end
......@@ -33,8 +33,6 @@
end
describe 'aws:process', type: :rake do
let(:dummy_client) { AwsDummyClass.new }
before :all do
Rake.application.rake_require 'gitlab/tasks/aws'
end
......@@ -42,36 +40,5 @@
before do
Rake::Task['aws:process'].reenable
allow_any_instance_of(Kernel).to receive(:system).and_return(true)
allow(Aws::EC2::Client).to receive(:new).and_return(dummy_client)
end
it 'should identify ce category correctly, if specified' do
allow(Build::Info).to receive(:edition).and_return('ce')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect { Rake::Task['aws:process'].invoke }.to output(/Finding existing images of GitLab Community Edition/).to_stdout
end
it 'should identify ce category correctly if nothing is specified' do
allow(Build::Info).to receive(:edition).and_return(nil)
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect { Rake::Task['aws:process'].invoke }.to output(/Finding existing images of GitLab Community Edition/).to_stdout
end
it 'should identify ee category correctly' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect { Rake::Task['aws:process'].invoke }.to output(/Finding existing images of GitLab Enterprise Edition/).to_stdout
end
it 'should identify ee ultimate category correctly' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Gitlab::Util).to receive(:get_env).and_call_original
allow(Gitlab::Util).to receive(:get_env).with("AWS_RELEASE_TYPE").and_return('ultimate')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect { Rake::Task['aws:process'].invoke }.to output(/Finding existing images of GitLab Enterprise Edition Ultimate/).to_stdout
end
......@@ -76,8 +43,28 @@
end
it 'should identify ee premium category correctly' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Gitlab::Util).to receive(:get_env).and_call_original
allow(Gitlab::Util).to receive(:get_env).with("AWS_RELEASE_TYPE").and_return('premium')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
describe 'on a regular tag' do
before do
allow(Build::Check).to receive(:on_tag?).and_return(true)
allow(Build::Check).to receive(:is_auto_deploy?).and_return(false)
allow(Build::Check).to receive(:is_rc_tag?).and_return(false)
allow(Build::Info).to receive(:package_download_url).and_return('http://example.com')
end
it 'should identify ce category correctly, if specified' do
allow(Build::Info).to receive(:edition).and_return('ce')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect_any_instance_of(Kernel).to receive(:system).with(*["support/packer/packer_ami.sh", "9.3.0", "ce", "http://example.com", ""])
Rake::Task['aws:process'].invoke
end
it 'should identify ce category correctly if nothing is specified' do
allow(Build::Info).to receive(:edition).and_return(nil)
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect_any_instance_of(Kernel).to receive(:system).with(*["support/packer/packer_ami.sh", "9.3.0", "ce", "http://example.com", ""])
Rake::Task['aws:process'].invoke
end
......@@ -83,4 +70,33 @@
expect { Rake::Task['aws:process'].invoke }.to output(/Finding existing images of GitLab Enterprise Edition Premium/).to_stdout
it 'should identify ee category correctly' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect_any_instance_of(Kernel).to receive(:system).with(*["support/packer/packer_ami.sh", "9.3.0", "ee", "http://example.com", ""])
Rake::Task['aws:process'].invoke
end
it 'should identify ee ultimate category correctly' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Gitlab::Util).to receive(:get_env).and_call_original
allow(Gitlab::Util).to receive(:get_env).with("AWS_RELEASE_TYPE").and_return('ultimate')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect_any_instance_of(Kernel).to receive(:system).with(*["support/packer/packer_ami.sh", "9.3.0", "ee-ultimate", "http://example.com", "AWS_ULTIMATE_LICENSE_FILE"])
Rake::Task['aws:process'].invoke
end
it 'should identify ee premium category correctly' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Gitlab::Util).to receive(:get_env).and_call_original
allow(Gitlab::Util).to receive(:get_env).with("AWS_RELEASE_TYPE").and_return('premium')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('9.3.0')
expect_any_instance_of(Kernel).to receive(:system).with(*["support/packer/packer_ami.sh", "9.3.0", "ee-premium", "http://example.com", "AWS_PREMIUM_LICENSE_FILE"])
Rake::Task['aws:process'].invoke
end
end
......@@ -85,8 +101,10 @@
end
# it 'should delete existing smaller versioned AMIs' do
# allow(File).to receive(:read).with('VERSION').and_return('8.16.4-ce')
# expect_any_instance_of(AwsDummyClass).to receive(:deregister_image).and_return(true)
# expect { Rake::Task['aws:process'].invoke }.to output(/Found to be smaller. Deleting/).to_stdout
# end
describe 'on an rc tag' do
before do
allow(Build::Check).to receive(:on_tag?).and_return(true)
allow(Build::Check).to receive(:is_auto_deploy?).and_return(false)
allow(Build::Check).to receive(:is_rc_tag?).and_return(true)
allow(Build::Info).to receive(:package_download_url).and_return('http://example.com')
end
......@@ -92,8 +110,4 @@
it 'should call packer with necessary arguments' do
allow(Build::Info).to receive(:edition).and_return('ce')
allow(Build::Info).to receive(:package_download_url).and_return('http://example.com')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('8.16.4')
allow(Build::Check).to receive(:is_ee?).and_return(false)
allow(Build::Check).to receive(:match_tag?).and_return(true)
it 'does not do anything' do
expect(AWSHelper).not_to receive(:new)
......@@ -99,5 +113,5 @@
expect_any_instance_of(Kernel).to receive(:system).with(*["support/packer/packer_ami.sh", "8.16.4", "ce", "http://example.com", ""])
expect { Rake::Task['aws:process'].invoke }.to output(/No greater version exists. Creating AMI/).to_stdout
Rake::Task['aws:process'].invoke
end
end
......@@ -102,6 +116,10 @@
end
it 'should identify existing greater versioned AMIs' do
allow(Build::Info).to receive(:edition).and_return('ee')
allow(Omnibus::BuildVersion).to receive(:semver).and_return('8.16.4')
describe 'on an auto-deploy tag' do
before do
allow(Build::Check).to receive(:on_tag?).and_return(true)
allow(Build::Check).to receive(:is_auto_deploy?).and_return(true)
allow(Build::Check).to receive(:is_rc_tag?).and_return(false)
allow(Build::Info).to receive(:package_download_url).and_return('http://example.com')
end
......@@ -107,4 +125,4 @@
expect { Rake::Task['aws:process'].invoke }.to output(/Greater version already exists. Skipping/).to_stdout
end
it 'does not do anything' do
expect(AWSHelper).not_to receive(:new)
......@@ -110,6 +128,5 @@
it 'should not build cloud image for RC versions' do
allow(Build::Check).to receive(:match_tag?).and_return(false)
expect(Kernel).not_to receive(:system)
Rake::Task['aws:process'].invoke
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment