2021-08-02 04:51:58 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-08-06 17:38:06 +00:00
|
|
|
"path/filepath"
|
2021-08-02 04:51:58 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-09-05 19:37:03 +00:00
|
|
|
"coopcloud.tech/abra/pkg/client/convert"
|
|
|
|
loader "coopcloud.tech/abra/pkg/client/stack"
|
|
|
|
stack "coopcloud.tech/abra/pkg/client/stack"
|
2021-08-06 17:38:06 +00:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2021-09-10 22:54:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-08-02 04:51:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Type aliases to make code hints easier to understand
|
2021-08-02 07:02:18 +00:00
|
|
|
|
|
|
|
// AppEnv is a map of the values in an apps env config
|
2021-08-02 04:51:58 +00:00
|
|
|
type AppEnv = map[string]string
|
2021-08-02 07:02:18 +00:00
|
|
|
|
|
|
|
// AppName is AppName
|
2021-08-02 04:51:58 +00:00
|
|
|
type AppName = string
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// AppFile represents app env files on disk without reading the contents
|
2021-08-02 04:51:58 +00:00
|
|
|
type AppFile struct {
|
|
|
|
Path string
|
|
|
|
Server string
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// AppFiles is a slice of appfiles
|
2021-08-02 04:51:58 +00:00
|
|
|
type AppFiles map[AppName]AppFile
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// App reprents an app with its env file read into memory
|
2021-08-02 04:51:58 +00:00
|
|
|
type App struct {
|
|
|
|
Name AppName
|
|
|
|
Type string
|
|
|
|
Domain string
|
|
|
|
Env AppEnv
|
2021-09-05 21:17:35 +00:00
|
|
|
Server string
|
|
|
|
Path string
|
2021-08-02 04:51:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// StackName gets what the docker safe stack name is for the app
|
2021-08-02 04:51:58 +00:00
|
|
|
func (a App) StackName() string {
|
|
|
|
return SanitiseAppName(a.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SORTING TYPES
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// ByServer sort a slice of Apps
|
2021-08-02 04:51:58 +00:00
|
|
|
type ByServer []App
|
|
|
|
|
|
|
|
func (a ByServer) Len() int { return len(a) }
|
|
|
|
func (a ByServer) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByServer) Less(i, j int) bool {
|
2021-09-05 21:17:35 +00:00
|
|
|
return strings.ToLower(a[i].Server) < strings.ToLower(a[j].Server)
|
2021-08-02 04:51:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// ByServerAndType sort a slice of Apps
|
2021-08-02 04:51:58 +00:00
|
|
|
type ByServerAndType []App
|
|
|
|
|
|
|
|
func (a ByServerAndType) Len() int { return len(a) }
|
|
|
|
func (a ByServerAndType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByServerAndType) Less(i, j int) bool {
|
2021-09-05 21:17:35 +00:00
|
|
|
if a[i].Server == a[j].Server {
|
2021-08-02 04:51:58 +00:00
|
|
|
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
|
|
|
|
}
|
2021-09-05 21:17:35 +00:00
|
|
|
return strings.ToLower(a[i].Server) < strings.ToLower(a[j].Server)
|
2021-08-02 04:51:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// ByType sort a slice of Apps
|
2021-08-02 04:51:58 +00:00
|
|
|
type ByType []App
|
|
|
|
|
|
|
|
func (a ByType) Len() int { return len(a) }
|
|
|
|
func (a ByType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByType) Less(i, j int) bool {
|
|
|
|
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// ByName sort a slice of Apps
|
2021-08-02 04:51:58 +00:00
|
|
|
type ByName []App
|
|
|
|
|
|
|
|
func (a ByName) Len() int { return len(a) }
|
|
|
|
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a ByName) Less(i, j int) bool {
|
|
|
|
return strings.ToLower(a[i].Name) < strings.ToLower(a[j].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAppEnvFile(appFile AppFile, name AppName) (App, error) {
|
|
|
|
env, err := ReadEnv(appFile.Path)
|
|
|
|
if err != nil {
|
|
|
|
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
|
|
|
logrus.Debugf("read env '%s' from '%s'", env, appFile.Path)
|
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
app, err := newApp(env, name, appFile)
|
|
|
|
if err != nil {
|
|
|
|
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newApp creates new App object
|
|
|
|
func newApp(env AppEnv, name string, appFile AppFile) (App, error) {
|
|
|
|
// Checking for type as it is required - apps wont work without it
|
|
|
|
domain := env["DOMAIN"]
|
|
|
|
apptype, ok := env["TYPE"]
|
|
|
|
if !ok {
|
|
|
|
return App{}, errors.New("missing TYPE variable")
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
return App{
|
|
|
|
Name: name,
|
|
|
|
Domain: domain,
|
|
|
|
Type: apptype,
|
|
|
|
Env: env,
|
2021-09-05 21:17:35 +00:00
|
|
|
Server: appFile.Server,
|
|
|
|
Path: appFile.Path,
|
2021-08-02 04:51:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// LoadAppFiles gets all app files for a given set of servers or all servers
|
2021-08-02 04:51:58 +00:00
|
|
|
func LoadAppFiles(servers ...string) (AppFiles, error) {
|
|
|
|
appFiles := make(AppFiles)
|
|
|
|
if len(servers) == 1 {
|
|
|
|
if servers[0] == "" {
|
|
|
|
// Empty servers flag, one string will always be passed
|
|
|
|
var err error
|
|
|
|
servers, err = getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
|
|
|
logrus.Debugf("collecting metadata from '%v' servers: '%s'", len(servers), servers)
|
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
serverDir := path.Join(ABRA_SERVER_FOLDER, server)
|
|
|
|
files, err := getAllFilesInDirectory(serverDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
appName := strings.TrimSuffix(file.Name(), ".env")
|
|
|
|
appFilePath := path.Join(ABRA_SERVER_FOLDER, server, file.Name())
|
|
|
|
appFiles[appName] = AppFile{
|
|
|
|
Path: appFilePath,
|
|
|
|
Server: server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return appFiles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetApp loads an apps settings, reading it from file, in preparation to use it
|
|
|
|
//
|
|
|
|
// ONLY use when ready to use the env file to keep IO down
|
|
|
|
func GetApp(apps AppFiles, name AppName) (App, error) {
|
|
|
|
appFile, exists := apps[name]
|
|
|
|
if !exists {
|
2021-09-05 22:45:13 +00:00
|
|
|
return App{}, fmt.Errorf("cannot find app with name '%s'", name)
|
2021-08-02 04:51:58 +00:00
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
app, err := readAppEnvFile(appFile, name)
|
|
|
|
if err != nil {
|
|
|
|
return App{}, err
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// GetApps returns a slice of Apps with their env files read from a given slice of AppFiles
|
2021-08-02 04:51:58 +00:00
|
|
|
func GetApps(appFiles AppFiles) ([]App, error) {
|
|
|
|
var apps []App
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
for name := range appFiles {
|
|
|
|
app, err := GetApp(appFiles, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
apps = append(apps, app)
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
return apps, nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 07:10:05 +00:00
|
|
|
// GetAppServiceNames retrieves a list of app service names.
|
|
|
|
func GetAppServiceNames(appName string) ([]string, error) {
|
|
|
|
var serviceNames []string
|
|
|
|
|
|
|
|
appFiles, err := LoadAppFiles("")
|
|
|
|
if err != nil {
|
|
|
|
return serviceNames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
app, err := GetApp(appFiles, appName)
|
|
|
|
if err != nil {
|
|
|
|
return serviceNames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
composeFiles, err := GetAppComposeFiles(app.Type, app.Env)
|
|
|
|
if err != nil {
|
|
|
|
return serviceNames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := stack.Deploy{Composefiles: composeFiles}
|
|
|
|
compose, err := GetAppComposeConfig(app.Type, opts, app.Env)
|
|
|
|
if err != nil {
|
|
|
|
return serviceNames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range compose.Services {
|
|
|
|
serviceNames = append(serviceNames, service.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceNames, nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
// GetAppNames retrieves a list of app names.
|
2021-09-08 11:43:55 +00:00
|
|
|
func GetAppNames() ([]string, error) {
|
2021-09-16 07:09:51 +00:00
|
|
|
var appNames []string
|
|
|
|
|
2021-09-07 14:57:39 +00:00
|
|
|
appFiles, err := LoadAppFiles("")
|
|
|
|
if err != nil {
|
2021-09-16 07:09:51 +00:00
|
|
|
return appNames, err
|
2021-09-07 14:57:39 +00:00
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-09-07 14:57:39 +00:00
|
|
|
apps, err := GetApps(appFiles)
|
|
|
|
if err != nil {
|
2021-09-16 07:09:51 +00:00
|
|
|
return appNames, err
|
2021-09-07 14:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, app := range apps {
|
|
|
|
appNames = append(appNames, app.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return appNames, nil
|
|
|
|
}
|
|
|
|
|
2021-09-16 13:04:04 +00:00
|
|
|
// TemplateAppEnvSample copies the example env file for the app into the users env files
|
|
|
|
func TemplateAppEnvSample(appType, appName, server, domain, recipe string) error {
|
2021-08-02 04:51:58 +00:00
|
|
|
envSamplePath := path.Join(ABRA_DIR, "apps", appType, ".env.sample")
|
|
|
|
envSample, err := ioutil.ReadFile(envSamplePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
appEnvPath := path.Join(ABRA_DIR, "servers", server, fmt.Sprintf("%s.env", appName))
|
|
|
|
if _, err := os.Stat(appEnvPath); err == nil {
|
|
|
|
return fmt.Errorf("%s already exists?", appEnvPath)
|
|
|
|
}
|
|
|
|
|
2021-09-17 05:38:38 +00:00
|
|
|
envSample = []byte(strings.Replace(string(envSample), fmt.Sprintf("%s.example.com", recipe), domain, -1))
|
|
|
|
envSample = []byte(strings.Replace(string(envSample), "example.com", domain, -1))
|
2021-09-16 13:04:04 +00:00
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
err = ioutil.WriteFile(appEnvPath, envSample, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("copied '%s' to '%s'", envSamplePath, appEnvPath)
|
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// SanitiseAppName makes a app name usable with Docker by replacing illegal characters
|
2021-08-02 04:51:58 +00:00
|
|
|
func SanitiseAppName(name string) string {
|
|
|
|
return strings.ReplaceAll(name, ".", "_")
|
|
|
|
}
|
|
|
|
|
2021-08-02 07:02:18 +00:00
|
|
|
// GetAppStatuses queries servers to check the deployment status of given apps
|
2021-08-02 04:51:58 +00:00
|
|
|
func GetAppStatuses(appFiles AppFiles) (map[string]string, error) {
|
2021-09-16 07:26:12 +00:00
|
|
|
statuses := map[string]string{}
|
|
|
|
|
|
|
|
servers, err := GetServers()
|
|
|
|
if err != nil {
|
|
|
|
return statuses, err
|
|
|
|
}
|
|
|
|
|
2021-08-03 07:49:16 +00:00
|
|
|
ch := make(chan stack.StackStatus, len(servers))
|
2021-08-02 04:51:58 +00:00
|
|
|
for _, server := range servers {
|
2021-08-25 12:06:42 +00:00
|
|
|
go func(s string) { ch <- stack.GetAllDeployedServices(s) }(server)
|
2021-08-02 04:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for range servers {
|
|
|
|
status := <-ch
|
|
|
|
for _, service := range status.Services {
|
2021-08-03 07:49:16 +00:00
|
|
|
name := service.Spec.Labels[convert.LabelNamespace]
|
2021-08-02 04:51:58 +00:00
|
|
|
if _, ok := statuses[name]; !ok {
|
|
|
|
statuses[name] = "deployed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 22:54:02 +00:00
|
|
|
logrus.Debugf("retrieved app statuses: '%s'", statuses)
|
|
|
|
|
2021-08-02 04:51:58 +00:00
|
|
|
return statuses, nil
|
|
|
|
}
|
2021-08-06 17:38:06 +00:00
|
|
|
|
2021-09-04 20:02:49 +00:00
|
|
|
// GetAppComposeFiles gets the list of compose files for an app which should be
|
|
|
|
// merged into a composetypes.Config while respecting the COMPOSE_FILE env var.
|
|
|
|
func GetAppComposeFiles(recipe string, appEnv AppEnv) ([]string, error) {
|
|
|
|
if _, ok := appEnv["COMPOSE_FILE"]; !ok {
|
2021-09-16 07:45:02 +00:00
|
|
|
logrus.Debug("no COMPOSE_FILE detected, loading all compose files")
|
2021-09-04 20:02:49 +00:00
|
|
|
pattern := fmt.Sprintf("%s/%s/compose**yml", APPS_DIR, recipe)
|
|
|
|
composeFiles, err := filepath.Glob(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return composeFiles, err
|
|
|
|
}
|
|
|
|
return composeFiles, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var composeFiles []string
|
|
|
|
composeFileEnvVar := appEnv["COMPOSE_FILE"]
|
2021-09-16 07:45:02 +00:00
|
|
|
envVars := strings.Split(composeFileEnvVar, ":")
|
2021-09-16 07:48:28 +00:00
|
|
|
logrus.Debugf("COMPOSE_FILE detected ('%s'), loading '%s'", composeFileEnvVar, envVars)
|
2021-09-04 20:02:49 +00:00
|
|
|
for _, file := range strings.Split(composeFileEnvVar, ":") {
|
|
|
|
path := fmt.Sprintf("%s/%s/%s", APPS_DIR, recipe, file)
|
|
|
|
composeFiles = append(composeFiles, path)
|
2021-08-06 17:38:06 +00:00
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
|
|
|
logrus.Debugf("retrieved '%s' configs for '%s'", strings.Join(composeFiles, ", "), recipe)
|
|
|
|
|
2021-09-04 20:02:49 +00:00
|
|
|
return composeFiles, nil
|
|
|
|
}
|
2021-08-06 17:38:06 +00:00
|
|
|
|
2021-09-04 20:02:49 +00:00
|
|
|
// GetAppComposeConfig retrieves a compose specification for a recipe. This
|
|
|
|
// specification is the result of a merge of all the compose.**.yml files in
|
|
|
|
// the recipe repository.
|
|
|
|
func GetAppComposeConfig(recipe string, opts stack.Deploy, appEnv AppEnv) (*composetypes.Config, error) {
|
2021-09-01 13:01:20 +00:00
|
|
|
compose, err := loader.LoadComposefile(opts, appEnv)
|
2021-08-06 17:38:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return &composetypes.Config{}, err
|
|
|
|
}
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-09-14 14:14:09 +00:00
|
|
|
logrus.Debugf("retrieved '%s' for '%s'", compose.Filename, recipe)
|
2021-09-10 22:54:02 +00:00
|
|
|
|
2021-08-06 17:38:06 +00:00
|
|
|
return compose, nil
|
|
|
|
}
|