func (cmd *DestroyCommand) Execute(args []string) error {
	Logger.Info("Destroying... using information from dir:", cmd.Options.Dir)

	// Check Config and Buildfiles
	configFile, buildFile, err := cmd.Controls.CheckConfigs(cmd.App, cmd.Options)
	if err != nil {
		return err
	}

	dc := NewDockerApi(cmd.App.AppState.Meta, configFile, buildFile)
	dc.ShowInfo()

	// Register channel so we can watch for events as they happen
	eventsChannel := make(ApiChannel)
	go watchForEventsOn(eventsChannel)
	dc.RegisterChannel(eventsChannel)

	fqImageName := cmd.App.AppState.BuildFile.ImageName + ":" + cmd.App.AppState.BuildFile.Tag

	if _, err := dc.DeleteContainer(fqImageName); err != nil {
		Logger.Error("Could not delete container,", err)
		return err
	}

	if _, err := dc.DeleteImage(fqImageName); err != nil {
		Logger.Error("Could not delete image,", err)
		return err
	}

	msg := "Destroyed all known containers and images associated with: " + fqImageName
	Logger.Console(msg)

	// Nothing to do
	return nil
}
func (cmd *GroundCommand) Execute(args []string) error {
	Logger.Info("Grounding Tests... in dir:", cmd.Options.Dir)

	// Check Config and Buildfiles
	configFile, buildFile, err := cmd.Controls.CheckConfigs(cmd.App, cmd.Options)
	if err != nil {
		return err
	}

	dc := NewDockerApi(cmd.App.AppState.Meta, configFile, buildFile)
	dc.ShowInfo()

	if err := dc.createTestTemplates(*cmd.Options); err != nil {
		return err
	}

	// Register channel so we can watch for events as they happen
	eventsChannel := make(ApiChannel)
	go watchForEventsOn(eventsChannel)
	dc.RegisterChannel(eventsChannel)

	fqImageName := cmd.App.AppState.BuildFile.ImageName + ":" + cmd.App.AppState.BuildFile.Tag
	if running, err := dc.ListContainers(fqImageName); err != nil {
		Logger.Error("Error while trying to get a list of containers for ", fqImageName)
		return err
	} else {
		for _, container := range running {
			dc.StopContainer(container)
		}
	}

	Logger.Console("Grounded.")

	return nil
}
Exemple #3
0
func watchContainerOn(channel ContainerChannel, wg *sync.WaitGroup) {
	for msg := range channel {
		runtime.Gosched()
		Logger.Console(msg)
	}

	wg.Done()
}
func (cmd *TemplateCommand) Execute(args []string) error {
	Logger.Info("Creating Templates... in dir:", cmd.Options.Dir)

	_, _, err := cmd.Controls.CheckConfigs(cmd.App, cmd.Options)
	if err != nil {
		return err
	}

	cmd.App.AppState.Meta.Dir = cmd.Options.Dir

	dc := NewDockerApi(cmd.App.AppState.Meta, cmd.App.AppState.ConfigFile, cmd.App.AppState.BuildFile)

	err = dc.createTestTemplates(*cmd.Options)

	if err != nil {
		Logger.Console("Could not create Test-Flight templates.")
		return err
	}
	Logger.Console("Test-Flight templates created.")
	return nil
}
func (cmd *CheckCommand) Execute(args []string) error {
	Logger.Info("Running Pre-Flight Check... in dir:", cmd.Options.Dir)

	// Check Config and Buildfiles
	_, b, err := cmd.Controls.CheckConfigs(cmd.App, cmd.Options)
	if err != nil {
		Logger.Error("Could not verify config files. " + err.Error())
	} else {
		generateRequiredFilesFrom(b)

		Logger.Console("All checks passed! Files found!")
	}

	return nil
}
func (api *ApiDockerImage) Print() {
	info := []string{
		"",
		"Architecture: " + api.Architecture,
		"Author: " + api.Author,
		"Comment: " + api.Comment,
		"Container: " + api.Container,
		"DockerVersion: " + api.DockerVersion,
		"Container ID: " + api.Id,
		"OS: " + api.Os,
		"Parent: " + api.Parent,
		"",
	}

	Logger.Console(strings.Join(info, "\n"))
}
// == Build Command ==
// Should build a docker image
func (cmd *BuildCommand) Execute(args []string) error {
	// Set vars
	Logger.Info("Building... using information from dir:", cmd.Options.Dir)

	// Check Config and Buildfiles
	configFile, buildFile, err := cmd.Controls.CheckConfigs(cmd.App, cmd.Options)
	if err != nil {
		return err
	}

	// Api interaction here
	dc := NewDockerApi(cmd.App.AppState.Meta, configFile, buildFile)
	dc.ShowInfo()

	// Generate Templates
	// TODO: fails here with filemode
	if err := dc.createTestTemplates(*cmd.Options); err != nil {
		return err
	}

	// Register channel so we can watch for events as they happen
	eventsChannel := make(ApiChannel)
	go watchForEventsOn(eventsChannel)
	dc.RegisterChannel(eventsChannel)

	fqImageName := buildFile.ImageName + ":" + buildFile.Tag

	image, err := dc.CreateDockerImage(fqImageName, cmd.Options)
	if err != nil {
		return err
	}

	msg := "Created Docker Image: " + image
	Logger.Console(msg)
	return nil
}
func (cmd *VersionCommand) Execute(args []string) error {
	msg := "Test-Flight Version: " + cmd.App.AppState.Meta.Version
	Logger.Console(msg)

	return nil
}
func (cmd *LaunchCommand) Execute(args []string) error {
	Logger.Info("Launching Tests... in dir:", cmd.Options.Dir)
	Logger.Debug("Force:", cmd.Options.Force)

	var wg sync.WaitGroup // used for channels

	// Check Config and Buildfiles
	configFile, buildFile, err := cmd.Controls.CheckConfigs(cmd.App, cmd.Options)
	if err != nil {
		return err
	}

	dc := NewDockerApi(cmd.App.AppState.Meta, configFile, buildFile)
	dc.ShowInfo()

	if err := dc.createTestTemplates(*cmd.Options); err != nil {
		return err
	}
	Logger.Trace("Created test-flight templates.")

	// Register channel so we can watch for events as they happen
	eventsChannel := make(ApiChannel)
	go watchForEventsOn(eventsChannel)
	dc.RegisterChannel(eventsChannel)

	fqImageName := cmd.App.AppState.BuildFile.ImageName + ":" + buildFile.Tag

	getImageId := func() (string, error) {

		if image, err := dc.GetImageDetails(fqImageName); err != nil {
			return "", err
		} else { // response from endpoint (but could be 404)
			if image == nil { // doesn't exist
				if newImage, err := dc.CreateDockerImage(fqImageName, cmd.Options); err != nil {
					return "", err
				} else {
					return newImage, nil
				}
			} else { // exists
				if cmd.Options.Force { // recreate anyway
					if newImage, err := dc.CreateDockerImage(fqImageName, cmd.Options); err != nil {
						return "", err
					} else {
						return newImage, nil
					}
				} else { // warn user
					Logger.Warn("Will be starting a container from an image which already exists.")
					return image.Id, nil
				}
			}
			return image.Id, nil
		}
	}

	createAndStartContainerFrom := func(imageId string) error {
		if container, err := dc.CreateContainer(imageId); err != nil {
			return err
		} else {
			Logger.Trace("Docker Container to start:", container.Id)
			if _, err := dc.StartContainer(container.Id); err != nil {
				return err
			} else {
				wg.Add(1)
				containerChannel := dc.Attach(container.Id)
				go watchContainerOn(containerChannel, &wg)
				wg.Wait()
				return nil
			}
		}
	}

	if imageId, err := getImageId(); err != nil {
		return err
	} else {
		msg := "Launching docker container: " + imageId
		Logger.Console(msg)

		return createAndStartContainerFrom(imageId)
	}

	return nil
}