From bb094ee316d0253cc21ddcf9b3d2035834667526 Mon Sep 17 00:00:00 2001 From: Joaquim Rocha Date: Wed, 22 Sep 2021 14:26:00 +0100 Subject: [PATCH] Remove updaters legacy code We have not being maintaining this code and it's not used, so let's removed it. --- Makefile | 2 +- updaters/examples/demo/demo.go | 116 ------------------------ updaters/lib/go/helpers.go | 161 --------------------------------- 3 files changed, 1 insertion(+), 278 deletions(-) delete mode 100644 updaters/examples/demo/demo.go delete mode 100644 updaters/lib/go/helpers.go diff --git a/Makefile b/Makefile index 94324f23f..5cd3ec03f 100644 --- a/Makefile +++ b/Makefile @@ -98,7 +98,7 @@ backend-binary: run-generators build-backend-binary .PHONY: test-clean-work-tree-backend test-clean-work-tree-backend: @cd backend && \ - if ! git diff --quiet -- go.mod go.sum pkg cmd updaters tools/tools.go; then \ + if ! git diff --quiet -- go.mod go.sum pkg cmd tools/tools.go; then \ echo; \ echo 'Working tree of backend code is not clean'; \ echo; \ diff --git a/updaters/examples/demo/demo.go b/updaters/examples/demo/demo.go deleted file mode 100644 index 1321cd149..000000000 --- a/updaters/examples/demo/demo.go +++ /dev/null @@ -1,116 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io" - "log" - "net/http" - "os" - "syscall" - "time" - - "github.com/facebookgo/grace/gracehttp" - cr "github.com/kinvolk/nebraska/backend/updaters/lib/go" -) - -const ( - checkFrequency = 30 * time.Second -) - -var ( - serverAddr string - instanceID string - appID = "780d6940-9a48-4414-88df-95ba63bbe9cb" - groupID = "51a32aa9-3552-49fc-a28c-6543bccf0069" - currentVersion = "1.0.0" -) - -func init() { - flag.StringVar(&instanceID, "instance-id", "demo-instance-id", "Instance ID") -} - -func main() { - flag.Parse() - - go checkForUpdates() - - gracehttp.Serve( - &http.Server{Addr: ":8111", Handler: newHandler("handler1")}, - &http.Server{Addr: ":8112", Handler: newHandler("handler2")}, - &http.Server{Addr: ":8113", Handler: newHandler("handler3")}, - ) -} - -func checkForUpdates() { - checkTicker := time.Tick(checkFrequency) - - for range checkTicker { - log.Println("Checking for updates..") - update, err := cr.GetUpdate(instanceID, appID, groupID, currentVersion) - if err != nil { - log.Printf("\t- No updates (error: %v)\n", err) - continue - } - log.Println("\t- Updates available!") - - log.Println("\t- Downloading update package..") - cr.EventDownloadStarted(instanceID, appID, groupID) - downloadPackage(update.URL, update.Filename) - - log.Println("\t- Update package downloaded") - cr.EventDownloadFinished(instanceID, appID, groupID) - - log.Println("\t- Update completed successfully :)") - cr.EventUpdateSucceeded(instanceID, appID, groupID) - - log.Println("\t- Restarting server using new package") - syscall.Kill(syscall.Getpid(), syscall.SIGUSR2) - - break - } -} - -func downloadPackage(url, filename string) { - time.Sleep(3 * time.Second) - - output, err := os.Create("demo") - if err != nil { - return - } - defer output.Close() - - response, err := http.Get(url + filename) - if err != nil { - return - } - defer response.Body.Close() - - if _, err := io.Copy(output, response.Body); err != nil { - return - } -} - -// From: https://github.com/facebookgo/grace/blob/master/gracedemo/demo.go -func newHandler(name string) http.Handler { - mux := http.NewServeMux() - mux.HandleFunc("/sleep/", func(w http.ResponseWriter, r *http.Request) { - duration, err := time.ParseDuration(r.FormValue("duration")) - if err != nil { - http.Error(w, err.Error(), 400) - return - } - time.Sleep(duration) - fmt.Fprintf( - w, - "(SERVER VERSION: %s) started at %s slept for %f seconds from pid %d.\n", - currentVersion, - time.Now(), - duration.Seconds(), - os.Getpid(), - ) - }) - return mux -} - -// http://localhost:8111/sleep/?duration=5s diff --git a/updaters/lib/go/helpers.go b/updaters/lib/go/helpers.go deleted file mode 100644 index f4d95dc7e..000000000 --- a/updaters/lib/go/helpers.go +++ /dev/null @@ -1,161 +0,0 @@ -package helpers - -import ( - "bytes" - "encoding/xml" - "errors" - "io/ioutil" - "net/http" - "os" - - "github.com/kinvolk/go-omaha/omaha" -) - -// Update represents some information about an update received from CR. -type Update struct { - Version string - URL string - Filename string - Hash string -} - -const ( - defaultOmahaURL = "http://localhost:8000/omaha/" -) - -var ( - // ErrInvalidOmahaResponse error indicates that the omaha response received - // from CR was invalid. - ErrInvalidOmahaResponse = errors.New("invalid omaha response") - - // ErrNoUpdate error indicates that there wasn't any update available for - // instance requesting it in the context of the appID/groupID provided. - ErrNoUpdate = errors.New("no update available") -) - -// GetUpdate asks CR for an update for the given instance in the context of the -// application and group provided. -func GetUpdate(instanceID, appID, groupID, version string) (*Update, error) { - req := buildOmahaUpdateRequest(instanceID, appID, groupID, version) - resp, err := doOmahaRequest(req) - if err != nil { - return nil, err - } - - if resp == nil || len(resp.Apps) != 1 { - return nil, ErrInvalidOmahaResponse - } - - app := resp.Apps[0] - if app.Status != omaha.AppOK { - return nil, errors.New(app.Status.Error()) - } - - status := app.UpdateCheck.Status - switch status { - case omaha.UpdateOK: - update := &Update{ - Version: app.UpdateCheck.Manifest.Version, - URL: app.UpdateCheck.URLs[0].CodeBase, - Filename: app.UpdateCheck.Manifest.Packages[0].Name, - Hash: app.UpdateCheck.Manifest.Packages[0].SHA1, - } - return update, nil - case omaha.NoUpdate: - return nil, ErrNoUpdate - default: - return nil, errors.New(status.Error()) - } -} - -// EventDownloadStarted posts an event to CR to indicate that the download of -// the update has started. -func EventDownloadStarted(instanceID, appID, groupID string) error { - req := buildOmahaEventRequest(instanceID, appID, groupID, omaha.EventTypeDownloadStarted, omaha.EventResultSuccess) - _, err := doOmahaRequest(req) - - return err -} - -// EventDownloadFinished posts an event to CR to indicate that the download of -// the update has finished. -func EventDownloadFinished(instanceID, appID, groupID string) error { - req := buildOmahaEventRequest(instanceID, appID, groupID, omaha.EventTypeUpdateDownloadFinished, omaha.EventResultSuccess) - _, err := doOmahaRequest(req) - - return err -} - -// EventUpdateSucceeded posts an event to CR to indicate that the update was -// installed successfully and the new version is working fine. -func EventUpdateSucceeded(instanceID, appID, groupID string) error { - req := buildOmahaEventRequest(instanceID, appID, groupID, omaha.EventTypeUpdateComplete, omaha.EventResultSuccessReboot) - _, err := doOmahaRequest(req) - - return err -} - -// EventUpdateFailed posts an event to CR to indicate that the update process -// complete but it didn't succeed. -func EventUpdateFailed(instanceID, appID, groupID string) error { - req := buildOmahaEventRequest(instanceID, appID, groupID, omaha.EventTypeUpdateComplete, omaha.EventResultError) - _, err := doOmahaRequest(req) - - return err -} - -func buildOmahaUpdateRequest(instanceID, appID, groupID, version string) *omaha.Request { - req := &omaha.Request{} - app := req.AddApp(appID, version) - app.MachineID = instanceID - app.BootID = instanceID - app.Track = groupID - app.AddUpdateCheck() - - return req -} - -func buildOmahaEventRequest(instanceID, appID, groupID string, eventType omaha.EventType, eventResult omaha.EventResult) *omaha.Request { - req := &omaha.Request{} - app := req.AddApp(appID, "") - app.MachineID = instanceID - app.BootID = instanceID - app.Track = groupID - event := app.AddEvent() - event.Type = eventType - event.Result = eventResult - - return req -} - -func doOmahaRequest(req *omaha.Request) (*omaha.Response, error) { - omahaURL := os.Getenv("CR_OMAHA_URL") - if omahaURL == "" { - omahaURL = defaultOmahaURL - } - - httpClient := &http.Client{} - - payload, err := xml.Marshal(req) - if err != nil { - return nil, err - } - - resp, err := httpClient.Post(omahaURL, "text/xml", bytes.NewReader(payload)) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - oresp := &omaha.Response{} - if err = xml.Unmarshal(body, oresp); err != nil { - return nil, err - } - - return oresp, nil -}