lib/capistrano/tasks/nginx-unit.rake in capistrano-nginx-unit-0.2 vs lib/capistrano/tasks/nginx-unit.rake in capistrano-nginx-unit-0.3

- old
+ new

@@ -20,38 +20,37 @@ on release_roles(fetch(:nginx_unit_roles)) do pid_file = fetch(:nginx_unit_pid) if test("[ -e #{pid_file} ] && kill -0 `cat #{pid_file}`") info "NGINX Unit is already started" else - execute :sudo, :unitd, - "--pid #{pid_file}", + execute :sudo, :unitd, + "--pid #{pid_file}", "--control unix:#{fetch(:nginx_unit_control_sock)}", fetch(:nginx_unit_options) end end end # If you want to apply new code when deployed, # please invoke this task after deploy:published - desc "Set listener and application configuration for NGINX Unit" - task :configure do + desc "Attach listener and application configuration to NGINX Unit" + task :attach do invoke "nginx_unit:start" - invoke "nginx_unit:configure_app" - invoke "nginx_unit:configure_listener" + invoke "nginx_unit:attach_app" + invoke "nginx_unit:attach_listener" end - desc "Set listener configuration for NGINX Unit" - task :configure_listener do + desc "Attach listener configuration to NGINX Unit" + task :attach_listener do on release_roles(fetch(:nginx_unit_roles)) do listener_json = JSON.generate("application" => fetch(:application)) - - configure_nginx_unit("/listeners/#{fetch(:nginx_unit_listen)}", listener_json) + control_nginx_unit(:put, path: "/listeners/#{fetch(:nginx_unit_listen)}", json: listener_json) end end - desc "Set application configuration for NGINX Unit" - task :configure_app do + desc "Attach application configuration to NGINX Unit" + task :attach_app do on release_roles(fetch(:nginx_unit_roles)) do |role| released_dir = capture(:readlink, "-f", current_path) raise "Doesn't exist released dir: #{released_dir}" unless test("[ -d #{released_dir} ]") app_json = JSON.generate({ @@ -60,29 +59,43 @@ user: fetch(:nginx_unit_user) || role.user, group: fetch(:nginx_unit_group) || role.user, script: File.join(released_dir, fetch(:nginx_unit_script)) }) - info "application config: #{app_json}" - configure_nginx_unit("/applications/#{fetch(:nginx_unit_app_name)}", app_json) + control_nginx_unit(:put, path: "/applications/#{fetch(:nginx_unit_app_name)}", json: app_json) end end - # Send request to NGINX Unit control socket - def configure_nginx_unit(path, json) - info "#{path} => #{json}" + desc "Detach listener and application configuration from NGINX Unit" + task :detach do + invoke "nginx_unit:detach_listener" + invoke "nginx_unit:detach_app" + end - res = JSON.parse(capture(:curl, - "-X PUT", - "-d '#{json}'", + desc "Detach listener configuration from NGINX Unit" + task :detach_listener do + on release_roles(fetch(:nginx_unit_roles)) do + control_nginx_unit(:delete, path: "/listeners/#{fetch(:nginx_unit_listen)}") + end + end + + desc "Detach application configuration from NGINX Unit" + task :detach_app do + on release_roles(fetch(:nginx_unit_roles)) do + control_nginx_unit(:delete, path: "/applications/#{fetch(:nginx_unit_app_name)}") + end + end + + # Send request to NGINX Unit control socket + def control_nginx_unit(method, path: "", json: nil) + args = [ + "-fs", + "-X #{method.to_s.upcase}", "--unix-socket #{fetch(:nginx_unit_control_sock)}", "'http://localhost/#{path}'" - )) + ] - if res["error"] - fatal res - raise "Faild to configure" - else - info res - end + args << "-d '#{json}'" if json + + execute :curl, *args end end