Skip to content

Commit

Permalink
FS-1180 Fix for updating firmware with multiple models (#160)
Browse files Browse the repository at this point in the history
* FS-1180 Fix for updating firmware with multiple models
* Update go version in CI
* Update version of golangci-lint
* Update version of go in goreleaser
  • Loading branch information
coffeefreak101 authored Jan 30, 2024
1 parent 389ac6f commit 1825239
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 55 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
name: 🏗️ Lint, test and build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
- name: Set up Go 1.21
uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.21'
id: go

- name: Check out code into the Go module directory
Expand All @@ -31,7 +31,7 @@ jobs:
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.48.0
version: v1.55.2
args: --config .golangci.yml --timeout=5m

- name: Run firmware-syncer tests
Expand All @@ -54,10 +54,10 @@ jobs:
if: ( contains(github.ref, 'tags') && ! contains(github.ref, 'staging') )
needs: lint-test-build-artifacts
steps:
- name: Set up Go 1.19
- name: Set up Go 1.21
uses: actions/setup-go@v4
with:
go-version: '1.19'
go-version: '1.21'
id: go

- name: Set git token env var
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: 1.21
- name: install cosign
uses: sigstore/cosign-installer@main
- uses: anchore/sbom-action/[email protected]
Expand Down
54 changes: 41 additions & 13 deletions internal/inventory/serverservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"net/url"
"path"
"slices"
"sort"
"strings"

"github.com/coreos/go-oidc"
Expand Down Expand Up @@ -113,28 +115,54 @@ func (s *serverService) Publish(ctx context.Context, newFirmware *serverservice.
uuids[i] = firmwares[i].UUID.String()
}

uuidLog := strings.Join(uuids, ",")

s.logger.WithField("uuids", uuidLog).
s.logger.WithField("matchingUUIDs", uuids).
WithField("checksum", newFirmware.Checksum).
WithField("firmware", newFirmware.Filename).
WithField("vendor", newFirmware.Vendor).
WithField("version", newFirmware.Version).
Error("Multiple firmware IDs found with checksum")

return errors.Wrap(ErrServerServiceDuplicateFirmware, uuidLog)
return errors.Wrap(ErrServerServiceDuplicateFirmware, strings.Join(uuids, ","))
}

newFirmware.UUID = firmwares[0].UUID
currentFirmware := &firmwares[0]
newFirmware.UUID = currentFirmware.UUID
newFirmware.Model = mergeModels(currentFirmware.Model, newFirmware.Model)

if isDifferent(newFirmware, &firmwares[0]) {
if isDifferent(newFirmware, currentFirmware) {
return s.updateFirmware(ctx, newFirmware)
}

s.logger.WithField("firmware", newFirmware.Filename).
WithField("uuid", newFirmware.UUID).
WithField("vendor", newFirmware.Vendor).
WithField("version", newFirmware.Version).
Debug("Firmware already exists and is up to date")

return nil
}

func mergeModels(models1, models2 []string) []string {
allModels := []string(nil)
modelsSet := make(map[string]bool)

for _, model := range models1 {
modelsSet[model] = true
}

for _, model := range models2 {
modelsSet[model] = true
}

for model := range modelsSet {
allModels = append(allModels, model)
}

sort.Strings(allModels) // For consistent ordering

return allModels
}

func isDifferent(firmware1, firmware2 *serverservice.ComponentFirmwareVersion) bool {
if firmware1.Vendor != firmware2.Vendor {
return true
Expand Down Expand Up @@ -164,22 +192,21 @@ func isDifferent(firmware1, firmware2 *serverservice.ComponentFirmwareVersion) b
return true
}

if strings.Join(firmware1.Model, ",") != strings.Join(firmware2.Model, ",") {
if !slices.Equal(firmware1.Model, firmware2.Model) {
return true
}

return false
}

func (s *serverService) createFirmware(ctx context.Context, firmware *serverservice.ComponentFirmwareVersion) error {
id, response, err := s.client.CreateServerComponentFirmware(ctx, *firmware)

id, _, err := s.client.CreateServerComponentFirmware(ctx, *firmware)
if err != nil {
return errors.Wrap(ErrServerServiceQuery, "CreateServerComponentFirmware: "+err.Error())
}

s.logger.WithField("response", response).
WithField("firmware", firmware.Filename).
s.logger.WithField("firmware", firmware.Filename).
WithField("version", firmware.Version).
WithField("vendor", firmware.Vendor).
WithField("uuid", id).
Info("Created firmware")
Expand All @@ -188,14 +215,15 @@ func (s *serverService) createFirmware(ctx context.Context, firmware *serverserv
}

func (s *serverService) updateFirmware(ctx context.Context, firmware *serverservice.ComponentFirmwareVersion) error {
response, err := s.client.UpdateServerComponentFirmware(ctx, firmware.UUID, *firmware)
_, err := s.client.UpdateServerComponentFirmware(ctx, firmware.UUID, *firmware)
if err != nil {
return errors.Wrap(ErrServerServiceQuery, "UpdateServerComponentFirmware: "+err.Error())
}

s.logger.WithField("firmware", firmware.Filename).
WithField("uuid", firmware.UUID).
WithField("version", firmware.Version).
WithField("vendor", firmware.Vendor).
WithField("response", response).
Info("Updated firmware")

return nil
Expand Down
Loading

0 comments on commit 1825239

Please sign in to comment.