Exemple #1
0
func main() {
	app := cli.NewApp()
	app.Name = "interlock"
	app.Version = version.FullVersion()
	app.Author = "@ehazlett"
	app.Email = "*****@*****.**"
	app.Usage = "an event driven extension system for docker"
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "debug, D",
			Usage: "Enable debug logging",
		},
	}
	app.Commands = []cli.Command{
		cmdSpec,
		cmdRun,
	}
	app.Before = func(c *cli.Context) error {
		if c.Bool("debug") {
			log.SetLevel(log.DebugLevel)
		}

		return nil
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}
Exemple #2
0
func cmdInfo(c *cli.Context) {
	haproxyOut, hErr := exec.Command("/usr/sbin/haproxy", "-v").Output()
	if hErr != nil {
		log.Fatal(hErr)
	}

	hData := strings.Split(string(haproxyOut), "\n")

	nginxOut, nErr := exec.Command("/usr/sbin/nginx", "-v").CombinedOutput()
	if nErr != nil {
		log.Fatal(nErr)
	}

	nData := strings.Split(string(nginxOut), "\n")

	fmt.Println("interlock " + version.FullVersion())
	fmt.Println(" " + string(hData[0]))
	fmt.Println(" " + string(nData[0]))
}
Exemple #3
0
func cmdStart(c *cli.Context) {
	swarmURL := c.GlobalString("swarm-url")
	swarmTLSCaCert := c.GlobalString("swarm-tls-ca-cert")
	swarmTLSCert := c.GlobalString("swarm-tls-cert")
	swarmTLSKey := c.GlobalString("swarm-tls-key")
	allowInsecureTLS := c.GlobalBool("swarm-allow-insecure")

	// only load env vars if no args
	// check environment for docker client config
	envDockerHost := os.Getenv("DOCKER_HOST")
	if swarmURL == "" && envDockerHost != "" {
		swarmURL = envDockerHost
	}

	// only load env vars if no args
	envDockerCertPath := os.Getenv("DOCKER_CERT_PATH")
	envDockerTLSVerify := os.Getenv("DOCKER_TLS_VERIFY")
	if swarmTLSCaCert == "" && envDockerCertPath != "" && envDockerTLSVerify != "" {
		swarmTLSCaCert = filepath.Join(envDockerCertPath, "ca.pem")
		swarmTLSCert = filepath.Join(envDockerCertPath, "cert.pem")
		swarmTLSKey = filepath.Join(envDockerCertPath, "key.pem")
	}

	config := &interlock.Config{}
	config.SwarmUrl = swarmURL
	config.EnabledPlugins = c.GlobalStringSlice("plugin")

	// load tlsconfig
	var tlsConfig *tls.Config
	if swarmTLSCaCert != "" && swarmTLSCert != "" && swarmTLSKey != "" {
		log.Infof("using tls for communication with swarm")
		caCert, err := ioutil.ReadFile(swarmTLSCaCert)
		if err != nil {
			log.Fatalf("error loading tls ca cert: %s", err)
		}

		cert, err := ioutil.ReadFile(swarmTLSCert)
		if err != nil {
			log.Fatalf("error loading tls cert: %s", err)
		}

		key, err := ioutil.ReadFile(swarmTLSKey)
		if err != nil {
			log.Fatalf("error loading tls key: %s", err)
		}

		cfg, err := getTLSConfig(caCert, cert, key, allowInsecureTLS)
		if err != nil {
			log.Fatalf("error configuring tls: %s", err)
		}
		tlsConfig = cfg
	}

	m := manager.NewManager(config, tlsConfig)

	log.Infof("interlock running version=%s", version.FullVersion())
	if err := m.Run(); err != nil {
		log.Fatal(err)
	}

	waitForInterrupt()

	log.Infof("shutting down")
	if err := m.Stop(); err != nil {
		log.Fatal(err)
	}
}
Exemple #4
0
func main() {
	app := cli.NewApp()
	app.Name = "interlock"
	app.Version = version.FullVersion()
	app.Author = "@ehazlett"
	app.Email = ""
	app.Usage = "event driven docker plugins"
	app.Before = func(c *cli.Context) error {
		if c.GlobalBool("debug") {
			log.SetLevel(log.DebugLevel)
		}
		return nil
	}
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:  "swarm-url, s",
			Value: "unix:///var/run/docker.sock",
			Usage: "swarm addr",
		},
		cli.StringFlag{
			Name:  "swarm-tls-ca-cert",
			Value: "",
			Usage: "tls ca certificate",
		},
		cli.StringFlag{
			Name:  "swarm-tls-cert",
			Value: "",
			Usage: "tls certificate",
		},
		cli.StringFlag{
			Name:  "swarm-tls-key",
			Value: "",
			Usage: "tls key",
		},
		cli.BoolFlag{
			Name:  "swarm-allow-insecure",
			Usage: "enable insecure tls communication",
		},
		cli.StringSliceFlag{
			Name:  "plugin, p",
			Usage: "enable plugin",
			Value: &cli.StringSlice{},
		},
		cli.BoolFlag{
			Name:  "debug, D",
			Usage: "enable debug",
		},
	}
	// base commands
	baseCommands := []cli.Command{
		{
			Name:   "start",
			Action: cmdStart,
		},
		{
			Name:   "list-plugins",
			Action: cmdListPlugins,
		},
		{
			Name:   "info",
			Action: cmdInfo,
		},
	}
	// plugin supplied commands
	baseCommands = append(baseCommands, plugins.GetCommands()...)

	app.Commands = baseCommands

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}
Exemple #5
0
func runAction(c *cli.Context) {
	log.Infof("interlock %s", version.FullVersion())

	// init kv
	kvOpts := &kvstore.Config{
		ConnectionTimeout: time.Second * 10,
	}

	dURL := c.String("discovery")
	dTLSCACert := c.String("discovery-tls-ca-cert")
	dTLSCert := c.String("discovery-tls-cert")
	dTLSKey := c.String("discovery-tls-key")

	var data string
	if dURL != "" {
		log.Debugf("using kv: addr=%s", dURL)
		if dTLSCACert != "" && dTLSCert != "" && dTLSKey != "" {
			tlsConfig, err := tlsconfig.Client(tlsconfig.Options{
				CAFile:   dTLSCACert,
				CertFile: dTLSCert,
				KeyFile:  dTLSKey,
			})
			if err != nil {
				log.Fatal(err)
			}

			log.Debug("configuring TLS for KV")
			kvOpts.TLS = tlsConfig
		}

		kv, err := getKVStore(dURL, kvOpts)
		if err != nil {
			log.Fatal(err)
		}

		// get config from kv
		exists, err := kv.Exists(kvConfigKey)
		if err != nil {
			log.Fatal(err)
		}

		if !exists {
			log.Warnf("unable to find config in key %s; using default config", kvConfigKey)
			data = defaultConfig
		} else {
			kvPair, err := kv.Get(kvConfigKey)
			if err != nil {
				log.Fatalf("error getting configuration from kv: %s", err)
			}

			data = string(kvPair.Value)

			if data == "" {
				data = defaultConfig
			}
		}
	} else {
		configPath := c.String("config")

		d, err := ioutil.ReadFile(configPath)
		switch {
		case os.IsNotExist(err):
			log.Debug("no config detected; using default config")
			data = defaultConfig
		case err == nil:
			data = string(d)
		default:
			log.Fatal(err)
		}
	}

	config, err := config.ParseConfig(data)
	if err != nil {
		log.Fatal(err)
	}

	srv, err := server.NewServer(config)
	if err != nil {
		log.Fatal(err)
	}

	if err := srv.Run(); err != nil {
		log.Fatal(err)
	}
}