From 57e20ceb57732f14d0089eb331ee4fc8ef971c86 Mon Sep 17 00:00:00 2001 From: vinayada1 <28875764+vinayada1@users.noreply.github.com> Date: Thu, 28 May 2020 13:19:40 -0700 Subject: [PATCH] tested on mac --- pkg/standalone/run.go | 42 +++++++++++-------------------- pkg/standalone/standalone.go | 49 +++++++++++++++++++++++++++++++----- pkg/standalone/uninstall.go | 17 ++++++++++++- pkg/version/version.go | 10 +++----- utils/utils.go | 29 +++++++++++++++++++++ 5 files changed, 105 insertions(+), 42 deletions(-) diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index 65ffec95..ef997a86 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -25,7 +25,6 @@ import ( ) const ( - componentsDirName = "components" messageBusYamlFileName = "pubsub.yaml" stateStoreYamlFileName = "statestore.yaml" sentryDefaultAddress = "localhost:50001" @@ -183,15 +182,6 @@ func getAppCommand(httpPort, grpcPort, metricsPort int, command string, args []s return cmd, nil } -func absoluteComponentsDir() (string, error) { - wd, err := os.Getwd() - if err != nil { - return "", err - } - - return filepath.Join(wd, componentsDirName), nil -} - func createRedisStateStore(redisHost string, componentsPath string) error { redisStore := component{ APIVersion: "dapr.io/v1alpha1", @@ -281,24 +271,9 @@ func Run(config *RunConfig) (*RunOutput, error) { } } - var componentsPath string - - if config.ComponentsPath == "" { - componentsPath, err = absoluteComponentsDir() - if err != nil { - return nil, err - } - - err = utils.CreateDirectory(componentsPath) - if err != nil { - return nil, err - } - } else { - _, err = os.Stat(config.ComponentsPath) - if os.IsNotExist(err) { - return nil, err - } - componentsPath = config.ComponentsPath + componentsPath, err := getComponentsPath(config) + if err != nil { + return nil, err } componentsLoader := components.NewStandaloneComponents(modes.StandaloneConfig{ComponentsPath: componentsPath}) @@ -369,3 +344,14 @@ func Run(config *RunConfig) (*RunOutput, error) { DaprGRPCPort: daprGRPCPort, }, nil } + +func getComponentsPath(config *RunConfig) (string, error) { + if config.ComponentsPath == "" { + componentsPath, err := utils.GetDefaultComponentsFolder() + fmt.Println("Read components env: ", componentsPath) + return componentsPath, err + } + + _, err := os.Stat(config.ComponentsPath) + return config.ComponentsPath, err +} diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index d189a196..49c2ab7e 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -18,6 +18,7 @@ import ( "os/exec" "os/user" "path" + "path/filepath" path_filepath "path/filepath" "runtime" "strings" @@ -374,7 +375,10 @@ func installDaprBinary(wg *sync.WaitGroup, errorChan chan<- error, dir, version return } - daprPath, err := moveFileToPath(extractedFilePath, installLocation) + destDir := getDestDir(installLocation) + fmt.Println("destDir: ", destDir) + + daprPath, err := moveFileToPath(extractedFilePath, installLocation, destDir) if err != nil { errorChan <- fmt.Errorf("error moving Dapr binary to path: %s", err) return @@ -386,9 +390,39 @@ func installDaprBinary(wg *sync.WaitGroup, errorChan chan<- error, dir, version return } + fmt.Println("Trying to create components folder with input dir: ", destDir) + err = createComponentsDir(destDir) + if err != nil { + errorChan <- fmt.Errorf("error creating default components folder: %s", err) + return + } errorChan <- nil } +func createComponentsDir(daprPath string) error { + + // Make default components directory under install path + componentsDir := filepath.Join(daprPath, utils.ComponentsDirName) + fmt.Printf("default install location: %s\ncomponents folder location: %s\n", daprPath, componentsDir) + fmt.Println("Creating default components dir: ", componentsDir) + _, err := os.Stat(componentsDir) + if os.IsNotExist(err) { + errDir := os.MkdirAll(componentsDir, 0755) + if errDir != nil { + return errDir + } + + if runtime.GOOS == daprWindowsOS { + _, err := utils.RunCmdAndWait("ATTRIB", "+s +h", componentsDir) + if err != nil { + return err + } + } + } + + return nil +} + func makeExecutable(filepath string) error { if runtime.GOOS != daprWindowsOS { err := os.Chmod(filepath, 0777) @@ -492,21 +526,24 @@ func untar(filepath, targetDir string) (string, error) { } } -func moveFileToPath(filepath string, installLocation string) (string, error) { +func getDestDir(installLocation string) string { destDir := daprDefaultLinuxAndMacInstallPath if runtime.GOOS == daprWindowsOS { destDir = daprDefaultWindowsInstallPath - filepath = strings.Replace(filepath, "/", "\\", -1) } - fileName := path_filepath.Base(filepath) - destFilePath := "" - // if user specified --install-path, use that if installLocation != "" { destDir = installLocation } + return destDir +} + +func moveFileToPath(filepath string, installLocation string, destDir string) (string, error) { + fileName := path_filepath.Base(filepath) + destFilePath := "" + destFilePath = path.Join(destDir, fileName) input, err := ioutil.ReadFile(filepath) diff --git a/pkg/standalone/uninstall.go b/pkg/standalone/uninstall.go index 2fb2a866..00a30dff 100644 --- a/pkg/standalone/uninstall.go +++ b/pkg/standalone/uninstall.go @@ -3,6 +3,7 @@ package standalone import ( "errors" "fmt" + "os" "github.com/dapr/cli/pkg/rundata" "github.com/dapr/cli/utils" @@ -48,7 +49,15 @@ func removeContainers(uninstallAll bool, dockerNetwork string) []error { return containerErrs } -// Uninstall deletes all installed containers +func removeDefaultComponentsFolder() error { + defaultComponentsPath, err := utils.GetDefaultComponentsFolder() + fmt.Println("Cleaning up default Components folder: ", defaultComponentsPath) + err = os.RemoveAll(defaultComponentsPath) + + return err +} + +// Uninstall reverts all changes made by init. Deletes all installed containers, removes default components folder, unsets env variables func Uninstall(uninstallAll bool, dockerNetwork string) error { var containerErrs []error @@ -62,6 +71,12 @@ func Uninstall(uninstallAll bool, dockerNetwork string) error { fmt.Println("WARNING: could not delete run data file") } + fmt.Println("Removing default components folder") + err = removeDefaultComponentsFolder() + if err != nil { + fmt.Println("WARNING: could not delete default components folder") + } + err = errors.New("uninstall failed") if !dockerInstalled { return fmt.Errorf("%w \n could not connect to Docker. Docker may not be installed or running", err) diff --git a/pkg/version/version.go b/pkg/version/version.go index 68348965..ccf57f4f 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -11,8 +11,9 @@ import ( "io/ioutil" "net/http" "os/exec" - "runtime" "strings" + + "github.com/dapr/cli/utils" ) const ( @@ -24,12 +25,7 @@ const ( // GetRuntimeVersion returns the version for the local Dapr runtime. func GetRuntimeVersion() string { - runtimeName := "" - if runtime.GOOS == "windows" { - runtimeName = "daprd.exe" - } else { - runtimeName = "daprd" - } + runtimeName := utils.GetDaprRuntimeName() out, err := exec.Command(runtimeName, "--version").Output() if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 999e4654..9fd8537e 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -13,12 +13,17 @@ import ( "io/ioutil" "os" "os/exec" + path_filepath "path/filepath" + "runtime" "strings" "github.com/docker/docker/client" "github.com/olekukonko/tablewriter" ) +// ComponentsDirName is the default hidden components folder name created at init time +const ComponentsDirName = ".components" + // PrintTable to print in the table format func PrintTable(csvContent string) { table := tablewriter.NewWriter(os.Stdout) @@ -118,3 +123,27 @@ func IsDockerInstalled() bool { _, err = cli.Ping(context.Background()) return err == nil } + +// GetDaprRuntimeName returns the name of the dapr runtime binary +func GetDaprRuntimeName() string { + runtimeName := "" + if runtime.GOOS == "windows" { + runtimeName = "daprd.exe" + } else { + runtimeName = "daprd" + } + + return runtimeName +} + +// GetDefaultComponentsFolder returns the hidden .components folder created under install directory at init time +func GetDefaultComponentsFolder() (string, error) { + daprBinaryName := GetDaprRuntimeName() + daprRuntimePath, err := RunCmdAndWait("which", daprBinaryName) + if err != nil { + return "", err + } + + defaultComponentsPath := path_filepath.Join(daprRuntimePath[0:len(daprRuntimePath)-len(daprBinaryName)-1], ComponentsDirName) + return defaultComponentsPath, err +}