Class: Adminix::System

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/adminix/system.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSystem

Returns a new instance of System.



30
31
32
33
34
35
# File 'lib/adminix/system.rb', line 30

def initialize
  define_os
  parse_system_info
  define_adminix_bin
  check_system_load
end

Instance Attribute Details

#adminix_binObject (readonly)

Returns the value of attribute adminix_bin.



28
29
30
# File 'lib/adminix/system.rb', line 28

def adminix_bin
  @adminix_bin
end

#homeObject (readonly)

Returns the value of attribute home.



28
29
30
# File 'lib/adminix/system.rb', line 28

def home
  @home
end

#memory_loadObject (readonly)

Returns the value of attribute memory_load.



28
29
30
# File 'lib/adminix/system.rb', line 28

def memory_load
  @memory_load
end

#osObject (readonly)

Returns the value of attribute os.



28
29
30
# File 'lib/adminix/system.rb', line 28

def os
  @os
end

#processor_loadObject (readonly)

Returns the value of attribute processor_load.



28
29
30
# File 'lib/adminix/system.rb', line 28

def processor_load
  @processor_load
end

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



28
29
30
# File 'lib/adminix/system.rb', line 28

def ruby_version
  @ruby_version
end

#usernameObject (readonly)

Returns the value of attribute username.



28
29
30
# File 'lib/adminix/system.rb', line 28

def username
  @username
end

Class Method Details

.linux?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/adminix/system.rb', line 20

def self.linux?
  self.unix? and not self.mac?
end

.mac?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/adminix/system.rb', line 12

def self.mac?
  (/darwin/ =~ RUBY_PLATFORM) != nil
end

.rvm?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/adminix/system.rb', line 24

def self.rvm?
  true
end

.unix?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/adminix/system.rb', line 16

def self.unix?
  !self.windows?
end

.windows?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/adminix/system.rb', line 8

def self.windows?
  (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end

Instance Method Details

#check_system_loadObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/adminix/system.rb', line 58

def check_system_load
  case @os
    when :mac
      @processor_load = `ps -A -o %cpu | awk '{s+=$1} END {print s "%"}'`.to_f
      @memory_load = 0.1
    when :linux, :unix
      @processor_load = `grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'`.to_f
      @memory_load = `free | grep Mem | awk '{print $3/$2 * 100.0}'`.to_f
    end
end

#eval(cmd) ⇒ Object



48
49
50
51
52
# File 'lib/adminix/system.rb', line 48

def eval(cmd)
  `#{cmd}`
rescue => e
  e.to_s
end

#generate_daemonObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/adminix/system.rb', line 37

def generate_daemon
  template = File.read("#{Adminix::root}/views/daemon_scripts/upstart.conf.erb")
  file_content = ERB.new(template).result(binding)

  file_path = `pwd`.split("\n")[0] + '/adminix.conf'
  File.delete(file_path) if File.exists?(file_path)
  File.open(file_path, 'w') { |file| file.write(file_content) }

  file_path
end

#log(message) ⇒ Object



69
70
71
# File 'lib/adminix/system.rb', line 69

def log(message)
  puts message unless config.daemon
end

#sudo?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/adminix/system.rb', line 54

def sudo?
  @username == 'root'
end