Refactor layout
This commit is contained in:
@@ -34,15 +34,15 @@ module Pipeline
|
||||
end
|
||||
|
||||
require "pipeline/analyzer_repo"
|
||||
require "pipeline/analyzer_build"
|
||||
require "pipeline/validation/check_invokable"
|
||||
require "pipeline/validation/check_environment_invariants"
|
||||
require "pipeline/validation/check_fixtures"
|
||||
require "pipeline/validation/fixture_check_error.rb"
|
||||
require "pipeline/validate_build"
|
||||
require "pipeline/validation/validate_build"
|
||||
require "pipeline/util/container_driver"
|
||||
require "pipeline/util/runc_configurator"
|
||||
require "pipeline/util/img_wrapper"
|
||||
require "pipeline/util/runc_wrapper"
|
||||
require "pipeline/build_image"
|
||||
require "pipeline/publish_image"
|
||||
require "pipeline/build/build_image"
|
||||
require "pipeline/build/publish_image"
|
||||
require "pipeline/build/analyzer_build"
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
class Pipeline::AnalyzerBuild
|
||||
include Mandate
|
||||
|
||||
attr_accessor :img, :target_sha, :image_tag
|
||||
|
||||
initialize_with :build_tag, :track_slug
|
||||
|
||||
def call
|
||||
setup_utilities
|
||||
check_tag_exists
|
||||
build
|
||||
validate
|
||||
publish
|
||||
end
|
||||
|
||||
def setup_utilities
|
||||
@img = Pipeline::Util::ImgWrapper.new
|
||||
end
|
||||
|
||||
def check_tag_exists
|
||||
return if build_tag == "master"
|
||||
raise "Build tag does not exist" unless repo.tags[build_tag]
|
||||
end
|
||||
|
||||
def build
|
||||
@image_tag = Pipeline::BuildImage.(build_tag, image_name, repo, img)
|
||||
end
|
||||
|
||||
def validate
|
||||
Pipeline::ValidateBuild.(image_tag, "fixtures/#{track_slug}")
|
||||
end
|
||||
|
||||
def publish
|
||||
Pipeline::PublishImage.(img, image_name, image_tag, build_tag)
|
||||
end
|
||||
|
||||
def image_name
|
||||
suffix = "-dev" unless ENV["env"] == "production"
|
||||
"#{track_slug}-analyzer#{suffix}"
|
||||
end
|
||||
|
||||
memoize
|
||||
|
||||
def repo
|
||||
Pipeline::AnalyzerRepo.for_track(track_slug)
|
||||
end
|
||||
end
|
||||
49
lib/pipeline/build/analyzer_build.rb
Normal file
49
lib/pipeline/build/analyzer_build.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
module Pipeline::Build
|
||||
class AnalyzerBuild
|
||||
include Mandate
|
||||
|
||||
attr_accessor :img, :target_sha, :image_tag
|
||||
|
||||
initialize_with :build_tag, :track_slug
|
||||
|
||||
def call
|
||||
setup_utilities
|
||||
check_tag_exists
|
||||
build
|
||||
validate
|
||||
publish
|
||||
end
|
||||
|
||||
def setup_utilities
|
||||
@img = Pipeline::Util::ImgWrapper.new
|
||||
end
|
||||
|
||||
def check_tag_exists
|
||||
return if build_tag == "master"
|
||||
raise "Build tag does not exist" unless repo.tags[build_tag]
|
||||
end
|
||||
|
||||
def build
|
||||
@image_tag = Pipeline::Build::BuildImage.(build_tag, image_name, repo, img)
|
||||
end
|
||||
|
||||
def validate
|
||||
Pipeline::Validation::ValidateBuild.(image_tag, "fixtures/#{track_slug}")
|
||||
end
|
||||
|
||||
def publish
|
||||
Pipeline::Build::PublishImage.(img, image_name, image_tag, build_tag)
|
||||
end
|
||||
|
||||
def image_name
|
||||
suffix = "-dev" unless ENV["env"] == "production"
|
||||
"#{track_slug}-analyzer#{suffix}"
|
||||
end
|
||||
|
||||
memoize
|
||||
|
||||
def repo
|
||||
Pipeline::AnalyzerRepo.for_track(track_slug)
|
||||
end
|
||||
end
|
||||
end
|
||||
30
lib/pipeline/build/build_image.rb
Normal file
30
lib/pipeline/build/build_image.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
module Pipeline::Build
|
||||
class BuildImage
|
||||
include Mandate
|
||||
|
||||
attr_accessor :target_sha
|
||||
|
||||
initialize_with :build_tag, :image_slug, :repo, :img
|
||||
|
||||
def call
|
||||
repo.fetch!
|
||||
checkout
|
||||
build
|
||||
end
|
||||
|
||||
def checkout
|
||||
@target_sha = repo.checkout(build_tag)
|
||||
end
|
||||
|
||||
def build
|
||||
Dir.chdir(repo.workdir) do
|
||||
img.build(local_tag)
|
||||
end
|
||||
local_tag
|
||||
end
|
||||
|
||||
def local_tag
|
||||
"#{image_slug}:#{target_sha}"
|
||||
end
|
||||
end
|
||||
end
|
||||
79
lib/pipeline/build/publish_image.rb
Normal file
79
lib/pipeline/build/publish_image.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
module Pipeline::Build
|
||||
class PublishImage
|
||||
include Mandate
|
||||
|
||||
initialize_with :img, :image_name, :image_tag, :build_tag
|
||||
|
||||
def call
|
||||
puts "PUBLISHING #{image_tag}"
|
||||
puts "Login to repo"
|
||||
create_repository
|
||||
login_to_repository
|
||||
tag_build
|
||||
push_build
|
||||
logout
|
||||
end
|
||||
|
||||
def create_repository
|
||||
puts "Checking if repository exists"
|
||||
begin
|
||||
ecr.describe_repositories({
|
||||
repository_names: [image_name]
|
||||
})
|
||||
return
|
||||
rescue Aws::ECR::Errors::RepositoryNotFoundException
|
||||
puts "Repository #{image_name} not found"
|
||||
end
|
||||
puts "Creating repository"
|
||||
ecr.create_repository({
|
||||
repository_name: image_name,
|
||||
image_tag_mutability: "MUTABLE"
|
||||
})
|
||||
end
|
||||
|
||||
def login_to_repository
|
||||
authorization_token = ecr.get_authorization_token.authorization_data[0].authorization_token
|
||||
plain = Base64.decode64(authorization_token)
|
||||
user,password = plain.split(":")
|
||||
img.login("AWS", password, registry_endpoint)
|
||||
end
|
||||
|
||||
def logout
|
||||
img.logout(registry_endpoint)
|
||||
end
|
||||
|
||||
def tag_build
|
||||
img.tag(image_tag, remote_tag)
|
||||
img.tag(image_tag, remote_human_tag) unless build_tag.nil?
|
||||
img.tag(image_tag, remote_latest_tag)
|
||||
end
|
||||
|
||||
def push_build
|
||||
img.push(remote_tag)
|
||||
img.push(remote_human_tag) unless build_tag.nil?
|
||||
img.push(remote_latest_tag)
|
||||
end
|
||||
|
||||
def remote_tag
|
||||
"#{registry_endpoint}/#{image_tag}"
|
||||
end
|
||||
|
||||
def remote_human_tag
|
||||
"#{registry_endpoint}/#{image_name}:#{build_tag}"
|
||||
end
|
||||
|
||||
def remote_latest_tag
|
||||
"#{registry_endpoint}/#{image_name}:latest"
|
||||
end
|
||||
|
||||
def registry_endpoint
|
||||
Pipeline.config["registry_endpoint"]
|
||||
end
|
||||
|
||||
memoize
|
||||
def ecr
|
||||
Aws::ECR::Client.new(region: 'eu-west-1')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -1,28 +0,0 @@
|
||||
class Pipeline::BuildImage
|
||||
include Mandate
|
||||
|
||||
attr_accessor :target_sha
|
||||
|
||||
initialize_with :build_tag, :image_slug, :repo, :img
|
||||
|
||||
def call
|
||||
repo.fetch!
|
||||
checkout
|
||||
build
|
||||
end
|
||||
|
||||
def checkout
|
||||
@target_sha = repo.checkout(build_tag)
|
||||
end
|
||||
|
||||
def build
|
||||
Dir.chdir(repo.workdir) do
|
||||
img.build(local_tag)
|
||||
end
|
||||
local_tag
|
||||
end
|
||||
|
||||
def local_tag
|
||||
"#{image_slug}:#{target_sha}"
|
||||
end
|
||||
end
|
||||
@@ -1,77 +0,0 @@
|
||||
class Pipeline::PublishImage
|
||||
include Mandate
|
||||
|
||||
initialize_with :img, :image_name, :image_tag, :build_tag
|
||||
|
||||
def call
|
||||
puts "PUBLISHING #{image_tag}"
|
||||
puts "Login to repo"
|
||||
create_repository
|
||||
login_to_repository
|
||||
tag_build
|
||||
push_build
|
||||
logout
|
||||
end
|
||||
|
||||
def create_repository
|
||||
puts "Checking if repository exists"
|
||||
begin
|
||||
ecr.describe_repositories({
|
||||
repository_names: [image_name]
|
||||
})
|
||||
return
|
||||
rescue Aws::ECR::Errors::RepositoryNotFoundException
|
||||
puts "Repository #{image_name} not found"
|
||||
end
|
||||
puts "Creating repository"
|
||||
ecr.create_repository({
|
||||
repository_name: image_name,
|
||||
image_tag_mutability: "MUTABLE"
|
||||
})
|
||||
end
|
||||
|
||||
def login_to_repository
|
||||
authorization_token = ecr.get_authorization_token.authorization_data[0].authorization_token
|
||||
plain = Base64.decode64(authorization_token)
|
||||
user,password = plain.split(":")
|
||||
img.login("AWS", password, registry_endpoint)
|
||||
end
|
||||
|
||||
def logout
|
||||
img.logout(registry_endpoint)
|
||||
end
|
||||
|
||||
def tag_build
|
||||
img.tag(image_tag, remote_tag)
|
||||
img.tag(image_tag, remote_human_tag) unless build_tag.nil?
|
||||
img.tag(image_tag, remote_latest_tag)
|
||||
end
|
||||
|
||||
def push_build
|
||||
img.push(remote_tag)
|
||||
img.push(remote_human_tag) unless build_tag.nil?
|
||||
img.push(remote_latest_tag)
|
||||
end
|
||||
|
||||
def remote_tag
|
||||
"#{registry_endpoint}/#{image_tag}"
|
||||
end
|
||||
|
||||
def remote_human_tag
|
||||
"#{registry_endpoint}/#{image_name}:#{build_tag}"
|
||||
end
|
||||
|
||||
def remote_latest_tag
|
||||
"#{registry_endpoint}/#{image_name}:latest"
|
||||
end
|
||||
|
||||
def registry_endpoint
|
||||
Pipeline.config["registry_endpoint"]
|
||||
end
|
||||
|
||||
memoize
|
||||
def ecr
|
||||
Aws::ECR::Client.new(region: 'eu-west-1')
|
||||
end
|
||||
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
class Pipeline::ValidateBuild
|
||||
class Pipeline::Validation::ValidateBuild
|
||||
include Mandate
|
||||
|
||||
initialize_with :build_tag, :fixtures_folder
|
||||
@@ -1,7 +1,7 @@
|
||||
require 'test_helper'
|
||||
require 'json'
|
||||
|
||||
module Pipeline
|
||||
module Pipeline::Build
|
||||
class AnalyzerBuildTest < Minitest::Test
|
||||
|
||||
def setup
|
||||
@@ -48,13 +48,13 @@ module Pipeline
|
||||
stub_img = stub()
|
||||
@cmd.img = stub_img
|
||||
@cmd.expects(:repo).returns(stub_repo)
|
||||
Pipeline::BuildImage.expects(:call).with("v0.1.1", "demotrack-analyzer-dev", stub_repo, stub_img)
|
||||
Pipeline::Build::BuildImage.expects(:call).with("v0.1.1", "demotrack-analyzer-dev", stub_repo, stub_img)
|
||||
@cmd.build
|
||||
end
|
||||
|
||||
def test_validate_delegates_correctly
|
||||
@cmd.image_tag = "my_image_tag"
|
||||
Pipeline::ValidateBuild.expects(:call).with("my_image_tag", "fixtures/demotrack")
|
||||
Pipeline::Validation::ValidateBuild.expects(:call).with("my_image_tag", "fixtures/demotrack")
|
||||
@cmd.validate
|
||||
end
|
||||
|
||||
@@ -62,7 +62,7 @@ module Pipeline
|
||||
stub_img = stub()
|
||||
@cmd.img = stub_img
|
||||
@cmd.image_tag = "my_image_tag"
|
||||
Pipeline::PublishImage.expects(:call).with(stub_img, "demotrack-analyzer-dev", "my_image_tag", "v0.1.1")
|
||||
Pipeline::Build::PublishImage.expects(:call).with(stub_img, "demotrack-analyzer-dev", "my_image_tag", "v0.1.1")
|
||||
@cmd.publish
|
||||
end
|
||||
|
||||
@@ -8,8 +8,8 @@ module Pipeline
|
||||
demo_analyzer_repo = "https://github.com/exercism/stub-analyzer.git"
|
||||
repo = Pipeline::AnalyzerRepo.new(demo_analyzer_repo)
|
||||
img = Pipeline::Util::ImgWrapper.new
|
||||
image_tag = Pipeline::BuildImage.("master", "demo", repo, img)
|
||||
Pipeline::ValidateBuild.(image_tag, "test-fixtures/demo")
|
||||
image_tag = Pipeline::Build::BuildImage.("master", "demo", repo, img)
|
||||
Pipeline::Validation::ValidateBuild.(image_tag, "test-fixtures/demo")
|
||||
end
|
||||
|
||||
end
|
||||
15
test/pipeline/runtime/release_analyzer_test.rb
Normal file
15
test/pipeline/runtime/release_analyzer_test.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
require 'test_helper'
|
||||
require 'json'
|
||||
|
||||
module Pipeline::Runtime
|
||||
class ReleaseAnalyzerTest < Minitest::Test
|
||||
|
||||
def setup
|
||||
end
|
||||
|
||||
def test_call_invokes_each_phase
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -17,7 +17,7 @@ module Pipeline::Validation
|
||||
configurator = Pipeline::Util::RuncConfigurator.new
|
||||
configurator.seed_from_env
|
||||
|
||||
image_tag = Pipeline::BuildImage.("master", track_slug, repo, img)
|
||||
image_tag = Pipeline::Build::BuildImage.("master", track_slug, repo, img)
|
||||
|
||||
@container_driver = Pipeline::Util::ContainerDriver.new(runc, img, configurator, workdir)
|
||||
container_driver.prepare_workdir
|
||||
|
||||
Reference in New Issue
Block a user