Module: DockerSocketModule

Included in:
Takelage::DockerContainer, Takelage::DockerSocket
Defined in:
lib/takelage/docker/socket/module.rb

Overview

takelage docker socket module

Instance Method Summary collapse

Instance Method Details

#_get_socket_pathsObject

get socket paths



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/takelage/docker/socket/module.rb', line 100

def _get_socket_paths
  cmd_gpgconf_listdirs =
      config.active['cmd_docker_get_socket_paths_docker_socket_gpgconf']

  stdout_str = run cmd_gpgconf_listdirs

  stdout_str.split(/\n+/).each do |gpg_path|
    @sockets.each do |socket, socket_config|
      gpg_socket = gpg_path.split(':')
      if gpg_socket[0] == socket.to_s
        @sockets[socket][:path] = gpg_socket[1]
      end
    end
  end
end

#_get_socket_start_commands(sockets_up) ⇒ Object

get socket start commands sockets_up is a boolean which defines if the sockets need to be up to be included in the resulting array of socket start commands



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/takelage/docker/socket/module.rb', line 73

def _get_socket_start_commands sockets_up
  cmds_start_socket = []

  # loop over sockets
  @sockets.each do |socket, socket_config|
    cmd_start_socket =
        config.active['cmd_docker_get_socket_start_commands_docker_socket_start'] % {
        host: socket_config[:host],
        port: socket_config[:port],
        path: socket_config[:path],
    }

    if sockets_up
      if _socket_up? socket, socket_config
        cmds_start_socket << cmd_start_socket
      end
    else
      unless _socket_up? socket, socket_config
        cmds_start_socket << cmd_start_socket
      end
    end
  end

  cmds_start_socket
end

#_socket_up?(socket, socket_config) ⇒ Boolean

check if a socket is available but trying to connect to it via TCP

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/takelage/docker/socket/module.rb', line 118

def _socket_up? socket, socket_config
  host = socket_config[:host]
  port = socket_config[:port]
  path = socket_config[:path]

  error_message = "failed to connect to " +
      "socket \"#{socket}\" " +
      "using host \"#{host}\", " +
      "port \"#{port}\", " +
      "path \"#{path}\""

  # check if socket is available
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new host, port
        s.close
        log.debug "Socket \"#{socket}\" available"
        return true
      rescue Errno::ECONNREFUSED
        log.debug "Connection refused: #{error_message}"
        return false
      rescue Errno::EHOSTUNREACH
        log.debug "Host unreachable: #{error_message}"
        return false
      rescue SocketError
        log.debug "Socket error: #{error_message}"
        return false
      end
    end
  rescue Timeout::Error
    log.debug "Timeout: #{error_message}"
    return false
  end
end

#docker_socket_startObject

Backend method for docker socket start.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/takelage/docker/socket/module.rb', line 5

def docker_socket_start
  log.debug 'Starting sockets for docker container'

  return false unless docker_check_running

  cmds_start_socket = _get_socket_start_commands sockets_up = false

  unless cmds_start_socket.empty?
    log.debug 'Request sudo so that subsequent background tasks run without delay'

    cmd_sudo_true =
        config.active['cmd_docker_socket_start_sudo_true']

    run cmd_sudo_true
  end

  cmds_start_socket.each do |cmd_start_socket|
    run_and_fork cmd_start_socket
  end

  true
end

#docker_socket_stopObject

Backend method for docker socket stop.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/takelage/docker/socket/module.rb', line 29

def docker_socket_stop
  log.debug 'Stopping sockets for docker container'

  return false unless docker_check_running

  # get process list
  # assuming format: "pid command"
  cmd_ps =
      config.active['cmd_docker_socket_stop_docker_socket_ps']

  stdout_str = run cmd_ps

  cmds_start_socket = _get_socket_start_commands sockets_up = true

  # loop over process list
  stdout_str.split(/\n+/).each do |process|

    # split processes in process id and process command
    pid_command = process.strip.split(/ /, 2)
    pid = pid_command[0]
    command = pid_command[1]

    # loop over socket start commands
    cmds_start_socket.each do |cmd_start_socket|

      if command == cmd_start_socket
        log.debug "Killing PID #{pid}"

        cmd_kill =
            config.active['cmd_docker_socket_stop_docker_socket_kill'] % {
                pid: pid
            }

        run cmd_kill
      end
    end
  end

  true
end