global

package
v0.0.0-...-e4215d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InfraCtlRoot   = "infractl"
	BuildSubFolder = "build"
	CratsSubFolder = "crafts"
	FilePerm       = 0744

	KiB = 1024
	MiB = KiB * KiB
	GiB = MiB * KiB
	KB  = 1000
	MB  = KB * KB
	GB  = MB * KB

	OneVCPU = 1024

	RabbitMQPort = 5672
)

Variables

View Source
var ForceRebuild bool

ForceRebuild is a flag to force the rebuild of the docker images even if no code change is detected

View Source
var RootFolder = "."

RootFolder is the absolute path of the root folder of the Infractl tree

View Source
var RootParent = ""

Functions

func AddQuotes

func AddQuotes(s string) string

func AdjustAwsString

func AdjustAwsString(s string) string

func BuildFromTemplate

func BuildFromTemplate(templateString, buildFolder, fileName string, config any) (err error)

func CollectCraftNames

func CollectCraftNames(path string, names map[string]bool) (err error)

func DeleteFiles

func DeleteFiles(folder, wildcard string) (err error)

func ExpandEnvInFileNames

func ExpandEnvInFileNames(files []string)

func FileExists

func FileExists(path string) bool

FileExists also works for folders

func GetAbsoluteCraftPath

func GetAbsoluteCraftPath(path string) (string, error)

func GetBuildFolder

func GetBuildFolder(craftName string) (buildFolder string, err error)

func GetCraftPath

func GetCraftPath(args []string) (path string, err error)

GetCraftPath returns the craft absolute path from the command line arguments and sets the root folder

func GetEnvMapFromFiles

func GetEnvMapFromFiles(files []string) (envMap map[string]string, err error)

GetEnvMapFromFiles assumes that relative names refer to root parent

func GetTerraformOutputs

func GetTerraformOutputs(folder string, outputs map[string]string) (err error)

func HashOfFolder

func HashOfFolder(folder string) (string, error)

HashOfFolder computes a hash of files in the folder, 16 hex characters This is used to determine if the folder has changed

func ListServices

func ListServices(craftPath string) error

func NameOnly

func NameOnly(path string) string

func QuotedArray

func QuotedArray(arr []string) string

func ReadEnvFile

func ReadEnvFile(name string, envMap map[string]string) error

ReadEnvFile reads a file of format name=value and adds to envMap

func RemoveFileExtension

func RemoveFileExtension(fileName string) string

func RemoveQuotes

func RemoveQuotes(s string) (string, bool)

RemoveQuotes removes quotes from a string if present and returns the string and true if quotes were removed

func RunTerraformCommand

func RunTerraformCommand(folder string, command []string) error

RunTerraformCommand runs a terraform command in a folder

func SetBackend

func SetBackend(backendS string) error

func SetDefaultRootFolder

func SetDefaultRootFolder()

func SetOsEnvsFromCraft

func SetOsEnvsFromCraft(craftPath string) (err error)

func SetRootFolder

func SetRootFolder(args []string)

func StringFromTemplate

func StringFromTemplate(templateString string, config any) (s string, err error)

func TerraformDeploy

func TerraformDeploy(buildFolder, craftName string) (err error)

func ToAbsPathBasedOn

func ToAbsPathBasedOn(base, path string) string

func TrimAndRemoveQuotes

func TrimAndRemoveQuotes(s string) string

func WriteFargateFile

func WriteFargateFile(buildFolder, file, content string) (err error)

func WriteStringToFile

func WriteStringToFile(path, content string) (err error)

Types

type BackendConfig

type BackendConfig struct {
	Bucket string
	Key    string
}
var Backend *BackendConfig

type Config

type Config struct {
	Compose *types.Project // From compose file
	Recipe  any            // ECSRecipe or NetworkRecipe

	NameToService map[string]*Service
	BuildFolder   string
	// The Terraform outputs to be used as environment variables in other crafts
	// The first key is the craft name, the second key is the output name
	OutputsMap map[string]map[string]string
}

Config is top level collection of configuration info

