func BuildConfigFromFlags() torus.Config {
	var err error
	readCacheSize, err = humanize.ParseBytes(readCacheSizeStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error parsing read-cache-size: %s\n", err)
		os.Exit(1)
	}
	localBlockSize, err = humanize.ParseBytes(localBlockSizeStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error parsing write-cache-size: %s\n", err)
		os.Exit(1)
	}

	var rl torus.ReadLevel
	switch readLevel {
	case "spread":
		rl = torus.ReadSpread
	case "seq":
		rl = torus.ReadSequential
	case "block":
		rl = torus.ReadBlock
	default:
		fmt.Fprintf(os.Stderr, "invalid readlevel; use one of 'spread', 'seq', or 'block'")
		os.Exit(1)
	}

	wl, err := torus.ParseWriteLevel(writeLevel)
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}

	return torus.Config{
		StorageSize:   localBlockSize,
		ReadCacheSize: readCacheSize,
		WriteLevel:    wl,
		ReadLevel:     rl,
	}
}
Esempio n. 2
0
func BuildConfigFromFlags() torus.Config {
	var err error
	if config == "" {
		config = defaultConfigPath()
	}
	if config != "" {
		conf, err := LoadConfigFile(config)
		if err != nil {
			fmt.Fprintf(os.Stderr, err.Error())
			os.Exit(1)
		}
		if etcdAddress == "" {
			etcdAddress = conf.EtcdConfig[profile].Etcd
		}
		if etcdCertFile == "" {
			etcdCertFile = conf.EtcdConfig[profile].EtcdCertFile
		}
		if etcdKeyFile == "" {
			etcdKeyFile = conf.EtcdConfig[profile].EtcdKeyFile
		}
		if etcdCAFile == "" {
			etcdCAFile = conf.EtcdConfig[profile].EtcdCAFile
		}
	}

	readCacheSize, err = humanize.ParseBytes(readCacheSizeStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error parsing read-cache-size: %s\n", err)
		os.Exit(1)
	}
	localBlockSize, err = humanize.ParseBytes(localBlockSizeStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error parsing write-cache-size: %s\n", err)
		os.Exit(1)
	}

	rl, err := torus.ParseReadLevel(readLevel)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error parsing read-level: %s\n", err)
		os.Exit(1)
	}

	wl, err := torus.ParseWriteLevel(writeLevel)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error parsing write-level: %s\n", err)
		os.Exit(1)
	}

	if etcdAddress == "" {
		etcdAddress = defaultEtcdAddress
	}

	cfg := torus.Config{
		StorageSize:     localBlockSize,
		ReadCacheSize:   readCacheSize,
		WriteLevel:      wl,
		ReadLevel:       rl,
		MetadataAddress: etcdAddress,
	}
	etcdURL, err := url.Parse(etcdAddress)
	if err != nil {
		fmt.Fprintf(os.Stderr, "invalid etcd address: %s", err)
		os.Exit(1)
	}

	if etcdCertFile != "" {
		etcdCert, err := tls.LoadX509KeyPair(etcdCertFile, etcdKeyFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "couldn't load cert/key: %s", err)
			os.Exit(1)
		}
		caPem, err := ioutil.ReadFile(etcdCAFile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "couldn't load trusted CA cert: %s", err)
			os.Exit(1)
		}
		etcdCertPool := x509.NewCertPool()
		etcdCertPool.AppendCertsFromPEM(caPem)
		cfg.TLS = &tls.Config{
			Certificates: []tls.Certificate{etcdCert},
			RootCAs:      etcdCertPool,
			ServerName:   strings.Split(etcdURL.Host, ":")[0],
		}
	}

	return cfg
}
Esempio n. 3
0
File: flex.go Progetto: coreos/torus
//{"kubernetes.io/fsType":"ext4","kubernetes.io/readwrite":"rw","volume":"block1"}
func attachAction(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		onErr(errors.New("unexpected number of arguments"))
	}
	vol := parseJSONArg(args[0])

	dev, err := nbd.FindDevice()
	if err != nil {
		onErr(err)
	}

	sysd := connectSystemd()

	svc := devToUnitName(dev)

	me, err := osext.Executable()
	if err != nil {
		onErr(err)
	}

	cmdList := []string{
		me,
		"-C",
		vol.Etcd,
		"nbd",
		vol.VolumeName,
		dev,
	}
	if vol.WriteLevel != "" {
		_, err := torus.ParseWriteLevel(vol.WriteLevel)
		if err != nil {
			onErr(err)
		}
		cmdList = append(cmdList, []string{"--write-level", vol.WriteLevel}...)
	}
	if vol.WriteCacheSize != "" {
		cmdList = append(cmdList, []string{"--write-cache-size", vol.WriteCacheSize}...)
	}

	ch := make(chan string)

	sysd.ResetFailedUnit(svc)
	_, err = sysd.StartTransientUnit(svc, "fail", []dbus.Property{
		dbus.PropExecStart(cmdList, false),
	}, ch)
	if err != nil {
		onErr(err)
	}
	<-ch
	status := sysd.wait(svc)
	if status == "failed" {
		onErr(errors.New("Couldn't attach"))
	} else if status == "active" {
		writeResponse(Response{
			Status: "Success",
			Device: dev,
		})
	} else {
		onErr(errors.New(status))
	}
	os.Exit(0)
}