Module: Amplify::AWS::MixIns::NoCommands

Includes:
DLDInternet::Mixlib::Logging
Defined in:
lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb

Instance Method Summary collapse

Instance Method Details

#_expand(opts, k, v, regex, rerun) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 128

def _expand(opts,k,v,regex,rerun)
  matches = v.match(regex)
  if matches
    var = matches[1]
    if opts[var]
      opts[k]=v.gsub(/\%\(#{var}\)/,opts[var]).gsub(/\%#{var}/,opts[var])
    else
      rerun[var] = 1
    end
  end
end

#check_missing_options(method, optionset) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 240

def check_missing_options(method,optionset)
  parse_options
  missing = []
  optionset.each do |req|
    case req.class.name
      when /String|Symbol/
        missing << req unless @options[req]
      when /Array/
        flag = false
        req.each do |o|
          if @options[o]
            flag = true
            break
          end
        end
        missing << req.join('|') unless flag
      else
        logger.fatal "Internal: #{method}: Unsupported missing option type: #{req.class}"
        exit 99
    end
  end

  if missing.size > 0
    logger.error "#{method}: Missing required options: #{missing.join(', ')}"
    exit 1
  end
end

#expand_optionsObject



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
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 127

def expand_options()
  def _expand(opts,k,v,regex,rerun)
    matches = v.match(regex)
    if matches
      var = matches[1]
      if opts[var]
        opts[k]=v.gsub(/\%\(#{var}\)/,opts[var]).gsub(/\%#{var}/,opts[var])
      else
        rerun[var] = 1
      end
    end
  end

  pending = nil
  rerun   = {}
  begin
    pending = rerun
    rerun   = {}
    @options.to_hash.each{|k,v|
      if v.to_s.match(/\%/)
        _expand(@options,k,v,%r'[^\\]\%\((\w+)\)', rerun)
        _expand(@options,k,v,%r'[^\\]\%(\w+)',     rerun)
      end
    }
    # Should break out the first time that we make no progress!
  end while pending != rerun
end

#load_credentials_inifile(inifile, section = 'global', map = nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 223

def load_credentials_inifile(inifile, section='global', map=nil)
  begin
    ini = load_inifile(inifile)
    ini[section].each{ |key,value|
      #@options[key.to_s]=value
      # @logger.debug "#{key} '#{value.gsub(/./,'*')}'"
      ENV[key.to_s]=value.to_s
    }
    expand_options()
  rescue ::IniFile::Error => e
    # noop
  rescue ::Exception => e
    @logger.error "#{e.class.name} #{e.message}"
    raise e
  end
end

#load_credentials_profile(profile, map = nil) ⇒ Object



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
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 155

def load_credentials_profile(profile, map=nil)
  begin
    if @saved_env and @saved_env.size > 0
      @saved_env.each do |k,v|
        ENV[k] = v
      end
      ENV.to_hash.each do |k,v|
        unless @saved_env.has_key?(k)
          ENV.delete(k)
        end
      end
    else
      @saved_env = ENV.to_hash.dup
    end
    ini = load_inifile('~/.aws/config')
    ini['default'].each{ |key,value|
      k = if map[key.to_s]
            map[key.to_s]
          else
            key.to_s
          end
      ENV[k]=value
    }
    ini["profile #{profile}"].each{ |key,value|
      k = if map[key.to_s]
            map[key.to_s]
          else
            key.to_s
          end
      ENV[k]=value
    }
     ini = load_inifile('~/.aws/credentials')
    unless ini.sections.include?(profile)
      logger.error "No credentials found for profile #{profile}"
      exit 11
    end
    ini[profile].each{ |key,value|
      k = if map[key.to_s]
            map[key.to_s]
          else
            key.to_s
          end
      ENV[k]=value
    }
    expand_options()
  rescue ::IniFile::Error => e
    # noop
  rescue SystemExit => e
    raise e
  rescue ::Exception => e
    @logger.error "#{e.class.name} #{e.message}"
    raise e
  end
end

#load_inifile(inifile) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 210

def load_inifile(inifile)
  inifile = File.expand_path(inifile)
  unless File.exist?(inifile)
    raise "#{inifile} not found!"
  end
  begin
    ini = ::IniFile.load(inifile)
  rescue ::Exception => e
    @logger.error "#{e.class.name} #{e.message}"
    raise e
  end
end

#parse_optionsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 56

def parse_options
  validate_options

  colormap = {
      :trace => :blue,
      :debug => :cyan,
      :info  => :green,
      :step  => :green,
      :warn  => :yellow,
      :error => :red,
      :fatal => :red,
      :todo  => :purple,
  }
  unless options[:color]
    LOG_LEVELS.each do |l,_|
      colormap[l] = :clear
    end
  end
  lcs = ::Logging::ColorScheme.new( 'compiler', :levels => colormap)
  scheme = lcs.scheme
  if options[:color]
    scheme['trace'] = "\e[38;5;33m"
    scheme['fatal'] = "\e[38;5;89m"
    scheme['todo']  = "\e[38;5;55m"
  end
  lcs.scheme scheme
  @config[:log_opts] = lambda{|mlll| {
      :pattern      => "%#{mlll}l: %m %g\n",
      :date_pattern => '%Y-%m-%d %H:%M:%S',
      :color_scheme => 'compiler',
      :trace        => (@config[:trace].nil? ? false : @config[:trace]),
      # [2014-06-30 Christo] DO NOT do this ... it needs to be a FixNum!!!!
      # If you want to do ::Logging.init first then fine ... go ahead :)
      # :level        => @config[:log_level],
  }
  }
  @logger = getLogger(@config)

  if @options[:inifile]
    load_credentials_inifile(@options[:inifile], 'global')
  elsif @options[:profile]
    unless File.directory?(File.expand_path('~/.aws'))
      msg = 'Cannot load profile. ~/.aws does not exist!?'
      @logger.error msg
      raise msg
    end
    unless File.exists?(File.expand_path('~/.aws/config'))
      msg = 'Cannot load profile. ~/.aws/config does not exist!?'
      @logger.error msg
      raise msg
    end
    unless File.exists?(File.expand_path('~/.aws/credentials'))
      msg = 'Cannot load profile. ~/.aws/credentials does not exist!?'
      @logger.error msg
      raise msg
    end
    @logger.debug "Profile: #{@options[:profile]}"
    load_credentials_profile(@options[:profile],  {
                                                    'output'                => 'AWS_DEFAULT_OUTPUT',
                                                    'region'                => 'AWS_DEFAULT_REGION',
                                                    'aws_access_key_id'     => 'AWS_ACCESS_KEY_ID',
                                                    'aws_secret_access_key' => 'AWS_SECRET_ACCESS_KEY',
                                                  })
  end

  if options[:debug]
    @logger.info "Options:\n#{options.ai}"
  end

end

#prepare_accountsObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 274

def prepare_accounts
  @accounts = []
  if @options[:iniglob]
    iniglob = File.expand_path(@options[:iniglob])
    Dir.glob( iniglob) {| filename |
      @accounts << IniFileCredentials.new(filename)
    }
  elsif @options[:inifile]
    @accounts << IniFileCredentials.new(@options[:inifile]) unless (@options[:inifile].nil? or @options[:inifile].empty?)
  end

  if @options[:profile]
    if @options[:profiles]
      @options[:profiles] << @options[:profile]
    else
      @options[:profiles] = [@options[:profile]]
    end
  end
  if @options[:profiles]
    @options[:profiles].each {| profile |
      @accounts << ProfileCredentials.new(profile)
    }
  end
  unless @accounts.size > 0
    parse_options
    logger.fatal 'No options allow for account identification'
  end
  # Make the options mutable
  unless @options.is_a?(Hash) or @options.is_a?(Hashie::Mash)
    @options = Hashie::Mash.new(@options.to_hash)
  end
end


268
269
270
271
272
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 268

def print_error(err)

  logger.error err.to_s

end

#validate_optionsObject



46
47
48
49
50
51
52
53
54
# File 'lib/cicd/builder/environments-list/mixlib/lib/mixins/no_commands.rb', line 46

def validate_options
  @config = @options.dup
  if @config[:log_level]
    log_level = @config[:log_level].to_sym
    raise "Invalid log-level: #{log_level}" unless LOG_LEVELS.include?(log_level)
    @config[:log_level] = log_level
  end
  @config[:log_level] ||= :info
end