Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/agent/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
deployCmd.Flags().String("agent.config", "", "path to agent configuration file")
deployCmd.Flags().String("run.format", "progress", "output format")
deployCmd.Flags().Bool("run.dry-run", false, "perform a dry run without deploying the agent")
deployCmd.Flags().String("alloy.version", "latest", "version of Alloy to install")

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

err = a.Deploy()
alloyVersion, _ := cmd.Flags().GetString("alloy.version")
err = a.Deploy(alloyVersion)
errors.CheckErr(err, formatType)
}
6 changes: 4 additions & 2 deletions cmd/agent/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ var updateCmd = &cobra.Command{
func init() {
updateCmd.Flags().String("agent.config", "", "path to agent configuration file")
updateCmd.Flags().String("run.format", "progress", "output format")
updateCmd.Flags().Bool("run.dry-run", false, "perform a dry run without updateing the agent")
updateCmd.Flags().Bool("run.dry-run", false, "perform a dry run without updating the agent")
updateCmd.Flags().Bool("skip.config", false, "skip configuration file update")
updateCmd.Flags().Bool("skip.binaries", false, "skip binaries update")
updateCmd.Flags().String("alloy.version", "latest", "version of Alloy to install")

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

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

err = a.Update(skipConfig, skipBinaries)
err = a.Update(skipConfig, skipBinaries, alloyVersion)
errors.CheckErr(err, formatType)
}
8 changes: 4 additions & 4 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a *Agent) Teardown() error {
return nil
}

