~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/common/machinestatus.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package common
 
5
 
 
6
import (
 
7
        "github.com/juju/juju/state"
 
8
        "github.com/juju/juju/status"
 
9
)
 
10
 
 
11
// MachineStatusGetter defines the machine functionality
 
12
// required to status.
 
13
type MachineStatusGetter interface {
 
14
        Status() (status.StatusInfo, error)
 
15
        AgentPresence() (bool, error)
 
16
        Id() string
 
17
        Life() state.Life
 
18
}
 
19
 
 
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()
 
24
        if err != nil {
 
25
                return status.StatusInfo{}, err
 
26
        }
 
27
 
 
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
 
32
        }
 
33
 
 
34
        agentAlive, err := machine.AgentPresence()
 
35
        if err != nil {
 
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
 
39
        }
 
40
        if machine.Life() != state.Dead && !agentAlive {
 
41
                machineStatus.Status = status.Down
 
42
                machineStatus.Message = "agent is not communicating with the server"
 
43
        }
 
44
        return machineStatus, nil
 
45
}
 
46
 
 
47
func canMachineBeDown(machineStatus status.StatusInfo) bool {
 
48
        switch machineStatus.Status {
 
49
        case status.Pending, status.Stopped:
 
50
                return false
 
51
        }
 
52
        return true
 
53
}