hako-switch-hitter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 244a32a5f2ef24f9cf9732eabc121d997f51a38f
4
+ data.tar.gz: 3a739ccc5a2413a35fe21bdba2ad71d993da6ba4
5
+ SHA512:
6
+ metadata.gz: 87129974e6e7202a672d1ccdb354ac61dd3e005f87e59089c80289dbfc4adcd68092379f24ca6345035e15b8426b1129c6a51683457ef47a880d1f22dd6e797b
7
+ data.tar.gz: 0a72671ce0b48c4f39d0f664a546cff0190c208bff53fec588cd745e4b9bd39474efa06c6cdcfef3bcce19337c826835f9ffb44686d1e3338967d6cb9eef0547
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ - 2.4.0
6
+ - ruby-head
7
+ before_install: gem install bundler -v 1.14.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hako-switch-hitter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 shinya-watanabe
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.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Hako::Switch::Hitter
2
+
3
+ Hit endpoint at [hako](https://github.com/eagletmt/hako) script deploy_finished.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hako-switch-hitter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hako-switch-hitter
20
+
21
+ ## Usage
22
+
23
+ ```yaml
24
+ scripts:
25
+ - type: switch_hitter
26
+ endpoint:
27
+ proto: http
28
+ host: example.com
29
+ port: 10080
30
+ path: /switch_endpoint
31
+ ```
32
+
33
+ config | detail | required
34
+ --- | --- | ---
35
+ proto | http or https | false
36
+ host | example.com | false
37
+ port | 80 | false
38
+ path | /switch_endpoint | true
39
+
40
+ If `proto`, `host`, `port` is not configured, hako-switch-hitter will use ELB v2 address.
41
+
42
+ ## Development
43
+
44
+ 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.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wata-gh/hako-switch-hitter.
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hako/switch/hitter"
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__)
data/bin/setup ADDED
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'hako-switch-hitter'
7
+ spec.version = '0.1.0'
8
+ spec.authors = ['wata']
9
+ spec.email = ['wata.gm@gmail.com']
10
+
11
+ spec.summary = 'Hit switch endpoint on deploy_finished'
12
+ spec.description = 'Hit switch endpoint on deploy_finished'
13
+ spec.homepage = 'https://github.com/wata-gh/hako-switch-hitter'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'hako'
24
+ spec.add_development_dependency 'bundler', '~> 1.14'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'webmock'
28
+ spec.add_development_dependency 'pry-byebug'
29
+ end
@@ -0,0 +1,119 @@
1
+ require 'aws-sdk'
2
+ require 'hako'
3
+ require 'hako/error'
4
+ require 'hako/script'
5
+ require 'net/http'
6
+
7
+ module Hako
8
+ module Scripts
9
+ class SwitchHitter < Script
10
+
11
+ # @param [Hash] options
12
+ def configure(options)
13
+ @options = options
14
+ end
15
+
16
+ # @param [Hash<String, Container>] containers
17
+ # @return [nil]
18
+ def deploy_finished(containers)
19
+ hit_switch
20
+ end
21
+
22
+ alias_method :rollback, :deploy_finished
23
+
24
+ private
25
+
26
+ # @return [Hash]
27
+ def endpoint
28
+ raise Error.new("Switch hitter endpoint is not configured") unless @options[:endpoint]
29
+ @options[:endpoint]
30
+ end
31
+
32
+ # @return [String]
33
+ def endpoint_proto
34
+ endpoint[:proto] || protocol
35
+ end
36
+
37
+ # @return [String]
38
+ def endpoint_host
39
+ return endpoint[:host] if endpoint[:host]
40
+
41
+ load_balancer = describe_load_balancer
42
+ load_balancer.dns_name
43
+ end
44
+
45
+ # @return [Fixnum]
46
+ def endpoint_port
47
+ endpoint[:port] || port
48
+ end
49
+
50
+ # @return [String]
51
+ def endpoint_path
52
+ raise Error.new("Switch hitter path is not configured") unless endpoint[:path]
53
+ endpoint[:path]
54
+ end
55
+
56
+ # @return [Net::HTTP]
57
+ def http(host, port)
58
+ Net::HTTP.new(host, port)
59
+ end
60
+
61
+ # @return [nil]
62
+ def hit_switch
63
+ net_http = http(endpoint_host, endpoint_port)
64
+
65
+ Hako.logger.info("Switch endpoint #{endpoint_proto.downcase}://#{endpoint_host}:#{endpoint_port}#{endpoint_path}")
66
+ if endpoint_proto.upcase == 'HTTPS'
67
+ net_http.use_ssl = true
68
+ net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE
69
+ end
70
+
71
+ net_http.start do
72
+ req = Net::HTTP::Get.new(endpoint_path)
73
+ res = net_http.request(req)
74
+ unless res.code == '200'
75
+ raise Error.new("Switch hitter HTTP Error: #{res.code}: #{res.body}")
76
+ end
77
+ Hako.logger.info("Enabled #{endpoint_path} at #{res.body}")
78
+ end
79
+ end
80
+
81
+ # @return [String]
82
+ def region
83
+ @app.yaml.fetch(:scheduler).fetch(:region)
84
+ end
85
+
86
+ # @return [Fixnum]
87
+ def port
88
+ @app.yaml.fetch(:scheduler).fetch(:elb_v2).fetch(:listeners)[0].port
89
+ end
90
+
91
+ # @return [String]
92
+ def protocol
93
+ @app.yaml.fetch(:scheduler).fetch(:elb_v2).fetch(:listeners)[0].protocol
94
+ end
95
+
96
+ # @return [Aws::ElasticLoadBalancingV2::Client]
97
+ def elb_v2
98
+ @elb_v2 ||= Aws::ElasticLoadBalancingV2::Client.new(region: region)
99
+ end
100
+
101
+ # @return [Aws::ElasticLoadBalancingV2::Types::Listener]
102
+ def describe_listener(load_balancer_arn)
103
+ elb_v2.describe_listeners(load_balancer_arn: load_balancer_arn).listeners[0]
104
+ end
105
+
106
+ # @return [Aws::ElasticLoadBalancingV2::Types::LoadBalancer]
107
+ def describe_load_balancer
108
+ elb_v2.describe_load_balancers(names: [name]).load_balancers[0]
109
+ rescue Aws::ElasticLoadBalancingV2::Errors::LoadBalancerNotFound
110
+ nil
111
+ end
112
+
113
+ # @return [String]
114
+ def name
115
+ "hako-#{@app.id}"
116
+ end
117
+ end
118
+ end
119
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hako-switch-hitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - wata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hako
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Hit switch endpoint on deploy_finished
98
+ email:
99
+ - wata.gm@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - hako-switch-hitter.gemspec
114
+ - lib/hako/scripts/switch_hitter.rb
115
+ homepage: https://github.com/wata-gh/hako-switch-hitter
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.5.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Hit switch endpoint on deploy_finished
139
+ test_files: []