Skip to content

Commit

Permalink
updater: Address comments, improve docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
yolossn committed Sep 13, 2021
1 parent 2c18577 commit 026f221
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 388 deletions.
214 changes: 129 additions & 85 deletions updater/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Each update has four main parts involved:
The way omaha managed updates work is that omaha responds to update checks, and
relies on the information given by the application's instance to keep track of
each update's state. So the basic workflow for using this updater package is:
1. Check for an update, if there's one available then:
1. Check for an update, if there's one available then. if there is not, try again later.
2. Inform the server we're started it (this is done by sending a progress
report of "download started").
3. Actually perform the download or whatever represents fetching the
Expand All @@ -38,7 +38,7 @@ each update's state. So the basic workflow for using this updater package is:
5. Apply the update (this deeply depends on what each application does, but
may involve extracting files into locations, migrating configuration,
etc.).
6.Inform the server that the update installation is finished; run the new
6. Inform the server that the update installation is finished; run the new
version of the application and report that the update is now complete
(these are two different progress reports and may involve running).
Expand All @@ -50,11 +50,11 @@ version if now running).
Initialization:
An instance of the updater needs to be initialized with the needed details for
the updates in order for it to be used:
An instance of the updater needs to be initialized.
import (
"os"
"context"
"fmt"
"github.com/kinvolk/nebraska/updater"
)
Expand All @@ -64,89 +64,101 @@ the updates in order for it to be used:
return os.Getenv("MACHINE_NAME")
}
func getCurrentVersion() string {
func getAppVersion() string {
// Your own implementation here...
return os.Getenv("MACHINE_VERSION")
}
conf := updater.Config{
OmahaURL: "http://myupdateserver.io/v1/update",
AppID: "io.phony.App",
Channel: "stable",
InstanceID: getInstanceID(),
InstanceVersion: getCurrentVersion(),
}
u, err := updater.New(conf)
if err != nil{
fmt.Printf("error setting up updater.. %v\n", err)
os.Exit(1)
}
func main(){
conf := updater.Config{
OmahaURL: "http://test.omahaserver.com/v1/update/",
AppID: "application_id",
Channel: "stable",
InstanceID: getInstanceID(),
InstanceVersion: getAppVersion(),
}
appUpdater, err := updater.New(conf)
if err != nil {
fmt.Println("error setting up updater",err)
os.Exit(1)
}
Performing updates manually:
After we have the updater instance, we can try updating:
ctx := context.TODO()
info, err := u.CheckForUpdates(ctx)
if err != nil {
fmt.Printf("oops, something didn't go well... %v\n", err)
return
}
ctx := context.TODO()
if !info.HasUpdate {
fmt.Printf("no updates, try next time...")
return
}
newVersion := info.GetVersion()
// So we got an update, let's report we'll start downloading it.
u.ReportProgress(ctx, updater.ProgressDownloadStarted)
updateInfo, err := appUpdater.CheckForUpdates(ctx)
if err != nil {
fmt.Printf("oops, something didn't go well... %v\n", err)
return
}
// This should be your own implementation
download, err := someFunctionThatDownloadsAFile(info.GetURL())
if err != nil {
// Oops something went wrong
u.ReportProgress(ctx, updater.ProgressError)
return
}
if !updateInfo.HasUpdate() {
fmt.Printf("no updates, try next time...")
return
}
// The download was successful, let's inform of that
u.ReportProgress(ctx, updater.ProgressDownloadFinished)
// So we got an update, let's report we'll start downloading it.
if err := appUpdater.ReportProgress(ctx, updater.ProgressDownloadStarted); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting progress error:", progressErr)
}
fmt.Println("error reporting progress download started:",err)
return
}
// We got our update file, let's install it!
u.ReportProgress(ctx, updater.ProgressInstallationStarted)
// This should be implemented by the user.
// download, err := someFunctionThatDownloadsAFile(ctx, info.GetURL())
// if err != nil {
// // Oops something went wrong
// if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
// fmt.Println("reporting error:", progressErr)
// }
// return err
// }
// The download was successful, let's inform that to the omaha server
if err := appUpdater.ReportProgress(ctx, updater.ProgressDownloadFinished); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting progress error:", progressErr)
}
return err
}
// This should be your own implementation
err := someFunctionThatExtractsTheUpdateAndInstallIt(download)
if err != nil {
// Oops something went wrong
u.ReportProgress(ctx, updater.ProgressError)
return
}
// let's install the update!
if err := appUpdater.ReportProgress(ctx, updater.ProgressInstallationStarted); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting progress error:", progressErr)
}
return err
}
// We've just applied the update
u.ReportProgress(ctx, updater.ProgressInstallationFinished)
// This should be users own implementation
// err := someFunctionThatExtractsTheUpdateAndInstallIt(ctx, download)
// if err != nil {
// // Oops something went wrong
// if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
// fmt.Println("reporting error:", progressErr)
// }
// return err
// }
if err := appUpdater.CompleteUpdate(ctx, updateInfo); err != nil {
if progressErr := appUpdater.ReportError(ctx, nil); progressErr != nil {
fmt.Println("reporting progress error:", progressErr)
}
return err
}
err := someFunctionThatExitsAndRerunsTheApp()
if err != nil {
// Oops something went wrong
u.ReportProgress(ctx, updater.ProgressError)
return
return nil
}
u.SetInstanceVersion(newVersion)
// We're running the new version! Hurray!
u.ReportProgress(ctx, updater.ProgressUpdateComplete)
If instead of rerunning the application in the example above, we'd perform a
restart, then upon running the logic again and detecting that we're running a
new version, we could report that we did so:
u.ReportProgress(ctx, updater.ProgressUpdateCompleteAndRestarted)
Expand All @@ -161,41 +173,73 @@ progress reports automatically: TryUpdate
// After initializing our Updater instance...
type updateHandler struct {}
import (
"context"
"fmt"
func (e updateHandler) FetchUpdate(ctx context.Context, info *UpdateInfo) error {
download, err := someFunctionThatDownloadsAFile(info.GetURL())
if err != nil {
// Oops something went wrong
return err
}
"github.com/kinvolk/nebraska/updater"
)
func getInstanceID() string {
// Your own implementation here...
return os.Getenv("MACHINE_NAME")
}
func getAppVersion() string {
// Your own implementation here...
return os.Getenv("MACHINE_VERSION")
}
setDownloadFile(ctx)
type exampleUpdateHandler struct {
}
func (e exampleUpdateHandler) FetchUpdate(ctx context.Context, info updater.UpdateInfo) error {
// download, err := someFunctionThatDownloadsAFile(ctx, info.GetURL())
// if err != nil {
// return err
// }
return nil
}
func (e updateHandler) ApplyUpdate(ctx context.Context, info *UpdateInfo) error {
err := someFunctionThatExtractsTheUpdateAndInstallIt(getDownloadFile(ctx))
func (e exampleUpdateHandler) ApplyUpdate(ctx context.Context, info updater.UpdateInfo) error {
// err := someFunctionThatExtractsTheUpdateAndInstallIt(ctx, getDownloadFile(ctx))
// if err != nil {
// // Oops something went wrong
// return err
// }
// err := someFunctionThatExitsAndRerunsTheApp(ctx)
// if err != nil {
// // Oops something went wrong
// return err
// }
return nil
}
func main() {
conf := updater.Config{
OmahaURL: "http://test.omahaserver.com/v1/update/",
AppID: "application_id",
Channel: "stable",
InstanceID: getInstanceID(),
InstanceVersion: getAppVersion(),
}
appUpdater, err := updater.New(conf)
if err != nil {
// Oops something went wrong
return err
}
err := someFunctionThatExitsAndRerunsTheApp()
if err != nil {
// Oops something went wrong
ctx := context.TODO()
if err := appUpdater.TryUpdate(ctx, exampleUpdateHandler{}); err != nil {
return err
}
return nil
}
err := u.TryUpdate(ctx, updateHandler{})
if err != nil {
// Oops something went wrong
}
// If the update succeeded, then u.GetInstanceVersion() should be set to the new version
*/
Expand Down
1 change: 0 additions & 1 deletion updater/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.16

require (
github.com/google/uuid v1.3.0
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/kinvolk/go-omaha v0.0.1
github.com/kinvolk/nebraska/backend v0.0.0-20210826093658-9a22e1e98f4b
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 0 additions & 5 deletions updater/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,10 @@ github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoP
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down
10 changes: 0 additions & 10 deletions updater/handler.go

This file was deleted.

58 changes: 0 additions & 58 deletions updater/omahaHandler.go

This file was deleted.

Loading

0 comments on commit 026f221

Please sign in to comment.