Module: Jerbil::Support

Defined in:
lib/jerbil/support.rb

Overview

Set of utilities to assist in managing Jerbil services. Used largely by JerbilService modules and classes

Class Method Summary collapse

Class Method Details

.create_private_key(name, env, key_dir) ⇒ String

create a private key, save it to a key file in the given directory and return it

Private keys should be created by the daemon start script and used to supervise the service (e.g. stop)

Parameters:

  • name (Symbol)

    of the service

  • env (Symbol)

    the services is running in

  • key_dir (String)

    path to directory where key file is to be written

Returns:

  • (String)

    the private key

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jerbil/support.rb', line 86

def Support.create_private_key(name,  env, key_dir)
  key = Digest::SHA1.hexdigest(Time.now.to_s + rand(12341234).to_s)[1..20]
  key_file = "#{key_dir}/#{name.to_s}-#{env}.asc"
  FileUtils.rm_f(key_file) if File.exists?(key_file) # avoid permissions probs
  File.open(key_file, "w") do |kfile|
    kfile.puts key
  end
  return key
rescue Errno::ENOENT
  # failed to write pid to file
  raise Jerbil::ServiceConfigError, "Cannot write key file: #{key_file}"
end

.get_key_and_delete_file(name, env, key_dir) ⇒ String

Note:

This deletes the key file

return a previously saved private key

Parameters:

  • name (Symbol)

    of the service

  • env (Symbol)

    the services is running in

  • key_dir (String)

    path to directory where key file is to be written

Returns:

  • (String)

    the private key



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jerbil/support.rb', line 113

def Support.get_key_and_delete_file(name, env, key_dir)
  key = get_private_key(name, env, key_dir)
  if key != '' then
    # there is a key file, so delete it
    key_file = "#{key_dir}/#{name.to_s}-#{env}.asc"
    FileUtils.rm_f(key_file)
  end
  return key
rescue
  # hmm. something went wrong, but do I ignore it
  return ''

end

.get_pid_and_delete_file(name, env, pid_dir) ⇒ Integer

Note:

This deletes the pid file as well

retrieve the pid from a perviously created pid file

Parameters:

  • name (Symbol)

    of the service

  • env (Symbol)

    the services is running in

  • pid_dir (String)

    path to directory where pid file is to be written

Returns:

  • (Integer)

    the pid from the pid file



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jerbil/support.rb', line 63

def Support.get_pid_and_delete_file(name, env, pid_dir)
  pid = get_pid_from_file(name, env, pid_dir)
  if pid > 0 then
    # there is a pid, so delete it
    pid_file = "#{pid_dir}/#{name.to_s}-#{env}.pid"
    FileUtils.rm_f(pid_file)
  end
  return pid
rescue
  # hmm. something went wrong, but do I ignore it
  return 0
end

.get_pid_from_file(name, env, pid_dir) ⇒ Integer

retrieve the pid from a perviously created pid file

Parameters:

  • name (Symbol)

    of the service

  • env (Symbol)

    the services is running in

  • pid_dir (String)

    path to directory where pid file is to be written

Returns:

  • (Integer)

    the pid from the pid file



52
53
54
55
56
57
58
59
# File 'lib/jerbil/support.rb', line 52

def Support.get_pid_from_file(name, env, pid_dir)
  pid_file = "#{pid_dir}/#{name.to_s}-#{env}.pid"
  pid = File.read(pid_file).chomp
  return pid.to_i
rescue
  # something went wrong so return 0
  return 0
end

.get_private_key(name, env, key_dir) ⇒ String

return a previously saved private key

Parameters:

  • name (Symbol)

    of the service

  • env (Symbol)

    the services is running in

  • key_dir (String)

    path to directory where key file is to be written

Returns:

  • (String)

    the private key



103
104
105
106
107
108
109
# File 'lib/jerbil/support.rb', line 103

def Support.get_private_key(name, env, key_dir)
  key_file = "#{key_dir}/#{name.to_s}-#{env}.asc"
  key = File.read(key_file).chomp
  return key
rescue
  return ''
end

.write_pid_file(name, env, pid_dir, pid = Process.pid) ⇒ String

create a pid file for the given service and env in the given pid_dir

Parameters:

  • name (Symbol)

    of the service

  • env (Symbol)

    the services is running in

  • pid_dir (String)

    path to directory where pid file is to be written

Returns:

  • (String)

    the pid

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jerbil/support.rb', line 35

def Support.write_pid_file(name, env, pid_dir, pid=Process.pid)
  #pid = Process.pid.to_s
  pid_file = "#{pid_dir}/#{name.to_s}-#{env}.pid"
  FileUtils.rm_f(pid_file) if File.exists?(pid_file) # avoid permissions probs
  File.open(pid_file, "w") do |pfile|
    pfile.puts pid.to_s
  end
  return pid.to_s
rescue Errno::ENOENT
  # failed to write pid to file
  raise Jerbil::ServiceConfigError, "Cannot write pid file: #{pid_file}"
end