From 3d9f1ed8e8333561c0f5c2bf2513213492d68fd1 Mon Sep 17 00:00:00 2001 From: Simon Thulbourn Date: Mon, 2 Feb 2015 14:22:32 +0000 Subject: [PATCH] move cert creation to create commandw Signed-off-by: Simon Thulbourn --- commands.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 64 ----------------------------------------------------- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/commands.go b/commands.go index 14ca71e5b7..7333d327b3 100644 --- a/commands.go +++ b/commands.go @@ -59,6 +59,69 @@ func (h hostListItemByName) Less(i, j int) bool { return strings.ToLower(h[i].Name) < strings.ToLower(h[j].Name) } +func beforeCreate(c *cli.Context) error { + caCertPath := c.GlobalString("tls-ca-cert") + caKeyPath := c.GlobalString("tls-ca-key") + clientCertPath := c.GlobalString("tls-client-cert") + clientKeyPath := c.GlobalString("tls-client-key") + + org := utils.GetUsername() + bits := 2048 + + if _, err := os.Stat(utils.GetMachineDir()); err != nil { + if os.IsNotExist(err) { + if err := os.MkdirAll(utils.GetMachineDir(), 0700); err != nil { + log.Fatalf("Error creating machine config dir: %s", err) + } + } else { + log.Fatal(err) + } + } + + if _, err := os.Stat(caCertPath); os.IsNotExist(err) { + log.Infof("Creating CA: %s", caCertPath) + + // check if the key path exists; if so, error + if _, err := os.Stat(caKeyPath); err == nil { + log.Fatalf("The CA key already exists. Please remove it or specify a different key/cert.") + } + + if err := utils.GenerateCACertificate(caCertPath, caKeyPath, org, bits); err != nil { + log.Infof("Error generating CA certificate: %s", err) + } + } + + if _, err := os.Stat(clientCertPath); os.IsNotExist(err) { + log.Infof("Creating client certificate: %s", clientCertPath) + + if _, err := os.Stat(utils.GetMachineClientCertDir()); err != nil { + if os.IsNotExist(err) { + if err := os.Mkdir(utils.GetMachineClientCertDir(), 0700); err != nil { + log.Fatalf("Error creating machine client cert dir: %s", err) + } + } else { + log.Fatal(err) + } + } + + // check if the key path exists; if so, error + if _, err := os.Stat(clientKeyPath); err == nil { + log.Fatalf("The client key already exists. Please remove it or specify a different key/cert.") + } + + if err := utils.GenerateCert([]string{""}, clientCertPath, clientKeyPath, caCertPath, caKeyPath, org, bits); err != nil { + log.Fatalf("Error generating client certificate: %s", err) + } + + // copy ca.pem to client cert dir for docker client + if err := utils.CopyFile(caCertPath, filepath.Join(utils.GetMachineClientCertDir(), "ca.pem")); err != nil { + log.Fatalf("Error copying ca.pem to client cert dir: %s", err) + } + } + + return nil +} + var Commands = []cli.Command{ { Name: "active", @@ -66,6 +129,7 @@ var Commands = []cli.Command{ Action: cmdActive, }, { + Before: beforeCreate, Flags: append( drivers.GetCreateFlags(), cli.StringFlag{ diff --git a/main.go b/main.go index f06d0ae1ad..8383f06494 100644 --- a/main.go +++ b/main.go @@ -10,69 +10,6 @@ import ( "github.com/docker/machine/utils" ) -func before(c *cli.Context) error { - caCertPath := c.GlobalString("tls-ca-cert") - caKeyPath := c.GlobalString("tls-ca-key") - clientCertPath := c.GlobalString("tls-client-cert") - clientKeyPath := c.GlobalString("tls-client-key") - - org := utils.GetUsername() - bits := 2048 - - if _, err := os.Stat(utils.GetMachineDir()); err != nil { - if os.IsNotExist(err) { - if err := os.MkdirAll(utils.GetMachineDir(), 0700); err != nil { - log.Fatalf("Error creating machine config dir: %s", err) - } - } else { - log.Fatal(err) - } - } - - if _, err := os.Stat(caCertPath); os.IsNotExist(err) { - log.Infof("Creating CA: %s", caCertPath) - - // check if the key path exists; if so, error - if _, err := os.Stat(caKeyPath); err == nil { - log.Fatalf("The CA key already exists. Please remove it or specify a different key/cert.") - } - - if err := utils.GenerateCACertificate(caCertPath, caKeyPath, org, bits); err != nil { - log.Infof("Error generating CA certificate: %s", err) - } - } - - if _, err := os.Stat(clientCertPath); os.IsNotExist(err) { - log.Infof("Creating client certificate: %s", clientCertPath) - - if _, err := os.Stat(utils.GetMachineClientCertDir()); err != nil { - if os.IsNotExist(err) { - if err := os.Mkdir(utils.GetMachineClientCertDir(), 0700); err != nil { - log.Fatalf("Error creating machine client cert dir: %s", err) - } - } else { - log.Fatal(err) - } - } - - // check if the key path exists; if so, error - if _, err := os.Stat(clientKeyPath); err == nil { - log.Fatalf("The client key already exists. Please remove it or specify a different key/cert.") - } - - if err := utils.GenerateCert([]string{""}, clientCertPath, clientKeyPath, caCertPath, caKeyPath, org, bits); err != nil { - log.Fatalf("Error generating client certificate: %s", err) - } - - // copy ca.pem to client cert dir for docker client - if err := utils.CopyFile(caCertPath, filepath.Join(utils.GetMachineClientCertDir(), "ca.pem")); err != nil { - log.Fatalf("Error copying ca.pem to client cert dir: %s", err) - } - } - - return nil -} - func main() { for _, f := range os.Args { if f == "-D" || f == "--debug" || f == "-debug" { @@ -86,7 +23,6 @@ func main() { app.Commands = Commands app.CommandNotFound = cmdNotFound app.Usage = "Create and manage machines running Docker." - app.Before = before app.Version = VERSION app.Flags = []cli.Flag{