2024-04-05 17:22:17 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-09 19:30:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2024-04-05 17:22:17 +00:00
|
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"io"
|
2024-04-09 19:30:05 +00:00
|
|
|
"log/slog"
|
|
|
|
"net/url"
|
|
|
|
_ "os"
|
|
|
|
_ "os/exec"
|
2024-04-05 17:22:17 +00:00
|
|
|
"path/filepath"
|
2024-04-09 19:30:05 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PackageType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
PackageTypeApk PackageType = "apk"
|
|
|
|
PackageTypeApt PackageType = "apt"
|
|
|
|
PackageTypeDeb PackageType = "deb"
|
|
|
|
PackageTypeDnf PackageType = "dnf"
|
|
|
|
PackageTypeRpm PackageType = "rpm"
|
|
|
|
PackageTypePip PackageType = "pip"
|
|
|
|
PackageTypeYum PackageType = "yum"
|
2024-04-05 17:22:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Package struct {
|
2024-04-09 19:30:05 +00:00
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
Required string `json:"required" yaml:"required"`
|
|
|
|
Version string `yaml:"version" json:"version"`
|
|
|
|
PackageType PackageType `yaml:"type" json:"type"`
|
2024-04-05 17:22:17 +00:00
|
|
|
|
2024-04-09 19:30:05 +00:00
|
|
|
CreateCommand *Command `yaml:"-" json:"-"`
|
|
|
|
ReadCommand *Command `yaml:"-" json:"-"`
|
|
|
|
UpdateCommand *Command `yaml:"-" json:"-"`
|
|
|
|
DeleteCommand *Command `yaml:"-" json:"-"`
|
2024-04-05 17:22:17 +00:00
|
|
|
// state attributes
|
2024-04-09 19:30:05 +00:00
|
|
|
State string `yaml:"state" json:"state"`
|
2024-04-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ResourceTypes.Register("package", func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
2024-04-09 19:30:05 +00:00
|
|
|
ResourceTypes.Register(string(PackageTypeApk), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
ResourceTypes.Register(string(PackageTypeApt), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
ResourceTypes.Register(string(PackageTypeDeb), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
ResourceTypes.Register(string(PackageTypeDnf), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
ResourceTypes.Register(string(PackageTypeRpm), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
ResourceTypes.Register(string(PackageTypePip), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
|
|
|
ResourceTypes.Register(string(PackageTypeYum), func(u *url.URL) Resource {
|
|
|
|
p := NewPackage()
|
|
|
|
return p
|
|
|
|
})
|
2024-04-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPackage() *Package {
|
|
|
|
return &Package{}
|
|
|
|
}
|
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
func (p *Package) Clone() Resource {
|
|
|
|
newp := &Package {
|
|
|
|
Name: p.Name,
|
|
|
|
Required: p.Required,
|
|
|
|
Version: p.Version,
|
|
|
|
PackageType: p.PackageType,
|
|
|
|
State: p.State,
|
|
|
|
}
|
|
|
|
newp.CreateCommand, newp.ReadCommand, newp.UpdateCommand, newp.DeleteCommand = newp.PackageType.NewCRUD()
|
|
|
|
return newp
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:22:17 +00:00
|
|
|
func (p *Package) URI() string {
|
2024-04-09 19:30:05 +00:00
|
|
|
return fmt.Sprintf("package://%s?version=%s&type=%s", p.Name, p.Version, p.PackageType)
|
2024-04-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) SetURI(uri string) error {
|
|
|
|
resourceUri, e := url.Parse(uri)
|
|
|
|
if e == nil {
|
|
|
|
if resourceUri.Scheme == "package" {
|
2024-04-09 19:30:05 +00:00
|
|
|
p.Name = filepath.Join(resourceUri.Hostname(), resourceUri.Path)
|
2024-04-05 17:22:17 +00:00
|
|
|
p.Version = resourceUri.Query().Get("version")
|
|
|
|
if p.Version == "" {
|
|
|
|
p.Version = "latest"
|
|
|
|
}
|
2024-04-09 19:30:05 +00:00
|
|
|
p.PackageType = PackageType(resourceUri.Query().Get("type"))
|
|
|
|
if p.PackageType == "" {
|
|
|
|
e = fmt.Errorf("%w: %s is not a package known resource ", ErrInvalidResourceURI, uri)
|
|
|
|
}
|
2024-04-05 17:22:17 +00:00
|
|
|
} else {
|
|
|
|
e = fmt.Errorf("%w: %s is not a package resource ", ErrInvalidResourceURI, uri)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:30:05 +00:00
|
|
|
func (p *Package) JSON() ([]byte, error) {
|
|
|
|
return json.Marshal(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) Validate() error {
|
|
|
|
s := NewSchema(p.Type())
|
|
|
|
jsonDoc, jsonErr := p.JSON()
|
|
|
|
if jsonErr == nil {
|
|
|
|
return s.Validate(string(jsonDoc))
|
|
|
|
}
|
|
|
|
return jsonErr
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:22:17 +00:00
|
|
|
func (p *Package) ResolveId(ctx context.Context) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) Apply() error {
|
2024-04-09 19:30:05 +00:00
|
|
|
if p.Version == "latest" {
|
|
|
|
p.Version = ""
|
|
|
|
}
|
|
|
|
_, err := p.CreateCommand.Execute(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-09 20:47:00 +00:00
|
|
|
_,e := p.Read(context.Background())
|
|
|
|
return e
|
2024-04-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) Load(r io.Reader) error {
|
|
|
|
c := NewYAMLDecoder(r)
|
|
|
|
return c.Decode(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) LoadDecl(yamlResourceDeclaration string) error {
|
|
|
|
c := NewYAMLStringDecoder(yamlResourceDeclaration)
|
|
|
|
return c.Decode(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) Type() string { return "package" }
|
|
|
|
|
|
|
|
func (p *Package) Read(ctx context.Context) ([]byte, error) {
|
2024-04-09 19:30:05 +00:00
|
|
|
out, err := p.ReadCommand.Execute(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
exErr := p.ReadCommand.Extractor(out, p)
|
|
|
|
if exErr != nil {
|
|
|
|
return nil, exErr
|
|
|
|
}
|
2024-04-05 17:22:17 +00:00
|
|
|
return yaml.Marshal(p)
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:30:05 +00:00
|
|
|
func (p *Package) UnmarshalJSON(data []byte) error {
|
2024-04-09 20:47:00 +00:00
|
|
|
if unmarshalErr := json.Unmarshal(data, p); unmarshalErr != nil {
|
2024-04-09 19:30:05 +00:00
|
|
|
return unmarshalErr
|
|
|
|
}
|
|
|
|
p.CreateCommand, p.ReadCommand, p.UpdateCommand, p.DeleteCommand = p.PackageType.NewCRUD()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Package) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
type decodePackage Package
|
|
|
|
if unmarshalErr := value.Decode((*decodePackage)(p)); unmarshalErr != nil {
|
|
|
|
return unmarshalErr
|
|
|
|
}
|
|
|
|
p.CreateCommand, p.ReadCommand, p.UpdateCommand, p.DeleteCommand = p.PackageType.NewCRUD()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PackageType) NewCRUD() (create *Command, read *Command, update *Command, del *Command) {
|
|
|
|
switch *p {
|
|
|
|
case PackageTypeApk:
|
|
|
|
return NewApkCreateCommand(), NewApkReadCommand(), NewApkUpdateCommand(), NewApkDeleteCommand()
|
|
|
|
case PackageTypeApt:
|
|
|
|
return NewAptCreateCommand(), NewAptReadCommand(), NewAptUpdateCommand(), NewAptDeleteCommand()
|
|
|
|
case PackageTypeDeb:
|
|
|
|
case PackageTypeDnf:
|
|
|
|
case PackageTypeRpm:
|
|
|
|
case PackageTypePip:
|
|
|
|
case PackageTypeYum:
|
2024-04-21 06:13:17 +00:00
|
|
|
default:
|
2024-04-09 19:30:05 +00:00
|
|
|
}
|
|
|
|
return nil, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PackageType) UnmarshalValue(value string) error {
|
|
|
|
switch value {
|
|
|
|
case string(PackageTypeApk), string(PackageTypeApt), string(PackageTypeDeb), string(PackageTypeDnf), string(PackageTypeRpm), string(PackageTypePip), string(PackageTypeYum):
|
|
|
|
*p = PackageType(value)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid PackageType value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PackageType) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if unmarshalRouteTypeErr := json.Unmarshal(data, &s); unmarshalRouteTypeErr != nil {
|
|
|
|
return unmarshalRouteTypeErr
|
|
|
|
}
|
|
|
|
return p.UnmarshalValue(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PackageType) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
var s string
|
|
|
|
if err := value.Decode(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.UnmarshalValue(s)
|
|
|
|
}
|
2024-04-05 17:22:17 +00:00
|
|
|
|
|
|
|
func NewApkCreateCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apk"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("add"),
|
2024-04-09 19:30:05 +00:00
|
|
|
CommandArg("{{ .Name }}{{ .Required }}"),
|
2024-04-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApkReadCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apk"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("info"),
|
|
|
|
CommandArg("-ev"),
|
|
|
|
CommandArg("{{ .Name }}"),
|
|
|
|
}
|
2024-04-09 19:30:05 +00:00
|
|
|
c.Extractor = func(out []byte, target any) error {
|
|
|
|
p := target.(*Package)
|
|
|
|
pkg := strings.Split(string(out), "-")
|
|
|
|
if pkg[0] == p.Name {
|
|
|
|
p.Name = pkg[0]
|
|
|
|
p.Version = pkg[1]
|
|
|
|
p.State = "present"
|
|
|
|
} else {
|
|
|
|
p.State = "absent"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApkUpdateCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apk"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("del"),
|
|
|
|
CommandArg("{{ .Name }}"),
|
|
|
|
}
|
2024-04-05 17:22:17 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApkDeleteCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apk"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("del"),
|
|
|
|
CommandArg("{{ .Name }}"),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2024-04-09 19:30:05 +00:00
|
|
|
|
|
|
|
func NewAptCreateCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apt-get"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("satisfy"),
|
|
|
|
CommandArg("-y"),
|
|
|
|
CommandArg("{{ .Name }} ({{ .Required }})"),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAptReadCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "dpkg"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("-s"),
|
|
|
|
CommandArg("{{ .Name }}"),
|
|
|
|
}
|
|
|
|
c.Extractor = func(out []byte, target any) error {
|
|
|
|
p := target.(*Package)
|
|
|
|
slog.Info("Extract()", "out", out)
|
|
|
|
pkginfo := strings.Split(string(out), "\n")
|
|
|
|
for _, infofield := range pkginfo {
|
|
|
|
if len(infofield) > 0 && infofield[0] != ' ' {
|
|
|
|
fieldKeyValue := strings.SplitN(infofield, ":", 2)
|
|
|
|
if len(fieldKeyValue) > 1 {
|
|
|
|
key := strings.TrimSpace(fieldKeyValue[0])
|
|
|
|
value := strings.TrimSpace(fieldKeyValue[1])
|
|
|
|
switch key {
|
|
|
|
case "Package":
|
2024-04-19 07:52:10 +00:00
|
|
|
if value != p.Name {
|
2024-04-09 19:30:05 +00:00
|
|
|
p.State = "absent"
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-19 07:52:10 +00:00
|
|
|
case "Status":
|
|
|
|
statusFields := strings.SplitN(value, " ", 3)
|
|
|
|
if len(statusFields) > 1 {
|
|
|
|
if statusFields[2] == "installed" {
|
|
|
|
p.State = "present"
|
|
|
|
} else {
|
|
|
|
p.State = "absent"
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 19:30:05 +00:00
|
|
|
case "Version":
|
|
|
|
p.Version = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-19 07:52:10 +00:00
|
|
|
slog.Info("Extract()", "package", p)
|
2024-04-09 19:30:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAptUpdateCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apt"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("install"),
|
|
|
|
CommandArg("{{ .Name }}"),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAptDeleteCommand() *Command {
|
|
|
|
c := NewCommand()
|
|
|
|
c.Path = "apt"
|
|
|
|
c.Args = []CommandArg{
|
|
|
|
CommandArg("remove"),
|
|
|
|
CommandArg("{{ .Name }}"),
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|