Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions .github/workflows/go-lint-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,11 @@ permissions:
checks: write

jobs:
gofmt-unix:
strategy:
matrix:
go: ["1.21"]
os: [macos-latest, ubuntu-latest]
name: gofmt
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
cache: true
- name: Run format checks
run: |
if [ $(gofmt -d -l -e . | wc -l) -eq 0 ]; then
printf 'Format Checks Have Passed!!!'
else
printf 'Format Checks Have Failed!\nPlease use gofmt in your system to format your code.'
gofmt -l -e -d .
exit 1
fi
golangci:
strategy:
matrix:
go: ["1.21"]
os: [macos-latest, ubuntu-latest]
os: [ubuntu-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters:
enable:
- gofmt
51 changes: 51 additions & 0 deletions cmd/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/swiftwave-org/swiftwave/system_config"
"os"
)

var applyPatchesCmd = &cobra.Command{
Use: "apply-patches",
Short: "Apply patches to swiftwave",
Long: `Apply patches to swiftwave`,
Run: func(cmd *cobra.Command, args []string) {
err := ApplyPatches()
if err != nil {
printError("Failed to apply patches")
printError(err.Error())
os.Exit(1)
} else {
printSuccess("Patches applied successfully")
}
},
}

func ApplyPatches() error {
// deep copy system config
systemConfigCopy := systemConfig.DeepCopy()
if systemConfigCopy == nil {
return fmt.Errorf("failed to deep copy system config file")
}
return runPatch(systemConfigCopy, []func(*system_config.Config) error{
saveUpdatedSystemConfig,
})
}

// Patches list
func saveUpdatedSystemConfig(config *system_config.Config) error {
return config.WriteToFile(configFilePath)
}

// private function
func runPatch(config *system_config.Config, listedPatches []func(*system_config.Config) error) error {
for _, patch := range listedPatches {
err := patch(config)
if err != nil {
return err
}
}
return nil
}
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ var swiftwaveVersion string

func init() {
rootCmd.PersistentFlags().Bool("dev", false, "Run in development mode")
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(setupCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(createUserCmd)
rootCmd.AddCommand(deleteUserCmd)
rootCmd.AddCommand(tlsCmd)
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(serviceCmd)
rootCmd.AddCommand(haproxyCmd)
rootCmd.AddCommand(udpProxyCmd)
rootCmd.AddCommand(postgresCmd)
rootCmd.AddCommand(dbMigrateCmd)
rootCmd.AddCommand(serviceCmd)
rootCmd.AddCommand(applyPatchesCmd)
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(autoUpdaterCmd)
rootCmd.AddCommand(infoCmd)
rootCmd.AddCommand(versionCmd)
}

var rootCmd = &cobra.Command{
Expand Down
62 changes: 56 additions & 6 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,50 @@ var updateCmd = &cobra.Command{
printError("Failed to make new binary executable")
return
}
// copy old binary to /usr/bin/swiftwave.old, don't move it
var oldBinaryFile *os.File
oldBinaryFile, err = os.Open("/usr/bin/swiftwave")
if err != nil {
printError("Failed to open old binary")
return
}
var currentBinaryFile *os.File
currentBinaryFile, err = os.Create("/usr/bin/swiftwave.old")
if err != nil {
printError("Failed to create old binary")
return
}
_, err = io.Copy(currentBinaryFile, oldBinaryFile)
if err != nil {
printError("Failed to copy old binary")
return
}
err = oldBinaryFile.Close()
if err != nil {
printError("Failed to close old binary")
return
}
err = currentBinaryFile.Close()
if err != nil {
printError("Failed to close current binary")
return
}
// replace it at /usr/bin/swiftwave
err = os.Rename(newBinaryPath, "/usr/bin/swiftwave")
if err != nil {
revertToOldBinary()
printError("Failed to replace binary")
return
}
// apply patches
err = ApplyPatches()
if err != nil {
revertToOldBinary()
printError("Failed to apply patches")
return
} else {
printSuccess("Patches applied successfully")
}
// daemon-reload
runCommand := exec.Command("systemctl", "daemon-reload")
err = runCommand.Run()
Expand All @@ -81,6 +119,7 @@ var updateCmd = &cobra.Command{
runCommand = exec.Command("systemctl", "restart", "swiftwave.service")
err = runCommand.Run()
if err != nil {
revertToOldBinary()
printError("Failed to restart swiftwave service")
return
}
Expand Down Expand Up @@ -229,17 +268,28 @@ func extractTarGz(downloadedPackagePath, destFolder string) error {
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
return
}
}(file)
// copy file data
_, err = io.Copy(file, tarReader)
if err != nil {
_ = file.Close()
return err
}
_ = file.Close()
}
return nil
}

func revertToOldBinary() {
// check if old binary exists
if _, err := os.Stat("/usr/bin/swiftwave.old"); os.IsNotExist(err) {
printError("Revert Failed ! Failed to find old binary /usr/bin/swiftwave.old")
return
}
// replace it at /usr/bin/swiftwave
err := os.Rename("/usr/bin/swiftwave.old", "/usr/bin/swiftwave")
if err != nil {
printError("Revert Failed ! Failed to replace binary")
return
}
printSuccess("Reverted to old binary")
}
4 changes: 4 additions & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "1.0"
profile:
name: qodana.starter
linter: jetbrains/qodana-go:latest
12 changes: 12 additions & 0 deletions system_config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ func (config *Config) WriteToFile(path string) error {
}
return nil
}

func (config *Config) DeepCopy() *Config {
// marshal to yaml
out, _ := config.String()
// unmarshal to new object
newConfig := &Config{}
err := yaml.Unmarshal([]byte(out), newConfig)
if err != nil {
return nil
}
return newConfig
}