示例#1
0
func (r *Stop) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	conn, err := us.Dial()
	if err != nil {
		return nil, err
	}

	job, err := conn.Job(r.Name)
	if err != nil {
		return nil, err
	}

	err = job.Stop()
	if err != nil {
		if strings.Index(err.Error(), "Unknown instance") == 0 {
			res := tachyon.NewResult(false)
			res.Add("name", r.Name)

			return res, nil
		}
	}

	res := tachyon.NewResult(true)
	res.Add("name", r.Name)

	return res, nil
}
示例#2
0
func (r *Restart) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	conn, err := us.Dial()
	if err != nil {
		return nil, err
	}

	job, err := conn.Job(r.Name)
	if err != nil {
		return nil, err
	}

	inst, err := job.Restart()
	if err != nil {
		return nil, err
	}

	pid, err := inst.Pid()
	if err != nil {
		return nil, err
	}

	res := tachyon.NewResult(true)
	res.Add("name", r.Name)
	res.Add("pid", pid)

	return res, nil
}
示例#3
0
func (t *Task) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	cfg := us.TaskConfig(t.Name, t.Command)

	cfg.Instance = t.Instance

	err := setScript(env, &cfg.PreStart, t.PreStart)
	if err != nil {
		return nil, err
	}

	err = setScript(env, &cfg.PostStart, t.PostStart)
	if err != nil {
		return nil, err
	}

	err = setScript(env, &cfg.PreStop, t.PreStop)
	if err != nil {
		return nil, err
	}

	err = setScript(env, &cfg.PostStop, t.PostStop)
	if err != nil {
		return nil, err
	}

	err = cfg.Install()
	if err != nil {
		return nil, err
	}

	res := tachyon.NewResult(true)
	res.Add("name", t.Name)

	return res, nil
}
示例#4
0
func (r *Start) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	conn, err := us.Dial()
	if err != nil {
		return nil, err
	}

	var ienv []string

	for k, v := range r.Env {
		ienv = append(ienv, fmt.Sprintf("%s=%s", k, v))
	}

	job, err := conn.Job(r.Name)
	if err != nil {
		return nil, err
	}

	inst, err := job.StartWithOptions(ienv, true)
	if err != nil {
		if strings.Index(err.Error(), "Job is already running") == 0 {
			res := tachyon.NewResult(false)
			res.Add("name", r.Name)

			return res, nil
		}
		return nil, err
	}

	pid, err := inst.Pid()
	if err != nil {
		return nil, err
	}

	res := tachyon.NewResult(true)
	res.Add("name", r.Name)
	res.Add("pid", pid)

	return res, nil
}
示例#5
0
func (d *Daemon) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	cfg := us.DaemonConfig(d.Name, d.Command)
	cfg.Env = d.Env

	if d.Foreground {
		cfg.Foreground()
	}

	if d.OneFork {
		cfg.Expect = "fork"
	}

	err := setScript(env, &cfg.PreStart, d.PreStart)
	if err != nil {
		return nil, err
	}

	err = setScript(env, &cfg.PostStart, d.PostStart)
	if err != nil {
		return nil, err
	}

	err = setScript(env, &cfg.PreStop, d.PreStop)
	if err != nil {
		return nil, err
	}

	err = setScript(env, &cfg.PostStop, d.PostStop)
	if err != nil {
		return nil, err
	}

	cfg.Instance = d.Instance

	err = cfg.Install()
	if err != nil {
		return nil, err
	}

	res := tachyon.NewResult(true)
	res.Add("name", d.Name)

	return res, nil
}
示例#6
0
func (s *S3) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	auth, err := aws.GetAuth("", "", "", time.Time{})
	if err != nil {
		return nil, err
	}

	c := s3.New(auth, aws.USWest2)
	b := c.Bucket(s.Bucket)

	res := tachyon.NewResult(true)

	res.Add("bucket", s.Bucket)
	res.Add("remote", s.At)

	if s.PutFile != "" {
		path := env.Paths.File(s.PutFile)

		f, err := os.Open(path)
		if err != nil {
			return nil, err
		}

		if f == nil {
			return nil, fmt.Errorf("Unknown local file %s", s.PutFile)
		}

		defer f.Close()

		var perm s3.ACL

		if s.Public {
			if s.Writable {
				perm = s3.PublicReadWrite
			} else {
				perm = s3.PublicRead
			}
		} else {
			perm = s3.Private
		}

		ct := s.ContentType
		if ct == "" {
			ct = "application/octet-stream"
		}

		fi, err := f.Stat()
		if err != nil {
			return nil, err
		}

		var (
			input io.Reader
			opts  s3.Options
			size  int64
		)

		h := md5.New()

		if s.GZip {
			var buf bytes.Buffer

			z := gzip.NewWriter(io.MultiWriter(h, &buf))

			_, err = io.Copy(z, f)
			if err != nil {
				return nil, err
			}

			z.Close()

			opts.ContentEncoding = "gzip"

			input = &buf
			size = int64(buf.Len())
		} else {
			input = io.TeeReader(f, h)
			size = fi.Size()
		}

		err = b.PutReader(s.At, input, size, ct, perm, opts)

		rep, err := b.Head(s.At, nil)
		if err != nil {
			return nil, err
		}

		localMD5 := hex.EncodeToString(h.Sum(nil))

		res.Add("wrote", size)
		res.Add("local", s.PutFile)
		res.Add("md5", localMD5)

		etag := rep.Header.Get("ETag")
		if etag != "" {
			etag = etag[1 : len(etag)-1]

			if localMD5 != etag {
				return nil, fmt.Errorf("corruption uploading file detected")
			}
		}

	} else if s.GetFile != "" {
		f, err := os.OpenFile(s.GetFile, os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			return nil, err
		}

		defer f.Close()

		i, err := b.GetReader(s.At)
		if err != nil {
			return nil, err
		}

		defer i.Close()

		n, err := io.Copy(f, i)
		if err != nil {
			return nil, err
		}

		res.Add("read", n)
		res.Add("local", s.GetFile)
	} else {
		return nil, fmt.Errorf("Specify put_file or get_file")
	}

	return res, nil
}
示例#7
0
func (a *Apt) Run(env *tachyon.CommandEnv) (*tachyon.Result, error) {
	state := a.State
	if state == "" {
		state = "present"
	}

	if a.Cache {
		home, err := tachyon.HomeDir()
		if err != nil {
			return nil, err
		}

		checkFile := filepath.Join(home, ".tachyon", "apt-cache-timestamp")

		runUpdate := true

		if a.CacheTime != "" {
			fi, err := os.Stat(checkFile)
			if err == nil {
				dur, err := time.ParseDuration(a.CacheTime)
				if err != nil {
					return nil, fmt.Errorf("cache_time was not in the proper format")
				}

				runUpdate = time.Now().After(fi.ModTime().Add(dur))
			}
		}

		if runUpdate {
			_, err := tachyon.RunCommand(env, "apt-get", "update")
			if err != nil {
				return nil, err
			}
			ioutil.WriteFile(checkFile, []byte(``), 0666)
		}
	}

	if a.Pkg == "" {
		simp := tachyon.NewResult(true)
		simp.Add("cache", "updated")

		return simp, nil
	}

	out, err := tachyon.RunCommand(env, "apt-cache", "policy", a.Pkg)
	if err != nil {
		return nil, err
	}

	res := installed.FindSubmatch(out.Stdout)
	if res == nil {
		return nil, fmt.Errorf("No package '%s' available", a.Pkg)
	}

	curVer := string(res[1])
	if curVer == "(none)" {
		curVer = ""
	}

	res = candidate.FindSubmatch(out.Stdout)
	if res == nil {
		return nil, fmt.Errorf("Error parsing apt-cache output")
	}

	canVer := string(res[1])

	if state == "absent" {
		rd := tachyon.ResultData{}

		if curVer == "" {
			return tachyon.WrapResult(false, rd), nil
		}

		rd.Set("removed", curVer)

		_, err = tachyon.RunCommand(env, "apt-get", "remove", "-y", a.Pkg)

		if err != nil {
			return nil, err
		}

		return tachyon.WrapResult(true, rd), nil
	}

	rd := tachyon.ResultData{}
	rd.Set("installed", curVer)
	rd.Set("candidate", canVer)

	if state == "present" && curVer == canVer {
		return tachyon.WrapResult(false, rd), nil
	}

	if a.Dry {
		rd.Set("dryrun", true)
		return tachyon.WrapResult(true, rd), nil
	}

	e := append(os.Environ(), "DEBIAN_FRONTEND=noninteractive", "DEBIAN_PRIORITY=critical")

	_, err = tachyon.RunCommandInEnv(env, e, "apt-get", "install", "-y", a.Pkg)
	if err != nil {
		return nil, err
	}

	rd.Set("installed", canVer)

	return tachyon.WrapResult(true, rd), nil
}