Class: SensuGenerator::SensuServer

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu_generator/sensu_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address:, config: Application.config, logger: Application.logger) ⇒ SensuServer

Returns a new instance of SensuServer.



9
10
11
12
13
# File 'lib/sensu_generator/sensu_server.rb', line 9

def initialize(address:, config: Application.config, logger: Application.logger)
  @address = address
  @config  = config
  @logger  = logger
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



6
7
8
# File 'lib/sensu_generator/sensu_server.rb', line 6

def address
  @address
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/sensu_generator/sensu_server.rb', line 7

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/sensu_generator/sensu_server.rb', line 7

def logger
  @logger
end

Instance Method Details

#processObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/sensu_generator/sensu_server.rb', line 15

def process
  unless @process
    client = RubySupervisor::Client.new(address, 9001,
                                          user: config.get[:sensu][:supervisor][:user],
                                          password: config.get[:sensu][:supervisor][:password]
                                        )
    @process = client.process('sensu-server')
  end
  @process
end

#restartObject



26
27
28
29
30
# File 'lib/sensu_generator/sensu_server.rb', line 26

def restart
  process.restart
  logger.info "Send restart command to sensu-server #{address}"
  running?
end

#running?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sensu_generator/sensu_server.rb', line 32

def running?
  10.times do |t|
    logger.info "Trying to restart server #{address}. Attempt=#{t}."
    if process.state.to_s == 'running'
      logger.info "Sensu-server #{address} was successfully restarted"
      return true
    else
      sleep 1
      if t == 10
        raise SensuServerError, "Sensu-server #{address} restart FAILED"
      end
    end
  end
rescue SensuServerError
  false
end

#stateObject



49
50
51
# File 'lib/sensu_generator/sensu_server.rb', line 49

def state
  process.state
end

#syncObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sensu_generator/sensu_server.rb', line 53

def sync
  begin
    res = Rsync.run("#{config.result_dir}/", "rsync://#{address}/#{config.get[:sensu][:rsync_repo]}", "--delete --recursive")
    status = res.success?
    if status
      msg = "synced"
      logger.info "Sensu-server #{address}: #{msg}"
    else
      msg = "sync FAILED, out: #{res.inspect}"
      raise SensuServerError, "Sensu-server #{address}: #{msg}"
    end
  rescue SensuServerError
    status = false
  end
  status
end