Module: AppRb::Util

Defined in:
lib/app-rb/util.rb

Defined Under Namespace

Modules: Build, Consul, Docker, Registry

Constant Summary collapse

MIN_PORT =
10_000
MAX_PORT =
50_000

Class Method Summary collapse

Class Method Details

.blue(txt) ⇒ Object



8
# File 'lib/app-rb/util.rb', line 8

def self.blue(txt);   "\e[0;34m#{txt}\e[0m"; end

.compare_versions(a, b) ⇒ Object



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

def self.compare_versions(a, b)
  parse = proc { |v| v.split(".", 3).map(&:to_i) + [v.index("-dev") ? 1 : 0] }
  parse.call(a) <=> parse.call(b)
end

.do_it(cmd) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/app-rb/util.rb', line 10

def self.do_it(cmd)
  puts "[exec] #{cmd}"
  system(cmd)
  unless $?.success?
    puts red("FATAL :(")
    exit 
  end
end

.get_free_port(user, host) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/app-rb/util.rb', line 36

def self.get_free_port(user, host)
  port = nil
  10.times do
    a = MIN_PORT + rand(MAX_PORT - MIN_PORT)
    if just_cmd("ssh #{user}@#{host} ss -ln src :#{a} | fgrep -c ':#{a}'", true) == "0"
      port = a
      break
    end
  end
  raise "Dont find free port :-((" unless port 
  port
end

.green(txt) ⇒ Object



7
# File 'lib/app-rb/util.rb', line 7

def self.green(txt);  "\e[0;32m#{txt}\e[0m"; end

.just_cmd(cmd, skip_exit_status = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/app-rb/util.rb', line 19

def self.just_cmd(cmd, skip_exit_status = false)
  puts "[exec] #{cmd}"
  output = `#{cmd}`
  if $?.success? || skip_exit_status
    output.strip
  else
    puts red(output)
    puts red("FATAL :(")
    exit
  end
end

.red(txt) ⇒ Object



6
# File 'lib/app-rb/util.rb', line 6

def self.red(txt);    "\e[0;31m#{txt}\e[0m"; end

.yellow(txt) ⇒ Object



5
# File 'lib/app-rb/util.rb', line 5

def self.yellow(txt); "\e[0;33m#{txt}\e[0m"; end