Skip to content

Commit 48e1008

Browse files
committed
feature: make alloy version settable
1 parent 662a828 commit 48e1008

File tree

6 files changed

+56
-32
lines changed

6 files changed

+56
-32
lines changed

cmd/agent/deploy.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func init() {
2525
deployCmd.Flags().String("agent.config", "", "path to agent configuration file")
2626
deployCmd.Flags().String("run.format", "progress", "output format")
2727
deployCmd.Flags().Bool("run.dry-run", false, "perform a dry run without deploying the agent")
28+
deployCmd.Flags().String("alloy.version", "latest", "version of Alloy to install")
2829

2930
_ = deployCmd.RegisterFlagCompletionFunc("run.format", completion.CompleteRunFormat)
3031
}
@@ -45,6 +46,7 @@ func runDeployCmd(cmd *cobra.Command, args []string) {
4546
a, err := agent.New(config, targetUrl, formatType, dryRun)
4647
errors.CheckErr(err, formatType)
4748

48-
err = a.Deploy()
49+
alloyVersion, _ := cmd.Flags().GetString("alloy.version")
50+
err = a.Deploy(alloyVersion)
4951
errors.CheckErr(err, formatType)
5052
}

cmd/agent/update.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ var updateCmd = &cobra.Command{
2424
func init() {
2525
updateCmd.Flags().String("agent.config", "", "path to agent configuration file")
2626
updateCmd.Flags().String("run.format", "progress", "output format")
27-
updateCmd.Flags().Bool("run.dry-run", false, "perform a dry run without updateing the agent")
27+
updateCmd.Flags().Bool("run.dry-run", false, "perform a dry run without updating the agent")
2828
updateCmd.Flags().Bool("skip.config", false, "skip configuration file update")
2929
updateCmd.Flags().Bool("skip.binaries", false, "skip binaries update")
30+
updateCmd.Flags().String("alloy.version", "latest", "version of Alloy to install")
3031

3132
_ = updateCmd.RegisterFlagCompletionFunc("run.format", completion.CompleteRunFormat)
3233
}
@@ -39,6 +40,7 @@ func runUpdateCmd(cmd *cobra.Command, args []string) {
3940
config, _ := cmd.Flags().GetString("agent.config")
4041
skipConfig, _ := cmd.Flags().GetBool("skip.config")
4142
skipBinaries, _ := cmd.Flags().GetBool("skip.binaries")
43+
alloyVersion, _ := cmd.Flags().GetString("alloy.version")
4244

4345
if skipConfig && skipBinaries {
4446
errors.CheckErr(fmt.Errorf("at least one of --skip.config or --skip.binaries must be false"), formatType)
@@ -54,6 +56,6 @@ func runUpdateCmd(cmd *cobra.Command, args []string) {
5456
a, err := agent.New(config, targetUrl, formatType, dryRun)
5557
errors.CheckErr(err, formatType)
5658

57-
err = a.Update(skipConfig, skipBinaries)
59+
err = a.Update(skipConfig, skipBinaries, alloyVersion)
5860
errors.CheckErr(err, formatType)
5961
}

internal/agent/agent.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (a *Agent) Teardown() error {
4747
return nil
4848
}
4949

50-
func (a *Agent) Deploy() error {
50+
func (a *Agent) Deploy(version string) error {
5151
defer func() {
5252
if a.format == target.FormatProgress {
5353
println()
@@ -63,7 +63,7 @@ func (a *Agent) Deploy() error {
6363
return err
6464
}
6565

66-
if err := a.deployAgent(machine); err != nil {
66+
if err := a.deployAgent(machine, version); err != nil {
6767
return err
6868
}
6969

@@ -86,7 +86,7 @@ func (a *Agent) Config(service, resourceID string) ([]byte, error) {
8686
return a.configAgent(service, resourceID)
8787
}
8888

89-
func (a *Agent) Update(skipConfig bool, skipBinaries bool) error {
89+
func (a *Agent) Update(skipConfig bool, skipBinaries bool, version string) error {
9090
defer func() {
9191
if a.format == target.FormatProgress {
9292
println()
@@ -102,7 +102,7 @@ func (a *Agent) Update(skipConfig bool, skipBinaries bool) error {
102102
return err
103103
}
104104

105-
return a.updateAgent(machine, skipConfig, skipBinaries)
105+
return a.updateAgent(machine, skipConfig, skipBinaries, version)
106106
}
107107

108108
func (a *Agent) Describe(service, resourceID string) (*DescribeData, error) {

internal/agent/agent_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Test_Deploy(t *testing.T) {
2424
assert.NoError(t, err, "create agent")
2525

2626
record := capture(func() {
27-
err = a.Deploy()
27+
err = a.Deploy("latest")
2828
})
2929
assert.NoError(t, err)
3030

@@ -41,7 +41,7 @@ func Test_Deploy(t *testing.T) {
4141
assert.NoError(t, err, "create agent")
4242

4343
record = capture(func() {
44-
err = a.Deploy()
44+
err = a.Deploy("latest")
4545
})
4646
assert.NoError(t, err)
4747

@@ -94,7 +94,7 @@ func Test_Update(t *testing.T) {
9494
assert.NoError(t, err, "create agent")
9595

9696
record := capture(func() {
97-
err = a.Update(false, false)
97+
err = a.Update(false, false, "latest")
9898
})
9999
assert.NoError(t, err, "update agent")
100100

@@ -111,7 +111,7 @@ func Test_Update(t *testing.T) {
111111
assert.NoError(t, err, "create agent")
112112

113113
record = capture(func() {
114-
err = a.Update(false, false)
114+
err = a.Update(false, false, "latest")
115115
})
116116
assert.NoError(t, err, "update agent")
117117

internal/agent/deploy.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ func (a *Agent) __deployCopySystemdServiceUnit() error {
6868
return nil
6969
}
7070

71-
func (a *Agent) __deployDownloadRelease(release string, tmpdir string) (string, error) {
72-
url := fmt.Sprintf("https://github.com/grafana/alloy/releases/latest/download/%s.zip", release)
71+
func (a *Agent) __deployDownloadRelease(release string, version string, tmpdir string) (string, error) {
72+
var url string
73+
if version != "latest" {
74+
url = fmt.Sprintf("https://github.com/grafana/alloy/releases/download/%s/%s.zip", version, release)
75+
} else {
76+
url = fmt.Sprintf("https://github.com/grafana/alloy/releases/latest/download/%s.zip", release)
77+
}
7378
tmpfile := fmt.Sprintf("%s/%s-%s.zip", tmpdir, release, time.Now().Format("19800212015200"))
7479

7580
a.__helperPrintProgress(fmt.Sprintf("Running 'GET %s'", url))
@@ -201,7 +206,7 @@ func (a *Agent) __helperPrintProgress(message string) {
201206
target.PrintProgress(fmt.Sprintf("%s as %s@localhost", message, username), a.format)
202207
}
203208

204-
func (a *Agent) deployAgent(machine *MachineInfo) error {
209+
func (a *Agent) deployAgent(machine *MachineInfo, alloyVersion string) error {
205210
if err := a.__deployMakeDirHierarchy(); err != nil {
206211
return err
207212
}
@@ -223,7 +228,7 @@ func (a *Agent) deployAgent(machine *MachineInfo) error {
223228
}()
224229

225230
release := fmt.Sprintf("alloy-%s-%s", machine.Kernel, machine.Arch)
226-
zip, err := a.__deployDownloadRelease(release, tmpdir)
231+
zip, err := a.__deployDownloadRelease(release, alloyVersion, tmpdir)
227232
if err != nil {
228233
return err
229234
}

internal/agent/update.go

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)