func setDebugOutputLevel() { // TODO: I'm not really a fan of this method and really would rather // use -v / --verbose TBQH for _, f := range os.Args { if f == "-D" || f == "--debug" || f == "-debug" { log.SetDebug(true) } } debugEnv := os.Getenv("MACHINE_DEBUG") if debugEnv != "" { showDebug, err := strconv.ParseBool(debugEnv) if err != nil { fmt.Fprintf(os.Stderr, "Error parsing boolean value from MACHINE_DEBUG: %s\n", err) os.Exit(1) } log.SetDebug(showDebug) } }
func NewDockerMachine(config DockerMachineConfig) (DockerMachineAPI, error) { storePath := config.StorePath temp := false if storePath == "" { tempPath, err := ioutil.TempDir("", "") if err != nil { return nil, errors.Wrap(err, "failed to create temp dir") } storePath = tempPath temp = true } certsPath := filepath.Join(storePath, "certs") if _, err := os.Stat(certsPath); os.IsNotExist(err) { err := os.MkdirAll(certsPath, 0700) if err != nil { return nil, errors.WithMessage(err, "failed to create certs dir") } } if config.CaPath != "" { err := mcnutils.CopyFile(filepath.Join(config.CaPath, "ca.pem"), filepath.Join(certsPath, "ca.pem")) if err != nil { return nil, errors.Wrap(err, "failed to copy ca file") } err = mcnutils.CopyFile(filepath.Join(config.CaPath, "ca-key.pem"), filepath.Join(certsPath, "ca-key.pem")) if err != nil { return nil, errors.Wrap(err, "failed to copy ca key file") } } if config.OutWriter != nil { log.SetOutWriter(config.OutWriter) } else { log.SetOutWriter(ioutil.Discard) } if config.ErrWriter != nil { log.SetOutWriter(config.ErrWriter) } else { log.SetOutWriter(ioutil.Discard) } log.SetDebug(config.IsDebug) client := libmachine.NewClient(storePath, certsPath) client.IsDebug = config.IsDebug if _, err := os.Stat(client.GetMachinesDir()); os.IsNotExist(err) { err := os.MkdirAll(client.GetMachinesDir(), 0700) if err != nil { return nil, errors.WithMessage(err, "failed to create machines dir") } } return &DockerMachine{ StorePath: storePath, CertsPath: certsPath, client: client, temp: temp, }, nil }
func main() { log.SetDebug(true) client := libmachine.NewClient("/tmp/automatic", "/tmp/automatic/certs") defer client.Close() hostName := "myfunhost" // Set some options on the provider... driver := virtualbox.NewDriver(hostName, "/tmp/automatic") driver.CPU = 2 driver.Memory = 2048 data, err := json.Marshal(driver) if err != nil { log.Error(err) return } h, err := client.NewHost("virtualbox", data) if err != nil { log.Error(err) return } h.HostOptions.EngineOptions.StorageDriver = "overlay" if err := client.Create(h); err != nil { log.Error(err) return } out, err := h.RunSSHCommand("df -h") if err != nil { log.Error(err) return } fmt.Printf("Results of your disk space query:\n%s\n", out) fmt.Println("Powering down machine now...") if err := h.Stop(); err != nil { log.Error(err) return } }
func RegisterDriver(d drivers.Driver) { if os.Getenv(localbinary.PluginEnvKey) != localbinary.PluginEnvVal { fmt.Fprintf(os.Stderr, `This is a Docker Machine plugin binary. Plugin binaries are not intended to be invoked directly. Please use this plugin through the main 'docker-machine' binary. (API version: %d) `, version.APIVersion) os.Exit(1) } log.SetDebug(true) os.Setenv("MACHINE_DEBUG", "1") rpcd := rpcdriver.NewRPCServerDriver(d) rpc.RegisterName(rpcdriver.RPCServiceNameV0, rpcd) rpc.RegisterName(rpcdriver.RPCServiceNameV1, rpcd) rpc.HandleHTTP() listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { fmt.Fprintf(os.Stderr, "Error loading RPC server: %s\n", err) os.Exit(1) } defer listener.Close() fmt.Println(listener.Addr()) go http.Serve(listener, nil) for { select { case <-rpcd.CloseCh: log.Debug("Closing plugin on server side") os.Exit(0) case <-rpcd.HeartbeatCh: continue case <-time.After(heartbeatTimeout): // TODO: Add heartbeat retry logic os.Exit(1) } } }
// get config for testing // Examples // cv := tc.GetExpectsData("TestGetAPIVersion", "CurrentVersion") // log.Infof("tc test_data -> %s\n", tc.EqualFaceI(cv, 120)) // log.Infof("2 tc compare s -> %s\n", tc.GetExpectsData("TestGetAPIVersion", "FakeData") == "foo") // log.Infof("tc compare s -> %s\n", tc.EqualFaceS(tc.GetExpectsData("TestGetAPIVersion", "FakeData"), "foo")) // log.Infof("get no test data -> %+v \n", tc.GetTestData("TestGetAPIVersion", "Surprise")) // log.Infof("is test enabled -> %+v \n", tc.IsTestEnabled("TestGetAPIVersion")) // func (tc *TestConfig) GetTestingConfiguration(config_name string) { var ( package_root string Pkg PackageInfo test_data_dir string ) if os.Getenv("ONEVIEW_DEBUG") == "true" { log.SetDebug(true) } package_root = os.Getenv("TESTCONFIG_PACKAGE_ROOT_PATH") if found, package_full_dir := Pkg.GetPackageRootDir(package_root); found == true { test_data_dir = Pkg.JoinPath([]string{package_full_dir, os.Getenv("TESTCONFIG_JSON_DATA_DIR")}) if found, _ := Pkg.DirExists(test_data_dir); found == true { files, _ := ioutil.ReadDir(test_data_dir) for _, f := range files { log.Debugf("working on f -> %+v", f) data, err := ioutil.ReadFile(Pkg.JoinPath([]string{test_data_dir, f.Name()})) if err != nil { log.Errorf("File error : %v\n", err) os.Exit(1) } tc.UnMarshallTestingConfig(data) if tc.Name == config_name { log.Debugf("Found test data, %s \n", tc.Name) log.Debugf("tc -> %+v \n", tc) return } } } else { log.Errorf("No configuration found in %s, %s \n", test_data_dir, config_name) } } log.Errorf("No configuration found in %s, %s \n", package_root, config_name) os.Exit(1) return }
func TestExecServer(t *testing.T) { log.SetDebug(true) machineName := "test" logReader, logWriter := io.Pipe() log.SetOutput(logWriter) defer func() { log.SetDebug(false) log.SetOutput(os.Stderr) }() stdoutReader, stdoutWriter := io.Pipe() stderrReader, stderrWriter := io.Pipe() fe := &FakeExecutor{ stdout: stdoutReader, stderr: stderrReader, } lbp := &Plugin{ MachineName: machineName, Executor: fe, addrCh: make(chan string, 1), stopCh: make(chan bool, 1), } finalErr := make(chan error) // Start the docker-machine-foo plugin server go func() { finalErr <- lbp.execServer() }() expectedAddr := "127.0.0.1:12345" expectedPluginOut := "Doing some fun plugin stuff..." expectedPluginErr := "Uh oh, something in plugin went wrong..." logScanner := bufio.NewScanner(logReader) if _, err := io.WriteString(stdoutWriter, expectedAddr+"\n"); err != nil { t.Fatalf("Error attempting to write plugin address: %s", err) } if addr := <-lbp.addrCh; addr != expectedAddr { t.Fatalf("Expected to read the expected address properly in server but did not") } expectedOut := fmt.Sprintf("%s%s", fmt.Sprintf(pluginOutPrefix, machineName), expectedPluginOut) if _, err := io.WriteString(stdoutWriter, expectedPluginOut+"\n"); err != nil { t.Fatalf("Error attempting to write to out in plugin: %s", err) } if logScanner.Scan(); logScanner.Text() != expectedOut { t.Fatalf("Output written to log was not what we expected\nexpected: %s\nactual: %s", expectedOut, logScanner.Text()) } expectedErr := fmt.Sprintf("%s%s", fmt.Sprintf(pluginErrPrefix, machineName), expectedPluginErr) if _, err := io.WriteString(stderrWriter, expectedPluginErr+"\n"); err != nil { t.Fatalf("Error attempting to write to err in plugin: %s", err) } if logScanner.Scan(); logScanner.Text() != expectedErr { t.Fatalf("Error written to log was not what we expected\nexpected: %s\nactual: %s", expectedErr, logScanner.Text()) } lbp.Close() if err := <-finalErr; err != nil { t.Fatalf("Error serving: %s", err) } }
// RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ Use: "minishift", Short: "Minishift is a tool for managing local OpenShift clusters.", Long: `Minishift is a CLI tool that provisions and manages single-node OpenShift clusters optimized for development workflows.`, PersistentPreRun: func(cmd *cobra.Command, args []string) { for _, path := range dirs { if err := os.MkdirAll(path, 0777); err != nil { glog.Exitf("Error creating minishift directory: %s", err) } } shouldShowLibmachineLogs := viper.GetBool(showLibmachineLogs) log.SetDebug(shouldShowLibmachineLogs) if !shouldShowLibmachineLogs { log.SetOutWriter(ioutil.Discard) log.SetErrWriter(ioutil.Discard) } if enableUpdateNotification { update.MaybeUpdateFromGithub(os.Stdout) } }, } // Execute adds all child commands to the root command sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := RootCmd.Execute(); err != nil {
// RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ Use: "minikube", Short: "Minikube is a tool for managing local Kubernetes clusters.", Long: `Minikube is a CLI tool that provisions and manages single-node Kubernetes clusters optimized for development workflows.`, PersistentPreRun: func(cmd *cobra.Command, args []string) { for _, path := range dirs { if err := os.MkdirAll(path, 0777); err != nil { glog.Exitf("Error creating minikube directory: %s", err) } } shouldShowLibmachineLogs := viper.GetBool(showLibmachineLogs) if glog.V(3) { log.SetDebug(true) } if !shouldShowLibmachineLogs { log.SetOutWriter(ioutil.Discard) log.SetErrWriter(ioutil.Discard) } if enableUpdateNotification { notify.MaybePrintUpdateTextFromGithub(os.Stdout) } }, } // Execute adds all child commands to the root command sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() {
// Streaming the output of an SSH session in virtualbox. func streaming() { log.SetDebug(true) client := libmachine.NewClient("/tmp/automatic", "/tmp/automatic/certs") defer client.Close() hostName := "myfunhost" // Set some options on the provider... driver := virtualbox.NewDriver(hostName, "/tmp/automatic") data, err := json.Marshal(driver) if err != nil { log.Error(err) return } h, err := client.NewHost("virtualbox", data) if err != nil { log.Error(err) return } if err := client.Create(h); err != nil { log.Error(err) return } h.HostOptions.EngineOptions.StorageDriver = "overlay" sshClient, err := h.CreateSSHClient() if err != nil { log.Error(err) return } stdout, stderr, err := sshClient.Start("yes | head -n 10000") if err != nil { log.Error(err) return } defer func() { _ = stdout.Close() _ = stderr.Close() }() scanner := bufio.NewScanner(stdout) for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Error(err) } if err := sshClient.Wait(); err != nil { log.Error(err) } fmt.Println("Powering down machine now...") if err := h.Stop(); err != nil { log.Error(err) return } }