From c0bc5dd5ee0ba75f485b0de2a2be5bebb8857302 Mon Sep 17 00:00:00 2001 From: joker1007 Date: Wed, 2 Aug 2017 14:00:06 +0900 Subject: [PATCH] Implement notifier base --- lib/wrapbox/notifier.rb | 23 +++++++++++++++++++++ lib/wrapbox/notifier/base.rb | 39 +++++++++++++++++++++++++++++++++++ lib/wrapbox/notifier/slack.rb | 31 ++++++++++++++++++++++++++++ lib/wrapbox/runner/docker.rb | 7 +++++++ lib/wrapbox/runner/ecs.rb | 3 +++ 5 files changed, 103 insertions(+) create mode 100644 lib/wrapbox/notifier.rb create mode 100644 lib/wrapbox/notifier/base.rb create mode 100644 lib/wrapbox/notifier/slack.rb diff --git a/lib/wrapbox/notifier.rb b/lib/wrapbox/notifier.rb new file mode 100644 index 0000000..51b37ec --- /dev/null +++ b/lib/wrapbox/notifier.rb @@ -0,0 +1,23 @@ +module Wrapbox + module Notifier + class << self + @types = {} + def types + @types + end + + def register(name, klass) + @types[name.to_sym] = klass + end + + def get_notifier_class(name) + @types.fetch(name.to_sym) + end + + def build_notifier(notifier_definition) + klass = get_notifier_class(notifier_definition.delete(:type)) + klass.new(notifier_definition) + end + end + end +end diff --git a/lib/wrapbox/notifier/base.rb b/lib/wrapbox/notifier/base.rb new file mode 100644 index 0000000..8d99047 --- /dev/null +++ b/lib/wrapbox/notifier/base.rb @@ -0,0 +1,39 @@ +require 'shellwords' + +module Wrapbox + module Notifier + class Base + class << self + def register(name) + Wrapbox::Notifier.register(name, self) + end + end + + def initialize(_definition) + raise NotImplementedError + end + + def notify(cmd:, environments:, definition:, **options) + body = build_body(cmd, environments, definition) + do_notify(body: body, **options) + end + + private + + def do_notify(body:, **options) + raise NotImplementedError + end + + def build_body(cmd, environments, container_definition) + command_string = Shellwords.shelljoin(cmd) + environments_string = environments.map { |env| "#{env[:name]}=#{env[:value]}" }.join(" ") + container_definition_string = container_definition.to_s + <<~BODY + cmd = #{command_string} + envs = #{environments_string} + def = #{container_definition_string} + BODY + end + end + end +end diff --git a/lib/wrapbox/notifier/slack.rb b/lib/wrapbox/notifier/slack.rb new file mode 100644 index 0000000..5851906 --- /dev/null +++ b/lib/wrapbox/notifier/slack.rb @@ -0,0 +1,31 @@ +require "net/http" +require "json" + +module Wrapbox + module Notifier + class Slack < Base + register :slack + + def initialize(title: nil, webhook_url:, color: nil) + @webhook_uri = URI.parse(webhook_url) + @base_options = { + "title" => title, + "color" => color, + "mrkdwn_in" => ["text"], + "short" => false, + } + end + + def do_notify(body:, **options) + http = Net::HTTP.new(@webhook_uri.host) + http.use_ssl = true + http.ssl_version = 'TLSv1_2' + + req = Net::HTTP::Post.new(@webhook_uri.path, {"Content-Type" => "application/json"}) + req.body = JSON.generate(@base_options.merge({"text" => "```#{body}```"})) + + http.start { |session| session.request(req) } + end + end + end +end diff --git a/lib/wrapbox/runner/docker.rb b/lib/wrapbox/runner/docker.rb index 8292955..080fe41 100644 --- a/lib/wrapbox/runner/docker.rb +++ b/lib/wrapbox/runner/docker.rb @@ -17,6 +17,9 @@ def initialize(options) @name = options[:name] @container_definition = options[:container_definition] @keep_container = options[:keep_container] + @notifiers = options[:notifiers].map do |notifier_definition| + Notifiers.build_notifier(notifier_definition) + end end def run(class_name, method_name, args, container_definition_overrides: {}, environments: []) @@ -57,6 +60,10 @@ def extract_environments(environments) end def exec_docker(definition:, cmd:, environments: []) + @notifiers.each do |n| + n.notify(definition: definition, cmd: ["bundle", "exec", "rake", "wrapbox:run"], environments: envs) + end + ::Docker::Image.create("fromImage" => definition[:image]) options = { "Image" => definition[:image], diff --git a/lib/wrapbox/runner/ecs.rb b/lib/wrapbox/runner/ecs.rb index 64ac8f4..2b53683 100644 --- a/lib/wrapbox/runner/ecs.rb +++ b/lib/wrapbox/runner/ecs.rb @@ -41,6 +41,9 @@ def initialize(options) @task_role_arn = options[:task_role_arn] $stdout.sync = true @logger = Logger.new($stdout) + @notifiers = options[:notifiers].map do |notifier_definition| + Notifiers.build_notifier(notifier_definition) + end end class Parameter