Skip to content

Commit

Permalink
Merge branch 'master' into feat/interact-with-kubernetes-dapr-apps
Browse files Browse the repository at this point in the history
  • Loading branch information
imneov authored Mar 22, 2022
2 parents 9c87b6a + f1c9ee2 commit 8d399b4
Show file tree
Hide file tree
Showing 3 changed files with 406 additions and 195 deletions.
4 changes: 3 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
enableMTLS bool
enableHA bool
values []string
fromDir string
)

var InitCmd = &cobra.Command{
Expand Down Expand Up @@ -97,7 +98,7 @@ dapr init -s
dockerNetwork = viper.GetString("network")
imageRepositoryURL = viper.GetString("image-repository")
}
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRepositoryURL)
err := standalone.Init(runtimeVersion, dashboardVersion, dockerNetwork, slimMode, imageRepositoryURL, fromDir)
if err != nil {
print.FailureStatusEvent(os.Stderr, err.Error())
os.Exit(1)
Expand Down Expand Up @@ -130,6 +131,7 @@ func init() {
InitCmd.Flags().BoolVarP(&enableMTLS, "enable-mtls", "", true, "Enable mTLS in your cluster")
InitCmd.Flags().BoolVarP(&enableHA, "enable-ha", "", false, "Enable high availability (HA) mode")
InitCmd.Flags().String("network", "", "The Docker network on which to deploy the Dapr runtime")
InitCmd.Flags().StringVarP(&fromDir, "from-dir", "", "", "Use Dapr artifacts from local directory instead of from network to init")
InitCmd.Flags().BoolP("help", "h", false, "Print this help message")
InitCmd.Flags().StringArrayVar(&values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
InitCmd.Flags().String("image-repository", "", "Custom/Private docker image repository url")
Expand Down
129 changes: 129 additions & 0 deletions pkg/standalone/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package standalone

import (
"fmt"
"io"
"os"
"os/exec"
path_filepath "path/filepath"
"strings"

"github.com/dapr/cli/utils"
)

func runDockerLoad(in io.Reader) error {
subProcess := exec.Command("docker", "load")

stdin, err := subProcess.StdinPipe()
if err != nil {
return err
}
defer stdin.Close()

subProcess.Stdout = os.Stdout
subProcess.Stderr = os.Stderr

if err = subProcess.Start(); err != nil {
return err
}

if _, err = io.Copy(stdin, in); err != nil {
return err
}

stdin.Close()

if err = subProcess.Wait(); err != nil {
return err
}

return nil
}

func loadDocker(dir string, dockerImage string) error {
var imageFile io.Reader
var err error
imageFile, err = os.Open(path_filepath.Join(dir, imageFileName(dockerImage)))
if err != nil {
return fmt.Errorf("fail to read docker image file %s: %w", dockerImage, err)
}
err = runDockerLoad(imageFile)
if err != nil {
return fmt.Errorf("fail to load docker image %s: %w", dockerImage, err)
}

return nil
}

// check if the container either exists and stopped or is running.
func confirmContainerIsRunningOrExists(containerName string, isRunning bool) (bool, error) {
// e.g. docker ps --filter name=dapr_redis --filter status=running --format {{.Names}}.

args := []string{"ps", "--all", "--filter", "name=" + containerName}

if isRunning {
args = append(args, "--filter", "status=running")
}

args = append(args, "--format", "{{.Names}}")
response, err := utils.RunCmdAndWait("docker", args...)
response = strings.TrimSuffix(response, "\n")

// If 'docker ps' failed due to some reason.
if err != nil {
//nolint
return false, fmt.Errorf("unable to confirm whether %s is running or exists. error\n%v", containerName, err.Error())
}
// 'docker ps' worked fine, but the response did not have the container name.
if response == "" || response != containerName {
if isRunning {
return false, fmt.Errorf("container %s is not running", containerName)
}
return false, nil
}

return true, nil
}

func isContainerRunError(err error) bool {
//nolint
if exitError, ok := err.(*exec.ExitError); ok {
exitCode := exitError.ExitCode()
return exitCode == 125
}
return false
}

func parseDockerError(component string, err error) error {
//nolint
if exitError, ok := err.(*exec.ExitError); ok {
exitCode := exitError.ExitCode()
if exitCode == 125 { // see https://github.com/moby/moby/pull/14012
return fmt.Errorf("failed to launch %s. Is it already running?", component)
}
if exitCode == 127 {
return fmt.Errorf("failed to launch %s. Make sure Docker is installed and running", component)
}
}
return err
}

func imageFileName(image string) string {
filename := image + ".tar.gz"
filename = strings.ReplaceAll(filename, "/", "-")
filename = strings.ReplaceAll(filename, ":", "-")
return filename
}
Loading

0 comments on commit 8d399b4

Please sign in to comment.