Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/registry-facade/cmd/setup.go
2500 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"fmt"
9
"os"
10
"path/filepath"
11
"regexp"
12
13
"github.com/spf13/cobra"
14
15
"github.com/gitpod-io/gitpod/common-go/log"
16
)
17
18
var (
19
hostname string
20
hostfs string
21
port int
22
)
23
24
var setupCmd = &cobra.Command{
25
Use: "setup",
26
Short: "Updates the /etc/hosts file, updates the CA certificates and creates the registry host for containerd",
27
Run: func(cmd *cobra.Command, args []string) {
28
{
29
log.Info("Creating containerd registry directory...")
30
regDirectory := fmt.Sprintf("/etc/containerd/certs.d/%v:%v", hostname, port)
31
32
fakeRegPath := filepath.Join(hostfs, regDirectory)
33
err := os.MkdirAll(fakeRegPath, 0644)
34
if err != nil {
35
log.Fatalf("cannot create containerd cert directory: %v", err)
36
}
37
38
caPath := filepath.Join(fakeRegPath, "ca.crt")
39
err = copyFile("/usr/local/share/ca-certificates/gitpod-ca.crt", caPath)
40
if err != nil {
41
log.Fatal(err)
42
}
43
44
// https://github.com/containerd/containerd/blob/main/docs/cri/config.md#registry-configuration
45
// https://github.com/containerd/containerd/blob/main/docs/hosts.md
46
hostsToml := fmt.Sprintf(`
47
server = "https://%v:%v"
48
49
[host."https://%v:%v"]
50
capabilities = ["pull", "resolve"]
51
ca = "%v"
52
# skip verifications of the registry's certificate chain and host name when set to true
53
#skip_verify = true
54
`, hostname, port, hostname, port, filepath.Join(regDirectory, "ca.crt"))
55
56
err = os.WriteFile(filepath.Join(fakeRegPath, "hosts.toml"), []byte(hostsToml), 0644)
57
if err != nil {
58
log.Fatalf("cannot create hosts.toml file: %v", err)
59
}
60
}
61
62
{
63
log.Info("Updating /etc/hosts file...")
64
hostsPath := filepath.Join(hostfs, "/etc/hosts")
65
if !hostExists(hostname, hostsPath) {
66
err := addHost(hostname, "127.0.0.1", hostsPath)
67
if err != nil {
68
log.Fatalf("cannot update hosts file: %v", err)
69
}
70
}
71
}
72
},
73
}
74
75
func init() {
76
rootCmd.AddCommand(setupCmd)
77
78
setupCmd.Flags().StringVar(&hostname, "hostname", "", "registry facade host <hostname:port>")
79
setupCmd.Flags().StringVar(&hostfs, "hostfs", "", "Mount point path for the root filesystem")
80
setupCmd.Flags().IntVar(&port, "port", 31750, "Listening port for the new registry hostname")
81
82
_ = setupCmd.MarkFlagRequired("hostname")
83
_ = setupCmd.MarkFlagRequired("hostfs")
84
}
85
86
func hostExists(hostname, hostsPath string) bool {
87
b, err := os.ReadFile(hostsPath)
88
if err != nil {
89
panic(err)
90
}
91
92
exist, err := regexp.Match(hostname, b)
93
if err != nil {
94
return false
95
}
96
97
return exist
98
}
99
100
func addHost(hostname, ip, hostPath string) error {
101
f, err := os.OpenFile(hostPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
102
if err != nil {
103
return err
104
}
105
defer f.Close()
106
107
_, err = f.WriteString(fmt.Sprintf("%v %v\n", ip, hostname))
108
if err != nil {
109
return err
110
}
111
112
return nil
113
}
114
115
func copyFile(source, target string) error {
116
input, err := os.ReadFile(source)
117
if err != nil {
118
return fmt.Errorf("cannot read source file %v: %v", source, err)
119
}
120
121
err = os.WriteFile(target, input, 0644)
122
if err != nil {
123
return fmt.Errorf("cannot write to target file %v: %v", source, err)
124
}
125
126
return nil
127
}
128
129