Skip to content

Commit 341d2e3

Browse files
authored
feat: reset services on install and reset khatru pyramid database if new pubkey is used (#64)
1 parent 4bde15e commit 341d2e3

File tree

10 files changed

+123
-2
lines changed

10 files changed

+123
-2
lines changed

cmd/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ var installCmd = &cobra.Command{
113113
}
114114

115115
// Step 7: Download and install the relay binary
116-
khatru_pyramid.InstallRelayBinary()
116+
khatru_pyramid.InstallRelayBinary(pubKey)
117117

118118
// Step 8: Set up the relay service
119119
khatru_pyramid.SetupRelayService(relayDomain, pubKey, relayContact)

pkg/relays/khatru29/install.go

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/nodetec/rwz/pkg/relays"
66
"github.com/nodetec/rwz/pkg/utils/files"
7+
"github.com/nodetec/rwz/pkg/utils/systemd"
78
"github.com/pterm/pterm"
89
"path/filepath"
910
)
@@ -24,6 +25,16 @@ func InstallRelayBinary() {
2425
// Download and copy the file
2526
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)
2627

28+
// Check if the service file exists and disable and stop the service if it does
29+
if files.FileExists(ServiceFilePath) {
30+
// Disable and stop the Nostr relay service
31+
spinner.UpdateText("Disabling and stopping service...")
32+
systemd.DisableService(ServiceName)
33+
systemd.StopService(ServiceName)
34+
} else {
35+
spinner.UpdateText("Service file not found...")
36+
}
37+
2738
// Extract binary
2839
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
2940

