Skip to content

Commit

Permalink
chore: refactor relay install files
Browse files Browse the repository at this point in the history
  • Loading branch information
jchiarulli committed Nov 28, 2024
1 parent 179e081 commit a8b0849
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 432 deletions.
164 changes: 11 additions & 153 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"

"github.com/nodetec/rwz/pkg/manager"
"github.com/nodetec/rwz/pkg/network"
"github.com/nodetec/rwz/pkg/relays"
Expand Down Expand Up @@ -87,19 +86,19 @@ var installCmd = &cobra.Command{
pterm.Println(pterm.Yellow("If you make a mistake, you can always re-run this installer."))
pterm.Println()

// Step 1: Install necessary packages using APT
// Install necessary packages using APT
manager.AptInstallPackages(selectedRelayOption)

// Step 2: Configure the firewall
// Configure the firewall
network.ConfigureFirewall()

// Step 3: Configure the intrusion detection system
// Configure the intrusion detection system
network.ConfigureIntrusionDetection()

// Step 4: Configure Nginx
// Configure Nginx
network.ConfigureNginx()

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

if selectedRelayOption == khatru_pyramid.RelayName {
// Step 6: Configure Nginx for HTTP
khatru_pyramid.ConfigureNginxHttp(relayDomain)

// Step 7: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 8: Configure Nginx for HTTPS
khatru_pyramid.ConfigureNginxHttps(relayDomain)
}

// Step 9: Download and install the relay binary
khatru_pyramid.InstallRelayBinary(pubKey)

// Step 10: Set up the relay data directory
khatru_pyramid.SetUpRelayDataDir()

// Step 11: Configure the relay
khatru_pyramid.ConfigureRelay(relayDomain, pubKey, relayContact)

// Step 12: Set up the relay service
khatru_pyramid.SetUpRelayService()

// Step 13: Show success messages
khatru_pyramid.SuccessMessages(relayDomain, httpsEnabled)
khatru_pyramid.Install(relayDomain, pubKey, relayContact)
} else if selectedRelayOption == nostr_rs_relay.RelayName {
// Step 6: Configure Nginx for HTTP
nostr_rs_relay.ConfigureNginxHttp(relayDomain)

// Step 7: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 8: Configure Nginx for HTTPS
nostr_rs_relay.ConfigureNginxHttps(relayDomain)
}

// Step 9: Download and install the relay binary
nostr_rs_relay.InstallRelayBinary()

// Step 10: Set up the relay data directory
nostr_rs_relay.SetUpRelayDataDir()

// Step 11: Configure the relay
nostr_rs_relay.ConfigureRelay(relayDomain, pubKey, relayContact, httpsEnabled)

// Step 12: Set up the relay service
nostr_rs_relay.SetUpRelayService()

// Step 13: Show success messages
nostr_rs_relay.SuccessMessages(relayDomain, httpsEnabled)
nostr_rs_relay.Install(relayDomain, pubKey, relayContact)
} else if selectedRelayOption == strfry.RelayName {
// Step 6: Configure Nginx for HTTP
strfry.ConfigureNginxHttp(relayDomain)

// Step 7: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 8: Configure Nginx for HTTPS
strfry.ConfigureNginxHttps(relayDomain)
}

// Step 9: Download and install the relay binary
strfry.InstallRelayBinary()

// Step 10: Set up the relay data directory
strfry.SetUpRelayDataDir()

// Step 11: Configure the relay
strfry.ConfigureRelay(pubKey, relayContact)

// Step 12: Set up the relay service
strfry.SetUpRelayService()

// Step 13: Show success messages
strfry.SuccessMessages(relayDomain, httpsEnabled)
strfry.Install(relayDomain, pubKey, relayContact)
} else if selectedRelayOption == wot_relay.RelayName {
// Step 6: Configure Nginx for HTTP
wot_relay.ConfigureNginxHttp(relayDomain)

// Step 7: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 8: Configure Nginx for HTTPS
wot_relay.ConfigureNginxHttps(relayDomain)
}

// Step 9: Download and install the relay binary
wot_relay.InstallRelayBinary(pubKey)

// Step 10: Set up the relay data directory
wot_relay.SetUpRelayDataDir()

// Step 11: Configure the relay
wot_relay.ConfigureRelay(relayDomain, pubKey, relayContact, httpsEnabled)

// Step 12: Set up the relay site
wot_relay.SetUpRelaySite(relayDomain)

// Step 13: Set up the relay service
wot_relay.SetUpRelayService()

