Exemplo n.º 1
0
// GuestInfoSinkWithPrefix adds a prefix to all keys accessed. The key must not have leading
// or trailing separator characters, but may have separators in other positions. The separator
// (either . or /) will be replaced with the appropriate value for the key in question.
func GuestInfoSinkWithPrefix(prefix string) (DataSink, error) {
	guestinfo := rpcvmx.NewConfig()

	if !vmcheck.IsVirtualWorld() {
		return nil, errors.New("not in a virtual world")
	}

	return func(key, value string) error {
		if strings.Contains(key, "/") {
			// quietly skip if it's a read-only key
			return nil
		}

		key = addPrefixToKey(DefaultGuestInfoPrefix, prefix, key)

		if value == "" {
			value = "<nil>"
		}

		log.Debugf("GuestInfoSink: setting key: %s, value: %#v", key, value)
		err := guestinfo.SetString(key, value)
		log.Debugf("GuestInfoSink: error: %#v", err)
		return err
	}, nil
}
Exemplo n.º 2
0
// GuestInfoSourceWithPrefix adds a prefix to all keys accessed. The key must not have leading
// or trailing separator characters, but may have separators in other positions. The separator
// (either . or /) will be replaced with the appropriate value for the key in question.
func GuestInfoSourceWithPrefix(prefix string) (DataSource, error) {
	guestinfo := rpcvmx.NewConfig()

	if !vmcheck.IsVirtualWorld() {
		return nil, errors.New("not in a virtual world")
	}

	source := func(key string) (string, error) {
		if key != guestinfoSecretKey {
			key = addPrefixToKey(DefaultGuestInfoPrefix, prefix, key)
		}

		value, err := guestinfo.String(key, "")
		if value == "" {
			err = ErrKeyNotFound
		} else if value == "<nil>" {
			value = ""
		}

		if key != guestinfoSecretKey { // don't log the secret key
			log.Debugf("GuestInfoSource: key: %s, value: %#v, error: %s", key, value, err)
		}

		return value, err
	}

	return new(SecretKey).Source(source), nil
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: vmware/vic
func main() {
	if version.Show() {
		fmt.Fprintf(os.Stdout, "%s\n", version.String())
		return
	}

	if !vmcheck.IsVirtualWorld() {
		log.Fatalf("ERROR: not in a virtual world.")
	}

	if !set && !get && !fork {
		flag.Usage()
	}

	config := rpcvmx.NewConfig()
	if set {
		if flag.NArg() != 2 {
			log.Fatalf("ERROR: Please provide guestinfo key / value pair (eg; -set foo bar")
		}
		if err := config.SetString(flag.Arg(0), flag.Arg(1)); err != nil {
			log.Fatalf("ERROR: SetString failed with %s", err)
		}
	}

	if get {
		if flag.NArg() != 1 {
			log.Fatalf("ERROR: Please provide guestinfo key (eg; -get foo)")
		}
		if out, err := config.String(flag.Arg(0), ""); err != nil {
			log.Fatalf("ERROR: String failed with %s", err)
		} else {
			fmt.Printf("%s\n", out)
		}
	}

	if fork {
		out, ok, err := rpcout.SendOne("vmfork-begin -1 -1")
		if err != nil {
			log.Fatalf("ERROR: %s | %s | %t", err, out, ok)
		} else if !ok {
			log.Fatalf("FAILED: %s", out)
		} else {
			fmt.Printf("%s\n", out)
		}
	}
}
Exemplo n.º 4
0
Arquivo: main.go Projeto: vmware/vic
func main() {
	if !vmcheck.IsVirtualWorld() {
		fmt.Println("not in a virtual world... :(")
		return
	}

	config := rpcvmx.NewConfig()

	fmt.Println(config.SetString("foo", "bar"))
	fmt.Println(config.String("foo", "foo"))

	fmt.Println(config.SetInt("foo", 3))
	fmt.Println(config.Int("foo", 0))

	fmt.Println(config.SetBool("foo", false))
	fmt.Println(config.Bool("foo", true))

}
Exemplo n.º 5
0
// GuestInfoSourceWithPrefix adds a prefix to all keys accessed. The key must not have leading
// or trailing separator characters, but may have separators in other positions. The separator
// (either . or /) will be replaced with the appropriate value for the key in question.
func GuestInfoSourceWithPrefix(prefix string) (DataSource, error) {
	guestinfo := rpcvmx.NewConfig()

	if !vmcheck.IsVirtualWorld() {
		return nil, errors.New("not in a virtual world")
	}

	return func(key string) (string, error) {
		key = addPrefixToKey(DefaultGuestInfoPrefix, prefix, key)

		value, err := guestinfo.String(key, "")
		if value == "" {
			err = ErrKeyNotFound
		} else if value == "<nil>" {
			value = ""
		}

		log.Debugf("GuestInfoSource: key: %s, value: %#v, error: %s", key, value, err)
		return value, err
	}, nil
}
Exemplo n.º 6
0
// GuestInfoSourceWithPrefix adds a prefix to all keys accessed. The key must not have leading
// or trailing separator characters, but may have separators in other positions. The separator
// (either . or /) will be replaced with the appropriate value for the key in question.
func GuestInfoSourceWithPrefix(prefix string) (DataSource, error) {
	guestinfo := rpcvmx.NewConfig()

	if !vmcheck.IsVirtualWorld() {
		return nil, errors.New("not in a virtual world")
	}

	return func(key string) (string, error) {
		key = addPrefixToKey(DefaultGuestInfoPrefix, prefix, key)

		value, err := guestinfo.String(key, "")
		if value == "" {
			// if we don't assume this then we never return error for missing keys
			// which is problematic for decoding
			err = errors.New("no value for key")
		} else if value == "<nil>" {
			value = ""
		}

		log.Debugf("GuestInfoSource: key: %s, value: %#v, error: %s", key, value, err)
		return value, err
	}, nil
}