diff --git a/scripts/method-helpers/README.md b/scripts/method-helpers/README.md new file mode 100644 index 0000000..01961fc --- /dev/null +++ b/scripts/method-helpers/README.md @@ -0,0 +1,3 @@ +# Method helpers + +A collection of various scripts to be called via method args. diff --git a/scripts/steward/create-docker-compose-files/README.md b/scripts/steward/create-docker-compose-files/README.md new file mode 100644 index 0000000..8d472d4 --- /dev/null +++ b/scripts/steward/create-docker-compose-files/README.md @@ -0,0 +1,23 @@ +# Create docker compose file for Steward + +## docker-compose + + Create a directory where you want your docker compose files, and enter that directory + + Clone the steward repository: + +```bash +mkdir my_dir +cd my_dir +git clone https://github.com/RaaLabs/steward.git +``` + +Then create the public and private nkey's by running: + +`go run ./steward/scripts/nats-server/generate-nkeys/main.go` + +To create the docker-compose and env.env run: + +```bash +go run main.go -brokerAddress=127.0.0.1:4223 -nkeySeedFile=./seed.txt +``` diff --git a/scripts/steward/create-docker-compose-files/main.go b/scripts/steward/create-docker-compose-files/main.go index c004401..7e9f28d 100644 --- a/scripts/steward/create-docker-compose-files/main.go +++ b/scripts/steward/create-docker-compose-files/main.go @@ -4,19 +4,71 @@ package main import ( + "bufio" + "bytes" "flag" "fmt" "log" "os" + "os/exec" "path" + "strings" "text/template" ) +func generateNkeys(fileDir string) error { + cmd := exec.Command("nk", "-gen", "user", "-pubout") + out, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("error: check if the nk tool is installed on the system: %v", err) + } + + fmt.Printf("%v\n", string(out)) + + scanner := bufio.NewScanner(bytes.NewReader(out)) + + for scanner.Scan() { + text := scanner.Text() + fmt.Println("scanning line : ", text) + + if strings.HasPrefix(text, "S") { + fmt.Println("found S") + p := path.Join(fileDir, "seed.txt") + err := writekey(p, []byte(text)) + return err + } + if strings.HasPrefix(text, "U") { + fmt.Println("found U") + p := path.Join(fileDir, "user.txt") + err := writekey(p, []byte(text)) + return err + } + } + + return nil +} + +func writekey(fileName string, b []byte) error { + fh, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600) + if err != nil { + return fmt.Errorf("error: failed to open create/open file for writing: %v", err) + } + defer fh.Close() + + _, err = fh.Write(b) + if err != nil { + return fmt.Errorf("error: failed to write file: %v", err) + } + + return nil +} + // generateEnv will generate the env.env file. -func generateEnv(templateFile string, brokerAddress string) error { +func generateEnv(fileDir string, templateDir string, brokerAddress string) error { + templateFile := path.Join(templateDir, "template_env.env") tpl, err := template.ParseFiles(templateFile) if err != nil { - return fmt.Errorf("error: parsing template file: %v, err: %v", templateFile, err) + return fmt.Errorf("error: parsing template file, you might need to set the -templateDir path: %v, err: %v", templateFile, err) } data := struct { @@ -25,7 +77,8 @@ func generateEnv(templateFile string, brokerAddress string) error { BrokerAddressAndPort: brokerAddress, } - fh, err := os.OpenFile("env.env", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755) + p := path.Join(fileDir, "env.env") + fh, err := os.OpenFile(p, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755) if err != nil { return fmt.Errorf("error: opening env.env for writing: %v, err: %v", templateFile, err) } @@ -40,10 +93,10 @@ func generateEnv(templateFile string, brokerAddress string) error { } // generateDockerCompose will generate the docker-compose.yml file. -func generateDockerCompose(templateFile string, imageAndVersion string, exposedProfilingPort string, exposedPrometheusPort string, exposedDataFolderPort string, exposedTcpListenerPort string, exposedHttpListenerPort string, nkeySeedFile string, socketFolder string) error { +func generateDockerCompose(fileDir string, templateFile string, imageAndVersion string, exposedProfilingPort string, exposedPrometheusPort string, exposedDataFolderPort string, exposedTcpListenerPort string, exposedHttpListenerPort string, nkeySeedFile string, socketFolder string) error { tpl, err := template.ParseFiles(templateFile) if err != nil { - return fmt.Errorf("error: parsing template file: %v, err: %v", templateFile, err) + return fmt.Errorf("error: parsing template file, you might need to set the -templateDir flag : %v, err: %v", templateFile, err) } data := struct { @@ -66,9 +119,10 @@ func generateDockerCompose(templateFile string, imageAndVersion string, exposedP SocketFolder: socketFolder, } - fh, err := os.OpenFile("docker-compose.yml", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755) + p := path.Join(fileDir, "docker-compose.yml") + fh, err := os.OpenFile(p, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755) if err != nil { - return fmt.Errorf("error: opening docker-compose.yml for writing: %v, err: %v", templateFile, err) + return fmt.Errorf("error: opening docker-compose.yml for writing: %v, err: %v", p, err) } defer fh.Close() @@ -81,6 +135,8 @@ func generateDockerCompose(templateFile string, imageAndVersion string, exposedP } func main() { + fileDir := flag.String("fileDir", "./", "the directory path for where to store the files") + brokerAddress := flag.String("brokerAddress", "", "the address:port of the broker to connect to") imageAndVersion := flag.String("imageAndVersion", "", "The name:version of the docker image to use") exposedProfilingPort := flag.String("exposedProfilingPort", "6666", "the address:port to expose") @@ -88,10 +144,11 @@ func main() { exposedDataFolderPort := flag.String("exposedDataFolderPort", "8090", "the address:port to expose") exposedTcpListenerPort := flag.String("exposedTcpListenerPort", "8091", "the address:port to expose") exposedHttpListenerPort := flag.String("exposedHttpListenerPort", "8092", "the address:port to expose") - nkeySeedFile := flag.String("nkeySeedFile", "/tmp/seed.txt", "the complete path of the seed file to mount") - socketFolder := flag.String("sockerFolder", "/tmp/tmp/", "the complete path of the socket folder to mount") + nkeySeedFile := flag.String("nkeySeedFile", "./seed.txt", "the complete path of the seed file to mount") + socketFolder := flag.String("socketFolder", "./tmp/", "the complete path of the socket folder to mount") + + templateDir := flag.String("templateDir", "./steward/scripts/steward/create-docker-compose-files/", "the directory path to where the template files are located") flag.Parse() - templateDir := flag.String("templateDir", "./", "the directory path to where the template files are located") if *brokerAddress == "" { log.Printf("error: -brokerAddress flag can not be empty\n") @@ -99,9 +156,7 @@ func main() { } { - p := path.Join(*templateDir, "template_env.env") - - err := generateEnv(p, *brokerAddress) + err := generateNkeys(*fileDir) if err != nil { log.Printf("%v\n", err) return @@ -109,8 +164,16 @@ func main() { } { - p := path.Join(*templateDir, "template_docker-compose.yml") - err := generateDockerCompose(p, *imageAndVersion, *exposedProfilingPort, *exposedPrometheusPort, *exposedDataFolderPort, *exposedTcpListenerPort, *exposedHttpListenerPort, *nkeySeedFile, *socketFolder) + err := generateEnv(*fileDir, *templateDir, *brokerAddress) + if err != nil { + log.Printf("%v\n", err) + return + } + } + + { + template := path.Join(*templateDir, "template_docker-compose.yml") + err := generateDockerCompose(*fileDir, template, *imageAndVersion, *exposedProfilingPort, *exposedPrometheusPort, *exposedDataFolderPort, *exposedTcpListenerPort, *exposedHttpListenerPort, *nkeySeedFile, *socketFolder) if err != nil { log.Printf("%v\n", err) return