Class: Tamashii::Agent::Master

Inherits:
Component show all
Defined in:
lib/tamashii/agent/master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Component

#check_new_event, #clean_up, #default_device_name, #display_name, #get_device_class_name, #get_device_instance, #handle_new_event, #initialize_device, #load_default_device, #restart_current_component_async, #run, #run!, #run_worker_loop, #send_event, #stop_threads, #worker_loop

Methods included from Common::Loggable

#display_name, #logger, #progname

Constructor Details

#initializeMaster

Returns a new instance of Master.



15
16
17
18
19
20
21
# File 'lib/tamashii/agent/master.rb', line 15

def initialize
  super(:master, self)
  logger.info "Starting Tamashii::Agent #{Tamashii::Agent::VERSION} in #{Config.env} mode"
  @serial_number = get_serial_number
  logger.info "Serial number: #{@serial_number}"
  create_components
end

Instance Attribute Details

#serial_numberObject (readonly)

Returns the value of attribute serial_number.



13
14
15
# File 'lib/tamashii/agent/master.rb', line 13

def serial_number
  @serial_number
end

Instance Method Details

#broadcast_event(event) ⇒ Object



132
133
134
135
136
# File 'lib/tamashii/agent/master.rb', line 132

def broadcast_event(event)
  @components.each_value do |c|
    c.send_event(event)
  end
end

#create_component(name, params) {|c| ... } ⇒ Object

Yields:

  • (c)


47
48
49
50
51
52
53
54
55
# File 'lib/tamashii/agent/master.rb', line 47

def create_component(name, params)
  klass = Agent.const_get(params[:class_name])
  logger.info "Starting component #{name}:#{klass}"
  c = klass.new(name, self, params[:options])
  c.instance_eval(&params[:block]) if params[:block] 
  yield c if block_given?
  c.run
  @components[name] = c
end

#create_componentsObject



40
41
42
43
44
45
# File 'lib/tamashii/agent/master.rb', line 40

def create_components
  @components = {}
  Config.components.each do |name, params|
    create_component(name, params) 
  end
end

#get_serial_numberObject



23
24
25
26
27
28
# File 'lib/tamashii/agent/master.rb', line 23

def get_serial_number
  serial = ENV['SERIAL_NUMBER']
  serial = read_serial_from_cpuinfo if serial.nil?
  serial = "#{Config.env}_pid_#{Process.pid}".upcase if serial.nil?
  serial
end

#process_event(event) ⇒ Object

override



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tamashii/agent/master.rb', line 70

def process_event(event)
  super
  case event.type
  when Event::SYSTEM_COMMAND
    logger.info "System command code: #{event.body}"
    case event.body.to_i
    when Tamashii::Type::REBOOT
      system_reboot
    when Tamashii::Type::POWEROFF
      system_poweroff
    when Tamashii::Type::RESTART
      system_restart
    when Tamashii::Type::UPDATE
      system_update
    end
  when Event::CONNECTION_NOT_READY
    broadcast_event(Event.new(Event::BEEP, "error"))
    broadcast_event(Event.new(Event::LCD_MESSAGE, "Fatal Error\nConnection Error"))
  when Event::RESTART_COMPONENT
    restart_component(event.body)
  else
    broadcast_event(event)
  end
end

#read_serial_from_cpuinfoObject



30
31
32
33
34
35
36
37
38
# File 'lib/tamashii/agent/master.rb', line 30

def read_serial_from_cpuinfo
  return nil unless File.exists?("/proc/cpuinfo")
  File.open("/proc/cpuinfo") do |f|
    content = f.read
    if content =~ /Serial\s*:\s*(\w+)/
      return $1
    end
  end
end

#restart_component(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tamashii/agent/master.rb', line 57

def restart_component(name)
  if old_component = @components[name]
    params = Config.components[name]
    logger.info "Stopping component: #{name}"
    old_component.stop # TODO: set timeout for stopping?
    logger.info "Restarting component: #{name}"
    create_component(name, params)
  else
    logger.error "Restart component failed: unknown component #{name}"
  end
end

#show_message(message) ⇒ Object



95
96
97
98
99
# File 'lib/tamashii/agent/master.rb', line 95

def show_message(message)
  logger.info message
  broadcast_event(Event.new(Event::LCD_MESSAGE, message))
  sleep 1
end

#stopObject

override



123
124
125
126
127
128
129
# File 'lib/tamashii/agent/master.rb', line 123

def stop
  super
  @components.each_value do |c|
    c.stop
  end
  logger.info "Master stopped"
end

#system_poweroffObject



106
107
108
109
# File 'lib/tamashii/agent/master.rb', line 106

def system_poweroff
  show_message "Powering  Off"
  system("poweroff &")
end

#system_rebootObject



101
102
103
104
# File 'lib/tamashii/agent/master.rb', line 101

def system_reboot
  show_message "Rebooting"
  system("reboot &")
end

#system_restartObject



111
112
113
114
# File 'lib/tamashii/agent/master.rb', line 111

def system_restart
  show_message "Restarting"
  system("systemctl restart tamashii-agent.service &")
end

#system_updateObject



116
117
118
119
120
# File 'lib/tamashii/agent/master.rb', line 116

def system_update
  show_message("Updating")
  system("gem update tamashii-agent")
  system_restart
end