diff --git a/.gitignore b/.gitignore index bf9dfdc..3b0b961 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.swp .DS_Store +tmp/* +opt/ diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a88b301 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source :rubygems + +gem "activesupport" +gem "mandate" +gem "propono" +gem "rugged" +gem 'aws-sdk-ecr' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..c637eed --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,52 @@ +GEM + remote: http://rubygems.org/ + specs: + activesupport (5.2.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + aws-eventstream (1.0.3) + aws-partitions (1.196.0) + aws-sdk-core (3.62.0) + aws-eventstream (~> 1.0, >= 1.0.2) + aws-partitions (~> 1.0) + aws-sigv4 (~> 1.1) + jmespath (~> 1.0) + aws-sdk-ecr (1.20.0) + aws-sdk-core (~> 3, >= 3.61.1) + aws-sigv4 (~> 1.1) + aws-sdk-sns (1.19.0) + aws-sdk-core (~> 3, >= 3.61.1) + aws-sigv4 (~> 1.1) + aws-sdk-sqs (1.20.0) + aws-sdk-core (~> 3, >= 3.61.1) + aws-sigv4 (~> 1.1) + aws-sigv4 (1.1.0) + aws-eventstream (~> 1.0, >= 1.0.2) + concurrent-ruby (1.1.5) + i18n (1.6.0) + concurrent-ruby (~> 1.0) + jmespath (1.4.0) + mandate (0.2.0) + minitest (5.11.3) + propono (2.1.0) + aws-sdk-sns + aws-sdk-sqs + rugged (0.28.2) + thread_safe (0.3.6) + tzinfo (1.2.5) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + activesupport + aws-sdk-ecr + mandate + propono + rugged + +BUNDLED WITH + 1.17.2 diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/bin/builderd b/bin/builderd new file mode 100755 index 0000000..c08f1fa --- /dev/null +++ b/bin/builderd @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby + +puts "OK" diff --git a/bin/spike b/bin/spike new file mode 100755 index 0000000..54d7c9d --- /dev/null +++ b/bin/spike @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby +require "bundler/setup" +$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) + +require "pipeline" + +Pipeline.spike diff --git a/lib/pipeline.rb b/lib/pipeline.rb new file mode 100644 index 0000000..ae798a1 --- /dev/null +++ b/lib/pipeline.rb @@ -0,0 +1,21 @@ +require "mandate" +require "propono" +require "active_support" +require 'securerandom' +require 'rugged' +require 'aws-sdk-ecr' + +Aws.config.update({ + credentials: Aws::Credentials.new('AKIAZ5OU5BBSQDMMFQ7J', '+Q2fMSju+dJljn6G2XFZOt6vUHgSF56P736yPQhj') +}) + +module Pipeline + def self.spike + puts "OK" + AnalyzerBuild.("ruby") + puts "DONE" + end +end + +require "pipeline/analyzer_repo" +require "pipeline/analyzer_build" diff --git a/lib/pipeline/analyzer_build.rb b/lib/pipeline/analyzer_build.rb new file mode 100644 index 0000000..ad295b5 --- /dev/null +++ b/lib/pipeline/analyzer_build.rb @@ -0,0 +1,88 @@ +class Pipeline::AnalyzerBuild + include Mandate + + attr_accessor :img + + initialize_with :track_slug + + def call + @img = File.expand_path "./opt/img" + repo.fetch! + checkout + build + puts "login" + login_to_repository + tag_build + push_build + logout + end + + + + def checkout + repo.checkout("master") + end + + def build + opt_dir = File.expand_path "./opt" + Dir.chdir(repo.workdir) do + cmd = "#{build_cmd} -t #{current_tag} ." + exec_cmd cmd + end + end + + def login_to_repository + ecr = Aws::ECR::Client.new(region: 'eu-west-1') + authorization_token = ecr.get_authorization_token.authorization_data[0].authorization_token + plain = Base64.decode64(authorization_token) + user,password = plain.split(":") + exec_cmd "#{img} login -u AWS -p \"#{password}\" #{registry_endpoint}" + end + + def logout + exec_cmd "#{img} logout #{registry_endpoint}" + end + + def tag_build + # exec_cmd "#{img} tag #{current_tag} #{registry_endpoint}/#{current_tag}" + # exec_cmd "#{img} tag #{current_tag} #{registry_endpoint}/#{slug}:latest" + end + + def push_build + exec_cmd "#{push_cmd} #{current_tag}" + # exec_cmd "#{img} push #{registry_endpoint}/#{slug}:latest" + end + + def push_cmd + "#{img} push -state /tmp/state-img" + end + + def build_cmd + "#{img} build -state /tmp/state-img" + end + + def exec_cmd(cmd) + puts "> #{cmd}" + puts "------------------------------------------------------------" + success = system({}, cmd) + raise "Failed #{cmd}" unless success + end + + def current_tag + "#{registry_endpoint}/#{slug}:abc" + end + + def registry_endpoint + "681735686245.dkr.ecr.eu-west-1.amazonaws.com" + end + + def slug + "#{track_slug}-analyzer-dev" + end + + memoize + def repo + repo_url = "https://github.com/exercism/#{track_slug}-analyzer" + Pipeline::AnalyzerRepo.new(repo_url) + end +end diff --git a/lib/pipeline/analyzer_repo.rb b/lib/pipeline/analyzer_repo.rb new file mode 100644 index 0000000..2e1122d --- /dev/null +++ b/lib/pipeline/analyzer_repo.rb @@ -0,0 +1,62 @@ +class Pipeline::AnalyzerRepo + + BASE_DIR = ENV.fetch("ANALYZER_REPO_BASE_DIR", "./tmp/repos") + + attr_reader :repo_url + + def initialize(repo_url) + @repo_url = repo_url + puts repo_dir + end + + def fetch! + repo.fetch('origin') + end + + def head + head_commit.oid + end + + def checkout(ref) + repo.checkout(ref) + end + + def workdir + repo.workdir + end + + private + + def repo + @repo ||= if repo_dir_exists? + Rugged::Repository.new(repo_dir) + else + Rugged::Repository.clone_at(repo_url, repo_dir) + end + rescue => e + puts "Failed to clone repo #{repo_url}" + puts e.message + raise + end + + def main_branch_ref + "origin/master" + end + + def repo_dir_exists? + File.directory?(repo_dir) + end + + def repo_dir + "#{BASE_DIR}/#{url_hash}-#{local_name}" + end + + def url_hash + Digest::SHA1.hexdigest(repo_url) + end + + def local_name + repo_url.split("/").last + end + +end