Skip to content

Commit

Permalink
feat (engine): engine isupdate (#2408)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault committed Mar 19, 2018
1 parent df10e98 commit 143133f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cli/cdsctl/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions engine/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`)
Expand All @@ -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)
}
Expand Down
63 changes: 63 additions & 0 deletions engine/uptodate.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
2 changes: 1 addition & 1 deletion engine/worker/cmd_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions sdk/cdsclient/client_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions sdk/cdsclient/client_mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 143133f

Please sign in to comment.