ecs_oneshot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0cfac0339722cc6435602b20ce69e5e9f5a1430e92b2d4f4e6d48a8fd3bb6f4
4
+ data.tar.gz: ae42c0b13212d467f86435c130fcd1c0a20a19b2975ee712cbcad9e813b53db0
5
+ SHA512:
6
+ metadata.gz: 44a8facfad2c7c490e93eee9fa445287bd6544ab4ad4db12267b0f5c22170efa765aaeb2f596cab8124faf5497e743f724825b3d8f61b985a050405d17eefac1
7
+ data.tar.gz: 53c135308e463e910ed298e8a5370af6ecd34cde721db1bc7b1539004c36d4df9d90c7003f66d322f9b2deb76330e4d8d39d99acd6206a71ef47e22a460ba8fe
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.1
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.0.rc.1
17
+ bundle install
18
+ bundle exec rake
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .ecs_oneshot.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.5
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ Metrics/BlockLength:
20
+ Exclude:
21
+ - 'spec/**/*.rb'
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at sinsoku.listy@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ecs_oneshot.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 0.80"
11
+ gem "simplecov"
12
+ gem "webmock"
@@ -0,0 +1,91 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ecs_oneshot (0.1.0)
5
+ aws-sdk-cloudwatchlogs
6
+ aws-sdk-ecs
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ ast (2.4.1)
14
+ aws-eventstream (1.1.0)
15
+ aws-partitions (1.357.0)
16
+ aws-sdk-cloudwatchlogs (1.34.0)
17
+ aws-sdk-core (~> 3, >= 3.99.0)
18
+ aws-sigv4 (~> 1.1)
19
+ aws-sdk-core (3.104.3)
20
+ aws-eventstream (~> 1, >= 1.0.2)
21
+ aws-partitions (~> 1, >= 1.239.0)
22
+ aws-sigv4 (~> 1.1)
23
+ jmespath (~> 1.0)
24
+ aws-sdk-ecs (1.67.0)
25
+ aws-sdk-core (~> 3, >= 3.99.0)
26
+ aws-sigv4 (~> 1.1)
27
+ aws-sigv4 (1.2.2)
28
+ aws-eventstream (~> 1, >= 1.0.2)
29
+ crack (0.4.3)
30
+ safe_yaml (~> 1.0.0)
31
+ diff-lcs (1.4.4)
32
+ docile (1.3.2)
33
+ hashdiff (1.0.1)
34
+ jmespath (1.4.0)
35
+ parallel (1.19.2)
36
+ parser (2.7.1.4)
37
+ ast (~> 2.4.1)
38
+ public_suffix (4.0.5)
39
+ rainbow (3.0.0)
40
+ rake (13.0.1)
41
+ regexp_parser (1.7.1)
42
+ rexml (3.2.4)
43
+ rspec (3.9.0)
44
+ rspec-core (~> 3.9.0)
45
+ rspec-expectations (~> 3.9.0)
46
+ rspec-mocks (~> 3.9.0)
47
+ rspec-core (3.9.2)
48
+ rspec-support (~> 3.9.3)
49
+ rspec-expectations (3.9.2)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.9.0)
52
+ rspec-mocks (3.9.1)
53
+ diff-lcs (>= 1.2.0, < 2.0)
54
+ rspec-support (~> 3.9.0)
55
+ rspec-support (3.9.3)
56
+ rubocop (0.89.1)
57
+ parallel (~> 1.10)
58
+ parser (>= 2.7.1.1)
59
+ rainbow (>= 2.2.2, < 4.0)
60
+ regexp_parser (>= 1.7)
61
+ rexml
62
+ rubocop-ast (>= 0.3.0, < 1.0)
63
+ ruby-progressbar (~> 1.7)
64
+ unicode-display_width (>= 1.4.0, < 2.0)
65
+ rubocop-ast (0.3.0)
66
+ parser (>= 2.7.1.4)
67
+ ruby-progressbar (1.10.1)
68
+ safe_yaml (1.0.5)
69
+ simplecov (0.19.0)
70
+ docile (~> 1.1)
71
+ simplecov-html (~> 0.11)
72
+ simplecov-html (0.12.2)
73
+ unicode-display_width (1.7.0)
74
+ webmock (3.8.3)
75
+ addressable (>= 2.3.6)
76
+ crack (>= 0.3.2)
77
+ hashdiff (>= 0.4.0, < 2.0.0)
78
+
79
+ PLATFORMS
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ ecs_oneshot!
84
+ rake (~> 13.0)
85
+ rspec (~> 3.0)
86
+ rubocop (~> 0.80)
87
+ simplecov
88
+ webmock
89
+
90
+ BUNDLED WITH
91
+ 2.2.0.rc.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Takumi Shotoku
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # EcsOneshot
2
+
3
+ ![Ruby](https://github.com/sinsoku/ecs_oneshot/workflows/Ruby/badge.svg)
4
+
5
+ A CLI tool that simply executes tasks on AWS Fargate.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ecs_oneshot'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ecs_oneshot
22
+
23
+ ## Usage
24
+
25
+ ### Quickstart
26
+
27
+ Run any command with the same configurations as your existing ECS service.
28
+
29
+ ```console
30
+ $ ecs_oneshot --cluster <mycluster> --service <myservice> --container <mycontainer> -- bin/rails -T
31
+ === Wait for Task Starting...
32
+ === Following Logs...
33
+ rails about # List versions of all Rails frameworks and the environment
34
+ rails app:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
35
+ rails app:update # Update configs and some other initially generated files (or use just update:configs or update:bin)
36
+ (...)
37
+
38
+ === Task Stopped.
39
+ ```
40
+
41
+ ### Configuration file
42
+
43
+ If the configuration file exists, it will be loaded.
44
+
45
+ ```yaml
46
+ # .ecs_oneshot.yml
47
+ ---
48
+ production:
49
+ cluster: mycluster
50
+ service: myservice
51
+ container: mycontainer
52
+ ```
53
+
54
+ You can simply execute the command by omitting the arguments.
55
+
56
+ ```console
57
+ $ ecs_oneshot -- echo "hello"
58
+ ```
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sinsoku/ecs_oneshot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sinsoku/ecs_oneshot/blob/master/CODE_OF_CONDUCT.md).
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
74
+
75
+ ## Code of Conduct
76
+
77
+ Everyone interacting in the EcsOneshot project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sinsoku/ecs_oneshot/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ #!/usr/bin/env ruby
4
+
5
+ require "bundler/setup"
6
+ require "ecs_oneshot"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ecs_oneshot/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ecs_oneshot"
7
+ spec.version = EcsOneshot::VERSION
8
+ spec.authors = ["Takumi Shotoku"]
9
+ spec.email = ["sinsoku.listy@gmail.com"]
10
+
11
+ spec.summary = "A CLI tool that simply executes tasks on AWS Fargate."
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/sinsoku/ecs_oneshot"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "aws-sdk-cloudwatchlogs"
27
+ spec.add_runtime_dependency "aws-sdk-ecs"
28
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __dir__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require "ecs_oneshot"
8
+ cli = EcsOneshot::CLI.new
9
+ cli.run
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ecs_oneshot/cli"
4
+ require "ecs_oneshot/config"
5
+ require "ecs_oneshot/task"
6
+ require "ecs_oneshot/version"
7
+
8
+ module EcsOneshot
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require "aws-sdk-ecs"
5
+ require "aws-sdk-cloudwatchlogs"
6
+
7
+ module EcsOneshot
8
+ class CLI
9
+ def run(args = ARGV)
10
+ options = parse_options(args)
11
+
12
+ if options[:init]
13
+ init_config(options)
14
+ else
15
+ run_task(options)
16
+ end
17
+ rescue Error => e
18
+ warn e.message
19
+ exit 1
20
+ end
21
+
22
+ private
23
+
24
+ def load_config(options)
25
+ path = options[:config]
26
+ env = options[:environment]
27
+ config = File.exist?(path) ? Config.load(path, env) : Config.new
28
+
29
+ other = Config.safe_build(options)
30
+ config.merge(other)
31
+ end
32
+
33
+ def run_task(options)
34
+ config = load_config(options)
35
+ raise Error, "<command> is required." if config.command.empty?
36
+
37
+ t = Task.new(config)
38
+ t.run
39
+ puts "=== Wait for Task Starting..."
40
+ t.wait_running
41
+ puts "=== Following Logs..."
42
+ t.each_log { |log| puts(log) }
43
+ puts "\n=== Task Stopped."
44
+ end
45
+
46
+ def init_config(options)
47
+ path = options[:config]
48
+ env = options[:environment]
49
+
50
+ Config.safe_build(options).save(path, env)
51
+ end
52
+
53
+ def parse_options(args) # rubocop:disable Metrics/MethodLength
54
+ opts = OptionParser.new
55
+ opts.banner = "Usage: ecs_oneshot [options] -- <commmand>"
56
+ opts.version = VERSION
57
+
58
+ opts.on("-c", "--config FILE", "Specify configuration file. (default: .ecs_oneshot.yml)")
59
+ opts.on("-e", "--environment ENVIRONMENT", "Specify environment. (default: production)")
60
+ opts.on("--cluster CLUSTER")
61
+ opts.on("--service SERVICE")
62
+ opts.on("--container CONTAINER")
63
+ opts.on("--init", "Generate a configuration file.")
64
+
65
+ {}.tap do |h|
66
+ h[:command] = opts.parse(args, into: h)
67
+ h[:config] ||= ".ecs_oneshot.yml"
68
+ h[:environment] ||= "production"
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module EcsOneshot
6
+ Config = Struct.new(:cluster, :service, :container, :command, keyword_init: true)
7
+
8
+ class Config
9
+ class << self
10
+ def safe_build(attributes)
11
+ safe_attributes = attributes.slice(*members)
12
+ new(**safe_attributes)
13
+ end
14
+
15
+ def load(path, env)
16
+ raise Error, "'#{path}' file not found." unless File.exist?(path)
17
+
18
+ yaml = YAML.load_file(path)
19
+ raise Error, "'#{env}' env not found." unless yaml.key?(env)
20
+
21
+ options = yaml[env]
22
+ new(**options)
23
+ end
24
+ end
25
+
26
+ def save(path, env)
27
+ raise Error, "already exists at '#{path}'." if File.exist?(path)
28
+
29
+ File.open(path, "w") do |f|
30
+ YAML.dump({ env => to_h.transform_keys(&:to_s) }, f)
31
+ end
32
+ end
33
+
34
+ def merge(other)
35
+ new_options = to_h.merge(other.to_h.compact)
36
+ Config.new(**new_options)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "aws-sdk-cloudwatchlogs"
4
+ require "aws-sdk-ecs"
5
+
6
+ module EcsOneshot
7
+ class Task
8
+ # Number of seconds between API calls to get CloudWatch Logs
9
+ WAIT_TIME = 10
10
+
11
+ def initialize(config)
12
+ @config = config
13
+ @ecs = Aws::ECS::Client.new
14
+ @logs = Aws::CloudWatchLogs::Client.new
15
+ end
16
+
17
+ def run
18
+ resp = run_task
19
+ @arn = resp.tasks[0].task_arn
20
+ @id = @arn.split("/").last
21
+ end
22
+
23
+ def wait_running
24
+ ecs.wait_until(:tasks_running, cluster: config.cluster, tasks: [arn])
25
+ rescue Aws::Waiters::Errors::WaiterFailed
26
+ ecs.wait_until(:tasks_stopped, cluster: config.cluster, tasks: [arn])
27
+ end
28
+
29
+ def each_log(&block)
30
+ last_event = nil
31
+
32
+ loop do
33
+ start_time = last_event.timestamp + 1 if last_event
34
+ events = get_log_events(start_time: start_time)
35
+ break if last_event && events.empty?
36
+
37
+ events.each { |event| block.call(event.message) }
38
+ last_event = events.last
39
+
40
+ sleep(WAIT_TIME)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :id, :arn, :config, :ecs, :logs
47
+
48
+ def log_options
49
+ @log_options ||= task_definition.container_definitions
50
+ .find { |c| c.name == config.container }
51
+ .log_configuration.options
52
+ end
53
+
54
+ def get_log_events(start_time:)
55
+ awslogs_group = log_options["awslogs-group"]
56
+ awslogs_stream_prefix = log_options["awslogs-stream-prefix"]
57
+
58
+ resp = logs.get_log_events(
59
+ log_group_name: awslogs_group,
60
+ log_stream_name: "#{awslogs_stream_prefix}/#{config.container}/#{id}",
61
+ start_time: start_time
62
+ )
63
+ resp.events
64
+ end
65
+
66
+ def task_definition
67
+ return @task_definition if @task_definition
68
+
69
+ # NOTE: Delete a version to use latest task definition
70
+ task_definition = service.task_definition.sub(/:\d+$/, "")
71
+ @task_definition = ecs.describe_task_definition(task_definition: task_definition)
72
+ .task_definition
73
+ end
74
+
75
+ def service
76
+ @service ||= ecs.describe_services(cluster: config.cluster, services: [config.service])
77
+ .services[0]
78
+ end
79
+
80
+ def run_task
81
+ awsvpc = service.network_configuration.awsvpc_configuration
82
+ ecs.run_task(
83
+ cluster: config.cluster,
84
+ launch_type: "FARGATE",
85
+ network_configuration: {
86
+ awsvpc_configuration: { subnets: awsvpc.subnets, security_groups: awsvpc.security_groups }
87
+ },
88
+ overrides: { container_overrides: [{ name: config.container, command: config.command }] },
89
+ task_definition: task_definition.task_definition_arn
90
+ )
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EcsOneshot
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ecs_oneshot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takumi Shotoku
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-cloudwatchlogs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-ecs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A CLI tool that simply executes tasks on AWS Fargate.
42
+ email:
43
+ - sinsoku.listy@gmail.com
44
+ executables:
45
+ - ecs_oneshot
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".github/workflows/main.yml"
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - CODE_OF_CONDUCT.md
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - bin/console
60
+ - bin/setup
61
+ - ecs_oneshot.gemspec
62
+ - exe/ecs_oneshot
63
+ - lib/ecs_oneshot.rb
64
+ - lib/ecs_oneshot/cli.rb
65
+ - lib/ecs_oneshot/config.rb
66
+ - lib/ecs_oneshot/task.rb
67
+ - lib/ecs_oneshot/version.rb
68
+ homepage: https://github.com/sinsoku/ecs_oneshot
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.3.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.1.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A CLI tool that simply executes tasks on AWS Fargate.
91
+ test_files: []