Class: BeanDocker::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/bean_docker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.aws_credentials?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/bean_docker.rb', line 102

def self.aws_credentials?
  have_credentials = ENV['AWS_ACCESS_KEY'] && ENV['AWS_SECRET_KEY']
  puts "You must set AWS_ACCESS_KEY and AWS_SECRET_KEY to connect to AWS." unless have_credentials
  have_credentials
end

.get_aws_clientObject



92
93
94
95
96
97
98
99
100
# File 'lib/bean_docker.rb', line 92

def self.get_aws_client
  aws_settings = {
      :access_key_id => ENV['AWS_ACCESS_KEY'],
      :secret_access_key => ENV['AWS_SECRET_KEY'],
      :region => 'us-west-2'
  }

  Aws::ElasticBeanstalk::Client.new (AWS_SETTINGS)
end

.help_textObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/bean_docker.rb', line 197

def self.help_text
  puts "  |\n  |  Launch a new Docker container using same image and env vars Elastic Beanstalk uses\n  |\n  |  Usage:\n  |    bdrun\n  |    bdrun -h | --help\n  |    bdrun -s | --show\n  |\n  |  Options:\n  |     -h --help     Show this screen.\n  |     -s --show     Show the Docker run command but don't execute it\n  |\n  END\nend\n".gsub(/^\s+\|/, '')

.linux_gnu?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/bean_docker.rb', line 84

def self.linux_gnu?
  RbConfig::CONFIG['host_os'].start_with?('linux-gnu')
end

.mac_os?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/bean_docker.rb', line 80

def self.mac_os?
  RbConfig::CONFIG['host_os'].start_with?('darwin')
end

Instance Method Details

#aws_clientObject



88
89
90
# File 'lib/bean_docker.rb', line 88

def aws_client
  @aws_client ||= Docker.get_aws_client
end

#get_env_vars(environment) ⇒ Object

get_env_vars

Finds env vars for a Beanstalk environment, whether on an instance or on your local using the AWS Ruby gem connect to that environment



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bean_docker.rb', line 113

def get_env_vars environment
  var_hash = {}

  if on_beanstalk_instance?
    container_config = JSON.parse(File.read(CONFIG_PATH))
    raw_vars =  container_config['optionsettings']['aws:elasticbeanstalk:application:environment']
    var_hash[:variable], var_hash[:value] = raw_var.split('=')
  elsif aws_credentials?
    response = aws_client.describe_environments(:environment_names => [environment_name])
    application_name = response.environments[0].application_name
    environment_id = response.environments[0].environment_id

    response = aws_client.describe_configuration_settings({:application_name => application_name, :environment_name => environment_name})


    option_settings = response.configuration_settings[0].option_settings
    rails_env_setting = option_settings.select {|setting| setting.option_name == 'RAILS_ENV'}

    if rails_env_setting.first
      rails_env = rails_env_setting.first.value

      # Before adding a variable to the hash the very first time, ask the user
      # This means you check the config file to see if the env var is accepted or rejected

      saved_variables = {}

      test_app_dir = '/Users/abrown/apps/platform-respondent-nurture-service'

      # First find the file
      settings_file = File("#{test_app_dir}/tmp/bd_config", 'r')

      if settings_file
        settings_file.each_line do |line|
          # Ignore comments
          unless line.lstrip[0] == '#'
            value_found = line.index('=')
            if value_found
              variable = line.slice(0...value_found)
              value = line.slice(value_found + 1..-1)

              if value.length == 0
                saved_variables[variable] = value
              else
                saved_variables[variable] = value
              end
            end
          end
        end
      end

      # Append to the file
      config = File('tmp/bd_config', 'a')

      # Prompt the user regarding the new variables

      # Load each line into a hash key whose value is either blank or a quoted string
      # If it's blank, ignore this variable
      # If it's set, use this value
      # If it's missing, prompt the user to accept or override


      # If the user wants you to always ask, don't record the variable
      # Use the existing settings as you go to build the


      envvars = option_settings.select do |setting|
        setting[:namespace] == "aws:elasticbeanstalk:application:environment" &&
            !ignore_settings.include?(setting.option_name)
      end

      envvar_string = envvars.map{|envvar| "#{envvar.option_name}=#{envvar.value}"}.join(' ')


      # var_hash[:variable], var_hash[:value] = raw_var.split('=')


    end

  else
    puts "Must be on a Beanstalk instance.  Or have AWS_ACCESS_KEY and AWS_SECRET_KEY set."
  end

end

#help?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/bean_docker.rb', line 68

def help?
  return false unless @args && @args.any?
  
  %w[-h --help help].include? @args[0]
end

#run(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bean_docker.rb', line 10

def run(args)
  @args = args

  puts Docker.help_text && return if help?

  if Docker.mac_os?
    puts "This command can only run on an AWS Elastic Beanstalk instance."
    return
  end

  puts "This command has only been tested on Amazon Linux." unless Docker.linux_gnu?

  envvar_file_name = '/opt/elasticbeanstalk/deploy/configuration/containerconfiguration'

  begin
    container_config = JSON.parse(File.read(envvar_file_name))
  rescue => exception
    puts ""
    puts "  The environment variables needed to launch a new Docker container cannot be found."
    puts ""
    puts "  You should have installed this gem using sudo.  If you don't want to re-install, run this to make these variables accessible:"
    puts ""
    puts "    sudo chmod 664 #{envvar_file_name}"
    puts ""
  else
    begin
      raw_vars =  container_config['optionsettings']['aws:elasticbeanstalk:application:environment']

      command = "sudo docker run -ti -w=\"/usr/src/app\""

      raw_vars.each do |raw_var|
        variable, value = raw_var.split('=')
        if value
          command += " --env #{variable}=\"#{value.shellescape}\" "
        end
      end

      command = "#{command} #{DOCKER_IMAGE} bash"

      if show?
        puts ""
        puts "  Command for launching a new container:\n"
        puts ""
        puts "    #{command}"
        puts ""
      else
        puts ""
        puts "  Launching a new container...."
        puts ""

        exec( command )
      end
    rescue => exception
      puts "Exception: #{exception}"
    end
  end
end

#show?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/bean_docker.rb', line 74

def show?
  return false unless @args && @args.any?
  
  %w[-s --show show].include? @args[0]
end