func (a *Agent) Deploy() error {
func (a *Agent) Deploy(version string) error {
defer func() {
if a.format == target.FormatProgress {
println()
Expand All @@ -63,7 +63,7 @@ func (a *Agent) Deploy() error {
return err
}

if err := a.deployAgent(machine); err != nil {
if err := a.deployAgent(machine, version); err != nil {
return err
}

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

func (a *Agent) Update(skipConfig bool, skipBinaries bool) error {
func (a *Agent) Update(skipConfig bool, skipBinaries bool, version string) error {
defer func() {
if a.format == target.FormatProgress {
println()
Expand All @@ -102,7 +102,7 @@ func (a *Agent) Update(skipConfig bool, skipBinaries bool) error {
return err
}

return a.updateAgent(machine, skipConfig, skipBinaries)
return a.updateAgent(machine, skipConfig, skipBinaries, version)
}

func (a *Agent) Describe(service, resourceID string) (*DescribeData, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_Deploy(t *testing.T) {
assert.NoError(t, err, "create agent")

record := capture(func() {
err = a.Deploy()
err = a.Deploy("latest")
})
assert.NoError(t, err)

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

record = capture(func() {
err = a.Deploy()
err = a.Deploy("latest")
})
assert.NoError(t, err)

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

record := capture(func() {
err = a.Update(false, false)
err = a.Update(false, false, "latest")
})
assert.NoError(t, err, "update agent")

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

record = capture(func() {
err = a.Update(false, false)
err = a.Update(false, false, "latest")
})
assert.NoError(t, err, "update agent")

Expand Down
13 changes: 9 additions & 4 deletions internal/agent/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ func (a *Agent) __deployCopySystemdServiceUnit() error {
return nil
}

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

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

func (a *Agent) deployAgent(machine *MachineInfo) error {
func (a *Agent) deployAgent(machine *MachineInfo, alloyVersion string) error {
if err := a.__deployMakeDirHierarchy(); err != nil {
return err
}
Expand All @@ -223,7 +228,7 @@ func (a *Agent) deployAgent(machine *MachineInfo) error {
}()

release := fmt.Sprintf("alloy-%s-%s", machine.Kernel, machine.Arch)
zip, err := a.__deployDownloadRelease(release, tmpdir)
zip, err := a.__deployDownloadRelease(release, alloyVersion, tmpdir)
if err != nil {
return err
}
Expand Down
49 changes: 32 additions & 17 deletions internal/agent/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,54 @@ import (
"github.com/tschaefer/finchctl/internal/target"
)

func (a *Agent) __updateServiceBinaryIsNeeded() (bool, error) {
if a.dryRun {
target.PrintProgress("Skipping update check due to dry-run mode", a.format)
return false, nil
}

func (a *Agent) __updateServiceBinaryGetLatestTag() (string, error) {
url := "https://api.github.com/repos/grafana/alloy/releases/latest"
a.__helperPrintProgress(fmt.Sprintf("Running 'GET %s'", url))
resp, err := http.Get(url)
if err != nil {
return false, &UpdateAgentError{Message: err.Error(), Reason: ""}
return "", &UpdateAgentError{Message: err.Error(), Reason: ""}
}
defer func() {
_ = resp.Body.Close()
}()

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return false, &UpdateAgentError{Message: "failed to fetch latest release info", Reason: fmt.Sprintf("status code: %d", resp.StatusCode)}
return "", &UpdateAgentError{Message: "failed to fetch latest release info", Reason: fmt.Sprintf("status code: %d", resp.StatusCode)}
}

out, err := io.ReadAll(resp.Body)
if err != nil {
return false, &UpdateAgentError{Message: err.Error(), Reason: ""}
return "", &UpdateAgentError{Message: err.Error(), Reason: ""}
}

a.__helperPrintProgress("Running 'JSON unmarshal \"tag_name\"'")
var data any
if err := json.Unmarshal(out, &data); err != nil {
return false, &UpdateAgentError{Message: err.Error(), Reason: string(out)}
return "", &UpdateAgentError{Message: err.Error(), Reason: string(out)}
}

return data.(map[string]any)["tag_name"].(string), nil
}

func (a *Agent) __updateServiceBinaryIsNeeded(version string) (bool, error) {
if a.dryRun {
target.PrintProgress(
fmt.Sprintf("Skipping Alloy update check for version '%s' due to dry-run mode", version),
a.format,
)
return false, nil
}

latestVersion := version
if version == "latest" {
var err error
latestVersion, err = a.__updateServiceBinaryGetLatestTag()
if err != nil {
return false, err
}
}
latestVersion := data.(map[string]any)["tag_name"].(string)

out, err = a.target.Run("alloy --version | grep -o -E 'v[0-9\\.]+'")
out, err := a.target.Run("alloy --version | grep -o -E 'v[0-9\\.]+'")
if err != nil {
return false, &UpdateAgentError{Message: err.Error(), Reason: string(out)}
}
Expand All @@ -56,8 +71,8 @@ func (a *Agent) __updateServiceBinaryIsNeeded() (bool, error) {
return latestVersion != currentVersion, nil
}

func (a *Agent) __updateServiceBinary(machine *MachineInfo) error {
ok, err := a.__updateServiceBinaryIsNeeded()
func (a *Agent) __updateServiceBinary(machine *MachineInfo, version string) error {
ok, err := a.__updateServiceBinaryIsNeeded(version)
if err != nil {
return err
}
Expand All @@ -75,7 +90,7 @@ func (a *Agent) __updateServiceBinary(machine *MachineInfo) error {
}()

release := fmt.Sprintf("alloy-%s-%s", machine.Kernel, machine.Arch)
zip, err := a.__deployDownloadRelease(release, tmpdir)
zip, err := a.__deployDownloadRelease(release, version, tmpdir)
if err != nil {
return convertError(err, &UpdateAgentError{})
}
Expand All @@ -92,15 +107,15 @@ func (a *Agent) __updateServiceBinary(machine *MachineInfo) error {
return nil
}

func (a *Agent) updateAgent(machine *MachineInfo, skipConfig bool, skipBinaries bool) error {
func (a *Agent) updateAgent(machine *MachineInfo, skipConfig bool, skipBinaries bool, version string) error {
if !skipConfig {
if err := a.__deployCopyConfigFile(); err != nil {
return convertError(err, &UpdateAgentError{})
}
}

if !skipBinaries {
if err := a.__updateServiceBinary(machine); err != nil {
if err := a.__updateServiceBinary(machine, version); err != nil {
return convertError(err, &UpdateAgentError{})
}
}
Expand Down