Skip to content

Commit 6d43b8b

Browse files
authored
Merge pull request #150 from jacobweinstock/update
Clean up
2 parents 73b36d3 + 4de314b commit 6d43b8b

File tree

1 file changed

+19
-74
lines changed

1 file changed

+19
-74
lines changed

hook-docker/main.go

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,23 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"io"
7-
"net/http"
86
"os"
97
"os/exec"
8+
"path/filepath"
109
"strings"
1110
"time"
1211
)
1312

1413
type tinkConfig struct {
15-
registry string
16-
baseURL string
17-
tinkerbell string
18-
syslogHost string
19-
20-
// TODO add others
14+
syslogHost string
15+
insecureRegistries []string
2116
}
2217

2318
type dockerConfig struct {
24-
Debug bool `json:"debug"`
25-
LogDriver string `json:"log-driver,omitempty"`
26-
LogOpts map[string]string `json:"log-opts,omitempty"`
19+
Debug bool `json:"debug"`
20+
LogDriver string `json:"log-driver,omitempty"`
21+
LogOpts map[string]string `json:"log-opts,omitempty"`
22+
InsecureRegistries []string `json:"insecure-registries,omitempty"`
2723
}
2824

2925
func main() {
@@ -38,29 +34,24 @@ func main() {
3834
cmdLines := strings.Split(string(content), " ")
3935
cfg := parseCmdLine(cmdLines)
4036

41-
path := fmt.Sprintf("/etc/docker/certs.d/%s/", cfg.registry)
42-
43-
// Create the directory
44-
err = os.MkdirAll(path, os.ModeDir)
45-
if err != nil {
46-
panic(err)
47-
}
48-
// Download the configuration
49-
err = downloadFile(path+"ca.crt", cfg.baseURL+"/ca.pem")
50-
if err != nil {
51-
panic(err)
52-
}
53-
fmt.Println("Downloaded the repository certificates, starting the Docker Engine")
37+
fmt.Println("Starting the Docker Engine")
5438

5539
d := dockerConfig{
5640
Debug: true,
5741
LogDriver: "syslog",
5842
LogOpts: map[string]string{
5943
"syslog-address": fmt.Sprintf("udp://%v:514", cfg.syslogHost),
6044
},
45+
InsecureRegistries: cfg.insecureRegistries,
46+
}
47+
path := "/etc/docker"
48+
// Create the directory for the docker config
49+
err = os.MkdirAll(path, os.ModeDir)
50+
if err != nil {
51+
panic(err)
6152
}
62-
if err := d.writeToDisk("/etc/docker/daemon.json"); err != nil {
63-
fmt.Println("Failed to write docker config:", err)
53+
if err := d.writeToDisk(filepath.Join(path, "daemon.json")); err != nil {
54+
panic(fmt.Sprintf("Failed to write docker config: %v", err))
6455
}
6556

6657
// Build the command, and execute
@@ -95,61 +86,15 @@ func parseCmdLine(cmdLines []string) (cfg tinkConfig) {
9586
}
9687

9788
switch cmd := cmdLine[0]; cmd {
98-
// Find Registry configuration
99-
case "docker_registry":
100-
cfg.registry = cmdLine[1]
101-
case "packet_base_url":
102-
cfg.baseURL = cmdLine[1]
103-
case "tinkerbell":
104-
cfg.tinkerbell = cmdLine[1]
10589
case "syslog_host":
10690
cfg.syslogHost = cmdLine[1]
91+
case "insecure_registries":
92+
cfg.insecureRegistries = strings.Split(cmdLine[1], ",")
10793
}
10894
}
10995
return cfg
11096
}
11197

112-
// downloadFile will download a url to a local file. It's efficient because it will
113-
// write as it downloads and not load the whole file into memory.
114-
func downloadFile(filepath string, url string) error {
115-
// As all functions in the LinuxKit services run in parallel, ensure that we can fail
116-
// successfully until we accept that networking is actually broken
117-
118-
var maxRetryCount int
119-
var timeOut time.Duration
120-
maxRetryCount = 10
121-
timeOut = time.Millisecond * 500 // 0.5 seconds
122-
var resp *http.Response
123-
var err error
124-
125-
// Retry this task
126-
for retries := 0; retries < maxRetryCount; retries++ {
127-
// Get the data
128-
resp, err = http.Get(url)
129-
if err == nil {
130-
break
131-
}
132-
resp.Body.Close()
133-
134-
if retries == maxRetryCount-1 {
135-
return err
136-
}
137-
time.Sleep(timeOut)
138-
}
139-
defer resp.Body.Close()
140-
141-
// Create the file
142-
out, err := os.Create(filepath)
143-
if err != nil {
144-
return err
145-
}
146-
defer out.Close()
147-
148-
// Write the body to file
149-
_, err = io.Copy(out, resp.Body)
150-
return err
151-
}
152-
15398
func rebootWatch() {
15499
fmt.Println("Starting Reboot Watcher")
155100

0 commit comments

Comments
 (0)