forked from mendersoftware/deployments
/
device_deployment.go
161 lines (132 loc) · 4.53 KB
/
device_deployment.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2017 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package deployments
import (
"time"
"github.com/asaskevich/govalidator"
"github.com/satori/go.uuid"
"github.com/mendersoftware/deployments/resources/images"
)
// Deployment statuses
const (
DeviceDeploymentStatusDownloading = "downloading"
DeviceDeploymentStatusInstalling = "installing"
DeviceDeploymentStatusRebooting = "rebooting"
DeviceDeploymentStatusPending = "pending"
DeviceDeploymentStatusSuccess = "success"
DeviceDeploymentStatusFailure = "failure"
DeviceDeploymentStatusNoArtifact = "noartifact"
DeviceDeploymentStatusAlreadyInst = "already-installed"
DeviceDeploymentStatusAborted = "aborted"
DeviceDeploymentStatusDecommissioned = "decommissioned"
)
// DeviceDeploymentStatus is a helper type for reporting status changes through
// the layers
type DeviceDeploymentStatus struct {
// status reported by device
Status string `valid:"required"`
// substate reported by device
SubState *string
// finish time
FinishTime *time.Time
}
type DeviceDeployment struct {
// Internal field of initial creation of deployment
Created *time.Time `json:"created" valid:"required"`
// Update finish time
Finished *time.Time `json:"finished,omitempty" valid:"-"`
// Status
Status *string `json:"status" valid:"required"`
// Device id
DeviceId *string `json:"id" valid:"required"`
// Deployment id
DeploymentId *string `json:"-" valid:"uuidv4,required"`
// ID
Id *string `json:"-" bson:"_id" valid:"uuidv4,required"`
// Assigned software image
Image *images.SoftwareImage `json:"-" valid:"-"`
// Target device type
DeviceType *string `json:"device_type,omitempty" valid:"-"`
// Presence of deployment log
IsLogAvailable bool `json:"log" valid:"-" bson:"log"`
// Device reported substate
SubState *string `json:"substate,omitempty" valid:"-" bson:"substate"`
}
func NewDeviceDeployment(deviceId, deploymentId string) *DeviceDeployment {
now := time.Now()
initStatus := DeviceDeploymentStatusPending
id := uuid.NewV4().String()
return &DeviceDeployment{
Status: &initStatus,
DeviceId: &deviceId,
DeploymentId: &deploymentId,
Id: &id,
Created: &now,
IsLogAvailable: false,
}
}
func (d *DeviceDeployment) Validate() error {
_, err := govalidator.ValidateStruct(d)
return err
}
// Deployment statistics wrapper, each value carries a count of deployments
// aggregated by state.
type Stats map[string]int
func NewDeviceDeploymentStats() Stats {
statuses := []string{
DeviceDeploymentStatusNoArtifact,
DeviceDeploymentStatusFailure,
DeviceDeploymentStatusSuccess,
DeviceDeploymentStatusPending,
DeviceDeploymentStatusRebooting,
DeviceDeploymentStatusInstalling,
DeviceDeploymentStatusDownloading,
DeviceDeploymentStatusAlreadyInst,
DeviceDeploymentStatusAborted,
DeviceDeploymentStatusDecommissioned,
}
s := make(Stats)
// populate statuses with 0s
for _, v := range statuses {
s[v] = 0
}
return s
}
func IsDeviceDeploymentStatusFinished(status string) bool {
if status == DeviceDeploymentStatusFailure || status == DeviceDeploymentStatusSuccess ||
status == DeviceDeploymentStatusNoArtifact || status == DeviceDeploymentStatusAlreadyInst ||
status == DeviceDeploymentStatusAborted || status == DeviceDeploymentStatusDecommissioned {
return true
}
return false
}
// ActiveDeploymentStatuses lists statuses that represent deployment in active state (not finished).
func ActiveDeploymentStatuses() []string {
return []string{
DeviceDeploymentStatusPending,
DeviceDeploymentStatusDownloading,
DeviceDeploymentStatusInstalling,
DeviceDeploymentStatusRebooting,
}
}
// InstalledDeviceDeployment describes a deployment currently installed on the
// device, usually reported by a device
type InstalledDeviceDeployment struct {
Artifact string `valid:"required"`
DeviceType string `valid:"required"`
}
func (i *InstalledDeviceDeployment) Validate() error {
_, err := govalidator.ValidateStruct(i)
return err
}