From b8c2f3b8a5e95332ea226315809da70c2ab1e015 Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Thu, 18 May 2023 13:27:02 +0530 Subject: [PATCH] add system install for gitlab runner Signed-off-by: Nikhil Sharma --- cmd/system/gitlab_runner.go | 93 +++++++++++++++++++++++++++++++++++++ cmd/system/install.go | 1 + 2 files changed, 94 insertions(+) create mode 100644 cmd/system/gitlab_runner.go diff --git a/cmd/system/gitlab_runner.go b/cmd/system/gitlab_runner.go new file mode 100644 index 000000000..19b2299ae --- /dev/null +++ b/cmd/system/gitlab_runner.go @@ -0,0 +1,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 `, + 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 +} diff --git a/cmd/system/install.go b/cmd/system/install.go index fdf301139..a87d6f0fb 100644 --- a/cmd/system/install.go +++ b/cmd/system/install.go @@ -30,6 +30,7 @@ func MakeInstall() *cobra.Command { command.AddCommand(MakeInstallNode()) command.AddCommand(MakeInstallTCRedirectTap()) command.AddCommand(MakeInstallRegistry()) + command.AddCommand(MakeInstallGitLabRunner()) return command }