jx/internal/resource/exec.go

131 lines
2.6 KiB
Go
Raw Normal View History

2024-03-25 20:27:30 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
2024-03-25 20:31:06 +00:00
"context"
"fmt"
"gopkg.in/yaml.v3"
_ "log"
"net/url"
_ "os"
_ "os/exec"
2024-03-27 21:14:40 +00:00
"path/filepath"
2024-04-21 06:13:17 +00:00
_ "strings"
"io"
2024-05-06 00:48:54 +00:00
"gitea.rosskeen.house/rosskeen.house/machine"
"decl/internal/codec"
2024-03-25 20:27:30 +00:00
)
type Exec struct {
2024-05-24 05:11:51 +00:00
stater machine.Stater `yaml:"-" json:"-"`
2024-04-05 17:22:17 +00:00
Id string `yaml:"id" json:"id"`
CreateTemplate Command `yaml:"create" json:"create"`
ReadTemplate Command `yaml:"read" json:"read"`
UpdateTemplate Command `yaml:"update" json:"update"`
DeleteTemplate Command `yaml:"delete" json:"delete"`
2024-03-25 20:27:30 +00:00
2024-07-01 07:16:55 +00:00
config ConfigurationValueGetter
2024-03-25 20:31:06 +00:00
// state attributes
State string `yaml:"state"`
2024-03-25 20:27:30 +00:00
}
func init() {
2024-07-01 07:16:55 +00:00
ResourceTypes.Register([]string{"exec"}, func(u *url.URL) Resource {
2024-03-25 20:31:06 +00:00
x := NewExec()
return x
})
2024-03-25 20:27:30 +00:00
}
func NewExec() *Exec {
2024-04-21 06:13:17 +00:00
return &Exec{}
2024-03-25 20:27:30 +00:00
}
2024-04-19 07:52:10 +00:00
func (x *Exec) Clone() Resource {
return &Exec {
Id: x.Id,
CreateTemplate: x.CreateTemplate,
ReadTemplate: x.ReadTemplate,
UpdateTemplate: x.UpdateTemplate,
DeleteTemplate: x.DeleteTemplate,
State: x.State,
}
}
2024-05-06 00:48:54 +00:00
func (x *Exec) StateMachine() machine.Stater {
2024-05-24 05:11:51 +00:00
if x.stater == nil {
x.stater = ProcessMachine(x)
}
return x.stater
2024-05-06 00:48:54 +00:00
}
2024-03-25 20:27:30 +00:00
func (x *Exec) URI() string {
2024-03-25 20:31:06 +00:00
return fmt.Sprintf("exec://%s", x.Id)
2024-03-25 20:27:30 +00:00
}
func (x *Exec) SetURI(uri string) error {
2024-03-27 21:14:40 +00:00
resourceUri, e := url.Parse(uri)
2024-04-05 17:22:17 +00:00
if e == nil {
if resourceUri.Scheme == "exec" {
x.Id = filepath.Join(resourceUri.Hostname(), resourceUri.RequestURI())
} else {
e = fmt.Errorf("%w: %s is not an exec resource ", ErrInvalidResourceURI, uri)
}
2024-03-27 21:14:40 +00:00
}
return e
2024-03-25 20:27:30 +00:00
}
2024-07-01 07:16:55 +00:00
func (x *Exec) UseConfig(config ConfigurationValueGetter) {
x.config = config
}
2024-03-25 20:27:30 +00:00
func (x *Exec) ResolveId(ctx context.Context) string {
2024-03-25 20:31:06 +00:00
return ""
2024-03-25 20:27:30 +00:00
}
2024-04-09 19:30:05 +00:00
func (x *Exec) Validate() error {
return fmt.Errorf("failed")
}
2024-03-25 20:27:30 +00:00
func (x *Exec) Apply() error {
2024-03-25 20:31:06 +00:00
return nil
2024-03-25 20:27:30 +00:00
}
2024-05-24 05:11:51 +00:00
func (x *Exec) Notify(m *machine.EventMessage) {
ctx := context.Background()
switch m.On {
case machine.ENTERSTATEEVENT:
switch m.Dest {
case "start_create":
if e := x.Create(ctx); e == nil {
if triggerErr := x.stater.Trigger("created"); triggerErr == nil {
return
}
}
x.State = "absent"
case "present":
x.State = "present"
}
case machine.EXITSTATEEVENT:
}
}
2024-04-21 06:13:17 +00:00
func (x *Exec) Load(r io.Reader) error {
return codec.NewYAMLDecoder(r).Decode(x)
2024-04-21 06:13:17 +00:00
}
func (x *Exec) LoadDecl(yamlResourceDeclaration string) error {
return codec.NewYAMLStringDecoder(yamlResourceDeclaration).Decode(x)
2024-03-25 20:27:30 +00:00
}
func (x *Exec) Type() string { return "exec" }
2024-05-24 05:11:51 +00:00
func (x *Exec) Create(ctx context.Context) error {
return nil
}
2024-03-25 20:27:30 +00:00
func (x *Exec) Read(ctx context.Context) ([]byte, error) {
2024-03-25 20:31:06 +00:00
return yaml.Marshal(x)
2024-03-25 20:27:30 +00:00
}