tagenv 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: ff00251ab82c3052552b502bd8dee481fa649f67
4
+ data.tar.gz: 4839c25d9af623187b3fb11fcc4be1ff839a5afc
5
+ SHA512:
6
+ metadata.gz: e73818a7e2e7fc8afce45e994fd245b6ae59d1b19f9fb75ce65f87be28c4093430bf4eda66d0b9b6d2e489c60ac192d8f3d15af6341fe5882aa82d25c24bda5b
7
+ data.tar.gz: 9ffd30b454f16d10d50a54c21e4072ab674163f55faef973bcaf281545035c2fcc694d22fe978d24c0855ad58a3a9136cceea9694344e640e9530a03d24a8ca8
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ Gemfile.lock
2
+ doc/
3
+ pkg/
4
+ **/*.gem
5
+ .yardoc/
6
+ .bundle/
7
+ vendor/bundle/
8
+ .tags
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.*
5
+ - 2.1.*
6
+ - 2.2.*
7
+ - 2.3.0
8
+
9
+ gemfile:
10
+ - Gemfile
11
+
12
+ os:
13
+ - linux
14
+ - osx
15
+
16
+ before_install: gem update bundler
17
+
18
+ script: 'bundle exec rake spec'
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title "tagenv Documentation" --protected
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2017-03-09
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'kramdown'
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 toyama0919
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # tagenv [![Build Status](https://secure.travis-ci.org/toyama0919/tagenv.png?branch=master)](http://travis-ci.org/toyama0919/tagenv)
2
+
3
+ ec2 instance tag apply environment variables.
4
+
5
+ ## Examples
6
+
7
+ ```
8
+ require 'tagenv'
9
+
10
+ Tagenv::Env.load(prefix: 'EC2TAG_')
11
+
12
+ p ENV
13
+ # {"EC2TAG_Group"=>"group_a", "EC2TAG_Roles"=>"front", "EC2TAG_Stages"=>"staging", "EC2TAG_Name"=>"feweb07", "EC2TAG_Project"=>"project_a"}
14
+ ```
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'tagenv'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install tagenv
29
+
30
+ ## Synopsis
31
+
32
+ $ tagenv
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new [Pull Request](../../pull/new/master)
41
+
42
+ ## Information
43
+
44
+ * [Homepage](https://github.com/toyama0919/tagenv)
45
+ * [Issues](https://github.com/toyama0919/tagenv/issues)
46
+ * [Documentation](http://rubydoc.info/gems/tagenv/frames)
47
+ * [Email](mailto:toyama0919@gmail.com)
48
+
49
+ ## Copyright
50
+
51
+ Copyright (c) 2017 toyama0919
52
+
53
+ See [LICENSE.txt](../LICENSE.txt) for details.
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler/setup'
7
+ rescue LoadError => e
8
+ abort e.message
9
+ end
10
+
11
+ require 'rake'
12
+
13
+
14
+ require 'rubygems/tasks'
15
+ Gem::Tasks.new
16
+
17
+ require 'rspec/core/rake_task'
18
+ RSpec::Core::RakeTask.new
19
+
20
+ task :test => :spec
21
+ task :default => :spec
22
+
23
+ require 'yard'
24
+ YARD::Rake::YardocTask.new
25
+ task :doc => :yard
data/bin/tagenv ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'tagenv'
5
+
6
+ Tagenv::CLI.start
data/lib/tagenv/cli.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "thor"
2
+
3
+ module Tagenv
4
+ class CLI < Thor
5
+
6
+ map '--version' => :version
7
+ default_task :load
8
+
9
+ def initialize(args = [], options = {}, config = {})
10
+ super(args, options, config)
11
+ @global_options = config[:shell].base.options
12
+ end
13
+
14
+ desc 'load', 'load'
15
+ option :prefix, aliases: '-p', default: 'EC2TAG_',type: :string, desc: 'prefix'
16
+ option :instance_id, aliases: '-i', type: :string, desc: 'instance_id'
17
+ option :print, aliases: '-P', type: :boolean, desc: 'print'
18
+ def load
19
+ Tagenv::Env.load(
20
+ prefix: options[:prefix],
21
+ instance_id: options[:instance_id]
22
+ )
23
+ p ENV if options[:print]
24
+ end
25
+
26
+ desc 'version', 'show version'
27
+ def version
28
+ puts VERSION
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Tagenv
2
+ TIME_OUT = 3
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'logger'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ module Tagenv
6
+ module Ec2
7
+ class Metadata
8
+ def self.get_metadata(path)
9
+ begin
10
+ result = {}
11
+ ::Timeout.timeout(TIME_OUT) {
12
+ body = open('http://169.254.169.254' + path).read
13
+ return body
14
+ }
15
+ return result
16
+ rescue Timeout::Error => e
17
+ raise "not EC2 instance"
18
+ end
19
+ end
20
+
21
+ def self.get_document
22
+ JSON.parse(get_metadata('/latest/dynamic/instance-identity/document/'))
23
+ end
24
+
25
+ def self.get_instance_id
26
+ get_metadata('/latest/meta-data/instance-id')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ require 'aws-sdk'
2
+
3
+ module Tagenv
4
+ module Ec2
5
+ class Tag
6
+ def initialize
7
+ @logger = Logger.new(STDOUT)
8
+ region = ENV['AWS_REGION'] || Metadata.get_document['region']
9
+ @ec2 = Aws::EC2::Client.new(region: region)
10
+ end
11
+
12
+ def instances_tag_with_id(instance_id)
13
+ tags = @ec2.describe_instances(
14
+ instance_ids: [instance_id]
15
+ ).data.to_h[:reservations].map { |instance| instance[:instances].first }.first[:tags]
16
+ get_tag_hash(tags)
17
+ end
18
+
19
+ def get_tag_hash(tags)
20
+ result = {}
21
+ tags.each {|hash|
22
+ result[hash['key'] || hash[:key]] = hash['value'] || hash[:value]
23
+ }
24
+ result
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/tagenv/env.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Tagenv
2
+ class Env
3
+ def self.load(prefix: '', instance_id: nil)
4
+ @ec2_tag = Ec2::Tag.new
5
+ instance_id = instance_id || Ec2::Metadata.get_instance_id
6
+ tag_hash = @ec2_tag.instances_tag_with_id(instance_id)
7
+ tag_hash.each do |k, v|
8
+ ENV[prefix + k] = v
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module Tagenv
2
+ # tagenv version
3
+ VERSION = "0.1.0"
4
+ end
data/lib/tagenv.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'tagenv/version'
2
+ require 'tagenv/constants'
3
+ require 'tagenv/env'
4
+ require 'tagenv/ec2/metadata'
5
+ require 'tagenv/ec2/tag'
6
+ require 'tagenv/cli'
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'tagenv'
3
+
4
+ describe Tagenv::CLI do
5
+ before do
6
+ end
7
+
8
+ it "should stdout sample" do
9
+ output = capture_stdout do
10
+ Tagenv::CLI.start(['help'])
11
+ end
12
+ expect(output).not_to eq(nil)
13
+ end
14
+
15
+ it "include" do
16
+ output = capture_stdout do
17
+ Tagenv::CLI.start(['help', 'sample'])
18
+ end
19
+ expect(output).to include('--fields')
20
+ end
21
+
22
+ after do
23
+ end
24
+ end
data/spec/core_spec.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'tagenv'
3
+
4
+ describe Tagenv::Core do
5
+ before do
6
+ @core = Core.new
7
+ end
8
+
9
+ it "core not nil" do
10
+ expect(@core).not_to eq(nil)
11
+ end
12
+
13
+ it "private method" do
14
+ @core.send(:sample, []).should == []
15
+ end
16
+
17
+ after do
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ require 'rspec'
2
+ require 'tagenv/version'
3
+
4
+ include Tagenv
5
+
6
+ def capture_stdout
7
+ out = StringIO.new
8
+ $stdout = out
9
+ yield
10
+ return out.string
11
+ ensure
12
+ $stdout = STDOUT
13
+ end
14
+
15
+ def capture_stderr
16
+ out = StringIO.new
17
+ $stderr = out
18
+ yield
19
+ return out.string
20
+ ensure
21
+ $stderr = STDERR
22
+ end
23
+
24
+ def clear_env
25
+ ENV['PASSPHRASE'] = nil
26
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'tagenv'
3
+
4
+ describe Tagenv do
5
+ it "should have a VERSION constant" do
6
+ expect(subject.const_get('VERSION')).to_not be_empty
7
+ end
8
+ end
data/tagenv.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/tagenv/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "tagenv"
7
+ gem.version = Tagenv::VERSION
8
+ gem.summary = %q{ec2 instance tag apply environment variables.}
9
+ gem.description = %q{ec2 instance tag apply environment variables.}
10
+ gem.license = "MIT"
11
+ gem.authors = ["toyama0919"]
12
+ gem.email = "toyama0919@gmail.com"
13
+ gem.homepage = "https://github.com/toyama0919/tagenv"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_dependency 'thor'
21
+ gem.add_dependency 'aws-sdk', '>= 2'
22
+
23
+ gem.add_development_dependency 'bundler'
24
+ gem.add_development_dependency 'pry', '~> 0.10.1'
25
+ gem.add_development_dependency 'rake', '~> 10.3.2'
26
+ gem.add_development_dependency 'rspec', '~> 3.0'
27
+ gem.add_development_dependency 'rubocop', '~> 0.24.1'
28
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
29
+ gem.add_development_dependency 'yard', '~> 0.8'
30
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tagenv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - toyama0919
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
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
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
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.3.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 10.3.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.24.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.24.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubygems-tasks
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.2'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.8'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.8'
139
+ description: ec2 instance tag apply environment variables.
140
+ email: toyama0919@gmail.com
141
+ executables:
142
+ - tagenv
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".document"
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - ".yardopts"
151
+ - ChangeLog.md
152
+ - Gemfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/tagenv
157
+ - lib/tagenv.rb
158
+ - lib/tagenv/cli.rb
159
+ - lib/tagenv/constants.rb
160
+ - lib/tagenv/ec2/metadata.rb
161
+ - lib/tagenv/ec2/tag.rb
162
+ - lib/tagenv/env.rb
163
+ - lib/tagenv/version.rb
164
+ - spec/cli_spec.rb
165
+ - spec/core_spec.rb
166
+ - spec/spec_helper.rb
167
+ - spec/tagenv_spec.rb
168
+ - tagenv.gemspec
169
+ homepage: https://github.com/toyama0919/tagenv
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 2.6.8
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: ec2 instance tag apply environment variables.
193
+ test_files:
194
+ - spec/cli_spec.rb
195
+ - spec/core_spec.rb
196
+ - spec/spec_helper.rb
197
+ - spec/tagenv_spec.rb