Initial commit
This commit is contained in:
99
vendor/github.com/hyperhq/hypercli/hyper/common.go
generated
vendored
Normal file
99
vendor/github.com/hyperhq/hypercli/hyper/common.go
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/go-connections/tlsconfig"
|
||||
"github.com/hyperhq/hypercli/cli"
|
||||
"github.com/hyperhq/hypercli/cliconfig"
|
||||
flag "github.com/hyperhq/hypercli/pkg/mflag"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTrustKeyFile = "key.json"
|
||||
defaultCaFile = "ca.pem"
|
||||
defaultKeyFile = "key.pem"
|
||||
defaultCertFile = "cert.pem"
|
||||
tlsVerifyKey = "tlsverify"
|
||||
)
|
||||
|
||||
var (
|
||||
commonFlags = &cli.CommonFlags{FlagSet: new(flag.FlagSet)}
|
||||
|
||||
dockerCertPath = os.Getenv("HYPER_CERT_PATH")
|
||||
dockerTLSVerify = os.Getenv("HYPER_TLS_VERIFY") != ""
|
||||
)
|
||||
|
||||
func init() {
|
||||
if dockerCertPath == "" {
|
||||
dockerCertPath = cliconfig.ConfigDir()
|
||||
}
|
||||
|
||||
commonFlags.PostParse = postParseCommon
|
||||
|
||||
cmd := commonFlags.FlagSet
|
||||
|
||||
cmd.BoolVar(&commonFlags.Debug, []string{"D", "-debug"}, false, "Enable debug mode")
|
||||
cmd.StringVar(&commonFlags.LogLevel, []string{"l", "-log-level"}, "info", "Set the logging level")
|
||||
cmd.BoolVar(&commonFlags.TLS, []string{}, true, "Use TLS; implied by --tlsverify")
|
||||
cmd.BoolVar(&commonFlags.TLSVerify, []string{}, dockerTLSVerify, "Use TLS and verify the remote")
|
||||
|
||||
// TODO use flag flag.String([]string{"i", "-identity"}, "", "Path to libtrust key file")
|
||||
|
||||
var tlsOptions tlsconfig.Options
|
||||
commonFlags.TLSOptions = &tlsOptions
|
||||
cmd.StringVar(&tlsOptions.CAFile, []string{}, filepath.Join(dockerCertPath, defaultCaFile), "Trust certs signed only by this CA")
|
||||
cmd.StringVar(&tlsOptions.CertFile, []string{}, filepath.Join(dockerCertPath, defaultCertFile), "Path to TLS certificate file")
|
||||
cmd.StringVar(&tlsOptions.KeyFile, []string{}, filepath.Join(dockerCertPath, defaultKeyFile), "Path to TLS key file")
|
||||
|
||||
cmd.StringVar(&commonFlags.Region, []string{"R", "-region"}, "", "Set the region of hyper.sh")
|
||||
}
|
||||
|
||||
func postParseCommon() {
|
||||
cmd := commonFlags.FlagSet
|
||||
|
||||
setDaemonLogLevel(commonFlags.LogLevel)
|
||||
|
||||
// Regardless of whether the user sets it to true or false, if they
|
||||
// specify --tlsverify at all then we need to turn on tls
|
||||
// TLSVerify can be true even if not set due to DOCKER_TLS_VERIFY env var, so we need to check that here as well
|
||||
if cmd.IsSet("-"+tlsVerifyKey) || commonFlags.TLSVerify {
|
||||
commonFlags.TLS = true
|
||||
}
|
||||
|
||||
if !commonFlags.TLS {
|
||||
commonFlags.TLSOptions = nil
|
||||
} else {
|
||||
tlsOptions := commonFlags.TLSOptions
|
||||
tlsOptions.InsecureSkipVerify = !commonFlags.TLSVerify
|
||||
|
||||
// Reset CertFile and KeyFile to empty string if the user did not specify
|
||||
// the respective flags and the respective default files were not found.
|
||||
if !cmd.IsSet("-tlscert") {
|
||||
if _, err := os.Stat(tlsOptions.CertFile); os.IsNotExist(err) {
|
||||
tlsOptions.CertFile = ""
|
||||
}
|
||||
}
|
||||
if !cmd.IsSet("-tlskey") {
|
||||
if _, err := os.Stat(tlsOptions.KeyFile); os.IsNotExist(err) {
|
||||
tlsOptions.KeyFile = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setDaemonLogLevel(logLevel string) {
|
||||
if logLevel != "" {
|
||||
lvl, err := logrus.ParseLevel(logLevel)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", logLevel)
|
||||
os.Exit(1)
|
||||
}
|
||||
logrus.SetLevel(lvl)
|
||||
} else {
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user