Module: Jack::Util

Overview

Any class that includes this module should define @root as it is used in the settings method.

Constant Summary collapse

@@aws_bin =
nil
@@eb_bin =
nil

Instance Method Summary collapse

Instance Method Details

#app_name_convention(env_name) ⇒ Object



31
32
33
34
# File 'lib/jack/util.rb', line 31

def app_name_convention(env_name)
  pattern = settings.app_name_pattern
  env_name.match(pattern)[1]
end

#aws_binObject

Auto detects the aws binary for use, will use an aws binary based on the following precedence:

1. JACK_AWS_BIN environmental variable
2. aws detected using the load path.
   For example: /usr/bin/local/aws if /usr/bin/local/ is earlest in the load path.
3. /opt/bolts/embedded/bin/aws - This comes packaged with the the bolts toolbelt.
   https://boltops.com/toolbelt

If an aws installation is not deetcted it’ll display a message and exit the program.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jack/util.rb', line 100

def aws_bin
  return @@aws_bin if @@aws_bin

  return @@aws_bin = "aws" if ENV['TEST']

  return @@aws_bin = ENV["JACK_AWS_BIN"] if ENV["JACK_AWS_BIN"]

  which_aws = `which aws`.strip
  return @@aws_bin = which_aws if which_aws != ''

  embedded_aws = "/opt/bolts/embedded/bin/aws"
  return @@aws_bin = embedded_aws if File.exist?(embedded_aws)

  # if reach here we did not detect the eb binary
  message = "ERROR: Unable to auto detect an aws executable. Please make sure you have installed the aws cli tool.\n"
  message << if RUBY_PLATFORM =~ /darwin/
      "You can install the aws tool via homebrew:\n\nbrew install awscli"
    else
      "Installation instructions: http://docs.aws.amazon.com/cli/latest/userguide/installing.html"
    end
  abort(message)
end

#check_aws_setupObject

Checks main if the ~/.aws/config has been set up properly. If it has not print a message to the user and exit the program.



59
60
61
62
# File 'lib/jack/util.rb', line 59

def check_aws_setup
  # abusing `aws configure get region` to check that aws is probably properly configured
  get_region # method will exits when unable to query for the region
end

#confirm(message) ⇒ Object



4
5
6
7
8
9
# File 'lib/jack/util.rb', line 4

def confirm(message)
  Jack::UI.say(message)
  print "yes/no? [no] " unless @options[:mute] || @options[:sure]
  answer = get_answer
  answer =~ /^y/
end

#ebObject



42
43
44
# File 'lib/jack/util.rb', line 42

def eb
  @@eb ||= Aws::ElasticBeanstalk::Client.new
end

#eb_base_flagsObject



153
154
155
156
157
158
159
160
161
162
# File 'lib/jack/util.rb', line 153

def eb_base_flags
  return @eb_base_flags if @eb_base_flags
  region = get_region
  profile = ENV['AWS_PROFILE']
  flags = {
    profile: region ? " --profile #{profile}" : "",
    region: region ? " -r #{region}" : ""
  }
  @eb_base_flags = "#{flags[:profile]}#{flags[:region]}"
end

#eb_binObject

Auto detects the eb binary for use, will use an eb binary based on the following precedence:

1. JACK_EB_BIN environmental variable
2. eb detected using the load path.
   For example: /usr/bin/local/eb if /usr/bin/local/ is earlest in the load path.
3. /opt/bolts/embedded/bin/eb - This comes packaged with the the bolts toolbelt.
   https://boltops.com/toolbelt

If an eb installation is not detected it’ll display a message and exit the program.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/jack/util.rb', line 133

def eb_bin
  return @@eb_bin if @@eb_bin

  return @@eb_bin = "eb" if ENV['TEST']

  return @@eb_bin = ENV["JACK_EB_BIN"] if ENV["JACK_EB_BIN"]

  which_eb = `which eb`.strip
  return @@eb_bin = which_eb if which_eb != ''

  embedded_eb = "/opt/bolts/embedded/bin/eb"
  return @@eb_bin = embedded_eb if File.exist?(embedded_eb)

  # if reach here we did not detect the eb binary
  message = "ERROR: Unable to auto detect an eb executable. Please make sure you have installed the eb cli tool.\n"
  instructions = VersionChecker.new.install_instructions
  message << instructions
  abort(message)
end

#ensure_folder_exist(folder) ⇒ Object



46
47
48
# File 'lib/jack/util.rb', line 46

def ensure_folder_exist(folder)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
end

#get_answerObject



11
12
13
14
# File 'lib/jack/util.rb', line 11

def get_answer
  return 'y' if @options[:sure]
  $stdin.gets
end

#get_regionObject

get the region from ~/.aws/config settings The eb tool reqiures you to specify the region when you run ‘eb init`. After that the setting is saved in the ~/.elasticbeanstalk folder. We will default to what is set in ~/.aws/config.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jack/util.rb', line 68

def get_region
  return 'us-west-2' if ENV['TEST']

  command = "#{aws_bin} configure get region"
  region = `#{command}`.strip
  success = $?.success?
  unless success
    abort <<~EOS.colorize(:red)
      ERROR: Unable to infer the region from your ~/.aws/config settings with command: `#{command}`.
      Maybe it is not properly configured? Please double check ~/.aws/config.
      If you haven't set your region yet, ou can set your region with `aws configure set region REGION`. Example:
      aws configure set region us-west-2
    EOS
  end

  # Defaults to us-east-1:
  # right now aws configure get region will actually return an error 1 code so we
  # will never default to us-east-1 here but doing this just in case the return code
  # changes to 0.
  region == '' ? 'us-east-1' : region
end

#prerequisitesObject

Sets up common prerequisites like setting up the .elasticbeanstsalk/ files for create or deploy to work



52
53
54
55
# File 'lib/jack/util.rb', line 52

def prerequisites
  check_aws_setup
  Jack::EbConfig::Create.new(@options).sync unless @options[:noop]
end

#settingsObject



36
37
38
39
40
# File 'lib/jack/util.rb', line 36

def settings
  # do not like the instance @root variable in this module but better
  # than having to pass settings around
  @settings ||= Jack::Settings.new(@root)
end

#sh(command, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jack/util.rb', line 16

def sh(command, options={})
  Jack::UI.say "=> #{command.colorize(:green)}"
  return command if @options[:noop]

  if options[:backtick]
    out = `#{command}`
    Jack::UI.say out
    success = $?.success?
  else
    success = system(command)
  end

  abort "Exiting! Error running command: #{command}".colorize(:red) unless success
end