main.go 3.27 KB
Newer Older
Ricardo Rocha's avatar
Ricardo Rocha committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
// Copyright ©2016 CERN
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
	"log"
	"os"

	"github.com/docker/go-plugins-helpers/volume"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

const (
	socketAddress     = "/run/docker/plugins/eos.sock"
	defaultMountpoint = "/eos"
	noRepoGi
)

var (
	cfgFile    string
	mountpoint string
	socket     string
	verbose    bool
)

// rootCmd is the base command when called without any subcommands.
var rootCmd = &cobra.Command{
	Use:   "docker-volume-eos",
	Short: "A docker volume plugin for eos.",
	Long: `With no command given, the default is to start the eos docker 
volume plugin and listen for requests. The daemon will by default listen on 
/run/docker/plugins/eos.sock, you can override this with --socket.

Type --help to get more information regarding additional commands, these exist
mostly to support kubernetes volumes via the flexvolume plugin.

More information on EOS available at:
	http://eos.readthedocs.io/en/latest/
`,
	Run: func(cmd *cobra.Command, args []string) {
		d := newEosDriver(mountpoint)
		log.Printf("registering with docker\n")
		h := volume.NewHandler(d)
		log.Printf("listening on %v\n", socketAddress)
		err := h.ServeUnix("root", socketAddress)
		log.Printf("%v", err)
	},
}

func init() {
	cobra.OnInitialize(initConfig)
	rootCmd.AddCommand(initCmd)
	rootCmd.AddCommand(attachCmd)
	rootCmd.AddCommand(detachCmd)
	rootCmd.AddCommand(mountCmd)
	rootCmd.AddCommand(umountCmd)

	// Command flags
	rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "be very verbose")
	rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "alternative config file to use")
	rootCmd.PersistentFlags().StringVar(&mountpoint, "mountpoint", defaultMountpoint, "mountpoint to use (default /eos)")
	rootCmd.PersistentFlags().StringVar(&socket, "socket", socketAddress, "location for the plugin socket")

}

// Read in config file and ENV variables if set.
func initConfig() {
	if cfgFile != "" { // enable ability to specify config file via flag
		viper.SetConfigFile(cfgFile)
	}

	viper.SetConfigName("config.yaml")              // name of config file (without extension)
	viper.AddConfigPath("$HOME/.docker-volume-eos") // adding home directory as first search path
	viper.AddConfigPath("/etc/docker-volume-eos")   // adding /etc as the second search path
	viper.AddConfigPath(".")                        // adding /etc as the second search path
	viper.AutomaticEnv()                            // read in environment variables that match

	// If a config file is found, read it in.
	if err := viper.ReadInConfig(); err == nil {
		log.Printf("using config file: %v\n", viper.ConfigFileUsed())
	}
}

func main() {
	log.SetPrefix("docker-volume-eos: ")
	log.SetFlags(0)

	if err := rootCmd.Execute(); err != nil {
		os.Exit(-1)
	}
}