Class: CloudWatchEventManager

Inherits:
Object
  • Object
show all
Defined in:
lib/hibernate/cloud_watch_event_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events_client, instance_id = nil, instance_name = nil, lambda_function_arn) ⇒ CloudWatchEventManager

Returns a new instance of CloudWatchEventManager.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 8

def initialize(events_client, instance_id = nil, instance_name = nil, lambda_function_arn)
  config_loader = Hibernate::ConfigLoader.new
  @events_client = events_client
  @instance_id = instance_id
  @instance_name = instance_name
  @lambda_function_arn = lambda_function_arn
  @aws_region = config_loader.aws_credentials[:region]
  @account_id = config_loader.aws_credentials[:account_id]
  @lambda_client = Aws::Lambda::Client.new(
    region: @aws_region,
    access_key_id: config_loader.aws_credentials[:access_key_id],
    secret_access_key: config_loader.aws_credentials[:secret_access_key]
  )
end

Instance Attribute Details

#instance_id=(value) ⇒ Object (writeonly)

Sets the attribute instance_id

Parameters:

  • value

    the value to set the attribute instance_id to.



23
24
25
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 23

def instance_id=(value)
  @instance_id = value
end

#instance_name=(value) ⇒ Object (writeonly)

Sets the attribute instance_name

Parameters:

  • value

    the value to set the attribute instance_name to.



23
24
25
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 23

def instance_name=(value)
  @instance_name = value
end

Instance Method Details

#create_start_rule(cron_expression) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 25

def create_start_rule(cron_expression)
  rule_name = "StartInstanceRule-#{@instance_id}-#{cron_expression_hash(cron_expression)}"
  create_rule(
    rule_name,
    cron_expression,
    { instance_id: @instance_id, action: 'start' },
    'start'
  )
end

#create_stop_rule(cron_expression) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 35

def create_stop_rule(cron_expression)
  rule_name = "StopInstanceRule-#{@instance_id}-#{cron_expression_hash(cron_expression)}"
  create_rule(
    rule_name,
    cron_expression,
    { instance_id: @instance_id, action: 'stop' },
    'stop'
  )
end

#get_instance_id_from_rule(rule_name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 100

def get_instance_id_from_rule(rule_name)
  response = @events_client.list_targets_by_rule({ rule: rule_name })
  return nil if response.targets.empty?

  target_input = response.targets[0].input
  parsed_input = JSON.parse(target_input)
  parsed_input['instance_id'] if parsed_input.key?('instance_id')
rescue Aws::CloudWatchEvents::Errors::ResourceNotFoundException => e
  puts "Error fetching targets for rule: #{rule_name} - #{e.message}"
  nil
end

#list_event_rules(options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 50

def list_event_rules(options)
  next_token = nil

  column_widths = {
    rule_name: 40,
    instance_id: 22,
    schedule: 30,
    state: 10,
    action: 10
  }

  print_header(column_widths)

  loop do
    response = @events_client.list_rules(next_token: next_token)

    response.rules.each do |rule|
      process_rule(rule, options, column_widths)
    end

    next_token = response.next_token
    break if next_token.nil?
  end

  print_footer(column_widths)
end

#remove_rule(rule_name) ⇒ Object



45
46
47
48
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 45

def remove_rule(rule_name)
  remove_rule_by_name(rule_name)
  remove_lambda_permission(rule_name)
end

#rule_exists?(rule_name) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 112

def rule_exists?(rule_name)
  begin
    response = @events_client.describe_rule({ name: rule_name })
    return true unless response.nil?
  rescue Aws::CloudWatchEvents::Errors::ResourceNotFoundException
    return false
  end
end

#update_rule_state(rule_name, state) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hibernate/cloud_watch_event_manager.rb', line 77

def update_rule_state(rule_name, state)
  state = state == 'enable' ? 'ENABLED' : 'DISABLED'

  rule_details = @events_client.describe_rule({ name: rule_name })

  params = {
    name: rule_name,
    state: state,
    description: rule_details.description
  }

  if rule_details.schedule_expression
    params[:schedule_expression] = rule_details.schedule_expression
  elsif rule_details.event_pattern
    params[:event_pattern] = rule_details.event_pattern
  else
    puts "No ScheduleExpression or EventPattern found for rule '#{rule_name}'."
    exit 1
  end

  @events_client.put_rule(params)
end