-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from itamae-kitchen/handler
Introduce Handler (experimental feature)
- Loading branch information
Showing
17 changed files
with
367 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'itamae/handler/base' | ||
|
||
module Itamae | ||
module Handler | ||
def self.from_type(type) | ||
first_time = true | ||
|
||
class_name = type.split('_').map(&:capitalize).join | ||
self.const_get(class_name) | ||
rescue NameError | ||
require "itamae/handler/#{type}" | ||
|
||
if first_time | ||
first_time = false | ||
retry | ||
else | ||
raise | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'socket' | ||
|
||
module Itamae | ||
module Handler | ||
class Base | ||
attr_reader :recipes, :resources, :actions | ||
|
||
def initialize(options) | ||
@options = options | ||
|
||
@recipes = [] | ||
@resources = [] | ||
@actions = [] | ||
end | ||
|
||
def event(type, payload = {}) | ||
case type | ||
when :recipe_started | ||
@recipes << payload | ||
when :recipe_completed, :recipe_failed | ||
@recipes.pop | ||
when :resource_started | ||
@resources << payload | ||
when :resource_completed, :resource_failed | ||
@resources.pop | ||
when :action_started | ||
@actions << payload | ||
when :action_completed, :action_failed | ||
@actions.pop | ||
end | ||
end | ||
|
||
private | ||
|
||
def hostname | ||
@hostname ||= @options['hostname'] || Socket.gethostname | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Itamae | ||
module Handler | ||
class Debug < Base | ||
def event(type, payload = {}) | ||
super | ||
Itamae.logger.info("EVENT:#{type} #{payload}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Itamae | ||
module Handler | ||
class Fluentd < Base | ||
attr_accessor :fluent_logger # for test | ||
|
||
def initialize(*) | ||
super | ||
load_fluent_logger | ||
end | ||
|
||
def event(type, payload = {}) | ||
super | ||
|
||
unless @fluent_logger.post(type, payload.merge(hostname: hostname)) | ||
Itamae.logger.warn "Sending logs to Fluentd failed: #{@fluent_logger.last_error}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_fluent_logger | ||
begin | ||
require 'fluent-logger' | ||
rescue LoadError | ||
raise "Loading fluent-logger gem failed. Please install 'fluent-logger' gem to use fluentd handler." | ||
end | ||
|
||
@fluent_logger = Fluent::Logger::FluentLogger.new(tag_prefix, host: fluentd_host, port: fluentd_port) | ||
end | ||
|
||
def tag_prefix | ||
@options['tag_prefix'] || 'itamae_server' | ||
end | ||
|
||
def fluentd_host | ||
@options['host'] || 'localhost' | ||
end | ||
|
||
def fluentd_port | ||
(@options['port'] || 24224).to_i | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Itamae | ||
module Handler | ||
class Json < Base | ||
def initialize(*) | ||
super | ||
require 'time' | ||
open_file | ||
end | ||
|
||
def event(type, payload = {}) | ||
super | ||
@f.puts({'time' => Time.now.iso8601, 'event' => type, 'payload' => payload}.to_json) | ||
end | ||
|
||
private | ||
|
||
def open_file | ||
@f = open(@options.fetch('path'), 'a') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Itamae | ||
class HandlerProxy | ||
def initialize | ||
@instances = [] | ||
end | ||
|
||
def register_instance(instance) | ||
@instances << instance | ||
end | ||
|
||
def event(*args, &block) | ||
if block_given? | ||
_event_with_block(*args, &block) | ||
else | ||
_event(*args) | ||
end | ||
end | ||
|
||
private | ||
|
||
def _event(*args) | ||
@instances.each do |i| | ||
i.event(*args) | ||
end | ||
end | ||
|
||
def _event_with_block(event_name, *args, &block) | ||
event("#{event_name}_started".to_sym, *args) | ||
block.call | ||
rescue | ||
event("#{event_name}_failed".to_sym, *args) | ||
raise | ||
else | ||
event("#{event_name}_completed".to_sym, *args) | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.