Ejemplo n.º 1
0
func (r *Stop) Run(env *goansible.CommandEnv) (*goansible.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 := goansible.NewResult(false)
			res.Add("name", r.Name)

			return res, nil
		}
	}

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

	return res, nil
}
Ejemplo n.º 2
0
func (r *Restart) Run(env *goansible.CommandEnv) (*goansible.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 := goansible.NewResult(true)
	res.Add("name", r.Name)
	res.Add("pid", pid)

	return res, nil
}
Ejemplo n.º 3
0
func (r *Start) Run(env *goansible.CommandEnv) (*goansible.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 := goansible.NewResult(false)
			res.Add("name", r.Name)

			return res, nil
		}
		return nil, err
	}

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

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

	return res, nil
}
Ejemplo n.º 4
0
func (d *Daemon) Run(env *goansible.CommandEnv) (*goansible.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 := goansible.NewResult(true)
	res.Add("name", d.Name)

	return res, nil
}
Ejemplo n.º 5
0
func (t *Task) Run(env *goansible.CommandEnv) (*goansible.Result, error) {
	cfg := us.TaskConfig(t.Name, t.Command)
	cfg.Env = t.Env

	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 := goansible.NewResult(true)
	res.Add("name", t.Name)

	return res, nil
}
Ejemplo n.º 6
0
func (a *Apt) Run(env *goansible.CommandEnv) (*goansible.Result, error) {
	state := a.State
	if state == "" {
		state = "present"
	}

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

		checkFile := filepath.Join(home, ".goansible", "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 := goansible.RunCommand(env, "apt-get", "update")
			if err != nil {
				return nil, err
			}
			ioutil.WriteFile(checkFile, []byte(``), 0666)
		}
	}

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

		return simp, nil
	}

	out, err := goansible.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 := goansible.ResultData{}

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

		rd.Set("removed", curVer)

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

		if err != nil {
			return nil, err
		}

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

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

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

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

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

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

	rd.Set("installed", canVer)

	return goansible.WrapResult(true, rd), nil
}
Ejemplo n.º 7
0
func (s *S3) S3CpFile(a, b string) (*goansible.Result, error) {
	if s.Region == "" {
		s.Region = "ap-northeast-1"
	}
	S3 := s3.New(session.New(), &aws.Config{Region: aws.String(s.Region)})

	res := goansible.NewResult(true)

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

	if s.PutFile != "" {

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

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

		defer f.Close()

		var perm string

		if s.Public {
			if s.Writable {
				perm = "public-read-write"
			} else {
				perm = "public-read"
			}
		} else {
			perm = "private"
		}

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

		var po *s3.PutObjectOutput
		//	var pr *s3.PutObjectInput
		if s.GZip {
			_, po, err = s.ZipUploadReaderD(S3, s.At, f, ct, perm)
		} else {
			_, po, err = s.UploadFileD(S3, s.At, f, ct, perm)
		}
		if err != nil {
			return nil, err
		}

		//s.At: path ct:contentType, perm ,opts

		md5 := (*po.ETag)[1 : len(*po.ETag)-1]

		//pp.Print(pr)
		//res.Add("wrote", *pr.ContentLength)
		res.Add("local", s.PutFile)
		res.Add("md5sum", md5)

	} else if s.GetFile != "" {
		_, err := os.Stat(s.GetFile)
		if !os.IsNotExist(err) {
			f, err := os.Open(s.GetFile)
			if err != nil {
				return nil, err
			}
			md5hex, _, _, err := Md5Sum(f)
			f.Close()
			if err != nil {
				return nil, err
			}
			req := &s3.HeadObjectInput{
				Bucket: &s.Bucket,
				Key:    &s.At,
			}
			r, err := S3.HeadObject(req)
			if err != nil {
				return nil, err
			}
			if *r.ETag == `"`+md5hex+`"` {
				res.Changed = false
				res.Add("size", *r.ContentLength)
				res.Add("md5sum", md5hex)
				res.Add("local", s.GetFile)
				return res, nil
			}

		}
		if s.Mkdir {
			if err := os.MkdirAll(filepath.Dir(s.GetFile), 0755); err != nil {
				return nil, err
			}
		}

		req := &s3.GetObjectInput{
			Bucket: &s.Bucket,
			Key:    &s.At,
		}
		resGetObj, err := S3.GetObject(req)
		if err != nil {
			return nil, err
		}
		defer resGetObj.Body.Close()

		dist, err := os.OpenFile(s.GetFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
		if err != nil {
			return nil, err
		}
		defer dist.Close()
		n, err := io.Copy(dist, resGetObj.Body)
		if err != nil {
			return nil, err
		}

		res.Add("read", n)
		res.Add("md5sum", (*resGetObj.ETag)[1:len(*resGetObj.ETag)-1])
		res.Add("local", s.GetFile)
	} else {
		return nil, fmt.Errorf("Specify put_file or get_file")
	}

	return res, nil
}