Compare commits

..

No commits in common. "78e52933eb9e09925845e150bd2aa34a4d2e720e" and "05b229d5002fceeb589bcd4504d187ab51f491fb" have entirely different histories.

15 changed files with 81 additions and 274 deletions

View File

@ -24,13 +24,12 @@ type CommandArg string
type Command struct {
Path string `json:"path" yaml:"path"`
Args []CommandArg `json:"args" yaml:"args"`
Split bool `json:"split" yaml:"split`
Executor CommandExecutor `json:"-" yaml:"-"`
Extractor CommandExtractAttributes `json:"-" yaml:"-"`
}
func NewCommand() *Command {
c := &Command{ Split: true }
c := &Command{}
c.Executor = func(value any) ([]byte, error) {
args, err := c.Template(value)
if err != nil {
@ -89,12 +88,7 @@ func (c *Command) Template(value any) ([]string, error) {
return nil, err
}
if commandLineArg.Len() > 0 {
var splitArg []string
if c.Split {
splitArg = strings.Split(commandLineArg.String(), " ")
} else {
splitArg = []string{commandLineArg.String()}
}
splitArg := strings.Split(commandLineArg.String(), " ")
slog.Info("Template()", "split", splitArg, "len", len(splitArg))
args = append(args, splitArg...)
}

View File

@ -31,7 +31,6 @@ type ContainerNetworkClient interface {
}
type ContainerNetwork struct {
stater machine.Stater `json:"-" yaml:"-"`
Id string `json:"ID,omitempty" yaml:"ID,omitempty"`
Name string `json:"name" yaml:"name"`
@ -72,26 +71,7 @@ func (n *ContainerNetwork) Clone() Resource {
}
func (n *ContainerNetwork) StateMachine() machine.Stater {
if n.stater == nil {
n.stater = StorageMachine(n)
}
return n.stater
}
func (n *ContainerNetwork) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := n.Create(ctx); e != nil {
n.stater.Trigger("created")
}
case "present":
n.State = "present"
}
case machine.EXITSTATEEVENT:
}
return StorageMachine()
}
func (n *ContainerNetwork) URI() string {

View File

@ -39,7 +39,6 @@ func NewDeclaration() *Declaration {
func (d *Declaration) Clone() *Declaration {
return &Declaration {
Type: d.Type,
Transition: d.Transition,
Attributes: d.Attributes.Clone(),
}
}
@ -67,14 +66,7 @@ func (d *Declaration) Resource() Resource {
func (d *Declaration) Apply() error {
stater := d.Attributes.StateMachine()
switch d.Transition {
case "absent":
default:
fallthrough
case "create", "present":
return stater.Trigger("create")
}
return nil
return stater.Trigger(d.Transition)
}
func (d *Declaration) SetURI(uri string) error {

View File

@ -111,24 +111,3 @@ func TestDeclarationJson(t *testing.T) {
assert.Equal(t, "10012", userResourceDeclaration.Attributes.(*User).UID)
}
func TestDeclarationTransition(t *testing.T) {
fileName := filepath.Join(TempDir, "testdecl.txt")
fileDeclJson := fmt.Sprintf(`
{
"type": "file",
"transition": "present",
"attributes": {
"path": "%s"
}
}
`, fileName)
resourceDeclaration := NewDeclaration()
e := json.Unmarshal([]byte(fileDeclJson), resourceDeclaration)
assert.Nil(t, e)
assert.Equal(t, TypeName("file"), resourceDeclaration.Type)
assert.Equal(t, fileName, resourceDeclaration.Attributes.(*File).Path)
resourceDeclaration.Apply()
assert.FileExists(t, fileName)
}

View File

@ -122,7 +122,6 @@ func (d *Document) YAML() ([]byte, error) {
}
func (d *Document) Diff(with *Document, output io.Writer) (string, error) {
slog.Info("Document.Diff()")
opts := []yamldiff.DoOptionFunc{}
if output == nil {
output = &strings.Builder{}
@ -131,6 +130,7 @@ func (d *Document) Diff(with *Document, output io.Writer) (string, error) {
if yerr != nil {
return "", yerr
}
yamlDiff,yamlDiffErr := yamldiff.Load(string(ydata))
if yamlDiffErr != nil {
return "", yamlDiffErr
@ -145,9 +145,9 @@ func (d *Document) Diff(with *Document, output io.Writer) (string, error) {
return "", withDiffErr
}
for _,docDiffResults := range yamldiff.Do(yamlDiff, withDiff, opts...) {
slog.Info("Diff()", "diff", docDiffResults, "dump", docDiffResults.Dump())
_,e := output.Write([]byte(docDiffResults.Dump()))
for _,diff := range yamldiff.Do(yamlDiff, withDiff, opts...) {
slog.Info("Diff()", "diff", diff)
_,e := output.Write([]byte(diff.Dump()))
if e != nil {
return "", e
}

View File

@ -49,7 +49,6 @@ func init() {
// Manage the state of file system objects
type File struct {
stater machine.Stater `json:"-" yaml:"-"`
normalizePath bool `json:"-" yaml:"-"`
Path string `json:"path" yaml:"path"`
Owner string `json:"owner" yaml:"owner"`
@ -106,26 +105,7 @@ func (f *File) Clone() Resource {
}
func (f *File) StateMachine() machine.Stater {
if f.stater == nil {
f.stater = StorageMachine(f)
}
return f.stater
}
func (f *File) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := f.Create(ctx); e != nil {
f.stater.Trigger("created")
}
case "present":
f.State = "present"
}
case machine.EXITSTATEEVENT:
}
return StorageMachine()
}
func (f *File) URI() string {
@ -159,7 +139,63 @@ func (f *File) Apply() error {
return removeErr
}
case "present":
return f.Create(context.Background())
{
uid, uidErr := LookupUID(f.Owner)
if uidErr != nil {
return fmt.Errorf("%w: unkwnon user %d", ErrInvalidFileOwner, uid)
}
gid, gidErr := LookupGID(f.Group)
if gidErr != nil {
return gidErr
}
slog.Info("File.Mode", "mode", f.Mode)
mode, modeErr := strconv.ParseInt(f.Mode, 8, 64)
if modeErr != nil {
return fmt.Errorf("%w: invalid mode %d", ErrInvalidFileMode, mode)
}
//e := os.Stat(f.path)
//if os.IsNotExist(e) {
switch f.FileType {
case SymbolicLinkFile:
linkErr := os.Symlink(f.Target, f.Path)
if linkErr != nil {
return linkErr
}
case DirectoryFile:
if mkdirErr := os.MkdirAll(f.Path, os.FileMode(mode)); mkdirErr != nil {
return mkdirErr
}
default:
fallthrough
case RegularFile:
createdFile, e := os.Create(f.Path)
if e != nil {
return e
}
defer createdFile.Close()
if chmodErr := createdFile.Chmod(os.FileMode(mode)); chmodErr != nil {
return chmodErr
}
_, writeErr := createdFile.Write([]byte(f.Content))
if writeErr != nil {
return writeErr
}
if !f.Mtime.IsZero() && !f.Atime.IsZero() {
if chtimesErr := os.Chtimes(f.Path, f.Atime, f.Mtime); chtimesErr != nil {
return chtimesErr
}
}
}
if chownErr := os.Chown(f.Path, uid, gid); chownErr != nil {
return chownErr
}
}
}
return nil
@ -227,58 +263,6 @@ func (f *ResourceFileInfo) Sys() any {
return nil
}
func (f *File) Create(ctx context.Context) error {
uid, uidErr := LookupUID(f.Owner)
if uidErr != nil {
return fmt.Errorf("%w: unkwnon user %d", ErrInvalidFileOwner, uid)
}
gid, gidErr := LookupGID(f.Group)
if gidErr != nil {
return gidErr
}
mode, modeErr := strconv.ParseInt(f.Mode, 8, 64)
if modeErr != nil {
return fmt.Errorf("%w: invalid mode %d", ErrInvalidFileMode, mode)
}
//e := os.Stat(f.path)
//if os.IsNotExist(e) {
switch f.FileType {
case SymbolicLinkFile:
linkErr := os.Symlink(f.Target, f.Path)
if linkErr != nil {
return linkErr
}
case DirectoryFile:
if mkdirErr := os.MkdirAll(f.Path, os.FileMode(mode)); mkdirErr != nil {
return mkdirErr
}
default:
fallthrough
case RegularFile:
createdFile, e := os.Create(f.Path)
if e != nil {
return e
}
defer createdFile.Close()
if chmodErr := createdFile.Chmod(os.FileMode(mode)); chmodErr != nil {
return chmodErr
}
_, writeErr := createdFile.Write([]byte(f.Content))
if writeErr != nil {
return writeErr
}
if !f.Mtime.IsZero() && !f.Atime.IsZero() {
if chtimesErr := os.Chtimes(f.Path, f.Atime, f.Mtime); chtimesErr != nil {
return chtimesErr
}
}
}
if chownErr := os.Chown(f.Path, uid, gid); chownErr != nil {
return chownErr
}
return nil
}
func (f *File) UpdateContentAttributes() {
f.Size = int64(len(f.Content))
f.Sha256 = fmt.Sprintf("%x", sha256.Sum256([]byte(f.Content)))

View File

@ -35,7 +35,6 @@ type HTTPHeader struct {
// Manage the state of an HTTP endpoint
type HTTP struct {
stater machine.Stater `yaml:"-" json:"-"`
client *http.Client `yaml:"-" json:"-"`
Endpoint string `yaml:"endpoint" json:"endpoint"`
Headers []HTTPHeader `yaml:"headers,omitempty" json:"headers,omitempty"`
@ -58,26 +57,7 @@ func (h *HTTP) Clone() Resource {
}
func (h *HTTP) StateMachine() machine.Stater {
if h.stater == nil {
h.stater = StorageMachine(h)
}
return h.stater
}
func (h *HTTP) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := h.Create(ctx); e != nil {
h.stater.Trigger("created")
}
case "present":
h.State = "present"
}
case machine.EXITSTATEEVENT:
}
return StorageMachine()
}
func (h *HTTP) URI() string {
@ -132,7 +112,7 @@ func (h *HTTP) ResolveId(ctx context.Context) string {
return h.Endpoint
}
func (h *HTTP) Create(ctx context.Context) error {
func (h *HTTP) Create() error {
body := strings.NewReader(h.Body)
req, reqErr := http.NewRequest("POST", h.Endpoint, body)
if reqErr != nil {

View File

@ -67,7 +67,6 @@ endpoint: "%s/resource/user/foo"
}
func TestHTTPCreate(t *testing.T) {
ctx := context.Background()
userdecl := `
type: "user"
attributes:
@ -97,6 +96,6 @@ body: |
`, server.URL, re.ReplaceAllString(userdecl, " $1"))
assert.Nil(t, h.LoadDecl(decl))
assert.Greater(t, len(h.Body), 0)
e := h.Create(ctx)
e := h.Create()
assert.Nil(t, e)
}

View File

@ -103,7 +103,6 @@ const (
// Manage the state of iptables rules
// iptable://filter/INPUT/0
type Iptable struct {
stater machine.Stater `json:"-" yaml:"-"`
Id uint `json:"id,omitempty" yaml:"id,omitempty"`
Table IptableName `json:"table" yaml:"table"`
Chain IptableChain `json:"chain" yaml:"chain"`
@ -152,26 +151,7 @@ func (i *Iptable) Clone() Resource {
}
func (i *Iptable) StateMachine() machine.Stater {
if i.stater == nil {
i.stater = StorageMachine(i)
}
return i.stater
}
func (i *Iptable) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := i.Create(ctx); e != nil {
i.stater.Trigger("created")
}
case "present":
i.State = "present"
}
case machine.EXITSTATEEVENT:
}
return StorageMachine()
}
func (i *Iptable) URI() string {

View File

@ -108,7 +108,6 @@ const (
// Manage the state of network routes
type NetworkRoute struct {
stater machine.Stater `json:"-" yaml:"-"`
Id string
To string `json:"to" yaml:"to"`
Interface string `json:"interface" yaml:"interface"`
@ -141,30 +140,7 @@ func (n *NetworkRoute) Clone() Resource {
}
func (n *NetworkRoute) StateMachine() machine.Stater {
if n.stater == nil {
n.stater = StorageMachine(n)
}
return n.stater
}
func (n *NetworkRoute) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := n.Create(ctx); e != nil {
n.stater.Trigger("created")
}
case "present":
n.State = "present"
}
case machine.EXITSTATEEVENT:
}
}
func (n *NetworkRoute) Create(ctx context.Context) error {
return nil
return StorageMachine()
}
func (n *NetworkRoute) URI() string {

View File

@ -31,10 +31,9 @@ const (
)
type Package struct {
stater machine.Stater `yaml:"-" json:"-"`
Name string `yaml:"name" json:"name"`
Required string `json:"required,omitempty" yaml:"required,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
Required string `json:"required" yaml:"required"`
Version string `yaml:"version" json:"version"`
PackageType PackageType `yaml:"type" json:"type"`
CreateCommand *Command `yaml:"-" json:"-"`
@ -42,7 +41,7 @@ type Package struct {
UpdateCommand *Command `yaml:"-" json:"-"`
DeleteCommand *Command `yaml:"-" json:"-"`
// state attributes
State string `yaml:"state,omitempty" json:"state,omitempty"`
State string `yaml:"state" json:"state"`
}
func init() {
@ -81,7 +80,7 @@ func init() {
}
func NewPackage() *Package {
return &Package{ PackageType: PackageTypeApk }
return &Package{}
}
func (p *Package) Clone() Resource {
@ -97,26 +96,7 @@ func (p *Package) Clone() Resource {
}
func (p *Package) StateMachine() machine.Stater {
if p.stater == nil {
p.stater = StorageMachine(p)
}
return p.stater
}
func (p *Package) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := p.Create(ctx); e != nil {
p.stater.Trigger("created")
}
case "present":
p.State = "present"
}
case machine.EXITSTATEEVENT:
}
return StorageMachine()
}
func (p *Package) URI() string {
@ -160,18 +140,6 @@ func (p *Package) ResolveId(ctx context.Context) string {
return ""
}
func (p *Package) Create(ctx context.Context) error {
if p.Version == "latest" {
p.Version = ""
}
_, err := p.CreateCommand.Execute(p)
if err != nil {
return err
}
_,e := p.Read(ctx)
return e
}
func (p *Package) Apply() error {
if p.Version == "latest" {
p.Version = ""
@ -324,11 +292,10 @@ func NewApkDeleteCommand() *Command {
func NewAptCreateCommand() *Command {
c := NewCommand()
c.Path = "apt-get"
c.Split = false
c.Args = []CommandArg{
CommandArg("satisfy"),
CommandArg("-y"),
CommandArg("{{ .Name }} ({{ if .Required }}{{ .Required }}{{ else }}>=0.0.0{{ end }})"),
CommandArg("{{ .Name }} ({{ .Required }})"),
}
return c
}

View File

@ -58,21 +58,17 @@ func NewResource(uri string) Resource {
return nil
}
func StorageMachine(sub machine.Subscriber) machine.Stater {
func StorageMachine() machine.Stater {
// start_destroy -> absent -> start_create -> present -> start_destroy
stater := machine.New("absent")
stater.AddStates("absent", "start_create", "present", "start_delete", "start_read", "start_update")
stater.AddTransition("create", "absent", "start_create")
stater.AddSubscription("create", sub)
stater.AddTransition("created", "start_create", "present")
stater.AddTransition("read", "*", "start_read")
stater.AddSubscription("read", sub)
stater.AddTransition("state_read", "start_read", "present")
stater.AddTransition("update", "*", "start_update")
stater.AddSubscription("update", sub)
stater.AddTransition("updated", "start_update", "present")
stater.AddTransition("delete", "*", "start_delete")
stater.AddSubscription("delete", sub)
stater.AddTransition("deleted", "start_delete", "absent")
return stater
}

View File

@ -7,12 +7,12 @@
"properties": {
"name": {
"type": "string",
"pattern": "^[a-zA-Z0-9][-a-zA-Z0-9+._]+$"
"pattern": "^[a-zA-Z0-9][a-zA-Z0-9+.-_]+$"
},
"required": {
"description": "version requirement",
"type": "string",
"pattern": "^([><~=]{0,2}[-_a-zA-Z0-9+.]+|)$"
"pattern": "^([><~=]{0,1}[-_a-zA-Z0-9+.]+|)$"
},
"version": {
"type": "string"

View File

@ -28,7 +28,6 @@ const (
)
type User struct {
stater machine.Stater `json:"-" yaml:"-"`
Name string `json:"name" yaml:"name"`
UID string `json:"uid,omitempty" yaml:"uid,omitempty"`
Group string `json:"group,omitempty" yaml:"group,omitempty"`
@ -84,26 +83,7 @@ func (u *User) Clone() Resource {
}
func (u *User) StateMachine() machine.Stater {
if u.stater == nil {
u.stater = StorageMachine(u)
}
return u.stater
}
func (u *User) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := u.Create(ctx); e != nil {
u.stater.Trigger("created")
}
case "present":
u.State = "present"
}
case machine.EXITSTATEEVENT:
}
return StorageMachine()
}
func (u *User) SetURI(uri string) error {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 MiB

After

Width:  |  Height:  |  Size: 105 KiB