Module: HelperClasses::Platform

Extended by:
DPuts, Platform
Included in:
Platform
Defined in:
lib/helper_classes/platform.rb

Instance Attribute Summary collapse

Attributes included from DPuts

#log_file, #logall_file, #max_msg_len, #mutex, #show_time, #silent, #terminal_width

Instance Method Summary collapse

Methods included from DPuts

ddputs, dlog_msg, dp, dputs, dputs_func, dputs_getcaller, dputs_out, dputs_unfunc, dputs_write, log_msg, logfile_valid

Instance Attribute Details

#servicesObject

Returns the value of attribute services.



6
7
8
# File 'lib/helper_classes/platform.rb', line 6

def services
  @services
end

#systemObject

Returns the value of attribute system.



6
7
8
# File 'lib/helper_classes/platform.rb', line 6

def system
  @system
end

Instance Method Details

#daemon_reloadObject



101
102
103
# File 'lib/helper_classes/platform.rb', line 101

def daemon_reload
  reload
end

#disable(service) ⇒ Object



89
90
91
92
93
94
# File 'lib/helper_classes/platform.rb', line 89

def disable(service)
  service_run(service, {ArchLinux: 'systemctl disable ##',
                        Ubuntu: 'systemctl disable ##',
                        MacOSX: nil}
  )
end

#enable(service) ⇒ Object



82
83
84
85
86
87
# File 'lib/helper_classes/platform.rb', line 82

def enable(service)
  service_run(service, {ArchLinux: 'systemctl enable ##',
                        Ubuntu: 'systemctl enable ##',
                        MacOSX: nil}
  )
end

#enable_start(service) ⇒ Object



105
106
107
108
# File 'lib/helper_classes/platform.rb', line 105

def enable_start(service)
  enable(service)
  start(service)
end

#net_restart(iface) ⇒ Object



133
134
135
136
# File 'lib/helper_classes/platform.rb', line 133

def net_restart(iface)
  net_stop(iface)
  net_start(iface)
end

#net_start(iface) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/helper_classes/platform.rb', line 115

def net_start(iface)
  c = "#{@services[:net_start][@system]} #{iface}"
  if !System.run_bool(c)
    log_msg :Services, "Command #{c} failed"
    return false
  end
  return true
end

#net_status(iface, dev) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/helper_classes/platform.rb', line 138

def net_status(iface, dev)
  case @system
    when :ArchLinux
      return System.run_str("netctl status #{iface} | grep Active") =~ /: active/
    when :Ubuntu
      return System.run_str("ifconfig | grep #{dev}") != ''
    else
      return false
  end
end

#net_stop(iface) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/helper_classes/platform.rb', line 124

def net_stop(iface)
  c = "#{@services[:net_stop][@system]} #{iface}"
  if !System.run_bool(c)
    log_msg :Services, "Command #{c} failed"
    return false
  end
  return true
end

#reloadObject



96
97
98
99
# File 'lib/helper_classes/platform.rb', line 96

def reload
  return unless @system == :ArchLinux
  System.run_bool('/usr/bin/systemctl daemon-reload')
end

#restart(service) ⇒ Object



75
76
77
78
79
80
# File 'lib/helper_classes/platform.rb', line 75

def restart(service)
  service_run(service, {ArchLinux: 'systemctl restart ##',
                        Ubuntu: 'systemctl restart ##',
                        MacOSX: nil}
  )
end

#service_get(service) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/helper_classes/platform.rb', line 28

def service_get(service)
  begin
    @services[service.to_sym][@system]
  rescue NoMethodError => _e
    service.to_s
  end
end

#service_run(service, cmd) ⇒ Object



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/helper_classes/platform.rb', line 36

def service_run(service, cmd)
  return unless @system
  if !cmd
    log_msg :Services, "System #{@system} can't start services"
    return false
  end
  service_name = service_get(service)
  if !service_name
    log_msg :Services, "System #{@system} doesn't have service #{service}"
    return false
  end
  cmd_system = cmd[@system]
  if !cmd_system
    log_msg :Services, "System #{@system} doesn't know how to do #{cmd}"
    return false
  end
  [service_name].flatten.each { |s|
    c = cmd_system.sub(/##/, s)
    if !System.run_bool(c)
      log_msg :Services, "Command #{c} failed"
      return false
    end
  }
end

#start(service) ⇒ Object



61
62
63
64
65
66
# File 'lib/helper_classes/platform.rb', line 61

def start(service)
  service_run(service, {ArchLinux: 'systemctl start ##',
                        Ubuntu: 'systemctl start ##',
                        MacOSX: nil}
  )
end

#stop(service) ⇒ Object



68
69
70
71
72
73
# File 'lib/helper_classes/platform.rb', line 68

def stop(service)
  service_run(service, {ArchLinux: 'systemctl stop ##',
                        Ubuntu: 'systemctl stop ##',
                        MacOSX: nil}
  )
end

#stop_disable(service) ⇒ Object



110
111
112
113
# File 'lib/helper_classes/platform.rb', line 110

def stop_disable(service)
  disable(service)
  stop(service)
end