Module: Splash::Helpers

Overview

Helpers namespace

facilities for file system commands collapse

Verifiers for application : FS and TCP/IP services collapse

Instance Method Summary collapse

Instance Method Details

#check_unicode_termBoolean

check if unicode must be used with term ENV

Returns:

  • (Boolean)


359
360
361
362
363
364
365
366
# File 'lib/splash/helpers.rb', line 359

def check_unicode_term
  return false unless ENV.include? "TERM"
  if ENV.values_at("LC_ALL","LC_CTYPE","LANG").compact.include?("UTF-8") and ENV.values_at('TERM').include? "xterm" then
    return true
  else
    return false
  end
end

#daemonize(options) { ... } ⇒ Fixnum

method for daemonize blocks

Examples:

usage inline

class Test
  include Splash::Helpers
  private :daemonize
  def initialize
    @loop = Proc::new do
      loop do
        sleep 1
      end
    end
  end

  def run
    daemonize({:description => "A loop daemon", :pid_file => '/tmp/pid.file'}, &@loop)
  end
 end

usage block

class Test
  include Splash::Helpers
  include Dorsal::Privates
  private :daemonize
  def initialize
  end

  def run
    daemonize :description => "A loop daemon", :pid_file => '/tmp/pid.file' do
      loop do
        sleep 1
      end
    end
  end
 end

Parameters:

  • options (Hash)

    the list of options, keys are symbols

Options Hash (options):

  • :description (String)

    the description of the process, use for $0

  • :pid_file (String)

    the pid filename

  • :daemon_user (String)

    the user to change privileges

  • :daemon_group (String)

    the group to change privileges

  • :stderr_trace (String)

    the path of the file where to redirect STDERR

  • :stdout_trace (String)

    the path of the file where to redirect STDOUT

  • :sigint_handler (Proc)

    handler Proc for SIGINT signal

  • :sigterm_handler (Proc)

    handler Proc for SIGTERM signal

  • :sighup_handler (Proc)

    handler Proc for SIGHuP signal

  • :foreground (Bool)

    option to run foreground

Yields:

  • a process definion or block given

Returns:

  • (Fixnum)

    pid the pid of the forked processus



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
191
192
193
194
195
# File 'lib/splash/helpers.rb', line 145

def daemonize(options)
  #Process.euid = 0
  #Process.egid = 0

  trap("SIGINT"){
    if options[:sigint_handler] then
      options[:sigint_handler].call
    else
      exit! 0
    end
  }
  trap("SIGTERM"){
    if options[:sigterm_handler] then
      options[:sigterm_handler].call
    else
      exit! 0
    end
   }
  trap("SIGHUP"){
    if options[:sighup_handler] then
      options[:sighup_handler].call
    else
      exit! 0
    end
   }
  if options[:foreground]
    change_logger logger: :dual
    Process.setproctitle options[:description] if options[:description]
    return yield
  end
  fork do
    change_logger logger: :daemon
    #Process.daemon
    File.open(options[:pid_file],"w"){|f| f.puts Process.pid } if options[:pid_file]
    if options[:daemon_user] and options[:daemon_group] then
      uid = Etc.getpwnam(options[:daemon_user]).uid
      gid = Etc.getgrnam(options[:daemon_group]).gid
      Process::UID.change_privilege(uid)
      #  Process::GID.change_privilege(gid)
    end
    $stdout.reopen(options[:stdout_trace], "w") if options[:stdout_trace]
    $stderr.reopen(options[:stderr_trace], "w") if options[:stderr_trace]

    #$0 = options[:description]
    Process.setproctitle options[:description] if options[:description]

    yield

  end
  return 0
end

#format_by_extensions(extension) ⇒ Object



348
349
350
351
352
353
354
355
# File 'lib/splash/helpers.rb', line 348

