diff options
author | Phus Lu <phuslu@hotmail.com> | 2015-04-24 16:37:17 +0800 |
---|---|---|
committer | Zheng Chai <zheng.chai@citrix.com> | 2015-05-14 17:30:24 +0800 |
commit | 75bc98ab536804963551416a4206ec8c43ebcc34 (patch) | |
tree | 158e65bf08b638bff6375baeb2d13fda16667196 /xenstore | |
parent | 2c85890a25bff9f327ea95018c541c1190267492 (diff) | |
download | vyos-xe-guest-utilities-75bc98ab536804963551416a4206ec8c43ebcc34.tar.gz vyos-xe-guest-utilities-75bc98ab536804963551416a4206ec8c43ebcc34.zip |
CP-11399: Go Linux guest agent for XenServer
This is the reviewed and tested Go guest agent for XenServer Linux guests.
Go guest agent is a static linked binary without any dependency (e.g. Bash or Python execution environment) with below benefits:
1. Cross platform, Go version works well with all kinds Linux distributions (i386 and x86_64) with the porting ability to arm, FreeBSD, Darwin OS etc.
2. Standalone binary, works well with some restricted environment for example, CoreOS and Boot2Docker Linux
3. Easy to maintain and structured design, with Golang's nature
Change history:
1: Refined Rob Robert's comments.
2: Add unit test for xenstoreclient and refact folder structure.
3: Refined codes according to Robert's comments
4: To run 32bit xe-guest-agent in Linux 64bit OS(eg, CoreOS):
we need
4.1 - Switch to ip/ifconfig CLI tool instead of net package
4.2 - Switch to log package instead of syslog package
Signed-off-by: phus lu <phus.lu@citrix.com>
Diffstat (limited to 'xenstore')
-rw-r--r-- | xenstore/xenstore.go | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/xenstore/xenstore.go b/xenstore/xenstore.go new file mode 100644 index 0000000..c6fe1a5 --- /dev/null +++ b/xenstore/xenstore.go @@ -0,0 +1,123 @@ +package main + +import ( + xenstoreclient "../xenstoreclient" + "fmt" + "os" + "strings" +) + +func die(format string, a ...interface{}) { + fmt.Fprintf(os.Stderr, format, a...) + fmt.Fprintln(os.Stderr) + os.Exit(1) +} + +func usage() { + die( + `Usage: xenstore read key [ key ... ] + write key value [ key value ... ] + rm key [ key ... ] + exists key [ key ... ]`) +} + +func new_xs() xenstoreclient.XenStoreClient { + xs, err := xenstoreclient.NewXenstore(0) + if err != nil { + die("xenstore.Open error: %v", err) + } + + return xs +} + +func xs_read(script_name string, args []string) { + if len(args) == 0 || args[0] == "-h" { + die("Usage: %s key [ key ... ]", script_name) + } + + xs := new_xs() + for _, key := range args[:] { + result, err := xs.Read(key) + if err != nil { + die("%s error: %v", script_name, err) + } + + fmt.Println(result) + } +} + +func xs_write(script_name string, args []string) { + if len(args) == 0 || args[0] == "-h" || len(args)%2 != 0 { + die("Usage: %s key value [ key value ... ]", script_name) + } + + xs := new_xs() + for i := 0; i < len(args); i += 2 { + key := args[i] + value := args[i+1] + + err := xs.Write(key, value) + if err != nil { + die("%s error: %v", script_name, err) + } + } +} + +func xs_rm(script_name string, args []string) { + if len(args) == 0 || args[0] == "-h" { + die("Usage: %s key [ key ... ]", script_name) + } + + xs := new_xs() + for _, key := range args[:] { + err := xs.Rm(key) + if err != nil { + die("%s error: %v", script_name, err) + } + } +} + +func xs_exists(script_name string, args []string) { + if len(args) == 0 || args[0] == "-h" { + die("Usage: %s key [ key ... ]", script_name) + } + + xs := new_xs() + for _, key := range args[:] { + _, err := xs.Read(key) + if err != nil { + die("%s error: %v", script_name, err) + } + } +} + +func main() { + var operation string + var args []string + + script_name := os.Args[0] + if strings.Contains(script_name, "-") { + operation = script_name[strings.LastIndex(script_name, "-")+1:] + args = os.Args[1:] + } else { + if len(os.Args) < 2 { + usage() + } + operation = os.Args[1] + script_name = script_name + " " + operation + args = os.Args[2:] + } + + switch operation { + case "read": + xs_read(script_name, args) + case "write": + xs_write(script_name, args) + case "rm": + xs_rm(script_name, args) + case "exists": + xs_exists(script_name, args) + default: + usage() + } +} |