From d2d4750a42a185b541d971d64fd1169afd2565b6 Mon Sep 17 00:00:00 2001 From: J the Code Monkey Date: Sat, 9 Nov 2024 12:41:06 -0500 Subject: [PATCH] feat: use wot-relay index file and static directory, update env file, and remove data directory if using new pubkey --- cmd/install.go | 2 +- pkg/relays/wot_relay/constants.go | 52 ++++++++++--------------------- pkg/relays/wot_relay/install.go | 25 ++++++++++++++- pkg/relays/wot_relay/service.go | 51 +++++++++++++++++------------- pkg/utils/directories/utils.go | 10 ++++++ pkg/utils/templates/utils.go | 41 ------------------------ 6 files changed, 81 insertions(+), 100 deletions(-) delete mode 100644 pkg/utils/templates/utils.go diff --git a/cmd/install.go b/cmd/install.go index cecc4cb..08ef914 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -173,7 +173,7 @@ var installCmd = &cobra.Command{ } // Step 8: Download and install the relay binary - wot_relay.InstallRelayBinary() + wot_relay.InstallRelayBinary(pubKey) // Step 9: Set up the relay service wot_relay.SetupRelayService(relayDomain, pubKey, relayContact, httpsEnabled) diff --git a/pkg/relays/wot_relay/constants.go b/pkg/relays/wot_relay/constants.go index 32de4d6..edb4279 100644 --- a/pkg/relays/wot_relay/constants.go +++ b/pkg/relays/wot_relay/constants.go @@ -1,58 +1,39 @@ package wot_relay +const GitRepoBranch = "v0.1.12" +const GitRepoURL = "https://github.com/bitvora/wot-relay.git" +const GitRepoTmpDirPath = "/tmp/wot-relay" const DownloadURL = "https://github.com/nodetec/relays/releases/download/v0.4.0/wot-relay-0.1.12-x86_64-linux-gnu.tar.gz" const BinaryName = "wot-relay" const BinaryFilePath = "/usr/local/bin/wot-relay" const NginxConfigFilePath = "/etc/nginx/conf.d/wot_relay.conf" +const DataDirPath = "/var/lib/wot-relay" +const ConfigDirPath = "/etc/wot-relay" const TemplatesDirPath = "/etc/wot-relay/templates" const IndexFilePath = "/etc/wot-relay/templates/index.html" -const IndexFileTemplate = ` - - - - - WoT Relay - - - - -
-
-
- WoT Relay -
-
- Domain: {{.Domain}} -
-
- Pubkey: {{.PubKey}} -
-
-
- - -` +const TmpIndexFilePath = "/tmp/wot-relay/templates/index.html" const StaticDirPath = "/etc/wot-relay/templates/static" -const DataDirPath = "/var/lib/wot-relay" +const TmpStaticDirPath = "/tmp/wot-relay/templates/static" const ServiceName = "wot-relay" const EnvFilePath = "/etc/systemd/system/wot-relay.env" const EnvFileTemplate = `RELAY_NAME="WoT Relay" -RELAY_DESCRIPTION="WoT Nostr Relay" -RELAY_ICON="https://pfp.nostr.build/56306a93a88d4c657d8a3dfa57b55a4ed65b709eee927b5dafaab4d5330db21f.png" -RELAY_URL="{{.WSScheme}}://{{.Domain}}" RELAY_PUBKEY="{{.PubKey}}" +RELAY_DESCRIPTION="Stores only notes in your WoT" +RELAY_URL="{{.WSScheme}}://{{.Domain}}" +RELAY_ICON="https://pfp.nostr.build/56306a93a88d4c657d8a3dfa57b55a4ed65b709eee927b5dafaab4d5330db21f.png" RELAY_CONTACT="{{.RelayContact}}" +DB_PATH="/var/lib/wot-relay/db" INDEX_PATH="/etc/wot-relay/templates/index.html" STATIC_PATH="/etc/wot-relay/templates/static" -DB_PATH="/var/lib/wot-relay/db" -REFRESH_INTERVAL_HOURS=24 -MINIMUM_FOLLOWERS=3 +REFRESH_INTERVAL_HOURS=3 +MINIMUM_FOLLOWERS=1 ARCHIVAL_SYNC="FALSE" ARCHIVE_REACTIONS="FALSE" +MAX_AGE_DAYS=0 ` const ServiceFilePath = "/etc/systemd/system/wot-relay.service" const ServiceFileTemplate = `[Unit] -Description=WoT Nostr Relay Service +Description=WoT Relay Service After=network.target [Service] @@ -63,8 +44,7 @@ WorkingDirectory=/home/nostr EnvironmentFile={{.EnvFilePath}} ExecStart={{.BinaryFilePath}} Restart=on-failure -MemoryHigh=512M -MemoryMax=1G +MemoryMax=2G [Install] WantedBy=multi-user.target diff --git a/pkg/relays/wot_relay/install.go b/pkg/relays/wot_relay/install.go index b60caf4..ce954d1 100644 --- a/pkg/relays/wot_relay/install.go +++ b/pkg/relays/wot_relay/install.go @@ -3,16 +3,24 @@ package wot_relay import ( "fmt" "github.com/nodetec/rwz/pkg/relays" + "github.com/nodetec/rwz/pkg/utils/directories" "github.com/nodetec/rwz/pkg/utils/files" + "github.com/nodetec/rwz/pkg/utils/git" "github.com/nodetec/rwz/pkg/utils/systemd" "github.com/pterm/pterm" "path/filepath" ) // Function to download and make the binary executable -func InstallRelayBinary() { +func InstallRelayBinary(pubKey string) { spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s...", RelayName)) + // Check for and remove existing git repository + directories.RemoveDirectory(GitRepoTmpDirPath) + + // Download git repository + git.Clone(GitRepoBranch, GitRepoURL, GitRepoTmpDirPath) + // Determine the file name from the URL tmpFileName := filepath.Base(DownloadURL) @@ -35,6 +43,21 @@ func InstallRelayBinary() { spinner.UpdateText("Service file not found...") } + // Check if environment file exists + if files.FileExists(EnvFilePath) { + // Check if the pubKey exists in the environment file + spinner.UpdateText(fmt.Sprintf("Checking for public key in the %s file...", EnvFilePath)) + lineExists := files.LineExists(fmt.Sprintf(`RELAY_PUBKEY="%s"`, pubKey), EnvFilePath) + + // If false remove data directory + if !lineExists { + spinner.UpdateText("Public key not found, removing data directory...") + directories.RemoveDirectory(DataDirPath) + } else { + spinner.UpdateText("Public key found, keeping data directory.") + } + } + // Extract binary files.ExtractFile(tmpFilePath, relays.BinaryDestDir) diff --git a/pkg/relays/wot_relay/service.go b/pkg/relays/wot_relay/service.go index d2d7843..2a46756 100644 --- a/pkg/relays/wot_relay/service.go +++ b/pkg/relays/wot_relay/service.go @@ -1,11 +1,11 @@ package wot_relay import ( + "fmt" "github.com/nodetec/rwz/pkg/relays" "github.com/nodetec/rwz/pkg/utils/directories" "github.com/nodetec/rwz/pkg/utils/files" "github.com/nodetec/rwz/pkg/utils/systemd" - "github.com/nodetec/rwz/pkg/utils/templates" "github.com/pterm/pterm" ) @@ -13,41 +13,50 @@ import ( func SetupRelayService(domain, pubKey, relayContact string, httpsEnabled bool) { spinner, _ := pterm.DefaultSpinner.Start("Configuring relay service...") - // Ensure the templates directory exists and set ownership - spinner.UpdateText("Creating templates directory...") - directories.CreateDirectory(TemplatesDirPath, 0755) - - // Use chown command to set ownership of the templates directory to the nostr user - directories.SetOwnerAndGroup(relays.User, relays.User, TemplatesDirPath) - - // Ensure the static directory exists and set ownership - spinner.UpdateText("Creating static directory...") - directories.CreateDirectory(StaticDirPath, 0755) - - // Use chown command to set ownership of the static directory to the nostr user - directories.SetOwnerAndGroup(relays.User, relays.User, StaticDirPath) - // Ensure the data directory exists and set ownership spinner.UpdateText("Creating data directory...") directories.CreateDirectory(DataDirPath, 0755) + directories.CreateDirectory(fmt.Sprintf("%s/db", DataDirPath), 0755) - // Use chown command to set ownership of the data directory to the nostr user + // Use chown command to set ownership of the data directory and its content to the nostr user directories.SetOwnerAndGroup(relays.User, relays.User, DataDirPath) + // Ensure the config directory exists and set permissions + spinner.UpdateText("Creating config directory...") + directories.CreateDirectory(ConfigDirPath, 0755) + + // Ensure the templates directory exists and set permissions + spinner.UpdateText("Creating templates directory...") + directories.CreateDirectory(TemplatesDirPath, 0755) + + // Use chown command to set ownership of the config directory and its content to the nostr user + directories.SetOwnerAndGroup(relays.User, relays.User, ConfigDirPath) + // Check if the index.html file exists and remove it if it does files.RemoveFile(IndexFilePath) + // Copy the index.html file to templates directory + files.CopyFile(TmpIndexFilePath, TemplatesDirPath) + + // Use chown command to set ownership of the index.html file to the nostr user + files.SetOwnerAndGroup(relays.User, relays.User, IndexFilePath) + + // Remove the static directory and all of its content if it exists + spinner.UpdateText("Removing static directory...") + directories.RemoveDirectory(StaticDirPath) + + // Copy the static directory and all of its content to the templates directory + directories.CopyDirectory(TmpStaticDirPath, TemplatesDirPath) + + // Use chown command to set ownership of the static directory and its content to the nostr user + directories.SetOwnerAndGroup(relays.User, relays.User, StaticDirPath) + // Check if the environment file exists and remove it if it does files.RemoveFile(EnvFilePath) // Check if the service file exists and remove it if it does files.RemoveFile(ServiceFilePath) - // Create the index.html file - spinner.UpdateText("Creating index.html file...") - indexFileParams := templates.IndexFileParams{Domain: domain, HTTPSEnabled: httpsEnabled, PubKey: pubKey} - templates.CreateIndexFile(IndexFilePath, IndexFileTemplate, &indexFileParams) - // Create the environment file spinner.UpdateText("Creating environment file...") envFileParams := systemd.EnvFileParams{Domain: domain, HTTPSEnabled: httpsEnabled, PubKey: pubKey, RelayContact: relayContact} diff --git a/pkg/utils/directories/utils.go b/pkg/utils/directories/utils.go index e90ffd3..5c2aae9 100644 --- a/pkg/utils/directories/utils.go +++ b/pkg/utils/directories/utils.go @@ -30,6 +30,16 @@ func CreateDirectory(path string, permissions FileMode) { } } +// Function to copy a directory and all of its content +func CopyDirectory(dirToCopyPath, destDirPath string) { + err := exec.Command("cp", "-R", dirToCopyPath, destDirPath).Run() + if err != nil { + pterm.Println() + pterm.Error.Println(fmt.Sprintf("Failed to copy the %s directory to the %s directory: %v", dirToCopyPath, destDirPath, err)) + os.Exit(1) + } +} + // Function to set owner and group of a directory func SetOwnerAndGroup(owner, group, dir string) { err := exec.Command("chown", "-R", fmt.Sprintf("%s:%s", owner, group), dir).Run() diff --git a/pkg/utils/templates/utils.go b/pkg/utils/templates/utils.go deleted file mode 100644 index 3f9dab3..0000000 --- a/pkg/utils/templates/utils.go +++ /dev/null @@ -1,41 +0,0 @@ -package templates - -import ( - "fmt" - "github.com/nodetec/rwz/pkg/utils/network" - "github.com/pterm/pterm" - "os" - "text/template" -) - -type IndexFileParams struct { - Domain string - HTTPSEnabled bool - PubKey string -} - -func CreateIndexFile(indexFilePath, indexTemplate string, indexFileParams *IndexFileParams) { - indexFile, err := os.Create(indexFilePath) - if err != nil { - pterm.Println() - pterm.Error.Println(fmt.Sprintf("Failed to create index.html file: %v", err)) - os.Exit(1) - } - defer indexFile.Close() - - indexTmpl, err := template.New("index").Parse(indexTemplate) - if err != nil { - pterm.Println() - pterm.Error.Println(fmt.Sprintf("Failed to parse index.html template: %v", err)) - os.Exit(1) - } - - HTTPScheme := network.HTTPEnabled(indexFileParams.HTTPSEnabled) - - err = indexTmpl.Execute(indexFile, struct{ Domain, HTTPScheme, PubKey string }{Domain: indexFileParams.Domain, HTTPScheme: HTTPScheme, PubKey: indexFileParams.PubKey}) - if err != nil { - pterm.Println() - pterm.Error.Println(fmt.Sprintf("Failed to execute index.html template: %v", err)) - os.Exit(1) - } -}