-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add containerd for system applications.
Signed-off-by: Czékus Máté <[email protected]>
- Loading branch information
Showing
3 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
// 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 { | ||
systemdUnitName := "containerd.service" | ||
systemdUnitUrl := fmt.Sprintf("https://raw.githubusercontent.com/containerd/containerd/%s/%s", version, systemdUnitName) | ||
|
||
if err = installSystemdUnit(systemdUnitUrl, installPath, systemdUnitName); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func installSystemdUnit(systemdUnitUrl string, installPath string, systemdUnitName string) error { | ||
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")) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters