@@ -15,39 +15,54 @@ import (
1515 "github.com/tschaefer/finchctl/internal/target"
1616)
1717
18- func (a * Agent ) __updateServiceBinaryIsNeeded () (bool , error ) {
19- if a .dryRun {
20- target .PrintProgress ("Skipping update check due to dry-run mode" , a .format )
21- return false , nil
22- }
23-
18+ func (a * Agent ) __updateServiceBinaryGetLatestTag () (string , error ) {
2419 url := "https://api.github.com/repos/grafana/alloy/releases/latest"
2520 a .__helperPrintProgress (fmt .Sprintf ("Running 'GET %s'" , url ))
2621 resp , err := http .Get (url )
2722 if err != nil {
28- return false , & UpdateAgentError {Message : err .Error (), Reason : "" }
23+ return "" , & UpdateAgentError {Message : err .Error (), Reason : "" }
2924 }
3025 defer func () {
3126 _ = resp .Body .Close ()
3227 }()
3328
3429 if resp .StatusCode < 200 || resp .StatusCode >= 300 {
35- return false , & UpdateAgentError {Message : "failed to fetch latest release info" , Reason : fmt .Sprintf ("status code: %d" , resp .StatusCode )}
30+ return "" , & UpdateAgentError {Message : "failed to fetch latest release info" , Reason : fmt .Sprintf ("status code: %d" , resp .StatusCode )}
3631 }
3732
3833 out , err := io .ReadAll (resp .Body )
3934 if err != nil {
40- return false , & UpdateAgentError {Message : err .Error (), Reason : "" }
35+ return "" , & UpdateAgentError {Message : err .Error (), Reason : "" }
4136 }
4237
4338 a .__helperPrintProgress ("Running 'JSON unmarshal \" tag_name\" '" )
4439 var data any
4540 if err := json .Unmarshal (out , & data ); err != nil {
46- return false , & UpdateAgentError {Message : err .Error (), Reason : string (out )}
41+ return "" , & UpdateAgentError {Message : err .Error (), Reason : string (out )}
42+ }
43+
44+ return data .(map [string ]any )["tag_name" ].(string ), nil
45+ }
46+
47+ func (a * Agent ) __updateServiceBinaryIsNeeded (version string ) (bool , error ) {
48+ if a .dryRun {
49+ target .PrintProgress (
50+ fmt .Sprintf ("Skipping Alloy update check for version '%s' due to dry-run mode" , version ),
51+ a .format ,
52+ )
53+ return false , nil
54+ }
55+
56+ latestVersion := version
57+ if version == "latest" {
58+ var err error
59+ latestVersion , err = a .__updateServiceBinaryGetLatestTag ()
60+ if err != nil {
61+ return false , err
62+ }
4763 }
48- latestVersion := data .(map [string ]any )["tag_name" ].(string )
4964
50- out , err = a .target .Run ("alloy --version | grep -o -E 'v[0-9\\ .]+'" )
65+ out , err : = a .target .Run ("alloy --version | grep -o -E 'v[0-9\\ .]+'" )
5166 if err != nil {
5267 return false , & UpdateAgentError {Message : err .Error (), Reason : string (out )}
5368 }
@@ -56,8 +71,8 @@ func (a *Agent) __updateServiceBinaryIsNeeded() (bool, error) {
5671 return latestVersion != currentVersion , nil
5772}
5873
59- func (a * Agent ) __updateServiceBinary (machine * MachineInfo ) error {
60- ok , err := a .__updateServiceBinaryIsNeeded ()
74+ func (a * Agent ) __updateServiceBinary (machine * MachineInfo , version string ) error {
75+ ok , err := a .__updateServiceBinaryIsNeeded (version )
6176 if err != nil {
6277 return err
6378 }
@@ -75,7 +90,7 @@ func (a *Agent) __updateServiceBinary(machine *MachineInfo) error {
7590 }()
7691
7792 release := fmt .Sprintf ("alloy-%s-%s" , machine .Kernel , machine .Arch )
78- zip , err := a .__deployDownloadRelease (release , tmpdir )
93+ zip , err := a .__deployDownloadRelease (release , version , tmpdir )
7994 if err != nil {
8095 return convertError (err , & UpdateAgentError {})
8196 }
@@ -92,15 +107,15 @@ func (a *Agent) __updateServiceBinary(machine *MachineInfo) error {
92107 return nil
93108}
94109
95- func (a * Agent ) updateAgent (machine * MachineInfo , skipConfig bool , skipBinaries bool ) error {
110+ func (a * Agent ) updateAgent (machine * MachineInfo , skipConfig bool , skipBinaries bool , version string ) error {
96111 if ! skipConfig {
97112 if err := a .__deployCopyConfigFile (); err != nil {
98113 return convertError (err , & UpdateAgentError {})
99114 }
100115 }
101116
102117 if ! skipBinaries {
103- if err := a .__updateServiceBinary (machine ); err != nil {
118+ if err := a .__updateServiceBinary (machine , version ); err != nil {
104119 return convertError (err , & UpdateAgentError {})
105120 }
106121 }
0 commit comments