Class: PicsolveDockerBuilder::Helpers::SshAuthForwarding

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb

Overview

Ruby class that forwards a ssh auth socket into a docker container

Instance Method Summary collapse

Methods included from Base

#base_dir, #config, #config_file, #config_path, #config_paths, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initializeSshAuthForwarding

Returns a new instance of SshAuthForwarding.



11
12
13
14
# File 'lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb', line 11

def initialize
  fail 'Environment var SSH_AUTH_SOCK not found, ssh forward impossible' \
    if env_ssh_auth_sock.nil?
end

Instance Method Details

#cleanup(dir = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb', line 44

def cleanup(dir = nil)
  # return if nothing to do
  if dir.nil?
    if @dir.nil?
      return
    else
      dir = @dir
      @dir = nil
    end
  end

  log.debug "Removing temporary dir '#{dir}'"
  FileUtils.rm_rf(dir)
end

#create_dirObject



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

def create_dir
  dir = generate_dir_path
  begin
    Dir.mkdir(dir, 0700)
    # ensure cleanup
    at_exit do
      cleanup(dir)
    end
  rescue Errno::EEXIST
    FileUtils.rm_rf(dir)
    log.warn "Directory '#{dir}' already exists, removing it"
    retry
  end

  log.debug \
    "Creating temporary dir '#{dir}' for mapping auth socket into docker"
  FileUtils.chmod 0700, dir
  FileUtils.ln env_ssh_auth_sock, File.join(dir, 'ssh_auth_sock')
  dir
end

#dirObject



59
60
61
# File 'lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb', line 59

def dir
  @dir ||= create_dir
end

#env_ssh_auth_sockObject



67
68
69
# File 'lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb', line 67

def env_ssh_auth_sock
  ENV[env_var_name]
end

#env_var_nameObject



63
64
65
# File 'lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb', line 63

def env_var_name
  'SSH_AUTH_SOCK'
end

#generate_dir_pathObject



16
17
18
19
20
21
# File 'lib/picsolve_docker_builder/helpers/ssh_auth_forwarding.rb', line 16

def generate_dir_path
  File.join(
    File.dirname(env_ssh_auth_sock),
    "docker_builder_temp_#{SecureRandom.hex(2)}"
  )
end