diff --git a/Makefile b/Makefile index 7c74944a..d15a117b 100644 --- a/Makefile +++ b/Makefile @@ -50,13 +50,13 @@ run: sudo ~/go/bin/linuxkit run qemu --mem 2048 out/hook-${ARCH} dev-bootkitBuild: - cd bootkit; docker buildx build -load -t $(ORG)/hook-bootkit:0.0 . + cd bootkit; docker buildx build --load -t $(ORG)/hook-bootkit:0.0 . bootkitBuild: cd bootkit; docker buildx build --platform linux/amd64,linux/arm64 --push -t $(ORG)/hook-bootkit:0.0 . dev-tink-dockerBuild: - cd tink-docker; docker buildx build -load -t $(ORG)/hook-docker:0.0 . + cd tink-docker; docker buildx build --load -t $(ORG)/hook-docker:0.0 . tink-dockerBuild: cd tink-docker; docker buildx build --platform linux/amd64,linux/arm64 --push -t $(ORG)/hook-docker:0.0 . @@ -145,4 +145,4 @@ out/linters/golangci-lint-$(GOLINT_VERSION): curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLINT_VERSION) mv out/linters/golangci-lint out/linters/golangci-lint-$(GOLINT_VERSION) -# END: lint-install ../hook \ No newline at end of file +# END: lint-install ../hook diff --git a/bootkit/main.go b/bootkit/main.go index 48ba5779..845dceb5 100644 --- a/bootkit/main.go +++ b/bootkit/main.go @@ -51,11 +51,11 @@ func main() { panic(err) } - cmdlines := strings.Split(string(content), " ") - cfg, _ := parsecmdline(cmdlines) + cmdLines := strings.Split(string(content), " ") + cfg := parseCmdLine(cmdLines) // Get the ID from the metadata service - err = cfg.MetaDataQuery() + err = cfg.metaDataQuery() if err != nil { panic(err) } @@ -131,7 +131,11 @@ func main() { if err != nil { panic(err) } - io.Copy(os.Stdout, out) + + _, err = io.Copy(os.Stdout, out) + if err != nil { + panic(err) + } resp, err := cli.ContainerCreate(ctx, tinkContainer, tinkHostConfig, nil, nil, "") if err != nil { @@ -145,74 +149,42 @@ func main() { fmt.Println(resp.ID) } -func parsecmdline(cmdlines []string) (cfg tinkConfig, err error) { - - for i := range cmdlines { - cmdline := strings.Split(cmdlines[i], "=") - if len(cmdline) != 0 { - - // Find Registry configuration - if cmdline[0] == "docker_registry" { - cfg.registry = cmdline[1] - } - if cmdline[0] == "registry_username" { - cfg.username = cmdline[1] - } - if cmdline[0] == "registry_password" { - cfg.password = cmdline[1] - } - - // Find Tinkerbell servers settings - if cmdline[0] == "packet_base_url" { - cfg.baseURL = cmdline[1] - } - if cmdline[0] == "tinkerbell" { - cfg.tinkerbell = cmdline[1] - } - - // Find GRPC configuration - if cmdline[0] == "grpc_authority" { - cfg.grpcAuthority = cmdline[1] - } - if cmdline[0] == "grpc_cert_url" { - cfg.grpcCertURL = cmdline[1] - } - - // Find the worker configuration - if cmdline[0] == "worker_id" { - cfg.workerID = cmdline[1] - } +// parseCmdLine will parse the command line. +func parseCmdLine(cmdLines []string) (cfg tinkConfig) { + for i := range cmdLines { + cmdLine := strings.Split(cmdLines[i], "=") + if len(cmdLine) == 0 { + continue } - } - return -} - -// DownloadFile will download a url to a local file. It's efficient because it will -// write as it downloads and not load the whole file into memory. -func DownloadFile(filepath string, url string) error { - // Get the data - resp, err := http.Get(url) - if err != nil { - return err - } - defer resp.Body.Close() - - // Create the file - out, err := os.Create(filepath) - if err != nil { - return err + switch cmd := cmdLine[0]; cmd { + // Find Registry configuration + case "docker_registry": + cfg.registry = cmdLine[1] + case "registry_username": + cfg.username = cmdLine[1] + case "registry_password": + cfg.password = cmdLine[1] + // Find Tinkerbell servers settings + case "packet_base_url": + cfg.baseURL = cmdLine[1] + case "tinkerbell": + cfg.tinkerbell = cmdLine[1] + // Find GRPC configuration + case "grpc_authority": + cfg.grpcAuthority = cmdLine[1] + case "grpc_cert_url": + cfg.grpcCertURL = cmdLine[1] + // Find the worker configuration + case "worker_id": + cfg.workerID = cmdLine[1] + } } - defer out.Close() - - // Write the body to file - _, err = io.Copy(out, resp.Body) - return err + return cfg } -// MetaDataQuery will query the metadata -func (cfg *tinkConfig) MetaDataQuery() error { - +// metaDataQuery will query the metadata. +func (cfg *tinkConfig) metaDataQuery() error { spaceClient := http.Client{ Timeout: time.Second * 60, // Timeout after 60 seconds (seems massively long is this dial-up?) } diff --git a/tink-docker/main.go b/tink-docker/main.go index 6e966940..27d697a0 100644 --- a/tink-docker/main.go +++ b/tink-docker/main.go @@ -23,13 +23,13 @@ func main() { fmt.Println("Starting Tink-Docker") go rebootWatch() - // Parse the cmdline in order to find the urls for the repostiory and path to the cert + // Parse the cmdline in order to find the urls for the repository and path to the cert content, err := ioutil.ReadFile("/proc/cmdline") if err != nil { panic(err) } - cmdlines := strings.Split(string(content), " ") - cfg, _ := parsecmdline(cmdlines) + cmdLines := strings.Split(string(content), " ") + cfg := parseCmdLine(cmdLines) path := fmt.Sprintf("/etc/docker/certs.d/%s/", cfg.registry) @@ -39,7 +39,7 @@ func main() { panic(err) } // Download the configuration - err = DownloadFile(path+"ca.crt", cfg.baseURL+"/ca.pem") + err = downloadFile(path+"ca.crt", cfg.baseURL+"/ca.pem") if err != nil { panic(err) } @@ -55,29 +55,30 @@ func main() { } } -func parsecmdline(cmdlines []string) (cfg tinkConfig, err error) { +// parseCmdLine will parse the command line. +func parseCmdLine(cmdLines []string) (cfg tinkConfig) { + for i := range cmdLines { + cmdLine := strings.Split(cmdLines[i], "=") + if len(cmdLine) == 0 { + continue + } - for i := range cmdlines { - cmdline := strings.Split(cmdlines[i], "=") - if len(cmdline) != 0 { - if cmdline[0] == "docker_registry" { - cfg.registry = cmdline[1] - } - if cmdline[0] == "packet_base_url" { - cfg.baseURL = cmdline[1] - } - if cmdline[0] == "tinkerbell" { - cfg.tinkerbell = cmdline[1] - } + switch cmd := cmdLine[0]; cmd { + // Find Registry configuration + case "docker_registry": + cfg.registry = cmdLine[1] + case "packet_base_url": + cfg.baseURL = cmdLine[1] + case "tinkerbell": + cfg.tinkerbell = cmdLine[1] } } - return + return cfg } -// DownloadFile will download a url to a local file. It's efficient because it will +// downloadFile will download a url to a local file. It's efficient because it will // write as it downloads and not load the whole file into memory. -func DownloadFile(filepath string, url string) error { - +func downloadFile(filepath string, url string) error { // Get the data resp, err := http.Get(url) if err != nil { @@ -103,7 +104,6 @@ func rebootWatch() { // Forever loop for { if fileExists("/worker/reboot") { - //Build the command, and execute cmd := exec.Command("/sbin/reboot") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr