Beispiel #1
0
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
}
Beispiel #2
0
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
}
Beispiel #3
0
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()
}
Beispiel #4
0
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
}
Beispiel #5
0
func (io *importOptions) Execute(args []string) error {
	if err := app.CheckArity(2, 2, args); err != nil {
		return err
	}

	dir := args[0]

	i := &Importer{dir, nil, nil}

	name, tag := env.ParseRepositoryTag(args[1])

	fmt.Printf("Importing %s:%s...\n", name, tag)

	repoPath := path.Join(i.dir, "repositories")

	data, err := ioutil.ReadFile(repoPath)

	if err != nil {
		return err
	}

	i.tags = &env.TagStore{}

	err = json.Unmarshal(data, &i.tags)

	if err != nil {
		return err
	}

	sub := i.tags.Repositories[name]

	if sub == nil {
		return fmt.Errorf("No repo named %s found\n", name)
	}

	hash, ok := sub[tag]

	if !ok {
		return fmt.Errorf("No tag named %s found\n", tag)
	}

	i.sysTags = &env.TagStore{}
	sysPath := path.Join(env.DIR, "repositories")

	sysData, err := ioutil.ReadFile(sysPath)

	if err != nil {
		i.sysTags.Repositories = make(map[string]env.Repository)
	} else {
		err = json.Unmarshal(sysData, &i.sysTags)

		if err != nil {
			return err
		}
	}

	i.importLayer(hash)

	sysData, err = json.Marshal(i.sysTags)

	if err != nil {
		return err
	}

	ioutil.WriteFile(sysPath, sysData, 0644)

	return nil
}
Beispiel #6
0
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")
}