From 870bd1331eb2c4eeac4797a1f42c7456c84ce6a0 Mon Sep 17 00:00:00 2001 From: Aaron Vinson Date: Mon, 1 Oct 2018 16:28:01 -0700 Subject: [PATCH 1/4] update elastic_whenever for FARGATE launch type --- lib/elastic_whenever/option.rb | 20 +++++++++ lib/elastic_whenever/task/target.rb | 63 ++++++++++++++++++++++------- lib/elastic_whenever/version.rb | 2 +- 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/lib/elastic_whenever/option.rb b/lib/elastic_whenever/option.rb index 21bf159..49eb8d2 100644 --- a/lib/elastic_whenever/option.rb +++ b/lib/elastic_whenever/option.rb @@ -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 @@ -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 @@ -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 opts.on('-f', '--file schedule_file', 'Default: config/schedule.rb') do |file| @schedule_file = file end diff --git a/lib/elastic_whenever/task/target.rb b/lib/elastic_whenever/task/target.rb index 8e8962b..5b5e84f 100644 --- a/lib/elastic_whenever/task/target.rb +++ b/lib/elastic_whenever/task/target.rb @@ -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 @@ -37,25 +41,56 @@ 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, + } + } + } } - } - ] - ) + ] + ) + 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: { + launch_type: launch_type, + task_definition_arn: definition.arn, + task_count: 1, + } + } + ] + ) + end end private diff --git a/lib/elastic_whenever/version.rb b/lib/elastic_whenever/version.rb index 85be545..dcea942 100644 --- a/lib/elastic_whenever/version.rb +++ b/lib/elastic_whenever/version.rb @@ -1,3 +1,3 @@ module ElasticWhenever - VERSION = "0.3.2" + VERSION = "0.3.3" end From fd9342aaffc0932277460989b94322fc18a9aacb Mon Sep 17 00:00:00 2001 From: Aaron Vinson Date: Mon, 1 Oct 2018 16:46:41 -0700 Subject: [PATCH 2/4] don't pass launch_type for EC2 for tests --- lib/elastic_whenever/task/target.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/elastic_whenever/task/target.rb b/lib/elastic_whenever/task/target.rb index 5b5e84f..56a6661 100644 --- a/lib/elastic_whenever/task/target.rb +++ b/lib/elastic_whenever/task/target.rb @@ -83,7 +83,6 @@ def create input: input_json(container, commands), role_arn: role.arn, ecs_parameters: { - launch_type: launch_type, task_definition_arn: definition.arn, task_count: 1, } From 945ac653aaa1f5793767c2a15c2d136949ef12e3 Mon Sep 17 00:00:00 2001 From: Aaron Vinson Date: Wed, 21 Nov 2018 13:43:37 -0800 Subject: [PATCH 3/4] pull in changes from master, add platform option --- lib/elastic_whenever/cli.rb | 2 +- lib/elastic_whenever/option.rb | 10 ++++++++++ lib/elastic_whenever/schedule.rb | 5 +++-- lib/elastic_whenever/task.rb | 5 +++-- lib/elastic_whenever/task/target.rb | 10 +++++++--- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/elastic_whenever/cli.rb b/lib/elastic_whenever/cli.rb index 87c5751..873e1c2 100644 --- a/lib/elastic_whenever/cli.rb +++ b/lib/elastic_whenever/cli.rb @@ -46,7 +46,7 @@ def run(args) private def update_tasks(option, dry_run:) - schedule = Schedule.new(option.schedule_file, option.variables) + schedule = Schedule.new(option.schedule_file, option.verbose, option.variables) schedule.validate! cluster = Task::Cluster.new(option, schedule.cluster) diff --git a/lib/elastic_whenever/option.rb b/lib/elastic_whenever/option.rb index 49eb8d2..e0b7d39 100644 --- a/lib/elastic_whenever/option.rb +++ b/lib/elastic_whenever/option.rb @@ -8,9 +8,11 @@ class Option attr_reader :identifier attr_reader :mode + attr_reader :verbose attr_reader :variables attr_reader :assign_public_ip attr_reader :launch_type + attr_reader :platform_version attr_reader :security_groups attr_reader :subnets attr_reader :schedule_file @@ -20,9 +22,11 @@ class InvalidOptionException < StandardError; end def initialize(args) @identifier = nil @mode = DRYRUN_MODE + @verbose = false @variables = [] @assign_public_ip = 'DISABLED' @launch_type = 'EC2' + @platform_version = 'LATEST' @security_groups = nil @subnets = nil @schedule_file = 'config/schedule.rb' @@ -67,6 +71,9 @@ def initialize(args) opts.on('--subnets subnets', "Example: --subnets 'subnet-4973d63f,subnet-45827d1d'") do |subnets| @subnets = subnets end + opts.on('--platform_version version', "For Fargate launch type, optionally specify the platform version. Example: --platform_version 1.2.0") do |version| + @platform_version = version + end opts.on('-f', '--file schedule_file', 'Default: config/schedule.rb') do |file| @schedule_file = file end @@ -85,6 +92,9 @@ def initialize(args) opts.on('-v', '--version', 'Print version') do @mode = PRINT_VERSION_MODE end + opts.on('-V', '--verbose', 'Run rake jobs without --silent') do + @verbose = true + end end.parse(args) @credentials = if profile diff --git a/lib/elastic_whenever/schedule.rb b/lib/elastic_whenever/schedule.rb index 16c54a8..72d2b3a 100644 --- a/lib/elastic_whenever/schedule.rb +++ b/lib/elastic_whenever/schedule.rb @@ -51,8 +51,9 @@ def years end using WheneverNumeric - def initialize(file, variables) + def initialize(file, verbose, variables) @environment = "production" + @verbose = verbose @tasks = [] @cluster = nil @task_definition = nil @@ -69,7 +70,7 @@ def set(key, value) end def every(frequency, options = {}, &block) - @tasks << Task.new(@environment, @bundle_command, schedule_expression(frequency, options)).tap do |task| + @tasks << Task.new(@environment, @verbose, @bundle_command, schedule_expression(frequency, options)).tap do |task| task.instance_eval(&block) end rescue UnsupportedFrequencyException => exn diff --git a/lib/elastic_whenever/task.rb b/lib/elastic_whenever/task.rb index cb0b3d2..71a043e 100644 --- a/lib/elastic_whenever/task.rb +++ b/lib/elastic_whenever/task.rb @@ -3,8 +3,9 @@ class Task attr_reader :commands attr_reader :expression - def initialize(environment, bundle_command, expression) + def initialize(environment, verbose, bundle_command, expression) @environment = environment + @verbose_mode = verbose ? '' : "--silent" @bundle_command = bundle_command.split(" ") @expression = expression @commands = [] @@ -15,7 +16,7 @@ def command(task) end def rake(task) - @commands << [@bundle_command, "rake", task, "--silent"].flatten + @commands << [@bundle_command, "rake", task, @verbose_mode].flatten end def runner(src) diff --git a/lib/elastic_whenever/task/target.rb b/lib/elastic_whenever/task/target.rb index 56a6661..102fcfa 100644 --- a/lib/elastic_whenever/task/target.rb +++ b/lib/elastic_whenever/task/target.rb @@ -7,6 +7,7 @@ class Target attr_reader :commands attr_reader :assign_public_ip attr_reader :launch_type + attr_reader :platform_version attr_reader :security_groups attr_reader :subnets @@ -43,6 +44,7 @@ def initialize(option, cluster:, definition:, container:, commands:, rule:, role @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) @@ -64,11 +66,12 @@ def create task_count: 1, network_configuration: { awsvpc_configuration: { - subnets: [ subnets ], - security_groups: [ security_groups ], + subnets: subnets.split(','), + security_groups: security_groups.split(','), assign_public_ip: assign_public_ip, } - } + }, + platform_version: platform_version } } ] @@ -83,6 +86,7 @@ def create input: input_json(container, commands), role_arn: role.arn, ecs_parameters: { + launch_type: launch_type, task_definition_arn: definition.arn, task_count: 1, } From 3c529d76cec2a892b30b0582a758d2fdd54b27d3 Mon Sep 17 00:00:00 2001 From: wata_mac Date: Sun, 16 Dec 2018 21:09:25 +0900 Subject: [PATCH 4/4] Fix spec --- spec/cli_spec.rb | 6 +++--- spec/schedule_spec.rb | 6 +++--- spec/task_spec.rb | 8 ++++---- spec/tasks/rule_spec.rb | 2 +- spec/tasks/target_spec.rb | 1 + 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index f53a141..b7ed48a 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -3,7 +3,7 @@ RSpec.describe ElasticWhenever::CLI do describe "run" do let(:task) do - ElasticWhenever::Task.new("production", "bundle exec", "cron(0 0 * * ? *)").tap do |task| + ElasticWhenever::Task.new("production", false, "bundle exec", "cron(0 0 * * ? *)").tap do |task| task.runner("Hoge.run") end end @@ -21,7 +21,7 @@ let(:definition) { double(arn: "arn:aws:ecs:us-east-1:123456789:task-definition/wordpress:2", name: "wordpress:2", containers: ["testContainer"]) } let(:role) { double(arn: "arn:aws:ecs:us-east-1:123456789:role/testRole") } before do - allow(ElasticWhenever::Schedule).to receive(:new).with((Pathname(__dir__) + "fixtures/schedule.rb").to_s, kind_of(Array)).and_return(schedule) + allow(ElasticWhenever::Schedule).to receive(:new).with((Pathname(__dir__) + "fixtures/schedule.rb").to_s, boolean, kind_of(Array)).and_return(schedule) allow(ElasticWhenever::Task::Cluster).to receive(:new).with(kind_of(ElasticWhenever::Option), "test").and_return(cluster) allow(ElasticWhenever::Task::Definition).to receive(:new).with(kind_of(ElasticWhenever::Option), "wordpress:2").and_return(definition) allow(ElasticWhenever::Task::Role).to receive(:new).with(kind_of(ElasticWhenever::Option)).and_return(role) @@ -77,7 +77,7 @@ end it "receives schedule file name and variables" do - expect(ElasticWhenever::Schedule).to receive(:new).with((Pathname(__dir__) + "fixtures/schedule.rb").to_s, [{ key: "environment", value: "staging" }, { key: "cluster", value: "ecs-test" }]) + expect(ElasticWhenever::Schedule).to receive(:new).with((Pathname(__dir__) + "fixtures/schedule.rb").to_s, boolean, [{ key: "environment", value: "staging" }, { key: "cluster", value: "ecs-test" }]) ElasticWhenever::CLI.run(%W(-i test --set environment=staging&cluster=ecs-test --region us-east-1 -f #{(Pathname(__dir__) + "fixtures/schedule.rb").to_s})) end diff --git a/spec/schedule_spec.rb b/spec/schedule_spec.rb index cb9b354..206a798 100644 --- a/spec/schedule_spec.rb +++ b/spec/schedule_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" RSpec.describe ElasticWhenever::Schedule do - let(:schedule) { ElasticWhenever::Schedule.new((Pathname(__dir__) + "fixtures/schedule.rb").to_s, []) } + let(:schedule) { ElasticWhenever::Schedule.new((Pathname(__dir__) + "fixtures/schedule.rb").to_s, false, []) } describe "#initialize" do it "has attributes" do @@ -14,7 +14,7 @@ end context "when received variables from cli" do - let(:schedule) { ElasticWhenever::Schedule.new((Pathname(__dir__) + "fixtures/schedule.rb").to_s, [{ key: "environment", value: "staging" }]) } + let(:schedule) { ElasticWhenever::Schedule.new((Pathname(__dir__) + "fixtures/schedule.rb").to_s, false, [{ key: "environment", value: "staging" }]) } it "overrides attributes" do expect(schedule.instance_variable_get(:@environment)).to eq "staging" @@ -39,7 +39,7 @@ end context "when use unsupported method" do - let(:schedule) { ElasticWhenever::Schedule.new((Pathname(__dir__) + "fixtures/unsupported_schedule.rb").to_s, []) } + let(:schedule) { ElasticWhenever::Schedule.new((Pathname(__dir__) + "fixtures/unsupported_schedule.rb").to_s, false, []) } it "does not have tasks" do expect(schedule.tasks.count).to eq(0) diff --git a/spec/task_spec.rb b/spec/task_spec.rb index b8c2693..65e2c34 100644 --- a/spec/task_spec.rb +++ b/spec/task_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" RSpec.describe ElasticWhenever::Task do - let(:task) { ElasticWhenever::Task.new("production", "bundle exec", "cron(0 17 * * ? *)") } + let(:task) { ElasticWhenever::Task.new("production", false, "bundle exec", "cron(0 17 * * ? *)") } describe "#initialize" do it "has attributes" do @@ -23,7 +23,7 @@ end context "when unset bundle command" do - let(:task) { ElasticWhenever::Task.new("production", "", "cron(0 17 * * ? *)") } + let(:task) { ElasticWhenever::Task.new("production", false, "", "cron(0 17 * * ? *)") } it "generates rake commands" do task.rake("hoge:run") @@ -39,7 +39,7 @@ end context "when unset bundle command" do - let(:task) { ElasticWhenever::Task.new("production", "", "cron(0 17 * * ? *)") } + let(:task) { ElasticWhenever::Task.new("production", false, "", "cron(0 17 * * ? *)") } it "generates rake commands" do task.runner("Hoge.run") @@ -55,7 +55,7 @@ end context "when unset bundle command" do - let(:task) { ElasticWhenever::Task.new("production", "", "cron(0 17 * * ? *)") } + let(:task) { ElasticWhenever::Task.new("production", false, "", "cron(0 17 * * ? *)") } it "generates rake commands" do task.script("runner.rb") diff --git a/spec/tasks/rule_spec.rb b/spec/tasks/rule_spec.rb index 3734c0b..6512036 100644 --- a/spec/tasks/rule_spec.rb +++ b/spec/tasks/rule_spec.rb @@ -19,7 +19,7 @@ describe "convert" do it "converts scheduled task syntax" do - task = ElasticWhenever::Task.new("production", "bundle exec", "cron(0 0 * * ? *)") + task = ElasticWhenever::Task.new("production", false, "bundle exec", "cron(0 0 * * ? *)") task.rake "hoge:run" expect(ElasticWhenever::Task::Rule.convert(option, task)).to have_attributes( diff --git a/spec/tasks/target_spec.rb b/spec/tasks/target_spec.rb index 15e5434..8952d23 100644 --- a/spec/tasks/target_spec.rb +++ b/spec/tasks/target_spec.rb @@ -82,6 +82,7 @@ }.to_json, role_arn: "arn:aws:ecs:us-east-1:123456789:role/testRole", ecs_parameters: { + launch_type: "EC2", task_definition_arn: "arn:aws:ecs:us-east-1:123456789:task-definition/wordpress:2", task_count: 1, }