Skip to content

Commit b4406cc

Browse files
committed
[#78] New status DOWNLOADED_FILE_STORED is added at the end of download success with reporting downloaded path in it
Signed-off-by: Mahesha Mutharayappa (SDS/EPE2) <[email protected]>
1 parent aceebb9 commit b4406cc

File tree

8 files changed

+66
-31
lines changed

8 files changed

+66
-31
lines changed

hawkbit/lib_software_module_id.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package hawkbit
1616
const (
1717
softwareModuleNameParam = "name"
1818
softwareModuleVersionParam = "version"
19+
softwareModuleStoredPath = "path"
1920
)
2021

2122
// SoftwareModuleID represents an unique identifier for software modules.
@@ -24,4 +25,6 @@ type SoftwareModuleID struct {
2425
Name string `json:"name"`
2526
// Version of the software module.
2627
Version string `json:"version"`
28+
// Stored downloaded file path
29+
Path string `json:"path,omitempty"`
2730
}

hawkbit/lib_status.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ type Status string
1717

1818
// Supported operation statuses.
1919
const (
20-
StatusStarted Status = "STARTED"
21-
StatusDownloading Status = "DOWNLOADING"
22-
StatusDownloadingWaiting Status = "DOWNLOADING_WAITING"
23-
StatusDownloaded Status = "DOWNLOADED"
24-
StatusInstalling Status = "INSTALLING"
25-
StatusInstallingWaiting Status = "INSTALLING_WAITING"
26-
StatusInstalled Status = "INSTALLED"
27-
StatusRemoving Status = "REMOVING"
28-
StatusRemovingWaiting Status = "REMOVING_WAITING"
29-
StatusRemoved Status = "REMOVED"
30-
StatusCancelingWaiting Status = "CANCELING_WAITING"
31-
StatusCancelRejected Status = "CANCEL_REJECTED"
32-
StatusFinishedCanceled Status = "FINISHED_CANCELED"
33-
StatusFinishedError Status = "FINISHED_ERROR"
34-
StatusFinishedSuccess Status = "FINISHED_SUCCESS"
35-
StatusFinishedWarning Status = "FINISHED_WARNING"
36-
StatusFinishedRejected Status = "FINISHED_REJECTED"
20+
StatusStarted Status = "STARTED"
21+
StatusDownloading Status = "DOWNLOADING"
22+
StatusDownloadingWaiting Status = "DOWNLOADING_WAITING"
23+
StatusDownloaded Status = "DOWNLOADED"
24+
StatusDownloadedFileStored Status = "DOWNLOADED_FILE_STORED"
25+
StatusInstalling Status = "INSTALLING"
26+
StatusInstallingWaiting Status = "INSTALLING_WAITING"
27+
StatusInstalled Status = "INSTALLED"
28+
StatusRemoving Status = "REMOVING"
29+
StatusRemovingWaiting Status = "REMOVING_WAITING"
30+
StatusRemoved Status = "REMOVED"
31+
StatusCancelingWaiting Status = "CANCELING_WAITING"
32+
StatusCancelRejected Status = "CANCEL_REJECTED"
33+
StatusFinishedCanceled Status = "FINISHED_CANCELED"
34+
StatusFinishedError Status = "FINISHED_ERROR"
35+
StatusFinishedSuccess Status = "FINISHED_SUCCESS"
36+
StatusFinishedWarning Status = "FINISHED_WARNING"
37+
StatusFinishedRejected Status = "FINISHED_REJECTED"
3738
)

hawkbit/software_updatable.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,16 @@ func MapLastOperation(LastOperationPtr *OperationStatus) map[string]interface{}
140140
lastOperationMap[statusParam] = LastOperationPtr.Status
141141

142142
if LastOperationPtr.SoftwareModule != nil {
143-
lastOperationMap[softwareModuleParam] = map[string]interface{}{
143+
innerMap := map[string]interface{}{
144144
softwareModuleNameParam: LastOperationPtr.SoftwareModule.Name,
145145
softwareModuleVersionParam: LastOperationPtr.SoftwareModule.Version,
146146
}
147+
148+
if LastOperationPtr.SoftwareModule.Path != "" {
149+
innerMap[softwareModuleStoredPath] = LastOperationPtr.SoftwareModule.Path
150+
}
151+
152+
lastOperationMap[softwareModuleParam] = innerMap
147153
}
148154

149155
if LastOperationPtr.Software != nil {
@@ -195,3 +201,8 @@ func (su *SoftwareUpdatable) SetLastOperation(os *OperationStatus) error {
195201
}
196202
return su.setProperty(suPropertyLastOperation, MapLastOperation(su.status.LastOperation))
197203
}
204+
205+
// GetLastStatus returns the recorded last operation status
206+
func (su *SoftwareUpdatable) GetLastStatus() *softwareUpdatableStatus {
207+
return su.status
208+
}

internal/feature_download.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ func (f *ScriptBasedSoftwareUpdatable) downloadModules(
5252
}
5353

5454
// Archive all modules.
55-
for i, module := range updatable.Modules {
56-
if err := f.store.ArchiveModule(filepath.Join(toDir, strconv.Itoa(i))); err != nil {
57-
logger.Errorf("failed to archive module [%s.%s]: %v", module.Name, module.Version, err)
55+
if su.GetLastStatus().LastOperation.Status == hawkbit.StatusFinishedSuccess {
56+
for i, module := range updatable.Modules {
57+
if err := f.store.ArchiveModule(filepath.Join(toDir, strconv.Itoa(i)), &module.Path); err != nil {
58+
logger.Errorf("failed to archive module [%s.%s]: %v", module.Name, module.Version, err)
59+
} else {
60+
logger.Debugf("Archived downloaded file path: %s", module.Path)
61+
setLastOS(su, newOS(updatable.CorrelationID, module, hawkbit.StatusDownloadedFileStored))
62+
}
5863
}
5964
}
6065

internal/feature_internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (f *ScriptBasedSoftwareUpdatable) fail(cid string, modules []*hawkbit.Softw
175175
// newOS returns newly created OperationStatus pointer.
176176
func newOS(cid string, module *storage.Module, status hawkbit.Status) *hawkbit.OperationStatus {
177177
return hawkbit.NewOperationStatusUpdate(cid, status,
178-
&hawkbit.SoftwareModuleID{Name: module.Name, Version: module.Version})
178+
&hawkbit.SoftwareModuleID{Name: module.Name, Version: module.Version, Path: module.Path})
179179
}
180180

181181
// newFileOS returns newly created OperationStatus pointer, filled with the status file data.

internal/feature_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,18 @@ func testDisconnectWhileRunningOperation(feature *ScriptBasedSoftwareUpdatable,
185185
postDisconnectEventCount := 3 // DOWNLOADING(100)/INSTALLING(100), DOWNLOADED(100)/INSTALLED(100), FINISHED_SUCCESS
186186
if install {
187187
preDisconnectEventCount = 5 // STARTED, DOWNLOADING(0), DOWNLOADING(100), DOWNLOADED(100), INSTALLING(0)
188+
} else {
189+
postDisconnectEventCount += 1 //DOWNLOADED_FILE_STORED
188190
}
189-
statuses := pullStatusChanges(mc, preDisconnectEventCount) // should go between DOWNLOADING/INSTALLING and next state
191+
192+
statuses := pullStatusChanges(mc, install, preDisconnectEventCount) // should go between DOWNLOADING/INSTALLING and next state
190193

191194
go func() { // decrements count number with 1, when disconnected
192195
feature.Disconnect(false)
193196
waitDisconnect.Done()
194197
}()
195198

196-
statuses = append(statuses, pullStatusChanges(mc, postDisconnectEventCount)...)
199+
statuses = append(statuses, pullStatusChanges(mc, install, postDisconnectEventCount)...)
197200
waitDisconnect.Wait()
198201
defer connectFeature(t, mc, feature, NewDefaultConfig().FeatureID)
199202
if install {
@@ -310,13 +313,19 @@ func testScriptBasedSoftwareUpdatableOperationsLocal(t *testing.T, installDirs [
310313
testDownloadInstall(feature, mc, artifacts, expectedSuccess, copyArtifacts, t)
311314
}
312315

313-
func pullStatusChanges(mc *mockedClient, expectedCount int) []interface{} {
316+
func pullStatusChanges(mc *mockedClient, install bool, expectedCount int) []interface{} {
314317
var statuses []interface{}
315318
for i := 0; i < expectedCount; i++ {
316319
lo := mc.pullLastOperationStatus()
317320
statuses = append(statuses, lo)
318-
if lo["status"] == string(hawkbit.StatusFinishedSuccess) || lo["status"] == string(hawkbit.StatusFinishedError) {
319-
break
321+
if install {
322+
if lo["status"] == string(hawkbit.StatusFinishedSuccess) || lo["status"] == string(hawkbit.StatusFinishedError) {
323+
break
324+
}
325+
} else {
326+
if lo["status"] == string(hawkbit.StatusDownloadedFileStored) || lo["status"] == string(hawkbit.StatusFinishedError) {
327+
break
328+
}
320329
}
321330
}
322331
return statuses
@@ -334,7 +343,8 @@ func testDownloadInstall(feature *ScriptBasedSoftwareUpdatable, mc *mockedClient
334343
// Try to execute a simple download operation.
335344
feature.downloadHandler(sua, feature.su)
336345

337-
statuses := pullStatusChanges(mc, 5+extraDownloadingEventsCount) // STARTED, DOWNLOADING(0), DOWNLOADING(x extraDownloadingEventsCount), DOWNLOADING(100), DOWNLOADED(100), FINISHED_SUCCESS
346+
statuses := pullStatusChanges(mc, false, 6+extraDownloadingEventsCount) // STARTED, DOWNLOADING(0), DOWNLOADING(x extraDownloadingEventsCount),
347+
// DOWNLOADING(100), DOWNLOADED(100), FINISHED_SUCCESS, DOWNLOADED_FILE_STORED
338348
if expectedSuccess {
339349
checkDownloadStatusEvents(extraDownloadingEventsCount, statuses, t)
340350
if copyArtifacts == "" {
@@ -349,7 +359,8 @@ func testDownloadInstall(feature *ScriptBasedSoftwareUpdatable, mc *mockedClient
349359
// Try to execute a simple install operation.
350360
feature.installHandler(sua, feature.su)
351361

352-
statuses = pullStatusChanges(mc, 8+extraDownloadingEventsCount) // STARTED, DOWNLOADING(0), DOWNLOADING(x extraDownloadingEventsCount), DOWNLOADING(100), DOWNLOADED(100),
362+
statuses = pullStatusChanges(mc, true, 8+extraDownloadingEventsCount) // STARTED, DOWNLOADING(0), DOWNLOADING(x extraDownloadingEventsCount),
363+
//DOWNLOADING(100), DOWNLOADED(100),
353364
// INSTALLING(0), INSTALLING(100), INSTALLED(100), FINISHED_SUCCESS
354365
if expectedSuccess {
355366
checkInstallStatusEvents(extraDownloadingEventsCount, statuses, t)
@@ -391,6 +402,7 @@ func checkDownloadStatusEvents(extraDownloadingEventsCount int, actualStatuses [
391402
createStatus(hawkbit.StatusDownloading, completeProgress, noMessage),
392403
createStatus(hawkbit.StatusDownloaded, completeProgress, noMessage),
393404
createStatus(hawkbit.StatusFinishedSuccess, nil, noMessage),
405+
createStatus(hawkbit.StatusDownloadedFileStored, nil, noMessage),
394406
)
395407
checkStatusEvents(expectedStatuses, actualStatuses, t)
396408
}

internal/storage/storage.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Updatable struct {
5858
type Module struct {
5959
Name string `json:"name"`
6060
Version string `json:"version"`
61+
Path string `json:"path,omitempty"`
6162
Artifacts []*Artifact `json:"artifacts,omitempty"`
6263
Metadata map[string]string `json:"metadata,omitempty"`
6364
}
@@ -212,12 +213,13 @@ func (st *Storage) MoveInstalledDeps(dir string, metadata map[string]string) err
212213
}
213214

214215
// ArchiveModule to modules directory.
215-
func (st *Storage) ArchiveModule(dir string) error {
216+
func (st *Storage) ArchiveModule(dir string, dest *string) error {
216217
logger.Debugf("Archive module from directory: %s", dir)
217218
path, err := FindAvailableLocation(st.ModulesPath)
218219
if err != nil {
219220
return err
220221
}
222+
*dest = path
221223
if err := os.MkdirAll(path, 0755); err != nil {
222224
return err
223225
}

internal/storage/storage_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,11 @@ func TestDownloadArchiveModule(t *testing.T) {
248248
existence(filepath.Join(path, art.FileName), true, "[initial download]", t)
249249

250250
// 2. Archive module.
251+
storedPath := ""
251252
if err := WriteLn(filepath.Join(path, InternalStatusName), m.Name+":"+m.Version); err != nil {
252253
t.Fatalf("fail to write module id: %v", err)
253254
}
254-
if err := store.ArchiveModule(path); err != nil {
255+
if err := store.ArchiveModule(path, &storedPath); err != nil {
255256
t.Fatalf("fail to archive module: %v", err)
256257
}
257258
existence(filepath.Join(store.ModulesPath, "0", art.FileName), true, "[archive]", t)

0 commit comments

Comments
 (0)