mikoshi 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
+ SHA1:
3
+ metadata.gz: eac75c4c47d39e6117c67963780dbc9f2d652a38
4
+ data.tar.gz: a33828b7f842b6ecb1ee2104bbe13d02e8b64aed
5
+ SHA512:
6
+ metadata.gz: 9be8095a252a4d1a4a62dd87598d5eb7ae5a02384c46f5626a06c56b90bcb041fa49616648d99372e27e0e6e9cebb2fb8c9d6b2bae14b0cc997351d5c992d080
7
+ data.tar.gz: ce52570e19cc1704add39a4bcf1927896bcb8575e6c0b1751608a613f5d79ca86af634781ce83f9cf35cc294525c22898de9fa28ba40a5d5c0e7b350805964c2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,8 @@
1
+ inherit_gem:
2
+ unasukecop:
3
+ - "config/rubocop.yml"
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+ Style/RegexpLiteral:
8
+ EnforcedStyle: percent_r
@@ -0,0 +1,10 @@
1
+ notifications:
2
+ email: false
3
+ sudo: false
4
+ language: ruby
5
+ rvm:
6
+ - 2.2.7
7
+ - 2.3.4
8
+ - 2.4.1
9
+ - ruby-head
10
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mikoshi.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Yusuke Nakamura
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,96 @@
1
+ # Mikoshi
2
+
3
+ This gem is tool to deploy ECS task definition and service with described by yaml documents.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mikoshi'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mikoshi
20
+
21
+ ## Usage
22
+ First, describe task definition to yaml.
23
+
24
+ ```yaml
25
+ # task_definitions/ping2googledns.yml.erb
26
+ family: "ping2googledns"
27
+ network_mode: "bridge"
28
+ container_definitions:
29
+ - name: "ping"
30
+ image: "unasuke/ping2googledns:latest"
31
+ cpu: 128
32
+ memory: 128
33
+ ```
34
+
35
+ ... and service too.
36
+
37
+ ```yaml
38
+ # services/ping2googledns.yml.erb
39
+ cluster: "default"
40
+ service: "ping2googledns"
41
+ task_definition: <%= "ping2googledns:#{ENV['TASK_REVISION']}" %>
42
+ desired_count: 1
43
+ ```
44
+
45
+ Then, invoke those commands.
46
+
47
+ ```shell
48
+ # update task_definition
49
+ $ mikoshi update_task ping2googledns --region ap-northeast-1
50
+ Update task definition: ping2googledns
51
+ Done update task definition: ping2googledns revison: 6
52
+
53
+ # update service
54
+ $ TASK_REVISION=3 mikoshi update_service ping2googledns
55
+ Update service : ping2googledns
56
+ Waiting for 10 sec...
57
+ Update service success
58
+
59
+ # update task_definition and service
60
+ $ mikoshi deploy -t ping2googledns -s ping2googledns
61
+ Update task definition: ping2googledns
62
+ Done update task definition: ping2googledns revison: 7
63
+ Update service : ping2googledns
64
+ Waiting for 10 sec...
65
+ Waiting for 10 sec...
66
+ Waiting for 10 sec...
67
+ Update service success
68
+
69
+ # show help
70
+ $ mikoshi help
71
+ Commands:
72
+ mikoshi deploy # Deploy task definition and service
73
+ mikoshi help [COMMAND] # Describe available commands or one specific command
74
+ mikoshi update_service SERVICE_NAME # Update service
75
+ mikoshi update_task TASK_NAME # Update task definition
76
+
77
+ Options:
78
+ [--region=REGION] # aws region
79
+
80
+ ```
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ 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).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/unasuke/mikoshi.
91
+
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
96
+
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rubocop/rake_task'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << 'test'
7
+ t.libs << 'lib'
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ RuboCop::RakeTask.new
12
+
13
+ task default: %i[test rubocop]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mikoshi'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
3
+ require 'mikoshi'
4
+ Mikoshi::Cli.start
@@ -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,9 @@
1
+ require 'mikoshi/version'
2
+ require 'mikoshi/cli'
3
+ require 'mikoshi/plan'
4
+ require 'mikoshi/plan/task_definition'
5
+ require 'mikoshi/plan/service'
6
+
7
+ module Mikoshi
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,74 @@
1
+ require 'thor'
2
+ require 'aws-sdk'
3
+ require 'timeout'
4
+ require 'mikoshi/plan'
5
+ require 'mikoshi/plan/task_definition'
6
+ require 'mikoshi/plan/service'
7
+
8
+ module Mikoshi
9
+ class Cli < Thor
10
+ TASK_DEFINITION_PATH = 'task_definitions'.freeze
11
+ SERVICE_PATH = 'services'.freeze
12
+ PLAN_EXT = '.yml.erb'.freeze
13
+ FETCH_INTERVAL = 10
14
+ DEPLOY_TIMEOUT = 300
15
+
16
+ class_option :region, type: :string, desc: 'aws region'
17
+
18
+ desc 'update_task TASK_NAME', 'Update task definition'
19
+ def update_task_definition(task_name)
20
+ task = ::Mikoshi::Plan::TaskDefinition.new(
21
+ yaml_path: File.join(TASK_DEFINITION_PATH, task_name + PLAN_EXT),
22
+ client: aws_client,
23
+ )
24
+ puts "Update task definition: #{task_name}"
25
+ task.register_task_definition
26
+ puts "Done update task definition: #{task_name} revison: #{ENV['TASK_REVISION']}"
27
+ end
28
+
29
+ desc 'update_service SERVICE_NAME', 'Update service'
30
+ def update_service(service_name)
31
+ service = ::Mikoshi::Plan::Service.new(
32
+ yaml_path: File.join(SERVICE_PATH, service_name + PLAN_EXT),
33
+ client: aws_client,
34
+ )
35
+ puts "Update service : #{service_name}"
36
+ service.deploy_service
37
+ begin
38
+ Timeout.timeout(DEPLOY_TIMEOUT) do
39
+ loop do
40
+ puts "Waiting for #{FETCH_INTERVAL} sec..."
41
+ sleep FETCH_INTERVAL
42
+ break if service.deployed?
43
+ end
44
+ end
45
+ rescue Timeout::Error
46
+ puts "Update failed by timeout(#{DEPLOY_TIMEOUT} sec)"
47
+ exit(false)
48
+ end
49
+
50
+ puts "Done update service #{service_name}"
51
+ end
52
+
53
+ desc 'deploy', 'Deploy task definition and service'
54
+ method_option :task_definition, type: :string, desc: 'task_definition name', aliases: '-t'
55
+ method_option :service, type: :string, desc: 'service name', aliases: '-s'
56
+ def deploy
57
+ update_task_definition(options[:task_definition]) unless options[:task_definition].nil?
58
+ update_service(options[:service]) unless options[:service].nil?
59
+ end
60
+
61
+ no_tasks do
62
+ def aws_client
63
+ opt =
64
+ if options[:region].nil?
65
+ {}
66
+ else
67
+ { region: options[:region] }
68
+ end
69
+
70
+ Aws::ECS::Client.new(opt)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+ require 'erb'
3
+ require 'yaml'
4
+
5
+ module Mikoshi
6
+ class Plan
7
+ class Base
8
+ attr_reader :data, :client
9
+
10
+ def initialize(yaml_path: nil, client: nil)
11
+ raise ArgumentError, 'Yaml file path is required.' if yaml_path.nil?
12
+ raise ArgumentError, 'client is required.' if client.nil?
13
+
14
+ @data = YAML.safe_load(ERB.new(File.new(yaml_path).read).result).symbolize_keys
15
+ @client = client
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ require 'active_support/core_ext/hash/except'
2
+ require 'mikoshi/plan'
3
+
4
+ module Mikoshi
5
+ class Plan
6
+ class Service < Base
7
+ TASK_DEFINITION_WITH_REVISION = %r{\A\S+:\d+\z}
8
+
9
+ def initialize(yaml_path: nil, client: nil)
10
+ super
11
+
12
+ if @data[:task_definition].match(TASK_DEFINITION_WITH_REVISION).nil?
13
+ raise ArgumentError, 'task_definition should have revison by numerically.'
14
+ end
15
+
16
+ @data.store :service_name, @data[:service]
17
+ end
18
+
19
+ def create_service
20
+ @client.create_service(@data.except(:service))
21
+ end
22
+
23
+ def update_service
24
+ @client.update_service(@data.except(:service_name))
25
+ end
26
+
27
+ def deploy_service
28
+ resp = @client.describe_services(cluster: @data[:cluster], services: [@data[:service]])
29
+ if resp.services.first.status == 'ACTIVE'
30
+ update_service
31
+ else
32
+ create_service
33
+ end
34
+ end
35
+
36
+ def deployed?
37
+ resp = @client.describe_services(cluster: @data[:cluster], services: [@data[:service]])
38
+ deployment = resp.services.first.deployments.find do |d|
39
+ d.task_definition.end_with?(@data[:task_definition])
40
+ end
41
+
42
+ if deployment.running_count == @data[:desired_count]
43
+ true
44
+ else
45
+ false
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,17 @@
1
+ require 'mikoshi/plan'
2
+
3
+ module Mikoshi
4
+ class Plan
5
+ class TaskDefinition < Base
6
+ def initialize(yaml_path: nil, client: nil)
7
+ super
8
+ end
9
+
10
+ def register_task_definition
11
+ resp = @client.register_task_definition(@data)
12
+ ENV['TASK_REVISION'] = resp.task_definition.revision.to_s
13
+ resp
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Mikoshi
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'mikoshi/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'mikoshi'
9
+ spec.version = Mikoshi::VERSION
10
+ spec.authors = ['Yusuke Nakamura']
11
+ spec.email = ['yusuke1994525@gmail.com']
12
+
13
+ spec.summary = 'deploy and manage aws ecs'
14
+ spec.description = 'Mikoshi is a tool that deploy to ECS and manage task definition and cluster.'
15
+ spec.homepage = 'https://github.com/unasuke/mikoshi'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport', '~> 5'
26
+ spec.add_dependency 'aws-sdk', '~> 2'
27
+ spec.add_dependency 'thor'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.14'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'minitest', '~> 5.0'
32
+ spec.add_development_dependency 'unasukecop'
33
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mikoshi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Nakamura
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: unasukecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Mikoshi is a tool that deploy to ECS and manage task definition and cluster.
112
+ email:
113
+ - yusuke1994525@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/mikoshi
127
+ - bin/setup
128
+ - lib/mikoshi.rb
129
+ - lib/mikoshi/cli.rb
130
+ - lib/mikoshi/plan.rb
131
+ - lib/mikoshi/plan/service.rb
132
+ - lib/mikoshi/plan/task_definition.rb
133
+ - lib/mikoshi/version.rb
134
+ - mikoshi.gemspec
135
+ homepage: https://github.com/unasuke/mikoshi
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.5.2
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: deploy and manage aws ecs
159
+ test_files: []