Class: Ufo::DSL::Helper

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Util
Defined in:
lib/ufo/dsl/helper.rb

Overview

provides some helperally context variables

Instance Method Summary collapse

Methods included from Util

#default_cluster, #display_params, #execute, #pretty_time, #settings, #task_definition_arns, #user_params

Instance Method Details

#current_regionObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/ufo/dsl/helper.rb', line 74

def current_region
  default_region = 'us-east-1'
  return default_region if ENV['TEST']

  return ENV['UFO_AWS_REGION'] if ENV['UFO_AWS_REGION'] # highest precedence
  return ENV['AWS_REGION'] if ENV['AWS_REGION']

  region = `aws configure get region`.strip rescue default_region
  region.blank? ? default_region : region
end

#dockerfile_portObject

helper variables



16
17
18
19
20
21
# File 'lib/ufo/dsl/helper.rb', line 16

def dockerfile_port
  dockerfile_path = "#{Ufo.root}/Dockerfile"
  if File.exist?(dockerfile_path)
    parse_for_dockerfile_port(dockerfile_path)
  end
end

#env_file(path) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ufo/dsl/helper.rb', line 64

def env_file(path)
  full_path = "#{Ufo.root}/#{path}"
  unless File.exist?(full_path)
    puts "The #{full_path} env file could not be found.  Are you sure it exists?"
    exit 1
  end
  text = IO.read(full_path)
  env_vars(text)
end

#env_vars(text) ⇒ Object

helper methods



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ufo/dsl/helper.rb', line 30

def env_vars(text)
  lines = filtered_lines(text)
  lines.map do |line|
    key,*value = line.strip.split("=").map do |x|
      remove_surrounding_quotes(x.strip)
    end
    value = value.join('=')
    {
      name: key,
      value: value,
    }
  end
end

#filtered_lines(text) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ufo/dsl/helper.rb', line 54

def filtered_lines(text)
  lines = text.split("\n")
  # remove comment at the end of the line
  lines.map! { |l| l.sub(/\s+#.*/,'').strip }
  # filter out commented lines
  lines = lines.reject { |l| l =~ /(^|\s)#/i }
  # filter out empty lines
  lines = lines.reject { |l| l.strip.empty? }
end

#full_image_nameObject



23
24
25
26
# File 'lib/ufo/dsl/helper.rb', line 23

def full_image_name
  # Dont need to use @options here. Helps simplify the Helper initialization.
  Docker::Builder.new({}).full_image_name
end

#parse_for_dockerfile_port(dockerfile_path) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/ufo/dsl/helper.rb', line 85

def parse_for_dockerfile_port(dockerfile_path)
  lines = IO.read(dockerfile_path).split("\n")
  expose_line = lines.find { |l| l =~ /^EXPOSE / }
  if expose_line
    md = expose_line.match(/EXPOSE (\d+)/)
    port = md[1] if md
  end
  port.to_i if port
end

#remove_surrounding_quotes(s) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ufo/dsl/helper.rb', line 44

def remove_surrounding_quotes(s)
  if s =~ /^"/ && s =~ /"$/
    s.sub(/^["]/, '').gsub(/["]$/,'') # remove surrounding double quotes
  elsif s =~ /^'/ && s =~ /'$/
    s.sub(/^[']/, '').gsub(/[']$/,'') # remove surrounding single quotes
  else
    s
  end
end