pkg/relays/khatru_pyramid/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const BinaryName = "khatru-pyramid"
55
const BinaryFilePath = "/usr/local/bin/khatru-pyramid"
66
const NginxConfigFilePath = "/etc/nginx/conf.d/khatru_pyramid.conf"
77
const DataDirPath = "/var/lib/khatru-pyramid"
8+
const UsersFilePath = "/var/lib/khatru-pyramid/users.json"
89
const ServiceName = "khatru-pyramid"
910
const EnvFilePath = "/etc/systemd/system/khatru-pyramid.env"
1011
const EnvFileTemplate = `DOMAIN="{{.Domain}}"

pkg/relays/khatru_pyramid/install.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package khatru_pyramid
33
import (
44
"fmt"
55
"github.com/nodetec/rwz/pkg/relays"
6+
"github.com/nodetec/rwz/pkg/utils/directories"
67
"github.com/nodetec/rwz/pkg/utils/files"
8+
"github.com/nodetec/rwz/pkg/utils/systemd"
79
"github.com/pterm/pterm"
810
"path/filepath"
911
)
1012

1113
// Function to download and make the binary executable
12-
func InstallRelayBinary() {
14+
func InstallRelayBinary(pubKey string) {
1315
spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s relay...", RelayName))
1416

1517
// Determine the file name from the URL
@@ -24,6 +26,31 @@ func InstallRelayBinary() {
2426
// Download and copy the file
2527
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)
2628

29+
// Check if the service file exists and disable and stop the service if it does
30+
if files.FileExists(ServiceFilePath) {
31+
// Disable and stop the Nostr relay service
32+
spinner.UpdateText("Disabling and stopping service...")
33+
systemd.DisableService(ServiceName)
34+
systemd.StopService(ServiceName)
35+
} else {
36+
spinner.UpdateText("Service file not found...")
37+
}
38+
39+
// Check if users.json file exists
40+
if files.FileExists(UsersFilePath) {
41+
// Check if the pubKey exists in the users.json file
42+
spinner.UpdateText("Checking for public key in users.json file...")
43+
lineExists := files.LineExists(fmt.Sprintf(`"%s":""`, pubKey), UsersFilePath)
44+
45+
// If false remove data directory
46+
if !lineExists {
47+
spinner.UpdateText("Public key not found, removing data directory...")
48+
directories.RemoveDirectory(DataDirPath)
49+
} else {
50+
spinner.UpdateText("Public key found, keeping data directory.")
51+
}
52+
}
53+
2754
// Extract binary
2855
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
2956

pkg/relays/nostr_rs_relay/install.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/nodetec/rwz/pkg/utils/directories"
77
"github.com/nodetec/rwz/pkg/utils/files"
88
"github.com/nodetec/rwz/pkg/utils/git"
9+
"github.com/nodetec/rwz/pkg/utils/systemd"
910
"github.com/pterm/pterm"
1011
"path/filepath"
1112
)
@@ -32,6 +33,16 @@ func InstallRelayBinary() {
3233
// Download and copy the file
3334
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)
3435

36+
// Check if the service file exists and disable and stop the service if it does
37+
if files.FileExists(ServiceFilePath) {
38+
// Disable and stop the Nostr relay service
39+
spinner.UpdateText("Disabling and stopping service...")
40+
systemd.DisableService(ServiceName)
41+
systemd.StopService(ServiceName)
42+
} else {
43+
spinner.UpdateText("Service file not found...")
44+
}
45+
3546
// Extract binary
3647
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
3748

pkg/relays/strfry/install.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/nodetec/rwz/pkg/utils/directories"
77
"github.com/nodetec/rwz/pkg/utils/files"
88
"github.com/nodetec/rwz/pkg/utils/git"
9+
"github.com/nodetec/rwz/pkg/utils/systemd"
910
"github.com/pterm/pterm"
1011
"path/filepath"
1112
)
@@ -33,6 +34,16 @@ func InstallRelayBinary() {
3334
// Download and copy the file
3435
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)
3536

37+
// Check if the service file exists and disable and stop the service if it does
38+
if files.FileExists(ServiceFilePath) {
39+
// Disable and stop the Nostr relay service
40+
spinner.UpdateText("Disabling and stopping service...")
41+
systemd.DisableService(ServiceName)
42+
systemd.StopService(ServiceName)
43+
} else {
44+
spinner.UpdateText("Service file not found...")
45+
}
46+
3647
// Extract binary
3748
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
3849

pkg/relays/strfry29/install.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/nodetec/rwz/pkg/utils/directories"
77
"github.com/nodetec/rwz/pkg/utils/files"
88
"github.com/nodetec/rwz/pkg/utils/git"
9+
"github.com/nodetec/rwz/pkg/utils/systemd"
910
"github.com/pterm/pterm"
1011
"path/filepath"
1112
)
@@ -48,6 +49,16 @@ func InstallRelayBinary() {
4849
// Download and copy the file
4950
files.DownloadAndCopyFile(tmpFilePath, BinaryPluginDownloadURL)
5051

52+
// Check if the service file exists and disable and stop the service if it does
53+
if files.FileExists(ServiceFilePath) {
54+
// Disable and stop the Nostr relay service
55+
spinner.UpdateText("Disabling and stopping service...")
56+
systemd.DisableService(ServiceName)
57+
systemd.StopService(ServiceName)
58+
} else {
59+
spinner.UpdateText("Service file not found...")
60+
}
61+
5162
// Extract binary
5263
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
5364

pkg/relays/wot_relay/install.go

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/nodetec/rwz/pkg/relays"
66
"github.com/nodetec/rwz/pkg/utils/files"
7+
"github.com/nodetec/rwz/pkg/utils/systemd"
78
"github.com/pterm/pterm"
89
"path/filepath"
910
)
@@ -24,6 +25,16 @@ func InstallRelayBinary() {
2425
// Download and copy the file
2526
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)
2627

28+
// Check if the service file exists and disable and stop the service if it does
29+
if files.FileExists(ServiceFilePath) {
30+
// Disable and stop the Nostr relay service
31+
spinner.UpdateText("Disabling and stopping service...")
32+
systemd.DisableService(ServiceName)
33+
systemd.StopService(ServiceName)
34+
} else {
35+
spinner.UpdateText("Service file not found...")
36+
}
37+
2738
// Extract binary
2839
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
2940

pkg/utils/files/utils.go

+20
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ func InPlaceEdit(command, path string) {
8282
}
8383
}
8484

85+
// Function to check if a line exists
86+
func LineExists(pattern, path string) bool {
87+
cmd := exec.Command("grep", "-q", pattern, path)
88+
err := cmd.Run()
89+
90+
if err != nil {
91+
if exitError, ok := err.(*exec.ExitError); ok {
92+
exitCode := exitError.ExitCode()
93+
if exitCode == 1 {
94+
return false
95+
} else {
96+
pterm.Println()
97+
pterm.Error.Println(fmt.Sprintf("Failed to search %s for %s: %v", path, pattern, err))
98+
os.Exit(1)
99+
}
100+
}
101+
}
102+
return true
103+
}
104+
85105
// Function to download and copy a file
86106
func DownloadAndCopyFile(tmpFilePath, downloadURL string) {
87107
// Create a temporary file

pkg/utils/systemd/utils.go

+18
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ func StartService(name string) {
100100
}
101101
}
102102

103+
func DisableService(name string) {
104+
err := exec.Command("systemctl", "disable", name).Run()
105+
if err != nil {
106+
pterm.Println()
107+
pterm.Error.Println(fmt.Sprintf("Failed to disable %s service: %v", name, err))
108+
os.Exit(1)
109+
}
110+
}
111+
112+
func StopService(name string) {
113+
err := exec.Command("systemctl", "stop", name).Run()
114+
if err != nil {
115+
pterm.Println()
116+
pterm.Error.Println(fmt.Sprintf("Failed to stop %s service: %v", name, err))
117+
os.Exit(1)
118+
}
119+
}
120+
103121
func ReloadService(name string) {
104122
err := exec.Command("systemctl", "reload", name).Run()
105123
if err != nil {

0 commit comments

Comments
 (0)