From 4881376bd4482df9e4c429201b936aad38d0bf04 Mon Sep 17 00:00:00 2001 From: gferraro Date: Thu, 26 Sep 2024 17:44:22 +0200 Subject: [PATCH] update to get latest commit from salt-version-info --- cmd/salt-helper/main.go | 96 +---------------------------------------- saltrequester.go | 68 +++++++++++------------------ 2 files changed, 28 insertions(+), 136 deletions(-) diff --git a/cmd/salt-helper/main.go b/cmd/salt-helper/main.go index 3d055c5..56cc707 100644 --- a/cmd/salt-helper/main.go +++ b/cmd/salt-helper/main.go @@ -20,13 +20,9 @@ package main import ( "bufio" - "encoding/json" "errors" "fmt" "io" - "net" - "net/http" - "net/url" "os" "os/exec" @@ -258,13 +254,13 @@ func runMain() error { log.Printf("Last update was run at '%s', with nodegroup '%s'", state.LastUpdate.Format("2006-01-02 15:04:05"), nodegroup) // Log when the latest software was released. - latestUpdateTime, err := GetLatestUpdateTime(nodegroup) + _, latestUpdateTime, err := saltrequester.UpdateExistsForNodeGroup(nodegroup) if err != nil { log.Errorf("Error getting latest update time: %v", err) return err } log.Printf("Latest software update was published at '%s', for nodegroup '%s'", latestUpdateTime.Format("2006-01-02 15:04:05"), nodegroup) - if state.LastUpdate.Before(*latestUpdateTime) { + if state.LastUpdate.Before(latestUpdateTime) { log.Info("Found new update, recommend a salt update.") return nil } else { @@ -466,94 +462,6 @@ func trackUpdateProgress(s *saltUpdater, stop chan bool) { } } -const saltrepoURL = "https://api.github.com/repos/TheCacophonyProject/saltops/commits" - -var nodeGroupToBranch = map[string]string{ - "tc2-dev": "dev", - "tc2-test": "test", - "tc2-prod": "prod", - "dev-pis": "dev", - "test-pis": "test", - "prod-pis": "prod", -} - -func GetLatestUpdateTime(nodegroup string) (*time.Time, error) { - // Check what branch the is used for this nodegroup - branch, ok := nodeGroupToBranch[nodegroup] - if !ok { - err := fmt.Errorf("cant find a salt branch mapping for %v nodegroup", nodegroup) - log.Errorf(err.Error()) - return nil, err - } - - // Make request to salt repo - u, err := url.Parse(saltrepoURL) - if err != nil { - log.Errorf("Failed to parse salt repo URL: %v", err) - return nil, err - } - params := url.Values{} - params.Add("sha", branch) - params.Add("per_page", "1") - u.RawQuery = params.Encode() - req, _ := http.NewRequest("GET", u.String(), nil) - req.Header.Set("Accept", "application/vnd.github+json") - req.Header.Set("X-GitHub-Api-Version", "2022-11-28") - client := &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: (&net.Dialer{ - KeepAlive: 30 * time.Second, - DualStack: true, - }).DialContext, - ExpectContinueTimeout: 1 * time.Second, - MaxIdleConns: 5, - IdleConnTimeout: 90 * time.Second, - }, - } - - // Send request - resp, err := client.Do(req) - if err != nil { - log.Errorf("Failed to send request: %v", err) - return nil, err - } - defer resp.Body.Close() - - // Check response - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - err := fmt.Errorf("bad update status check %v from url %v", resp.StatusCode, u.String()) - log.Errorf(err.Error()) - return nil, err - } - - // Parse response - body, err := io.ReadAll(resp.Body) - if err != nil { - log.Errorf("Failed to read body: %v", err) - return nil, err - } - var details []interface{} - err = json.Unmarshal(body, &details) - if err != nil { - log.Errorf("Failed to unmarshal body: %v", err) - return nil, err - } - if len(details) == 0 { - log.Printf("No updates exists for %v node group", nodegroup) - return nil, nil - } - commitDate := details[0].(map[string]interface{})["commit"].(map[string]interface{})["author"].(map[string]interface{})["date"].(string) - layout := "2006-01-02T15:04:05Z" - updateTime, err := time.Parse(layout, commitDate) - if err != nil { - log.Errorf("Failed to parse commit date: %v", err) - return nil, err - } - - return &updateTime, nil -} - func (s *saltUpdater) CheckIfUpdateAvailable() bool { _, _, err := saltrequester.UpdateExists() return err == nil diff --git a/saltrequester.go b/saltrequester.go index 934f103..9954417 100644 --- a/saltrequester.go +++ b/saltrequester.go @@ -5,9 +5,7 @@ import ( "errors" "fmt" "io" - "net" "net/http" - "net/url" "os" "strings" @@ -187,76 +185,62 @@ func ReadStateFile() (*SaltState, error) { return saltState, err } -// UpdateExists checks if there has been any git updates since the last update time for this minions nodegroup -// uses github api to view last commit to the repo func UpdateExists() (bool, time.Time, error) { - nodegroupOut, err := os.ReadFile("/etc/cacophony/salt-nodegroup") if err != nil { return false, time.Time{}, err } - nodeGroup := string(nodegroupOut) + return UpdateExistsForNodeGroup(string(nodegroupOut)) +} + +// UpdateExists checks if there has been any git updates since the last update time for this minions nodegroup +// uses github api to view last commit to the repo +func UpdateExistsForNodeGroup(nodeGroup string) (bool, time.Time, error) { + nodeGroup = strings.TrimSuffix(nodeGroup, "\n") branch, ok := nodeGroupToBranch[nodeGroup] var updateTime time.Time if !ok { - return false, updateTime, fmt.Errorf("cant find a salt branch mapping for %v nodegroup", nodegroupOut) + return false, updateTime, fmt.Errorf("cant find a salt branch mapping for %v nodegroup", nodeGroup) } saltState, _ := ReadStateFile() log.Printf("Checking for updates for saltops %v branch, last update was %v", branch, saltState.LastUpdate) + resp, err := http.Get("https://raw.githubusercontent.com/TheCacophonyProject/salt-version-info/refs/heads/main/salt-version-info.json") - const saltrepoURL = "https://api.github.com/repos/TheCacophonyProject/saltops/commits" - u, err := url.Parse(saltrepoURL) - if err != nil { - return false, updateTime, err - } - params := url.Values{} - params.Add("sha", branch) - params.Add("per_page", "1") - - u.RawQuery = params.Encode() - - req, _ := http.NewRequest("GET", u.String(), nil) - req.Header.Set("Accept", "application/vnd.github+json") - req.Header.Set("X-GitHub-Api-Version", "2022-11-28") - - client := &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DialContext: (&net.Dialer{ - KeepAlive: 30 * time.Second, - DualStack: true, - }).DialContext, - ExpectContinueTimeout: 1 * time.Second, - MaxIdleConns: 5, - IdleConnTimeout: 90 * time.Second, - }, - } - - resp, err := client.Do(req) if err != nil { return false, updateTime, err } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { - return false, updateTime, fmt.Errorf("bad update status check %v from url %v", resp.StatusCode, u.String()) + return false, updateTime, fmt.Errorf("bad update status check %v from url %v", resp.StatusCode, "https://raw.githubusercontent.com/TheCacophonyProject/salt-version-info/refs/heads/main/salt-version-info.json") } body, err := io.ReadAll(resp.Body) if err != nil { return false, updateTime, err } - var details []interface{} + var details map[string]interface{} err = json.Unmarshal(body, &details) if err != nil { return false, updateTime, err } - if len(details) == 0 { - log.Printf("No updates exists for %v node group", nodegroupOut) - return false, updateTime, nil + + var commitDate string + if branchDetails, ok := details[branch]; ok { + if tc2, ok := branchDetails.(map[string]interface{})["tc2"]; ok { + if commitDate, ok = tc2.(map[string]interface{})["commitDate"].(string); !ok { + err = fmt.Errorf("Could not find commitDate key in json %v", commitDate) + } + } else { + err = fmt.Errorf("Could not find tc2 key in json %v", branchDetails) + } + } else { + err = fmt.Errorf("Could not find %v key in json %v", branch, details) + } + if err != nil { + return false, updateTime, err } - commitDate := details[0].(map[string]interface{})["commit"].(map[string]interface{})["author"].(map[string]interface{})["date"].(string) layout := "2006-01-02T15:04:05Z" updateTime, err = time.Parse(layout, commitDate) if err != nil {