|
| 1 | +/* |
| 2 | +Copyright 2022 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package standalone |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "io" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + path_filepath "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/dapr/cli/utils" |
| 25 | +) |
| 26 | + |
| 27 | +func runDockerLoad(in io.Reader) error { |
| 28 | + subProcess := exec.Command("docker", "load") |
| 29 | + |
| 30 | + stdin, err := subProcess.StdinPipe() |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + defer stdin.Close() |
| 35 | + |
| 36 | + subProcess.Stdout = os.Stdout |
| 37 | + subProcess.Stderr = os.Stderr |
| 38 | + |
| 39 | + if err = subProcess.Start(); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + if _, err = io.Copy(stdin, in); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + stdin.Close() |
| 48 | + |
| 49 | + if err = subProcess.Wait(); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func loadDocker(dir string, dockerImage string) error { |
| 57 | + var imageFile io.Reader |
| 58 | + var err error |
| 59 | + imageFile, err = os.Open(path_filepath.Join(dir, imageFileName(dockerImage))) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("fail to read docker image file %s: %w", dockerImage, err) |
| 62 | + } |
| 63 | + err = runDockerLoad(imageFile) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("fail to load docker image %s: %w", dockerImage, err) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// check if the container either exists and stopped or is running. |
| 72 | +func confirmContainerIsRunningOrExists(containerName string, isRunning bool) (bool, error) { |
| 73 | + // e.g. docker ps --filter name=dapr_redis --filter status=running --format {{.Names}}. |
| 74 | + |
| 75 | + args := []string{"ps", "--all", "--filter", "name=" + containerName} |
| 76 | + |
| 77 | + if isRunning { |
| 78 | + args = append(args, "--filter", "status=running") |
| 79 | + } |
| 80 | + |
| 81 | + args = append(args, "--format", "{{.Names}}") |
| 82 | + response, err := utils.RunCmdAndWait("docker", args...) |
| 83 | + response = strings.TrimSuffix(response, "\n") |
| 84 | + |
| 85 | + // If 'docker ps' failed due to some reason. |
| 86 | + if err != nil { |
| 87 | + //nolint |
| 88 | + return false, fmt.Errorf("unable to confirm whether %s is running or exists. error\n%v", containerName, err.Error()) |
| 89 | + } |
| 90 | + // 'docker ps' worked fine, but the response did not have the container name. |
| 91 | + if response == "" || response != containerName { |
| 92 | + if isRunning { |
| 93 | + return false, fmt.Errorf("container %s is not running", containerName) |
| 94 | + } |
| 95 | + return false, nil |
| 96 | + } |
| 97 | + |
| 98 | + return true, nil |
| 99 | +} |
| 100 | + |
| 101 | +func isContainerRunError(err error) bool { |
| 102 | + //nolint |
| 103 | + if exitError, ok := err.(*exec.ExitError); ok { |
| 104 | + exitCode := exitError.ExitCode() |
| 105 | + return exitCode == 125 |
| 106 | + } |
| 107 | + return false |
| 108 | +} |
| 109 | + |
| 110 | +func parseDockerError(component string, err error) error { |
| 111 | + //nolint |
| 112 | + if exitError, ok := err.(*exec.ExitError); ok { |
| 113 | + exitCode := exitError.ExitCode() |
| 114 | + if exitCode == 125 { // see https://github.com/moby/moby/pull/14012 |
| 115 | + return fmt.Errorf("failed to launch %s. Is it already running?", component) |
| 116 | + } |
| 117 | + if exitCode == 127 { |
| 118 | + return fmt.Errorf("failed to launch %s. Make sure Docker is installed and running", component) |
| 119 | + } |
| 120 | + } |
| 121 | + return err |
| 122 | +} |
| 123 | + |
| 124 | +func imageFileName(image string) string { |
| 125 | + filename := image + ".tar.gz" |
| 126 | + filename = strings.ReplaceAll(filename, "/", "-") |
| 127 | + filename = strings.ReplaceAll(filename, ":", "-") |
| 128 | + return filename |
| 129 | +} |
0 commit comments