From 28d195bc30719d05932458cd6a9de0109f4d7945 Mon Sep 17 00:00:00 2001 From: Alexei Yuzhakov Date: Mon, 7 Jun 2021 21:07:34 +0700 Subject: [PATCH] Prototype of automated deployer --- actions/upload.go | 4 +++ cmd/deploy.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + locales/locales.go | 2 ++ 4 files changed, 70 insertions(+) create mode 100644 cmd/deploy.go diff --git a/actions/upload.go b/actions/upload.go index b029610..e827924 100644 --- a/actions/upload.go +++ b/actions/upload.go @@ -89,6 +89,10 @@ func UploadDirectory(host types.Server, domain types.Domain, ovw bool, dry bool, } return filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { + if info.IsDir() && (info.Name() == ".git" || info.Name() == ".idea") { + return filepath.SkipDir + } + pathPart := strings.TrimPrefix(p, dir) if pathPart == "." { return nil diff --git a/cmd/deploy.go b/cmd/deploy.go new file mode 100644 index 0000000..1b6d736 --- /dev/null +++ b/cmd/deploy.go @@ -0,0 +1,63 @@ +// Copyright 1999-2021. Plesk International GmbH. + +package cmd + +import ( + "errors" + "fmt" + "github.com/plesk/pleskapp/plesk/actions" + "github.com/plesk/pleskapp/plesk/config" + "github.com/plesk/pleskapp/plesk/locales" + "github.com/plesk/pleskapp/plesk/types" + "github.com/spf13/cobra" + "os" + "path" + "strings" +) + +var deployCmd = &cobra.Command{ + Use: "deploy [SERVER]", + Short: locales.L.Get("deploy.description"), + RunE: func(cmd *cobra.Command, args []string) error { + workDir, _ := os.Getwd() + appName := path.Base(workDir) + fmt.Println("App name:", appName) + + server, err := config.GetServerByArgs(args) + if err != nil { + return err + } + + if 0 == len(server.Info.IP.IPv4) { + return errors.New("IPv4 address is required") + } + defaultIp := server.Info.IP.IPv4[0] + fmt.Printf("IP: %v\n", defaultIp) + + domainName := fmt.Sprintf("%s.%s.plesk.page", appName, strings.ReplaceAll(defaultIp, ".", "-")) + fmt.Println("Domain name:", domainName) + + var domain *types.Domain + domain, err = config.GetDomain(*server, domainName) + if err != nil { + fmt.Printf("Creating the domain %s...\n", domainName) + err = actions.DomainAdd(*server, domainName, types.ServerIPAddresses{ + IPv4: []string{defaultIp}, + }) + if err != nil { + return err + } + fmt.Println("Domain has been created.") + } else { + fmt.Printf("Domain %s has been found.\n", domainName) + } + + fmt.Println("Uploading the content...") + err = actions.UploadDirectory(*server, *domain, true, false, workDir, nil) + if err != nil { + return err + } + + return nil + }, +} diff --git a/cmd/root.go b/cmd/root.go index 8f762c7..65d34c8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,6 +33,7 @@ func Execute() error { webCmd, completionCmd, pleskCmd, + deployCmd, ) return rootCmd.Execute() diff --git a/locales/locales.go b/locales/locales.go index 8cb2fb1..1471589 100644 --- a/locales/locales.go +++ b/locales/locales.go @@ -140,6 +140,8 @@ func init() { "web.description": "Run local web server to serve current directory", "completion.description": "Output shell completion code for the specified shell", + + "deploy.description": "Deploy the app in the current directory to default server", }, }