mirror of
https://github.com/ddworken/hishtory.git
synced 2025-06-22 13:01:33 +02:00
Improved update flow
Using the previously added new API endpoint, the update flow can now skip updates if the latest version is already installed. This also improves the output by making it so update can print the version. Also improved the error handling.
This commit is contained in:
parent
735a98a611
commit
158f08f5c6
@ -461,6 +461,7 @@ func TestUpdate(t *testing.T) {
|
|||||||
|
|
||||||
// Update
|
// Update
|
||||||
RunInteractiveBashCommands(t, `hishtory update`)
|
RunInteractiveBashCommands(t, `hishtory update`)
|
||||||
|
// TODO: assert on the output of ^
|
||||||
|
|
||||||
// Then check the status command again to confirm the update worked
|
// Then check the status command again to confirm the update worked
|
||||||
out = RunInteractiveBashCommands(t, `hishtory status`)
|
out = RunInteractiveBashCommands(t, `hishtory status`)
|
||||||
|
@ -40,6 +40,8 @@ var ConfigShContents string
|
|||||||
//go:embed test_config.sh
|
//go:embed test_config.sh
|
||||||
var TestConfigShContents string
|
var TestConfigShContents string
|
||||||
|
|
||||||
|
var Version string = "Unknown"
|
||||||
|
|
||||||
func getCwd() (string, error) {
|
func getCwd() (string, error) {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -421,17 +423,25 @@ func copyFile(src, dst string) error {
|
|||||||
|
|
||||||
func Update() error {
|
func Update() error {
|
||||||
// Download the binary
|
// Download the binary
|
||||||
var stdout bytes.Buffer
|
respBody, err := ApiGet("/api/v1/download")
|
||||||
var stderr bytes.Buffer
|
|
||||||
cmd := exec.Command("bash", "-c", `
|
|
||||||
curl -L -o /tmp/hishtory-client.intoto.jsonl https://api.hishtory.dev/download/hishtory-linux-amd64.intoto.jsonl;
|
|
||||||
curl -L -o /tmp/hishtory-client https://api.hishtory.dev/download/hishtory-linux-amd64;
|
|
||||||
chmod +x /tmp/hishtory-client`)
|
|
||||||
cmd.Stdout = &stdout
|
|
||||||
cmd.Stderr = &stderr
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download update: %v, stdout=%#v, stderr=%#v", err, stdout.String(), stderr.String())
|
return fmt.Errorf("failed to download update info: %v", err)
|
||||||
|
}
|
||||||
|
var downloadData shared.UpdateInfo
|
||||||
|
err = json.Unmarshal(respBody, &downloadData)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse update info: %v", err)
|
||||||
|
}
|
||||||
|
if downloadData.Version == Version {
|
||||||
|
fmt.Printf("Latest version (v0.%s) is already installed\n", Version)
|
||||||
|
}
|
||||||
|
err = downloadFile("/tmp/hishtory-client", downloadData.LinuxAmd64Url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = downloadFile("/tmp/hishtory-client.intoto.jsonl", downloadData.LinuxAmd64AttestationUrl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the SLSA attestation
|
// Verify the SLSA attestation
|
||||||
@ -440,7 +450,7 @@ func Update() error {
|
|||||||
return fmt.Errorf("failed to verify SLSA provenance of the updated binary, aborting update: %v", err)
|
return fmt.Errorf("failed to verify SLSA provenance of the updated binary, aborting update: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlink the existing binary
|
// Unlink the existing binary so we can overwrite it even though it is still running
|
||||||
homedir, err := os.UserHomeDir()
|
homedir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get user's home directory: %v", err)
|
return fmt.Errorf("failed to get user's home directory: %v", err)
|
||||||
@ -449,16 +459,39 @@ func Update() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to unlink %s for update: %v", path.Join(homedir, shared.HISHTORY_PATH, "hishtory"), err)
|
return fmt.Errorf("failed to unlink %s for update: %v", path.Join(homedir, shared.HISHTORY_PATH, "hishtory"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Install the new one
|
// Install the new one
|
||||||
|
cmd := exec.Command("chmod", "+x", "/tmp/hishtory-client")
|
||||||
|
err = cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to chmod +x the update: %v", err)
|
||||||
|
}
|
||||||
cmd = exec.Command("/tmp/hishtory-client", "install")
|
cmd = exec.Command("/tmp/hishtory-client", "install")
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to update: %v", err)
|
return fmt.Errorf("failed to update: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println("Successfully updated hishtory!")
|
fmt.Printf("Successfully updated hishtory from v0.%s to %s\n", Version, downloadData.Version)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func downloadFile(filename, url string) error {
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to download file at %s to %s: %v", url, filename, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
out, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to save file to %s: %v", filename, err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func getServerHostname() string {
|
func getServerHostname() string {
|
||||||
if server := os.Getenv("HISHTORY_SERVER"); server != "" {
|
if server := os.Getenv("HISHTORY_SERVER"); server != "" {
|
||||||
return server
|
return server
|
||||||
|
@ -14,10 +14,7 @@ import (
|
|||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var GitCommit string = "Unknown"
|
||||||
GitCommit string = "Unknown"
|
|
||||||
Version string = "Unknown"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) == 1 {
|
if len(os.Args) == 1 {
|
||||||
@ -44,7 +41,7 @@ func main() {
|
|||||||
case "status":
|
case "status":
|
||||||
config, err := lib.GetConfig()
|
config, err := lib.GetConfig()
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
fmt.Printf("Hishtory: v0.%s\nEnabled: %v\n", Version, config.IsEnabled)
|
fmt.Printf("Hishtory: v0.%s\nEnabled: %v\n", lib.Version, config.IsEnabled)
|
||||||
fmt.Printf("Secret Key: %s\n", config.UserSecret)
|
fmt.Printf("Secret Key: %s\n", config.UserSecret)
|
||||||
if len(os.Args) == 3 && os.Args[2] == "-v" {
|
if len(os.Args) == 3 && os.Args[2] == "-v" {
|
||||||
fmt.Printf("User ID: %s\n", data.UserId(config.UserSecret))
|
fmt.Printf("User ID: %s\n", data.UserId(config.UserSecret))
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
GIT_HASH=$(git rev-parse HEAD)
|
GIT_HASH=$(git rev-parse HEAD)
|
||||||
echo "-X main.GitCommit=$GIT_HASH -X main.Version=`cat VERSION` -w -extldflags \"-static\""
|
echo "-X main.GitCommit=$GIT_HASH -X client.lib.Version=`cat VERSION` -w -extldflags \"-static\""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user