• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1622978334

13 Jan 2025 03:51PM UTC coverage: 72.802% (-3.8%) from 76.608%
1622978334

Pull #300

gitlab-ci

alfrunes
fix: Deployment device count should not exceed max devices

Added a condition to skip deployments when the device count reaches max
devices.

Changelog: Title
Ticket: MEN-7847
Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #300: fix: Deployment device count should not exceed max devices

4251 of 6164 branches covered (68.96%)

Branch coverage included in aggregate %.

0 of 18 new or added lines in 1 file covered. (0.0%)

2544 existing lines in 83 files now uncovered.

42741 of 58384 relevant lines covered (73.21%)

21.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.13
/backend/services/deployments/app/app_os.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14

15
package app
16

17
import (
18
        "context"
19

20
        "github.com/pkg/errors"
21

22
        "github.com/mendersoftware/mender-server/pkg/log"
23

24
        "github.com/mendersoftware/mender-server/services/deployments/model"
25
)
26

27
func (d *Deployments) isAlreadyInstalled(
28
        request *model.DeploymentNextRequest,
29
        deviceDeployment *model.DeviceDeployment,
30
) bool {
1✔
31
        if request == nil ||
1✔
32
                request.DeviceProvides == nil ||
1✔
33
                deviceDeployment == nil ||
1✔
34
                deviceDeployment.Image == nil ||
1✔
35
                deviceDeployment.Image.ArtifactMeta == nil {
1✔
36
                return false
×
37
        }
×
38

39
        // check if the device reported same artifact name as the one
40
        // in the artifact selected for a given device deployment
41
        return request.DeviceProvides.ArtifactName == deviceDeployment.Image.ArtifactMeta.Name
1✔
42
}
43

44
func (d *Deployments) handleAlreadyInstalled(
45
        ctx context.Context,
46
        deviceDeployment *model.DeviceDeployment,
47
) error {
1✔
48
        l := log.FromContext(ctx)
1✔
49
        if err := d.updateDeviceDeploymentStatus(
1✔
50
                ctx,
1✔
51
                deviceDeployment,
1✔
52
                model.DeviceDeploymentState{
1✔
53
                        Status: model.DeviceDeploymentStatusAlreadyInst,
1✔
54
                }); err != nil {
1✔
55
                return errors.Wrap(err, "Failed to update deployment status")
×
56
        }
×
57
        if err := d.reindexDevice(ctx, deviceDeployment.DeviceId); err != nil {
1✔
58
                l.Warn(errors.Wrap(err, "failed to trigger a device reindex"))
×
59
        }
×
60
        if err := d.reindexDeployment(ctx, deviceDeployment.DeviceId,
1✔
61
                deviceDeployment.DeploymentId, deviceDeployment.Id); err != nil {
1✔
62
                l.Warn(errors.Wrap(err, "failed to trigger a device reindex"))
×
63
        }
×
64

65
        return nil
1✔
66
}
67

68
// assignArtifact assigns artifact to the device deployment
69
func (d *Deployments) assignArtifact(
70
        ctx context.Context,
71
        deployment *model.Deployment,
72
        deviceDeployment *model.DeviceDeployment,
73
        installed *model.InstalledDeviceDeployment) error {
1✔
74

1✔
75
        // Assign artifact to the device deployment.
1✔
76
        var artifact *model.Image
1✔
77
        var err error
1✔
78

1✔
79
        if err = installed.Validate(); err != nil {
1✔
80
                return err
×
81
        }
×
82

83
        if deviceDeployment.DeploymentId == "" || deviceDeployment.DeviceId == "" {
1✔
84
                return ErrModelInternal
×
85
        }
×
86

87
        // Clear device deployment image
88
        // New artifact will be selected for the device deployment
89
        deviceDeployment.Image = nil
1✔
90

1✔
91
        // First case is for backward compatibility.
1✔
92
        // It is possible that there is old deployment structure in the system.
1✔
93
        // In such case we need to select artifact using name and device type.
1✔
94
        if len(deployment.Artifacts) == 0 {
2✔
95
                artifact, err = d.db.ImageByNameAndDeviceType(
1✔
96
                        ctx,
1✔
97
                        installed.ArtifactName,
1✔
98
                        installed.DeviceType,
1✔
99
                )
1✔
100
                if err != nil {
1✔
101
                        return errors.Wrap(err, "assigning artifact to device deployment")
×
102
                }
×
UNCOV
103
        } else {
×
UNCOV
104
                // Select artifact for the device deployment from artifacts assigned to the deployment.
×
UNCOV
105
                artifact, err = d.db.ImageByIdsAndDeviceType(
×
UNCOV
106
                        ctx,
×
UNCOV
107
                        deployment.Artifacts,
×
UNCOV
108
                        installed.DeviceType,
×
UNCOV
109
                )
×
UNCOV
110
                if err != nil {
×
111
                        return errors.Wrap(err, "assigning artifact to device deployment")
×
112
                }
×
113
        }
114

115
        // If not having appropriate image, set noartifact status
116
        if artifact == nil {
1✔
UNCOV
117
                return d.assignNoArtifact(ctx, deviceDeployment)
×
UNCOV
118
        }
×
119

120
        if err := d.db.AssignArtifact(
1✔
121
                ctx,
1✔
122
                deviceDeployment.DeviceId,
1✔
123
                deviceDeployment.DeploymentId,
1✔
124
                artifact,
1✔
125
        ); err != nil {
1✔
126
                return errors.Wrap(err, "Assigning artifact to the device deployment")
×
127
        }
×
128

129
        deviceDeployment.Image = artifact
1✔
130

1✔
131
        return nil
1✔
132
}
133

134
func (d *Deployments) assignNoArtifact(
135
        ctx context.Context,
136
        deviceDeployment *model.DeviceDeployment,
UNCOV
137
) error {
×
UNCOV
138
        l := log.FromContext(ctx)
×
UNCOV
139
        if err := d.updateDeviceDeploymentStatus(ctx, deviceDeployment,
×
UNCOV
140
                model.DeviceDeploymentState{
×
UNCOV
141
                        Status: model.DeviceDeploymentStatusNoArtifact,
×
UNCOV
142
                }); err != nil {
×
143
                return errors.Wrap(err, "Failed to update deployment status")
×
144
        }
×
UNCOV
145
        if err := d.reindexDevice(ctx, deviceDeployment.DeviceId); err != nil {
×
146
                l.Warn(errors.Wrap(err, "failed to trigger a device reindex"))
×
147
        }
×
UNCOV
148
        if err := d.reindexDeployment(ctx, deviceDeployment.DeviceId,
×
UNCOV
149
                deviceDeployment.DeploymentId, deviceDeployment.Id); err != nil {
×
150
                l := log.FromContext(ctx)
×
151
                l.Warn(errors.Wrap(err, "failed to trigger a device reindex"))
×
152
        }
×
UNCOV
153
        return nil
×
154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc