1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/juju/state"
8
"github.com/juju/juju/status"
11
// MachineStatusGetter defines the machine functionality
12
// required to status.
13
type MachineStatusGetter interface {
14
Status() (status.StatusInfo, error)
15
AgentPresence() (bool, error)
20
// MachineStatus returns the machine agent status for a given
21
// machine, with special handling for agent presence.
22
func MachineStatus(machine MachineStatusGetter) (status.StatusInfo, error) {
23
machineStatus, err := machine.Status()
25
return status.StatusInfo{}, err
28
if !canMachineBeDown(machineStatus) {
29
// The machine still being provisioned - there's no point in
30
// enquiring about the agent liveness.
31
return machineStatus, nil
34
agentAlive, err := machine.AgentPresence()
36
// We don't want any presence errors affecting status.
37
logger.Debugf("error determining presence for machine %s: %v", machine.Id(), err)
38
return machineStatus, nil
40
if machine.Life() != state.Dead && !agentAlive {
41
machineStatus.Status = status.Down
42
machineStatus.Message = "agent is not communicating with the server"
44
return machineStatus, nil
47
func canMachineBeDown(machineStatus status.StatusInfo) bool {
48
switch machineStatus.Status {
49
case status.Pending, status.Stopped: