Class: ElasticWhenever::Task::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_whenever/task/target.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, cluster:, definition:, container:, commands:, rule:, role:) ⇒ Target

Returns a new instance of Target.



27
28
29
30
31
32
33
34
35
# File 'lib/elastic_whenever/task/target.rb', line 27

def initialize(option, cluster:, definition:, container:, commands:, rule:, role:)
  @cluster = cluster
  @definition = definition
  @container = container
  @commands = commands
  @rule = rule
  @role = role
  @client = Aws::CloudWatchEvents::Client.new(option.aws_config)
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



4
5
6
# File 'lib/elastic_whenever/task/target.rb', line 4

def cluster
  @cluster
end

#commandsObject (readonly)

Returns the value of attribute commands.



7
8
9
# File 'lib/elastic_whenever/task/target.rb', line 7

def commands
  @commands
end

#containerObject (readonly)

Returns the value of attribute container.



6
7
8
# File 'lib/elastic_whenever/task/target.rb', line 6

def container
  @container
end

#definitionObject (readonly)

Returns the value of attribute definition.



5
6
7
# File 'lib/elastic_whenever/task/target.rb', line 5

def definition
  @definition
end

Class Method Details

.fetch(option, rule) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/elastic_whenever/task/target.rb', line 9

def self.fetch(option, rule)
  client = Aws::CloudWatchEvents::Client.new(option.aws_config)
  targets = client.list_targets_by_rule(rule: rule.name).targets
  targets.map do |target|
    input = JSON.parse(target.input, symbolize_names: true)

    self.new(
      option,
      cluster: Cluster.new(option, target.arn),
      definition: Definition.new(option, target.ecs_parameters.task_definition_arn),
      container: input[:containerOverrides].first[:name],
      commands: input[:containerOverrides].first[:command],
      rule: rule,
      role: Role.new(option)
    )
  end
end

Instance Method Details

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elastic_whenever/task/target.rb', line 37

def create
  client.put_targets(
    rule: rule.name,
    targets: [
      {
        id: Digest::SHA1.hexdigest(commands.join("-")),
        arn: cluster.arn,
        input: input_json(container, commands),
        role_arn: role.arn,
        ecs_parameters: {
          task_definition_arn: definition.arn,
          task_count: 1,
        }
      }
    ]
  )
end