From 10b8c93da495549ee41397ea68cb6cf6b14daba4 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 3 Mar 2016 19:12:44 -0500 Subject: [PATCH] Make resource "kind" determination more obvious By adding the "kind" to the base resource, it is still identifiable even when the resource specific elements are gone in calls that are only defined in the base resource. This is also more logical for building resources! This also switches resources to use an Init() method. This will be useful for when resources have more complex initialization to do. --- config.go | 43 +++++++++++++++++++------------------------ exec.go | 14 +++++++------- file.go | 13 +++++++------ pkg.go | 10 +++++++++- resources.go | 21 ++++++++++++--------- svc.go | 13 +++++++------ 6 files changed, 61 insertions(+), 53 deletions(-) diff --git a/config.go b/config.go index 4ae2a9b6..086b133f 100644 --- a/config.go +++ b/config.go @@ -45,11 +45,11 @@ type edgeConfig struct { type GraphConfig struct { Graph string `yaml:"graph"` Resources struct { - Noop []NoopRes `yaml:"noop"` - Pkg []PkgRes `yaml:"pkg"` - File []FileRes `yaml:"file"` - Svc []SvcRes `yaml:"svc"` - Exec []ExecRes `yaml:"exec"` + Noop []*NoopRes `yaml:"noop"` + Pkg []*PkgRes `yaml:"pkg"` + File []*FileRes `yaml:"file"` + Svc []*SvcRes `yaml:"svc"` + Exec []*ExecRes `yaml:"exec"` } `yaml:"resources"` Collector []collectorResConfig `yaml:"collect"` Edges []edgeConfig `yaml:"edges"` @@ -114,11 +114,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO var keep []*Vertex // list of vertex which are the same in new graph - for _, t := range config.Resources.Noop { - obj := NewNoopRes(t.Name) - obj.Meta = t.Meta + for _, obj := range config.Resources.Noop { v := g.GetVertexMatch(obj) if v == nil { // no match found + obj.Init() v = NewVertex(obj) g.AddVertex(v) // call standalone in case not part of an edge } @@ -126,11 +125,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO keep = append(keep, v) // append } - for _, t := range config.Resources.Pkg { - obj := NewPkgRes(t.Name, t.State, false, false, false) - obj.Meta = t.Meta + for _, obj := range config.Resources.Pkg { v := g.GetVertexMatch(obj) if v == nil { // no match found + obj.Init() v = NewVertex(obj) g.AddVertex(v) // call standalone in case not part of an edge } @@ -138,24 +136,23 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO keep = append(keep, v) // append } - for _, t := range config.Resources.File { + for _, obj := range config.Resources.File { // XXX: should we export based on a @@ prefix, or a metaparam // like exported => true || exported => (host pattern)||(other pattern?) - if strings.HasPrefix(t.Name, "@@") { // exported resource + if strings.HasPrefix(obj.Name, "@@") { // exported resource // add to etcd storage... - t.Name = t.Name[2:] //slice off @@ - if !etcdO.EtcdPut(hostname, t.Name, "file", t) { - log.Printf("Problem exporting file resource %v.", t.Name) + obj.Name = obj.Name[2:] //slice off @@ + if !etcdO.EtcdPut(hostname, obj.Name, "file", obj) { + log.Printf("Problem exporting file resource %v.", obj.Name) continue } } else { - obj := NewFileRes(t.Name, t.Path, t.Dirname, t.Basename, t.Content, t.State) // XXX: we don't have a way of knowing if any of the // metaparams are undefined, and as a result to set the // defaults that we want! I hate the go yaml parser!!! - obj.Meta = t.Meta v := g.GetVertexMatch(obj) if v == nil { // no match found + obj.Init() v = NewVertex(obj) g.AddVertex(v) // call standalone in case not part of an edge } @@ -164,11 +161,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO } } - for _, t := range config.Resources.Svc { - obj := NewSvcRes(t.Name, t.State, t.Startup) - obj.Meta = t.Meta + for _, obj := range config.Resources.Svc { v := g.GetVertexMatch(obj) if v == nil { // no match found + obj.Init() v = NewVertex(obj) g.AddVertex(v) // call standalone in case not part of an edge } @@ -176,11 +172,10 @@ func UpdateGraphFromConfig(config *GraphConfig, hostname string, g *Graph, etcdO keep = append(keep, v) // append } - for _, t := range config.Resources.Exec { - obj := NewExecRes(t.Name, t.Cmd, t.Shell, t.Timeout, t.WatchCmd, t.WatchShell, t.IfCmd, t.IfShell, t.PollInt, t.State) - obj.Meta = t.Meta + for _, obj := range config.Resources.Exec { v := g.GetVertexMatch(obj) if v == nil { // no match found + obj.Init() v = NewVertex(obj) g.AddVertex(v) // call standalone in case not part of an edge } diff --git a/exec.go b/exec.go index 9a355410..494590a0 100644 --- a/exec.go +++ b/exec.go @@ -40,12 +40,9 @@ type ExecRes struct { } func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcmd, ifshell string, pollint int, state string) *ExecRes { - // FIXME if path = nil, path = name ... - return &ExecRes{ + obj := &ExecRes{ BaseRes: BaseRes{ - Name: name, - events: make(chan Event), - vertex: nil, + Name: name, }, Cmd: cmd, Shell: shell, @@ -57,10 +54,13 @@ func NewExecRes(name, cmd, shell string, timeout int, watchcmd, watchshell, ifcm PollInt: pollint, State: state, } + obj.Init() + return obj } -func (obj *ExecRes) Kind() string { - return "Exec" +func (obj *ExecRes) Init() { + obj.BaseRes.kind = "Exec" + obj.BaseRes.Init() // call base init, b/c we're overriding } // validate if the params passed in are valid data diff --git a/file.go b/file.go index d111e99f..c8c6148a 100644 --- a/file.go +++ b/file.go @@ -43,11 +43,9 @@ type FileRes struct { func NewFileRes(name, path, dirname, basename, content, state string) *FileRes { // FIXME if path = nil, path = name ... - return &FileRes{ + obj := &FileRes{ BaseRes: BaseRes{ - Name: name, - events: make(chan Event), - vertex: nil, + Name: name, }, Path: path, Dirname: dirname, @@ -56,10 +54,13 @@ func NewFileRes(name, path, dirname, basename, content, state string) *FileRes { State: state, sha256sum: "", } + obj.Init() + return obj } -func (obj *FileRes) Kind() string { - return "File" +func (obj *FileRes) Init() { + obj.BaseRes.kind = "File" + obj.BaseRes.Init() // call base init, b/c we're overriding } func (obj *FileRes) GetPath() string { diff --git a/pkg.go b/pkg.go index a1706fa4..06641b78 100644 --- a/pkg.go +++ b/pkg.go @@ -33,8 +33,9 @@ type PkgRes struct { AllowUnsupported bool `yaml:"allowunsupported"` // allow unsupported packages to be found? } +// helper function for creating new pkg resources that calls Init() func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupported bool) *PkgRes { - return &PkgRes{ + obj := &PkgRes{ BaseRes: BaseRes{ Name: name, events: make(chan Event), @@ -45,6 +46,13 @@ func NewPkgRes(name, state string, allowuntrusted, allownonfree, allowunsupporte AllowNonFree: allownonfree, AllowUnsupported: allowunsupported, } + obj.Init() + return obj +} + +func (obj *PkgRes) Init() { + obj.BaseRes.kind = "Pkg" + obj.BaseRes.Init() // call base init, b/c we're overriding } func (obj *PkgRes) Kind() string { diff --git a/resources.go b/resources.go index 77384246..8a265474 100644 --- a/resources.go +++ b/resources.go @@ -96,7 +96,8 @@ type Res interface { type BaseRes struct { Name string `yaml:"name"` Meta MetaParams `yaml:"meta"` // struct of all the metaparams - timestamp int64 // last updated timestamp ? + kind string + timestamp int64 // last updated timestamp ? events chan Event vertex *Vertex state resState @@ -114,14 +115,14 @@ type NoopRes struct { func NewNoopRes(name string) *NoopRes { // FIXME: we could get rid of this New constructor and use raw object creation with a required Init() - return &NoopRes{ + obj := &NoopRes{ BaseRes: BaseRes{ - Name: name, - events: make(chan Event), // unbuffered chan size to avoid stale events - vertex: nil, + Name: name, }, Comment: "", } + obj.Init() + return obj } // wraps the IFF method when used with a list of UUID's @@ -163,7 +164,7 @@ func (obj *BaseUUID) Reversed() bool { // initialize structures like channels if created without New constructor func (obj *BaseRes) Init() { - obj.events = make(chan Event) + obj.events = make(chan Event) // unbuffered chan size to avoid stale events } // this method gets used by all the resources, if we have one of (obj NoopRes) it would get overridden in that case! @@ -171,8 +172,9 @@ func (obj *BaseRes) GetName() string { return obj.Name } +// return the kind of resource this is func (obj *BaseRes) Kind() string { - return "Base" + return obj.kind } func (obj *BaseRes) GetMeta() MetaParams { @@ -410,8 +412,9 @@ func Process(obj Res) { } } -func (obj *NoopRes) Kind() string { - return "Noop" +func (obj *NoopRes) Init() { + obj.BaseRes.kind = "Noop" + obj.BaseRes.Init() // call base init, b/c we're overriding } // validate if the params passed in are valid data diff --git a/svc.go b/svc.go index adb3ea7b..19bd2965 100644 --- a/svc.go +++ b/svc.go @@ -35,19 +35,20 @@ type SvcRes struct { } func NewSvcRes(name, state, startup string) *SvcRes { - return &SvcRes{ + obj := &SvcRes{ BaseRes: BaseRes{ - Name: name, - events: make(chan Event), - vertex: nil, + Name: name, }, State: state, Startup: startup, } + obj.Init() + return obj } -func (obj *SvcRes) Kind() string { - return "Svc" +func (obj *SvcRes) Init() { + obj.BaseRes.kind = "Svc" + obj.BaseRes.Init() // call base init, b/c we're overriding } func (obj *SvcRes) Validate() bool {