update: Added Longhorn installation process and updated memory allocation for VMs

update: Added 'git' and 'vagrant' to required tools in pre-flight checks

fix: configured k3s install to use internal nic for flanel network

update: Added Longhorn installation process and updated memory allocation for VMs

update: Added 'git' and 'vagrant' to required tools in pre-flight checks

fix: configured k3s install to use internal nic for flanel network

fix: corrected JSON formatting for config json

update: reduce VM memory allocation to 2GB, add Longhorn installation scripts and prerequisites, and implement checks for existing pods

fix: merge issues

fix: merge issues

update: Added Longhorn installation process and updated memory allocation for VMs

update: Added 'git' and 'vagrant' to required tools in pre-flight checks

fix: configured k3s install to use internal nic for flanel network

update: Added Longhorn installation process and updated memory allocation for VMs

update: Added 'git' and 'vagrant' to required tools in pre-flight checks

fix: configured k3s install to use internal nic for flanel network

fix: corrected JSON formatting for config json

update: reduce VM memory allocation to 2GB, add Longhorn installation scripts and prerequisites, and implement checks for existing pods

update: improve error logging in RunJsonDeployment and RunCommand functions

update: add jq installation to provision script

update: add version flag

bump version

fix: improve error messages for config file reading

feat: add Windows gitbash installation support and improve binary download process

clean up tmp code

fix: increase timeout for some slower windows clients

feat: add Ingress and Service configurations for nginx deployment, and implement MetalLB  and Traeik installation scripts

refactor: remove obsolete Traefik installation script

feat: add environment checks and configurations for Vagrant setup, including dnsmasq  MetalLB  and ingress

feat: add deployment and installation scripts for infmon-cli, including Kubernetes configurations

feat: refactor customer project creation and add success/failure job scripts

refactor: rename customer references to project in configuration and application logic

feat: enhance JSON deployment handling with retry logic and command execution improvements

feat: enhance RunJsonDeployment with error handling and retry logic; add tests for configuration reading

feat: add automatic creation of base and config JSON files from examples if they do not exist

refactor: remove database package and related functionality; update app state initialization and error handling

refactor: update deployment handling to use ProjectConfig; improve error messages and logging

feat: enhance RunJsonDeployment retry logic with configurable delay; improve logging for retries

feat: implement LoadConfigs function for improved configuration loading; add logger setup

refactor: remove unused fields from BaseConfig and ProjectConfig structs for cleaner configuration management

refactor: clean up tests by removing obsolete functions and simplifying test cases

chore: update version to v0.0.5 in install script

feat: implement default configuration creation for BaseConfig and ProjectConfig; enhance validation logic

fix: enhance configuration parsing and loading; streamline flag handling and error reporting

refactor: remove obsolete configuration download logic from installation script
This commit is contained in:
jon brookes 2025-08-16 18:00:28 +01:00
parent d839fd5687
commit 11b1f1b637
61 changed files with 1573 additions and 761 deletions

View file

