bean_docker 0.1.22 → 0.1.30

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d7a1036781336a2047aa4b0aa7ae6bb1f4b1604
4
- data.tar.gz: 7c9371b86a91c9cac9ee5e8e4133e904a53b9465
3
+ metadata.gz: b6f4dda5aaaeaa492e6476a0e628e8e3f70971e1
4
+ data.tar.gz: 140c442a5e87036c68d80c18f390381ca2d0cb9f
5
5
  SHA512:
6
- metadata.gz: f546e8ba0a4ca3d4a037d85e4e796615957c198d31a7f619bfea029320d2f5e801a5ea2a3cef5915035bd99c5ad3b9e883f094f312f978a359ce46fae0756db6
7
- data.tar.gz: 07b614de4199a48d410b1c9cccd301da288578cb310c5d835c18f78b11f1530dde1a4576b44420820d57a60874cb5db550e00eeed338abc93ac0f129046fc698
6
+ metadata.gz: f4068a9da95077f44329374f997372d623398223971e1385fb9fe0817b8232a1cb09d294ac85d77fee651e6f5b464a5113560e7616542a38e0915666213025bd
7
+ data.tar.gz: 695b91cd41b77318bd36149716f3d70597abafa544146cb3e2a18d20273bfc4e084e0de5b4b19cafabdfb9f0563e74e451a81fc2f6f099fba7d03cd42cdf390f
data/.gitignore CHANGED
@@ -28,9 +28,12 @@ build/
28
28
 
29
29
  # for a library or gem, you might want to ignore these files since the code is
30
30
  # intended to run in multiple environments; otherwise, check them in:
31
- # Gemfile.lock
32
- # .ruby-version
33
- # .ruby-gemset
31
+ Gemfile.lock
32
+ .ruby-version
33
+ .ruby-gemset
34
34
 
35
35
  # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
36
  .rvmrc
37
+
38
+ # Ignoring Rubymine files
39
+ /.idea
data/README.md CHANGED
@@ -9,21 +9,13 @@ that Beanstalk uses to start each new container.
9
9
 
10
10
  Install this gem on your Elastic Beanstalk Docker instance:
11
11
 
12
- $ gem install bean_docker
13
-
14
- To use this gem, you will need to relax the permissions on the file containing your environment variables:
15
-
16
- $ sudo chmod 664 /opt/elasticbeanstalk/deploy/configuration/containerconfiguration
17
-
18
- And to restore the original permissions:
19
-
20
- $ sudo chmod 660 /opt/elasticbeanstalk/deploy/configuration/containerconfiguration
12
+ $ sudo gem install bean_docker
21
13
 
22
14
  ## Usage
23
15
 
24
16
  To launch a new container on an Elastic Beanstalk docker instance:
25
17
 
26
- $ bdrun
18
+ $ sudo /usr/local/bin/bdrun
27
19
 
28
20
  ## Development
29
21
 
@@ -33,7 +25,6 @@ Still experimental.
33
25
 
34
26
  Bug reports and pull requests are welcome on GitHub at https://github.com/calasyr/bean_docker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
35
27
 
36
-
37
28
  ## License
38
29
 
39
30
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/bean_docker.gemspec CHANGED
@@ -15,16 +15,11 @@ Gem::Specification.new do |spec|
15
15
  new container. Useful for running rake tasks in your production environment.}
16
16
  spec.homepage = "https://github.com/calasyr/bean_docker"
17
17
  spec.license = "MIT"
