Class: PhpFpmDocker::Pool
- Inherits:
-
Object
- Object
- PhpFpmDocker::Pool
- Defined in:
- lib/php_fpm_docker/pool.rb
Overview
A pool represent a single isolated PHP web instance
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
Instance Method Summary collapse
-
#bind_mounts ⇒ Object
Find out bind mount paths.
- #check ⇒ Object
- #container_name ⇒ Object
- #container_running? ⇒ Boolean
- #docker_create_opts ⇒ Object
- #docker_start_opts ⇒ Object
- #gid ⇒ Object
- #gid_from_group(group) ⇒ Object
-
#initialize(opts) ⇒ Pool
constructor
A new instance of Pool.
- #listen_gid ⇒ Object
- #listen_uid ⇒ Object
- #open_base_dirs ⇒ Object
- #php_command ⇒ Object
- #root_dir ⇒ Object
- #socket_dir ⇒ Object
-
#spawn_command ⇒ Object
Build the spawn command.
- #start ⇒ Object
- #stop ⇒ Object
- #to_s ⇒ Object
- #uid ⇒ Object
- #uid_from_user(user) ⇒ Object
- #valid_web_paths ⇒ Object
-
#web_path_regex ⇒ Object
Return web path regexs.
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
#enabled ⇒ Object (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_mounts ⇒ Object
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 |
#check ⇒ Object
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_name ⇒ Object
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
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_opts ⇒ Object
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_opts ⇒ Object
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 |
#gid ⇒ Object
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_gid ⇒ Object
91 92 93 |
# File 'lib/php_fpm_docker/pool.rb', line 91 def listen_gid gid_from_group(@config['listen.group']) end |
#listen_uid ⇒ Object
87 88 89 |
# File 'lib/php_fpm_docker/pool.rb', line 87 def listen_uid uid_from_user(@config['listen.owner']) end |
#open_base_dirs ⇒ Object
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_command ⇒ Object
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 = [] @config.each_key do |key| m = /^php_admin_value\[([^\]]+)\]$/.match(key) next if m.nil? << '-d' << "#{m[1]}=#{@config[key]}" end [@launcher.php_cmd_path] + end |
#root_dir ⇒ Object
71 72 73 |
# File 'lib/php_fpm_docker/pool.rb', line 71 def root_dir max(valid_web_paths) end |
#socket_dir ⇒ Object
75 76 77 |
# File 'lib/php_fpm_docker/pool.rb', line 75 def socket_dir File.dirname(@config['listen']) end |
#spawn_command ⇒ Object
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 |
#start ⇒ Object
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 |
#stop ⇒ Object
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_s ⇒ Object
165 166 167 |
# File 'lib/php_fpm_docker/pool.rb', line 165 def to_s "<Pool:#{@name}>" end |
#uid ⇒ Object
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_paths ⇒ Object
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_regex ⇒ Object
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 |