2021-10-05 07:25:39 +00:00
// Example for how to run
// go run main.go --brokerAddress=localhost:42222
2021-09-24 13:29:30 +00:00
package main
import (
2021-10-06 06:37:25 +00:00
"bufio"
"bytes"
2021-09-24 13:29:30 +00:00
"flag"
"fmt"
"log"
"os"
2021-10-06 06:37:25 +00:00
"os/exec"
2021-10-05 07:25:39 +00:00
"path"
2021-10-06 06:37:25 +00:00
"strings"
2021-09-24 13:29:30 +00:00
"text/template"
)
2021-10-06 06:37:25 +00:00
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" ) {
p := path . Join ( fileDir , "seed.txt" )
err := writekey ( p , [ ] byte ( text ) )
2021-10-06 07:22:44 +00:00
if err != nil {
return err
}
2021-10-06 06:37:25 +00:00
}
if strings . HasPrefix ( text , "U" ) {
p := path . Join ( fileDir , "user.txt" )
err := writekey ( p , [ ] byte ( text ) )
2021-10-06 07:22:44 +00:00
if err != nil {
return err
}
2021-10-06 06:37:25 +00:00
}
}
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
}
2021-09-24 13:29:30 +00:00
// generateEnv will generate the env.env file.
2021-10-06 06:37:25 +00:00
func generateEnv ( fileDir string , templateDir string , brokerAddress string ) error {
templateFile := path . Join ( templateDir , "template_env.env" )
2021-09-24 13:29:30 +00:00
tpl , err := template . ParseFiles ( templateFile )
if err != nil {
2021-10-06 06:37:25 +00:00
return fmt . Errorf ( "error: parsing template file, you might need to set the -templateDir path: %v, err: %v" , templateFile , err )
2021-09-24 13:29:30 +00:00
}
data := struct {
BrokerAddressAndPort string
} {
BrokerAddressAndPort : brokerAddress ,
}
2021-10-06 06:37:25 +00:00
p := path . Join ( fileDir , "env.env" )
fh , err := os . OpenFile ( p , os . O_CREATE | os . O_TRUNC | os . O_RDWR , 0755 )
2021-09-24 13:29:30 +00:00
if err != nil {
return fmt . Errorf ( "error: opening env.env for writing: %v, err: %v" , templateFile , err )
}
defer fh . Close ( )
err = tpl . Execute ( fh , data )
if err != nil {
return fmt . Errorf ( "error: exeuting template file: %v, err: %v" , templateFile , err )
}
return nil
}
// generateDockerCompose will generate the docker-compose.yml file.
2021-10-06 06:37:25 +00:00
func generateDockerCompose ( fileDir string , templateFile string , imageAndVersion string , exposedProfilingPort string , exposedPrometheusPort string , exposedDataFolderPort string , exposedTcpListenerPort string , exposedHttpListenerPort string , nkeySeedFile string , socketFolder string ) error {
2021-09-24 13:29:30 +00:00
tpl , err := template . ParseFiles ( templateFile )
if err != nil {
2021-10-06 06:37:25 +00:00
return fmt . Errorf ( "error: parsing template file, you might need to set the -templateDir flag : %v, err: %v" , templateFile , err )
2021-09-24 13:29:30 +00:00
}
data := struct {
ImageAndVersion string
ExposedProfilingPort string
ExposedPrometheusPort string
ExposedDataFolderPort string
ExposedTcpListenerPort string
ExposedHttpListenerPort string
NkeySeedFile string
SocketFolder string
} {
ImageAndVersion : imageAndVersion ,
ExposedProfilingPort : exposedProfilingPort ,
ExposedPrometheusPort : exposedPrometheusPort ,
ExposedDataFolderPort : exposedDataFolderPort ,
ExposedTcpListenerPort : exposedTcpListenerPort ,
ExposedHttpListenerPort : exposedHttpListenerPort ,
NkeySeedFile : nkeySeedFile ,
SocketFolder : socketFolder ,
}
2021-10-06 06:37:25 +00:00
p := path . Join ( fileDir , "docker-compose.yml" )
fh , err := os . OpenFile ( p , os . O_CREATE | os . O_TRUNC | os . O_RDWR , 0755 )
2021-09-24 13:29:30 +00:00
if err != nil {
2021-10-06 06:37:25 +00:00
return fmt . Errorf ( "error: opening docker-compose.yml for writing: %v, err: %v" , p , err )
2021-09-24 13:29:30 +00:00
}
defer fh . Close ( )
err = tpl . Execute ( fh , data )
if err != nil {
return fmt . Errorf ( "error: exeuting template file: %v, err: %v" , templateFile , err )
}
return nil
}
func main ( ) {
2021-10-06 06:37:25 +00:00
fileDir := flag . String ( "fileDir" , "./" , "the directory path for where to store the files" )
2021-09-24 13:29:30 +00:00
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" )
exposedPrometheusPort := flag . String ( "exposedPrometheusPort" , "2111" , "the address:port to expose" )
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" )
2021-10-06 06:37:25 +00:00
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" )
2021-10-06 07:22:44 +00:00
2021-09-24 13:29:30 +00:00
flag . Parse ( )
if * brokerAddress == "" {
log . Printf ( "error: -brokerAddress flag can not be empty\n" )
return
}
2021-10-05 07:25:39 +00:00
{
2021-10-06 06:37:25 +00:00
err := generateNkeys ( * fileDir )
if err != nil {
log . Printf ( "%v\n" , err )
return
}
}
2021-10-05 07:25:39 +00:00
2021-10-06 06:37:25 +00:00
{
err := generateEnv ( * fileDir , * templateDir , * brokerAddress )
2021-10-05 07:25:39 +00:00
if err != nil {
log . Printf ( "%v\n" , err )
return
}
2021-09-24 13:29:30 +00:00
}
2021-10-05 07:25:39 +00:00
{
2021-10-06 06:37:25 +00:00
template := path . Join ( * templateDir , "template_docker-compose.yml" )
err := generateDockerCompose ( * fileDir , template , * imageAndVersion , * exposedProfilingPort , * exposedPrometheusPort , * exposedDataFolderPort , * exposedTcpListenerPort , * exposedHttpListenerPort , * nkeySeedFile , * socketFolder )
2021-10-05 07:25:39 +00:00
if err != nil {
log . Printf ( "%v\n" , err )
return
}
2021-09-24 13:29:30 +00:00
}
}