-
Notifications
You must be signed in to change notification settings - Fork 19
/
states.go
129 lines (112 loc) · 3.63 KB
/
states.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* UpdateHub
* Copyright (C) 2017
* O.S. Systems Sofware LTDA: contato@ossystems.com.br
*
* SPDX-License-Identifier: Apache-2.0
*/
package updatehub
import (
"github.com/UpdateHub/updatehub/client"
"github.com/UpdateHub/updatehub/metadata"
)
// UpdateHubState holds the possible states for the agent
type UpdateHubState int
const (
// UpdateHubDummyState is a dummy state
UpdateHubDummyState = iota
// UpdateHubStateIdle is set when the agent is in the "idle" mode
UpdateHubStateIdle
// UpdateHubStatePoll is set when the agent is in the "polling" mode
UpdateHubStatePoll
// UpdateHubStateProbe is set when the agent is running a
// "probeUpdate" procedure
UpdateHubStateProbe
// UpdateHubStateDownloading is set when the agent is downloading
// an update
UpdateHubStateDownloading
// UpdateHubStateDownloaded is set when the agent finished
// downloading an update
UpdateHubStateDownloaded
// UpdateHubStateInstalling is set when the agent is starting an
// update installation
UpdateHubStateInstalling
// UpdateHubStateInstalled is set when the agent finished
// installing an update
UpdateHubStateInstalled
// UpdateHubStateExit is set when the daemon is about to quit
UpdateHubStateExit
// UpdateHubStateError is set when an error occured on the agent
UpdateHubStateError
// UpdateHubStateRebooting is set when an error occured on the agent
UpdateHubStateRebooting
)
var statusNames = map[UpdateHubState]string{
UpdateHubDummyState: "dummy",
UpdateHubStateIdle: "idle",
UpdateHubStatePoll: "poll",
UpdateHubStateProbe: "probe",
UpdateHubStateDownloading: "downloading",
UpdateHubStateDownloaded: "downloaded",
UpdateHubStateInstalling: "installing",
UpdateHubStateInstalled: "installed",
UpdateHubStateExit: "exit",
UpdateHubStateError: "error",
UpdateHubStateRebooting: "rebooting",
}
// ProgressTracker will define which way the progress is kept
type ProgressTracker interface {
SetProgress(progress int)
GetProgress() int
}
// ProgressTrackerImpl is for the ProgressTracker interface implementation
type ProgressTrackerImpl struct {
progress int
}
// SetProgress is for the ProgressTracker interface implementation
func (pti *ProgressTrackerImpl) SetProgress(progress int) {
pti.progress = progress
}
// GetProgress is for the ProgressTracker interface implementation
func (pti *ProgressTrackerImpl) GetProgress() int {
return pti.progress
}
// BaseState is the state from which all others must do composition
type BaseState struct {
id UpdateHubState
apiClient *client.ApiClient
}
// ToMap is for the State interface implementation
func (state *BaseState) ToMap() map[string]interface{} {
m := map[string]interface{}{}
m["status"] = StateToString(state.ID())
return m
}
// ID returns the state id
func (b *BaseState) ID() UpdateHubState {
return b.id
}
// ApiClient returns the apiClient
func (b *BaseState) ApiClient() *client.ApiClient {
return b.apiClient
}
// Cancel cancels a state if it is cancellable
func (b *BaseState) Cancel(ok bool, nextState State) bool {
return ok
}
// State interface describes the necessary operations for a State
type State interface {
ID() UpdateHubState
Handle(*UpdateHub) (State, bool) // Handle implements the behavior when the State is set
Cancel(bool, State) bool
ToMap() map[string]interface{}
ApiClient() *client.ApiClient
}
// StateToString converts a "UpdateHubState" to string
func StateToString(status UpdateHubState) string {
return statusNames[status]
}
// ReportableState interface describes the necessary operations for a State to be reportable
type ReportableState interface {
UpdateMetadata() *metadata.UpdateMetadata
}