/
gitlab_runner.go
93 lines (71 loc) · 2.52 KB
/
gitlab_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) arkade author(s) 2022. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package system
import (
"fmt"
"log"
"os"
"strings"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
"github.com/spf13/cobra"
)
func MakeInstallGitLabRunner() *cobra.Command {
command := &cobra.Command{
Use: "gitlab-runner",
Short: "Install GitLab Runner",
Long: `Install GitLab Runner for self-hosted CI.`,
Example: ` arkade system install gitlab-runner
arkade system install gitlab-runner --version <version>`,
SilenceUsage: true,
}
command.Flags().StringP("version", "v", "", "The version or leave blank to determine the latest available version")
command.Flags().String("path", "$HOME/gitlab-runner", "Installation path, where gitlab-runner binary file is downloaded")
command.Flags().Bool("progress", true, "Show download progress")
command.Flags().String("arch", "", "CPU architecture i.e. amd64")
command.PreRunE = func(cmd *cobra.Command, args []string) error {
return nil
}
command.RunE = func(cmd *cobra.Command, args []string) error {
installPath, _ := cmd.Flags().GetString("path")
version, _ := cmd.Flags().GetString("version")
fmt.Printf("Installing GitLab Runner to %s\n", installPath)
installPath = strings.ReplaceAll(installPath, "$HOME", os.Getenv("HOME"))
arch, osVer := env.GetClientArch()
if strings.ToLower(osVer) != "linux" {
return fmt.Errorf("this app only supports Linux")
}
if cmd.Flags().Changed("arch") {
arch, _ = cmd.Flags().GetString("arch")
}
dlArch := arch
if arch == "x86_64" {
dlArch = "amd64"
} else if arch == "aarch64" {
dlArch = "arm64"
} else if arch == "armv7" || arch == "armv7l" {
dlArch = "arm"
}
if version == "" {
version = "latest"
} else if !strings.HasPrefix(version, "v") {
version = "v" + version
}
fmt.Printf("Installing version: %s for: %s\n", version, dlArch)
dlURL := fmt.Sprintf("https://gitlab-runner-downloads.s3.amazonaws.com/%s/binaries/gitlab-runner-linux-%s", version, dlArch)
fmt.Printf("Downloading from: %s\n", dlURL)
progress, _ := cmd.Flags().GetBool("progress")
outPath, err := get.DownloadFileP(dlURL, progress)
if err != nil {
return err
}
defer os.Remove(outPath)
fmt.Printf("Downloaded to: %s\n", outPath)
if _, err := get.CopyFile(outPath, installPath); err != nil {
log.Fatal(err)
}
fmt.Printf("Binary file copied to: %s\n", installPath)
return nil
}
return command
}