Skip to content

Commit

Permalink
update to get latest commit from salt-version-info
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Sep 26, 2024
1 parent d2f24af commit 4881376
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 136 deletions.
96 changes: 2 additions & 94 deletions cmd/salt-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ package main

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"

"os"
"os/exec"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
68 changes: 26 additions & 42 deletions saltrequester.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"

Expand Down Expand Up @@ -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)

Check failure on line 233 in saltrequester.go

View workflow job for this annotation

GitHub Actions / build / build

error strings should not be capitalized (ST1005)
}
} else {
err = fmt.Errorf("Could not find tc2 key in json %v", branchDetails)

Check failure on line 236 in saltrequester.go

View workflow job for this annotation

GitHub Actions / build / build

error strings should not be capitalized (ST1005)
}
} else {
err = fmt.Errorf("Could not find %v key in json %v", branch, details)

Check failure on line 239 in saltrequester.go

View workflow job for this annotation

GitHub Actions / build / build

error strings should not be capitalized (ST1005)
}
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 {
Expand Down

0 comments on commit 4881376

Please sign in to comment.