Example #1
0
func (d *Directory) Run() (result *task.Result, err error) {
	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "directory"
		result.Extra["state"] = string(d.State)
		result.Extra["path"] = d.Path
		if d.State == Absent {
			result.Op = "remove"
			result, err = d.ensureAbsent(result)
		} else {
			result.Op = "create"
			result.Extra["mode"] = fmt.Sprintf("%o", d.Mode)
			result, err = d.ensurePresent(result)
		}
		return
	})

	if d.Owner != "" || d.Group != "" {
		chown := &file.Chown{
			Path:      d.Path,
			Owner:     d.Owner,
			Group:     d.Group,
			Recursive: false,
		}
		result, err = chown.Run()
	}
	return
}
Example #2
0
func (s *Service) ensureAutoStart(enabled bool) (result *task.Result, err error) {
	oldEnabled, err := s.autoStartEnabled()
	if err != nil {
		return
	}

	result, err = task.DoRun(func(result *task.Result) error {
		result.Module = "service"
		result.Op = "auto_start"
		result.Extra["name"] = s.Name

		var op string
		if enabled {
			if !oldEnabled {
				op = "on"
			}
		} else {
			if oldEnabled {
				op = "off"
			}
		}
		if op == "" {
			result.Skipped = true
			return nil
		} else {
			return result.ExecCommand("chkconfig", s.Name, op)
		}
	})
	return
}
Example #3
0
func (s *Shell) Run() (result *task.Result, err error) {
	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "shell"
		result.Extra["cmd"] = s.Command
		if s.Chdir != "" {
			result.Extra["chdir"] = s.Chdir
		}
		if s.Creates != "" {
			result.Extra["creates"] = s.Creates
		}

		var shell string
		if s.Shell == "" {
			shell = DEFAULT_SHELL
		} else {
			shell = s.Shell
		}
		result.Extra["shell"] = shell

		if s.Creates != "" && osutil.Exists(s.Creates) {
			result.Skipped = true
			return
		}

		var command string
		if s.Chdir != "" {
			command = "cd " + executil.QuoteWord(s.Chdir) + "; " + s.Command
		} else {
			command = s.Command
		}
		err = result.ExecCommand(shell, "-c", command)
		return
	})
	return
}
Example #4
0
func (y *Yum) Run() (result *task.Result, err error) {
	installed, err := y.isInstalled()
	if err != nil {
		return
	}

	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "yum"
		result.Extra["name"] = y.Name

		if y.State == Installed {
			result.Op = "install"
			if installed {
				result.Skipped = true
			} else {
				err = result.ExecCommand(
					"yum", "install", "-d", "2", "-y", y.Name)
				if strings.Contains(result.Stdout, "\nNothing to do\n") {
					result.Changed = false
				}
			}
		} else {
			result.Op = "remove"
			if installed {
				err = result.ExecCommand("yum", "-C", "remove", "-y", y.Name)
			} else {
				result.Skipped = true
			}
		}
		return
	})
	return
}
Example #5
0
func (c *Chmod) Run() (result *task.Result, err error) {
	oldModes, err := c.getModes()
	if err != nil {
		return
	}

	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "chmod"
		result.Op = "chmod"
		modeStr := fmt.Sprintf("%o", c.Mode)
		result.Extra["path"] = c.Path
		result.Extra["mode"] = modeStr
		result.Extra["recursive"] = c.Recursive
		result.Extra["old_modes"] = oldModes

		if len(oldModes) == 1 {
			oldMode := oldModes[0]

			if modeStr == oldMode {
				result.Skipped = true
				return
			}
		}

		var args []string
		if c.Recursive {
			args = []string{"-R", modeStr, c.Path}
		} else {
			args = []string{modeStr, c.Path}
		}
		err = result.ExecCommand("chmod", args...)
		return
	})
	return
}
Example #6
0
func (t *Template) Run() (result *task.Result, err error) {
	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "template"
		result.Op = "create"
		result.Extra["path"] = t.Path
		result.Extra["content"] = t.Content
		result.Extra["data"] = t.Data
		result.Extra["mode"] = fmt.Sprintf("%o", t.Mode)

		tmpl, err := templateutil.NewWithString(t.Path, t.Content)
		if err != nil {
			return
		}

		changed, err := templateutil.WriteIfChanged(tmpl, t.Data, t.Path,
			t.Mode)
		if err != nil {
			return
		}

		if changed {
			result.Changed = true
		} else {
			result.Skipped = true
		}
		return
	})
	return
}
Example #7
0
func (s *Service) autoStartEnabled() (enabled bool, err error) {
	result, err := task.DoRun(func(result *task.Result) error {
		result.Module = "service"
		result.Op = "check_auto_start"
		result.Extra["name"] = s.Name
		err := result.ExecCommand("chkconfig", s.Name, "--list")
		result.Changed = false
		return err
	})
	enabled = strings.Contains(result.Stdout, "\t2:on\t")
	return
}
Example #8
0
func (g *Group) Run() (result *task.Result, err error) {
	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "group"
		result.Extra["state"] = string(g.State)
		result.Extra["name"] = g.Name
		if g.State == Absent {
			result.Op = "remove"
			result, err = g.ensureAbsent(result)
		} else {
			result.Extra["gid"] = g.Gid
			result.Extra["system"] = g.System
			result, err = g.ensurePresent(result)
		}
		return
	})
	return
}
Example #9
0
func (s *Service) ensureState(state State) (result *task.Result, err error) {
	oldState, err := s.state()
	if err != nil {
		return
	}

	result, err = task.DoRun(func(result *task.Result) error {
		result.Module = "service"
		result.Op = "change_state"
		result.Extra["name"] = s.Name
		result.Extra["state"] = string(state)

		var op string
		switch s.State {
		case Started:
			if oldState == Stopped {
				op = "start"
			}
		case Stopped:
			if oldState == Started {
				op = "stop"
			}
		case Restarted:
			if oldState == Started {
				op = "restart"
			} else {
				op = "start"
			}
		case Reloaded:
			if oldState == Started {
				op = "reload"
			} else {
				op = "start"
			}
		}

		if op == "" {
			result.Skipped = true
			return nil
		} else {
			return result.ExecCommand("service", s.Name, op)
		}
	})
	return
}
Example #10
0
func (c *Chown) Run() (result *task.Result, err error) {
	oldOwners, err := c.getOwners()
	if err != nil {
		return
	}

	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "chown"
		result.Op = "chown"
		result.Extra["path"] = c.Path
		result.Extra["owner"] = c.Owner
		result.Extra["group"] = c.Group
		result.Extra["recursive"] = c.Recursive

		if len(oldOwners) == 1 {
			index := strings.Index(oldOwners[0], ":")
			oldOwner := oldOwners[0][:index]
			oldGroup := oldOwners[0][index+1:]

			if (c.Owner == "" || c.Owner == oldOwner) &&
				(c.Group == "" || c.Group == oldGroup) {
				result.Skipped = true
				return
			}
		}

		var owner string
		if c.Group != "" {
			owner = c.Owner + ":" + c.Group
		} else {
			owner = c.Owner
		}
		var args []string
		if c.Recursive {
			args = []string{"-R", owner, c.Path}
		} else {
			args = []string{owner, c.Path}
		}
		err = result.ExecCommand("chown", args...)
		return
	})
	return
}
Example #11
0
func (s *Service) state() (state State, err error) {
	_, err = task.DoRun(func(result *task.Result) error {
		result.Module = "service"
		result.Op = "state"
		result.Extra["name"] = s.Name
		err = result.ExecCommand("service", s.Name, "status")
		result.Changed = false
		if result.Rc == 3 {
			state = Stopped
			result.Err = nil
			err = nil
			result.Failed = false
		} else if result.Rc == 0 {
			state = Started
		}
		return err
	})
	return
}
Example #12
0
func (y *Yum) isInstalled() (installed bool, err error) {
	_, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "yum"
		result.Op = "check_installed"
		result.Extra["name"] = y.Name

		err = result.ExecCommand("rpm", "-q", y.Name)
		if result.Rc == 0 {
			installed = true
		} else if result.Rc == 1 {
			installed = false
			result.Err = nil
			err = nil
			result.Failed = false
		}
		return
	})
	return
}
Example #13
0
func (c *Chown) getOwners() (owners []string, err error) {
	_, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "chown"
		result.Op = "check_owners"
		result.Extra["path"] = c.Path
		result.Extra["recursive"] = c.Recursive

		var args []string
		if c.Recursive {
			args = []string{c.Path, "-printf", "%u:%g\\n", "-quit"}
		} else {
			args = []string{c.Path, "-printf", "%u:%g\\n"}
		}
		err = result.ExecCommand("find", args...)
		result.Changed = false

		lines := strings.Split(strings.TrimRight(result.Stdout, "\n"), "\n")
		owners = stringutil.Uniq(lines)
		return
	})
	return
}
Example #14
0
func (u *User) Run() (result *task.Result, err error) {
	result, err = task.DoRun(func(result *task.Result) (err error) {
		result.Module = "user"
		result.Extra["state"] = string(u.State)
		result.Extra["name"] = u.Name
		if u.State == Absent {
			result.Op = "remove"
			result, err = u.ensureAbsent(result)
		} else {
			result.Extra["uid"] = u.Uid
			result.Extra["system"] = u.System
			result.Extra["group"] = u.Group
			result.Extra["groups"] = u.Groups
			result.Extra["u.Appends"] = u.Appends
			result.Extra["comment"] = u.Comment
			result.Extra["home_dir"] = u.HomeDir
			result.Extra["shell"] = u.Shell
			result, err = u.ensurePresent(result)
		}
		return
	})
	return
}