Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/elastic_whenever/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class Option
attr_reader :identifier
attr_reader :mode
attr_reader :variables
attr_reader :assign_public_ip
attr_reader :launch_type
attr_reader :security_groups
attr_reader :subnets
attr_reader :schedule_file

class InvalidOptionException < StandardError; end
Expand All @@ -17,6 +21,10 @@ def initialize(args)
@identifier = nil
@mode = DRYRUN_MODE
@variables = []
@assign_public_ip = 'DISABLED'
@launch_type = 'EC2'
@security_groups = nil
@subnets = nil
@schedule_file = 'config/schedule.rb'
@profile = nil
@access_key = nil
Expand Down Expand Up @@ -47,6 +55,18 @@ def initialize(args)
@variables << { key: key, value: value }
end
end
opts.on('--assign_public_ip', 'Assign a public IP.') do
@assign_public_ip = 'ENABLED'
end
opts.on('--launch_type launch_type', 'Launch type. Defualt: EC2') do |launch_type|
@launch_type = launch_type
end
opts.on('--security_groups groups', "Example: --security_groups 'sg-2c503655,sg-72f0cb0a'") do |groups|
@security_groups = groups
end
opts.on('--subnets subnets', "Example: --subnets 'subnet-4973d63f,subnet-45827d1d'") do |subnets|
@subnets = subnets
end
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this option needs validation about launch type. For example, If security_groups or subnets are unspecified when launch_type is FARGATE, will they get an error?

Copy link
Copy Markdown
Contributor Author

@avinson avinson Oct 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wata727 I'm still thinking about this/looking into it. fyi, I did discover something kinda terrible about this AWS API in that it does not appear to validate either the subnets or the security groups. If you specify either a non-existent subnet or security group it will accept the event rule and then you'll get "invocation failures" in CloudWatch with no error logs or any indication of what's wrong.

Maybe it makes sense to do this validation in elastic_whenever? I presume that AWS will eventually add this on their end.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avinson Sorry for the late reply.

That's terrible... Perhaps I believe that validation is necessary for a better experience. Considering the behavior of the current AWS API, I think that it makes sense even if without the validation, but I'd like to add it as possible.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avinson @wata727 For now it would be best to add a TODO comment to keep it top of mind.

opts.on('-f', '--file schedule_file', 'Default: config/schedule.rb') do |file|
@schedule_file = file
end
Expand Down
62 changes: 48 additions & 14 deletions lib/elastic_whenever/task/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ class Target
attr_reader :definition
attr_reader :container
attr_reader :commands
attr_reader :assign_public_ip
attr_reader :launch_type
attr_reader :security_groups
attr_reader :subnets

class InvalidContainerException < StandardError; end

Expand Down Expand Up @@ -37,25 +41,55 @@ def initialize(option, cluster:, definition:, container:, commands:, rule:, role
@commands = commands
@rule = rule
@role = role
@assign_public_ip = option.assign_public_ip
@launch_type = option.launch_type
@security_groups = option.security_groups
@subnets = option.subnets
@client = Aws::CloudWatchEvents::Client.new(option.aws_config)
end

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,
if launch_type == 'FARGATE'
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: {
launch_type: launch_type,
task_definition_arn: definition.arn,
task_count: 1,
network_configuration: {
awsvpc_configuration: {
subnets: [ subnets ],
security_groups: [ security_groups ],
assign_public_ip: assign_public_ip,
}
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}
]
)
]
)
else
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
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/elastic_whenever/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ElasticWhenever
VERSION = "0.3.2"
VERSION = "0.3.3"
end