Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add containerd for system applications #672

Merged
merged 1 commit into from
Apr 11, 2022
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
200 changes: 200 additions & 0 deletions cmd/system/containerd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
// 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 (
"bytes"
"fmt"
"github.com/alexellis/arkade/pkg/archive"
"github.com/alexellis/arkade/pkg/env"
"github.com/alexellis/arkade/pkg/get"
execute "github.com/alexellis/go-execute/pkg/v1"
"github.com/spf13/cobra"
"io/ioutil"
"net/http"
"os"
"strings"
)

func MakeInstallContainerd() *cobra.Command {
cmd := &cobra.Command{
Use: "containerd",
Short: "Install containerd",
Long: "Install container container runtime.",
Example: `arkade system install containerd`,
SilenceUsage: true,
}

cmd.Flags().StringP("version", "v", "", "Version of the containerd binary pack, leave blank for latest")
cmd.Flags().String("path", "/usr/local/bin", "Install path, where the containerd binaries will installed")
cmd.Flags().String("arch", "", "Set system download arch")
cmd.Flags().Bool("systemd", true, "Add and enable systemd service for containerd")
cmd.Flags().Bool("progress", true, "Show download progress")

cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
_, err := cmd.Flags().GetString("path")
if err != nil {
return err
}

_, err = cmd.Flags().GetBool("progress")
if err != nil {
return err
}

return nil
}

cmd.RunE = func(cmd *cobra.Command, args []string) error {
installPath, _ := cmd.Flags().GetString("path")
version, _ := cmd.Flags().GetString("version")
progress, _ := cmd.Flags().GetBool("progress")
systemd, _ := cmd.Flags().GetBool("systemd")

arch, osVer := env.GetClientArch()
if cmd.Flags().Changed("arch") {
archFlag, _ := cmd.Flags().GetString("arch")
arch = archFlag
}

fmt.Printf("Installing containerd to %s\n", installPath)

if strings.ToLower(osVer) != "linux" {
return fmt.Errorf("this app currently only supports Linux")
}

if version == "" {
latestVerison, err := get.FindGitHubRelease("containerd", "containerd")
if err != nil {
return err
}
version = latestVerison
}

downloadArch := ""

if arch == "x86_64" {
downloadArch = "amd64"
} else if arch == "aarch64" {
downloadArch = "arm64"
} else {
return fmt.Errorf("this app currently only supports arm64 and amd64 archs")
}

containerdTool := get.Tool{
Name: "containerd",
Repo: "containerd",
Owner: "containerd",
Version: version,
BinaryTemplate: `
{{$archStr := .Arch}}
{{- if eq .Arch "aarch64" -}}
{{$archStr = "arm64"}}
{{- else if eq .Arch "x86_64" -}}
{{$archStr = "amd64"}}
{{- end -}}
{{.Name}}-{{.VersionNumber}}-{{.OS}}-{{$archStr}}.tar.gz
`,
}

url, err := containerdTool.GetURL(osVer, downloadArch, containerdTool.Version, !progress)
if err != nil {
return err
}

outPath, err := get.DownloadFileP(url, progress)
if err != nil {
return err
}
fmt.Printf("Downloaded to: %s\n", outPath)

f, err := os.OpenFile(outPath, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer f.Close()

tempDirName := os.TempDir() + "/containerd"

if err := archive.UntarNested(f, tempDirName); err != nil {
return err
}

fmt.Printf("Copying containerd binaries to: %s\n", installPath)

dir, err := os.ReadDir(tempDirName + "/bin")
if err != nil {
return err
}

if err := os.MkdirAll(installPath, 0755); err != nil && !os.IsExist(err) {
fmt.Printf("Error creating directory %s, error: %s\n", installPath, err.Error())
}

for _, entry := range dir {
_, err := get.CopyFile(tempDirName+"/bin/"+entry.Name(), installPath+"/"+entry.Name())
if err != nil {
return err
}
}

if systemd {
Shikachuu marked this conversation as resolved.
Show resolved Hide resolved
systemdUnitName := "containerd.service"
systemdUnitUrl := fmt.Sprintf("https://raw.githubusercontent.com/containerd/containerd/%s/%s", version, systemdUnitName)

response, err := http.Get(systemdUnitUrl)
if err != nil {
return err
}

content, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}

content = bytes.ReplaceAll(content, []byte("/usr/local/bin/containerd"), []byte(installPath+"/containerd"))

if err := createSystemdUnit(systemdUnitName, content); err != nil {
return err
}
}

return nil
}

return cmd
}

func createSystemdUnit(systemdUnitName string, content []byte) error {
fmt.Printf("Creating systemd unit file\n")

if err := os.WriteFile("/lib/systemd/system/"+systemdUnitName, content, os.FileMode(0700)); err != nil {
return err
}

taskReload := execute.ExecTask{
Command: "systemctl",
Args: []string{"daemon-reload"},
StreamStdio: false,
}
if _, err := taskReload.Execute(); err != nil {
return err
}

taskEnable := execute.ExecTask{
Command: "systemctl",
Args: []string{"enable", "--now", systemdUnitName},
StreamStdio: false,
}

result, err := taskEnable.Execute()
if err != nil {
return err
}

if result.ExitCode != 0 {
return fmt.Errorf("error enabling systemd service, stderr: %s", result.Stderr)
}
return nil
}
1 change: 1 addition & 0 deletions cmd/system/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func MakeInstall() *cobra.Command {
command.AddCommand(MakeInstallFirecracker())
command.AddCommand(MakeInstallPrometheus())
command.AddCommand(MakeInstallCNI())
command.AddCommand(MakeInstallContainerd())

return command
}
4 changes: 2 additions & 2 deletions cmd/system/system.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) arkade author(s) 2020. All rights reserved.
// 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.

// system contains a suite of Sponsored Apps for arkade
// system contains packages for Linux servers and workstations
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this copy/paste mistake

package system

import (
Expand Down