class Kyklos::CLI

Attributes

config_path[R]
job_list[R]

Public Class Methods

new(config_path:, adapter:, adapter_args: [], identifier: 'default') click to toggle source
# File lib/kyklos/cli.rb, line 12
def initialize(config_path:, adapter:, adapter_args: [], identifier: 'default')
  @config_path = config_path.to_s
  @adapter = adapter
  @adapter_args = adapter_args
  @identifier = identifier
  @job_list = Kyklos.parse(File.read(config_path))
end

Public Instance Methods

deploy() click to toggle source
# File lib/kyklos/cli.rb, line 20
def deploy
  cleanup
  put_jobs
end

Private Instance Methods

adapter() click to toggle source
# File lib/kyklos/cli.rb, line 89
def adapter
  adapter_klass = Kyklos::Adapters.const_get("#{@adapter.capitalize}Adapter")
  adapter_klass.new(*@adapter_args)
end
cleanup() click to toggle source
# File lib/kyklos/cli.rb, line 31
def cleanup
  names = eventnames
  find_rules.each do |rule|
    unless names.include?(rule.name)
      remove_job(rule)
    end
  end
end
cloudwatchevents() click to toggle source
# File lib/kyklos/cli.rb, line 27
def cloudwatchevents
  @cloudwatchevents ||= Aws::CloudWatchEvents::Client.new
end
eventname(job_id) click to toggle source
# File lib/kyklos/cli.rb, line 81
def eventname(job_id)
  "#{rule_name_prefix}#{Digest::MD5.hexdigest(job_id.to_s)}"
end
eventnames() click to toggle source
# File lib/kyklos/cli.rb, line 85
def eventnames
  job_list.each.map { |job_id, _job| eventname(job_id) }
end
find_rules() click to toggle source
# File lib/kyklos/cli.rb, line 94
def find_rules
  list_rules = ->(next_token) do
    resp = cloudwatchevents.
        list_rules(name_prefix: rule_name_prefix,
                   next_token: next_token)
    if resp.next_token
      resp.rules + list_rules.call(next_token)
    else
      resp.rules
    end
  end
  list_rules.call(nil)
end
find_targets(rule_name) click to toggle source
# File lib/kyklos/cli.rb, line 108
def find_targets(rule_name)
  list_targets = ->(next_token) do
    resp = cloudwatchevents.
        list_targets_by_rule(rule: rule_name,
                             next_token: next_token)
    if resp.next_token
      resp.targets + list_targets.call(next_token)
    else
      resp.targets
    end
  end
  list_targets.call(nil)
end
put_job(job_id, job) click to toggle source
# File lib/kyklos/cli.rb, line 46
def put_job(job_id, job)
  rule_params = {
      name: eventname(job_id),
      event_pattern: '',
      state: 'DISABLED',
      description: [@identifier, job.description].compact.join(': '),
      schedule_expression: job.schedule_expression,
  }
  cloudwatchevents.put_rule(rule_params)
  rule = cloudwatchevents.describe_rule(name: rule_params[:name])
  targets = adapter.assign_cloudwatchevents(
      job_id: job_id,
      rule_name_prefix: rule_name_prefix,
      rule: rule)
  cloudwatchevents.put_targets(
      rule: rule.name,
      targets: targets
  )
  cloudwatchevents.enable_rule(name: rule.name)
end
put_jobs() click to toggle source
# File lib/kyklos/cli.rb, line 40
def put_jobs
  job_list.each do |job_id, job|
    put_job(job_id, job)
  end
end
remove_job(rule) click to toggle source
# File lib/kyklos/cli.rb, line 67
def remove_job(rule)
  adapter.unassign_cloudwatchevents(rule_name_prefix: rule_name_prefix, rule: rule)

  target_ids = find_targets(rule.name).map(&:id)
  unless target_ids.empty?
    cloudwatchevents.remove_targets(rule: rule.name, ids: target_ids)
  end
  cloudwatchevents.delete_rule(name: rule.name)
end
rule_name_prefix() click to toggle source
# File lib/kyklos/cli.rb, line 77
def rule_name_prefix
  "kyslos-#{"#{@identifier}/#{Digest::MD5.hexdigest(@identifier)}".sum}-"
end