func NewConfig

func NewConfig(craft *EcsRecipe) (*Config, error)

func (*Config) BuildEcs

func (cfg *Config) BuildEcs() (err error)

BuildEcs creates the Terraform files from the Fargate templates configPath is the Infragraph configuration

func (*Config) BuildFromRecipeTemplate

func (cfg *Config) BuildFromRecipeTemplate(templateString, fileName string) (err error)

func (*Config) BuildFromServiceTemplate

func (cfg *Config) BuildFromServiceTemplate(templateString, fileName string, services Services) (err error)

func (*Config) BuildFromTemplate

func (cfg *Config) BuildFromTemplate(templateString, fileName string, config any) (err error)

func (*Config) BuildLambda

func (cfg *Config) BuildLambda() (err error)

func (*Config) BuildNetwork

func (cfg *Config) BuildNetwork() (err error)

func (*Config) BuildSliceFromTemplate

func (cfg *Config) BuildSliceFromTemplate(templateString, fileName string, slice []any) (err error)

func (*Config) GetEcsRecipe

func (cfg *Config) GetEcsRecipe() *EcsRecipe

func (*Config) GetLambdaRecipe

func (cfg *Config) GetLambdaRecipe() *LambdaRecipe

func (*Config) GetMqs

func (config *Config) GetMqs(services Services) (mqsMap map[string]Mq, servs Services, err error)

GetMqs returns the message queues defined in the simple_mqs section of the recipe and removes them from the services

func (*Config) GetNetworkRecipe

func (cfg *Config) GetNetworkRecipe() *NetworkRecipe

func (*Config) GetPortsFor

func (cfg *Config) GetPortsFor(name string) (ports map[int]bool)

func (*Config) GetRds

func (config *Config) GetRds(services Services) (rdsMap map[string]Rdb, servs Services, err error)

func (*Config) ProcessAllServices

func (config *Config) ProcessAllServices() (fargate Services, rdsMap map[string]Rdb, mqsMap map[string]Mq, err error)

type Craft

type Craft interface {
	GetCraftSection() *CraftSection
	GetCreds() *Creds
}

func NewCraft

func NewCraft(path string) (Craft, error)

NewCraft returns a ECSRecipe, NetworkRecipe or LambdaRecipe

type CraftSection

type CraftSection struct {
	RecipeVersion      string        `yaml:"recipe_version"`
	Type               string        `yaml:"type"` // ecs, network, lambda
	InfraEnvFile       string        `yaml:"infra_env_file"`
	DockerComposeFiles []string      `yaml:"docker_compose_files"`
	NetworkCraftFile   string        `yaml:"network_craft_file"`
	Name               string        // The name of the craft file
	BackendConfig      BackendConfig // optional
}

func (*CraftSection) ExpandEnvsInFileNames

func (cs *CraftSection) ExpandEnvsInFileNames()

type Creds

type Creds struct {
	AccessKey     string `yaml:"AWS_ACCESS_KEY_ID"`
	SecretKey     string `yaml:"AWS_SECRET_ACCESS_KEY"`
	DefaultRegion string `yaml:"AWS_DEFAULT_REGION"`
}

func (*Creds) SetOsEnvs

func (creds *Creds) SetOsEnvs()

type DataBaseTemplate

type DataBaseTemplate struct {
	ImageName     []string
	RdsEngineName string
	DatabaseName  []string
	UserName      string
	Password      string
}

type DbItems

type DbItems struct {
	Public      bool     `yaml:"public"`
	DbEngine    string   `yaml:"engine"`
	MachineType string   `yaml:"machine_type"`
	StorageType string   `yaml:"storage_type"`
	StorageIops int      `yaml:"storage_iops"`
	StorageGigs int      `yaml:"storage_gigs"`
	EnvFiles    []string `yaml:"env_files"`
}

type EcsRecipe

type EcsRecipe struct {
	CraftSection `yaml:"craft"`
	SimpleEcs    SimpleEcs  `yaml:"simple_ecs"`
	SimpleRds    SimpleRds  `yaml:"simple_rds"`
	SimpleMqs    SimpleMqs  `yaml:"simple_mqs"`
	Externals    []External `yaml:"externals"`
	Creds        `yaml:"credentials"`
	Network      Network // Not in craft file
}

