mirror of
https://github.com/davidallendj/configurator.git
synced 2025-12-20 11:37:00 -07:00
Added project files
This commit is contained in:
parent
40615a97f8
commit
be40387f4a
10 changed files with 486 additions and 0 deletions
54
internal/client.go
Normal file
54
internal/client.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package configurator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type SmdClient struct {
|
||||
http.Client
|
||||
Host string
|
||||
Port int
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (client *SmdClient) FetchDNS(config *Config) error {
|
||||
// fetch DNS related information from SMD's endpoint:
|
||||
return nil
|
||||
}
|
||||
|
||||
func (client *SmdClient) FetchEthernetInterfaces() ([]EthernetInterface, error) {
|
||||
if client == nil {
|
||||
return nil, fmt.Errorf("client is nil")
|
||||
}
|
||||
// fetch DHCP related information from SMD's endpoint:
|
||||
url := fmt.Sprintf("%s:%d/hsm/v2/Inventory/EthernetInterfaces", client.Host, client.Port)
|
||||
req, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer([]byte{}))
|
||||
|
||||
// include access token in authorzation header if found
|
||||
if client.AccessToken != "" {
|
||||
req.Header.Add("Authorization", "Bearer "+client.AccessToken)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create new HTTP request: %v", err)
|
||||
}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to make request: %v", err)
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read HTTP response: %v", err)
|
||||
}
|
||||
|
||||
// unmarshal JSON and extract
|
||||
eths := []EthernetInterface{} // []map[string]any{}
|
||||
json.Unmarshal(b, ðs)
|
||||
fmt.Printf("ethernet interfaces: %v\n", string(b))
|
||||
|
||||
return eths, nil
|
||||
}
|
||||
82
internal/config.go
Normal file
82
internal/config.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package configurator
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Version string `yaml:"version"`
|
||||
SmdHost string `yaml:"smd-host"`
|
||||
SmdPort int `yaml:"smd-port"`
|
||||
AccessToken string `yaml:"access-token"`
|
||||
TemplatePaths map[string]string `yaml:"templates"`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
return &Config{
|
||||
Version: "",
|
||||
SmdHost: "http://127.0.0.1",
|
||||
SmdPort: 27779,
|
||||
TemplatePaths: map[string]string{
|
||||
"dnsmasq": "templates/dhcp/dnsmasq.conf",
|
||||
"syslog": "templates/syslog/",
|
||||
"ansible": "templates/ansible",
|
||||
"powerman": "templates/powerman",
|
||||
"conman": "templates/conman",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig(path string) *Config {
|
||||
var c *Config = NewConfig()
|
||||
file, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Printf("failed to read config file: %v\n", err)
|
||||
return c
|
||||
}
|
||||
err = yaml.Unmarshal(file, &c)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to unmarshal config: %v\n", err)
|
||||
return c
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (config *Config) SaveConfig(path string) {
|
||||
path = filepath.Clean(path)
|
||||
if path == "" || path == "." {
|
||||
path = "config.yaml"
|
||||
}
|
||||
data, err := yaml.Marshal(config)
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal config: %v\n", err)
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(path, data, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Printf("failed to write default config file: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func SaveDefaultConfig(path string) {
|
||||
path = filepath.Clean(path)
|
||||
if path == "" || path == "." {
|
||||
path = "config.yaml"
|
||||
}
|
||||
var c = NewConfig()
|
||||
data, err := yaml.Marshal(c)
|
||||
if err != nil {
|
||||
log.Printf("failed to marshal config: %v\n", err)
|
||||
return
|
||||
}
|
||||
err = os.WriteFile(path, data, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Printf("failed to write default config file: %v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
56
internal/config.yaml
Normal file
56
internal/config.yaml
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
version: "0.0.1"
|
||||
server:
|
||||
host: "127.0.0.1"
|
||||
port: 3333
|
||||
callback: "/oidc/callback"
|
||||
|
||||
providers:
|
||||
facebook: "http://facebook.com"
|
||||
forgejo: "http://git.towk.local:3000"
|
||||
gitlab: "https://gitlab.newmexicoconsortium.org"
|
||||
github: "https://github.com"
|
||||
|
||||
authentication:
|
||||
clients:
|
||||
- id: "7527e7b4-c96a-4df0-8fc5-00fde18bb65d"
|
||||
secret: "gto_cc5uvpb5lsdczkwnbarvwmbpv5kcjwg7nhbc75zt65yrfh2ldenq"
|
||||
name: "forgejo"
|
||||
issuer: "http://git.towk.local:3000"
|
||||
scope:
|
||||
- "openid"
|
||||
- "profile"
|
||||
- "read"
|
||||
- "email"
|
||||
redirect-uris:
|
||||
- "http://127.0.0.1:3333/oidc/callback"
|
||||
- id: "7c0fab1153674a258a705976fcb9468350df3addd91de4ec622fc9ed24bfbcdd"
|
||||
secret: "a9a8bc55b0cd99236756093adc00ab17855fa507ce106b8038e7f9390ef2ad99"
|
||||
name: "gitlab"
|
||||
issuer: "http://gitlab.newmexicoconsortium.org"
|
||||
scope:
|
||||
- "openid"
|
||||
- "profile"
|
||||
- "email"
|
||||
redirect-uris:
|
||||
- "http://127.0.0.1:3333/oidc/callback"
|
||||
flows:
|
||||
authorization-code:
|
||||
state: ""
|
||||
client-credentials:
|
||||
|
||||
authorization:
|
||||
urls:
|
||||
#identities: http://127.0.0.1:4434/admin/identities
|
||||
trusted-issuers: http://127.0.0.1:4445/admin/trust/grants/jwt-bearer/issuers
|
||||
login: http://127.0.0.1:4433/self-service/login/api
|
||||
clients: http://127.0.0.1:4445/admin/clients
|
||||
authorize: http://127.0.0.1:4444/oauth2/auth
|
||||
register: http://127.0.0.1:4444/oauth2/register
|
||||
token: http://127.0.0.1:4444/oauth2/token
|
||||
|
||||
|
||||
options:
|
||||
decode-id-token: true
|
||||
decode-access-token: true
|
||||
run-once: true
|
||||
open-browser: false
|
||||
22
internal/configurator.go
Normal file
22
internal/configurator.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package configurator
|
||||
|
||||
type IPAddr struct {
|
||||
IpAddress string `json:"IPAddress"`
|
||||
Network string `json:"Network"`
|
||||
}
|
||||
|
||||
type EthernetInterface struct {
|
||||
Id string
|
||||
Description string
|
||||
MacAddress string
|
||||
LastUpdate string
|
||||
ComponentId string
|
||||
Type string
|
||||
IpAddresses []IPAddr
|
||||
}
|
||||
|
||||
type DHCP struct {
|
||||
Hostname string
|
||||
MacAddress string
|
||||
IpAddress []IPAddr
|
||||
}
|
||||
53
internal/generator/generator.go
Normal file
53
internal/generator/generator.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package generator
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"fmt"
|
||||
|
||||
configurator "github.com/OpenCHAMI/configurator/internal"
|
||||
"github.com/nikolalohinski/gonja/v2"
|
||||
"github.com/nikolalohinski/gonja/v2/exec"
|
||||
)
|
||||
|
||||
type Generator struct {
|
||||
}
|
||||
|
||||
func New() *Generator {
|
||||
return &Generator{}
|
||||
}
|
||||
|
||||
func (g *Generator) GenerateDNS(config *configurator.Config) {
|
||||
// generate file using jinja template
|
||||
// TODO: load template file for DNS
|
||||
// TODO: substitute DNS data fetched from SMD
|
||||
// TODO: print generated config file to STDOUT
|
||||
}
|
||||
|
||||
func (g *Generator) GenerateDHCP(config *configurator.Config, target string, eths []configurator.EthernetInterface) error {
|
||||
// generate file using gonja template
|
||||
// TODO: load template file for DHCP
|
||||
path := config.TemplatePaths[target]
|
||||
fmt.Printf("path: %s\neth count: %v\n", path, len(eths))
|
||||
t, err := gonja.FromFile(config.TemplatePaths[target])
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read template from file: %v", err)
|
||||
}
|
||||
template := "# ========== GENERATED BY OCHAMI CONFIGURATOR ==========\n"
|
||||
for _, eth := range eths {
|
||||
if eth.Type == "NodeBMC" {
|
||||
template += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
|
||||
} else {
|
||||
template += "dhcp-host=" + eth.MacAddress + "," + eth.ComponentId + "," + eth.IpAddresses[0].IpAddress + "\n"
|
||||
}
|
||||
}
|
||||
template += "# ======================================================"
|
||||
data := exec.NewContext(map[string]any{
|
||||
"hosts": template,
|
||||
})
|
||||
if err = t.Execute(os.Stdout, data); err != nil {
|
||||
return fmt.Errorf("failed to execute: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
43
internal/util/util.go
Normal file
43
internal/util/util.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func PathExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not create new HTTP request: %v", err)
|
||||
}
|
||||
req.Header.Add("User-Agent", "magellan")
|
||||
for k, v := range headers {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not make request: %v", err)
|
||||
}
|
||||
b, err := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("could not read response body: %v", err)
|
||||
}
|
||||
return res, b, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue