Class: SystemManager

Inherits:
Object
  • Object
show all
Defined in:
app/models/system_manager.rb

Constant Summary collapse

SERVICES =
['rmails', 'postfix', 'dovecot', 'nginx', 'dspam', 'amavisd']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSystemManager

Returns a new instance of SystemManager.



33
34
35
36
# File 'app/models/system_manager.rb', line 33

def initialize
  @i = @interpreter = AutomateIt.new
  @services_to_restart = []
end

Instance Attribute Details

#interpreterObject (readonly)

Returns the value of attribute interpreter.



3
4
5
# File 'app/models/system_manager.rb', line 3

def interpreter
  @interpreter
end

Class Method Details

.change_configuration_and_restartObject



7
8
9
10
11
12
13
# File 'app/models/system_manager.rb', line 7

def self.change_configuration_and_restart
  sm = ServiceManager.new
  sm.write_new_values_for Property::Dovecot
  sm.write_new_values_for Property::Postfix
  sm.write_new_values_for Property::Dspam
  sm.smart_restart_all
end

.run(service, goal) ⇒ Object



15
16
17
18
# File 'app/models/system_manager.rb', line 15

def self.run(service, goal)
  sm = ServiceManager.new
  sm.send "#{service}_switch", goal
end

Instance Method Details

#amavis_switch(goal) ⇒ Object



72
73
74
# File 'app/models/system_manager.rb', line 72

def amavis_switch(goal)

end

#dkim_switch(goal) ⇒ Object



76
77
78
79
80
81
# File 'app/models/system_manager.rb', line 76

def dkim_switch(goal)
  Property::Postfix.dkim_power @interpreter, goal
  action = goal ? 'restart' : 'stop'
  @i.service_manager.send('restart', "postfix")
  @i.service_manager.send(action, "opendkim")
end

#dspam_switch(goal) ⇒ Object



64
65
66
67
68
69
# File 'app/models/system_manager.rb', line 64

def dspam_switch(goal)
  Property::Postfix.dspam_power @interpreter, goal
  action = goal ? 'restart' : 'stop'
  @i.service_manager.send('restart', "postfix") # postfix will only restart
  @i.service_manager.send(action, "dspam")
end

#postfix_switch(goal) ⇒ Object



83
84
85
86
# File 'app/models/system_manager.rb', line 83

def postfix_switch(goal)
  action = goal ? 'restart' : 'stop'
  @i.service_manager.send(action, "postfix")
end

#restart_allObject



20
21
22
23
24
# File 'app/models/system_manager.rb', line 20

def restart_all
  SERVICES[1..-1].each do |s|
    @interpreter.service_manager.restart s
  end
end

#smart_restart_allObject



26
27
28
29
30
31
# File 'app/models/system_manager.rb', line 26

def smart_restart_all
  @services_to_restart.each do |klass|
    @interpreter.service_manager.restart SERVICES[klass.service]
  end
  @services_to_restart = []
end

#write_new_values_for(properties_of_service) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/system_manager.rb', line 38

def write_new_values_for(properties_of_service)
  # find properties to write
  new_values = properties_of_service.all_new_values_for_write
  if new_values.any?
    @services_to_restart << properties_of_service
  else
    return true
  end
  # update database in transaction
  properties_of_service.transaction do
    new_values.each do |property|
      property.value = property.new_value
      property.new_value = nil
      property.save
    end
  end
  # write properties with template
  new_values.group_by(&:template).each do |template, properties|
    locals = {}
    properties.each do |property|
      locals[property.key] = property.value
    end
    properties_of_service.send "#{template}_template", @interpreter, locals
  end
end