From 3b40b7149d4e3c0779adc29357104bda6b3eeb62 Mon Sep 17 00:00:00 2001 From: Cyrill Troxler Date: Wed, 17 Jul 2024 16:53:35 +0200 Subject: [PATCH] feat: add dockerfile flags This adds all relevant flags for the app create/update command to enable and configure dockerfile build support. --- create/application.go | 17 +++++++++++++++++ internal/format/print.go | 4 ++++ update/application.go | 23 +++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/create/application.go b/create/application.go index d78513e..971ba01 100644 --- a/create/application.go +++ b/create/application.go @@ -46,6 +46,7 @@ type applicationCmd struct { SkipRepoAccessCheck bool `help:"Skip the git repository access check" default:"false"` Debug bool `help:"Enable debug messages" default:"false"` Language string `help:"${app_language_help} Possible values: ${enum}" enum:"ruby,php,python,golang,nodejs,static,ruby-heroku," default:""` + DockerfileBuild dockerfileBuild `embed:""` } type gitConfig struct { @@ -65,6 +66,12 @@ type deployJob struct { Timeout time.Duration `default:"${app_default_deploy_job_timeout}" help:"Timeout of the job. Default is ${app_default_deploy_job_timeout}, minimum is 1 minute and maximum is 30 minutes."` } +type dockerfileBuild struct { + Enabled bool `name:"dockerfile" help:"${app_dockerfile_enable_help}" default:"false"` + Path string `name:"dockerfile-path" help:"${app_dockerfile_path_help}" default:""` + BuildContext string `name:"dockerfile-build-context" help:"${app_dockerfile_build_context_help}" default:""` +} + func (g gitConfig) sshPrivateKey() (*string, error) { if g.SSHPrivateKey != nil { return util.ValidatePEM(*g.SSHPrivateKey) @@ -294,6 +301,11 @@ func (app *applicationCmd) newApplication(project string) *apps.Application { Hosts: app.Hosts, Config: app.config(), BuildEnv: util.EnvVarsFromMap(app.BuildEnv), + DockerfileBuild: apps.DockerfileBuild{ + Enabled: app.DockerfileBuild.Enabled, + DockerfilePath: app.DockerfileBuild.Path, + BuildContext: app.DockerfileBuild.BuildContext, + }, }, }, } @@ -572,5 +584,10 @@ func ApplicationKongVars() (kong.Vars, error) { result["app_language_help"] = "Language specifies which language your app is. " + "If left empty, deploio will detect the language automatically. " + "Note that *-heroku languages are experimental and may be removed in future releases." + result["app_dockerfile_enable_help"] = "Enable Dockerfile build (Beta) instead of the automatic " + + "buildpack detection" + result["app_dockerfile_path_help"] = "Specifies the path to the Dockerfile. If left empty a file " + + "named Dockerfile will be searched in the application code root directory." + result["app_dockerfile_build_context_help"] = "Defines the build context. If left empty, the application code root directory will be used as build context." return result, nil } diff --git a/internal/format/print.go b/internal/format/print.go index 911a015..17ba927 100644 --- a/internal/format/print.go +++ b/internal/format/print.go @@ -53,6 +53,10 @@ func PrintFailuref(icon, format string, a ...any) { fmt.Print(FailureMessagef(icon, format, a...) + "\n") } +func PrintWarningf(msg string, a ...any) { + fmt.Printf(color.YellowString("Warning: ")+msg, a...) +} + // Confirmf prints a confirm dialog using format and then waits until prompt // is confirmed or denied. Only y and yes are accepted for confirmation. func Confirmf(format string, a ...any) (bool, error) { diff --git a/update/application.go b/update/application.go index 9d499e8..a7e902d 100644 --- a/update/application.go +++ b/update/application.go @@ -11,6 +11,7 @@ import ( "github.com/ninech/nctl/api" "github.com/ninech/nctl/api/util" "github.com/ninech/nctl/api/validation" + "github.com/ninech/nctl/internal/format" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -38,6 +39,7 @@ type applicationCmd struct { SkipRepoAccessCheck bool `help:"Skip the git repository access check" default:"false"` Debug bool `help:"Enable debug messages" default:"false"` Language *string `help:"${app_language_help} Possible values: ${enum}" enum:"ruby,php,python,golang,nodejs,static,ruby-heroku," default:""` + DockerfileBuild dockerfileBuild `embed:""` } type gitConfig struct { @@ -79,6 +81,11 @@ type deployJob struct { Timeout *time.Duration `help:"Timeout of the job." placeholder:"${app_default_deploy_job_timeout}"` } +type dockerfileBuild struct { + Path *string `name:"dockerfile-path" help:"${app_dockerfile_path_help}" placeholder:"."` + BuildContext *string `name:"dockerfile-build-context" help:"${app_dockerfile_build_context_help}" placeholder:"."` +} + func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error { app := &apps.Application{ ObjectMeta: metav1.ObjectMeta{ @@ -223,6 +230,16 @@ func (cmd *applicationCmd) applyUpdates(app *apps.Application) { buildDelEnv = *cmd.DeleteBuildEnv } app.Spec.ForProvider.BuildEnv = util.UpdateEnvVars(app.Spec.ForProvider.BuildEnv, buildEnv, buildDelEnv) + + if cmd.DockerfileBuild.Path != nil { + app.Spec.ForProvider.DockerfileBuild.DockerfilePath = *cmd.DockerfileBuild.Path + warnIfDockerfileNotEnabled(app, "path") + } + + if cmd.DockerfileBuild.BuildContext != nil { + app.Spec.ForProvider.DockerfileBuild.BuildContext = *cmd.DockerfileBuild.BuildContext + warnIfDockerfileNotEnabled(app, "build context") + } } func (job deployJob) applyUpdates(cfg *apps.Config) { @@ -253,3 +270,9 @@ func ensureDeployJob(cfg *apps.Config) *apps.Config { } return cfg } + +func warnIfDockerfileNotEnabled(app *apps.Application, flag string) { + if !app.Spec.ForProvider.DockerfileBuild.Enabled { + format.PrintWarningf("updating %s has no effect as dockefile builds are not enabled on this app\n", flag) + } +}