Skip to content

Commit a8b0849

Browse files
committed
chore: refactor relay install files
1 parent 179e081 commit a8b0849

File tree

15 files changed

+321
-432
lines changed

15 files changed

+321
-432
lines changed

cmd/install.go

+11-153
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
65
"github.com/nodetec/rwz/pkg/manager"
76
"github.com/nodetec/rwz/pkg/network"
87
"github.com/nodetec/rwz/pkg/relays"
@@ -87,19 +86,19 @@ var installCmd = &cobra.Command{
8786
pterm.Println(pterm.Yellow("If you make a mistake, you can always re-run this installer."))
8887
pterm.Println()
8988

90-
// Step 1: Install necessary packages using APT
89+
// Install necessary packages using APT
9190
manager.AptInstallPackages(selectedRelayOption)
9291

93-
// Step 2: Configure the firewall
92+
// Configure the firewall
9493
network.ConfigureFirewall()
9594

96-
// Step 3: Configure the intrusion detection system
95+
// Configure the intrusion detection system
9796
network.ConfigureIntrusionDetection()
9897

99-
// Step 4: Configure Nginx
98+
// Configure Nginx
10099
network.ConfigureNginx()
101100

102-
// Step 5: Create relay user
101+
// Create relay user
103102
spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Checking if '%s' user exists...", relays.User))
104103
if !users.UserExists(relays.User) {
105104
spinner.UpdateText(fmt.Sprintf("Creating '%s' user...", relays.User))
@@ -110,158 +109,17 @@ var installCmd = &cobra.Command{
110109
}
111110

112111
if selectedRelayOption == khatru_pyramid.RelayName {
113-
// Step 6: Configure Nginx for HTTP
114-
khatru_pyramid.ConfigureNginxHttp(relayDomain)
115-
116-
// Step 7: Get SSL/TLS certificates
117-
httpsEnabled := network.GetCertificates(relayDomain)
118-
if httpsEnabled {
119-
// Step 8: Configure Nginx for HTTPS
120-
khatru_pyramid.ConfigureNginxHttps(relayDomain)
121-
}
122-
123-
// Step 9: Download and install the relay binary
124-
khatru_pyramid.InstallRelayBinary(pubKey)
125-
126-
// Step 10: Set up the relay data directory
127-
khatru_pyramid.SetUpRelayDataDir()
128-
129-
// Step 11: Configure the relay
130-
khatru_pyramid.ConfigureRelay(relayDomain, pubKey, relayContact)
131-
132-
// Step 12: Set up the relay service
133-
khatru_pyramid.SetUpRelayService()
134-
135-
// Step 13: Show success messages
136-
khatru_pyramid.SuccessMessages(relayDomain, httpsEnabled)
112+
khatru_pyramid.Install(relayDomain, pubKey, relayContact)
137113
} else if selectedRelayOption == nostr_rs_relay.RelayName {
138-
// Step 6: Configure Nginx for HTTP
139-
nostr_rs_relay.ConfigureNginxHttp(relayDomain)
140-
141-
// Step 7: Get SSL/TLS certificates
142-
httpsEnabled := network.GetCertificates(relayDomain)
143-
if httpsEnabled {
144-
// Step 8: Configure Nginx for HTTPS
145-
nostr_rs_relay.ConfigureNginxHttps(relayDomain)
146-
}
147-
148-
// Step 9: Download and install the relay binary
149-
nostr_rs_relay.InstallRelayBinary()
150-
151-
// Step 10: Set up the relay data directory
152-
nostr_rs_relay.SetUpRelayDataDir()
153-
154-
// Step 11: Configure the relay
155-
nostr_rs_relay.ConfigureRelay(relayDomain, pubKey, relayContact, httpsEnabled)
156-
157-
// Step 12: Set up the relay service
158-
nostr_rs_relay.SetUpRelayService()
159-
160-
// Step 13: Show success messages
161-
nostr_rs_relay.SuccessMessages(relayDomain, httpsEnabled)
114+
nostr_rs_relay.Install(relayDomain, pubKey, relayContact)
162115
} else if selectedRelayOption == strfry.RelayName {
163-
// Step 6: Configure Nginx for HTTP
164-
strfry.ConfigureNginxHttp(relayDomain)
165-
166-
// Step 7: Get SSL/TLS certificates
167-
httpsEnabled := network.GetCertificates(relayDomain)
168-
if httpsEnabled {
169-
// Step 8: Configure Nginx for HTTPS
170-
strfry.ConfigureNginxHttps(relayDomain)
171-
}
172-
173-
// Step 9: Download and install the relay binary
174-
strfry.InstallRelayBinary()
175-
176-
// Step 10: Set up the relay data directory
177-
strfry.SetUpRelayDataDir()
178-
179-
// Step 11: Configure the relay
180-
strfry.ConfigureRelay(pubKey, relayContact)
181-
182-
// Step 12: Set up the relay service
183-
strfry.SetUpRelayService()
184-
185-
// Step 13: Show success messages
186-
strfry.SuccessMessages(relayDomain, httpsEnabled)
116+
strfry.Install(relayDomain, pubKey, relayContact)
187117
} else if selectedRelayOption == wot_relay.RelayName {
188-
// Step 6: Configure Nginx for HTTP
189-
wot_relay.ConfigureNginxHttp(relayDomain)
190-
191-
// Step 7: Get SSL/TLS certificates
192-
httpsEnabled := network.GetCertificates(relayDomain)
193-
if httpsEnabled {
194-
// Step 8: Configure Nginx for HTTPS
195-
wot_relay.ConfigureNginxHttps(relayDomain)
196-
}
197-
198-
// Step 9: Download and install the relay binary
199-
wot_relay.InstallRelayBinary(pubKey)
200-
201-
// Step 10: Set up the relay data directory
202-
wot_relay.SetUpRelayDataDir()
203-
204-
// Step 11: Configure the relay
205-
wot_relay.ConfigureRelay(relayDomain, pubKey, relayContact, httpsEnabled)
206-
207-
// Step 12: Set up the relay site
208-
wot_relay.SetUpRelaySite(relayDomain)
209-
210-
// Step 13: Set up the relay service
211-
wot_relay.SetUpRelayService()
212-
213-
// Step 14: Show success messages
214-
wot_relay.SuccessMessages(relayDomain, httpsEnabled)
118+
wot_relay.Install(relayDomain, pubKey, relayContact)
215119
} else if selectedRelayOption == khatru29.RelayName {
216-
// Step 6: Configure Nginx for HTTP
217-
khatru29.ConfigureNginxHttp(relayDomain)
218-
219-
// Step 7: Get SSL/TLS certificates
220-
httpsEnabled := network.GetCertificates(relayDomain)
221-
if httpsEnabled {
222-
// Step 8: Configure Nginx for HTTPS
223-
khatru29.ConfigureNginxHttps(relayDomain)
224-
}
225-
226-
// Step 9: Download and install the relay binary
227-
khatru29.InstallRelayBinary()
228-
229-
// Step 10: Set up the relay data directory
230-
khatru29.SetUpRelayDataDir()
231-
232-
// Step 11: Configure the relay
233-
khatru29.ConfigureRelay(relayDomain, privKey, relayContact)
234-
235-
// Step 12: Set up the relay service
236-
khatru29.SetUpRelayService()
237-
238-
// Step 13: Show success messages
239-
khatru29.SuccessMessages(relayDomain, httpsEnabled)
120+
khatru29.Install(relayDomain, privKey, relayContact)
240121
} else if selectedRelayOption == strfry29.RelayName {
241-
// Step 6: Configure Nginx for HTTP
242-
strfry29.ConfigureNginxHttp(relayDomain)
243-
244-
// Step 7: Get SSL/TLS certificates
245-
httpsEnabled := network.GetCertificates(relayDomain)
246-
if httpsEnabled {
247-
// Step 8: Configure Nginx for HTTPS
248-
strfry29.ConfigureNginxHttps(relayDomain)
249-
}
250-
251-
// Step 9: Download and install the relay binaries
252-
strfry29.InstallRelayBinaries()
253-
254-
// Step 10: Set up the relay data directory
255-
strfry29.SetUpRelayDataDir()
256-
257-
// Step 11: Configure the relay
258-
strfry29.ConfigureRelay(relayDomain, pubKey, privKey, relayContact)
259-
260-
// Step 12: Set up the relay service
261-
strfry29.SetUpRelayService()
262-
263-
// Step 13: Show success messages
264-
strfry29.SuccessMessages(relayDomain, httpsEnabled)
122+
strfry29.Install(relayDomain, pubKey, privKey, relayContact)
265123
}
266124

267125
pterm.Println()

pkg/relays/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package relays
22

33
const BinaryDestDir = "/usr/local/bin"
4+
const BinaryFilePerms = 0755
45
const TmpDirPath = "/tmp"
6+
const GitRepoDirPerms = 0755
57
const DBDir = "db"
68
const User = "nostr"
79
const NginxUser = "www-data"

pkg/relays/khatru29/install.go

+30-32
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,57 @@ package khatru29
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/network"
56
"github.com/nodetec/rwz/pkg/relays"
67
"github.com/nodetec/rwz/pkg/utils/files"
78
"github.com/nodetec/rwz/pkg/utils/systemd"
89
"github.com/nodetec/rwz/pkg/verification"
910
"github.com/pterm/pterm"
10-
"path/filepath"
1111
)
1212

13-
// Function to download and make the binary executable
14-
func InstallRelayBinary() {
15-
downloadSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s binary...", RelayName))
13+
// Install the relay
14+
func Install(relayDomain, privKey, relayContact string) {
15+
// Configure Nginx for HTTP
16+
ConfigureNginxHttp(relayDomain)
1617

17-
// Determine the file name from the URL
18-
tmpFileName := filepath.Base(DownloadURL)
18+
// Get SSL/TLS certificates
19+
httpsEnabled := network.GetCertificates(relayDomain)
20+
if httpsEnabled {
21+
// Configure Nginx for HTTPS
22+
ConfigureNginxHttps(relayDomain)
23+
}
1924

20-
// Temporary file path
21-
tmpFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpFileName)
25+
// Determine the temporary file path
26+
tmpCompressedBinaryFilePath := files.FilePathFromFilePathBase(DownloadURL, relays.TmpDirPath)
2227

2328
// Check if the temporary file exists and remove it if it does
24-
files.RemoveFile(tmpFilePath)
29+
files.RemoveFile(tmpCompressedBinaryFilePath)
2530

2631
// Download and copy the file
27-
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)
28-
32+
downloadSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s binary...", RelayName))
33+
files.DownloadAndCopyFile(tmpCompressedBinaryFilePath, DownloadURL, 0644)
2934
downloadSpinner.Success(fmt.Sprintf("%s binary downloaded", RelayName))
3035

3136
// Verify relay binary
32-
verification.VerifyRelayBinary(RelayName, tmpFilePath)
33-
34-
installSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s binary...", RelayName))
37+
verification.VerifyRelayBinary(RelayName, tmpCompressedBinaryFilePath)
3538

3639
// 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-
installSpinner.UpdateText("Disabling and stopping service...")
40-
systemd.DisableService(ServiceName)
41-
systemd.StopService(ServiceName)
42-
} else {
43-
installSpinner.UpdateText("Service file not found...")
44-
}
40+
systemd.DisableAndStopService(ServiceFilePath, ServiceName)
4541

46-
// Extract binary
47-
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)
42+
// Install the compressed relay binary and make it executable
43+
installSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s binary...", RelayName))
44+
files.InstallCompressedBinary(tmpCompressedBinaryFilePath, relays.BinaryDestDir, BinaryName, relays.BinaryFilePerms)
45+
installSpinner.Success(fmt.Sprintf("%s binary installed", RelayName))
4846

49-
// TODO
50-
// Currently, the downloaded binary is expected to have a name that matches the BinaryName variable
51-
// Ideally, the extracted binary file should be renamed to match the BinaryName variable
47+
// Set up the relay data directory
48+
SetUpRelayDataDir()
5249

53-
// Define the final destination path
54-
destPath := filepath.Join(relays.BinaryDestDir, BinaryName)
50+
// Configure the relay
51+
ConfigureRelay(relayDomain, privKey, relayContact)
5552

56-
// Make the file executable
57-
files.SetPermissions(destPath, 0755)
53+
// Set up the relay service
54+
SetUpRelayService()
5855

59-
installSpinner.Success(fmt.Sprintf("%s binary installed", RelayName))
56+
// Show success messages
57+
SuccessMessages(relayDomain, httpsEnabled)
6058
}

0 commit comments

Comments
 (0)