func fetch(cmd *cobra.Command, specs []string) { c := cli.NewClient(getServerURL()) if len(specs) == 0 { specs = cli.Read(os.Stdin) } for _, spec := range specs { cli.AssertSpec(spec) image, _ := c.Post("image", "images", spec) cli.JMap(image).Print(jsonout) } }
func job(cmd *cobra.Command, ids []string) { c := cli.NewClient(server) if len(ids) == 0 { ids = cli.Read(os.Stdin) } for _, id := range ids { cli.AssertID(id) job := getJob(c, id) job.Print(jsonout) } }
func del(cmd *cobra.Command, ids []string) { c := cli.NewClient(server) if len(ids) == 0 { ids = cli.Read(os.Stdin) } for _, id := range ids { cli.AssertID(id) j := deleteGuest(c, id) j.Print(jsonout) } }
func create(cmd *cobra.Command, specs []string) { c := cli.NewClient(server) if len(specs) == 0 { specs = cli.Read(os.Stdin) } for _, spec := range specs { cli.AssertSpec(spec) j := createGuest(c, spec) j.Print(jsonout) } }
func del(cmd *cobra.Command, ids []string) { c := cli.NewClient(getServerURL()) if len(ids) == 0 { ids = cli.Read(os.Stdin) } for _, id := range ids { cli.AssertID(id) image, _ := c.Delete("image", "images/"+id) cli.JMap(image).Print(jsonout) } }
func generateActionHandler(action string) func(*cobra.Command, []string) { return func(cmd *cobra.Command, ids []string) { c := cli.NewClient(server) if len(ids) == 0 { ids = cli.Read(os.Stdin) } for _, id := range ids { cli.AssertID(id) j := guestAction(c, id, action) j.Print(jsonout) } } }
func modify(cmd *cobra.Command, args []string) { c := cli.NewClient(server) if len(args) == 0 { args = cli.Read(os.Stdin) } if len(args)%2 != 0 { log.WithField("num", len(args)).Fatal("expected an even number of args") } for i := 0; i < len(args); i += 2 { id := args[i] cli.AssertID(id) spec := args[i+1] cli.AssertSpec(spec) guest := modifyGuest(c, id, spec) guest.Print(jsonout) } }
func subnetsDel(cmd *cobra.Command, args []string) { c := cli.NewClient(server) if len(args) == 0 { args = cli.Read(os.Stdin) } if len(args)%2 != 0 { log.WithField("num", len(args)).Fatal("expected an even amount of args") } for i := 0; i < len(args); i += 2 { hv := args[i] cli.AssertID(hv) subnet := args[i+1] cli.AssertID(subnet) deleted := deleteSubnet(c, hv, subnet) deleted.Print(jsonout) } }
func subnetsModify(cmd *cobra.Command, args []string) { c := cli.NewClient(server) if len(args) == 0 { args = cli.Read(os.Stdin) } if len(args)%2 != 0 { log.WithField("num", len(args)).Fatal("expected an even amount of args") } for i := 0; i < len(args); i += 2 { id := args[i] cli.AssertID(id) spec := args[i+1] cli.AssertSpec(spec) subnet := modifySubnets(c, id, spec) printTreeMap(id, "subnet", subnet) } }
func list(cmd *cobra.Command, args []string) { c := cli.NewClient(server) guests := []cli.JMap{} if len(args) == 0 { if termutil.Isatty(os.Stdin.Fd()) { guests = getGuests(c) sort.Sort(cli.JMapSlice(guests)) } else { args = cli.Read(os.Stdin) } } if len(guests) == 0 { for _, id := range args { cli.AssertID(id) guests = append(guests, getGuest(c, id)) } } for _, guest := range guests { guest.Print(jsonout) } }
func subnets(cmd *cobra.Command, ids []string) { c := cli.NewClient(server) if len(ids) == 0 { if termutil.Isatty(os.Stdin.Fd()) { for _, hv := range getHVs(c) { ids = append(ids, hv["id"].(string)) } } else { ids = cli.Read(os.Stdin) sort.Strings(ids) } } else { for _, id := range ids { cli.AssertID(id) } } for _, id := range ids { subnet, _ := c.Get("subnet", "hypervisors/"+id+"/subnets") printTreeMap(id, "subnet", subnet) } }
func guests(cmd *cobra.Command, ids []string) { c := cli.NewClient(server) if len(ids) == 0 { if termutil.Isatty(os.Stdin.Fd()) { for _, hv := range getHVs(c) { ids = append(ids, hv["id"].(string)) } } else { ids = cli.Read(os.Stdin) sort.Strings(ids) } } else { for _, id := range ids { cli.AssertID(id) } } for _, id := range ids { guests := getGuests(c, id) printTreeSlice(id, "guests", guests) } }
func list(cmd *cobra.Command, args []string) { c := cli.NewClient(server) hvs := []cli.JMap{} if len(args) == 0 { if termutil.Isatty(os.Stdin.Fd()) { hvs = getHVs(c) sort.Sort(cli.JMapSlice(hvs)) } else { args = cli.Read(os.Stdin) } } if len(hvs) == 0 { for _, id := range args { cli.AssertID(id) hvs = append(hvs, getHV(c, id)) } } for _, hv := range hvs { hv.Print(jsonout) } }
func list(cmd *cobra.Command, args []string) { c := cli.NewClient(getServerURL()) images := []cli.JMap{} if len(args) == 0 { if termutil.Isatty(os.Stdin.Fd()) { images = getImages(c) sort.Sort(cli.JMapSlice(images)) } else { args = cli.Read(os.Stdin) } } if len(images) == 0 { for _, id := range args { cli.AssertID(id) image, _ := c.Get("image", "images/"+id) images = append(images, image) } } for _, image := range images { image.Print(jsonout) } }
func upload(cmd *cobra.Command, specs []string) { if len(specs) == 0 { specs = cli.Read(os.Stdin) } uploadURL := getServerURL() + "/images" for _, spec := range specs { uploadImage := &metadata.Image{} if err := json.Unmarshal([]byte(spec), uploadImage); err != nil { log.WithFields(log.Fields{ "error": err, "json": spec, "func": "json.Unmarshal", }).Fatal("invalid spec") } sourcePath, err := filepath.Abs(uploadImage.Source) if err != nil { log.WithFields(log.Fields{ "error": err, "image": uploadImage, "func": "filepath.Abs", }).Fatal("failed determine absolute source path") } file, err := os.Open(sourcePath) if err != nil { log.WithFields(log.Fields{ "error": err, "path": sourcePath, "func": "os.Open", }).Fatal("failed to open file") } // File remains open until function exit defer logx.LogReturnedErr(file.Close, log.Fields{ "filename": sourcePath, }, "failed to close image source file") info, err := file.Stat() if err != nil { log.WithFields(log.Fields{ "error": err, "file": file.Name(), "func": "file.Stat", }).Fatal("failed to stat file") } req, err := http.NewRequest("PUT", uploadURL, file) if err != nil { log.WithFields(log.Fields{ "error": err, "url": uploadURL, "file": file.Name(), "func": "http.NewRequest", }).Fatal("failed to create request") } req.Header.Add("Content-Length", fmt.Sprintf("%d", info.Size())) req.Header.Add("X-Image-Type", uploadImage.Type) req.Header.Add("X-Image-Comment", uploadImage.Comment) req.Header.Add("Content-Type", "application/octet-stream") res, err := http.DefaultClient.Do(req) if err != nil { log.WithFields(log.Fields{ "error": err, "url": uploadURL, "file": file.Name(), "func": "http.DefaultClient.Do", }).Fatal("request error") } image := &cli.JMap{} cli.ProcessResponse(res, "image", "upload", []int{http.StatusOK}, image) image.Print(jsonout) } }
func download(cmd *cobra.Command, ids []string) { if len(ids) == 0 { ids = cli.Read(os.Stdin) } for _, id := range ids { success := false tempDest, err := ioutil.TempFile(downloadDir, "incompleteImage-") if err != nil { log.WithFields(log.Fields{ "error": err, "dir": downloadDir, "func": "ioutil.TempFile", }).Fatal("could not create temporary file") } defer func() { if !success { if err := os.Remove(tempDest.Name()); err != nil { log.WithFields(log.Fields{ "error": err, "tempfile": tempDest.Name(), "func": "os.Remove", }).Error("failed to remove temporary file") } } }() defer logx.LogReturnedErr(tempDest.Close, log.Fields{ "filename": tempDest.Name(), }, "failed to close temp dest") sourceURL := fmt.Sprintf("%s/images/%s/download", getServerURL(), id) resp, err := http.Get(sourceURL) if err != nil { log.WithFields(log.Fields{ "error": err, "sourceURL": sourceURL, "func": "http.Get", }).Error("request error") return } defer logx.LogReturnedErr(resp.Body.Close, nil, "failed to close response body") if resp.StatusCode != http.StatusOK { log.WithFields(log.Fields{ "sourceURL": sourceURL, "statusCode": resp.StatusCode, "func": "http.Get", }).Error("bad response code") return } if _, err := io.Copy(tempDest, resp.Body); err != nil { log.WithFields(log.Fields{ "error": err, "sourceURL": sourceURL, "tempFile": tempDest.Name(), "func": "io.Copy", }).Error("failed to download image") return } if _, err := tempDest.Seek(0, 0); err != nil { log.WithFields(log.Fields{ "error": err, "tempFile": tempDest.Name(), "func": "tempDest.Seek", }).Error("failed to seek to beginning of file") } fileBuffer := bufio.NewReader(tempDest) filetypeBytes, err := fileBuffer.Peek(512) if err != nil { log.WithFields(log.Fields{ "error": err, "tempFile": tempDest.Name(), "func": "tempDest.Peek", }).Error("failed to read image filetype bytes") return } extension := ".tar" if http.DetectContentType(filetypeBytes) == "application/x-gzip" { extension = extension + ".gz" } imagePath := filepath.Join(downloadDir, id+extension) if err := os.Rename(tempDest.Name(), imagePath); err != nil { log.WithFields(log.Fields{ "tempFile": tempDest.Name(), "imagePath": imagePath, "func": "os.Rename", }).Error("failed to rename image file") return } fmt.Println(imagePath) success = true } }