Rename GetRes() which is not descriptive, to Kind()

This used to be GetType(), but since now things are "resources", we want
to know what "kind" they are, since asking what "type" they are is
confusing, and makes less logical sense than "Kind".
This commit is contained in:
James Shubin
2016-02-23 00:24:43 -05:00
parent a56fb3c8cd
commit 06635dfa75
6 changed files with 38 additions and 38 deletions

20
exec.go
View File

@@ -59,7 +59,7 @@ func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcm
}
}
func (obj *ExecRes) GetRes() string {
func (obj *ExecRes) Kind() string {
return "Exec"
}
@@ -129,7 +129,7 @@ func (obj *ExecRes) Watch() {
cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Printf("%v[%v]: Error creating StdoutPipe for Cmd: %v", obj.GetRes(), obj.GetName(), err)
log.Printf("%v[%v]: Error creating StdoutPipe for Cmd: %v", obj.Kind(), obj.GetName(), err)
log.Fatal(err) // XXX: how should we handle errors?
}
scanner := bufio.NewScanner(cmdReader)
@@ -141,7 +141,7 @@ func (obj *ExecRes) Watch() {
cmd.Process.Kill() // TODO: is this necessary?
}()
if err := cmd.Start(); err != nil {
log.Printf("%v[%v]: Error starting Cmd: %v", obj.GetRes(), obj.GetName(), err)
log.Printf("%v[%v]: Error starting Cmd: %v", obj.Kind(), obj.GetName(), err)
log.Fatal(err) // XXX: how should we handle errors?
}
@@ -154,7 +154,7 @@ func (obj *ExecRes) Watch() {
case text := <-bufioch:
obj.SetConvergedState(resConvergedNil)
// each time we get a line of output, we loop!
log.Printf("%v[%v]: Watch output: %s", obj.GetRes(), obj.GetName(), text)
log.Printf("%v[%v]: Watch output: %s", obj.Kind(), obj.GetName(), text)
if text != "" {
send = true
}
@@ -164,10 +164,10 @@ func (obj *ExecRes) Watch() {
if err == nil { // EOF
// FIXME: add an "if watch command ends/crashes"
// restart or generate error option
log.Printf("%v[%v]: Reached EOF", obj.GetRes(), obj.GetName())
log.Printf("%v[%v]: Reached EOF", obj.Kind(), obj.GetName())
return
}
log.Printf("%v[%v]: Error reading input?: %v", obj.GetRes(), obj.GetName(), err)
log.Printf("%v[%v]: Error reading input?: %v", obj.Kind(), obj.GetName(), err)
log.Fatal(err)
// XXX: how should we handle errors?
@@ -195,7 +195,7 @@ func (obj *ExecRes) Watch() {
// TODO: expand the IfCmd to be a list of commands
func (obj *ExecRes) CheckApply(apply bool) (stateok bool, err error) {
log.Printf("%v[%v]: CheckApply(%t)", obj.GetRes(), obj.GetName(), apply)
log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply)
// if there is a watch command, but no if command, run based on state
if obj.WatchCmd != "" && obj.IfCmd == "" {
@@ -269,7 +269,7 @@ func (obj *ExecRes) CheckApply(apply bool) (stateok bool, err error) {
cmd.Stdout = &out
if err = cmd.Start(); err != nil {
log.Printf("%v[%v]: Error starting Cmd: %v", obj.GetRes(), obj.GetName(), err)
log.Printf("%v[%v]: Error starting Cmd: %v", obj.Kind(), obj.GetName(), err)
return false, err
}
@@ -283,12 +283,12 @@ func (obj *ExecRes) CheckApply(apply bool) (stateok bool, err error) {
select {
case err = <-done:
if err != nil {
log.Printf("%v[%v]: Error waiting for Cmd: %v", obj.GetRes(), obj.GetName(), err)
log.Printf("%v[%v]: Error waiting for Cmd: %v", obj.Kind(), obj.GetName(), err)
return false, err
}
case <-TimeAfterOrBlock(timeout):
log.Printf("%v[%v]: Timeout waiting for Cmd", obj.GetRes(), obj.GetName())
log.Printf("%v[%v]: Timeout waiting for Cmd", obj.Kind(), obj.GetName())
//cmd.Process.Kill() // TODO: is this necessary?
return false, errors.New("Timeout waiting for Cmd!")
}

View File

@@ -58,7 +58,7 @@ func NewFileRes(name, path, dirname, basename, content, state string) *FileRes {
}
}
func (obj *FileRes) GetRes() string {
func (obj *FileRes) Kind() string {
return "File"
}
@@ -142,7 +142,7 @@ func (obj *FileRes) Watch() {
} else if err == syscall.ENOSPC {
// XXX: occasionally: no space left on device,
// XXX: probably due to lack of inotify watches
log.Printf("%v[%v]: Out of inotify watches!", obj.GetRes(), obj.GetName())
log.Printf("%v[%v]: Out of inotify watches!", obj.Kind(), obj.GetName())
log.Fatal(err)
} else {
log.Printf("Unknown file[%v] error:", obj.Name)
@@ -318,7 +318,7 @@ func (obj *FileRes) FileApply() error {
}
func (obj *FileRes) CheckApply(apply bool) (stateok bool, err error) {
log.Printf("%v[%v]: CheckApply(%t)", obj.GetRes(), obj.GetName(), apply)
log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply)
if obj.isStateOK { // cache the state
return true, nil

View File

@@ -241,7 +241,7 @@ func (g *Graph) Graphviz() (out string) {
//out += "\tnode [shape=box];\n"
str := ""
for i := range g.Adjacency { // reverse paths
out += fmt.Sprintf("\t%v [label=\"%v[%v]\"];\n", i.GetName(), i.GetRes(), i.GetName())
out += fmt.Sprintf("\t%v [label=\"%v[%v]\"];\n", i.GetName(), i.Kind(), i.GetName())
for j := range g.Adjacency[i] {
k := g.Adjacency[i][j]
// use str for clearer output ordering
@@ -556,7 +556,7 @@ func (g *Graph) Start(wg *sync.WaitGroup, first bool) { // start or continue
go func(vv *Vertex) {
defer wg.Done()
vv.Res.Watch()
log.Printf("%v[%v]: Exited", vv.GetRes(), vv.GetName())
log.Printf("%v[%v]: Exited", vv.Kind(), vv.GetName())
}(v)
}
@@ -577,7 +577,7 @@ func (g *Graph) Start(wg *sync.WaitGroup, first bool) { // start or continue
for !v.Res.SendEvent(eventStart, true, false) {
if DEBUG {
// if SendEvent fails, we aren't up yet
log.Printf("%v[%v]: Retrying SendEvent(Start)", v.GetRes(), v.GetName())
log.Printf("%v[%v]: Retrying SendEvent(Start)", v.Kind(), v.GetName())
// sleep here briefly or otherwise cause
// a different goroutine to be scheduled
time.Sleep(1 * time.Millisecond)

10
pkg.go
View File

@@ -47,7 +47,7 @@ func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupporte
}
}
func (obj *PkgRes) GetRes() string {
func (obj *PkgRes) Kind() string {
return "Pkg"
}
@@ -136,10 +136,10 @@ func (obj *PkgRes) Watch() {
}
func (obj *PkgRes) CheckApply(apply bool) (stateok bool, err error) {
log.Printf("%v[%v]: CheckApply(%t)", obj.GetRes(), obj.GetName(), apply)
log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply)
if obj.State == "" { // TODO: Validate() should replace this check!
log.Fatalf("%v[%v]: Package state is undefined!", obj.GetRes(), obj.GetName())
log.Fatalf("%v[%v]: Package state is undefined!", obj.Kind(), obj.GetName())
}
if obj.isStateOK { // cache the state
@@ -254,7 +254,7 @@ func (obj *PkgRes) CheckApply(apply bool) (stateok bool, err error) {
transactionFlags += PK_TRANSACTION_FLAG_ENUM_ONLY_TRUSTED
}
// apply correct state!
log.Printf("%v[%v]: Set: %v...", obj.GetRes(), obj.GetName(), obj.State)
log.Printf("%v[%v]: Set: %v...", obj.Kind(), obj.GetName(), obj.State)
switch obj.State {
case "uninstalled": // run remove
// NOTE: packageId is different than when installed, because now
@@ -272,7 +272,7 @@ func (obj *PkgRes) CheckApply(apply bool) (stateok bool, err error) {
if err != nil {
return false, err // fail
}
log.Printf("%v[%v]: Set: %v success!", obj.GetRes(), obj.GetName(), obj.State)
log.Printf("%v[%v]: Set: %v success!", obj.Kind(), obj.GetName(), obj.State)
return false, nil // success
}

View File

@@ -45,7 +45,7 @@ const (
type Res interface {
Init()
GetName() string // can't be named "Name()" because of struct field
GetRes() string
Kind() string
Watch()
CheckApply(bool) (bool, error)
SetVertex(*Vertex)
@@ -105,7 +105,7 @@ func (obj *BaseRes) GetName() string {
return obj.Name
}
func (obj *BaseRes) GetRes() string {
func (obj *BaseRes) Kind() string {
return "Base"
}
@@ -146,7 +146,7 @@ func (obj *BaseRes) GetState() resState {
func (obj *BaseRes) SetState(state resState) {
if DEBUG {
log.Printf("%v[%v]: State: %v -> %v", obj.GetRes(), obj.GetName(), obj.GetState(), state)
log.Printf("%v[%v]: State: %v -> %v", obj.Kind(), obj.GetName(), obj.GetState(), state)
}
obj.state = state
}
@@ -174,7 +174,7 @@ func (obj *BaseRes) OKTimestamp() bool {
// b/c we should let our pre-req's go first...
x, y := obj.GetTimestamp(), n.Res.GetTimestamp()
if DEBUG {
log.Printf("%v[%v]: OKTimestamp: (%v) >= %v[%v](%v): !%v", obj.GetRes(), obj.GetName(), x, n.GetRes(), n.GetName(), y, x >= y)
log.Printf("%v[%v]: OKTimestamp: (%v) >= %v[%v](%v): !%v", obj.Kind(), obj.GetName(), x, n.Kind(), n.GetName(), y, x >= y)
}
if x >= y {
return false
@@ -195,12 +195,12 @@ func (obj *BaseRes) Poke(activity bool) {
// XXX: if n.Res.GetState() != resStateEvent { // is this correct?
if true { // XXX
if DEBUG {
log.Printf("%v[%v]: Poke: %v[%v]", v.GetRes(), v.GetName(), n.GetRes(), n.GetName())
log.Printf("%v[%v]: Poke: %v[%v]", v.Kind(), v.GetName(), n.Kind(), n.GetName())
}
n.SendEvent(eventPoke, false, activity) // XXX: can this be switched to sync?
} else {
if DEBUG {
log.Printf("%v[%v]: Poke: %v[%v]: Skipped!", v.GetRes(), v.GetName(), n.GetRes(), n.GetName())
log.Printf("%v[%v]: Poke: %v[%v]: Skipped!", v.Kind(), v.GetName(), n.Kind(), n.GetName())
}
}
}
@@ -221,12 +221,12 @@ func (obj *BaseRes) BackPoke() {
// happens earlier in the state cycle and that doesn't wrap nil
if x >= y && (s != resStateEvent && s != resStateCheckApply) {
if DEBUG {
log.Printf("%v[%v]: BackPoke: %v[%v]", v.GetRes(), v.GetName(), n.GetRes(), n.GetName())
log.Printf("%v[%v]: BackPoke: %v[%v]", v.Kind(), v.GetName(), n.Kind(), n.GetName())
}
n.SendEvent(eventBackPoke, false, false) // XXX: can this be switched to sync?
} else {
if DEBUG {
log.Printf("%v[%v]: BackPoke: %v[%v]: Skipped!", v.GetRes(), v.GetName(), n.GetRes(), n.GetName())
log.Printf("%v[%v]: BackPoke: %v[%v]: Skipped!", v.Kind(), v.GetName(), n.Kind(), n.GetName())
}
}
}
@@ -282,7 +282,7 @@ func (obj *BaseRes) ReadEvent(event *Event) (exit, poke bool) {
return false, false // don't poke on unpause!
} else {
// if we get a poke event here, it's a bug!
log.Fatalf("%v[%v]: Unknown event: %v, while paused!", obj.GetRes(), obj.GetName(), e)
log.Fatalf("%v[%v]: Unknown event: %v, while paused!", obj.Kind(), obj.GetName(), e)
}
}
@@ -295,7 +295,7 @@ func (obj *BaseRes) ReadEvent(event *Event) (exit, poke bool) {
// XXX: rename this function
func Process(obj Res) {
if DEBUG {
log.Printf("%v[%v]: Process()", obj.GetRes(), obj.GetName())
log.Printf("%v[%v]: Process()", obj.Kind(), obj.GetName())
}
obj.SetState(resStateEvent)
var ok = true
@@ -305,17 +305,17 @@ func Process(obj Res) {
// us back and we will run if needed then!
if obj.OKTimestamp() {
if DEBUG {
log.Printf("%v[%v]: OKTimestamp(%v)", obj.GetRes(), obj.GetName(), obj.GetTimestamp())
log.Printf("%v[%v]: OKTimestamp(%v)", obj.Kind(), obj.GetName(), obj.GetTimestamp())
}
obj.SetState(resStateCheckApply)
// if this fails, don't UpdateTimestamp()
stateok, err := obj.CheckApply(true)
if stateok && err != nil { // should never return this way
log.Fatalf("%v[%v]: CheckApply(): %t, %+v", obj.GetRes(), obj.GetName(), stateok, err)
log.Fatalf("%v[%v]: CheckApply(): %t, %+v", obj.Kind(), obj.GetName(), stateok, err)
}
if DEBUG {
log.Printf("%v[%v]: CheckApply(): %t, %v", obj.GetRes(), obj.GetName(), stateok, err)
log.Printf("%v[%v]: CheckApply(): %t, %v", obj.Kind(), obj.GetName(), stateok, err)
}
if !stateok { // if state *was* not ok, we had to have apply'ed
@@ -340,7 +340,7 @@ func Process(obj Res) {
}
}
func (obj *NoopRes) GetRes() string {
func (obj *NoopRes) Kind() string {
return "Noop"
}
@@ -388,7 +388,7 @@ func (obj *NoopRes) Watch() {
// CheckApply method for Noop resource. Does nothing, returns happy!
func (obj *NoopRes) CheckApply(apply bool) (stateok bool, err error) {
log.Printf("%v[%v]: CheckApply(%t)", obj.GetRes(), obj.GetName(), apply)
log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply)
return true, nil // state is always okay
}

4
svc.go
View File

@@ -46,7 +46,7 @@ func NewSvcRes(name, state, startup string) *SvcRes {
}
}
func (obj *SvcRes) GetRes() string {
func (obj *SvcRes) Kind() string {
return "Svc"
}
@@ -216,7 +216,7 @@ func (obj *SvcRes) Watch() {
}
func (obj *SvcRes) CheckApply(apply bool) (stateok bool, err error) {
log.Printf("%v[%v]: CheckApply(%t)", obj.GetRes(), obj.GetName(), apply)
log.Printf("%v[%v]: CheckApply(%t)", obj.Kind(), obj.GetName(), apply)
if obj.isStateOK { // cache the state
return true, nil