Skip to content

Commit

Permalink
feat (engine): engine update [--from-github|--api] (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and fsamin committed Dec 29, 2017
1 parent df5a79d commit 04c43bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions engine/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func init() {
startCmd.Flags().StringVar(&vaultToken, "vault-token", "", "(optional) Vault token to fetch secrets from vault")
//Version command
mainCmd.AddCommand(versionCmd)
//Update command
mainCmd.AddCommand(updateCmd)
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")

//Database command
mainCmd.AddCommand(database.DBCmd)
//Start command
Expand Down
63 changes: 63 additions & 0 deletions engine/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"net/http"
"runtime"

"github.com/inconshreveable/go-update"
"github.com/spf13/cobra"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/cdsclient"
)

var updateFromGithub bool
var updateURLAPI string

var updateCmd = &cobra.Command{
Use: "update",
Short: "Update engine binary",
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"`)
}

var urlBinary string
conf := cdsclient.Config{Host: updateURLAPI}
client := cdsclient.New(conf)
if updateFromGithub {
// no need to have apiEndpoint here
var errGH error
urlBinary, errGH = client.DownloadURLFromGithub("engine", runtime.GOOS, runtime.GOARCH)
if errGH != nil {
sdk.Exit("Error while getting URL from Github url:%s err:%s\n", urlBinary, errGH)
}
fmt.Printf("Updating binary from Github on %s...\n", urlBinary)
} else {
urlBinary = client.DownloadURLFromAPI("engine", runtime.GOOS, runtime.GOARCH)
fmt.Printf("Updating binary from CDS API on %s...\n", urlBinary)
}

resp, err := http.Get(urlBinary)
if err != nil {
sdk.Exit("Error while getting binary from CDS API: %s\n", err)
}
defer resp.Body.Close()

if err := sdk.CheckContentTypeBinary(resp); err != nil {
sdk.Exit(err.Error())
}

if resp.StatusCode != 200 {
sdk.Exit("Error http code: %d, url called: %s\n", resp.StatusCode, urlBinary)
}

if err := update.Apply(resp.Body, update.Options{}); err != nil {
sdk.Exit("Error while updating binary from CDS API: %s\n", err)
}
fmt.Println("Update engine done.")
},
}

0 comments on commit 04c43bc

Please sign in to comment.