Class: PhpFpmDocker::Launcher
- Inherits:
-
Object
- Object
- PhpFpmDocker::Launcher
- Defined in:
- lib/php_fpm_docker/launcher.rb
Overview
Represent a single docker image
Instance Attribute Summary collapse
-
#docker_image ⇒ Object
readonly
rubocop:disable ClassLength.
-
#php_cmd_path ⇒ Object
readonly
rubocop:disable ClassLength.
-
#spawn_cmd_path ⇒ Object
readonly
rubocop:disable ClassLength.
Instance Method Summary collapse
-
#bind_mounts ⇒ Object
Get neccessary bind mounts.
- #check_pools ⇒ Object
- #check_pools_n ⇒ Object
- #create_missing_pool_objects ⇒ Object
- #docker_opts ⇒ Object
- #fork_run ⇒ Object
-
#initialize(name) ⇒ Launcher
constructor
rubocop:disable MethodLength.
- #move_existing_pool_objects ⇒ Object
-
#parse_config ⇒ Object
Parse the config file for all pools.
- #pools_action(pools, pools_hashes, action) ⇒ Object
-
#pools_config_content_from_file(config_path) ⇒ Object
Reads config sections from a inifile.
-
#pools_config_contents ⇒ Object
Merges config sections form all inifiles.
-
#pools_from_config ⇒ Object
Hashes configs to detect changes.
- #reload_pools(pools = nil) ⇒ Object
- #run ⇒ Object
- #start_pools ⇒ Object
- #stop_pools ⇒ Object
- #test ⇒ Object
- #test_directories ⇒ Object
-
#test_docker_cmd(cmd) ⇒ Object
Docker init.
-
#test_docker_image ⇒ Object
Testing the docker image if i can be used.
- #to_s ⇒ Object
-
#web_path ⇒ Object
Get webs base path.
Constructor Details
#initialize(name) ⇒ Launcher
rubocop:disable MethodLength
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/php_fpm_docker/launcher.rb', line 14 def initialize(name) # rubocop:disable MethodLength @name = name # Create log dir if needed log_dir = Pathname.new('/var/log/php_fpm_docker') FileUtils.mkdir_p log_dir unless log_dir.directory? # Open logger @logger = Logger.new(log_dir.join("#{name}.log"), 'daily') @logger.info(to_s) { 'init' } test end |
Instance Attribute Details
#docker_image ⇒ Object (readonly)
rubocop:disable ClassLength
12 13 14 |
# File 'lib/php_fpm_docker/launcher.rb', line 12 def docker_image @docker_image end |
#php_cmd_path ⇒ Object (readonly)
rubocop:disable ClassLength
12 13 14 |
# File 'lib/php_fpm_docker/launcher.rb', line 12 def php_cmd_path @php_cmd_path end |
#spawn_cmd_path ⇒ Object (readonly)
rubocop:disable ClassLength
12 13 14 |
# File 'lib/php_fpm_docker/launcher.rb', line 12 def spawn_cmd_path @spawn_cmd_path end |
Instance Method Details
#bind_mounts ⇒ Object
Get neccessary bind mounts
164 165 166 |
# File 'lib/php_fpm_docker/launcher.rb', line 164 def bind_mounts @ini_file[:main]['bind_mounts'].split(',') || [] end |
#check_pools ⇒ Object
117 118 119 |
# File 'lib/php_fpm_docker/launcher.rb', line 117 def check_pools 'do nothing' end |
#check_pools_n ⇒ Object
121 122 123 |
# File 'lib/php_fpm_docker/launcher.rb', line 121 def check_pools_n pools_action(@pools, @pools.keys, :check) end |
#create_missing_pool_objects ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/php_fpm_docker/launcher.rb', line 77 def create_missing_pool_objects return if @pools.nil? return if @pools_old.nil? (@pools.keys & @pools_old.keys).each do |hash| @pools[hash][:object] = @pools_old[hash][:object] end end |
#docker_opts ⇒ Object
198 199 200 201 202 |
# File 'lib/php_fpm_docker/launcher.rb', line 198 def docker_opts { 'Image' => @docker_image.id } end |
#fork_run ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/php_fpm_docker/launcher.rb', line 52 def fork_run Signal.trap('USR1') do @logger.info(to_s) { 'Signal USR1 received reloading now' } reload_pools end Signal.trap('TERM') do @logger.info(to_s) { 'Signal TERM received stopping me now' } stop_pools exit 0 end Kernel.loop do check_pools sleep 1 end end |
#move_existing_pool_objects ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/php_fpm_docker/launcher.rb', line 85 def move_existing_pool_objects return if @pools.nil? @pools.keys.each do |hash| pool = @pools[hash] # skip if there's already an object next if pool.key?(:object) pool[:object] = Pool.new( config: pool[:config], name: pool[:name], launcher: self ) end end |
#parse_config ⇒ Object
Parse the config file for all pools
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/php_fpm_docker/launcher.rb', line 174 def parse_config # rubocop:disable MethodLength # Test for file usability fail "Config file '#{@config_path}' not found"\ unless @config_path.file? fail "Config file '#{@config_path}' not readable"\ unless @config_path.readable? @ini_file = IniFile.load(@config_path) begin docker_image = @ini_file[:main]['docker_image'] @docker_image = Docker::Image.get(docker_image) @logger.info(to_s) do "Docker image id=#{@docker_image.id[0..11]} name=#{docker_image}" end rescue NoMethodError raise 'No docker_image in section main in config found' rescue Docker::Error::NotFoundError raise "Docker_image '#{docker_image}' not found" rescue Excon::Errors::SocketError => e raise "Docker connection could not be established: #{e.message}" end end |
#pools_action(pools, pools_hashes, action) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/php_fpm_docker/launcher.rb', line 125 def pools_action(pools, pools_hashes, action) = '' if pools_hashes.length > 0 << "Pools to #{action}: " << pools_hashes.map { |p| pools[p][:name] }.join(', ') pools_hashes.each do |pool_hash| pool = pools[pool_hash] begin pool[:object].send(action) rescue => e @logger.warn(pool[:object].to_s) do "Failed to #{action}: #{e.message}" end end end else << "No pools to #{action}" end @logger.info(to_s) { } end |
#pools_config_content_from_file(config_path) ⇒ Object
Reads config sections from a inifile
205 206 207 208 209 210 211 212 213 |
# File 'lib/php_fpm_docker/launcher.rb', line 205 def pools_config_content_from_file(config_path) ini_file = IniFile.load(config_path) ret_val = [] ini_file.each_section do |section| ret_val << [section, ini_file[section]] end ret_val end |
#pools_config_contents ⇒ Object
Merges config sections form all inifiles
216 217 218 219 220 221 222 223 224 |
# File 'lib/php_fpm_docker/launcher.rb', line 216 def pools_config_contents ret_val = [] # Loop over Dir[@pools_directory.join('*.conf').to_s].each do |config_path| ret_val += pools_config_content_from_file(config_path) end ret_val end |
#pools_from_config ⇒ Object
Hashes configs to detect changes
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/php_fpm_docker/launcher.rb', line 227 def pools_from_config configs = {} pools_config_contents.each do |section| # Hash section name and content d = Digest::SHA2.new(256) hash = d.reset.update(section[0]).update(section[1].to_s).to_s configs[hash] = { name: section[0], config: section[1] } end configs end |
#reload_pools(pools = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/php_fpm_docker/launcher.rb', line 99 def reload_pools(pools = nil) @pools_old = @pools if pools.nil? @pools = pools_from_config else @pools = pools end move_existing_pool_objects create_missing_pool_objects # Pools to stop pools_action(@pools_old, @pools_old.keys - @pools.keys, :stop) # Pools to start pools_action(@pools, @pools.keys - @pools_old.keys, :start) end |
#run ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/php_fpm_docker/launcher.rb', line 42 def run start_pools pid = fork do fork_run end Process.detach(pid) pid end |
#start_pools ⇒ Object
68 69 70 71 |
# File 'lib/php_fpm_docker/launcher.rb', line 68 def start_pools @pools = {} reload_pools end |
#stop_pools ⇒ Object
73 74 75 |
# File 'lib/php_fpm_docker/launcher.rb', line 73 def stop_pools reload_pools({}) end |
#test ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/php_fpm_docker/launcher.rb', line 28 def test test_directories # Parse config parse_config # Test docker image test_docker_image rescue RuntimeError => e @logger.fatal(to_s) { "Error while init: #{e.message}" } exit 1 end |
#test_directories ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/php_fpm_docker/launcher.rb', line 146 def test_directories # Get config dirs and paths @conf_directory = Pathname.new('/etc/php_fpm_docker/conf.d').join(@name) fail "Config directory '#{@conf_directory}' not found" \ unless @conf_directory.directory? @pools_directory = @conf_directory.join('pools.d') fail "Pool directory '#{@pools_directory}' not found" \ unless @pools_directory.directory? @config_path = @conf_directory.join('config.ini') end |
#test_docker_cmd(cmd) ⇒ Object
Docker init
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/php_fpm_docker/launcher.rb', line 244 def test_docker_cmd(cmd) # rubocop:disable MethodLength # retry this block 3 times tries ||= 3 opts = docker_opts opts['Cmd'] = cmd dict = {} # Set timeout Docker.[:read_timeout] = 2 cont = Docker::Container.create(opts) cont.start output = cont.attach dict[:ret_val] = cont.wait(5)['StatusCode'] cont.delete(force: true) dict[:stdout] = output[0].first dict[:stderr] = output[1].first # Set timeout Docker.[:read_timeout] = 15 @logger.debug(to_s) do "cmd=#{cmd.join(' ')} ret_val=#{dict[:ret_val]}" \ " stdout=#{dict[:stdout]} stderr=#{dict[:stderr]}" end dict rescue Docker::Error::TimeoutError => e if (tries -= 1) > 0 cont.delete(force: true) if cont.nil? @logger.debug(to_s) { 'ran into timeout retry' } retry end raise e end |
#test_docker_image ⇒ Object
Testing the docker image if i can be used
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/php_fpm_docker/launcher.rb', line 283 def test_docker_image # rubocop:disable MethodLength # Test possible php commands ['php-cgi', 'php5-cgi', 'php', 'php5'].each do |php_cmd| result = test_docker_cmd [:which, php_cmd] next unless result[:ret_val] == 0 php_cmd_path = result[:stdout].strip result = test_docker_cmd [php_cmd_path, '-v'] next unless result[:ret_val] == 0 php_version_re = /PHP [A-Za-z0-9\.\-\_]+ \(cgi-fcgi\)/ next if php_version_re.match(result[:stdout]).nil? @php_cmd_path = php_cmd_path break end fail 'No usable fast-cgi enabled php found in image' if @php_cmd_path.nil? # Test if spawn-fcgi exists result = test_docker_cmd [:which, 'spawn-fcgi'] fail 'No usable spawn-fcgi found in image' unless result[:ret_val] == 0 @spawn_cmd_path = result[:stdout].strip end |
#to_s ⇒ Object
159 160 161 |
# File 'lib/php_fpm_docker/launcher.rb', line 159 def to_s "<Launcher:#{@name}>" end |
#web_path ⇒ Object
Get webs base path
169 170 171 |
# File 'lib/php_fpm_docker/launcher.rb', line 169 def web_path Pathname.new(@ini_file[:main]['web_path'] || '/var/www') end |