require 'highline/import' require 'uri' require 'socket' module Lux # Determine the IPv4 address of the current Docker host # Used to avoid long reverse DNS lookups on .local names # Returns either nil if the lookup failed, or a triplet: # - the IPv4 address as a string # - a URI object # - a string saying which variable was used, 'URL' or 'HOST' # Note that DOCKER_URL is used by the docker-api client, but # as Docker uses the misleading DOCKER_HOST we fallback to that. # def self.dockerip vars = ['DOCKER_URL', 'DOCKER_HOST'] var, url = vars.map{|v| [v, ENV[v]]}.find{|d| d[1]} return nil unless var uri = URI.parse(url) name = uri.host addrs = Socket.getaddrinfo(name, nil, :INET, :STREAM, 0, 0, false) rescue [] name = addrs.first[3] if addrs.size > 0 return name, var, uri end # Get the current list of images and make a guess at which one it is... # def self.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 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 else matching_image = image end return matching_image end # Return two elements: # - user name (defaults to current user), and # - a bash script setup command # def self.user_setup_cmd user = `id -nu`.strip [user, <<-COMMAND.gsub(/^\s*/,'').gsub(/\n/,' ; ')] uid=$(echo $(stat -c %u:%g #{ENV['HOME']}) | cut -d: -f2) useradd -M -d #{ENV['HOME']} -u $uid -s #{ENV['SHELL']} #{user} echo "#{user} ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/#{user} COMMAND end def self.die msg, rc = 1 HighLine.new(STDIN, STDERR).say HighLine.color(msg, HighLine::RED) exit(rc) end end # vim: ft=ruby sts=2 sw=2 ts=8