Skip to content

Commit

Permalink
Merge pull request mendersoftware#561 from kjaskiewiczz/fix-device-ty…
Browse files Browse the repository at this point in the history
…pe-handling

fix the device_type handling in the deployments/next handler
  • Loading branch information
kjaskiewiczz committed Mar 11, 2022
2 parents 4c8212d + ddd5538 commit 3527da9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 33 deletions.
32 changes: 27 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,12 +1372,17 @@ func (d *Deployments) assignArtifact(
}

if err := d.db.AssignArtifact(
ctx, deviceDeployment.DeviceId, deviceDeployment.DeploymentId, artifact); err != nil {
ctx,
deviceDeployment.DeviceId,
deviceDeployment.DeploymentId,
artifact,
installed.DeviceType,
installed.Provides,
); err != nil {
return errors.Wrap(err, "Assigning artifact to the device deployment")
}

deviceDeployment.Image = artifact
deviceDeployment.DeviceType = installed.DeviceType

return nil
}
Expand Down Expand Up @@ -1646,8 +1651,9 @@ func (d *Deployments) getDeploymentInstructions(ctx context.Context, deployment
phase *model.DeploymentPhase) (*model.DeploymentInstructions, error) {
idata := identity.FromContext(ctx)

// assign artifact only if the artifact was not assigned previously
// or the device type has changed
// assign artifact if the artifact was not assigned previously
// or the device_type or provides from the request does not match
// the ones from the device deployment
if err := d.maybeAssignArtifact(ctx,
deployment, deviceDeployment, request.DeviceProvides, phase); err != nil {
return nil, err
Expand Down Expand Up @@ -1856,13 +1862,29 @@ func (d *Deployments) maybeAssignArtifact(
installed *model.InstalledDeviceDeployment,
phase *model.DeploymentPhase,
) error {
if devDep.Image == nil || devDep.DeviceType != installed.DeviceType {
if devDep.Image == nil ||
installed.DeviceType != devDep.DeviceType ||
!providesEqual(installed.Provides, devDep.Provides) {
return d.assignArtifact(ctx, dep, devDep, installed, phase)
}

return nil
}

func providesEqual(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}

for k, v := range a {
if w, ok := b[k]; !ok || v != w {
return false
}
}

return true
}

// UpdateDeviceDeploymentStatus will update the deployment status for device of
// ID `deviceID`. Returns nil if update was successful.
func (d *Deployments) UpdateDeviceDeploymentStatus(ctx context.Context, deploymentID string,
Expand Down
12 changes: 11 additions & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 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.
Expand Down Expand Up @@ -933,6 +933,11 @@ func TestGetDeploymentForDeviceWithCurrent(t *testing.T) {
deviceID,
deploymentID,
img,
"bagelPi64",
map[string]string{
"device_type": "bagelPi64",
"foo": "bar",
},
).Return(nil)
// </maybeAssignArtifact.assignArtifact>
return ds
Expand Down Expand Up @@ -1044,6 +1049,11 @@ func TestGetDeploymentForDeviceWithCurrent(t *testing.T) {
deviceID,
deploymentID,
img,
"bagelPi64",
map[string]string{
"device_type": "bagelPi64",
"foo": "bar",
},
).Return(nil)
// </maybeAssignArtifact.assignArtifact>
return ds
Expand Down
5 changes: 4 additions & 1 deletion model/device_deployment.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 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.
Expand Down Expand Up @@ -217,6 +217,9 @@ type DeviceDeployment struct {
// Target device type
DeviceType string `json:"device_type,omitempty" bson:"devicetype"`

// Target provides
Provides map[string]string `json:"-" bson:"provides"`

// Presence of deployment log
IsLogAvailable bool `json:"log" bson:"log"`

Expand Down
12 changes: 9 additions & 3 deletions store/datastore.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Northern.tech AS
// Copyright 2022 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.
Expand Down Expand Up @@ -80,8 +80,14 @@ type DataStore interface {
) (model.DeviceDeploymentStatus, error)
UpdateDeviceDeploymentLogAvailability(ctx context.Context,
deviceID string, deploymentID string, log bool) error
AssignArtifact(ctx context.Context, deviceID string,
deploymentID string, artifact *model.Image) error
AssignArtifact(
ctx context.Context,
deviceID string,
deploymentID string,
artifact *model.Image,
deviceType string,
provides map[string]string,
) error
AggregateDeviceDeploymentByStatus(ctx context.Context,
id string) (model.Stats, error)
GetDeviceStatusesForDeployment(ctx context.Context,
Expand Down
40 changes: 20 additions & 20 deletions store/mocks/DataStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions store/mongo/datastore_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ const (
StorageKeyDeviceDeploymentFinished = "finished"
StorageKeyDeviceDeploymentIsLogAvailable = "log"
StorageKeyDeviceDeploymentArtifact = "image"
StorageKeyDeviceDeploymentDeviceType = "devicetype"
StorageKeyDeviceDeploymentProvides = "provides"
StorageKeyDeviceDeploymentPhaseId = "phase_id"
StorageKeyDeviceDeploymentRetries = "retries"
StorageKeyDeviceDeploymentAttempts = "attempts"
Expand Down Expand Up @@ -1407,8 +1409,14 @@ func (db *DataStoreMongo) UpdateDeviceDeploymentLogAvailability(ctx context.Cont
}

// AssignArtifact assigns artifact to the device deployment
func (db *DataStoreMongo) AssignArtifact(ctx context.Context,
deviceID string, deploymentID string, artifact *model.Image) error {
func (db *DataStoreMongo) AssignArtifact(
ctx context.Context,
deviceID string,
deploymentID string,
artifact *model.Image,
deviceType string,
provides map[string]string,
) error {

// Verify ID formatting
if len(deviceID) == 0 ||
Expand All @@ -1428,7 +1436,10 @@ func (db *DataStoreMongo) AssignArtifact(ctx context.Context,

update := bson.D{
{Key: "$set", Value: bson.M{
StorageKeyDeviceDeploymentArtifact: artifact}},
StorageKeyDeviceDeploymentArtifact: artifact,
StorageKeyDeviceDeploymentDeviceType: deviceType,
StorageKeyDeviceDeploymentProvides: provides,
}},
}

if res, err := collDevs.UpdateMany(ctx, selector, update); err != nil {
Expand Down

0 comments on commit 3527da9

Please sign in to comment.