forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildlogs.go
104 lines (90 loc) · 3.5 KB
/
buildlogs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cmd
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildmanualclient "github.com/openshift/origin/pkg/build/client/internalversion"
buildclientinternal "github.com/openshift/origin/pkg/build/generated/internalclientset"
buildutil "github.com/openshift/origin/pkg/build/util"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
)
var (
buildLogsLong = templates.LongDesc(`
Retrieve logs for a build
This command displays the log for the provided build. If the pod that ran the build has been deleted logs
will no longer be available. If the build has not yet completed, the build logs will be streamed until the
build completes or fails.`)
buildLogsExample = templates.Examples(`
# Stream logs from container
%[1]s build-logs 566bed879d2d`)
)
// NewCmdBuildLogs implements the OpenShift cli build-logs command
func NewCmdBuildLogs(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
opts := buildapi.BuildLogOptions{}
cmd := &cobra.Command{
Use: "build-logs BUILD",
Short: "Show logs from a build",
Long: buildLogsLong,
Example: fmt.Sprintf(buildLogsExample, fullName),
Deprecated: fmt.Sprintf("use \"oc %v build/<build-name>\" instead.", LogsRecommendedCommandName),
Run: func(cmd *cobra.Command, args []string) {
err := RunBuildLogs(fullName, f, out, cmd, opts, args)
if err, ok := err.(errors.APIStatus); ok {
if msg := err.Status().Message; strings.HasSuffix(msg, buildutil.NoBuildLogsMessage) {
fmt.Fprintf(out, msg)
os.Exit(1)
}
if err.Status().Code == http.StatusNotFound {
switch err.Status().Details.Kind {
case "build":
fmt.Fprintf(out, "The build %s could not be found. Therefore build logs cannot be retrieved.\n", err.Status().Details.Name)
case "pod":
fmt.Fprintf(out, "The pod %s for build %s could not be found. Therefore build logs cannot be retrieved.\n", err.Status().Details.Name, args[0])
}
os.Exit(1)
}
}
kcmdutil.CheckErr(err)
},
}
cmd.Flags().BoolVarP(&opts.Follow, "follow", "f", true, "Specify whether logs should be followed; default is true.")
cmd.Flags().BoolVarP(&opts.NoWait, "nowait", "w", false, "Specify whether to return immediately without waiting for logs to be available; default is false.")
return cmd
}
// RunBuildLogs contains all the necessary functionality for the OpenShift cli build-logs command
func RunBuildLogs(fullName string, f *clientcmd.Factory, out io.Writer, cmd *cobra.Command, opts buildapi.BuildLogOptions, args []string) error {
if len(args) != 1 {
cmdNamespace := kcmdutil.GetFlagString(cmd, "namespace")
var namespace string
if cmdNamespace != "" {
namespace = " -n " + cmdNamespace
}
return kcmdutil.UsageErrorf(cmd, "A build name is required - you can run `%s get builds%s` to list builds", fullName, namespace)
}
namespace, _, err := f.DefaultNamespace()
if err != nil {
return err
}
clientConfig, err := f.ClientConfig()
if err != nil {
return err
}
buildClient, err := buildclientinternal.NewForConfig(clientConfig)
if err != nil {
return err
}
readCloser, err := buildmanualclient.NewBuildLogClient(buildClient.Build().RESTClient(), namespace).Logs(args[0], opts).Stream()
if err != nil {
return err
}
defer readCloser.Close()
_, err = io.Copy(out, readCloser)
return err
}