Skip to content

Commit

Permalink
initial fixes for 0.8 hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlapao committed Jun 3, 2024
1 parent 20a5ecb commit 8fd8639
Show file tree
Hide file tree
Showing 31 changed files with 801 additions and 362 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

## [0.8.0] - 2024-06-03

### Fixed

- Fixed an issue where we were trying to get the virtual machines for other users
when not being a super admin

### Changed

- Moved database saving process to a 5 minutes setting to avoid overloading the
database with too many requests
- Changed the way the orchestrator was checking for VMs status changes to avoid
overloading the database
- Moved all the old commands to the new exec with context to enable timeouts
- Added a 30 seconds timeout when checking the status of the local vms

## [0.7.1] - 2024-05-29

### Added
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func registerPerformanceHandlers(ctx basecontext.ApiContext, version string) {
WithVersion(version).WithPath("/performance/db").
WithHandler(PerformDbTestHandler()).
Register()

}

func PerformDbTestHandler() restapi.ControllerHandler {
Expand Down Expand Up @@ -57,7 +56,7 @@ func PerformDbTestHandler() restapi.ControllerHandler {
for i := 0; i < request.TestCount; i++ {
ctx.LogInfof("This is a test log")
for j := 0; j < request.ConsecutiveCalls; j++ {
go dbService.Save(ctx)
go dbService.SaveNow(ctx)
if request.TimeBetweenConsecutiveCalls > 0 {
time.Sleep(time.Duration(request.TimeBetweenConsecutiveCalls) * time.Millisecond)
}
Expand Down
31 changes: 20 additions & 11 deletions src/data/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ func (j *JsonDatabase) CreateApiKey(ctx basecontext.ApiContext, apiKey models.Ap
apiKey.UpdatedAt = helpers.GetUtcCurrentDateTime()
apiKey.CreatedAt = helpers.GetUtcCurrentDateTime()
j.data.ApiKeys = append(j.data.ApiKeys, apiKey)
if err := j.Save(ctx); err != nil {
return nil, err
}

return &apiKey, nil
}
Expand All @@ -86,10 +83,16 @@ func (j *JsonDatabase) DeleteApiKey(ctx basecontext.ApiContext, id string) error

for i, apiKey := range j.data.ApiKeys {
if strings.EqualFold(apiKey.ID, id) || strings.EqualFold(apiKey.Name, id) || strings.EqualFold(apiKey.Key, id) {
j.data.ApiKeys = append(j.data.ApiKeys[:i], j.data.ApiKeys[i+1:]...)
if err := j.Save(ctx); err != nil {
return err
for {
if j.data.ApiKeys[i].DbRecord.IsLocked {
continue
}
LockRecord(ctx, j.data.ApiKeys[i].DbRecord)
j.data.ApiKeys = append(j.data.ApiKeys[:i], j.data.ApiKeys[i+1:]...)
UnlockRecord(ctx, j.data.ApiKeys[i].DbRecord)
break
}

return nil
}
}
Expand All @@ -107,12 +110,18 @@ func (j *JsonDatabase) UpdateKey(ctx basecontext.ApiContext, key models.ApiKey)
continue
}

j.data.ApiKeys[i].Revoked = key.Revoked
j.data.ApiKeys[i].RevokedAt = key.RevokedAt
j.data.ApiKeys[i].UpdatedAt = helpers.GetUtcCurrentDateTime()
if err := j.Save(ctx); err != nil {
return err
for {
if j.data.ApiKeys[i].DbRecord.IsLocked {
continue
}
LockRecord(ctx, j.data.ApiKeys[i].DbRecord)
j.data.ApiKeys[i].Revoked = key.Revoked
j.data.ApiKeys[i].RevokedAt = key.RevokedAt
j.data.ApiKeys[i].UpdatedAt = helpers.GetUtcCurrentDateTime()
UnlockRecord(ctx, j.data.ApiKeys[i].DbRecord)
break
}

return nil
}

Expand Down
64 changes: 64 additions & 0 deletions src/data/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package data

import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"

"github.com/Parallels/prl-devops-service/basecontext"
"github.com/cjlapao/common-go/helper"
)

func (j *JsonDatabase) Backup(ctx basecontext.ApiContext) error {
backupFiles, err := findBackupFiles(j.filename)
if err != nil {
ctx.LogErrorf("[Database] Error finding backup files: %v", err)
return err
}

if len(backupFiles) >= j.Config.NumberOfBackupFiles {
// Delete the oldest backup file
oldestFile := backupFiles[0]
err := os.Remove(oldestFile)
if err != nil {
ctx.LogErrorf("[Database] Error deleting backup file: %v", err)
return err
}
}

// Create a new backup file with timestamp
timestamp := time.Now().Format("20060102150405")
newBackupFile := fmt.Sprintf("%s.save.bak.%s", j.filename, timestamp)
err = helper.CopyFile(j.filename, newBackupFile)
if err != nil {
ctx.LogErrorf("[Database] Error creating new backup file: %v", err)
return err
}

return nil
}

func findBackupFiles(filename string) ([]string, error) {
dir := filepath.Dir(filename)
base := filepath.Base(filename)
pattern := fmt.Sprintf("%s.save.bak.*", base)
matches, err := filepath.Glob(filepath.Join(dir, pattern))
if err != nil {
return nil, err
}

// Sort the backup files by timestamp
sort.Slice(matches, func(i, j int) bool {
return extractTimestamp(matches[i]) < extractTimestamp(matches[j])
})

return matches, nil
}

func extractTimestamp(filename string) string {
parts := strings.Split(filename, ".")
return parts[len(parts)-1]
}
59 changes: 0 additions & 59 deletions src/data/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,6 @@ func (j *JsonDatabase) CreateCatalogManifest(ctx basecontext.ApiContext, manifes
manifest.UpdatedAt = helpers.GetUtcCurrentDateTime()

j.data.ManifestsCatalog = append(j.data.ManifestsCatalog, manifest)

if err := j.Save(ctx); err != nil {
return nil, err
}
return &manifest, nil
}

Expand All @@ -237,7 +233,6 @@ func (j *JsonDatabase) DeleteCatalogManifest(ctx basecontext.ApiContext, catalog
return ErrCatalogManifestNotFound
}

found := false
for {
catalogManifests, err := j.GetCatalogManifests(ctx, "")
if err != nil {
Expand All @@ -253,7 +248,6 @@ func (j *JsonDatabase) DeleteCatalogManifest(ctx basecontext.ApiContext, catalog
}
j.data.ManifestsCatalog = append(j.data.ManifestsCatalog[:index], j.data.ManifestsCatalog[index+1:]...)
deletedSomething = true
found = true
break
}
}
Expand All @@ -263,13 +257,6 @@ func (j *JsonDatabase) DeleteCatalogManifest(ctx basecontext.ApiContext, catalog
}
}

if found {
if err := j.Save(ctx); err != nil {
return err
}
return nil
}

return ErrCatalogManifestNotFound
}

Expand All @@ -296,9 +283,6 @@ func (j *JsonDatabase) DeleteCatalogManifestVersion(ctx basecontext.ApiContext,
if (strings.EqualFold(manifest.ID, catalogIdOrId) || strings.EqualFold(manifest.CatalogId, catalogIdOrId)) &&
strings.EqualFold(manifest.Version, version) {
j.data.ManifestsCatalog = append(j.data.ManifestsCatalog[:i], j.data.ManifestsCatalog[i+1:]...)
if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -329,9 +313,6 @@ func (j *JsonDatabase) DeleteCatalogManifestVersionArch(ctx basecontext.ApiConte
continue
}
j.data.ManifestsCatalog = append(j.data.ManifestsCatalog[:i], j.data.ManifestsCatalog[i+1:]...)
if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -370,9 +351,6 @@ func (j *JsonDatabase) UpdateCatalogManifest(ctx basecontext.ApiContext, record
j.data.ManifestsCatalog[i].RequiredClaims = record.RequiredClaims
j.data.ManifestsCatalog[i].RequiredRoles = record.RequiredRoles

if err := j.Save(ctx); err != nil {
return nil, err
}
return &j.data.ManifestsCatalog[i], nil
}
}
Expand All @@ -389,9 +367,6 @@ func (j *JsonDatabase) UpdateCatalogManifestTags(ctx basecontext.ApiContext, rec
if strings.EqualFold(manifest.ID, record.ID) || (strings.EqualFold(manifest.CatalogId, record.CatalogId) && strings.EqualFold(manifest.Version, record.Version)) {
j.data.ManifestsCatalog[i].Tags = record.Tags

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -420,9 +395,6 @@ func (j *JsonDatabase) UpdateCatalogManifestRequiredRoles(ctx basecontext.ApiCon
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -462,9 +434,6 @@ func (j *JsonDatabase) AddCatalogManifestRequiredClaims(ctx basecontext.ApiConte
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -492,10 +461,6 @@ func (j *JsonDatabase) RemoveCatalogManifestRequiredClaims(ctx basecontext.ApiCo
j.data.ManifestsCatalog[i].RequiredClaims = append(j.data.ManifestsCatalog[i].RequiredClaims[:foundAt], j.data.ManifestsCatalog[i].RequiredClaims[foundAt+1:]...)
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -535,9 +500,6 @@ func (j *JsonDatabase) AddCatalogManifestRequiredRoles(ctx basecontext.ApiContex
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -566,9 +528,6 @@ func (j *JsonDatabase) RemoveCatalogManifestRequiredRoles(ctx basecontext.ApiCon
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -597,9 +556,6 @@ func (j *JsonDatabase) AddCatalogManifestTags(ctx basecontext.ApiContext, record
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand Down Expand Up @@ -628,9 +584,6 @@ func (j *JsonDatabase) RemoveCatalogManifestTags(ctx basecontext.ApiContext, rec
}
}

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand All @@ -655,9 +608,6 @@ func (j *JsonDatabase) UpdateCatalogManifestDownloadCount(ctx basecontext.ApiCon
j.data.ManifestsCatalog[i].LastDownloadedUser = downloadUser
j.data.ManifestsCatalog[i].DownloadCount = j.data.ManifestsCatalog[i].DownloadCount + 1

if err := j.Save(ctx); err != nil {
return err
}
return nil
}
}
Expand All @@ -682,9 +632,6 @@ func (j *JsonDatabase) TaintCatalogManifestVersion(ctx basecontext.ApiContext, c
j.data.ManifestsCatalog[i].Tainted = true
j.data.ManifestsCatalog[i].TaintedBy = taintUser

if err := j.Save(ctx); err != nil {
return nil, err
}
return &j.data.ManifestsCatalog[i], nil
}
}
Expand All @@ -710,9 +657,6 @@ func (j *JsonDatabase) UnTaintCatalogManifestVersion(ctx basecontext.ApiContext,
j.data.ManifestsCatalog[i].UnTaintedBy = unTaintUser
j.data.ManifestsCatalog[i].TaintedBy = ""

if err := j.Save(ctx); err != nil {
return nil, err
}
return &j.data.ManifestsCatalog[i], nil
}
}
Expand All @@ -737,9 +681,6 @@ func (j *JsonDatabase) RevokeCatalogManifestVersion(ctx basecontext.ApiContext,
j.data.ManifestsCatalog[i].Revoked = true
j.data.ManifestsCatalog[i].RevokedBy = revokeUser

if err := j.Save(ctx); err != nil {
return nil, err
}
return &j.data.ManifestsCatalog[i], nil
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/data/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ func (j *JsonDatabase) CreateClaim(ctx basecontext.ApiContext, claim models.Clai
}

j.data.Claims = append(j.data.Claims, claim)
if err := j.Save(ctx); err != nil {
return nil, err
}

return &claim, nil
}
Expand Down Expand Up @@ -101,9 +98,7 @@ func (j *JsonDatabase) DeleteClaim(ctx basecontext.ApiContext, idOrName string)
}
}
}
if err := j.Save(ctx); err != nil {
return err
}

return nil
}
}
Expand Down Expand Up @@ -135,9 +130,7 @@ func (j *JsonDatabase) UpdateClaim(ctx basecontext.ApiContext, claim *models.Cla
}
}
}
if err := j.Save(ctx); err != nil {
return nil, err
}

return claim, nil
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/data/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package data

import "encoding/json"

func Diff(a interface{}, b interface{}) bool {
jsonA, err := json.Marshal(a)
if err != nil {
return false
}
jsonB, err := json.Marshal(b)
if err != nil {
return false
}

return string(jsonA) != string(jsonB)
}
Loading

0 comments on commit 8fd8639

Please sign in to comment.