orchestrator

package
v0.0.0-...-ca39941 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InitContainers = func(names []string) error {
	newClient()
	build := viper.GetString("docker.build")
	if err := pullImage(build); err != nil {
		return err
	}
	runtime := viper.GetString("docker.runtime")
	if err := pullImage(runtime); err != nil {
		return err
	}

	tests := config.GetTests()
	if len(names) == 0 {
		names = make([]string, len(tests))
		i := 0
		for name := range tests {
			names[i] = name
			i++
		}
	}
	containers, err := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})
	if err != nil {
		return err
	}
	for _, c := range containers {
		for _, name := range c.Names {
			if err := checkContainerExists(name, c, &names); err != nil {
				return err
			}
		}
	}
	for _, name := range names {
		if err := createContainer(name); err != nil {
			return err
		}
	}
	return nil
}

InitContainers will check if the containers are running and run them if not.

View Source
var RunContainer = func(name, args string, target *config.Target, insertFiles []string) (out string, code int, err error) {
	var cont *types.Container
	if cont, err = getContainer(name); err != nil {
		return
	}
	var ca io.ReadCloser
	if ca, err = tarFile("ca", target.Ca); err != nil {
		return
	}
	defer ca.Close()
	if err = dockerClient.CopyToContainer(context.Background(), cont.ID, "/certs", ca, types.CopyToContainerOptions{}); err != nil {
		return
	}
	var key io.ReadCloser
	if key, err = tarFile("key", target.CaKey); err != nil {
		return
	}
	if err = dockerClient.CopyToContainer(context.Background(), cont.ID, "/certs", key, types.CopyToContainerOptions{}); err != nil {
		return
	}
	defer key.Close()
	for _, f := range insertFiles {
		var insert io.ReadCloser
		if insert, err = tarFile(path.Base(f), f); err != nil {
			return
		}
		if err = dockerClient.CopyToContainer(context.Background(), cont.ID, "/tmp", insert, types.CopyToContainerOptions{}); err != nil {
			return
		}
		defer insert.Close()
	}
	command := make([]string, len(args)+1)
	command[0] = name
	if len(args) > 0 {
		for i, arg := range strings.Split(args, " ") {
			command[i+1] = arg
		}
	}
	var id types.IDResponse
	if id, err = dockerClient.ContainerExecCreate(context.Background(), cont.ID, types.ExecConfig{
		Cmd:          command,
		AttachStderr: true,
		AttachStdout: true,
	}); err != nil {
		return
	}
	var (
		resp types.HijackedResponse
		ctx  = context.Background()
		done = make(chan error)
		outBuf,
		errBuf bytes.Buffer
	)
	if resp, err = dockerClient.ContainerExecAttach(ctx, id.ID, types.ExecConfig{}); err != nil {
		err = fmt.Errorf("error starting exec process: %w", err)
		return
	}
	defer resp.Close()
	go func() {
		_, err := stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
		done <- err
	}()
	select {
	case err = <-done:
		if err != nil {
			return
		}
	case <-ctx.Done():
		return
	}
	var inspect types.ContainerExecInspect
	if inspect, err = dockerClient.ContainerExecInspect(context.Background(), id.ID); err != nil {
		err = fmt.Errorf("error inspecting exec process: %w", err)
		return
	}
	code = inspect.ExitCode
	out = errBuf.String()
	return
}

RunContainer runs an executable in a docker container.

Functions

func RunTests

func RunTests(tests []string, prompt callbackFunc, userFiles map[string]string, logger func(string, ...interface{}), out io.Writer) (success []string, err error)

RunTests will take in test name and run each test or all tests.

func WipeContainers

func WipeContainers(names []string) error

WipeContainers will delete the images and containers generated by the containers

Types

type Client

type Client interface {
	ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)
	ContainerStart(ctx context.Context, containerID string, options types.ContainerStartOptions) error
	ImageList(ctx context.Context, options types.ImageListOptions) ([]types.ImageSummary, error)
	ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error)
	ImagePull(ctx context.Context, ref string, options types.ImagePullOptions) (io.ReadCloser, error)
	ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error)
	ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error
	ContainerExecAttach(ctx context.Context, execID string, config types.ExecConfig) (types.HijackedResponse, error)
	ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error)
	ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error)
	CopyToContainer(ctx context.Context, container, path string, content io.Reader, options types.CopyToContainerOptions) error
	ContainerRemove(ctx context.Context, containerID string, options types.ContainerRemoveOptions) error
	ImageRemove(ctx context.Context, imageID string, options types.ImageRemoveOptions) ([]types.ImageDelete, error)
	ContainerStop(ctx context.Context, containerID string, timeout *time.Duration) error
}

Client used to interface with Docker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL