示例#1
0
func TestLoadActionDefinition(t *testing.T) {
	var e error
	actionName = strings.Replace(actionName, " ", "_", -1)
	act, _, e := LoadActionDefinition(actionName)
	if e != nil {
		log.Errorf("Error: action did not load properly: %v", e)
		t.FailNow()
	}

	actionName = strings.Replace(actionName, "_", " ", -1)
	if act.Name != actionName {
		log.Errorf("Error: improper action name on LOAD. expected: %s got: %s", actionName, act.Name)
		t.Fail()
	}
}
示例#2
0
文件: manage.go 项目: mxjxn/eris-cli
// TODO: skip errors flag
func RmData(do *definitions.Do) (err error) {
	if len(do.Operations.Args) == 0 {
		do.Operations.Args = []string{do.Name}
	}
	for _, name := range do.Operations.Args {
		do.Name = name
		if util.IsDataContainer(do.Name, do.Operations.ContainerNumber) {
			log.WithField("=>", do.Name).Info("Removing data container")

			srv := definitions.BlankServiceDefinition()
			srv.Operations.SrvContainerName = util.ContainersName("data", do.Name, do.Operations.ContainerNumber)

			if err = perform.DockerRemove(srv.Service, srv.Operations, false, do.Volumes); err != nil {
				log.Errorf("Error removing %s: %v", do.Name, err)
				return err
			}

		} else {
			err = fmt.Errorf("I cannot find that data container for %s. Please check the data container name you sent me.", do.Name)
			log.Error(err)
			return err
		}

		if do.RmHF {
			log.WithField("=>", do.Name).Warn("Removing host directory")
			if err = os.RemoveAll(filepath.Join(DataContainersPath, do.Name)); err != nil {
				return err
			}
		}
	}

	do.Result = "success"
	return err
}
示例#3
0
func removeErisImages(prompt bool) error {
	opts := docker.ListImagesOptions{
		All:     true,
		Filters: nil,
		Digests: false,
	}
	allTheImages, err := DockerClient.ListImages(opts)
	if err != nil {
		return err
	}

	//get all repo tags & IDs
	// [zr] this could probably be cleaner
	repoTags := make(map[int][]string)
	imageIDs := make(map[int]string)
	for i, image := range allTheImages {
		repoTags[i] = image.RepoTags
		imageIDs[i] = image.ID
	}

	erisImages := []string{}
	erisImageIDs := []string{}

	//searches through repo tags for eris images & "maps" to ID
	for i, repoTag := range repoTags {
		for _, rt := range repoTag {
			r, err := regexp.Compile(`eris`)
			if err != nil {
				log.Errorf("Regexp error: %v", err)
			}

			if r.MatchString(rt) == true {
				erisImages = append(erisImages, rt)
				erisImageIDs = append(erisImageIDs, imageIDs[i])
			}
		}
	}

	if !prompt || canWeRemove(erisImages, "images") {
		for i, imageID := range erisImageIDs {
			log.WithFields(log.Fields{
				"=>": erisImages[i],
				"id": imageID,
			}).Debug("Removing image")
			if err := DockerClient.RemoveImage(imageID); err != nil {
				return err
			}
		}
	} else {
		log.Warn("Permission to remove images not given, continuing with clean")
	}
	return nil
}
示例#4
0
func startInteractiveContainer(opts docker.CreateContainerOptions) error {
	// Trap signals so we can drop out of the container.
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	go func() {
		<-c
		log.WithField("=>", opts.Name).Info("Caught signal. Stopping container")
		if err := stopContainer(opts.Name, 5); err != nil {
			log.Errorf("Error stopping container: %v", err)
		}
	}()

	attached := make(chan struct{})
	go func(chan struct{}) {
		attachContainer(opts.Name, attached)
	}(attached)

	// Wait for a console prompt to appear.
	_, ok := <-attached
	if ok {
		attached <- struct{}{}
	}

	if err := startContainer(opts); err != nil {
		return err
	}

	log.WithField("=>", opts.Name).Info("Waiting for container to exit")

	// Set terminal into raw mode, and restore upon container exit.
	savedState, err := term.SetRawTerminal(os.Stdin.Fd())
	if err != nil {
		log.Info("Cannot set the terminal into raw mode")
	} else {
		defer term.RestoreTerminal(os.Stdin.Fd(), savedState)
	}

	if err := waitContainer(opts.Name); err != nil {
		return err
	}

	return nil
}
示例#5
0
func TestExportData(t *testing.T) {
	testCreateDataByImport(t, dataName)
	defer testKillDataCont(t, dataName)

	do := definitions.NowDo()
	do.Name = dataName
	do.Source = common.ErisContainerRoot
	do.Destination = filepath.Join(common.DataContainersPath, do.Name)
	do.Operations.ContainerNumber = 1
	if err := ExportData(do); err != nil {
		log.Error(err)
		t.FailNow()
	}

	if _, err := os.Stat(filepath.Join(common.DataContainersPath, dataName, "test")); os.IsNotExist(err) {
		log.Errorf("Tragic! Exported file does not exist: %s", err)
		t.Fail()
	}

}
示例#6
0
func TestRenameService(t *testing.T) {
	testStartService(t, "keys", false)
	testExistAndRun(t, "keys", 1, true, true)
	testNumbersExistAndRun(t, "keys", 1, 1)

	do := def.NowDo()
	do.Name = "keys"
	do.NewName = "syek"
	log.WithFields(log.Fields{
		"from": do.Name,
		"to":   do.NewName,
	}).Debug("Renaming service (from tests)")
	if e := RenameService(do); e != nil {
		tests.IfExit(fmt.Errorf("Error (tests fail) =>\t\t%v\n", e))
	}

	testExistAndRun(t, "syek", 1, true, true)
	testExistAndRun(t, "keys", 1, false, false)
	testNumbersExistAndRun(t, "syek", 1, 1)
	testNumbersExistAndRun(t, "keys", 0, 0)

	do = def.NowDo()
	do.Name = "syek"
	do.NewName = "keys"
	log.WithFields(log.Fields{
		"from": do.Name,
		"to":   do.NewName,
	}).Debug("Renaming service (from tests)")
	if e := RenameService(do); e != nil {
		log.Errorf("Error (tests fail): %v", e)
		tests.IfExit(e)
	}

	testExistAndRun(t, "keys", 1, true, true)
	testExistAndRun(t, "syek", 1, false, false)
	testNumbersExistAndRun(t, "keys", 1, 1)
	testNumbersExistAndRun(t, "syek", 0, 0)

	testKillService(t, "keys", true)
	testExistAndRun(t, "keys", 1, false, false)
}
示例#7
0
func TestPlopChain(t *testing.T) {
	cfgFilePath := filepath.Join(common.ChainsPath, "default", "config.toml")

	do := def.NowDo()
	do.ConfigFile = cfgFilePath
	do.Name = chainName
	do.Operations.ContainerNumber = 1
	do.Operations.PublishAllPorts = true
	log.WithField("=>", chainName).Info("Creating chain (from tests)")
	tests.IfExit(NewChain(do))
	defer testKillChain(t, chainName)

	do = def.NowDo()
	do.Type = "config"
	do.ChainID = chainName

	newWriter := new(bytes.Buffer)
	config.GlobalConfig.Writer = newWriter
	e := PlopChain(do)
	if e != nil {
		log.Error(e)
		t.Fail()
	}

	cfgFile, err := ioutil.ReadFile(cfgFilePath)
	tests.IfExit(err)
	cfgFilePlop := newWriter.Bytes()

	// remove [13] that shows up everywhere ...
	cfgFile = bytes.Replace(cfgFile, []byte{13}, []byte{}, -1)
	cfgFilePlop = bytes.Replace(cfgFilePlop, []byte{13}, []byte{}, -1)

	if !bytes.Equal(cfgFile, cfgFilePlop) {
		log.Errorf("Error: Got: %s. Expected: %s", cfgFilePlop, cfgFile)
		log.WithFields(log.Fields{
			"got":      cfgFilePlop,
			"expected": cfgFile,
		}).Error("Error comparing config files")
		t.Fail()
	}
}
示例#8
0
func DockerConnect(verbose bool, machName string) { // TODO: return an error...?
	var err error
	var dockerHost string
	var dockerCertPath string
	if runtime.GOOS == "linux" {
		// this means we aren't gonna use docker-machine (kind of)
		if (machName == "eris" || machName == "default") && (os.Getenv("DOCKER_HOST") == "" && os.Getenv("DOCKER_CERT_PATH") == "") {
			//if os.Getenv("DOCKER_HOST") == "" && os.Getenv("DOCKER_CERT_PATH") == "" {
			endpoint := "unix:///var/run/docker.sock"

			log.WithField("=>", endpoint).Debug("Checking Linux Docker socket")
			u, _ := url.Parse(endpoint)
			_, err := net.Dial(u.Scheme, u.Path)
			if err != nil {
				IfExit(fmt.Errorf("%v\n", mustInstallError()))
			}
			log.WithField("=>", endpoint).Debug("Connecting to Docker")
			DockerClient, err = docker.NewClient(endpoint)
			if err != nil {
				IfExit(fmt.Errorf("%v\n", mustInstallError()))
			}
		} else {
			log.WithFields(log.Fields{
				"host":      os.Getenv("DOCKER_HOST"),
				"cert path": os.Getenv("DOCKER_CERT_PATH"),
			}).Debug("Getting connection details from environment")
			log.WithField("machine", machName).Debug("Getting connection details from Docker Machine")
			dockerHost, dockerCertPath, err = getMachineDeets(machName)
			if err != nil {
				IfExit(fmt.Errorf("Error getting Docker Machine details for connection via TLS.\nERROR =>\t\t\t%v\n\nEither re-run the command without a machine or correct your machine name.\n", err))
			}

			log.WithFields(log.Fields{
				"host":      dockerHost,
				"cert path": dockerCertPath,
			}).Debug()

			if err := connectDockerTLS(dockerHost, dockerCertPath); err != nil {
				IfExit(fmt.Errorf("Error connecting to Docker via TLS.\nERROR =>\t\t\t%v\n", err))
			}

			setIPFSHostViaDockerHost(dockerHost)
		}

		log.Debug("Successfully connected to Docker daemon")

	} else {
		log.WithFields(log.Fields{
			"host":      os.Getenv("DOCKER_HOST"),
			"cert path": os.Getenv("DOCKER_CERT_PATH"),
		}).Debug("Getting connection details from environment")
		log.WithField("machine", machName).Debug("Getting connection details from Docker Machine")

		dockerHost, dockerCertPath, err = getMachineDeets(machName) // machName is "eris" by default

		if err != nil {
			log.Debug("Could not connect to Eris Docker Machine")
			log.Errorf("Trying %q Docker Machine: %v", "default", err)
			dockerHost, dockerCertPath, err = getMachineDeets("default") // during toolbox setup this is the machine that is created
			if err != nil {
				log.Debugf("Could not connect to %q Docker Machine", "default")
				log.Debugf("Error: %v", err)
				log.Debug("Trying to set up new machine")
				if e2 := CheckDockerClient(); e2 != nil {
					IfExit(fmt.Errorf("%v\n", e2))
				}
				dockerHost, dockerCertPath, _ = getMachineDeets("eris")
			}

		}

		log.WithFields(log.Fields{
			"host":      dockerHost,
			"cert path": dockerCertPath,
		}).Debug()

		if err := connectDockerTLS(dockerHost, dockerCertPath); err != nil {
			IfExit(fmt.Errorf("Error connecting to Docker Backend via TLS.\nERROR =>\t\t\t%v\n", err))
		}
		log.Debug("Successfully connected to Docker daemon")

		setIPFSHostViaDockerHost(dockerHost)
	}
}