Class: PoiseService::ServiceProviders::Dummy

Inherits:
Base
  • Object
show all
Defined in:
lib/poise_service/service_providers/dummy.rb

Overview

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#action_reload ⇒ Object

Since:

  • 1.0.0



127
128
129
130
131
132
# File 'lib/poise_service/service_providers/dummy.rb', line 127

def action_reload
  return if options['never_reload']
  return unless pid
  Chef::Log.debug("[#{new_resource}] Reloading with #{new_resource.reload_signal}. Current PID is #{pid.inspect}.")
  Process.kill(new_resource.reload_signal, pid)
end

#action_restart ⇒ Object

Since:

  • 1.0.0



119
120
121
122
123
124
125
# File 'lib/poise_service/service_providers/dummy.rb', line 119

def action_restart
  return if options['never_restart']
  action_stop
  # Give things a moment to stop before we try starting again.
  sleep(options['restart_delay'])
  action_start
end

#action_start ⇒ Object

Since:

  • 1.0.0



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
# File 'lib/poise_service/service_providers/dummy.rb', line 36

def action_start
  return if options['never_start']
  return if pid
  Chef::Log.debug("[#{new_resource}] Starting #{new_resource.command}")
  # Clear the pid file if it exists.
  ::File.unlink(pid_file) if ::File.exist?(pid_file)
  if Process.fork
    # Parent, wait for the final child to write the pid file.
    now = Time.now
    until ::File.exist?(pid_file)
      sleep(1)
      # After 30 seconds, show output at a higher level to avoid too much
      # confusing on failed process launches.
      if Time.now - now <= 30
        Chef::Log.debug("[#{new_resource}] Waiting for PID file")
      else
        Chef::Log.warning("[#{new_resource}] Waiting for PID file at #{pid_file} to be created")
      end
    end
  else
    # :nocov:
    begin
      Chef::Log.debug("[#{new_resource}] Forked")
      # First child, daemonize and go to town. This handles multi-fork,
      # setsid, and shutting down stdin/out/err.
      Process.daemon(true)
      Chef::Log.debug("[#{new_resource}] Daemonized")
      # Daemonized, set up process environment.
      Dir.chdir(new_resource.directory)
      Chef::Log.debug("[#{new_resource}] Directory changed to #{new_resource.directory}")
      ENV['HOME'] = Dir.home(new_resource.user)
      new_resource.environment.each do |key, val|
        ENV[key.to_s] = val.to_s
      end
      Chef::Log.debug("[#{new_resource}] Process environment configured")
      # Make sure to open the output file and write the pid file before we
      # drop privs.
      output = ::File.open(output_file, 'ab')
      IO.write(pid_file, Process.pid)
      Chef::Log.debug("[#{new_resource}] PID #{Process.pid} written to #{pid_file}")
      ent = Etc.getpwnam(new_resource.user)
      if Process.euid != ent.uid || Process.egid != ent.gid
        Process.initgroups(ent.name, ent.gid)
        Process::GID.change_privilege(ent.gid) if Process.egid != ent.gid
        Process::UID.change_privilege(ent.uid) if Process.euid != ent.uid
        Chef::Log.debug("[#{new_resource}] Changed privs to #{new_resource.user} (#{ent.uid}:#{ent.gid})")
      end
      # Log the command. Happens before ouput redirect or this ends up in the file.
      Chef::Log.debug("[#{new_resource}] Execing #{new_resource.command}")
      # Set up output logging.
      Chef::Log.debug("[#{new_resource}] Logging output to #{output_file}")
      $stdout.reopen(output)
      $stdout.sync = true
      $stderr.reopen(output)
      $stderr.sync = true
      $stdout.write("#{Time.now} Starting #{new_resource.command}")
      # Split the command so we don't get an extra sh -c.
      Kernel.exec(*Shellwords.split(new_resource.command))
      # Just in case, bail out.
      $stdout.reopen(STDOUT)
      $stderr.reopen(STDERR)
      Chef::Log.debug("[#{new_resource}] Exec failed, bailing out.")
      exit!
    rescue Exception => e
      # Welp, we tried.
      $stdout.reopen(STDOUT)
      $stderr.reopen(STDERR)
      Chef::Log.error("[#{new_resource}] Error during process spawn: #{e}")
      exit!
    end
    # :nocov:
  end
  Chef::Log.debug("[#{new_resource}] Started.")
end

#action_stop ⇒ Object

Since:

  • 1.0.0



111
112
113
114
115
116
117
# File 'lib/poise_service/service_providers/dummy.rb', line 111

def action_stop
  return if options['never_stop']
  return unless pid
  Chef::Log.debug("[#{new_resource}] Stopping with #{new_resource.stop_signal}. Current PID is #{pid.inspect}.")
  Process.kill(new_resource.stop_signal, pid)
  ::File.unlink(pid_file)
end

#pid ⇒ Object

Since:

  • 1.0.0



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/poise_service/service_providers/dummy.rb', line 134

def pid
  return nil unless ::File.exist?(pid_file)
  pid = IO.read(pid_file).to_i
  begin
    # Check if the PID is running.
    Process.kill(0, pid)
    pid
  rescue Errno::ESRCH
    nil
  end
end