Beispiel #1
0
// viper read config file, marshal to definition struct,
// load service, validate name and data container
func LoadChainDefinition(chainName string, newCont bool, cNum ...int) (*definitions.Chain, error) {
	if len(cNum) == 0 {
		cNum = append(cNum, 0)
	}

	if cNum[0] == 0 {
		cNum[0] = util.AutoMagic(0, definitions.TypeChain, newCont)
		log.WithField("=>", fmt.Sprintf("%s:%d", chainName, cNum[0])).Debug("Loading chain definition (autoassigned)")
	} else {
		log.WithField("=>", fmt.Sprintf("%s:%d", chainName, cNum[0])).Debug("Loading chain definition")
	}

	chain := definitions.BlankChain()
	chain.Name = chainName
	chain.Operations.ContainerNumber = cNum[0]
	chain.Operations.ContainerType = definitions.TypeChain
	chain.Operations.Labels = util.Labels(chain.Name, chain.Operations)
	if err := setChainDefaults(chain); err != nil {
		return nil, err
	}

	chainConf, err := config.LoadViperConfig(filepath.Join(ChainsPath), chainName, "chain")
	if err != nil {
		return nil, err
	}

	// marshal chain and always reset the operational requirements
	// this will make sure to sync with docker so that if changes
	// have occured in the interim they are caught.
	if err = MarshalChainDefinition(chainConf, chain); err != nil {
		return nil, err
	}

	// Docker 1.6 (which eris doesn't support) had different linking mechanism.
	if ver, _ := util.DockerClientVersion(); ver >= version.DVER_MIN {
		if chain.Dependencies != nil {
			addDependencyVolumesAndLinks(chain.Dependencies, chain.Service, chain.Operations)
		}
	}

	checkChainNames(chain)
	log.WithFields(log.Fields{
		"container number": chain.Operations.ContainerNumber,
		"environment":      chain.Service.Environment,
		"entrypoint":       chain.Service.EntryPoint,
		"cmd":              chain.Service.Command,
	}).Debug("Chain definition loaded")
	return chain, nil
}
Beispiel #2
0
// LoadDataDefinition returns an Operation structure for a blank data container
// specified by a name dataName and a cNum number.
func LoadDataDefinition(dataName string, cNum int) *definitions.Operation {
	if cNum == 0 {
		cNum = 1
	}

	log.WithField("=>", fmt.Sprintf("%s:%d", dataName, cNum)).Debug("Loading data definition")

	ops := definitions.BlankOperation()
	ops.ContainerNumber = cNum
	ops.ContainerType = definitions.TypeData
	ops.SrvContainerName = util.DataContainersName(dataName, cNum)
	ops.DataContainerName = util.DataContainersName(dataName, cNum)
	ops.Labels = util.Labels(dataName, ops)

	return ops
}
Beispiel #3
0
// LoadDataDefinition returns an Operation structure for a blank data container
// specified by a name dataName and a cNum number.
func LoadDataDefinition(dataName string, cNum int) *definitions.Operation {
	if cNum == 0 {
		cNum = 1
	}

	logger.Debugf("Loading Data Definition =>\t%s:%d\n", dataName, cNum)

	ops := definitions.BlankOperation()
	ops.ContainerNumber = cNum
	ops.ContainerType = definitions.TypeData
	ops.SrvContainerName = util.DataContainersName(dataName, cNum)
	ops.DataContainerName = util.DataContainersName(dataName, cNum)
	ops.Labels = util.Labels(dataName, ops)

	return ops
}
Beispiel #4
0
func MockServiceDefinition(servName string, newCont bool, cNum ...int) *definitions.ServiceDefinition {
	srv := definitions.BlankServiceDefinition()
	srv.Name = servName

	if len(cNum) == 0 {
		srv.Operations.ContainerNumber = util.AutoMagic(cNum[0], definitions.TypeService, newCont)
		logger.Debugf("Mocking Service Definition =>\t%s:%d (autoassigned)\n", servName, cNum[0])
	} else {
		srv.Operations.ContainerNumber = cNum[0]
		logger.Debugf("Mocking Service Definition =>\t%s:%d\n", servName, cNum[0])
	}

	srv.Operations.ContainerType = definitions.TypeService
	srv.Operations.Labels = util.Labels(servName, srv.Operations)

	ServiceFinalizeLoad(srv)
	return srv
}
Beispiel #5
0
func MockServiceDefinition(servName string, newCont bool, cNum ...int) *definitions.ServiceDefinition {
	srv := definitions.BlankServiceDefinition()
	srv.Name = servName

	if len(cNum) == 0 {
		srv.Operations.ContainerNumber = util.AutoMagic(cNum[0], definitions.TypeService, newCont)
		log.WithField("=>", fmt.Sprintf("%s:%d", servName, cNum[0])).Debug("Mocking service definition (autoassigned)")
	} else {
		srv.Operations.ContainerNumber = cNum[0]
		log.WithField("=>", fmt.Sprintf("%s:%d", servName, cNum[0])).Debug("Mocking service definition")
	}

	srv.Operations.ContainerType = definitions.TypeService
	srv.Operations.Labels = util.Labels(servName, srv.Operations)

	ServiceFinalizeLoad(srv)
	return srv
}
Beispiel #6
0
func MockChainDefinition(chainName, chainID string, newCont bool, cNum ...int) *definitions.Chain {
	chn := definitions.BlankChain()
	chn.Name = chainName
	chn.ChainID = chainID
	chn.Service.AutoData = true

	if len(cNum) == 0 {
		chn.Operations.ContainerNumber = util.AutoMagic(cNum[0], definitions.TypeChain, newCont)
		logger.Debugf("Mocking Chain Definition =>\t%s:%d (autoassigned)\n", chainName, cNum[0])
	} else {
		chn.Operations.ContainerNumber = cNum[0]
		logger.Debugf("Mocking Chain Definition =>\t%s:%d\n", chainName, cNum[0])
	}

	chn.Operations.ContainerType = definitions.TypeChain
	chn.Operations.Labels = util.Labels(chainName, chn.Operations)

	checkChainNames(chn)
	return chn
}
Beispiel #7
0
func MockChainDefinition(chainName, chainID string, newCont bool, cNum ...int) *definitions.Chain {
	chn := definitions.BlankChain()
	chn.Name = chainName
	chn.ChainID = chainID
	chn.Service.AutoData = true

	if len(cNum) == 0 {
		chn.Operations.ContainerNumber = util.AutoMagic(cNum[0], definitions.TypeChain, newCont)
		log.WithField("=>", fmt.Sprintf("%s:%d", chainName, cNum[0])).Debug("Mocking chain definition (autoassigned)")
	} else {
		chn.Operations.ContainerNumber = cNum[0]
		log.WithField("=>", fmt.Sprintf("%s:%d", chainName, cNum[0])).Debug("Mocking chain definition")
	}

	chn.Operations.ContainerType = definitions.TypeChain
	chn.Operations.Labels = util.Labels(chainName, chn.Operations)

	checkChainNames(chn)
	return chn
}
Beispiel #8
0
func LoadServiceDefinition(servName string, newCont bool, cNum ...int) (*definitions.ServiceDefinition, error) {
	if len(cNum) == 0 {
		cNum = append(cNum, 0)
	}

	if cNum[0] == 0 {
		cNum[0] = util.AutoMagic(0, definitions.TypeService, newCont)
		logger.Debugf("Loading Service Definition =>\t%s:%d (autoassigned)\n", servName, cNum[0])
	} else {
		logger.Debugf("Loading Service Definition =>\t%s:%d\n", servName, cNum[0])
	}

	srv := definitions.BlankServiceDefinition()
	srv.Operations.ContainerType = definitions.TypeService
	srv.Operations.ContainerNumber = cNum[0]
	srv.Operations.Labels = util.Labels(servName, srv.Operations)
	serviceConf, err := loadServiceDefinition(servName)
	if err != nil {
		return nil, err
	}

	if err = MarshalServiceDefinition(serviceConf, srv); err != nil {
		return nil, err
	}

	if srv.Service == nil {
		return nil, fmt.Errorf("No service given.")
	}

	if err = checkImage(srv.Service); err != nil {
		return nil, err
	}

	// Docker 1.6 (which eris doesn't support) had different linking mechanism.
	if ver, _ := util.DockerClientVersion(); ver >= version.DVER_MIN {
		addDependencyVolumesAndLinks(srv.Dependencies, srv.Service, srv.Operations)
	}

	ServiceFinalizeLoad(srv)
	return srv, nil
}
Beispiel #9
0
// DockerRename renames the container by removing and recreating it. The container
// is also restarted if it was running before rename. The container ops.SrvContainerName
// is renamed to a new name, constructed using a short given newName.
// DockerRename returns Docker errors on exit or ErrContainerExists
// if the container with the new (long) name exists.
//
//  ops.SrvContainerName  - container name
//  ops.ContainerNumber   - container number
//  ops.ContainerType     - container type
//  ops.Labels            - container creation time labels
//
func DockerRename(ops *def.Operation, newName string) error {
	longNewName := util.ContainersName(ops.ContainerType, newName, ops.ContainerNumber)

	logger.Infof("Renaming container =>\t\t%s to %s\n", ops.SrvContainerName, longNewName)

	logger.Debugln("\tChecking container exist")

	container, err := util.DockerClient.InspectContainer(ops.SrvContainerName)
	if err != nil {
		return err
	}

	logger.Debugln("\tChecking new container exist (should fail)")

	_, err = util.DockerClient.InspectContainer(longNewName)
	if err == nil {
		return ErrContainerExists
	}

	// Mark if the container's running to restart it later.
	_, wasRunning := ContainerRunning(ops)
	if wasRunning {
		logger.Debugln("\tStopping container")
		if err := util.DockerClient.StopContainer(container.ID, 5); err != nil {
			logger.Debugln("\tNot stopped")
		}
	}

	logger.Debugln("\tRemoving container")

	removeOpts := docker.RemoveContainerOptions{
		ID:            container.ID,
		RemoveVolumes: true,
		Force:         true,
	}
	if err := util.DockerClient.RemoveContainer(removeOpts); err != nil {
		return err
	}

	logger.Debugln("\tCreating the new container")

	createOpts := docker.CreateContainerOptions{
		Name:       longNewName,
		Config:     container.Config,
		HostConfig: container.HostConfig,
	}

	// If VolumesFrom contains links to non-existent containers, remove them.
	var newVolumesFrom []string
	for _, name := range createOpts.HostConfig.VolumesFrom {
		_, err = util.DockerClient.InspectContainer(name)
		if err != nil {
			continue
		}

		name = strings.TrimSuffix(name, ":ro")
		name = strings.TrimSuffix(name, ":rw")

		newVolumesFrom = append(newVolumesFrom, name)
	}
	createOpts.HostConfig.VolumesFrom = newVolumesFrom

	// Rename labels.
	createOpts.Config.Labels = util.Labels(newName, ops)

	newContainer, err := util.DockerClient.CreateContainer(createOpts)
	if err != nil {
		logger.Debugln("Not created")
		return err
	}

	// Was running before remove.
	if wasRunning {
		err := util.DockerClient.StartContainer(newContainer.ID, createOpts.HostConfig)
		if err != nil {
			logger.Debugln("Not restarted")
		}
	}

	logger.Infoln("Container renamed")

	return nil
}