Skip to content

Commit 8d399b4

Browse files
authored
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
2 parents 9c87b6a + f1c9ee2 commit 8d399b4

File tree

3 files changed

+406
-195
lines changed

3 files changed

+406
-195
lines changed

cmd/init.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
enableMTLS bool
3737
enableHA bool
3838
values []string
39+
fromDir string
3940
)
4041

4142
var InitCmd = &cobra.Command{
@@ -97,7 +98,7 @@ dapr init -s
9798
dockerNetwork = viper.GetString("network")
9899
imageRepositoryURL = viper.GetString("image-repository")
99100
}
100-
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRepositoryURL)
101+
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRepositoryURL, fromDir)
101102
if err != nil {
102103
print.FailureStatusEvent(os.Stderr, err.Error())
103104
os.Exit(1)
@@ -130,6 +131,7 @@ func init() {
130131
InitCmd.Flags().BoolVarP(&enableMTLS, "enable-mtls", "", true, "Enable mTLS in your cluster")
131132
InitCmd.Flags().BoolVarP(&enableHA, "enable-ha", "", false, "Enable high availability (HA) mode")
132133
InitCmd.Flags().String("network", "", "The Docker network on which to deploy the Dapr runtime")
134+
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory instead of from network to init")
133135
InitCmd.Flags().BoolP("help", "h", false, "Print this help message")
134136
InitCmd.Flags().StringArrayVar(&values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
135137
InitCmd.Flags().String("image-repository", "", "Custom/Private docker image repository url")

pkg/standalone/docker.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)