func analyse(imageName string) clair.ImageAnalysis { var err error var image docker.Image if !docker.IsLocal { image, err = docker.Pull(imageName) if err != nil { if err == xerrors.NotFound { fmt.Println(err) } else { fmt.Println(xerrors.InternalError) } logrus.Fatalf("pulling image %q: %v", imageName, err) } } else { image, err = docker.Parse(imageName) if err != nil { fmt.Println(xerrors.InternalError) logrus.Fatalf("parsing local image %q: %v", imageName, err) } docker.FromHistory(&image) if err != nil { fmt.Println(xerrors.InternalError) logrus.Fatalf("getting local image %q from history: %v", imageName, err) } } return docker.Analyse(image) }
//PushHandler push image to Clair func PushHandler(rw http.ResponseWriter, request *http.Request) error { local := request.URL.Query()["local"] docker.IsLocal = len(local) > 0 logrus.Debugf("Hyperclair is local: %v", docker.IsLocal) var image docker.Image if !docker.IsLocal { var err error image, err = docker.Pull(parseImageURL(request)) if err != nil { return err } } else { var err error image, err = docker.Parse(parseImageURL(request)) if err != nil { return err } err = docker.Prepare(&image) logrus.Debugf("prepared image layers: %d", len(image.FsLayers)) if err != nil { return err } } logrus.Info("Pushing Image") if err := docker.Push(image); err != nil { return err } rw.WriteHeader(http.StatusCreated) return nil }
//PullHandler return the Light Manifest representation of the docker image func PullHandler(rw http.ResponseWriter, request *http.Request) error { rw.Header().Set("Content-Type", "application/json") image, err := docker.Pull(parseImageURL(request)) if err != nil { return err } j, err := image.AsJSON() if err != nil { return err } fmt.Fprint(rw, j) return nil }
func ReportHandler(rw http.ResponseWriter, request *http.Request) error { rw.Header().Set("Content-Type", "text/html") image, err := docker.Pull(parseImageURL(request)) if err != nil { return err } analyses := docker.Analyse(image) analysesHTML, err := clair.ReportAsHTML(analyses) if err != nil { return err } fmt.Fprint(rw, analysesHTML) return nil }
func AnalyseHandler(rw http.ResponseWriter, request *http.Request) error { local := request.URL.Query()["local"] docker.IsLocal = len(local) > 0 logrus.Debugf("Hyperclair is local: %v", docker.IsLocal) var err error var image docker.Image if !docker.IsLocal { image, err = docker.Pull(parseImageURL(request)) if err != nil { return err } } else { image, err = docker.Parse(parseImageURL(request)) if err != nil { return err } docker.FromHistory(&image) if err != nil { return err } } analyses := docker.Analyse(image) analysesJSON, err := xstrings.ToIndentJSON(analyses) if err != nil { return err } rw.Header().Set("Content-Type", "application/json") fmt.Fprint(rw, string(analysesJSON)) return nil }
Long: `Upload a Docker image to Clair for further analysis`, Run: func(cmd *cobra.Command, args []string) { if len(args) != 1 { fmt.Printf("hyperclair: \"push\" requires a minimum of 1 argument\n") os.Exit(1) } startLocalServer() imageName := args[0] var image docker.Image if !docker.IsLocal { var err error image, err = docker.Pull(imageName) if err != nil { if err == xerrors.NotFound { fmt.Println(err) } else { fmt.Println(xerrors.InternalError) } logrus.Fatalf("pulling image %q: %v", imageName, err) } } else { var err error image, err = docker.Parse(imageName) if err != nil { fmt.Println(xerrors.InternalError) logrus.Fatalf("parsing local image %q: %v", imageName, err) }
{{end}} ` // pingCmd represents the ping command var pullCmd = &cobra.Command{ Use: "pull IMAGE", Short: "Pull Docker image information", Long: `Pull image information from Docker Hub or Registry`, Run: func(cmd *cobra.Command, args []string) { //TODO how to use args with viper if len(args) != 1 { fmt.Printf("hyperclair: \"pull\" requires a minimum of 1 argument\n") os.Exit(1) } im := args[0] image, err := docker.Pull(im) if err != nil { fmt.Println(xerrors.ServiceUnavailable) logrus.Fatalf("pulling image %v: %v", args[0], err) } err = template.Must(template.New("pull").Parse(pullTplt)).Execute(os.Stdout, image) if err != nil { fmt.Println(xerrors.InternalError) logrus.Fatalf("rendering image: %v", err) } }, } func init() { RootCmd.AddCommand(pullCmd)