def format_by_extensions(extension)
  result = {
    'json' => 'application/json',
    'yaml' => 'text/x-yaml',
    'yml' => 'text/x-yaml'
  }
   return result[extension]
end

#format_response(data, format) ⇒ Object

!@endgroup



339
340
341
342
343
344
345
346
# File 'lib/splash/helpers.rb', line 339

def format_response(data, format)
  response = case format
             when 'application/json' then JSON.pretty_generate(data)
             when 'text/x-yaml' then data.to_yaml
             else data.to_yaml
             end
  return response
end

#get_processes(options = {}) ⇒ String|Array

facility for retreiving PID from process query

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :pattern (String)

    a regexp to search

  • :patterns (Array)

    an array of regexp to search

  • :full (Bool)

    flag to retrieve all process data not only PID

Returns:

  • (String|Array)

    PID or data structure



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/splash/helpers.rb', line 28

def get_processes(options = {})
  patterns = []
  patterns  = options[:patterns] if options[:patterns]
  patterns << options[:pattern] if options[:pattern]
  res = PS.get_all_processes
  patterns.each do |item|
    res = res.find_processes item
  end
  if options[:full] then
    return res
  else
    return res.pick_attr('PID')
  end
end

#group_rootString

return the ‘root’ group name : root or wheel

Returns:

  • (String)

    name



18
19
20
# File 'lib/splash/helpers.rb', line 18

def group_root
  return Etc.getgrgid(0).name
end

#install_file(options = {}) ⇒ Object

facility for file installation

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :source (String)

    file source path

  • :target (String)

    file target path

  • :mode (String)

    String for OCTAL rights like “644”

  • :owner (String)

    file owner for target

  • :group (String)

    file group for target



206
207
208
209
210
211
212
213
214
215
# File 'lib/splash/helpers.rb', line 206

def install_file(options = {})
  #begin
    FileUtils::copy options[:source], options[:target] #unless File::exist? options[:target]
    FileUtils.chmod options[:mode].to_i(8), options[:target] if options[:mode]
    FileUtils.chown options[:owner], options[:group], options[:target] if options[:owner] and options[:group]
    return true
  #rescue StandardError
    # return false
  #end
end

#is_root?Bool

facility to verifyingif the active process run as root

Returns:

  • (Bool)

    status



78
79
80
81
82
83
84
85
# File 'lib/splash/helpers.rb', line 78

def is_root?
  case (Process.uid)
  when 0
    return true
  else
    return false
  end
end

#make_folder(options = {}) ⇒ Object

facility for folder creation

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :path (String)

    folder path (relative or absolute)

  • :mode (String)

    String for OCTAL rights like “644”

  • :owner (String)

    file owner for folder

  • :group (String)

    file group for folder



223
224
225
226
227
228
229
230
231
232
# File 'lib/splash/helpers.rb', line 223

def make_folder(options = {})
  begin
    FileUtils::mkdir_p options[:path] unless File::exist? options[:path]
    FileUtils.chmod options[:mode].to_i(8), options[:path] if options[:mode]
    FileUtils.chown options[:owner], options[:group], options[:path] if options[:owner] and options[:group]
    return true
  rescue StandardError
    return false
  end
end

facility for Symbolic link

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :source (String)

    path of the file

  • :link (String)

    path of the symlink



238
239
240
241
242
243
244
245
246
# File 'lib/splash/helpers.rb', line 238

def make_link(options = {})
  begin
    FileUtils::rm options[:link] if (File::symlink? options[:link] and not File::exist? options[:link])
    FileUtils::ln_s options[:source], options[:link] unless File::exist? options[:link]
    return true
  rescue StandardError
    return false
  end
end

#run_as_root(method, options = {}) ⇒ void

This method returns an undefined value.

wrapping execution if process run as root

Parameters:

  • method (Symbol)

    a method name th wrap



90
91
92
93
94
95
96
# File 'lib/splash/helpers.rb', line 90

