Пример #1
0
// DockerRebuild recreates the container based on the srv settings template.
// If pullImage is true, it updates the Docker image before recreating
// the container. Timeout is a number of seconds to wait before killing the
// container process ungracefully.
//
//  ops.SrvContainerName  - service or a chain container name to rebuild
//  ops.ContainerNumber   - container number
//  ops.ContainerType     - container type
//  ops.Labels            - container creation time labels
//
// Also see container parameters for DockerRunService.
func DockerRebuild(srv *def.Service, ops *def.Operation, pullImage bool, timeout uint) error {
	var wasRunning bool = false

	logger.Infof("Starting Docker Rebuild =>\t%s\n", srv.Name)

	if service, exists := ContainerExists(ops); exists {
		if _, running := ContainerRunning(ops); running {
			wasRunning = true
			err := DockerStop(srv, ops, timeout)
			if err != nil {
				return err
			}
		}

		logger.Infof("Removing old container =>\t%s\n", service.ID)
		err := removeContainer(service.ID, true)
		if err != nil {
			return err
		}

	} else {
		logger.Infoln("Service did not previously exist. Nothing to rebuild.")
		return nil
	}

	if pullImage {
		logger.Infof("Pulling new image =>\t\t%s\n", srv.Image)
		err := DockerPull(srv, ops)
		if err != nil {
			return err
		}
	}

	opts := configureServiceContainer(srv, ops)
	var err error
	srv.Volumes, err = util.FixDirs(srv.Volumes)
	if err != nil {
		return err
	}

	logger.Infof("Creating new cont for srv =>\t%s\n", srv.Name)
	_, err = createContainer(opts)
	if err != nil {
		return err
	}

	if wasRunning {
		logger.Infof("Restarting srv with new ID =>\t%s\n", opts.Name)
		err := startContainer(opts)
		if err != nil {
			return err
		}
	}

	logger.Infof("Finished rebuilding service =>\t%s\n", srv.Name)

	return nil
}
Пример #2
0
// DockerRebuild recreates the container based on the srv settings template.
// If pullImage is true, it updates the Docker image before recreating
// the container. Timeout is a number of seconds to wait before killing the
// container process ungracefully.
//
//  ops.SrvContainerName  - service or a chain container name to rebuild
//  ops.ContainerNumber   - container number
//  ops.ContainerType     - container type
//  ops.Labels            - container creation time labels
//
// Also see container parameters for DockerRunService.
func DockerRebuild(srv *def.Service, ops *def.Operation, pullImage bool, timeout uint) error {
	var wasRunning bool = false

	log.WithField("=>", srv.Name).Info("Rebuilding container")

	if _, exists := ContainerExists(ops); exists {
		if _, running := ContainerRunning(ops); running {
			wasRunning = true
			err := DockerStop(srv, ops, timeout)
			if err != nil {
				return err
			}
		}

		log.WithField("=>", ops.SrvContainerName).Info("Removing old container")
		err := removeContainer(ops.SrvContainerName, true, false)
		if err != nil {
			return err
		}

	} else {
		log.Info("Container did not previously exist. Nothing to rebuild")
		return nil
	}

	if pullImage {
		log.WithField("image", srv.Image).Info("Pulling image")
		err := DockerPull(srv, ops)
		if err != nil {
			return err
		}
	}

	opts := configureServiceContainer(srv, ops)
	var err error
	srv.Volumes, err = util.FixDirs(srv.Volumes)
	if err != nil {
		return err
	}

	log.WithField("=>", ops.SrvContainerName).Info("Recreating container")
	_, err = createContainer(opts)
	if err != nil {
		return err
	}

	if wasRunning {
		log.WithField("=>", opts.Name).Info("Restarting container")
		err := startContainer(opts)
		if err != nil {
			return err
		}
	}

	log.WithField("=>", ops.SrvContainerName).Info("Container rebuilt")

	return nil
}
Пример #3
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
}
Пример #4
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
}
Пример #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 {
	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
}
Пример #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
//  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
}