Module: Lux

Defined in:
lib/lux.rb,
lib/lux/version.rb,
lib/lux/docker_tasks.rb

Defined Under Namespace

Classes: App, DockerContainerTask, DockerImageTask

Constant Summary collapse

VERSION =
"1.1.4"
DISABLED =
case
when version.nil?
   error "Docker server not found, disabling Docker tasks!"
   true
else
   begin
     # Set this to avoid 5 sec timeouts with .local addresses
     # which don't resolve in IPv6
     Docker.options = { family: Socket::Constants::AF_INET, connect_timeout: 5 }
     Docker.validate_version!
     false
   rescue Exception => e
     error "Docker problem (#{e.message}), tasks will be disabled"
     true
   end
end

Class Method Summary collapse

Class Method Details

.die(msg, rc = 1) ⇒ Object



85
86
87
88
# File 'lib/lux.rb', line 85

def die msg, rc = 1
  error msg
  exit rc
end

.dockeripObject

Determine the IPv4 address of the current Docker host Used to avoid long reverse DNS lookups on .local names Also useful when running Docker for macOS which runs on a Unix socket but makes container ports available on localhost

Returns an array of three items:

  • the IPv4 address as a string or ‘127.0.0.1’ if it was a Unix socket

  • a URI object

  • a hash containing the Docker version information

These are all nil if no Docker server is found



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lux.rb', line 22

def dockerip
  begin
    info = Docker.version
    uri = URI.parse Docker.url
    case uri.scheme
    when 'unix'
      addr = '127.0.0.1'
    when 'tcp'
      Addrinfo.tcp(uri.host, uri.port).connect(timeout: 5) {|s|
        addr = s.remote_address.ip_address
        s.print "GET /version HTTP/1.0\r\nHost: localhost\r\n\r\n"
        response, emptyline, body = s.read.partition(/(\r\n){2}/)
        versioninfo = JSON.parse(body, symbolize_names: false)
      }
    else
      raise "Can't handle #{uri.scheme}"
    end
    return addr, uri, info
  rescue
    return nil, nil, nil
  end
end

.error(msg) ⇒ Object



90
91
92
# File 'lib/lux.rb', line 90

def error msg
  HighLine.new(STDIN, STDERR).say HighLine.color(msg, HighLine::RED)
end

.findimage(image) ⇒ Object

Get the current list of images and make a guess at which one it is…



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lux.rb', line 47

def findimage image
  local_images = `docker images`.strip.split("\n")[1..-1].map{|l| l.gsub!(/^(\S+)\s+(\S+).*/,'\1:\2')}.sort
  matching_images = local_images.select{|l| l.include? image }
  if matching_images.size > 0
    if image.count(':') == 0 and image.count('/') > 0
      matching_image = matching_images.select{|l| l.end_with? ':latest' }.first
    end
    unless matching_image
      if matching_images.size == 1
        matching_image = matching_images[0]
      else
        matching_image = HighLine.choose do |menu|
          menu.header = 'List of matching (local) images'
          menu.choices(*matching_images)
          menu.choice('None of the above') { nil }
        end
        exit 2 unless matching_image
      end
    end
  else
    matching_image = image
  end
  return matching_image
end

.info(msg) ⇒ Object



94
95
96
# File 'lib/lux.rb', line 94

def info msg
  HighLine.new(STDIN, STDERR).say HighLine.color(msg, HighLine::YELLOW)
end

.user_setup_cmd(user = `id -nu`.strip) ⇒ Object

Return two elements:

  • user name (defaults to current user), and

  • a bash script setup command

Omit the secret password: -p $1$Ih5pLngr$/boiJHqKbQm9AP/QQyq0b1



77
78
79
80
81
82
83
# File 'lib/lux.rb', line 77

def user_setup_cmd user = `id -nu`.strip
  [user, <<-COMMAND.gsub(/^\s*/,'').gsub(/\n/,' ; ')]
    useradd -M -d #{ENV['HOME']} -u #{Process.uid} -s #{ENV['SHELL']} #{user}
    mkdir -p /etc/sudoers.d
    echo "#{user} ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/#{user}
  COMMAND
end