func runStop(args *docopt.Args, client *cluster.Client) error { success := true clients := make(map[string]*cluster.Host) for _, id := range args.All["ID"].([]string) { hostID, err := cluster.ExtractHostID(id) if err != nil { fmt.Printf("could not parse %s: %s", id, err) success = false continue } hostClient, ok := clients[hostID] if !ok { var err error hostClient, err = client.Host(hostID) if err != nil { fmt.Printf("could not connect to host %s: %s\n", hostID, err) success = false continue } clients[hostID] = hostClient } if err := hostClient.StopJob(id); err != nil { fmt.Printf("could not stop job %s: %s\n", id, err) success = false continue } fmt.Println(id, "stopped") } if !success { return errors.New("could not stop all jobs") } return nil }
func getLog(hostID, jobID string, client *cluster.Client, follow, init bool, stdout, stderr io.Writer) error { hostClient, err := client.Host(hostID) if err != nil { return fmt.Errorf("could not connect to host %s: %s", hostID, err) } attachReq := &host.AttachReq{ JobID: jobID, Flags: host.AttachFlagStdout | host.AttachFlagStderr | host.AttachFlagLogs, } if follow { attachReq.Flags |= host.AttachFlagStream } if init { attachReq.Flags |= host.AttachFlagInitLog } attachClient, err := hostClient.Attach(attachReq, false) if err != nil { switch err { case host.ErrJobNotRunning: return nil case cluster.ErrWouldWait: return errors.New("no such job") } return err } defer attachClient.Close() _, err = attachClient.Receive(stdout, stderr) return err }
func runTagsDel(args *docopt.Args, client *cluster.Client) error { host, err := client.Host(args.String["<hostid>"]) if err != nil { return err } vars := args.All["<var>"].([]string) tags := make(map[string]string, len(vars)) for _, v := range vars { // empty tags get deleted on the host tags[v] = "" } return host.UpdateTags(tags) }
func runInspect(args *docopt.Args, client *cluster.Client) error { hostID, jobID, err := cluster.ParseJobID(args.String["ID"]) if err != nil { return err } hostClient, err := client.Host(hostID) if err != nil { return fmt.Errorf("could not connect to host %s: %s", hostID, err) } job, err := hostClient.GetJob(jobID) if err != nil { return fmt.Errorf("no such job") } printJobDesc(job, os.Stdout, !args.Bool["--omit-env"]) return nil }
func runTagsSet(args *docopt.Args, client *cluster.Client) error { host, err := client.Host(args.String["<hostid>"]) if err != nil { return err } pairs := args.All["<var>=<val>"].([]string) tags := make(map[string]string, len(pairs)) for _, s := range pairs { keyVal := strings.SplitN(s, "=", 2) if len(keyVal) == 1 && keyVal[0] != "" { tags[keyVal[0]] = "true" } else if len(keyVal) == 2 { tags[keyVal[0]] = keyVal[1] } } return host.UpdateTags(tags) }
func runVolumeCreate(args *docopt.Args, client *cluster.Client) error { hostId := args.String["<host>"] hostClient, err := client.Host(hostId) if err != nil { fmt.Println("could not connect to host", hostId) } provider := "default" if args.String["--provider"] != "" { provider = args.String["--provider"] } v, err := hostClient.CreateVolume(provider) if err != nil { fmt.Printf("could not create volume: %s\n", err) return err } fmt.Printf("created volume %s on %s\n", v.ID, hostId) return nil }
func runSignal(args *docopt.Args, client *cluster.Client) error { id := args.String["ID"] sig, err := strconv.Atoi(args.String["SIGNAL"]) if err != nil { fmt.Println("invalid value for SIGNAL") return err } hostID, err := cluster.ExtractHostID(id) if err != nil { fmt.Println("could not parse", id) return err } hostClient, err := client.Host(hostID) if err != nil { fmt.Println("could not connect to host", hostID) return err } if err := hostClient.SignalJob(id, sig); err != nil { fmt.Println("could not signal job", id) return err } fmt.Printf("sent signal %d to %s successfully\n", sig, id) return nil }