class PhpFpmDocker::Pool

A pool represent a single isolated PHP web instance

Attributes

enabled[R]

Public Class Methods

new(opts) click to toggle source
# File lib/php_fpm_docker/pool.rb, line 11
def initialize(opts)
  @config = opts[:config]
  @launcher = opts[:launcher]
  @name = opts[:name]
end

Public Instance Methods

bind_mounts() click to toggle source

Find out bind mount paths

# 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
check() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 154
def check
  return unless @enabled && !container_running?
  stop
  start
end
container_name() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 141
def container_name
  @container_name ||= "#{@name}_#{SecureRandom.hex[0..11]}"
end
container_running?() click to toggle source
# 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_opts() click to toggle source
# 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_opts() click to toggle source
# 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
gid() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 99
def gid
  gid_from_group(@config['group'])
end
gid_from_group(group) click to toggle source
# File lib/php_fpm_docker/pool.rb, line 83
def gid_from_group(group)
  Etc.getgrnam(group).gid
end
listen_gid() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 91
def listen_gid
  gid_from_group(@config['listen.group'])
end
listen_uid() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 87
def listen_uid
  uid_from_user(@config['listen.owner'])
end
open_base_dirs() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 67
def open_base_dirs
  @config['php_admin_value[open_basedir]'].split(':')
end
php_command() click to toggle source
# 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_dir() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 71
def root_dir
  max(valid_web_paths)
end
socket_dir() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 75
def socket_dir
  File.dirname(@config['listen'])
end
spawn_command() click to toggle source

Build the spawn command

# 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
start() click to toggle source
# 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
stop() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 160
def stop
  @enabled = false
  @container.delete(force: true) unless @container.nil?
end
to_s() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 165
def to_s
  "<Pool:#{@name}>"
end
uid() click to toggle source
# File lib/php_fpm_docker/pool.rb, line 95
def uid
  uid_from_user(@config['user'])
end
uid_from_user(user) click to toggle source
# File lib/php_fpm_docker/pool.rb, line 79
def uid_from_user(user)
  Etc.getpwnam(user).uid
end
valid_web_paths() click to toggle source
# 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_regex() click to toggle source

Return web path regexs

# 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