Compare commits
4 Commits
5bbe08ded5
...
09c1bd44c9
Author | SHA1 | Date | |
---|---|---|---|
|
09c1bd44c9 | ||
|
2f56639f5b | ||
|
84155e8590 | ||
|
2f095a8928 |
24
anvildb.go
24
anvildb.go
@ -1,24 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
|
|
||||||
"github.com/uptrace/bun"
|
|
||||||
"github.com/uptrace/bun/dialect/pgdialect"
|
|
||||||
"github.com/uptrace/bun/driver/pgdriver"
|
|
||||||
"github.com/uptrace/bun/extra/bundebug"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ConnectAnvilDB(dsn string) *bun.DB {
|
|
||||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
|
||||||
db := bun.NewDB(sqldb, pgdialect.New())
|
|
||||||
|
|
||||||
db.AddQueryHook(bundebug.NewQueryHook(
|
|
||||||
bundebug.WithVerbose(true),
|
|
||||||
bundebug.FromEnv("BUNDEBUG"),
|
|
||||||
))
|
|
||||||
return db
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//func GetFeeds(db *bun.DB)
|
|
14
functions.go
Normal file
14
functions.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||||||
|
"github.com/uptrace/bun/driver/pgdriver"
|
||||||
|
)
|
||||||
|
|
||||||
|
func OpenDB(dsn string) *sql.DB {
|
||||||
|
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
||||||
|
return bun.NewDB(sqldb, pgdialect.New())
|
||||||
|
}
|
4
go.mod
4
go.mod
@ -3,10 +3,13 @@ module anvil-client
|
|||||||
go 1.21.10
|
go 1.21.10
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
github.com/uptrace/bun v1.2.1
|
github.com/uptrace/bun v1.2.1
|
||||||
github.com/uptrace/bun/dialect/pgdialect v1.2.1
|
github.com/uptrace/bun/dialect/pgdialect v1.2.1
|
||||||
github.com/uptrace/bun/driver/pgdriver v1.2.1
|
github.com/uptrace/bun/driver/pgdriver v1.2.1
|
||||||
github.com/uptrace/bun/extra/bundebug v1.2.1
|
github.com/uptrace/bun/extra/bundebug v1.2.1
|
||||||
|
golang.org/x/crypto v0.21.0
|
||||||
|
golang.org/x/term v0.18.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@ -17,7 +20,6 @@ require (
|
|||||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||||
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
|
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
|
||||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||||
golang.org/x/crypto v0.21.0 // indirect
|
|
||||||
golang.org/x/sys v0.18.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
mellium.im/sasl v0.3.1 // indirect
|
mellium.im/sasl v0.3.1 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
@ -33,6 +35,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
||||||
|
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo=
|
mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo=
|
||||||
|
93
main.go
93
main.go
@ -2,71 +2,42 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var feeds []Feed
|
// Tofu Provider is the goal
|
||||||
var categories []Category
|
// Data Sources first
|
||||||
var entries []Entry
|
// Resources later
|
||||||
var users []User
|
|
||||||
|
var users []UserModel
|
||||||
|
var ssh_keys []SSHKeyModel
|
||||||
|
|
||||||
|
//var servers []Server
|
||||||
|
//var server_definitions []ServerDefinition
|
||||||
|
//var manifests []Manifest
|
||||||
|
//var jobs []Job
|
||||||
|
//var hosts []Host
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var (
|
||||||
ctx := context.Background()
|
ctx = context.Background()
|
||||||
dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable"
|
dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
|
||||||
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
|
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
|
||||||
|
)
|
||||||
|
// Calling PostgresClient as an instance method as per:
|
||||||
|
// https://stackoverflow.com/questions/38666404/not-enough-arguments-in-call-to-method-expression
|
||||||
|
anvildb := OpenDB(dsn)
|
||||||
|
|
||||||
anvildb := ConnectAnvilDB(dsn)
|
|
||||||
// Turn these into funcs with error handling, logging, return vals etc.
|
// Turn these into funcs with error handling, logging, return vals etc.
|
||||||
feederr := anvildb.NewSelect().
|
// if usererr := anvildb.NewSelect().
|
||||||
Model(&Feed{}).
|
// // Turning *User into an interface &User{}
|
||||||
OrderExpr("title ASC").
|
// Model(&UserModel{}).
|
||||||
Limit(10).
|
// OrderExpr("user_uuid ASC").
|
||||||
Scan(ctx, &feeds)
|
// Limit(10).
|
||||||
|
// Scan(ctx, &users); usererr != nil {
|
||||||
if feederr != nil {
|
// errors.Join(usererr, err)
|
||||||
errors.Join(feederr, err)
|
// }
|
||||||
}
|
// fmt.Printf("##\n## Users\n###\n\n")
|
||||||
categoryerr := anvildb.NewSelect().
|
// for _, val := range users {
|
||||||
Model(&Category{}).
|
// fmt.Printf("%s\n", val.Username)
|
||||||
OrderExpr("title ASC").
|
// }
|
||||||
Scan(ctx, &categories)
|
|
||||||
if categoryerr != nil {
|
|
||||||
errors.Join(categoryerr, err)
|
|
||||||
}
|
|
||||||
entryerr := anvildb.NewSelect().
|
|
||||||
Model(&Entry{}).
|
|
||||||
OrderExpr("published_at ASC").
|
|
||||||
Limit(10).
|
|
||||||
Scan(ctx, &entries)
|
|
||||||
if entryerr != nil {
|
|
||||||
errors.Join(entryerr, err)
|
|
||||||
}
|
|
||||||
usererr := anvildb.NewSelect().
|
|
||||||
Model(&User{}).
|
|
||||||
OrderExpr("id ASC").
|
|
||||||
Scan(ctx, &users)
|
|
||||||
if usererr != nil {
|
|
||||||
errors.Join(usererr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, val := range entries {
|
|
||||||
fmt.Printf("val = %s\n", val)
|
|
||||||
}
|
|
||||||
for _, val := range categories {
|
|
||||||
|
|
||||||
fmt.Printf("val = %s\n", val)
|
|
||||||
}
|
|
||||||
for _, val := range feeds {
|
|
||||||
fmt.Printf("val = %s\n", val)
|
|
||||||
}
|
|
||||||
for _, val := range users {
|
|
||||||
fmt.Printf("val = %s\n", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// fmt.Printf("%+v\n", entries)
|
|
||||||
// fmt.Printf("%+v\n", categories)
|
|
||||||
// fmt.Printf("%+v\n", feeds)
|
|
||||||
// fmt.Printf("%+v\n", users)
|
|
||||||
}
|
}
|
||||||
|
78
models.go
78
models.go
@ -1,37 +1,73 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/uptrace/bun"
|
"github.com/uptrace/bun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type UserModel struct {
|
||||||
bun.BaseModel `bun:"table:users"`
|
bun.BaseModel `bun:"table:users"`
|
||||||
|
|
||||||
ID int64 `bun:"id,pk,autoincrement"`
|
UUID uuid.UUID `bun:"user_uuid,pk"`
|
||||||
Username string `bun:"username"`
|
Username string `bun:"user_name"`
|
||||||
|
IsExperienced bool `bun:"user_is_experienced"`
|
||||||
|
IsTrusted bool `bun:"user_is_trusted"`
|
||||||
|
// https://pkg.go.dev/github.com/google/uuid#UUID
|
||||||
|
// SELECT user_uuid,user_name,user_is_experienced,user_is_trusted FROM \
|
||||||
|
// users ORDER BY user_uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
type Feed struct {
|
type SSHKeyModel struct {
|
||||||
bun.BaseModel `bun:"table:feeds"`
|
bun.BaseModel `bun:"table:ssh_keys"`
|
||||||
|
|
||||||
ID int64 `bun:"id,pk,autoincrement"`
|
UUID uuid.UUID `bun:"ssh_key_uuid,pk"`
|
||||||
CategoryID int64 `bun:"category_id"`
|
Username string `bun:"ssh_key_user_name"`
|
||||||
Title string `bun:"title"`
|
PublicKey []byte `bun:"ssh_key_public_key"`
|
||||||
// SELECT id,category_id,title FROM feeds ORDER BY id;
|
// https://pkg.go.dev/golang.org/x/crypto/ssh#PublicKey
|
||||||
|
// SELECT ssh_key_uuid,ssh_key_user_name FROM ssh_keys ORDER BY ssh_key_user_name;
|
||||||
}
|
}
|
||||||
type Entry struct {
|
|
||||||
bun.BaseModel `bun:"table:entries"`
|
|
||||||
|
|
||||||
ID int64 `bun:"id,pk,autoincrement"`
|
type ServerModel struct {
|
||||||
Title string `bun:"title"`
|
|
||||||
Author string `bun:"author"`
|
|
||||||
// SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10;
|
|
||||||
}
|
}
|
||||||
type Category struct {
|
|
||||||
bun.BaseModel `bun:"table:categories"`
|
|
||||||
|
|
||||||
ID int64 `bun:"id,pk,autoincrement"`
|
|
||||||
Title string `bun:"title"`
|
|
||||||
//SELECT id,title from categories;
|
|
||||||
|
|
||||||
|
type ServerDefinitionModel struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type ManifestModel struct {
|
||||||
|
bun.BaseModel `bun:"table:manifests"`
|
||||||
|
|
||||||
|
UUID uuid.UUID `bun:"mainifest_uuid,pk"`
|
||||||
|
Name string `bun:"manifest_name"`
|
||||||
|
LastRan string `bun:"manifest_last_ran"`
|
||||||
|
XML string `bun:"manifest_xml"`
|
||||||
|
Note string `bun:"manifest_note"`
|
||||||
|
// https://pkg.go.dev/encoding/xml#CharData
|
||||||
|
}
|
||||||
|
|
||||||
|
type JobModel struct {
|
||||||
|
bun.BaseModel `bun:"table:jobs"`
|
||||||
|
|
||||||
|
UUID uuid.UUID `bun:"job_uuid,pk"`
|
||||||
|
HostUUID uuid.UUID `bun:"host_uuid"`
|
||||||
|
Command string `bun:"job_command"`
|
||||||
|
Data string `bun:"job_data"`
|
||||||
|
PickedUpBy string `bun:"job_picked_up_by"`
|
||||||
|
PickedUpAt string `bun:"job_picked_up_at"`
|
||||||
|
Updated string `bun:"job_updated"`
|
||||||
|
Name string `bun:"job_name"`
|
||||||
|
Progress int64 `bun:"job_progress"`
|
||||||
|
Title string `bun:"job_title"`
|
||||||
|
Description string `bun:"job_description"`
|
||||||
|
Status string `bun:"job_status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type HostModel struct {
|
||||||
|
bun.BaseModel `bun:"table:hosts"`
|
||||||
|
|
||||||
|
UUID uuid.UUID `bun:"host_uuid,pk"`
|
||||||
|
HostName string `bun:"host_name"`
|
||||||
|
HostType string `bun:"host_type"`
|
||||||
|
HostKey string `bun:"host_key"`
|
||||||
|
HostIPMI string `bun:"host_ipmi"`
|
||||||
|
HostStatus string `bun:"host_status"`
|
||||||
}
|
}
|
||||||
|
11
ssh.go
Normal file
11
ssh.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AnvilSSHConnection struct {
|
||||||
|
thing ssh.Client
|
||||||
|
otherthing term.Terminal
|
||||||
|
}
|
49
types.go
Normal file
49
types.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Anvil struct {
|
||||||
|
Strikers []Striker
|
||||||
|
Hosts []Host
|
||||||
|
BMCs []BMC
|
||||||
|
Switches []Switch
|
||||||
|
PDUs []PDU
|
||||||
|
UPSes []UPS
|
||||||
|
}
|
||||||
|
|
||||||
|
type WebClient struct {
|
||||||
|
*http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
type SSHClient struct {
|
||||||
|
*ssh.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
type PostgresClient struct {
|
||||||
|
*sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
type Striker struct {
|
||||||
|
FQDN string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Host struct {
|
||||||
|
FQDN string
|
||||||
|
}
|
||||||
|
|
||||||
|
type BMC struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Switch struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type PDU struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type UPS struct {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user