示例#1
0
func DeleteNodebyData(path string, conn *zk.Conn, data []byte) error {

	children, _, err := conn.Children(path)
	if err != nil {
		glog.Warning("Could not list children")
		return err
	}
	for _, child := range children {
		child_path := path + "/" + child
		child_data, _, err := conn.Get(child_path)
		if err != nil {
			glog.Warning("Could not get data for %s", child_path)
			continue
		}
		if bytes.Compare(data, child_data) == 0 {
			for i := 0; i < 5; i++ {
				_, stats, _ := conn.Get(child_path)
				err = conn.Delete(child_path, stats.Version)
				if err == nil || err == zk.ErrNoNode {
					return nil
				}
			}
			glog.Error("Could not delete matched node %s", child_path)
		}
	}
	return nil
}
示例#2
0
func (p *ProcessInstance) ReadResponse(ns *socketio.NameSpace) {
	ns.On("stdout", func(n *socketio.NameSpace, stdout string) {
		glog.V(4).Infof("Process received stdout: %s", stdout)
		if p.closed {
			glog.Warning("connection closed; cannot write stdout: %s", stdout)
		} else {
			for _, b := range []byte(stdout) {
				p.Stdout <- b
			}
		}
	})

	ns.On("stderr", func(n *socketio.NameSpace, stderr string) {
		glog.V(4).Infof("Process received stderr: %s", stderr)
		if p.closed {
			glog.Warning("connection closed; cannot write stderr: %s", stderr)
		} else {
			for _, b := range []byte(stderr) {
				p.Stderr <- b
			}
		}
	})

	ns.On("result", func(n *socketio.NameSpace, result Result) {
		glog.V(0).Infof("Process received result: %s", result)
		p.Result <- result
	})
	glog.V(0).Info("Hooked up outgoing events!")
}
示例#3
0
// this function takes a filter directory and creates a map
// of filters by looking at the content in that directory.
// it is assumed the filter name is the name of the file minus
// the .conf part. So test.conf would be a filter named "test"
func getFiltersFromDirectory(path string) (filters map[string]string, err error) {
	filters = make(map[string]string)
	subpaths, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}
	for _, subpath := range subpaths {
		filterName := subpath.Name()

		// make sure it is a valid filter
		if !strings.HasSuffix(filterName, ".conf") {
			glog.Warning("Skipping %s because it doesn't have a .conf extension", filterName)
			continue
		}
		// read the contents and add it to our map
		contents, err := ioutil.ReadFile(path + "/" + filterName)
		if err != nil {
			glog.Errorf("Unable to read the file %s, skipping", path+"/"+filterName)
			continue
		}
		filterName = strings.TrimSuffix(filterName, ".conf")
		filters[filterName] = string(contents)
	}
	glog.V(2).Infof("Here are the filters %v from path %s", filters, path)
	return filters, nil
}
示例#4
0
func (p *ProcessInstance) ReadRequest(ns *socketio.NameSpace) {
	ns.On("signal", func(n *socketio.NameSpace, signal int) {
		glog.V(4).Infof("received signal %d", signal)
		if p.disconnected {
			glog.Warning("disconnected; cannot send signal: %s", signal)
		}
	})

	ns.On("stdin", func(n *socketio.NameSpace, stdin string) {
		glog.V(4).Infof("Received stdin: %s", stdin)
		if p.disconnected {
			glog.Warning("disconnected; cannot send stdin: %s", stdin)
		} else {
			for _, b := range []byte(stdin) {
				p.Stdin <- b
			}
		}
	})

	glog.V(0).Info("Hooked up incoming events!")
}
示例#5
0
func (c *Controller) checkPrereqs(prereqsPassed chan bool, rpcDead chan struct{}) error {
	if len(c.prereqs) == 0 {
		glog.Infof("No prereqs to pass.")
		prereqsPassed <- true
		return nil
	}
	healthCheckInterval := time.Tick(1 * time.Second)
	for {
		select {
		case <-rpcDead:
			glog.Fatalf("Exiting, RPC server has gone away")
		case <-healthCheckInterval:
			failedAny := false
			for _, script := range c.prereqs {
				glog.Infof("Running prereq command: %s", script.Script)
				cmd := exec.Command("sh", "-c", script.Script)
				err := cmd.Run()
				if err != nil {
					msg := fmt.Sprintf("Not starting service yet, waiting on prereq: %s", script.Name)
					glog.Warning(msg)
					fmt.Fprintln(os.Stderr, msg)
					failedAny = true
					break
				} else {
					glog.Infof("Passed prereq [%s].", script.Name)
				}
			}
			if !failedAny {
				glog.Infof("Passed all prereqs.")
				prereqsPassed <- true
				return nil
			}
		}
	}
	return nil
}
示例#6
0
// createVolumeDir() creates a directory on the running host using the user ids
// found within the specified image. For example, it can create a directory owned
// by the mysql user (as seen by the container) despite there being no mysql user
// on the host system.
// Assumes that the local docker image (imageSpec) exists and has been sync'd
// with the registry.
func createVolumeDir(hostPath, containerSpec, imageSpec, userSpec, permissionSpec string) error {

	createVolumeDirMutex.Lock()
	defer createVolumeDirMutex.Unlock()

	dotfileCompatibility := path.Join(hostPath, ".serviced.initialized") // for compatibility with previous versions of serviced
	dotfileHostPath := path.Join(filepath.Dir(hostPath), fmt.Sprintf(".%s.serviced.initialized", filepath.Base(hostPath)))
	dotfiles := []string{dotfileCompatibility, dotfileHostPath}
	for _, dotfileHostPath := range dotfiles {
		_, err := os.Stat(dotfileHostPath)
		if err == nil {
			glog.V(2).Infof("DFS volume initialized earlier for src:%s dst:%s image:%s user:%s perm:%s", hostPath, containerSpec, imageSpec, userSpec, permissionSpec)
			return nil
		}
	}

	starttime := time.Now()

	var err error
	var output []byte
	command := [...]string{
		"docker", "run",
		"--rm",
		"-v", hostPath + ":/mnt/dfs",
		imageSpec,
		"/bin/bash", "-c",
		fmt.Sprintf(`
set -e
if [ ! -d "%s" ]; then
	echo "WARNING: DFS mount %s does not exist in image %s"
else
	cp -rp %s/. /mnt/dfs/
fi
chown %s /mnt/dfs
chmod %s /mnt/dfs
sync
`, containerSpec, containerSpec, imageSpec, containerSpec, userSpec, permissionSpec),
	}

	for i := 0; i < 2; i++ {
		docker := exec.Command(command[0], command[1:]...)
		output, err = docker.CombinedOutput()
		if err == nil {
			duration := time.Now().Sub(starttime)
			if strings.Contains(string(output), "WARNING:") {
				glog.Warning(string(output))
			} else {
				glog.Info(string(output))
			}
			glog.Infof("DFS volume init #%d took %s for src:%s dst:%s image:%s user:%s perm:%s", i, duration, hostPath, containerSpec, imageSpec, userSpec, permissionSpec)

			if e := ioutil.WriteFile(dotfileHostPath, []byte(""), 0664); e != nil {
				glog.Errorf("unable to create DFS volume initialized dotfile %s: %s", dotfileHostPath, e)
				return e
			}
			return nil
		}
		time.Sleep(time.Second)
		glog.Warningf("retrying due to error creating DFS volume %+v: %s", hostPath, string(output))
	}

	glog.Errorf("could not create DFS volume %+v: %s", hostPath, string(output))
	return err
}