@ -7,37 +7,30 @@ import (
"os"
)
const Version = "v0.0.5"
// Package-level variables for flags
var (
baseConfigFile string
projectConfigFile string
pipelineFile string
)
type BaseConfig struct {
ProjectsDirectory string `json:"projects_directory"`
Env string `json:"env"`
StaticImages string `json:"static_images"`
PublicImages string `json:"public_images"`
PhpConf string `json:"php_conf"`
Exports string `json:"exports"`
Logs string `json:"logs"`
PreviewPath string `json:"preview_path"`
DataWww string `json:"data_www"`
NginxConf string `json:"nginx_conf"`
AdminURL string `json:"admin_url"`
PreviewURL string `json:"preview_url"`
AppImage string `json:"app_image"`
WebserverImage string `json:"webserver_image"`
EmptyDB string `json:"empty_db"`
DB string `json:"db"`
EmptyImages string `json:"empty_imaages"`
DeploymentType string `json:"deployment_type"`
DeploymentFile string `json:"deployment_file"`
Port int `json:"port"`
RetryDelaySenconds int `json:"retry_delay_seconds"`
}
func ReadBaseConfig(path string) (BaseConfig, error) {
deploymentType := os.Getenv("DEPLOYMENT_TYPE")
deploymentFile := flag.String("deployment-file", "", "path to config file")
deploymentFileShorthand := flag.String("f", "", "shorthand for -deployment-file")
// ParseFlags parses all command-line flags and handles help/version flags
func ParseFlags() {
helpFlag := flag.Bool("help", false, "show help")
versionFlag := flag.Bool("version", false, "show version")
vFlag := flag.Bool("v", false, "show version (shorthand)")
flag.StringVar(&baseConfigFile, "base-config", "", "Path to base config file (optional)")
flag.StringVar(&projectConfigFile, "project-config", "", "Path to project config file (optional)")
flag.StringVar(&pipelineFile, "f", "", "Path to pipeline file")
flag.StringVar(&pipelineFile, "deployment-file", "", "Path to pipeline file (long format)")
flag.Parse()
if *helpFlag {
@ -46,22 +39,79 @@ func ReadBaseConfig(path string) (BaseConfig, error) {
os.Exit(0)
}
var config BaseConfig
if *deploymentFileShorthand != "" {
config.DeploymentFile = *deploymentFileShorthand
} else if *deploymentFile != "" {
config.DeploymentFile = *deploymentFile
if *versionFlag || *vFlag {
fmt.Println("infctl-cli version:", Version)
os.Exit(0)
}
}
func ReadBaseConfig(path string) (BaseConfig, error) {
config := BaseConfig{}
// If base.json does not exist, create it with default value
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := CreateDefaultBaseConfig(path); err != nil {
return BaseConfig{}, fmt.Errorf("failed to create default base config: %w", err)
}
}
data, err := os.ReadFile(path)
if err != nil {
return BaseConfig{}, fmt.Errorf("failed to read file: %w", err)
return BaseConfig{}, fmt.Errorf("failed to read the config file: %w", err)
}
if err := json.Unmarshal(data, &config); err != nil {
return BaseConfig{}, fmt.Errorf("failed to unmarshal JSON: %w", err)
return config, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
config.DeploymentType = deploymentType
return config, nil
}
// CreateDefaultBaseConfig creates a default base.json with retry_delay_seconds: 3
func CreateDefaultBaseConfig(path string) error {
defaultConfig := BaseConfig{
RetryDelaySenconds: 3,
}
data, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal default base config: %w", err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("failed to write default base config to file: %w", err)
}
return nil
}
// LoadConfigs resolves config paths and loads both configs
func LoadConfigs() (BaseConfig, ProjectConfig, error) {
wd, err := os.Getwd()
if err != nil {
return BaseConfig{}, ProjectConfig{}, fmt.Errorf("failed to get current directory: %w", err)
}
var baseConfigPath string
if baseConfigFile == "" {
baseConfigPath = wd + string(os.PathSeparator) + "base.json"
} else {
baseConfigPath = baseConfigFile
}
var projectConfigPath string
if projectConfigFile == "" {
projectConfigPath = wd + string(os.PathSeparator) + "config.json"
} else {
projectConfigPath = projectConfigFile
}
baseConfig, err := ReadBaseConfig(baseConfigPath)
if err != nil {
return BaseConfig{}, ProjectConfig{}, fmt.Errorf("error reading base config file: %w", err)
}
projectConfig, err := ReadProjectConfig(projectConfigPath, &pipelineFile)
if err != nil {
return BaseConfig{}, ProjectConfig{}, fmt.Errorf("error reading project config file: %w", err)
}
return baseConfig, projectConfig, nil
}

50
config/base_test.go Normal file
View file

@ -0,0 +1,50 @@
package config
import (
"os"
"testing"
)
func TestReadBaseConfig_Basic(t *testing.T) {
// Create a temporary config file
file, err := os.CreateTemp("", "baseconfig_*.json")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(file.Name())
jsonContent := `{
"projects_directory": "/projects",
"env": "dev",
"static_images": "/static",
"public_images": "/public",
"php_conf": "/php.ini",
"exports": "/exports",
"logs": "/logs",
"preview_path": "/preview",
"data_www": "/data",
"nginx_conf": "/nginx.conf",
"admin_url": "http://admin",
"preview_url": "http://preview",
"app_image": "app:v1",
"webserver_image": "web:v1",
"empty_db": "empty.db",
"db": "app.db",
"empty_imaages": "empty.img",
"deployment_type": "json",
"deployment_file": "base.json",
"port": 8080
}`
file.WriteString(jsonContent)
file.Close()
os.Setenv("DEPLOYMENT_TYPE", "json")
config, err := ReadBaseConfig(file.Name())
if err != nil {
t.Fatalf("ReadBaseConfig failed: %v", err)
}
// Only check RetryDelaySenconds as that's the only field in BaseConfig now
if config.RetryDelaySenconds != 0 {
t.Errorf("expected RetryDelaySenconds 0, got %d", config.RetryDelaySenconds)
}
}

View file

@ -1,29 +0,0 @@
package config
import (
"encoding/json"
"fmt"
"os"
)
type CustomerConfig struct {
Project string `json:"project"`
CustomerDirectory string `json:"customer_directory"`
UIURL string `json:"ui_url"`
StaticURL string `json:"static_url"`
Port int `json:"port"`
}
func ReadCustomerConfig(path string) (CustomerConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return CustomerConfig{}, fmt.Errorf("failed to read file: %w", err)
}
var cust CustomerConfig
if err := json.Unmarshal(data, &cust); err != nil {
return CustomerConfig{}, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return cust, nil
}

108
config/project.go Normal file
View file

@ -0,0 +1,108 @@
package config
import (
"encoding/json"
"fmt"
"os"
)
type ProjectConfig struct {
LogFormat string `json:"log_format"`
DeploymentFile string `json:"deployment_file"`
DeploymentType string `json:"deployment_type"`
DeploymentMode string `json:"deployment_mode"`
}
func ValidateProjectConfig(config ProjectConfig) error {
if config.LogFormat != "full" && config.LogFormat != "basic" && config.LogFormat != "none" {
return fmt.Errorf("invalid LogFormat: %s (must be 'full', 'basic', or 'none')", config.LogFormat)
}
if _, err := os.Stat(config.DeploymentFile); os.IsNotExist(err) {
return fmt.Errorf("deployment file does not exist: %s", config.DeploymentFile)
} else if err != nil {
return fmt.Errorf("error checking deployment file: %w", err)
}
if config.DeploymentMode != "json" && config.DeploymentMode != "api" {
return fmt.Errorf("invalid DeploymentMode: %s (must be 'json' or 'api')", config.DeploymentMode)
}
fmt.Printf("DeploymentType: %s\n", config.DeploymentType)
if config.DeploymentType != "development" && config.DeploymentType != "pre-production" && config.DeploymentType != "production" {
return fmt.Errorf("invalid DeploymentType: %s (must be 'development', 'pre-production', or 'production')", config.DeploymentType)
}
return nil
}
func CreateDefaultJsonConfig(path string, depploymentFile string) error {
defaultConfig := ProjectConfig{
LogFormat: "full",
DeploymentType: "development",
DeploymentFile: depploymentFile,
DeploymentMode: "json",
}
data, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal default config: %w", err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
return fmt.Errorf("failed to write default config to file: %w", err)
}
return nil
}
func ReadProjectConfig(path string, pipelineFile *string) (ProjectConfig, error) {
var config ProjectConfig
if pipelineFile == nil || *pipelineFile == "" {
return ProjectConfig{}, fmt.Errorf("no deployment file specified, please use -f or --deployment-file flag")
}
config.DeploymentFile = *pipelineFile
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := CreateDefaultJsonConfig(path, config.DeploymentFile); err != nil {
return ProjectConfig{}, fmt.Errorf("failed to create default config: %w", err)
}
}
data, err := os.ReadFile(path)
if err != nil {
return ProjectConfig{}, fmt.Errorf("failed to read project configfile: %w", err)
}
var proj ProjectConfig
if err := json.Unmarshal(data, &proj); err != nil {
return ProjectConfig{}, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
if proj.DeploymentMode != "" {
config.DeploymentMode = proj.DeploymentMode
} else {
config.DeploymentMode = "json"
}
deploymentModeEnv := os.Getenv("DEPLOYMENT_MODE")
if deploymentModeEnv != "" {
config.DeploymentMode = deploymentModeEnv
}
config.LogFormat = proj.LogFormat
if config.LogFormat == "" {
config.LogFormat = "full"
}
config.DeploymentType = proj.DeploymentType
if err := ValidateProjectConfig(config); err != nil {
return ProjectConfig{}, err
}
return config, nil
}

46
config/project_test.go Normal file
View file

@ -0,0 +1,46 @@
package config
import (
"os"
"testing"
)
func TestReadProjectConfig_Basic(t *testing.T) {
file, err := os.CreateTemp("", "projectconfig_*.json")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
defer os.Remove(file.Name())
jsonContent := `{
"log_format": "full",
"deployment_file": "pipeline.json",
"deployment_mode": "json",
"deployment_type": "development"
}`
file.WriteString(jsonContent)
file.Close()
pipelineFile, err := os.CreateTemp("", "pipeline_*.json")
if err != nil {
t.Fatalf("failed to create temp pipeline file: %v", err)
}
defer os.Remove(pipelineFile.Name())
pipelineFile.WriteString(`{}`) // minimal valid JSON
pipelineFile.Close()
pipelineFilePath := pipelineFile.Name()
config, err := ReadProjectConfig(file.Name(), &pipelineFilePath)
if err != nil {
t.Fatalf("ReadProjectConfig failed: %v", err)
}
if config.LogFormat != "full" {
t.Errorf("expected LogFormat 'full', got '%s'", config.LogFormat)
}
if config.DeploymentFile != pipelineFilePath {
t.Errorf("expected DeploymentFile '%s', got '%s'", pipelineFilePath, config.DeploymentFile)
}
if config.DeploymentMode != "json" {
t.Errorf("expected DeploymentMode 'json', got '%s'", config.DeploymentMode)
}
}