// Returns the cert paths. // codegangsta/cli will not set the cert paths if the storage-path is set to // something different so we cannot use the paths in the global options. le // sigh. func getCertPathInfoFromContext(c CommandLine) cert.PathInfo { caCertPath := c.GlobalString("tls-ca-cert") if caCertPath == "" { caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem") } caKeyPath := c.GlobalString("tls-ca-key") if caKeyPath == "" { caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem") } clientCertPath := c.GlobalString("tls-client-cert") if clientCertPath == "" { clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem") } clientKeyPath := c.GlobalString("tls-client-key") if clientKeyPath == "" { clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem") } return cert.PathInfo{ CaCertPath: caCertPath, CaPrivateKeyPath: caKeyPath, ClientCertPath: clientCertPath, ClientKeyPath: clientKeyPath, } }
func fatalOnError(command func(commandLine CommandLine, api libmachine.API) error) func(context *cli.Context) { return func(context *cli.Context) { api := libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) defer api.Close() if context.GlobalBool("native-ssh") { api.SSHClientType = ssh.Native } api.GithubAPIToken = context.GlobalString("github-api-token") api.Filestore.Path = context.GlobalString("storage-path") // TODO (nathanleclaire): These should ultimately be accessed // through the libmachine client by the rest of the code and // not through their respective modules. For now, however, // they are also being set the way that they originally were // set to preserve backwards compatibility. mcndirs.BaseDir = api.Filestore.Path mcnutils.GithubAPIToken = api.GithubAPIToken ssh.SetDefaultClient(api.SSHClientType) if err := command(&contextCommandLine{context}, api); err != nil { log.Fatal(err) if crashErr, ok := err.(crashreport.CrashError); ok { crashReporter := crashreport.NewCrashReporter(mcndirs.GetBaseDir(), context.GlobalString("bugsnag-api-token")) crashReporter.Send(crashErr) } } } }
func run() error { machineName := flag.String("machine", "default", "Docker machine name") addr := flag.String("addr", "localhost:2375", "Address of the proxy") help := flag.Bool("help", false, "Show help") flag.Parse() if *help { flag.Usage() return nil } api := libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) defer api.Close() machine, err := api.Load(*machineName) if err != nil { return err } url, err := machine.URL() if err != nil { return err } return http.ListenAndServe(*addr, &proxy.DockerMachineProxy{ Machine: &mcn.DockerMachine{ Url: url, CertPath: filepath.Join(mcndirs.GetBaseDir(), "machines", *machineName), }, }) }
func tlsPath(c CommandLine, flag string, defaultName string) string { path := c.GlobalString(flag) if path != "" { return path } return filepath.Join(mcndirs.GetMachineCertDir(), defaultName) }
func WithApi(handler Handler, args map[string]string, form map[string][]string) func() (interface{}, error) { return func() (interface{}, error) { globalMutex.Lock() defer globalMutex.Unlock() api := libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) defer api.Close() return handler.Handle(api, args, form) } }
// findIP finds the IP address of a Docker Machine host given its name. func findIP(name string) (net.IP, error) { log.Println("Looking for virtualbox machine", name) api := libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) defer api.Close() machine, err := api.Load(name) if err != nil { return nil, err } driver := machine.Driver.DriverName() ip, err := machine.Driver.GetIP() if err != nil { return nil, err } log.Printf("Found %s(%s) with IP %s\n", name, driver, ip) return net.ParseIP(ip), nil }
func (d *DockerMachineFsNotify) NotifyVm(event FsEvent) error { api := libmachine.NewClient(mcndirs.GetBaseDir(), mcndirs.GetMachineCertDir()) host, err := api.Load(d.DockerMachineName) if err != nil { return err } currentState, err := host.Driver.GetState() if err != nil { return err } if currentState != state.Running { return errors.New("Docker Machine " + host.Name + " is not running") } client, err := host.CreateSSHClient() if err != nil { return err } return client.Shell("touch -t " + event.ModTime.UTC().Format(touchTimeFormat) + " -m -c " + event.File) }
func cmdCreateInner(c CommandLine) error { if len(c.Args()) > 1 { return fmt.Errorf("Invalid command line. Found extra arguments %v", c.Args()[1:]) } name := c.Args().First() driverName := c.String("driver") certInfo := getCertPathInfoFromContext(c) storePath := c.GlobalString("storage-path") store := &persist.Filestore{ Path: storePath, CaCertPath: certInfo.CaCertPath, CaPrivateKeyPath: certInfo.CaPrivateKeyPath, } if name == "" { c.ShowHelp() return errNoMachineName } validName := host.ValidateHostName(name) if !validName { return fmt.Errorf("Error creating machine: %s", mcnerror.ErrInvalidHostname) } if err := validateSwarmDiscovery(c.String("swarm-discovery")); err != nil { return fmt.Errorf("Error parsing swarm discovery: %s", err) } // TODO: Fix hacky JSON solution bareDriverData, err := json.Marshal(&drivers.BaseDriver{ MachineName: name, StorePath: c.GlobalString("storage-path"), }) if err != nil { return fmt.Errorf("Error attempting to marshal bare driver data: %s", err) } driver, err := newPluginDriver(driverName, bareDriverData) if err != nil { return fmt.Errorf("Error loading driver %q: %s", driverName, err) } h, err := store.NewHost(driver) if err != nil { return fmt.Errorf("Error getting new host: %s", err) } h.HostOptions = &host.Options{ AuthOptions: &auth.Options{ CertDir: mcndirs.GetMachineCertDir(), CaCertPath: certInfo.CaCertPath, CaPrivateKeyPath: certInfo.CaPrivateKeyPath, ClientCertPath: certInfo.ClientCertPath, ClientKeyPath: certInfo.ClientKeyPath, ServerCertPath: filepath.Join(mcndirs.GetMachineDir(), name, "server.pem"), ServerKeyPath: filepath.Join(mcndirs.GetMachineDir(), name, "server-key.pem"), StorePath: filepath.Join(mcndirs.GetMachineDir(), name), }, EngineOptions: &engine.Options{ ArbitraryFlags: c.StringSlice("engine-opt"), Env: c.StringSlice("engine-env"), InsecureRegistry: c.StringSlice("engine-insecure-registry"), Labels: c.StringSlice("engine-label"), RegistryMirror: c.StringSlice("engine-registry-mirror"), StorageDriver: c.String("engine-storage-driver"), TLSVerify: true, InstallURL: c.String("engine-install-url"), }, SwarmOptions: &swarm.Options{ IsSwarm: c.Bool("swarm"), Image: c.String("swarm-image"), Master: c.Bool("swarm-master"), Discovery: c.String("swarm-discovery"), Address: c.String("swarm-addr"), Host: c.String("swarm-host"), Strategy: c.String("swarm-strategy"), ArbitraryFlags: c.StringSlice("swarm-opt"), }, } exists, err := store.Exists(h.Name) if err != nil { return fmt.Errorf("Error checking if host exists: %s", err) } if exists { return mcnerror.ErrHostAlreadyExists{ Name: h.Name, } } // driverOpts is the actual data we send over the wire to set the // driver parameters (an interface fulfilling drivers.DriverOptions, // concrete type rpcdriver.RpcFlags). mcnFlags := driver.GetCreateFlags() driverOpts := getDriverOpts(c, mcnFlags) if err := h.Driver.SetConfigFromFlags(driverOpts); err != nil { return fmt.Errorf("Error setting machine configuration from flags provided: %s", err) } if err := libmachine.Create(store, h); err != nil { return fmt.Errorf("Error creating machine: %s", err) } if err := saveHost(store, h); err != nil { return fmt.Errorf("Error attempting to save store: %s", err) } log.Infof("To see how to connect Docker to this machine, run: %s", fmt.Sprintf("%s env %s", os.Args[0], name)) return nil }
func cmdCreateInner(c CommandLine, api libmachine.API) error { if len(c.Args()) > 1 { return fmt.Errorf("Invalid command line. Found extra arguments %v", c.Args()[1:]) } name := c.Args().First() if name == "" { c.ShowHelp() return errNoMachineName } validName := host.ValidateHostName(name) if !validName { return fmt.Errorf("Error creating machine: %s", mcnerror.ErrInvalidHostname) } if err := validateSwarmDiscovery(c.String("swarm-discovery")); err != nil { return fmt.Errorf("Error parsing swarm discovery: %s", err) } // TODO: Fix hacky JSON solution rawDriver, err := json.Marshal(&drivers.BaseDriver{ MachineName: name, StorePath: c.GlobalString("storage-path"), }) if err != nil { return fmt.Errorf("Error attempting to marshal bare driver data: %s", err) } driverName := c.String("driver") h, err := api.NewHost(driverName, rawDriver) if err != nil { return fmt.Errorf("Error getting new host: %s", err) } h.HostOptions = &host.Options{ AuthOptions: &auth.Options{ CertDir: mcndirs.GetMachineCertDir(), CaCertPath: tlsPath(c, "tls-ca-cert", "ca.pem"), CaPrivateKeyPath: tlsPath(c, "tls-ca-key", "ca-key.pem"), ClientCertPath: tlsPath(c, "tls-client-cert", "cert.pem"), ClientKeyPath: tlsPath(c, "tls-client-key", "key.pem"), ServerCertPath: filepath.Join(mcndirs.GetMachineDir(), name, "server.pem"), ServerKeyPath: filepath.Join(mcndirs.GetMachineDir(), name, "server-key.pem"), StorePath: filepath.Join(mcndirs.GetMachineDir(), name), ServerCertSANs: c.StringSlice("tls-san"), }, EngineOptions: &engine.Options{ ArbitraryFlags: c.StringSlice("engine-opt"), Env: c.StringSlice("engine-env"), InsecureRegistry: c.StringSlice("engine-insecure-registry"), Labels: c.StringSlice("engine-label"), RegistryMirror: c.StringSlice("engine-registry-mirror"), StorageDriver: c.String("engine-storage-driver"), TLSVerify: true, InstallURL: c.String("engine-install-url"), }, SwarmOptions: &swarm.Options{ IsSwarm: c.Bool("swarm") || c.Bool("swarm-master"), Image: c.String("swarm-image"), Agent: c.Bool("swarm"), Master: c.Bool("swarm-master"), Discovery: c.String("swarm-discovery"), Address: c.String("swarm-addr"), Host: c.String("swarm-host"), Strategy: c.String("swarm-strategy"), ArbitraryFlags: c.StringSlice("swarm-opt"), ArbitraryJoinFlags: c.StringSlice("swarm-join-opt"), IsExperimental: c.Bool("swarm-experimental"), }, } exists, err := api.Exists(h.Name) if err != nil { return fmt.Errorf("Error checking if host exists: %s", err) } if exists { return mcnerror.ErrHostAlreadyExists{ Name: h.Name, } } // driverOpts is the actual data we send over the wire to set the // driver parameters (an interface fulfilling drivers.DriverOptions, // concrete type rpcdriver.RpcFlags). mcnFlags := h.Driver.GetCreateFlags() driverOpts := getDriverOpts(c, mcnFlags) if err := h.Driver.SetConfigFromFlags(driverOpts); err != nil { return fmt.Errorf("Error setting machine configuration from flags provided: %s", err) } if err := api.Create(h); err != nil { // Wait for all the logs to reach the client time.Sleep(2 * time.Second) vBoxLog := "" if h.DriverName == "virtualbox" { vBoxLog = filepath.Join(api.GetMachinesDir(), h.Name, h.Name, "Logs", "VBox.log") } return crashreport.CrashError{ Cause: err, Command: "Create", Context: "api.performCreate", DriverName: h.DriverName, LogFilePath: vBoxLog, } } if err := api.Save(h); err != nil { return fmt.Errorf("Error attempting to save store: %s", err) } log.Infof("To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: %s env %s", os.Args[0], name) return nil }
func cmdCreate(c *cli.Context) { var ( driver drivers.Driver ) driverName := c.String("driver") name := c.Args().First() certInfo := getCertPathInfoFromContext(c) storePath := c.GlobalString("storage-path") store := &persist.Filestore{ Path: storePath, CaCertPath: certInfo.CaCertPath, CaPrivateKeyPath: certInfo.CaPrivateKeyPath, } // TODO: Not really a fan of "none" as the default driver... if driverName != "none" { var err error c.App.Commands, err = trimDriverFlags(driverName, c.App.Commands) if err != nil { log.Fatal(err) } } if name == "" { cli.ShowCommandHelp(c, "create") log.Fatal("You must specify a machine name") } if len(c.Args()) > 1 { log.Fatalf("Invalid command line. Found extra arguments %v", c.Args()[1:]) } validName := host.ValidateHostName(name) if !validName { log.Fatal("Error creating machine: ", mcnerror.ErrInvalidHostname) } if err := validateSwarmDiscovery(c.String("swarm-discovery")); err != nil { log.Fatalf("Error parsing swarm discovery: %s", err) } hostOptions := &host.HostOptions{ AuthOptions: &auth.AuthOptions{ CertDir: mcndirs.GetMachineCertDir(), CaCertPath: certInfo.CaCertPath, CaPrivateKeyPath: certInfo.CaPrivateKeyPath, ClientCertPath: certInfo.ClientCertPath, ClientKeyPath: certInfo.ClientKeyPath, ServerCertPath: filepath.Join(mcndirs.GetMachineDir(), name, "server.pem"), ServerKeyPath: filepath.Join(mcndirs.GetMachineDir(), name, "server-key.pem"), StorePath: filepath.Join(mcndirs.GetMachineDir(), name), }, EngineOptions: &engine.EngineOptions{ ArbitraryFlags: c.StringSlice("engine-opt"), Env: c.StringSlice("engine-env"), InsecureRegistry: c.StringSlice("engine-insecure-registry"), Labels: c.StringSlice("engine-label"), RegistryMirror: c.StringSlice("engine-registry-mirror"), StorageDriver: c.String("engine-storage-driver"), TlsVerify: true, InstallURL: c.String("engine-install-url"), }, SwarmOptions: &swarm.SwarmOptions{ IsSwarm: c.Bool("swarm"), Image: c.String("swarm-image"), Master: c.Bool("swarm-master"), Discovery: c.String("swarm-discovery"), Address: c.String("swarm-addr"), Host: c.String("swarm-host"), Strategy: c.String("swarm-strategy"), ArbitraryFlags: c.StringSlice("swarm-opt"), }, } driver, err := driverfactory.NewDriver(driverName, name, storePath) if err != nil { log.Fatalf("Error trying to get driver: %s", err) } h, err := store.NewHost(driver) if err != nil { log.Fatalf("Error getting new host: %s", err) } h.HostOptions = hostOptions exists, err := store.Exists(h.Name) if err != nil { log.Fatalf("Error checking if host exists: %s", err) } if exists { log.Fatal(mcnerror.ErrHostAlreadyExists{ Name: h.Name, }) } // TODO: This should be moved out of the driver and done in the // commands module. if err := h.Driver.SetConfigFromFlags(c); err != nil { log.Fatalf("Error setting machine configuration from flags provided: %s", err) } if err := libmachine.Create(store, h); err != nil { log.Fatalf("Error creating machine: %s", err) } info := fmt.Sprintf("%s env %s", os.Args[0], name) log.Infof("To see how to connect Docker to this machine, run: %s", info) }
func createMachine(api libmachine.API, name string, driver string, form map[string][]string) error { validName := host.ValidateHostName(name) if !validName { return fmt.Errorf("Error creating machine: %s", mcnerror.ErrInvalidHostname) } exists, err := api.Exists(name) if err != nil { return fmt.Errorf("Error checking if host exists: %s", err) } if exists { return mcnerror.ErrHostAlreadyExists{ Name: name, } } rawDriver, err := json.Marshal(&drivers.BaseDriver{ MachineName: name, StorePath: mcndirs.GetBaseDir(), }) if err != nil { return fmt.Errorf("Error attempting to marshal bare driver data: %s", err) } h, err := api.NewHost(driver, rawDriver) if err != nil { return err } globalOpts := globalFlags{ flags: form, } h.HostOptions = &host.Options{ AuthOptions: &auth.Options{ CertDir: mcndirs.GetMachineCertDir(), CaCertPath: filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem"), CaPrivateKeyPath: filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem"), ClientCertPath: filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem"), ClientKeyPath: filepath.Join(mcndirs.GetMachineCertDir(), "key.pem"), ServerCertPath: filepath.Join(mcndirs.GetMachineDir(), name, "server.pem"), ServerKeyPath: filepath.Join(mcndirs.GetMachineDir(), name, "server-key.pem"), StorePath: filepath.Join(mcndirs.GetMachineDir(), name), ServerCertSANs: globalOpts.StringSlice("tls-san"), }, EngineOptions: &engine.Options{ ArbitraryFlags: globalOpts.StringSlice("engine-opt"), Env: globalOpts.StringSlice("engine-env"), InsecureRegistry: globalOpts.StringSlice("engine-insecure-registry"), Labels: globalOpts.StringSlice("engine-label"), RegistryMirror: globalOpts.StringSlice("engine-registry-mirror"), StorageDriver: globalOpts.String("engine-storage-driver"), TLSVerify: true, InstallURL: globalOpts.String("engine-install-url"), }, SwarmOptions: &swarm.Options{ IsSwarm: globalOpts.Bool("swarm"), Image: globalOpts.String("swarm-image"), Master: globalOpts.Bool("swarm-master"), Discovery: globalOpts.String("swarm-discovery"), Address: globalOpts.String("swarm-addr"), Host: globalOpts.String("swarm-host"), Strategy: globalOpts.String("swarm-strategy"), ArbitraryFlags: globalOpts.StringSlice("swarm-opt"), IsExperimental: globalOpts.Bool("swarm-experimental"), }, } mcnFlags := h.Driver.GetCreateFlags() opts, err := parseFlags(form, mcnFlags, commands.SharedCreateFlags) if err != nil { return err } if err := h.Driver.SetConfigFromFlags(opts); err != nil { return fmt.Errorf("Error setting machine configuration from flags provided: %s", err) } if err := api.Create(h); err != nil { return err } if err := api.Save(h); err != nil { return fmt.Errorf("Error attempting to save store: %s", err) } return nil }