18
-
19
- envvar_file_name = '/opt/elasticbeanstalk/deploy/configuration/containerconfiguration'
20
-
21
- spec.post_install_message = %Q{\nThanks for installing! To use this gem, you will need to relax the permissions on the file containing your environment variables:\n
22
- sudo chmod 664 #{envvar_file_name}\n
23
- Then you can used the bdrun command to create a new container:\n
24
- bdrun\n
25
- And to restore the original permissions:\n
26
- sudo chmod 660 #{envvar_file_name}\n
27
- And to see exactly how your sensitive environment variables are used, see lib/bean_docker.rb
18
+
19
+ spec.post_install_message = %Q{\n Thanks for installing! To use this gem on Amazon Linux:\n
20
+ sudo /usr/local/bin/bdrun\n
21
+ If you did not install using sudo, the above will not work.\n
22
+ To see exactly how your sensitive environment variables are used, see lib/bean_docker.rb
28
23
  }
29
24
 
30
25
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
data/bin/bdrun CHANGED
File without changes
@@ -1,3 +1,3 @@
1
1
  module BeanDocker
2
- VERSION = "0.1.22"
2
+ VERSION = "0.1.30"
3
3
  end
data/lib/bean_docker.rb CHANGED
@@ -2,22 +2,35 @@ require "bean_docker/version"
2
2
  require 'shellwords'
3
3
  require 'json'
4
4
 
5
+ DOCKER_IMAGE = "aws_beanstalk/current-app:latest"
6
+ CONFIG_PATH = '/opt/elasticbeanstalk/deploy/configuration/containerconfiguration'
5
7
 
6
8
  module BeanDocker
7
- # Your code goes here...
8
9
  class Docker
9
10
  def run(args)
10
- mode = args[0] ? args[0] : ''
11
+ @args = args
12
+
13
+ puts Docker.help_text && return if help?
14
+
15
+ if Docker.mac_os?
16
+ puts "This command can only run on an AWS Elastic Beanstalk instance."
17
+ return
18
+ end
19
+
20
+ puts "This command has only been tested on Amazon Linux." unless Docker.linux_gnu?
11
21
 
12
- image_name = "aws_beanstalk/current-app:latest"
13
22
  envvar_file_name = '/opt/elasticbeanstalk/deploy/configuration/containerconfiguration'
14
23
 
15
24
  begin
16
25
  container_config = JSON.parse(File.read(envvar_file_name))
17
26
  rescue => exception
18
- puts "Exception: #{exception}"
19
- puts "Enable access to this file with:\n"
20
- puts " sudo chmod 664 #{envvar_file_name}"
27
+ puts ""
28
+ puts " The environment variables needed to launch a new Docker container cannot be found."
29
+ puts ""
30
+ puts " You should have installed this gem using sudo. If you don't want to re-install, run this to make these variables accessible:"
31
+ puts ""
32
+ puts " sudo chmod 664 #{envvar_file_name}"
33
+ puts ""
21
34
  else
22
35
  begin
23
36
  raw_vars = container_config['optionsettings']['aws:elasticbeanstalk:application:environment']
@@ -26,19 +39,23 @@ module BeanDocker
26
39
 
27
40
  raw_vars.each do |raw_var|
28
41
  variable, value = raw_var.split('=')
29
- if value # && !value.include?('`')
42
+ if value
30
43
  command += " --env #{variable}=\"#{value.shellescape}\" "
31
44
  end
32
45
  end
33
46
 
34
- command = "#{command} #{image_name} bash"
47
+ command = "#{command} #{DOCKER_IMAGE} bash"
35
48
 
36
- if mode == 'show'
37
- puts "Command for launching a new container:\n"
38
- puts command
49
+ if show?
50
+ puts ""
51
+ puts " Command for launching a new container:\n"
52
+ puts ""
53
+ puts " #{command}"
54
+ puts ""
39
55
  else
40
- puts "Launching a new container. To protect the environment variables file again after you exit this container, use:\n"
41
- puts " sudo chmod 660 #{envvar_file_name}"
56
+ puts ""
57
+ puts " Launching a new container...."
58
+ puts ""
42
59
 
43
60
  exec( command )
44
61
  end
@@ -47,5 +64,151 @@ module BeanDocker
47
64
  end
48
65
  end
49
66
  end
67
+
68
+ def help?
69
+ return false unless @args && @args.any?
70
+
71
+ %w[-h --help help].include? @args[0]
72
+ end
73
+
74
+ def show?
75
+ return false unless @args && @args.any?
76
+
77
+ %w[-s --show show].include? @args[0]
78
+ end
79
+
80
+ def self.mac_os?
81
+ RbConfig::CONFIG['host_os'].start_with?('darwin')
82
+ end
83
+
84
+ def self.linux_gnu?
85
+ RbConfig::CONFIG['host_os'].start_with?('linux-gnu')
86
+ end
87
+
88
+ def aws_client
89
+ @aws_client ||= Docker.get_aws_client
90
+ end
91
+
92
+ def self.get_aws_client
93
+ aws_settings = {
94
+ :access_key_id => ENV['AWS_ACCESS_KEY'],
95
+ :secret_access_key => ENV['AWS_SECRET_KEY'],
96
+ :region => 'us-west-2'
97
+ }
98
+
99
+ Aws::ElasticBeanstalk::Client.new (AWS_SETTINGS)
100
+ end
101
+
102
+ def self.aws_credentials?
103
+ have_credentials = ENV['AWS_ACCESS_KEY'] && ENV['AWS_SECRET_KEY']
104
+ puts "You must set AWS_ACCESS_KEY and AWS_SECRET_KEY to connect to AWS." unless have_credentials
105
+ have_credentials
106
+ end
107
+
108
+ #
109
+ # get_env_vars
110
+ #
111
+ # Finds env vars for a Beanstalk environment, whether on an instance or on
112
+ # your local using the AWS Ruby gem connect to that environment
113
+ def get_env_vars environment
114
+ var_hash = {}
115
+
116
+ if on_beanstalk_instance?
117
+ container_config = JSON.parse(File.read(CONFIG_PATH))
118
+ raw_vars = container_config['optionsettings']['aws:elasticbeanstalk:application:environment']
119
+ var_hash[:variable], var_hash[:value] = raw_var.split('=')
120
+ elsif aws_credentials?
121
+ response = aws_client.describe_environments(:environment_names => [environment_name])
122
+ application_name = response.environments[0].application_name
123
+ environment_id = response.environments[0].environment_id
124
+
125
+ response = aws_client.describe_configuration_settings({:application_name => application_name, :environment_name => environment_name})
126
+
127
+
128
+ option_settings = response.configuration_settings[0].option_settings
129
+ rails_env_setting = option_settings.select {|setting| setting.option_name == 'RAILS_ENV'}
130
+
131
+ if rails_env_setting.first
132
+ rails_env = rails_env_setting.first.value
133
+
134
+ # Before adding a variable to the hash the very first time, ask the user
135
+ # This means you check the config file to see if the env var is accepted or rejected
136
+
137
+ saved_variables = {}
138
+
139
+ test_app_dir = '/Users/abrown/apps/platform-respondent-nurture-service'
140
+
141
+ # First find the file
142
+ settings_file = File("#{test_app_dir}/tmp/bd_config", 'r')
143
+
144
+ if settings_file
145
+ settings_file.each_line do |line|
146
+ # Ignore comments
147
+ unless line.lstrip[0] == '#'
148
+ value_found = line.index('=')
149
+ if value_found
150
+ variable = line.slice(0...value_found)
151
+ value = line.slice(value_found + 1..-1)
152
+
153
+ if value.length == 0
154
+ saved_variables[variable] = value
155
+ else
156
+ saved_variables[variable] = value
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ # Append to the file
164
+ config = File('tmp/bd_config', 'a')
165
+
166
+ # Prompt the user regarding the new variables
167
+
168
+ # Load each line into a hash key whose value is either blank or a quoted string
169
+ # If it's blank, ignore this variable
170
+ # If it's set, use this value
171
+ # If it's missing, prompt the user to accept or override
172
+
173
+
174
+ # If the user wants you to always ask, don't record the variable
175
+ # Use the existing settings as you go to build the
176
+
177
+
178
+ envvars = option_settings.select do |setting|
179
+ setting[:namespace] == "aws:elasticbeanstalk:application:environment" &&
180
+ !ignore_settings.include?(setting.option_name)
181
+ end
182
+
183
+ envvar_string = envvars.map{|envvar| "#{envvar.option_name}=#{envvar.value}"}.join(' ')
184
+
185
+
186
+ # var_hash[:variable], var_hash[:value] = raw_var.split('=')
187
+
188
+
189
+ end
190
+
191
+ else
192
+ puts "Must be on a Beanstalk instance. Or have AWS_ACCESS_KEY and AWS_SECRET_KEY set."
193
+ end
194
+
195
+ end
196
+
197
+ def self.help_text
198
+ puts <<-END.gsub(/^\s+\|/, '')
199
+ |
200
+ | Launch a new Docker container using same image and env vars Elastic Beanstalk uses
201
+ |
202
+ | Usage:
203
+ | bdrun
204
+ | bdrun -h | --help
205
+ | bdrun -s | --show
206
+ |
207
+ | Options:
208
+ | -h --help Show this screen.
209
+ | -s --show Show the Docker run command but don't execute it
210
+ |
211
+ END
212
+ end
50
213
  end
51
214
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bean_docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.22
4
+ version: 0.1.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Brown
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-19 00:00:00.000000000 Z
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,13 +67,10 @@ licenses:
67
67
  - MIT
68
68
  metadata:
69
69
  allowed_push_host: https://rubygems.org
70
- post_install_message: "\nThanks for installing! To use this gem, you will need to
71
- relax the permissions on the file containing your environment variables:\n\n sudo
72
- chmod 664 /opt/elasticbeanstalk/deploy/configuration/containerconfiguration\n\nThen
73
- you can used the bdrun command to create a new container:\n\n bdrun\n\nAnd to
74
- restore the original permissions:\n\n sudo chmod 660 /opt/elasticbeanstalk/deploy/configuration/containerconfiguration\n\nAnd
75
- to see exactly how your sensitive environment variables are used, see lib/bean_docker.rb\n
76
- \ "
70
+ post_install_message: "\n Thanks for installing! To use this gem on Amazon Linux:\n\n
71
+ \ sudo /usr/local/bin/bdrun\n \n If you did not install using sudo, the above
72
+ will not work.\n\n To see exactly how your sensitive environment variables are
73
+ used, see lib/bean_docker.rb\n "
77
74
  rdoc_options: []
78
75
  require_paths:
79
76
  - lib
@@ -89,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
86
  version: '0'
90
87
  requirements: []
91
88
  rubyforge_project:
92
- rubygems_version: 2.4.8
89
+ rubygems_version: 2.6.11
93
90
  signing_key:
94
91
  specification_version: 4
95
92
  summary: Launch a new Docker container running bash on an Elastic Beanstalk instance.