// Step 14: Show success messages
wot_relay.SuccessMessages(relayDomain, httpsEnabled)
wot_relay.Install(relayDomain, pubKey, relayContact)
} else if selectedRelayOption == khatru29.RelayName {
// Step 6: Configure Nginx for HTTP
khatru29.ConfigureNginxHttp(relayDomain)

// Step 7: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 8: Configure Nginx for HTTPS
khatru29.ConfigureNginxHttps(relayDomain)
}

// Step 9: Download and install the relay binary
khatru29.InstallRelayBinary()

// Step 10: Set up the relay data directory
khatru29.SetUpRelayDataDir()

// Step 11: Configure the relay
khatru29.ConfigureRelay(relayDomain, privKey, relayContact)

// Step 12: Set up the relay service
khatru29.SetUpRelayService()

// Step 13: Show success messages
khatru29.SuccessMessages(relayDomain, httpsEnabled)
khatru29.Install(relayDomain, privKey, relayContact)
} else if selectedRelayOption == strfry29.RelayName {
// Step 6: Configure Nginx for HTTP
strfry29.ConfigureNginxHttp(relayDomain)

// Step 7: Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Step 8: Configure Nginx for HTTPS
strfry29.ConfigureNginxHttps(relayDomain)
}

// Step 9: Download and install the relay binaries
strfry29.InstallRelayBinaries()

// Step 10: Set up the relay data directory
strfry29.SetUpRelayDataDir()

// Step 11: Configure the relay
strfry29.ConfigureRelay(relayDomain, pubKey, privKey, relayContact)

// Step 12: Set up the relay service
strfry29.SetUpRelayService()

// Step 13: Show success messages
strfry29.SuccessMessages(relayDomain, httpsEnabled)
strfry29.Install(relayDomain, pubKey, privKey, relayContact)
}

pterm.Println()
Expand Down
2 changes: 2 additions & 0 deletions pkg/relays/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package relays

const BinaryDestDir = "/usr/local/bin"
const BinaryFilePerms = 0755
const TmpDirPath = "/tmp"
const GitRepoDirPerms = 0755
const DBDir = "db"
const User = "nostr"
const NginxUser = "www-data"
62 changes: 30 additions & 32 deletions pkg/relays/khatru29/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,57 @@ package khatru29

import (
"fmt"
"github.com/nodetec/rwz/pkg/network"
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/nodetec/rwz/pkg/verification"
"github.com/pterm/pterm"
"path/filepath"
)

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

// Determine the file name from the URL
tmpFileName := filepath.Base(DownloadURL)
// Get SSL/TLS certificates
httpsEnabled := network.GetCertificates(relayDomain)
if httpsEnabled {
// Configure Nginx for HTTPS
ConfigureNginxHttps(relayDomain)
}

// Temporary file path
tmpFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpFileName)
// Determine the temporary file path
tmpCompressedBinaryFilePath := files.FilePathFromFilePathBase(DownloadURL, relays.TmpDirPath)

// Check if the temporary file exists and remove it if it does
files.RemoveFile(tmpFilePath)
files.RemoveFile(tmpCompressedBinaryFilePath)

// Download and copy the file
files.DownloadAndCopyFile(tmpFilePath, DownloadURL)

downloadSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s binary...", RelayName))
files.DownloadAndCopyFile(tmpCompressedBinaryFilePath, DownloadURL, 0644)
downloadSpinner.Success(fmt.Sprintf("%s binary downloaded", RelayName))

// Verify relay binary
verification.VerifyRelayBinary(RelayName, tmpFilePath)

installSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s binary...", RelayName))
verification.VerifyRelayBinary(RelayName, tmpCompressedBinaryFilePath)

// Check if the service file exists and disable and stop the service if it does
if files.FileExists(ServiceFilePath) {
// Disable and stop the Nostr relay service
installSpinner.UpdateText("Disabling and stopping service...")
systemd.DisableService(ServiceName)
systemd.StopService(ServiceName)
} else {
installSpinner.UpdateText("Service file not found...")
}
systemd.DisableAndStopService(ServiceFilePath, ServiceName)

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

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

// Define the final destination path
destPath := filepath.Join(relays.BinaryDestDir, BinaryName)
// Configure the relay
ConfigureRelay(relayDomain, privKey, relayContact)

// Make the file executable
files.SetPermissions(destPath, 0755)
// Set up the relay service
SetUpRelayService()

installSpinner.Success(fmt.Sprintf("%s binary installed", RelayName))
// Show success messages
SuccessMessages(relayDomain, httpsEnabled)
}
Loading

0 comments on commit a8b0849

Please sign in to comment.