Skip to content

Commit

Permalink
initial fixes for 0.8 hotfix (#161)
Browse files Browse the repository at this point in the history
* initial fixes for 0.8 hotfix

* fixed the issue with the nil context during execution

* fixing the unhandled error in the main.go

* fixing the comments

* adding beta publishing
  • Loading branch information
cjlapao authored Jun 4, 2024
1 parent 20a5ecb commit 7688f2d
Show file tree
Hide file tree
Showing 32 changed files with 970 additions and 363 deletions.
156 changes: 156 additions & 0 deletions .github/workflows/publish_beta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
name: Publish Beta Release

on:
push:
tags:
- '*_beta'

workflow_dispatch:
inputs:
version:
description: "Version to release"
required: true

jobs:
check-version-change:
outputs:
changed: ${{ steps.check-version.outputs.result }}
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v3
- name: Check if version has changed
id: check-version
uses: actions/github-script@v6
with:
script: |
// Get the version from the workflow input
let version = '${{ github.event.inputs.version }}';
if (!version) {
fs = require('fs');
let v = '';
try {
v = fs.readFileSync('./VERSION', 'utf8');
console.log(`Version found: ${v}`);
}
catch (err) {
return false;
}
if (!v) {
return false;
} else {
version = v;
}
}
// Find a release for that version
const release = await github.rest.repos.getReleaseByTag({
owner: context.repo.owner,
repo: context.repo.repo,
tag: `v${version}-beta`,
}).catch(() => null);
// If the release exists, the version has not changed
if (release) {
console.log(`Version ${version} has an existing release`);
console.log(release.data.html_url);
core.summary.addLink(`Release v${version}`, release.data.html_url);
await core.summary.write();
return "false";
}
console.log(`Version ${version} does not have a release`);
return true;
release:
needs: check-version-change
if: ${{ needs.check-version-change.outputs.changed == 'true' }}
runs-on: ubuntu-latest
permissions:
contents: write
packages: read
env:
EXT_VERSION: "" # will be set in the workflow
outputs:
version: ${{ env.EXT_VERSION }}
steps:
- uses: actions/checkout@v3
- name: Parse version from package.json
run: |
echo "EXT_VERSION=$(cat ./VERSION)" >> "$GITHUB_ENV"
- name: Create release and upload release asset
uses: actions/github-script@v6
with:
script: |
const fs = require("fs");
const release = await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: "v${{ env.EXT_VERSION }}-beta",
name: "v${{ env.EXT_VERSION }}",
draft: false,
prerelease: true
});
core.summary.addLink(`Release v${{ env.EXT_VERSION }}`, release.data.html_url);
await core.summary.write();
releases-matrix:
needs: release
name: Release Go Binary
runs-on: ubuntu-latest
env:
EXT_VERSION: ${{ needs.release.outputs.version }}
AmplitudeApiKey: ${{ secrets.AMPLITUDE_API_KEY }}
strategy:
fail-fast: false
matrix:
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
goos: [linux, windows, darwin]
goarch: ["386", amd64, arm64]
exclude:
- goarch: "386"
goos: darwin
steps:
- uses: actions/checkout@v3
- name: Add Inbuilt Variables
run: |
sed -i "s/var AmplitudeApiKey = \"\"/var AmplitudeApiKey = \"${{ env.AmplitudeApiKey }}\"/g" ./src/constants/amplitude.go
- uses: wangyoucao577/go-release-action@v1
timeout-minutes: 10
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.21.1.linux-amd64.tar.gz"
project_path: "./src"
binary_name: "prldevops"
release_name: "v${{ env.EXT_VERSION }}"

build-containers:
needs: release
env:
EXT_VERSION: ${{ needs.release.outputs.version }}
AmplitudeApiKey: ${{ secrets.AMPLITUDE_API_KEY }}
name: Build Docker Images
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Add Inbuilt Variables
run: |
sed -i "s/var AmplitudeApiKey = \"\"/var AmplitudeApiKey = \"${{ env.AmplitudeApiKey }}\"/g" ./src/constants/amplitude.go
- uses: docker/setup-buildx-action@v1
- uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/prl-devops-service:latest
${{ secrets.DOCKER_USERNAME }}/prl-devops-service:${{ env.EXT_VERSION }}
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]
}
Loading

0 comments on commit 7688f2d

Please sign in to comment.