Class: ElasticWhenever::Task::Target

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

Defined Under Namespace

Classes: InvalidContainerException

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.



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

def initialize(option, cluster:, definition:, container:, commands:, rule:, role:)
  unless definition.containers.include?(container)
    raise InvalidContainerException.new("#{container} is invalid container. valid=#{definition.containers.join(",")}")
  end

  @cluster = cluster
  @definition = definition
  @container = container
  @commands = commands
  @rule = rule
  @role = role
  @assign_public_ip = option.assign_public_ip
  @launch_type = option.launch_type
  @platform_version = option.platform_version
  @security_groups = option.security_groups
  @subnets = option.subnets
  @client = Aws::CloudWatchEvents::Client.new(option.aws_config)
end

Instance Attribute Details

#assign_public_ipObject (readonly)

Returns the value of attribute assign_public_ip.



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

def assign_public_ip
  @assign_public_ip
end

#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

#launch_typeObject (readonly)

Returns the value of attribute launch_type.



9
10
11
# File 'lib/elastic_whenever/task/target.rb', line 9

def launch_type
  @launch_type
end

#platform_versionObject (readonly)

Returns the value of attribute platform_version.



10
11
12
# File 'lib/elastic_whenever/task/target.rb', line 10

def platform_version
  @platform_version
end

#security_groupsObject (readonly)

Returns the value of attribute security_groups.



11
12
13
# File 'lib/elastic_whenever/task/target.rb', line 11

def security_groups
  @security_groups
end

#subnetsObject (readonly)

Returns the value of attribute subnets.



12
13
14
# File 'lib/elastic_whenever/task/target.rb', line 12

def subnets
  @subnets
end

Class Method Details

.fetch(option, rule) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elastic_whenever/task/target.rb', line 16

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/elastic_whenever/task/target.rb', line 53

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: ecs_parameters,
      }
    ]
  )
end