2021-10-05 11:37:45 +00:00
// Example for how to run
// go run main.go -imageAndVersion=nats-server:2.5.0 -natsConfPath=./nats.conf -leCertPath=./le.crt -leKeyPath=./le.key -exposedNatsPort=40223
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"text/template"
)
// generateEnv will generate the env.env file.
func generateEnv ( templateFile string , eData envData ) error {
tpl , err := template . ParseFiles ( templateFile )
if err != nil {
2021-10-05 13:51:30 +00:00
return fmt . Errorf ( "error: parsing template file, check that the templateDir flag is set to the correct path: %v, err: %v" , templateFile , err )
2021-10-05 11:37:45 +00:00
}
fh , err := os . OpenFile ( "env.env" , 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 )
}
defer fh . Close ( )
err = tpl . Execute ( fh , eData )
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.
func generateDockerCompose ( templateFile string , cData composeData ) error {
tpl , err := template . ParseFiles ( templateFile )
if err != nil {
2021-10-05 13:51:30 +00:00
return fmt . Errorf ( "error: parsing template file, check that the templateDir flag is set to the correct path: %v, err: %v" , templateFile , err )
2021-10-05 11:37:45 +00:00
}
fh , err := os . OpenFile ( "docker-compose.yml" , 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 )
}
defer fh . Close ( )
err = tpl . Execute ( fh , cData )
if err != nil {
return fmt . Errorf ( "error: exeuting template file: %v, err: %v" , templateFile , err )
}
return nil
}
type composeData struct {
2022-02-14 04:52:17 +00:00
ContainerName string
2021-10-05 11:37:45 +00:00
ImageAndVersion string
NatsConfPath string
LeCertPath string
LeKeyPath string
ExposedNatsPort string
}
type envData struct {
NatsConfig string
Flags string
}
func main ( ) {
2022-02-14 04:52:17 +00:00
containerName := flag . String ( "containerName" , "" , "Name of the docker container" )
2021-10-05 11:37:45 +00:00
imageAndVersion := flag . String ( "imageAndVersion" , "" , "The name:version of the docker image to use" )
natsConfPath := flag . String ( "natsConfPath" , "./nats.conf" , "the full path of the nats.conf file" )
leCertPath := flag . String ( "leCertPath" , "" , "the full path to the LetsEncrypt crt file" )
leKeyPath := flag . String ( "leKeyPath" , "" , "the full path to the LetsEncrypt key file" )
exposedNatsPort := flag . String ( "exposedNatsPort" , "" , "the port docker will expose nats on" )
2021-12-08 09:13:37 +00:00
flags := flag . String ( "Flags" , "-D" , "flags to start nats-server with" )
2021-10-05 11:37:45 +00:00
2021-10-05 13:51:30 +00:00
templateDir := flag . String ( "templateDir" , "./steward/scripts/nats-server/create-docker-compose-files/" , "the directory path to where the template files are located" )
2021-10-05 11:37:45 +00:00
flag . Parse ( )
cData := composeData {
2022-02-14 04:52:17 +00:00
ContainerName : * containerName ,
2021-10-05 11:37:45 +00:00
ImageAndVersion : * imageAndVersion ,
NatsConfPath : * natsConfPath ,
LeCertPath : * leCertPath ,
LeKeyPath : * leKeyPath ,
ExposedNatsPort : * exposedNatsPort ,
}
eData := envData {
Flags : * flags ,
}
2022-02-14 04:52:17 +00:00
if cData . ContainerName == "" {
log . Printf ( "error: -containerName flag can not be empty\n" )
return
}
2021-10-05 11:37:45 +00:00
if cData . ImageAndVersion == "" {
log . Printf ( "error: -imageAndVersion flag can not be empty\n" )
return
}
if cData . NatsConfPath == "" {
log . Printf ( "error: -natsConfPath flag can not be empty\n" )
return
}
if cData . LeCertPath == "" {
log . Printf ( "error: -leCertPath flag can not be empty\n" )
return
}
if cData . LeKeyPath == "" {
log . Printf ( "error: -leKeyPath flag can not be empty\n" )
return
}
if cData . ExposedNatsPort == "" {
log . Printf ( "error: -exposedNatsPort flag can not be empty\n" )
return
}
{
2021-12-08 09:20:06 +00:00
p := path . Join ( * templateDir , "env.env.tpl" )
2021-10-05 11:37:45 +00:00
err := generateEnv ( p , eData )
if err != nil {
log . Printf ( "%v\n" , err )
return
}
}
{
2021-12-08 09:20:06 +00:00
p := path . Join ( * templateDir , "docker-compose.yml.tpl" )
2021-10-05 11:37:45 +00:00
err := generateDockerCompose ( p , cData )
if err != nil {
log . Printf ( "%v\n" , err )
return
}
}
}