Path: blob/main/components/registry-facade/cmd/setup.go
2500 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cmd56import (7"fmt"8"os"9"path/filepath"10"regexp"1112"github.com/spf13/cobra"1314"github.com/gitpod-io/gitpod/common-go/log"15)1617var (18hostname string19hostfs string20port int21)2223var setupCmd = &cobra.Command{24Use: "setup",25Short: "Updates the /etc/hosts file, updates the CA certificates and creates the registry host for containerd",26Run: func(cmd *cobra.Command, args []string) {27{28log.Info("Creating containerd registry directory...")29regDirectory := fmt.Sprintf("/etc/containerd/certs.d/%v:%v", hostname, port)3031fakeRegPath := filepath.Join(hostfs, regDirectory)32err := os.MkdirAll(fakeRegPath, 0644)33if err != nil {34log.Fatalf("cannot create containerd cert directory: %v", err)35}3637caPath := filepath.Join(fakeRegPath, "ca.crt")38err = copyFile("/usr/local/share/ca-certificates/gitpod-ca.crt", caPath)39if err != nil {40log.Fatal(err)41}4243// https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration44// https://github.com/containerd/containerd/blob/main/docs/hosts.md45hostsToml := fmt.Sprintf(`46server = "https://%v:%v"4748[host."https://%v:%v"]49capabilities = ["pull", "resolve"]50ca = "%v"51# skip verifications of the registry's certificate chain and host name when set to true52#skip_verify = true53`, hostname, port, hostname, port, filepath.Join(regDirectory, "ca.crt"))5455err = os.WriteFile(filepath.Join(fakeRegPath, "hosts.toml"), []byte(hostsToml), 0644)56if err != nil {57log.Fatalf("cannot create hosts.toml file: %v", err)58}59}6061{62log.Info("Updating /etc/hosts file...")63hostsPath := filepath.Join(hostfs, "/etc/hosts")64if !hostExists(hostname, hostsPath) {65err := addHost(hostname, "127.0.0.1", hostsPath)66if err != nil {67log.Fatalf("cannot update hosts file: %v", err)68}69}70}71},72}7374func init() {75rootCmd.AddCommand(setupCmd)7677setupCmd.Flags().StringVar(&hostname, "hostname", "", "registry facade host <hostname:port>")78setupCmd.Flags().StringVar(&hostfs, "hostfs", "", "Mount point path for the root filesystem")79setupCmd.Flags().IntVar(&port, "port", 31750, "Listening port for the new registry hostname")8081_ = setupCmd.MarkFlagRequired("hostname")82_ = setupCmd.MarkFlagRequired("hostfs")83}8485func hostExists(hostname, hostsPath string) bool {86b, err := os.ReadFile(hostsPath)87if err != nil {88panic(err)89}9091exist, err := regexp.Match(hostname, b)92if err != nil {93return false94}9596return exist97}9899func addHost(hostname, ip, hostPath string) error {100f, err := os.OpenFile(hostPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)101if err != nil {102return err103}104defer f.Close()105106_, err = f.WriteString(fmt.Sprintf("%v %v\n", ip, hostname))107if err != nil {108return err109}110111return nil112}113114func copyFile(source, target string) error {115input, err := os.ReadFile(source)116if err != nil {117return fmt.Errorf("cannot read source file %v: %v", source, err)118}119120err = os.WriteFile(target, input, 0644)121if err != nil {122return fmt.Errorf("cannot write to target file %v: %v", source, err)123}124125return nil126}127128129