Class: Asmodai::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/asmodai/info.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Asmodai.root) ⇒ Info

Returns a new instance of Info.



13
14
15
# File 'lib/asmodai/info.rb', line 13

def initialize(path=Asmodai.root)
  self.path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/asmodai/info.rb', line 5

def path
  @path
end

Class Method Details

.currentObject



8
9
10
# File 'lib/asmodai/info.rb', line 8

def current
  @current||=self.new
end

Instance Method Details

#base_fileObject

Returns the generated base file where the daemon class is declared in.



50
51
52
# File 'lib/asmodai/info.rb', line 50

def base_file
  path.join("#{daemon_name}.rb")
end

#base_file_ownerObject



54
55
56
# File 'lib/asmodai/info.rb', line 54

def base_file_owner
  Etc.getpwuid(Pathname.new(base_file).stat.uid)
end

#call_wrapper(argument) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/asmodai/info.rb', line 28

def call_wrapper(argument)
  command="#{ruby_exe_path} ./script/asmodai #{argument}"
  function_body=<<-FUNCTION_END
do_#{argument}() {
	cd #{path}
	sudo -u #{base_file_owner.name} bash -c "GEM_HOME=#{gem_home} #{command}"
}  
FUNCTION_END
end

#daemon_classObject

Returns the class of the Daemon



43
44
45
46
# File 'lib/asmodai/info.rb', line 43

def daemon_class
  require "./#{daemon_name}"
  eval(daemon_class_name)
end

#daemon_class_nameObject

Returns the camelized daemon name



24
25
26
# File 'lib/asmodai/info.rb', line 24

def daemon_class_name
  daemon_name.camelize
end

#daemon_nameObject

Returns the name of the daemon which is equal to the name of the directory, the daemon lives in



19
20
21
# File 'lib/asmodai/info.rb', line 19

def daemon_name
  File.basename(self.path)
end

#execute_sudo_checked(cmd) ⇒ Object

Executes cmd in the context of the user, even if asmodai is called sudoed



81
82
83
84
85
86
87
# File 'lib/asmodai/info.rb', line 81

def execute_sudo_checked(cmd)
  if (su=run_as_sudo?)
    `sudo -u #{su} bash -l -c '#{cmd}'`.strip
  else
    `#{cmd}`.strip
  end
end

#gem_homeObject



63
64
65
66
67
68
# File 'lib/asmodai/info.rb', line 63

def gem_home
  if (path=ENV['GEM_HOME']).blank?
    path=Gem.dir
  end
  path
end

#ruby_exe_pathObject



58
59
60
61
# File 'lib/asmodai/info.rb', line 58

def ruby_exe_path
  File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']).
    sub(/.*\s.*/m, '"\&"')
end

#run_as_sudo?Boolean

Return the sudoer if present, false otherwise

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/asmodai/info.rb', line 71

def run_as_sudo?
  if ENV['USER']=='root' and (su=ENV['SUDO_USER'])
    su
  else
    false
  end
end

#wrapped_commandsObject



38
39
40
# File 'lib/asmodai/info.rb', line 38

def wrapped_commands
  %w(start stop reopen foreground)
end