Module: Splash::Helpers

facilités sur le système de fichier collapse

Vérifiers de l'application collapse

Instance Method Summary collapse

Instance Method Details

#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



132
133
134
135
136
137
138
139
140
141
142
143
144
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
# File 'lib/splash/helpers.rb', line 132

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
    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

#get_process(options = {}) ⇒ String

facilité pour récupérer un PID depuis une regexp

Parameters:

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

Options Hash (options):

  • :pattern (String)

    une regexp

Returns:

  • (String)

    le PID



20
21
22
23
24
25
26
27
28
# File 'lib/splash/helpers.rb', line 20

def get_process(options = {})
  pattern = options[:pattern]
  res = `ps aux|grep '#{pattern}'|grep -v grep`.to_s
  unless res.empty? then
    return res.split(/\s+/)[1]
  else
    return ''
  end
end

#group_rootObject



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

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

#install_file(options = {}) ⇒ Object

facilité d’installation de fichier

Parameters:

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

Options Hash (options):

  • :source (String)

    le chemin source du fichier

  • :target (String)

    le chemin cible du fichier

  • :mode (String)

    les droits du fichier du type Octal “XXX”

  • :owner (String)

    le owner du fichier

  • :group (String)

    le groupe du fichier



192
193
194
195
196
197
198
199
200
201
# File 'lib/splash/helpers.rb', line 192

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

facilité pour vérifier si le process actif est root

Returns:

  • (Bool)

    vrai ou faux



65
66
67
68
69
70
71
72
# File 'lib/splash/helpers.rb', line 65

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

#make_folder(options = {}) ⇒ Object

facilité de création de répertoire

Parameters:

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

Options Hash (options):

  • :path (String)

    le répertoire à créer (relatif ou absolut)

  • :mode (String)

    les droits du fichier du type Octal “XXX”

  • :owner (String)

    le owner du fichier

  • :group (String)

    le groupe du fichier



209
210
211
212
213
214
215
216
217
218
# File 'lib/splash/helpers.rb', line 209

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

facilité de liaison symbolique de fichier

Parameters:

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

Options Hash (options):

  • :source (String)

    le chemin source du fichier

  • :link (String)

    le chemin du lien symbolique



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

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.

facilité pour s’assurer qu’on execute une méthode avec les droits root

Parameters:

  • method (Symbol)

    a method name th wrap



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

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

facilities 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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/splash/helpers.rb', line 36

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_rootObject



8
9
10
# File 'lib/splash/helpers.rb', line 8

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

#verify_file(options = {}) ⇒ Array

verifier d’existence d’un fichier

Parameters:

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

Options Hash (options):

  • :name (String)

    path du fichier obligatoire

  • :mode (String)

    droit du fichier optionnel

  • :owner (String)

    owner du fichier optionnel

  • :group (String)

    groupe du fichier optionnel

Returns:

  • (Array)

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



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/splash/helpers.rb', line 277

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

verifier d’existence d’un repertoire

Parameters:

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

Options Hash (options):

  • :path (String)

    le répertoire (relatif ou absolut) obligatoire

  • :mode (String)

    droit du répertoire optionnel

  • :owner (String)

    owner du répertoire optionnel

  • :group (String)

    groupe du répertoire optionnel

Returns:

  • (Array)

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/splash/helpers.rb', line 245

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

verifier d’existence d’un lien

Parameters:

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

Options Hash (options):

  • :name (String)

    path du lien

Returns:

  • (Bool)

    vrai ou faux



266
267
268
# File 'lib/splash/helpers.rb', line 266

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

#verify_service(options = {}) ⇒ Bool

verifier de l’ecoute d’un service sur un host et port donné en TCP

Parameters:

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

Options Hash (options):

  • :host (String)

    le nom d’hote

  • :port (String)

    le port TCP

Returns:

  • (Bool)

    vrai ou faux



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/splash/helpers.rb', line 299

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