|
| 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