func PrintLineByContainerName(containerName string, existing bool) ([]string, error) {
	cont0, exists := util.ParseContainers(containerName, true)
	if exists {
		cont1, running := util.ParseContainers(containerName, false)
		if running && (cont0.ID == cont1.ID) {
			return util.PrintLineByContainerID(cont0.ID, false) //will print "RUNNING" = Yes when asked for existing/running conts

		} else {
			return util.PrintLineByContainerID(cont0.ID, true) // will print "RUNNING" = No when asked for existing conts
		}
	}
	return nil, nil //fail silently
}
func makePartFromContainer(name string) (v Parts, err error) {
	// this block pulls out functionality from
	// PrintLineByContainerName{Id} & printLine
	var contID *docker.Container
	cont, exists := util.ParseContainers(name, true)
	if exists {
		contID, err = util.DockerClient.InspectContainer(cont.ID)
		if err != nil {
			return Parts{}, err
		}
	}
	if err != nil {
		return Parts{}, err
	}
	tmp, err := reflections.GetField(contID, "Name")
	if err != nil {
		return Parts{}, err
	}

	n := tmp.(string)

	Names := util.ContainerDisassemble(n)

	v = Parts{
		ShortName: Names.ShortName,
		//Running: set in previous function
		FullName:    Names.FullName,
		PortsOutput: util.FormulatePortsOutput(contID),
	}
	return v, nil
}
Exemple #3
0
// ContainerExists returns APIContainers containers list and true
// if the container ops.DataContainerName exists and running,
// otherwise false.
func DataContainerExists(ops *def.Operation) (docker.APIContainers, bool) {
	return util.ParseContainers(ops.DataContainerName, true)
}
Exemple #4
0
// ContainerExists returns APIContainers containers list and true
// if the container ops.SrvContainerName exists and is running,
// otherwise false.
func ContainerRunning(ops *def.Operation) (docker.APIContainers, bool) {
	return util.ParseContainers(ops.SrvContainerName, false)
}
Exemple #5
0
// DockerExecService creates and runs a chain or a service container interactively.
//
//  ops.Args         - command line parameters
//  ops.Interactive  - if true, set Entrypoint to ops.Args,
//                     if false, set Cmd to ops.Args
//
// See parameter description for DockerRunService.
func DockerExecService(srv *def.Service, ops *def.Operation) error {
	logger.Infof("Starting Service =>\t\t%s\n", srv.Name)

	optsServ := configureInteractiveContainer(srv, ops)

	// Fix volume paths.
	var err error
	srv.Volumes, err = util.FixDirs(srv.Volumes)
	if err != nil {
		return err
	}

	// Setup data container.
	logger.Infof("Manage data containers? =>\t%t\n", srv.AutoData)
	if srv.AutoData {
		optsData, err := configureDataContainer(srv, ops, &optsServ)
		if err != nil {
			return err
		}

		if _, exists := util.ParseContainers(ops.DataContainerName, true); exists {
			logger.Infoln("Data container already exists, am not creating.")
		} else {
			logger.Infoln("Data container does not exist, creating.")
			_, err := createContainer(optsData)
			if err != nil {
				return err
			}
		}
	}

	logger.Infof("Service container does not exist, creating from image (%s).\n", srv.Image)
	_, err = createContainer(optsServ)
	if err != nil {
		return err
	}

	defer func() {
		logger.Infof("Removing container =>\t\t%s\n", optsServ.Name)
		if err := removeContainer(optsServ.Name, false); err != nil {
			fmt.Errorf("Tragic! Error removing data container after executing (%v): %v", optsServ.Name, err)
		}
		logger.Infof("Container removed =>\t\t%s\n", optsServ.Name)
	}()

	// Start the container.
	logger.Infof("Starting service container =>\t%s\n", optsServ.Name)
	if srv.AutoData {
		logger.Infof("\twith data container =>\t%s\n", ops.DataContainerName)
	}
	logger.Debugf("\twith EntryPoint =>\t%v\n", optsServ.Config.Entrypoint)
	logger.Debugf("\twith CMD =>\t\t%v\n", optsServ.Config.Cmd)
	logger.Debugf("\twith Image =>\t\t%v\n", optsServ.Config.Image)
	logger.Debugf("\twith AllPortsPubl'd =>\t%v\n", optsServ.HostConfig.PublishAllPorts)
	logger.Debugf("\twith Environment =>\t%v\n", optsServ.Config.Env)
	if err := startInteractiveContainer(optsServ); err != nil {
		return err
	}

	return nil
}
Exemple #6
0
// DockerRunService creates and runs a chain or a service container with the srv
// settings template. It also creates dependent data containers if srv.AutoData
// is true. DockerRunService returns Docker errors if not successful.
//
//  srv.AutoData          - if true, create or use existing data container
//
//  ops.SrvContainerName  - service or a chain container name
//  ops.DataContainerName - dependent data container name
//  ops.ContainerNumber   - container number
//  ops.ContainerType     - container type
//  ops.Labels            - container creation time labels
//                          (use LoadServiceDefinition or LoadChainDefinition)
// Container parameters:
//
//  ops.Remove            - remove container on exit (similar to `docker run --rm`)
//  ops.PublishAllPorts   - if true, publish exposed ports to random ports
//  ops.CapAdd            - add linux capabilities (similar to `docker run --cap-add=[]`)
//  ops.CapDrop           - add linux capabilities (similar to `docker run --cap-drop=[]`)
//  ops.Privileged        - if true, give extended privileges
//  ops.Restart           - container restart policy ("always", "max:<#attempts>"
//                          or never if unspecified)
//
func DockerRunService(srv *def.Service, ops *def.Operation) error {
	logger.Infof("Starting Service =>\t\t%s\n", srv.Name)

	_, running := ContainerRunning(ops)
	if running {
		logger.Infof("Service already Started. Skipping.\n\tService Name=>\t\t%s\n", srv.Name)
		return nil
	}

	optsServ := configureServiceContainer(srv, ops)

	// Fix volume paths.
	var err error
	srv.Volumes, err = util.FixDirs(srv.Volumes)
	if err != nil {
		return err
	}

	// Setup data container.
	logger.Infof("Manage data containers? =>\t%t\n", srv.AutoData)
	if srv.AutoData {
		optsData, err := configureDataContainer(srv, ops, &optsServ)
		if err != nil {
			return err
		}

		if _, exists := util.ParseContainers(ops.DataContainerName, true); exists {
			logger.Infoln("Data Container already exists, am not creating.")
		} else {
			logger.Infoln("Data Container does not exist, creating.")
			_, err := createContainer(optsData)
			if err != nil {
				return err
			}
		}
	}

	// Check existence || create the container.
	if _, exists := ContainerExists(ops); exists {
		logger.Infoln("Service container already exists, am not creating.")
	} else {
		logger.Infof("Service container does not exist, creating from image (%s).\n", srv.Image)

		_, err := createContainer(optsServ)
		if err != nil {
			return err
		}
	}

	// Start the container.
	logger.Infof("Starting service container =>\t%s\n", optsServ.Name)
	if srv.AutoData {
		logger.Infof("\twith data container =>\t%s\n", ops.DataContainerName)
	}
	logger.Debugf("\twith EntryPoint =>\t%v\n", optsServ.Config.Entrypoint)
	logger.Debugf("\twith CMD =>\t\t%v\n", optsServ.Config.Cmd)
	logger.Debugf("\twith Image =>\t\t%v\n", optsServ.Config.Image)
	logger.Debugf("\twith AllPortsPubl'd =>\t%v\n", optsServ.HostConfig.PublishAllPorts)
	logger.Debugf("\twith Environment =>\t%v\n", optsServ.Config.Env)
	if err := startContainer(optsServ); err != nil {
		return err
	}

	if ops.Remove {
		logger.Infof("Removing container =>\t%s\n", optsServ.Name)
		if err := removeContainer(optsServ.Name, false); err != nil {
			return err
		}
	}

	logger.Infof("Successfully started service =>\t%s\n", optsServ.Name)

	return nil
}
Exemple #7
0
// DockerExecService creates and runs a chain or a service container interactively.
//
//  ops.Args         - command line parameters
//  ops.Interactive  - if true, set Entrypoint to ops.Args,
//                     if false, set Cmd to ops.Args
//
// See parameter description for DockerRunService.
func DockerExecService(srv *def.Service, ops *def.Operation) error {
	log.WithField("=>", ops.SrvContainerName).Info("Executing container")

	optsServ := configureInteractiveContainer(srv, ops)

	// Fix volume paths.
	var err error
	srv.Volumes, err = util.FixDirs(srv.Volumes)
	if err != nil {
		return err
	}

	// Setup data container.
	log.WithField("autodata", srv.AutoData).Info("Manage data containers?")

	if srv.AutoData {
		optsData, err := configureDataContainer(srv, ops, &optsServ)
		if err != nil {
			return err
		}

		if _, exists := util.ParseContainers(ops.DataContainerName, true); exists {
			log.Info("Data container already exists, am not creating")
		} else {
			log.Info("Data container does not exist. Creating")

			_, err := createContainer(optsData)
			if err != nil {
				return err
			}
		}
	}

	log.WithField("image", srv.Image).Debug("Container does not exist. Creating")
	_, err = createContainer(optsServ)
	if err != nil {
		return err
	}

	defer func() {
		log.WithField("=>", optsServ.Name).Info("Removing container")
		if err := removeContainer(optsServ.Name, false, false); err != nil {
			log.WithField("=>", optsServ.Name).Error("Tragic! Error removing data container after executing")
			log.Error(err)
		}
		log.WithField("=>", optsServ.Name).Info("Container removed")
	}()

	// Start the container.
	log.WithFields(log.Fields{
		"=>":              optsServ.Name,
		"data container":  ops.DataContainerName,
		"entrypoint":      optsServ.Config.Entrypoint,
		"cmd":             optsServ.Config.Cmd,
		"ports published": optsServ.HostConfig.PublishAllPorts,
		"environment":     optsServ.Config.Env,
		"image":           optsServ.Config.Image,
		"user":            optsServ.Config.User,
		"vols":            optsServ.HostConfig.Binds,
	}).Info("Executing interactive container")
	if err := startInteractiveContainer(optsServ); err != nil {
		return err
	}

	return nil
}
Exemple #8
0
// DockerRunService creates and runs a chain or a service container with the srv
// settings template. It also creates dependent data containers if srv.AutoData
// is true. DockerRunService returns Docker errors if not successful.
//
//  srv.AutoData          - if true, create or use existing data container
//  srv.Restart           - container restart policy ("always", "max:<#attempts>"
//                          or never if unspecified)
//
//  ops.SrvContainerName  - service or a chain container name
//  ops.DataContainerName - dependent data container name
//  ops.ContainerNumber   - container number
//  ops.ContainerType     - container type
//  ops.Labels            - container creation time labels
//                          (use LoadServiceDefinition or LoadChainDefinition)
// Container parameters:
//
//  ops.Remove            - remove container on exit (similar to `docker run --rm`)
//  ops.PublishAllPorts   - if true, publish exposed ports to random ports
//  ops.CapAdd            - add linux capabilities (similar to `docker run --cap-add=[]`)
//  ops.CapDrop           - add linux capabilities (similar to `docker run --cap-drop=[]`)
//  ops.Privileged        - if true, give extended privileges
//
func DockerRunService(srv *def.Service, ops *def.Operation) error {
	log.WithField("=>", ops.SrvContainerName).Info("Running container")

	_, running := ContainerRunning(ops)
	if running {
		log.WithField("=>", ops.SrvContainerName).Info("Container already started. Skipping")
		return nil
	}

	optsServ := configureServiceContainer(srv, ops)

	// Fix volume paths.
	var err error
	srv.Volumes, err = util.FixDirs(srv.Volumes)
	if err != nil {
		return err
	}

	// Setup data container.
	log.WithField("autodata", srv.AutoData).Info("Manage data containers?")
	if srv.AutoData {
		optsData, err := configureDataContainer(srv, ops, &optsServ)
		if err != nil {
			return err
		}

		if _, exists := util.ParseContainers(ops.DataContainerName, true); exists {
			log.Info("Data container already exists. Not creating")
		} else {
			log.Info("Data container does not exist. Creating")
			_, err := createContainer(optsData)
			if err != nil {
				return err
			}
		}
	}

	// Check existence || create the container.
	if _, exists := ContainerExists(ops); exists {
		log.Debug("Container already exists. Not creating")
	} else {
		log.WithField("image", srv.Image).Debug("Container does not exist. Creating")

		_, err := createContainer(optsServ)
		if err != nil {
			return err
		}
	}

	// Start the container.
	log.WithFields(log.Fields{
		"=>":              optsServ.Name,
		"data container":  ops.DataContainerName,
		"entrypoint":      optsServ.Config.Entrypoint,
		"cmd":             optsServ.Config.Cmd,
		"published ports": optsServ.HostConfig.PublishAllPorts,
		"environment":     optsServ.Config.Env,
		"image":           optsServ.Config.Image,
	}).Info("Starting container")
	if err := startContainer(optsServ); err != nil {
		return err
	}

	if ops.Remove {
		log.WithField("=>", optsServ.Name).Info("Removing container")
		if err := removeContainer(optsServ.Name, false, false); err != nil {
			return err
		}
	}

	log.WithField("=>", optsServ.Name).Info("Container started")

	return nil
}