From b4e1351b7d9f1864f0d1f6848ebe7fa33de393ec Mon Sep 17 00:00:00 2001 From: abdfnx Date: Fri, 16 Sep 2022 11:15:59 +0300 Subject: [PATCH] update `botway init --docker` and `botway vars set` commands api --- cmd/app/init.go | 7 +-- cmd/app/vars.go | 2 + dockerfiles/npm.dockerfile | 3 +- dockerfiles/pnpm.dockerfile | 3 +- go.mod | 6 +-- go.sum | 12 ++--- internal/railway/check-buildkit.go | 19 +++---- internal/railway/deploy.go | 5 +- internal/railway/vars.go | 69 +++++++++++++++++++++--- packages/botwaygo | 2 +- tools/setup-tokens-in-docker.go | 87 ------------------------------ 11 files changed, 88 insertions(+), 127 deletions(-) diff --git a/cmd/app/init.go b/cmd/app/init.go index 54ec154c..fe2662ec 100755 --- a/cmd/app/init.go +++ b/cmd/app/init.go @@ -4,7 +4,6 @@ import ( "github.com/abdfnx/botway/internal/options" "github.com/abdfnx/botway/internal/pipes/initx" "github.com/abdfnx/botway/tools" - "github.com/abdfnx/botwaygo" "github.com/spf13/cobra" ) @@ -22,11 +21,7 @@ func InitCMD() *cobra.Command { if opts.Docker { initx.DockerInit() - if botwaygo.GetBotInfo("bot.host_service") == "render.com" { - tools.SetupTokensInDockerRender() - } else { - tools.SetupTokensInDocker() - } + tools.SetupTokensInDocker() } else { initx.Init() diff --git a/cmd/app/vars.go b/cmd/app/vars.go index 94380812..440b0013 100644 --- a/cmd/app/vars.go +++ b/cmd/app/vars.go @@ -52,6 +52,8 @@ func VarsCMD() *cobra.Command { variablesGetCmd.Flags().StringP("service", "s", "", desc) variablesSetCmd.Flags().StringP("service", "s", "", desc) + variablesSetCmd.Flags().BoolP("no-redeploy-hint", "", false, "Don't show re-deploy hints after setting a new variable") + variablesSetCmd.Flags().BoolP("hidden", "", false, "Hide variable value") variablesRemoveCmd.Flags().StringP("service", "s", "", desc) } else if botwaygo.GetBotInfo("bot.host_service") == "render.com" { cmd.Run = func(cmd *cobra.Command, args []string) { diff --git a/dockerfiles/npm.dockerfile b/dockerfiles/npm.dockerfile index 2d40cc11..16b3ec53 100644 --- a/dockerfiles/npm.dockerfile +++ b/dockerfiles/npm.dockerfile @@ -6,7 +6,6 @@ RUN botway init --docker FROM node:alpine -ENV NODE_ENV "production" ENV PACKAGES "build-dependencies libtool autoconf automake gcc gcc-doc g++ make py3-pip py-pip zlib-dev python3 python3-dev libffi-dev build-base gcc git ffmpeg binutils openssl-dev zlib-dev boost boost-dev" COPY --from=bw /root/.botway /root/.botway @@ -19,6 +18,6 @@ RUN apk update && \ # Add packages you want # RUN apk add PACKAGE_NAME -RUN npm i --production +RUN npm i ENTRYPOINT ["npm", "start"] diff --git a/dockerfiles/pnpm.dockerfile b/dockerfiles/pnpm.dockerfile index 0886ed68..f4ddc242 100644 --- a/dockerfiles/pnpm.dockerfile +++ b/dockerfiles/pnpm.dockerfile @@ -6,7 +6,6 @@ RUN botway init --docker FROM node:alpine -ENV NODE_ENV "production" ENV PACKAGES "build-dependencies libtool autoconf automake gcc gcc-doc g++ make py3-pip py-pip zlib-dev python3 python3-dev libffi-dev build-base gcc git ffmpeg binutils openssl-dev zlib-dev boost boost-dev" RUN apk update && \ @@ -19,6 +18,8 @@ COPY --from=bw /root/.botway /root/.botway COPY . . +RUN npm i -g pnpm + RUN pnpm fetch --prod RUN pnpm install diff --git a/go.mod b/go.mod index e09f55b5..afb63387 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,12 @@ go 1.18 require ( github.com/AlecAivazis/survey/v2 v2.3.6 github.com/MakeNowJust/heredoc v1.0.0 - github.com/abdfnx/botwaygo v0.0.9 + github.com/abdfnx/botwaygo v0.1.0 github.com/abdfnx/looker v0.1.0 github.com/abdfnx/resto v0.1.6 github.com/abdfnx/tran v0.1.43 github.com/botwayorg/gh v0.0.9 - github.com/botwayorg/railway-api v0.0.3 + github.com/botwayorg/railway-api v0.0.4 github.com/briandowns/spinner v1.19.0 github.com/charmbracelet/bubbles v0.14.0 github.com/charmbracelet/bubbletea v0.22.1 @@ -26,7 +26,7 @@ require ( github.com/spf13/viper v1.13.0 github.com/tidwall/gjson v1.14.3 github.com/tidwall/sjson v1.2.5 - golang.org/x/sys v0.0.0-20220913175220-63ea55921009 + golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 745bd134..d146b008 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,8 @@ github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= -github.com/abdfnx/botwaygo v0.0.9 h1:Y1ZZst9Y97CYUvGfVQy7xOAKVtUYjU9qcmGlmq3UCV8= -github.com/abdfnx/botwaygo v0.0.9/go.mod h1:cSnH5z3N9YTIYlzOXwglI+gChnRFSvtNDkWJ7KMoqso= +github.com/abdfnx/botwaygo v0.1.0 h1:ypoen9U63lTWQjt7KfGSOyIFjs9ROUW7JjDFdYIKI3o= +github.com/abdfnx/botwaygo v0.1.0/go.mod h1:H1WMCWGuQ8Du9PF1/h+MJWI/FwEnmO40GKwjbKJ79iI= github.com/abdfnx/looker v0.1.0 h1:tMN7E0wKIgbydAPPQ1RkppJ1bGHn+B+y9PZy7mwa+3U= github.com/abdfnx/looker v0.1.0/go.mod h1:QVfPHnredPBUg4R+MtEkZbMBbqrgtoaj0JHO3KYkvyE= github.com/abdfnx/resto v0.1.6 h1:yOM9O9bpMP4lb2ox0U7/gcFXO78P5eUZBxWuKrfdrFA= @@ -62,8 +62,8 @@ github.com/botwayorg/gh v0.0.9 h1:jHXglsDdAup+wNAhLvqeeXNILTR4DEL12gubyDcxEWs= github.com/botwayorg/gh v0.0.9/go.mod h1:E3dTBPLt3L9wogY7e4QPHqUYHAbWrlNnumKgu9tcglA= github.com/botwayorg/git v0.0.0-20220813023031-a7a261511635 h1:AJLUr0Ef6avA5S2SXua7XG4yXI7vAjsZLBrZD5zCOk4= github.com/botwayorg/git v0.0.0-20220813023031-a7a261511635/go.mod h1:XdqqKSxdEZz88SByHfT4Vwpl/b9gQK9tjZ/FQuwrM0g= -github.com/botwayorg/railway-api v0.0.3 h1:7PIB7gkuswiG/iqLU1VtE8sHHIfiajtDRM6MEKrP82Y= -github.com/botwayorg/railway-api v0.0.3/go.mod h1:pc0Ko0nhKt++RMk0O3+cmMZQtpNOlCqQDIVgjN95nos= +github.com/botwayorg/railway-api v0.0.4 h1:vAvbBan3CZXN82X7zofdt6ix/9VpWboiuq15rSMTGQM= +github.com/botwayorg/railway-api v0.0.4/go.mod h1:oJjlseKXKSc2A7dlPTLtEfm42MIElTivzC8+uWmduAM= github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E= github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -513,8 +513,8 @@ golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220913175220-63ea55921009 h1:PuvuRMeLWqsf/ZdT1UUZz0syhioyv1mzuFZsXs4fvhw= -golang.org/x/sys v0.0.0-20220913175220-63ea55921009/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc= +golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= diff --git a/internal/railway/check-buildkit.go b/internal/railway/check-buildkit.go index 5cdaa0dc..0832cb33 100644 --- a/internal/railway/check-buildkit.go +++ b/internal/railway/check-buildkit.go @@ -1,34 +1,29 @@ package railway import ( - "bytes" "log" "os" "os/exec" "runtime" - "github.com/abdfnx/botway/constants" + "github.com/abdfnx/botway/internal/config" "github.com/abdfnx/botway/tools" + "github.com/abdfnx/botwaygo" "github.com/spf13/viper" - "github.com/tidwall/gjson" ) func CheckBuildKit() { tools.CheckDir() - viper.SetConfigType("yaml") + checkBuildKit := botwaygo.GetBotInfo("docker.enable_buildkit") - viper.ReadConfig(bytes.NewBuffer(constants.BotConfig)) + setVarCmd := "botway vars set --no-redeploy-hint DOCKER_BUILDKIT=0" - checkBuildKit := viper.GetBool("docker.enable_buildkit") - - setVarCmd := "botway vars set DOCKER_BUILDKIT=0" - - if checkBuildKit { - setVarCmd = "botway vars set DOCKER_BUILDKIT=1" + if checkBuildKit == "true" { + setVarCmd = "botway vars set --no-redeploy-hint DOCKER_BUILDKIT=1" } - botPath := gjson.Get(string(constants.BotwayConfig), "botway.bots."+viper.GetString("bot.name")+".path").String() + botPath := config.Get("botway.bots." + viper.GetString("bot.name") + ".path") cmd := exec.Command("bash", "-c", setVarCmd) diff --git a/internal/railway/deploy.go b/internal/railway/deploy.go index ee089497..b23e9a4b 100644 --- a/internal/railway/deploy.go +++ b/internal/railway/deploy.go @@ -8,6 +8,7 @@ import ( "github.com/abdfnx/botway/constants" "github.com/abdfnx/botway/tools" + "github.com/abdfnx/botwaygo" "github.com/botwayorg/railway-api/entity" CLIErrors "github.com/botwayorg/railway-api/errors" "github.com/botwayorg/railway-api/ui" @@ -49,8 +50,10 @@ func (h *Handler) Delpoy(ctx context.Context, req *entity.CommandRequest) error src = "." } + UpdateTokens(src, botwaygo.GetBotInfo("bot.type")) + fmt.Print(constants.INFO_BACKGROUND.Render("INFO")) - fmt.Println(constants.INFO_FOREGROUND.Render("Uploading directory " + constants.BOLD.Render(src))) + fmt.Println(constants.INFO_FOREGROUND.Render(" Uploading directory " + constants.BOLD.Render(src))) if isVerbose { fmt.Print(constants.INFO_BACKGROUND.Render("INFO")) diff --git a/internal/railway/vars.go b/internal/railway/vars.go index 7f6a0cc2..d6d55baa 100644 --- a/internal/railway/vars.go +++ b/internal/railway/vars.go @@ -4,10 +4,15 @@ import ( "context" "errors" "fmt" + "log" + "os" + "os/exec" + "runtime" "strings" "time" "github.com/abdfnx/botway/constants" + "github.com/abdfnx/botwaygo" "github.com/botwayorg/railway-api/entity" "github.com/botwayorg/railway-api/ui" "github.com/briandowns/spinner" @@ -31,7 +36,7 @@ func (h *Handler) Variables(ctx context.Context, req *entity.CommandRequest) err } fmt.Print(constants.HEADING + (fmt.Sprintf("%s Environment Variables", environment.Name))) - fmt.Print(ui.KeyValues(*envs)) + fmt.Print(ui.KeyValues(*envs, false)) return nil } @@ -76,6 +81,18 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest) replace = false } + noRedeployHint, err := req.Cmd.Flags().GetBool("no-redeploy-hint") + + if err != nil { + noRedeployHint = false + } + + hidden, err := req.Cmd.Flags().GetBool("hidden") + + if err != nil { + hidden = false + } + yes, err := req.Cmd.Flags().GetBool("yes") if err != nil { @@ -131,8 +148,8 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest) operation = "Replaced existing variables with" } - fmt.Print(constants.HEADING + (fmt.Sprintf("%s %s for \"%s\"", operation, strings.Join(updatedEnvNames, ", "), environment.Name))) - fmt.Print(ui.KeyValues(*variables)) + fmt.Println(constants.HEADING + (fmt.Sprintf("%s %s for \"%s\"", operation, strings.Join(updatedEnvNames, ", "), environment.Name))) + fmt.Print(ui.KeyValues(*variables, hidden)) if !skipRedeploy { serviceID, err := h.ctrl.GetServiceIdByName(ctx, &serviceName) @@ -141,7 +158,7 @@ func (h *Handler) VariablesSet(ctx context.Context, req *entity.CommandRequest) return err } - err = h.redeployAfterVariablesChange(ctx, environment, serviceID) + err = h.redeployAfterVariablesChange(ctx, environment, serviceID, noRedeployHint) if err != nil { return err @@ -163,6 +180,12 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques skipRedeploy = false } + noRedeployHint, err := req.Cmd.Flags().GetBool("no-redeploy-hint") + + if err != nil { + noRedeployHint = false + } + err = h.ctrl.DeleteEnvs(ctx, req.Args, &serviceName) if err != nil { return err @@ -181,7 +204,7 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques return err } - err = h.redeployAfterVariablesChange(ctx, environment, serviceID) + err = h.redeployAfterVariablesChange(ctx, environment, serviceID, noRedeployHint) if err != nil { return err } @@ -190,7 +213,7 @@ func (h *Handler) VariablesDelete(ctx context.Context, req *entity.CommandReques return nil } -func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment *entity.Environment, serviceID *string) error { +func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment *entity.Environment, serviceID *string, noRedeployHint bool) error { deployments, err := h.ctrl.GetDeployments(ctx) if err != nil { return err @@ -204,8 +227,10 @@ func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment // Don't redeploy if the latest deploy for environment came from up latestDeploy := deployments[0] if latestDeploy.Meta == nil || latestDeploy.Meta.Repo == "" { - fmt.Print(constants.INFO_BACKGROUND.Render("INFO")) - fmt.Println(constants.INFO_FOREGROUND.Render(fmt.Sprintf(" Run %s to redeploy your project ", constants.COMMAND_FOREGROUND.Render("botway deploy")))) + if !noRedeployHint { + fmt.Print(constants.INFO_BACKGROUND.Render("INFO")) + fmt.Println(constants.INFO_FOREGROUND.Render(fmt.Sprintf(" Run %s to redeploy your project ", constants.COMMAND_FOREGROUND.Render("botway deploy")))) + } return nil } @@ -227,3 +252,31 @@ func (h *Handler) redeployAfterVariablesChange(ctx context.Context, environment return nil } + +func UpdateTokens(botPath, botType string) { + setVarCmd := "botway vars set --no-redeploy-hint --hidden " + + if botType == "discord" { + setVarCmd += "DISCORD_TOKEN=" + botwaygo.GetToken() + " DISCORD_CLIENT_ID=" + botwaygo.GetAppId() + } else if botType == "slack" { + setVarCmd += "SLACK_TOKEN=" + botwaygo.GetToken() + " SLACK_APP_TOKEN=" + botwaygo.GetAppId() + " SIGNING_SECRET=" + botwaygo.GetSigningSecret() + } else if botType == "telegram" { + setVarCmd += "TELEGRAM_TOKEN=" + botwaygo.GetToken() + } + + cmd := exec.Command("bash", "-c", setVarCmd) + + if runtime.GOOS == "windows" { + cmd = exec.Command("powershell.exe", setVarCmd) + } + + cmd.Dir = botPath + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + + if err != nil { + log.Printf("error: %v\n", err) + } +} diff --git a/packages/botwaygo b/packages/botwaygo index a6a013fc..3fc2ecd1 160000 --- a/packages/botwaygo +++ b/packages/botwaygo @@ -1 +1 @@ -Subproject commit a6a013fca2577bf80cac53fd1cb872b5aa045f83 +Subproject commit 3fc2ecd11807ab2b6cd4c2da904560ae7811ceca diff --git a/tools/setup-tokens-in-docker.go b/tools/setup-tokens-in-docker.go index aaec8965..2ca7a957 100644 --- a/tools/setup-tokens-in-docker.go +++ b/tools/setup-tokens-in-docker.go @@ -1,7 +1,6 @@ package tools import ( - "bytes" "fmt" "os" "path/filepath" @@ -38,92 +37,6 @@ func SetupTokensInDocker() { bot_token = "TELEGRAM_TOKEN" } - botwayConfig := viper.New() - env := viper.New() - - botwayConfig.SetConfigType("json") - - if constants.Berr != nil { - panic(constants.Berr) - } - - botwayConfig.ReadConfig(bytes.NewBuffer(constants.BotwayConfig)) - botPath := botwayConfig.GetString("botway.bots." + botName + ".path") - - env.AddConfigPath(filepath.Join(botPath, "config")) - env.SetConfigName("botway-tokens") - env.SetConfigType("env") - - bot_token_content := botwayConfig.GetString("botway.bots." + botName + ".bot_token") - app_token_content := botwayConfig.GetString("botway.bots." + botName + "." + cid) - - if bot_token_content == "" || botType != "telegram" && app_token_content == "" || botType == "slack" && signing_secret == "" { - fmt.Print(constants.FAIL_BACKGROUND.Render("ERROR")) - fmt.Print(" ") - panic(constants.FAIL_FOREGROUND.Render("You didn't set bot token or app token or signing secret")) - } - - env.SetDefault(bot_token, bot_token_content) - - if botType != "telegram" { - env.SetDefault(app_token, app_token_content) - } - - if botType == "discord" { - if constants.Gerr != nil { - panic(constants.Gerr) - } else { - guilds := gjson.Get(string(constants.Guilds), "guilds.#") - - for x := 0; x < int(guilds.Int()); x++ { - server := gjson.Get(string(constants.Guilds), "guilds."+fmt.Sprint(x)).String() - - sgi := strings.ToUpper(server) + "_GUILD_ID" - sgi_content := botwayConfig.GetString("botway.bots." + botName + ".guilds." + server + ".server_id") - - env.Set(sgi, sgi_content) - } - } - } - - if botType == "slack" { - signing_secret_content := botwayConfig.GetString("botway.bots." + botName + ".signing_secret") - - env.SetDefault(signing_secret, signing_secret_content) - } - - if err := env.SafeWriteConfig(); err != nil { - if os.IsNotExist(err) { - err = env.WriteConfig() - - if err != nil { - panic(err) - } - } - } - - if err := env.ReadInConfig(); err != nil { - if _, ok := err.(viper.ConfigFileNotFoundError); ok { - panic(err) - } - } -} - -func SetupTokensInDockerRender() { - CheckDir() - - if botType == "discord" { - bot_token = "DISCORD_TOKEN" - app_token = "DISCORD_CLIENT_ID" - cid = "bot_app_id" - } else if botType == "slack" { - bot_token = "SLACK_TOKEN" - app_token = "SLACK_APP_TOKEN" - cid = "bot_app_token" - } else if botType == "telegram" { - bot_token = "TELEGRAM_TOKEN" - } - env := viper.New() pwd, _ := os.Getwd()