func NewEcsCraft

func NewEcsCraft(path string) (*EcsRecipe, error)

func (*EcsRecipe) BuildAndDeploy

func (ecs *EcsRecipe) BuildAndDeploy(craftPath, externalName string, outputs map[string]map[string]string) error

BuildAndDeploy builds the Terraform files, deploys them and collects outputs into the outputs map

func (*EcsRecipe) GetCraftSection

func (ecs *EcsRecipe) GetCraftSection() *CraftSection

func (*EcsRecipe) GetCreds

func (ecs *EcsRecipe) GetCreds() *Creds

type EcsService

type EcsService struct {
	Name         string `json:"name"`
	Arn          string `json:"arn"`
	Status       string `json:"status"`
	DesiredCount int    `json:"desired_count"`
	RunningCount int    `json:"running_count"`
}

type External

type External struct {
	Name      string `yaml:"name"`
	CraftFile string `yaml:"craft_file"` // If relative, then to the location of enclosing file
}

type HealthCheck

type HealthCheck struct {
	Test     string
	Interval int
	Timeout  int
	Retries  uint
}

type LambdaRecipe

type LambdaRecipe struct {
	CraftSection `yaml:"craft"`
	Network      Network      `yaml:"network"`
	SimpleLambda SimpleLambda `yaml:"simple_lambda"`
	Creds        `yaml:"credentials"`
}

func (*LambdaRecipe) BuildAndDeploy

func (lam *LambdaRecipe) BuildAndDeploy(craftPath string, outputs map[string]map[string]string) error

BuildAndDeploy builds the Terraform files, deploys them and collects outputs into the outputs map

func (*LambdaRecipe) GetCraftSection

func (lam *LambdaRecipe) GetCraftSection() *CraftSection

func (*LambdaRecipe) GetCreds

func (lam *LambdaRecipe) GetCreds() *Creds

type LambdaTriggers

type LambdaTriggers struct {
	ScheduleExpression string `yaml:"schedule_expression"`
	S3ObjectCreated    string `yaml:"s3_object_created"`
}

type Mq

type Mq struct {
	Name     string // From service name in compose file
	Engine   string // from craft file
	Public   bool   // from craft file
	UserName string // from envs
	Password string // from envs
}

Mq is a managed message queue

type MqItems

type MqItems struct {
	Public   bool     `yaml:"public"`
	Engine   string   `yaml:"engine"`
	EnvFiles []string `yaml:"env_files"`
}

type Network

type Network struct {
	ClusterName      string `yaml:"cluster_name"`
	VpcCidr          string `yaml:"vpc_cidr"`
	VpcId            string `yaml:"vpc_id"`
	PublicSubnetId   string
	PublicSubnet2Id  string
	PrivateSubnetId  string
	PrivateSubnet2Id string
}

type NetworkRecipe

type NetworkRecipe struct {
	CraftSection `yaml:"craft"`
	Network      Network `yaml:"network"`
	Creds        `yaml:"credentials"`
}

func NewNetworkCraft

func NewNetworkCraft(path string) (*NetworkRecipe, error)

func (*NetworkRecipe) BuildAndDeploy

func (net *NetworkRecipe) BuildAndDeploy(craftPath string, outputs map[string]map[string]string) error

BuildAndDeploy builds the Terraform files, deploys them and collects outputs into the outputs map

func (*NetworkRecipe) GetCraftSection

func (net *NetworkRecipe) GetCraftSection() *CraftSection

func (*NetworkRecipe) GetCreds

func (net *NetworkRecipe) GetCreds() *Creds

type Ports

type Ports struct {
	Target    int // On the container
	Published int // On the host
}

Ports is mapping the ports on the task or load balancer For Fargate task, both bort must be the same

func PortsFromString

func PortsFromString(s string) (Ports, error)

PortsFromString constructs Ports from a colon separate pair

