|
| 1 | +// Copyright (c) arkade author(s) 2024. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +package system |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/alexellis/arkade/pkg/env" |
| 13 | + "github.com/alexellis/arkade/pkg/get" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func MakeInstallNodeExporter() *cobra.Command { |
| 18 | + command := &cobra.Command{ |
| 19 | + Use: "node_exporter", |
| 20 | + Short: "Install Node Exporter", |
| 21 | + Long: `Install Node Exporter which is a Prometheus exporter for hardware and OS |
| 22 | +metrics exposed by a server/container such as CPU/RAM/Disk/Network.`, |
| 23 | + RunE: installNodeExporterE, |
| 24 | + SilenceUsage: true, |
| 25 | + } |
| 26 | + |
| 27 | + command.Flags().StringP("version", "v", "", "The version for Go, or leave blank for pinned version") |
| 28 | + command.Flags().String("path", "/usr/local/", "Installation path, where a go subfolder will be created") |
| 29 | + command.Flags().Bool("progress", true, "Show download progress") |
| 30 | + command.Flags().Bool("systemd", false, "Install and start a systemd service") |
| 31 | + |
| 32 | + return command |
| 33 | +} |
| 34 | + |
| 35 | +func installNodeExporterE(cmd *cobra.Command, args []string) error { |
| 36 | + installPath, _ := cmd.Flags().GetString("path") |
| 37 | + version, _ := cmd.Flags().GetString("version") |
| 38 | + progress, _ := cmd.Flags().GetBool("progress") |
| 39 | + systemd, _ := cmd.Flags().GetBool("systemd") |
| 40 | + |
| 41 | + arch, osVer := env.GetClientArch() |
| 42 | + |
| 43 | + if cmd.Flags().Changed("os") { |
| 44 | + osVer, _ = cmd.Flags().GetString("os") |
| 45 | + } |
| 46 | + if cmd.Flags().Changed("arch") { |
| 47 | + arch, _ = cmd.Flags().GetString("arch") |
| 48 | + } |
| 49 | + |
| 50 | + if strings.ToLower(osVer) != "linux" && strings.ToLower(osVer) != "darwin" { |
| 51 | + return fmt.Errorf("this app only supports Linux and Darwin") |
| 52 | + } |
| 53 | + |
| 54 | + tools := get.MakeTools() |
| 55 | + var tool *get.Tool |
| 56 | + for _, t := range tools { |
| 57 | + if t.Name == "node_exporter" { |
| 58 | + tool = &t |
| 59 | + break |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if tool == nil { |
| 64 | + return fmt.Errorf("unable to find node_exporter definition") |
| 65 | + } |
| 66 | + |
| 67 | + fmt.Printf("Installing node_exporter Server to %s\n", installPath) |
| 68 | + |
| 69 | + installPath = strings.ReplaceAll(installPath, "$HOME", os.Getenv("HOME")) |
| 70 | + |
| 71 | + if err := os.MkdirAll(installPath, 0755); err != nil && !os.IsExist(err) { |
| 72 | + fmt.Printf("Error creating directory %s, error: %s\n", installPath, err.Error()) |
| 73 | + } |
| 74 | + |
| 75 | + if version == "" { |
| 76 | + v, err := get.FindGitHubRelease("prometheus", "node_exporter") |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + version = v |
| 81 | + } else if !strings.HasPrefix(version, "v") { |
| 82 | + version = "v" + version |
| 83 | + } |
| 84 | + |
| 85 | + outFilePath, _, err := get.Download(tool, arch, osVer, version, installPath, progress, !progress) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if err = os.Chmod(outFilePath, readWriteExecuteEveryone); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + if systemd && strings.ToLower(osVer) != "linux" { |
| 94 | + return fmt.Errorf("systemd is only supported on Linux") |
| 95 | + } |
| 96 | + |
| 97 | + if systemd { |
| 98 | + systemdUnit := generateUnit(outFilePath) |
| 99 | + unitName := "/etc/systemd/system/node_exporter.service" |
| 100 | + if err := os.WriteFile(unitName, []byte(systemdUnit), readWriteExecuteEveryone); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Printf("Wrote: %s\n", systemdUnit) |
| 105 | + |
| 106 | + if _, err = executeShellCmd(context.Background(), "systemctl", "daemon-reload"); err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + if _, err = executeShellCmd(context.Background(), "systemctl", "enable", "node_exporter", "--now"); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + fmt.Printf("Started node_exporter\n") |
| 115 | + |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func generateUnit(outFilePath string) string { |
| 121 | + return fmt.Sprintf(`[Unit] |
| 122 | +Description=Node Exporter |
| 123 | +After=network.target |
| 124 | +
|
| 125 | +[Service] |
| 126 | +ExecStart=%s/node_exporter |
| 127 | +
|
| 128 | +[Install] |
| 129 | +WantedBy=multi-user.target |
| 130 | +`, outFilePath) |
| 131 | +} |
0 commit comments