Class: PhpFpmDocker::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/php_fpm_docker/pool.rb

Overview

A pool represent a single isolated PHP web instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Pool

Returns a new instance of Pool.



11
12
13
14
15
# File 'lib/php_fpm_docker/pool.rb', line 11

def initialize(opts)
  @config = opts[:config]
  @launcher = opts[:launcher]
  @name = opts[:name]
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



10
11
12
# File 'lib/php_fpm_docker/pool.rb', line 10

def enabled
  @enabled
end

Instance Method Details

#bind_mountsObject

Find out bind mount paths



60
61
62
63
64
65
# File 'lib/php_fpm_docker/pool.rb', line 60

def bind_mounts
  ret_val = @launcher.bind_mounts
  ret_val << File.dirname(@config['listen'])
  ret_val += valid_web_paths
  ret_val.uniq
end

#checkObject



154
155
156
157
158
# File 'lib/php_fpm_docker/pool.rb', line 154

def check
  return unless @enabled && !container_running?
  stop
  start
end

#container_nameObject



141
142
143
# File 'lib/php_fpm_docker/pool.rb', line 141

def container_name
  @container_name ||= "#{@name}_#{SecureRandom.hex[0..11]}"
end

#container_running?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
# File 'lib/php_fpm_docker/pool.rb', line 145

def container_running?
  return false if @container.nil?
  begin
    return @container.info['State']['Running']
  rescue NoMethodError
    return false
  end
end

#docker_create_optsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/php_fpm_docker/pool.rb', line 17

def docker_create_opts
  volumes = {}
  bind_mounts.each do |d|
    volumes[d] = {}
  end

  {
    'name' => container_name,
    'Image' => @launcher.docker_image.id,
    'Volumes' => volumes,
    'WorkingDir' => '/'
  }
end

#docker_start_optsObject



31
32
33
34
35
36
37
38
# File 'lib/php_fpm_docker/pool.rb', line 31

def docker_start_opts
  binds = bind_mounts.map do |d|
    "#{d}:#{d}"
  end
  {
    'Binds' => binds
  }
end

#gidObject



99
100
101
# File 'lib/php_fpm_docker/pool.rb', line 99

def gid
  gid_from_group(@config['group'])
end

#gid_from_group(group) ⇒ Object



83
84
85
# File 'lib/php_fpm_docker/pool.rb', line 83

def gid_from_group(group)
  Etc.getgrnam(group).gid
end

#listen_gidObject



91
92
93
# File 'lib/php_fpm_docker/pool.rb', line 91

def listen_gid
  gid_from_group(@config['listen.group'])
end

#listen_uidObject



87
88
89
# File 'lib/php_fpm_docker/pool.rb', line 87

def listen_uid
  uid_from_user(@config['listen.owner'])
end

#open_base_dirsObject



67
68
69
# File 'lib/php_fpm_docker/pool.rb', line 67

def open_base_dirs
  @config['php_admin_value[open_basedir]'].split(':')
end

#php_commandObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/php_fpm_docker/pool.rb', line 118

def php_command
  admin_options = []
  @config.each_key do |key|
    m = /^php_admin_value\[([^\]]+)\]$/.match(key)
    next if m.nil?

    admin_options << '-d'
    admin_options << "#{m[1]}=#{@config[key]}"

  end

  [@launcher.php_cmd_path] + admin_options
end

#root_dirObject



71
72
73
# File 'lib/php_fpm_docker/pool.rb', line 71

def root_dir
  max(valid_web_paths)
end

#socket_dirObject



75
76
77
# File 'lib/php_fpm_docker/pool.rb', line 75

def socket_dir
  File.dirname(@config['listen'])
end

#spawn_commandObject

Build the spawn command



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/php_fpm_docker/pool.rb', line 104

def spawn_command
  [
    @launcher.spawn_cmd_path,
    '-s', @config['listen'],
    '-U', listen_uid.to_s,
    '-G', listen_gid.to_s,
    '-M', '0660',
    '-u', uid.to_s,
    '-g', gid.to_s,
    '-C', '4',
    '-n'
  ]
end

#startObject



132
133
134
135
136
137
138
139
# File 'lib/php_fpm_docker/pool.rb', line 132

def start
  @enabled = true
  create_opts = docker_create_opts
  create_opts['Cmd'] = spawn_command + ['--'] + php_command

  @container = Docker::Container.create(create_opts)
  @container.start(docker_start_opts)
end

#stopObject



160
161
162
163
# File 'lib/php_fpm_docker/pool.rb', line 160

def stop
  @enabled = false
  @container.delete(force: true) unless @container.nil?
end

#to_sObject



165
166
167
# File 'lib/php_fpm_docker/pool.rb', line 165

def to_s
  "<Pool:#{@name}>"
end

#uidObject



95
96
97
# File 'lib/php_fpm_docker/pool.rb', line 95

def uid
  uid_from_user(@config['user'])
end

#uid_from_user(user) ⇒ Object



79
80
81
# File 'lib/php_fpm_docker/pool.rb', line 79

def uid_from_user(user)
  Etc.getpwnam(user).uid
end

#valid_web_pathsObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/php_fpm_docker/pool.rb', line 48

def valid_web_paths
  ret_val = []
  open_base_dirs.map do |dir|
    web_path_regex.each do |regex|
      m = regex.match(dir)
      ret_val << m[1] unless m.nil?
    end
  end
  ret_val
end

#web_path_regexObject

Return web path regexs



41
42
43
44
45
46
# File 'lib/php_fpm_docker/pool.rb', line 41

def web_path_regex
  [
    %r{(^#{@launcher.web_path}/clients/client\d+/web\d+)},
    %r{(^#{@launcher.web_path}/[^/]+)/web$}
  ]
end