def run_as_root(method, options = {})
  unless is_root?
    return {:case => :not_root, :more => "subcommands : #{method.to_s}"}
  else
    return self.send method, options
  end
end

#search_file_in_gem(_gem, _file) ⇒ String, False

facility to find a file in gem path

Parameters:

  • _gem (String)

    a Gem name

  • _file (String)

    a file relative path in the gem

Returns:

  • (String)

    the path of the file, if found.

  • (False)

    if not found



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/splash/helpers.rb', line 49

def search_file_in_gem(_gem,_file)
  if Gem::Specification.respond_to?(:find_by_name)
    begin
      spec = Gem::Specification.find_by_name(_gem)
    rescue LoadError
      spec = nil
    end
  else
    spec = Gem.searcher.find(_gem)
  end
  if spec then
    if Gem::Specification.respond_to?(:find_by_name)
      res = spec.lib_dirs_glob.split('/')
    else
      res = Gem.searcher.lib_dirs_for(spec).split('/')
    end
    res.pop
    services_path = res.join('/').concat("/#{_file}")
    return services_path if File::exist?(services_path)
    return false
  else
    return false
  end
end

#user_rootString

return the ‘root’ name

Returns:

  • (String)

    name



12
13
14
# File 'lib/splash/helpers.rb', line 12

def user_root
  return Etc.getpwuid(0).name
end

#verify_file(options = {}) ⇒ Array

check file

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)

    path of file

  • :mode (String)

    String for OCTAL rights like “644”, optionnal

  • :owner (String)

    file owner for file, optionnal

  • :group (String)

    file group for file, optionnal

Returns:

  • (Array)

    of Symbol with error type : [:inexistant,:mode,:owner,:group]



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/splash/helpers.rb', line 291

def verify_file(options ={})
  res = Array::new
  return  [:inexistant] unless File.file?(options[:name])
  stat = File.stat(options[:name])
  if options[:mode] then
    mode = "%o" % stat.mode
    res << :mode if mode[-3..-1] != options[:mode]
  end
  if options[:owner] then
    res << :owner if Etc.getpwuid(stat.uid).name != options[:owner]
  end
  if options[:group] then
    res << :group if Etc.getgrgid(stat.gid).name != options[:group]
  end
  return res
end

#verify_folder(options = {}) ⇒ Array

check folder

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)

    folder path (relative or absolute)

  • :mode (String)

    String for OCTAL rights like “644”, optionnal

  • :owner (String)

    file owner for folder, optionnal

  • :group (String)

    file group for folder, optionnal

Returns:

  • (Array)

    of Symbol with error type : [:inexistant,:mode,:owner,:group]



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/splash/helpers.rb', line 259

def verify_folder(options ={})
  res = Array::new
  return  [:inexistant] unless File.directory?(options[:name])
  stat = File.stat(options[:name])
  if options[:mode] then
    mode = "%o" % stat.mode
    res << :mode if mode[-3..-1] != options[:mode]
  end
  if options[:owner] then
    res << :owner if Etc.getpwuid(stat.uid).name != options[:owner]
  end
  if options[:group] then
    res << :group if Etc.getgrgid(stat.gid).name != options[:group]
  end
  return res
end

check symlink

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :name (String)

    path of the link

Returns:

  • (Boolean)


280
281
282
# File 'lib/splash/helpers.rb', line 280

def verify_link(options ={})
  return File.file?(options[:name])
end

#verify_service(options = {}) ⇒ Bool

TCP/IP service checker

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String)

    hostname

  • :port (String)

    TCP port

  • :url (String)

    full URL, priority on :host and :port

Returns:

  • (Bool)

    status



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/splash/helpers.rb', line 314

def verify_service(options ={})
  begin
    if options[:url] then
      uri = URI.parse(options[:url])
      host = uri.host
      port = uri.port
    else
      host = options[:host]
      port = options[:port]
    end
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(host, port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
    return false
  end
end