func (co *commitOptions) Execute(args []string) error { if err := app.CheckArity(1, 1, args); err != nil { return err } id := utils.ExpandID(env.DIR, args[0]) cont, err := env.LoadContainer(env.DIR, id) if err != nil { return fmt.Errorf("Unable to load %s: %s\n", id, err) } ts, err := env.DefaultTagStore() if err != nil { return err } img, err := cont.Commit(co.Comment, co.Author, nil, co.Squash, false) if err != nil { return fmt.Errorf("Unable to create image: %s\n", err) } repo, tag := env.ParseRepositoryTag(args[1]) ts.Add(repo, tag, img.ID) ts.Flush() return nil }
func (lo *lsOptions) Execute(args []string) error { if err := app.CheckArity(1, 2, args); err != nil { return err } repo := args[0] dir := "." if len(args) > 1 { dir = args[1] } tags, err := env.DefaultTagStore() if err != nil { return err } img, err := tags.LookupImage(repo) if err != nil { return err } e := exec.Command("ls", path.Join(env.DIR, "graph", img.ID, "layer", dir)) e.Stdout = os.Stdout return e.Run() }
func (po *psOptions) Execute(args []string) error { if err := app.CheckArity(0, 0, args); err != nil { return err } dir, err := ioutil.ReadDir(path.Join(env.DIR, "containers")) if err != nil { return err } ts, err := env.DefaultTagStore() w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0) fmt.Fprintf(w, " ID\tREPO\tCREATED\n") var cs Containers for _, f := range dir { id := f.Name() cont, err := env.LoadContainer(env.DIR, id) if err != nil { continue } cs = append(cs, cont) } sort.Sort(cs) for _, cont := range cs { repo, tag := ts.Find(cont.Image) state := " " _, err := os.Stat(cont.PathTo("running")) if err == nil { state = "* " } if repo == "" { fmt.Fprintf(w, "%s%s\t \t%s\n", state, cont.ID[0:12], cont.Created.String()) } else { fmt.Fprintf(w, "%s%s\t%s:%s\t%s\n", state, cont.ID[0:12], repo, tag, cont.Created.String()) } } w.Flush() return nil }
func (ro *runOptions) Execute(args []string) error { capa := &env.Capabilities{} config, hostcfg, err := ParseRun(ro, args, capa) if err != nil { return err } if config == nil { return nil } if config.Image == "" { app.ShowHelp() return nil } tags, err := env.DefaultTagStore() if err != nil { return err } container, err := env.ContainerCreate(tags, config) if err != nil { return fmt.Errorf("Unable to create container: %s\n", err) } c := make(chan os.Signal, 1) signal.Notify(c, forwardSignals...) go func() { for { sig := <-c fmt.Printf("Got signal!\n") container.Signal(sig) fmt.Printf("Done with signal!\n") } }() err = container.Start(hostcfg) if err != nil { container.Remove() return fmt.Errorf("Unable to start container: %s\n", err) } container.Wait(hostcfg) return nil }
func (to *tagOptions) Execute(args []string) error { ts, err := env.DefaultTagStore() if err != nil { panic(err) } if to.Resolve { if len(args) < 1 { return fmt.Errorf("Specify a repo:tag to resolve\n") } id, err := ts.Lookup(args[0]) if err != nil { return fmt.Errorf("Unable to resolve tag: %s\n", args[0]) } fmt.Printf("%s\n", id) return nil } if to.Del { if len(args) < 1 { return fmt.Errorf("Specify a repo:tag to delete\n") } repo, tag := env.ParseRepositoryTag(args[0]) ts.RemoveTag(repo, tag) } else { if len(args) < 2 { return fmt.Errorf("Specify a repo:tag and id to add\n") } repo, tag := env.ParseRepositoryTag(args[0]) id, ok := env.SafelyExpandImageID(args[1]) if !ok { return fmt.Errorf("Unable to find image matching '%s'\n", args[1]) } ts.Add(repo, tag, id) } ts.Flush() return nil }
func (so *s3Options) Execute(args []string) error { if err := app.CheckArity(1, 1, args); err != nil { return err } repo := args[0] s3 := s3.New(awsAuth, awsRegion) buk := s3.Bucket(defBucket) data, err := buk.Get("/binary/repos/repositories") if err != nil { return err } ts := &env.TagStore{} err = json.Unmarshal(data, &ts) id, err := ts.Lookup(repo) if err != nil { return err } dts, err := env.DefaultTagStore() if err != nil { return err } i := &Importer{tags: ts, sysTags: dts} if !so.Force { if i.alreadyExists(id) { return fmt.Errorf("Already have %s, skipping download\n", utils.TruncateID(id)) } } fmt.Printf("Downloading %s (%s)\n", repo, utils.TruncateID(id)) err = i.download(buk, id) dts.Flush() return err }
func (ro *rmiOptions) Execute(args []string) error { if err := app.CheckArity(1, 1, args); err != nil { return err } ts, err := env.DefaultTagStore() if err != nil { return err } if !ts.RemoveByPrefix(args[0]) { fmt.Println("Unable to remove image.") } return ts.Flush() }
func nukeImage(no *nukeOptions, id string) error { ts, err := env.DefaultTagStore() if err != nil { return err } img, err := ts.LookupImage(id) if err == nil { repo, tag := env.ParseRepositoryTag(id) ts.RemoveTag(repo, tag) } else { long := env.ExpandImageID(id) var ok bool img, ok = ts.Entries[long] if !ok { return fmt.Errorf("Unable to find repo '%s'\n", id) } else { err = nil } if !no.Force && ts.UsedAsParent(long) { return fmt.Errorf("%s is a parent image, not removing (use -force to force)\n", id) } } if img != nil { otherRepo, _ := ts.Find(img.ID) if otherRepo != "" { fmt.Printf("Removing %s tag on %s only\n", id, utils.TruncateID(img.ID)) } else { fmt.Printf("Nuking image %s..\n", id) img.Remove() } } else { return fmt.Errorf("Error locating image: %s\n", err) } return ts.Flush() }
func (b *buildFile) BuildTar(tar string) error { defer b.cleanup() b.context = "/" if err := b.CmdFrom(""); err != nil { return err } if err := b.CmdAdd(tar + " /"); err != nil { return err } if err := b.container.ToDisk(); err != nil { return err } if b.outImage != "" { img, err := b.container.Commit("", "", nil, b.squash, true) if err != nil { return err } ts, err := env.DefaultTagStore() if err != nil { return err } repo, tag := env.ParseRepositoryTag(b.outImage) ts.Add(repo, tag, img.ID) ts.Flush() fmt.Fprintf(b.out, "Built %s successfully\n", b.outImage) return nil } b.saveContainer = true fmt.Fprintf(b.out, "Successfully built %s\n", utils.TruncateID(b.container.ID)) return nil }
func (bo *buildOptions) Execute(args []string) error { ts, err := env.DefaultTagStore() if err != nil { return err } if err := app.CheckArity(1, 1, args); err != nil { return err } abort := make(chan os.Signal, 1) signal.Notify(abort, syscall.SIGINT) b := &buildFile{ tags: ts, config: &env.Config{}, out: os.Stdout, verbose: true, outImage: bo.Image, abort: abort, squash: bo.Squash, experiment: bo.Experiment, } if bo.Tar { err = b.BuildTar(args[0]) } else { err = b.Build(args[0]) } if err != nil && err != ErrAbort { return err } return nil }
func (b *buildFile) Build(context string) error { defer b.cleanup() b.context = context if _, err := os.Stat(path.Join(context, "build.sh")); err == nil { fmt.Printf("Step 0: Execute build.sh on host\n") utils.Shell("cd " + context + "; bash ./build.sh") } dockerfile, err := os.Open(path.Join(context, "Dockerfile")) if err != nil { return fmt.Errorf("Can't build a directory with no Dockerfile") } file := bufio.NewReader(dockerfile) stepN := 0 for { select { case <-b.abort: fmt.Printf("Aborting...\n") if b.container != nil { b.container.Remove() } return ErrAbort default: // continue } line, err := file.ReadString('\n') if err != nil { if err == io.EOF && line == "" { break } else if err != io.EOF { return err } } line = strings.Trim(strings.Replace(line, "\t", " ", -1), " \t\r\n") // Skip comments and empty line if len(line) == 0 || line[0] == '#' { continue } tmp := strings.SplitN(line, " ", 2) if len(tmp) != 2 { return fmt.Errorf("Invalid Dockerfile format") } instruction := strings.ToLower(strings.Trim(tmp[0], " ")) arguments := strings.Trim(tmp[1], " ") method, exists := reflect.TypeOf(b).MethodByName("Cmd" + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:])) if !exists { fmt.Fprintf(b.out, "# Skipping unknown instruction %s\n", strings.ToUpper(instruction)) continue } stepN += 1 fmt.Fprintf(b.out, "Step %d : %s %s\n", stepN, strings.ToUpper(instruction), arguments) ret := method.Func.Call([]reflect.Value{reflect.ValueOf(b), reflect.ValueOf(arguments)})[0].Interface() if ret != nil { return ret.(error) } } b.config.Cmd = nil err = b.container.ToDisk() if err != nil { return err } if b.experiment { b.CmdRun("/bin/bash") return nil } if b.image != "" && b.outImage != "" { img, err := b.container.Commit("", "", nil, b.squash, true) if err != nil { return err } ts, err := env.DefaultTagStore() if err != nil { return err } repo, tag := env.ParseRepositoryTag(b.outImage) ts.Add(repo, tag, img.ID) ts.Flush() fmt.Fprintf(b.out, "Built %s successfully\n", b.outImage) return nil } if b.image != "" { b.saveContainer = true fmt.Fprintf(b.out, "Successfully built %s\n", utils.TruncateID(b.container.ID)) return nil } return fmt.Errorf("An error occured during the build\n") }