Exemple #1
0
// NewContainer creates the paths for the given container.
func NewContainer(container *schemas.Container) (*Container, error) {
	root := filepath.Join(containersDir, container.ID)
	if err := os.MkdirAll(root, os.ModePerm|os.ModeDir); err != nil {
		return nil, err
	}

	sshPath := filepath.Join(sshDir, container.ID)
	if err := os.MkdirAll(root, os.ModePerm|os.ModeDir); err != nil {
		return nil, err
	}

	container.RemotePath = root
	container.SSHPath = sshPath
	return &Container{Container: container}, nil
}
Exemple #2
0
// Create creates the given container on the instance using a dockerfile
// as the base if given.
func Create(container *schemas.Container, dockerfile string) error {
	var body bytes.Buffer
	reqContainer := &requests.DockerfileContainerReq{
		Container:  container,
		Dockerfile: dockerfile,
	}

	encoder := json.NewEncoder(&body)
	err := encoder.Encode(reqContainer)
	if err != nil {
		return err
	}

	addr := net.JoinHostPort(container.Address, config.DelanceyProdPort)
	res, err := http.Post("http://"+addr, "application/json", &body)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	containerRes := new(requests.ContainerRes)
	decoder := json.NewDecoder(res.Body)
	err = decoder.Decode(containerRes)
	if err != nil {
		return err
	}

	if containerRes.Status != requests.StatusCreated {
		// If the error matches return var.
		if containerRes.Error() == ErrInUse.Error() {
			return ErrInUse
		}

		return containerRes
	}

	container.DockerID = containerRes.Container.DockerID
	container.RemotePath = containerRes.Container.RemotePath
	container.SSHPath = containerRes.Container.SSHPath
	container.ContainerPath = containerRes.Container.ContainerPath
	container.User = containerRes.Container.User
	container.Password = containerRes.Container.Password
	return nil
}