Class: PhpFpmDocker::Application
- Inherits:
-
Object
- Object
- PhpFpmDocker::Application
- Defined in:
- lib/php_fpm_docker/application.rb
Overview
Application that is used as init script
Instance Attribute Summary collapse
-
#php_name ⇒ Object
readonly
Returns the value of attribute php_name.
Instance Method Summary collapse
- #help ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
-
#install ⇒ Object
rubocop:disable MethodLength, CyclomaticComplexity, PerceivedComplexity, LineLength, AbcSize.
- #reload ⇒ Object
- #restart ⇒ Object
- #run ⇒ Object
- #start ⇒ Object
- #status ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ Application
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/php_fpm_docker/application.rb', line 10 def initialize @name = 'php_fpm_docker' @longname = 'PHP FPM Docker Wrapper' # Create log dir if needed log_dir = Pathname.new('/var/log/php_fpm_docker') FileUtils.mkdir_p log_dir unless log_dir.directory? # Init logger log_file = log_dir.join('wrapper.log') @logger = Logger.new(log_file, 'daily') end |
Instance Attribute Details
#php_name ⇒ Object
Returns the value of attribute php_name.
9 10 11 |
# File 'lib/php_fpm_docker/application.rb', line 9 def php_name @php_name end |
Instance Method Details
#help ⇒ Object
225 226 227 228 229 230 |
# File 'lib/php_fpm_docker/application.rb', line 225 def help $stderr.puts( "Usage: #{php_name} $NAME {#{allowed_methods.join '|'}}" ) $stderr.puts(" #{php_name} install") end |
#install ⇒ Object
rubocop:disable MethodLength, CyclomaticComplexity, PerceivedComplexity, LineLength, AbcSize
22 23 24 25 26 27 28 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 69 70 71 72 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 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 |
# File 'lib/php_fpm_docker/application.rb', line 22 def install # rubocop:disable MethodLength, CyclomaticComplexity, PerceivedComplexity, LineLength, AbcSize # Get launcher name begin puts 'Enter name of the php docker launcher instance:' name = $stdin.gets.chomp fail 'Only use these characters: a-z0-9-_.' \ unless /^[a-z0-9\.\-_]+$/.match(name) rescue RuntimeError => e $stderr.puts(e.) retry end # Get image name begin puts 'Enter name of the docker image to use:' image = $stdin.gets.chomp fail 'Only use these characters: a-z0-9-_./:' \ unless /^[a-z0-9\.\-_\/\:]+$/.match(name) rescue RuntimeError => e $stderr.puts(e.) retry end bin_name = 'php_fpm_docker' bin_path = nil? # Path begin ENV['PATH'].split(':').each do |folder| path = File.join(folder, bin_name) if File.exist? path bin_path = path break end end if bin_path.nil? bin_path = File.(File.join( File.dirname(__FILE__), '..', '..', 'bin', bin_name )) end rescue RuntimeError => e $stderr.puts(e.) end puts image config_basepath = Pathname.new '/etc/php_fpm_docker/conf.d' config_dir = config_basepath.join name config_path = config_dir.join 'config.ini' config_pool = config_dir.join 'pools.d' config_content = "[main]\ndocker_image=\#{image}\n" initd_name = "php_fpm_docker_#{name}" initd_path = Pathname.new File.join('/etc/init.d/', initd_name) initd_content = "#!/bin/sh\n### BEGIN INIT INFO\n# Provides: \#{initd_name}\n# Required-Start: $remote_fs $network\n# Required-Stop: $remote_fs $network\n# Default-Start: 2 3 4 5\n# Default-Stop: 0 1 6\n# Short-Description: starts PHP Docker launcher \#{name}\n# Description: Starts The PHP Docker launcher daemon \#{name}\n### END INIT INFO\n\nNAME=\#{name}\nDAEMON=\#{bin_path}\n\n\ncase \"$1\" in\nstart)\n $DAEMON $NAME start\n ;;\nstop)\n $DAEMON $NAME stop\n ;;\nreload)\n $DAEMON $NAME reload\n ;;\nstatus)\n $DAEMON $NAME status\n ;;\nrestart|force-reload)\n $DAEMON $NAME restart\n ;;\n *)\n echo \"Usage: $0 {start|stop|status|restart|force-reload|reload}\" >&2\n exit 1\n ;;\nesac\n\n\n" puts "Creating init script in '#{initd_path}'" File.open(initd_path, 'w') do |file| file.write(initd_content) end File.chmod(0755, initd_path) unless config_dir.exist? puts "Creating config directory '#{config_dir}'" FileUtils.mkdir_p config_dir end unless config_pool.exist? puts "Creating pools directory '#{config_pool}'" FileUtils.mkdir_p config_pool end puts "Creating config file '#{config_path}'" File.open(config_path, 'w') do |file| file.write(config_content) end 0 end |
#reload ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/php_fpm_docker/application.rb', line 205 def reload print "Reloading #{full_name}: " $stdout.flush unless running? puts 'not running' return 0 end Process.kill('USR1', pid) puts 'done' 0 end |
#restart ⇒ Object
219 220 221 222 223 |
# File 'lib/php_fpm_docker/application.rb', line 219 def restart ret_val = stop return ret_val if ret_val != 0 start end |
#run ⇒ Object
232 233 234 235 236 237 238 239 |
# File 'lib/php_fpm_docker/application.rb', line 232 def run method_to_call = parse_arguments(ARGV) exit send(method_to_call) rescue RuntimeError => e @logger.warn(php_name) { e. } help exit 3 end |
#start ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/php_fpm_docker/application.rb', line 146 def start print "Starting #{full_name}: " $stdout.flush if running? puts 'already running' return 0 end # init l = Launcher.new php_name # run daemon self.pid = l.run puts "done (pid=#{pid})" 0 end |
#status ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/php_fpm_docker/application.rb', line 192 def status print "Status of #{full_name}: " $stdout.flush if running? puts 'running' return 0 else puts 'not running' return 3 end end |
#stop ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/php_fpm_docker/application.rb', line 165 def stop print "Stopping #{full_name}: " $stdout.flush unless running? puts 'not running' return 0 end Process.kill('TERM', pid) count = 0 while running? && count <= 50 sleep 0.2 count += 1 end if running? puts 'still running' return 1 else self.pid = nil puts 'stopped' return 0 end end |