Module: Deis::Helpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.seconds_to_human(secs) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/deis/helpers.rb', line 173

module_function def seconds_to_human(secs)
  secs    = secs.round
  output  = ''
  seconds = secs % 60
  minutes = (secs - seconds) / 60
  output << "#{minutes}m" if minutes > 0
  output << "#{seconds}s"
end

Instance Method Details

#app_exists?(app) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/deis/helpers.rb', line 37

def app_exists?(app)
  !!deis_command('info', app: app)
rescue NonZeroExitError
  false
end

#capture_output(io) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/deis/helpers.rb', line 147

def capture_output(io)
  output = ''
  io.each do |line|
    output << line
    STDOUT.puts "#{line.strip}".light_black unless line.nil? || line.strip.empty? if debug?
  end
ensure
  return output
end

#capture_syscall(command) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/deis/helpers.rb', line 133

def capture_syscall(command)
  puts "\n#{command}".light_yellow if debug?
  String.new.tap do |output|
    threads = []
    PTY.spawn(command) do |p_out, p_in, pid|
      threads << Thread.new { p_in.close }
      threads << Thread.new { output << capture_output(p_out) }
      Process.wait pid
      threads.each(&:join)
    end
    raise NonZeroExitError, output unless $?.exitstatus == 0
  end
end

#debug?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/deis/helpers.rb', line 26

def debug?
  ENV['DEBUG'].in? TRUTHY_STRINGS
end

#deis_command(*cmds) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/deis/helpers.rb', line 95

def deis_command(*cmds)
  @retry_count ||= 0
  shell :deis, *cmds
rescue Errno::ENOENT
  deis_local_command *cmds
rescue Deis::NonZeroExitError => e
  raise e unless should_retry?
  deis_login!
  retry
end

#deis_local_command(*cmds) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/deis/helpers.rb', line 106

def deis_local_command(*cmds)
  @retry_count ||= 0
  shell('./deis', *cmds).tap { reset_retry! }
rescue Errno::ENOENT => e
  raise e unless should_retry?
  shell 'curl -sSL http://deis.io/deis-cli/install.sh | sh -s 1.6.1'
  singleton_class.send :alias_method, :deis_command, :deis_local_command
  retry
rescue Deis::NonZeroExitError => e
  raise(e) unless should_retry?
  deis_login!
  retry
end

#deis_login!Object



120
121
122
# File 'lib/deis/helpers.rb', line 120

def deis_login!
  deis_command :login, ENV['DEIS_CONTROLLER_URL'], username: ENV['DEIS_USERNAME'], password: ENV['DEIS_PASSWORD']
end

#deploy(url, options = {}) ⇒ Object



128
129
130
131
# File 'lib/deis/helpers.rb', line 128

def deploy(url, options = {})
  ref = options.delete(:ref) || 'master'
  shell "git push #{url} #{ref}:master", options
end

#get_runner(name) ⇒ Object



30
31
32
33
34
35
# File 'lib/deis/helpers.rb', line 30

def get_runner(name)
  Deis::Commands.const_get(name.gsub('-', '_').camelize)
rescue NameError
  STDERR.puts "command `#{name}` does not exist!".red
  exit 1
end

#get_units!Object



162
163
164
165
166
167
# File 'lib/deis/helpers.rb', line 162

def get_units!
  unit_string = deis_command('ps', app: app)
  shell('foreman check').strip.sub(/.*\((.*)\)/, "\\1").split(', ').each do |unit|
    units[unit] ||= unit_string.lines.select { |l| l.include? "#{unit}." }.count
  end
end

#git_clone(url, flags = {}) ⇒ Object



124
125
126
# File 'lib/deis/helpers.rb', line 124

def git_clone(url, flags = {})
  shell "git clone #{url}", flags
end

#increment_retry!Object



43
44
45
# File 'lib/deis/helpers.rb', line 43

def increment_retry!
  Deis.retry_counter += 1
end

#info(app) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/deis/helpers.rb', line 62

def info(app)
  cmd_result      = deis_command(:info, app: app)
  data            = JSON.load cmd_result.match(/Application\r\n(?<json>\{.*\})/m)[:json]
  controller_host = data['url'].split('.').tap { |p| p[p.index app] = 'deis' }.join('.')
  data['git_url'] = "ssh://git@#{controller_host}:2222/#{app}.git"
  data['domains'] = cmd_result.match(/Domains\r\n(?<domains>.*)/m)[:domains].lines.map(&:strip).reject(&:empty?)
  data.sort.to_h
end

#reset_retry!Object



47
48
49
# File 'lib/deis/helpers.rb', line 47

def reset_retry!
  Deis.retry_counter == 0
end

#run_util(name, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/deis/helpers.rb', line 16

def run_util(name, *args)
  reset_retry!
  status = false
  time   = Benchmark.realtime do
    status = get_runner(name).new(*args).run
  end
  STDOUT.puts "#{$0} #{name} #{args.join ' '}` took #{seconds_to_human time}".light_blue if debug?
  status
end

#scale(app, opts = {}) ⇒ Object



157
158
159
160
# File 'lib/deis/helpers.rb', line 157

def scale(app, opts={})
  process_string = opts.map { |k, v| "#{k}=#{v}" }.join ' '
  deis_command "scale #{process_string}", app: app
end

#shell(*commands) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/deis/helpers.rb', line 71

def shell(*commands)
  result = ''
  Bundler.with_clean_env do
    flags   = commands.last.is_a?(Hash) ? commands.pop : {}
    command = commands.join ' '
    flags.each_with_object(command) do |(k, v), c|
      c << case v
           when TrueClass, FalseClass
             v ? " --#{k}" : ''
           when String, Fixnum
             " --#{k} #{v}"
           else
             raise ArgumentError
           end
    end
    result = nil
    time   = Benchmark.realtime do
      result = capture_syscall command
    end
    STDOUT.puts "`#{command}` took #{seconds_to_human time}".light_black if debug?
  end
  result
end

#should_retry?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/deis/helpers.rb', line 51

def should_retry?
  increment_retry!
  Deis.retry_counter <= 3
end

#status(msg) ⇒ Object



56
57
58
59
60
# File 'lib/deis/helpers.rb', line 56

def status(msg)
  msg = "| #{msg} |"
  sep = ''.ljust(msg.size, '-')
  puts "\n\e[0;92;49m#{[sep, msg, sep].join "\n"}\e[0m"
end

#unitsObject



169
170
171
# File 'lib/deis/helpers.rb', line 169

def units
  @units ||= {}
end