Skip to content

Commit 04c43bc

Browse files
yesnaultfsamin
authored andcommitted
feat (engine): engine update [--from-github|--api] (#1829)
1 parent df5a79d commit 04c43bc

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

engine/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func init() {
4848
startCmd.Flags().StringVar(&vaultToken, "vault-token", "", "(optional) Vault token to fetch secrets from vault")
4949
//Version command
5050
mainCmd.AddCommand(versionCmd)
51+
//Update command
52+
mainCmd.AddCommand(updateCmd)
53+
updateCmd.Flags().BoolVar(&updateFromGithub, "from-github", false, "Update binary from latest github release")
54+
updateCmd.Flags().StringVar(&updateURLAPI, "api", "", "Update binary from a CDS Engine API")
55+
5156
//Database command
5257
mainCmd.AddCommand(database.DBCmd)
5358
//Start command

engine/update.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"runtime"
7+
8+
"github.com/inconshreveable/go-update"
9+
"github.com/spf13/cobra"
10+
11+
"github.com/ovh/cds/sdk"
12+
"github.com/ovh/cds/sdk/cdsclient"
13+
)
14+
15+
var updateFromGithub bool
16+
var updateURLAPI string
17+
18+
var updateCmd = &cobra.Command{
19+
Use: "update",
20+
Short: "Update engine binary",
21+
Run: func(cmd *cobra.Command, args []string) {
22+
fmt.Printf("CDS engine version:%s os:%s architecture:%s\n", sdk.VERSION, runtime.GOOS, runtime.GOARCH)
23+
24+
if !updateFromGithub && updateURLAPI == "" {
25+
sdk.Exit(`You have to use "./engine update --from-github" or "./engine update --api http://intance/of/your/cds/api"`)
26+
}
27+
28+
var urlBinary string
29+
conf := cdsclient.Config{Host: updateURLAPI}
30+
client := cdsclient.New(conf)
31+
if updateFromGithub {
32+
// no need to have apiEndpoint here
33+
var errGH error
34+
urlBinary, errGH = client.DownloadURLFromGithub("engine", runtime.GOOS, runtime.GOARCH)
35+
if errGH != nil {
36+
sdk.Exit("Error while getting URL from Github url:%s err:%s\n", urlBinary, errGH)
37+
}
38+
fmt.Printf("Updating binary from Github on %s...\n", urlBinary)
39+
} else {
40+
urlBinary = client.DownloadURLFromAPI("engine", runtime.GOOS, runtime.GOARCH)
41+
fmt.Printf("Updating binary from CDS API on %s...\n", urlBinary)
42+
}
43+
44+
resp, err := http.Get(urlBinary)
45+
if err != nil {
46+
sdk.Exit("Error while getting binary from CDS API: %s\n", err)
47+
}
48+
defer resp.Body.Close()
49+
50+
if err := sdk.CheckContentTypeBinary(resp); err != nil {
51+
sdk.Exit(err.Error())
52+
}
53+
54+
if resp.StatusCode != 200 {
55+
sdk.Exit("Error http code: %d, url called: %s\n", resp.StatusCode, urlBinary)
56+
}
57+
58+
if err := update.Apply(resp.Body, update.Options{}); err != nil {
59+
sdk.Exit("Error while updating binary from CDS API: %s\n", err)
60+
}
61+
fmt.Println("Update engine done.")
62+
},
63+
}

0 commit comments

Comments
 (0)