func getFinalPipelineRunStatus(apiClient pps.ApiClient, pipelineRunID string) (*pps.PipelineRunStatus, error) { // TODO(pedge): not good ticker := time.NewTicker(time.Second) for i := 0; i < 60; i++ { <-ticker.C getPipelineRunStatusResponse, err := ppsutil.GetPipelineRunStatus( apiClient, pipelineRunID, ) if err != nil { return nil, err } protolog.Printf("status at tick %d: %v\n", i, getPipelineRunStatusResponse.PipelineRunStatus) pipelineRunStatus := getPipelineRunStatusResponse.PipelineRunStatus switch pipelineRunStatus.PipelineRunStatusType { case pps.PipelineRunStatusType_PIPELINE_RUN_STATUS_TYPE_ERROR: return pipelineRunStatus, nil case pps.PipelineRunStatusType_PIPELINE_RUN_STATUS_TYPE_SUCCESS: return pipelineRunStatus, nil } } return nil, fmt.Errorf("did not get final pipeline status for %s", pipelineRunID) }
func do(appEnvObj interface{}) error { appEnv := appEnvObj.(*appEnv) clientConn, err := grpc.Dial(appEnv.Address) if err != nil { return err } apiClient := pps.NewApiClient(clientConn) var protoFlag bool inspectCmd := cobramainutil.Command{ Use: "inspect github.com/user/repository [path/to/specDir]", Long: "Inspect a pipeline specification.", MinNumArgs: 1, MaxNumArgs: 2, Run: func(cmd *cobra.Command, args []string) error { pipelineArgs, err := getPipelineArgs(args) if err != nil { return err } getPipelineResponse, err := ppsutil.GetPipelineGithub( apiClient, pipelineArgs.contextDir, pipelineArgs.user, pipelineArgs.repository, pipelineArgs.branch, pipelineArgs.accessToken, ) if err != nil { return err } if protoFlag { fmt.Printf("%v\n", getPipelineResponse.Pipeline) } else { data, err := json.MarshalIndent(getPipelineResponse.Pipeline, "", "\t ") if err != nil { return err } fmt.Println(string(data)) } return nil }, }.ToCobraCommand() inspectCmd.Flags().BoolVar(&protoFlag, "proto", false, "Print in proto format instead of JSON.") startCmd := cobramainutil.Command{ Use: "start github.com/user/repository [path/to/specDir]", Long: "Start a pipeline specification run.", MinNumArgs: 1, MaxNumArgs: 2, Run: func(cmd *cobra.Command, args []string) error { pipelineArgs, err := getPipelineArgs(args) if err != nil { return err } startPipelineRunResponse, err := ppsutil.StartPipelineRunGithub( apiClient, pipelineArgs.contextDir, pipelineArgs.user, pipelineArgs.repository, pipelineArgs.branch, pipelineArgs.accessToken, ) if err != nil { return err } fmt.Println(startPipelineRunResponse.PipelineRunId) return nil }, }.ToCobraCommand() statusCmd := cobramainutil.Command{ Use: "status pipelineRunID", Long: "Get the status of a pipeline run.", NumArgs: 1, Run: func(cmd *cobra.Command, args []string) error { getPipelineRunStatusResponse, err := ppsutil.GetPipelineRunStatus( apiClient, args[0], ) if err != nil { return err } name, ok := pps.PipelineRunStatusType_name[int32(getPipelineRunStatusResponse.PipelineRunStatus.PipelineRunStatusType)] if !ok { return fmt.Errorf("unknown run status") } fmt.Printf("%s %s\n", args[0], strings.Replace(name, "PIPELINE_RUN_STATUS_TYPE_", "", -1)) return nil }, }.ToCobraCommand() logsCmd := cobramainutil.Command{ Use: "logs pipelineRunID [node]", Long: "Get the logs from a pipeline run.", MinNumArgs: 1, MaxNumArgs: 2, Run: func(cmd *cobra.Command, args []string) error { node := "" if len(args) == 2 { node = args[1] } getPipelineRunLogsResponse, err := ppsutil.GetPipelineRunLogs( apiClient, args[0], node, ) if err != nil { return err } for _, pipelineRunLog := range getPipelineRunLogsResponse.PipelineRunLog { name, ok := pps.OutputStream_name[int32(pipelineRunLog.OutputStream)] if !ok { return fmt.Errorf("unknown pps.OutputStream") } name = strings.Replace(name, "OUTPUT_STREAM_", "", -1) containerID := pipelineRunLog.ContainerId if len(containerID) > 8 { containerID = containerID[:8] } logInfo := &logInfo{ Node: pipelineRunLog.Node, ContainerID: containerID, OutputStream: name, Time: protoutil.TimestampToTime(pipelineRunLog.Timestamp), } logInfoData, err := json.Marshal(logInfo) if err != nil { return err } s := fmt.Sprintf("%s %s", string(logInfoData), string(pipelineRunLog.Data)) fmt.Println(strings.TrimSpace(s)) } return nil }, }.ToCobraCommand() rootCmd := &cobra.Command{ Use: "pps", Long: `Access the PPS API. Note that this CLI is experimental and does not even check for common errors. The environment variable PPS_ADDRESS controls what server the CLI connects to, the default is 0.0.0.0:651.`, } rootCmd.AddCommand(cobramainutil.NewVersionCommand(clientConn, pachyderm.Version)) rootCmd.AddCommand(inspectCmd) rootCmd.AddCommand(startCmd) rootCmd.AddCommand(statusCmd) rootCmd.AddCommand(logsCmd) return rootCmd.Execute() }