From 143133ffc1e3a68e5e1043e3ab714ff1e137bf17 Mon Sep 17 00:00:00 2001 From: Yvonnick Esnault Date: Mon, 19 Mar 2018 15:27:57 +0100 Subject: [PATCH] feat (engine): engine isupdate (#2408) --- cli/cdsctl/update.go | 2 +- engine/main.go | 4 ++ engine/update.go | 6 ++- engine/uptodate.go | 63 ++++++++++++++++++++++++++++++++ engine/worker/cmd_update.go | 2 +- sdk/cdsclient/client_download.go | 6 +-- sdk/cdsclient/client_mon.go | 8 ++++ sdk/cdsclient/interface.go | 3 +- 8 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 engine/uptodate.go diff --git a/cli/cdsctl/update.go b/cli/cdsctl/update.go index f6d6ab5ff7..b7fcaea6e4 100644 --- a/cli/cdsctl/update.go +++ b/cli/cdsctl/update.go @@ -32,7 +32,7 @@ func updateRun(v cli.Values) error { if v.GetBool("from-github") { // no need to have apiEndpoint here var errGH error - urlBinary, errGH = client.DownloadURLFromGithub("cdsctl", runtime.GOOS, runtime.GOARCH) + urlBinary, errGH = client.DownloadURLFromGithub(sdk.GetArtifactFilename("cdsctl", runtime.GOOS, runtime.GOARCH)) if errGH != nil { return fmt.Errorf("Error while getting URL from Github url:%s err:%s", urlBinary, errGH) } diff --git a/engine/main.go b/engine/main.go index 1a3afc3536..79d71a7b2f 100644 --- a/engine/main.go +++ b/engine/main.go @@ -56,6 +56,10 @@ func init() { updateCmd.Flags().BoolVar(&updateFromGithub, "from-github", false, "Update binary from latest github release") updateCmd.Flags().StringVar(&updateURLAPI, "api", "", "Update binary from a CDS Engine API") + mainCmd.AddCommand(uptodateCmd) + uptodateCmd.Flags().BoolVar(&updateFromGithub, "from-github", false, "Update binary from latest github release") + uptodateCmd.Flags().StringVar(&updateURLAPI, "api", "", "Update binary from a CDS Engine API") + //Database command mainCmd.AddCommand(database.DBCmd) //Start command diff --git a/engine/update.go b/engine/update.go index 1d08cabe95..c98d86fa4c 100644 --- a/engine/update.go +++ b/engine/update.go @@ -20,7 +20,6 @@ var updateCmd = &cobra.Command{ Short: "Update engine binary", Example: "engine update --from-github", Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("CDS engine version:%s os:%s architecture:%s\n", sdk.VERSION, runtime.GOOS, runtime.GOARCH) if !updateFromGithub && updateURLAPI == "" { sdk.Exit(`You have to use "./engine update --from-github" or "./engine update --api http://intance/of/your/cds/api"`) @@ -29,10 +28,13 @@ var updateCmd = &cobra.Command{ var urlBinary string conf := cdsclient.Config{Host: updateURLAPI} client := cdsclient.New(conf) + + fmt.Printf("CDS engine version:%s os:%s architecture:%s\n", sdk.VERSION, runtime.GOOS, runtime.GOARCH) + if updateFromGithub { // no need to have apiEndpoint here var errGH error - urlBinary, errGH = client.DownloadURLFromGithub("engine", runtime.GOOS, runtime.GOARCH) + urlBinary, errGH = client.DownloadURLFromGithub(sdk.GetArtifactFilename("engine", runtime.GOOS, runtime.GOARCH)) if errGH != nil { sdk.Exit("Error while getting URL from Github url:%s err:%s\n", urlBinary, errGH) } diff --git a/engine/uptodate.go b/engine/uptodate.go new file mode 100644 index 0000000000..8f327b6757 --- /dev/null +++ b/engine/uptodate.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" + "strings" + + "github.com/spf13/cobra" + + "github.com/ovh/cds/sdk" + "github.com/ovh/cds/sdk/cdsclient" +) + +var uptodateCmd = &cobra.Command{ + Use: "uptodate", + Short: "check if engine is uptodate", + Long: `check if engine is uptodate with latest release on github (--from-github) or from an existing API. + +This command exit 0 if current binary is uptodate. +`, + Example: "engine uptodate --from-github", + Run: func(cmd *cobra.Command, args []string) { + if !updateFromGithub && updateURLAPI == "" { + sdk.Exit(`You have to use "./engine uptodate --from-github" or "./engine uptodate --api http://intance/of/your/cds/api"`) + } + + conf := cdsclient.Config{Host: updateURLAPI} + client := cdsclient.New(conf) + + var versionTxt string + if updateFromGithub { + urlVersionFile, errGH := client.DownloadURLFromGithub("VERSION") + if errGH != nil { + sdk.Exit("Error while getting URL from Github url:%s err:%s\n", urlVersionFile, errGH) + } + resp, errG := http.Get(urlVersionFile) + if errG != nil { + sdk.Exit("Error while getting binary from CDS API: %s\n", errG) + } + defer resp.Body.Close() + respB, errR := ioutil.ReadAll(resp.Body) + if errR != nil { + sdk.Exit("Error while reading VERSION file: %v\n", errR) + } + versionTxt = strings.TrimSpace(string(respB)) + } else { + remoteVersion, errv := client.MonVersion() + if errv != nil { + sdk.Exit("Error while getting version from GithubAPI err:%s\n", errv) + } + versionTxt = remoteVersion.Version + } + + if versionTxt == sdk.VERSION { + fmt.Println("uptodate:true") + os.Exit(0) + } + fmt.Println("uptodate:false") + os.Exit(1) + }, +} diff --git a/engine/worker/cmd_update.go b/engine/worker/cmd_update.go index 260357fa4a..7beeec2ecc 100644 --- a/engine/worker/cmd_update.go +++ b/engine/worker/cmd_update.go @@ -50,7 +50,7 @@ func updateCmd(w *currentWorker) func(cmd *cobra.Command, args []string) { w.client = cdsclient.NewWorker("", "download", nil) var errGH error - urlBinary, errGH = w.client.DownloadURLFromGithub("worker", runtime.GOOS, runtime.GOARCH) + urlBinary, errGH = w.client.DownloadURLFromGithub(sdk.GetArtifactFilename("worker", runtime.GOOS, runtime.GOARCH)) if errGH != nil { sdk.Exit("Error while getting URL from Github: %s", errGH) } diff --git a/sdk/cdsclient/client_download.go b/sdk/cdsclient/client_download.go index ae8cc13dc5..bf6498d1b1 100644 --- a/sdk/cdsclient/client_download.go +++ b/sdk/cdsclient/client_download.go @@ -22,7 +22,7 @@ func (c *client) DownloadURLFromAPI(name, os, arch string) string { return fmt.Sprintf("%s/download/%s/%s/%s", c.APIURL(), name, os, arch) } -func (c *client) DownloadURLFromGithub(name, os, arch string) (string, error) { +func (c *client) DownloadURLFromGithub(filename string) (string, error) { var httpClient = &http.Client{Timeout: 10 * time.Second} r, err := httpClient.Get("https://api.github.com/repos/ovh/cds/releases/latest") @@ -39,13 +39,13 @@ func (c *client) DownloadURLFromGithub(name, os, arch string) (string, error) { if len(release.Assets) > 0 { for _, asset := range release.Assets { - if *asset.Name == sdk.GetArtifactFilename(name, os, arch) { + if *asset.Name == filename { return *asset.BrowserDownloadURL, nil } } } - text := fmt.Sprintf("Invalid Artifacts on latest release (%s %s %s). Please try again in few minutes.\n", name, os, arch) + text := fmt.Sprintf("Invalid Artifacts on latest release (%s). Please try again in few minutes.\n", filename) text += fmt.Sprintf("If the problem persists, please open an issue on %s\n", sdk.URLGithubIssues) text += fmt.Sprintf("You can manually download binary from latest release: %s\n", sdk.URLGithubReleases) return "", fmt.Errorf(text) diff --git a/sdk/cdsclient/client_mon.go b/sdk/cdsclient/client_mon.go index b6b60e1f16..7a33e4c3ca 100644 --- a/sdk/cdsclient/client_mon.go +++ b/sdk/cdsclient/client_mon.go @@ -12,6 +12,14 @@ func (c *client) MonStatus() (*sdk.MonitoringStatus, error) { return &monStatus, nil } +func (c *client) MonVersion() (*sdk.Version, error) { + monVersion := sdk.Version{} + if _, err := c.GetJSON("/mon/version", &monVersion); err != nil { + return nil, err + } + return &monVersion, nil +} + func (c *client) MonDBMigrate() ([]sdk.MonDBMigrate, error) { monDBMigrate := []sdk.MonDBMigrate{} if _, err := c.GetJSON("/mon/db/migrate", &monDBMigrate); err != nil { diff --git a/sdk/cdsclient/interface.go b/sdk/cdsclient/interface.go index 29c877e4be..90e40263dc 100644 --- a/sdk/cdsclient/interface.go +++ b/sdk/cdsclient/interface.go @@ -111,7 +111,7 @@ type EnvironmentVariableClient interface { type DownloadClient interface { Download() ([]sdk.Download, error) DownloadURLFromAPI(name, os, arch string) string - DownloadURLFromGithub(name, os, arch string) (string, error) + DownloadURLFromGithub(filename string) (string, error) } // ActionClient exposes actions related functions @@ -256,6 +256,7 @@ type WorkflowClient interface { // MonitoringClient exposes monitoring functions type MonitoringClient interface { MonStatus() (*sdk.MonitoringStatus, error) + MonVersion() (*sdk.Version, error) MonDBTimes() (*sdk.MonDBTimes, error) MonDBMigrate() ([]sdk.MonDBMigrate, error) }