From 8772b96dc2a85862ba1b9877136fb8747f8a288a Mon Sep 17 00:00:00 2001 From: Nitishkumar Singh Date: Sat, 11 May 2024 20:39:18 +0530 Subject: [PATCH] add caddy server as system install Signed-off-by: Nitishkumar Singh --- README.md | 5 +- cmd/system/caddy.go | 109 ++++++++++++++++++++++++++++++++++++++++++ cmd/system/install.go | 1 + 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 cmd/system/caddy.go diff --git a/README.md b/README.md index 2e7de42c0..066d9e9bd 100644 --- a/README.md +++ b/README.md @@ -244,15 +244,16 @@ Run the following to see what's available `arkade system install`: ``` actions-runner Install GitHub Actions Runner buildkitd Install Buildkitd + caddy Install Caddy Server cni Install CNI plugins containerd Install containerd firecracker Install Firecracker - gitlab-runner Install Gitlab Runner + gitlab-runner Install GitLab Runner go Install Go node Install Node.js prometheus Install Prometheus + registry Install registry tc-redirect-tap Install tc-redirect-tap - registry Install Open Source Registry implementation for storing and distributing container images using the OCI Distribution Specification ``` The initial set of system apps is now complete, learn more in the original proposal: [Feature: system packages for Linux servers, CI and workstations #654](https://github.com/alexellis/arkade/issues/654) diff --git a/cmd/system/caddy.go b/cmd/system/caddy.go new file mode 100644 index 000000000..d02699afd --- /dev/null +++ b/cmd/system/caddy.go @@ -0,0 +1,109 @@ +// Copyright (c) arkade author(s) 2024. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package system + +import ( + "fmt" + "os" + "path" + "strings" + + "github.com/alexellis/arkade/pkg/archive" + "github.com/alexellis/arkade/pkg/env" + "github.com/alexellis/arkade/pkg/get" + "github.com/spf13/cobra" +) + +func MakeInstallCaddyServer() *cobra.Command { + command := &cobra.Command{ + Use: "caddy", + Short: "Install Caddy Server", + Long: `Install Caddy Server which is an extensible server platform that uses TLS by default`, + Example: ` arkade system install caddy + arkade system install caddy --version `, + SilenceUsage: true, + } + + command.Flags().StringP("version", "v", "", "The version or leave blank to determine the latest available version") + command.Flags().String("path", "/usr/local/bin", "Installation path, where a caddy subfolder will be created") + 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 Caddy Server to %s\n", installPath) + + installPath = strings.ReplaceAll(installPath, "$HOME", os.Getenv("HOME")) + + if err := os.MkdirAll(installPath, 0755); err != nil && !os.IsExist(err) { + fmt.Printf("Error creating directory %s, error: %s\n", installPath, err.Error()) + } + + 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 { + dlArch = arch + } + + if version == "" { + v, err := get.FindGitHubRelease("caddyserver", "caddy") + if err != nil { + return err + } + version = v + } else if !strings.HasPrefix(version, "v") { + version = "v" + version + } + + fmt.Printf("Installing version: %s for: %s\n", version, dlArch) + + filename := fmt.Sprintf("caddy_%s_linux_%s.tar.gz", strings.TrimPrefix(version, "v"), dlArch) + dlURL := fmt.Sprintf(githubDownloadTemplate, "caddyserver", "caddy", version, filename) + + 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) + + f, err := os.OpenFile(outPath, os.O_RDONLY, 0644) + if err != nil { + return err + } + defer f.Close() + + fmt.Printf("Unpacking Caddy to: %s\n", path.Join(installPath, "caddy")) + + if err := archive.Untar(f, installPath, true, true); err != nil { + return err + } + + return nil + } + + return command +} diff --git a/cmd/system/install.go b/cmd/system/install.go index 133a482c6..0a08598d2 100644 --- a/cmd/system/install.go +++ b/cmd/system/install.go @@ -32,6 +32,7 @@ func MakeInstall() *cobra.Command { command.AddCommand(MakeInstallRegistry()) command.AddCommand(MakeInstallGitLabRunner()) command.AddCommand((MakeInstallBuildkitd())) + command.AddCommand(MakeInstallCaddyServer()) return command }