type Rdb

type Rdb struct {
	Name        string // From service name in compose file
	DbEngine    string
	DbName      string
	UserName    string
	Password    string
	Port        int
	Public      bool
	MachineType string
	StorageType string
	StorageIops int
	StorageGigs int
}

Rdb is a managed relational database

func (*Rdb) Validate

func (rd *Rdb) Validate() string

type RecipeServiceItems

type RecipeServiceItems struct {
	DesiredNodes      int               `yaml:"desired_nodes"`
	Cpu               int               `yaml:"cpu"`
	Memory            int               `yaml:"memory"`
	DomainName        string            `yaml:"domain_name"`
	LoadBalancerHttp  []string          `yaml:"load_balancer_http"`
	LoadBalancerHttps []string          `yaml:"load_balancer_https"`
	Environment       map[string]string `yaml:"environment"`
	EnvFiles          []string          `yaml:"env_files"`
	DependsOn         []string          `yaml:"depends_on"`
}

RecipeServiceItems is the configuration for each service from the recipe

type Service

type Service struct {
	Name                 string
	Ports                []Ports
	DbPort               int
	LoadBalancerPortsTCP []Ports
	LoadBalancerPortsTLS []Ports
	LoadBalacerTargets   []int
	NeedsCertificate     bool
	Memory               int // in MiB
	Cpu                  int // 1024 = 1vCPU
	DesiredCount         int
	EnvsString           string
	Entrypoint           string
	IsOnlyService        bool // true if there is only one service in the compose file

	BuildContext    string // folder of where the build is done
	BuildHash       string // hash of the build context
	TaskHash        string // the last 10 characters of BuildHash
	BuildDockerfile string // name of Dockerfile
	BuildTarget     string

	Image     string // either a docker registry image or an ECR image after build and push
	DependsOn string

	HealthCheck HealthCheck
	DomainName  string
	HostedZone  string
	// contains filtered or unexported fields
}

func (*Service) ComputeDependsOn

func (serv *Service) ComputeDependsOn(nameToService map[string]*Service, rdsMap map[string]Rdb, mqsMap map[string]Mq) error

func (*Service) CreateEnvsString

func (srv *Service) CreateEnvsString()

func (*Service) ProcessImage

func (serv *Service) ProcessImage() error

func (*Service) ReadEnvFile

func (srv *Service) ReadEnvFile(name string) error

ReadEnvFile reads a file of format name=value

func (*Service) UpdateFromRecipe

func (svc *Service) UpdateFromRecipe(cfg *Config) error

UpdateFromRecipe updates the service from new data in the recipe

type Services

type Services []*Service

func FilterServices

func FilterServices(services Services, filter func(service *Service) bool) Services

type SimpleEcs

type SimpleEcs struct {
	Type        string                        `yaml:"type"`         // fargate or EC2
	MachineType string                        `yaml:"machine_type"` // only for EC2
	Services    map[string]RecipeServiceItems `yaml:"services"`
}

type SimpleLambda

type SimpleLambda struct {
	FunctionName     string `yaml:"function_name"`
	Handler          string `yaml:"handler"`
	Runtime          string `yaml:"runtime"`
	SourceFolder     string `yaml:"source_folder"`
	MemorySize       int    `yaml:"memory_size"`
	Timeout          int    `yaml:"timeout"`
	EphemeralStorage int    `yaml:"ephemeral_storage"`
	LambdaTriggers   `yaml:"triggers"`
	Environment      map[string]string `yaml:"environment"`
	EnvFiles         []string          `yaml:"env_files"`
	Layers           []string          `yaml:"layers"`

	EnvsString   string // Constructed
	LayersString string // Constructed
	BuildFolder  string // Constructed
}

type SimpleMqs

type SimpleMqs struct {
	Queues map[string]MqItems `yaml:"services"`
}

SimpleMqs is for AWS managed message queues

type SimpleRds

type SimpleRds struct {
	Databases map[string]DbItems `yaml:"services"`
}

type StringPair

type StringPair struct {
	S1 string
	S2 string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL