2021-07-18 03:21:26 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-07-27 10:52:09 +00:00
|
|
|
"context"
|
2021-07-18 09:49:31 +00:00
|
|
|
"errors"
|
2021-07-18 03:21:26 +00:00
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
2021-07-19 06:04:37 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-07-19 07:36:38 +00:00
|
|
|
"strings"
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-27 10:52:09 +00:00
|
|
|
"coopcloud.tech/abra/client"
|
2021-07-31 10:47:09 +00:00
|
|
|
"github.com/Autonomic-Cooperative/godotenv"
|
2021-07-27 10:52:09 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2021-07-19 06:04:37 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-07-18 03:21:26 +00:00
|
|
|
)
|
|
|
|
|
2021-07-27 19:25:08 +00:00
|
|
|
const dockerStackNamespace = "com.docker.stack.namespace"
|
|
|
|
|
2021-07-19 07:36:38 +00:00
|
|
|
var ABRA_DIR = os.ExpandEnv("$HOME/.abra")
|
2021-07-19 06:04:37 +00:00
|
|
|
var ABRA_SERVER_FOLDER = path.Join(ABRA_DIR, "servers")
|
2021-07-21 16:21:45 +00:00
|
|
|
var APPS_JSON = path.Join(ABRA_DIR, "apps.json")
|
2021-07-24 22:07:35 +00:00
|
|
|
var APPS_DIR = path.Join(ABRA_DIR, "apps")
|
|
|
|
var REPOS_BASE_URL = "https://git.coopcloud.tech/coop-cloud"
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-19 06:04:37 +00:00
|
|
|
// Type aliases to make code hints easier to understand
|
2021-07-18 09:49:31 +00:00
|
|
|
type AppEnv = map[string]string
|
2021-07-19 06:04:37 +00:00
|
|
|
type AppName = string
|
2021-07-18 09:49:31 +00:00
|
|
|
|
|
|
|
type App struct {
|
2021-07-19 06:04:37 +00:00
|
|
|
Name AppName
|
2021-07-18 09:49:31 +00:00
|
|
|
Type string
|
|
|
|
Domain string
|
|
|
|
Env AppEnv
|
2021-07-26 16:23:28 +00:00
|
|
|
File AppFile
|
2021-07-18 09:49:31 +00:00
|
|
|
}
|
2021-07-26 16:23:28 +00:00
|
|
|
|
2021-07-27 19:25:08 +00:00
|
|
|
func (a App) StackName() string {
|
|
|
|
return strings.ReplaceAll(a.Name, ".", "_")
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:23:28 +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 {
|
|
|
|
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
|
|
|
|
}
|
|
|
|
|
2021-07-26 17:47:44 +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 {
|
|
|
|
if a[i].File.Server == a[j].File.Server {
|
|
|
|
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
|
|
|
|
} else {
|
|
|
|
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:23:28 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-19 06:04:37 +00:00
|
|
|
type AppFile struct {
|
2021-07-19 07:36:38 +00:00
|
|
|
Path string
|
|
|
|
Server string
|
2021-07-18 09:49:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 19:25:08 +00:00
|
|
|
type AppFiles map[AppName]AppFile
|
|
|
|
|
|
|
|
func (a AppFiles) GetServers() []string {
|
|
|
|
var unique []string
|
|
|
|
servers := make(map[string]struct{})
|
|
|
|
for _, appFile := range a {
|
|
|
|
if _, ok := servers[appFile.Server]; !ok {
|
|
|
|
servers[appFile.Server] = struct{}{}
|
|
|
|
unique = append(unique, appFile.Server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unique
|
|
|
|
}
|
2021-07-19 06:04:37 +00:00
|
|
|
|
2021-07-20 12:00:03 +00:00
|
|
|
func LoadAppFiles(servers ...string) (AppFiles, error) {
|
2021-07-19 07:36:38 +00:00
|
|
|
appFiles := make(AppFiles)
|
2021-08-01 23:37:23 +00:00
|
|
|
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-07-20 12:00:03 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
}
|
2021-07-19 07:36:38 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
serverDir := path.Join(ABRA_SERVER_FOLDER, server)
|
2021-07-19 11:47:46 +00:00
|
|
|
files, err := getAllFilesInDirectory(serverDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-19 07:36:38 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return appFiles, nil
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
2021-07-19 06:04:37 +00:00
|
|
|
|
|
|
|
// GetApp loads an apps settings, reading it from file, in preparation to use it
|
|
|
|
//
|
2021-07-31 20:25:32 +00:00
|
|
|
// ONLY use when ready to use the env file to keep IO down
|
2021-07-19 06:04:37 +00:00
|
|
|
func GetApp(apps AppFiles, name AppName) (App, error) {
|
|
|
|
appFile, exists := apps[name]
|
|
|
|
if !exists {
|
|
|
|
return App{}, fmt.Errorf("cannot find app file with name '%s'", name)
|
|
|
|
}
|
|
|
|
app, err := readAppFile(appFile, name)
|
2021-07-18 09:49:31 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return App{}, err
|
2021-07-18 09:49:31 +00:00
|
|
|
}
|
2021-07-19 06:04:37 +00:00
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:23:28 +00:00
|
|
|
func GetApps(appFiles AppFiles) ([]App, error) {
|
|
|
|
var apps []App
|
|
|
|
for name := range appFiles {
|
|
|
|
app, err := GetApp(appFiles, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
apps = append(apps, app)
|
|
|
|
}
|
|
|
|
return apps, nil
|
|
|
|
}
|
|
|
|
|
2021-07-27 10:52:09 +00:00
|
|
|
func GetAppStatuses(appFiles AppFiles) (map[string]string, error) {
|
|
|
|
type status struct {
|
|
|
|
services []swarm.Service
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:25:08 +00:00
|
|
|
servers := appFiles.GetServers()
|
2021-07-27 10:52:09 +00:00
|
|
|
ch := make(chan status, len(servers))
|
2021-07-27 19:25:08 +00:00
|
|
|
for _, server := range servers {
|
2021-07-27 10:52:09 +00:00
|
|
|
go func(s string) {
|
|
|
|
cl, err := client.NewClientWithContext(s)
|
|
|
|
if err != nil {
|
2021-07-27 19:25:08 +00:00
|
|
|
if strings.Contains(err.Error(), "does not exist") {
|
|
|
|
// No local context found, bail out gracefully
|
|
|
|
ch <- status{services: []swarm.Service{}, err: nil}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- status{services: []swarm.Service{}, err: err}
|
2021-07-27 10:52:09 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-27 19:25:08 +00:00
|
|
|
ctx := context.Background()
|
2021-07-27 10:52:09 +00:00
|
|
|
filter := filters.NewArgs()
|
2021-07-27 19:25:08 +00:00
|
|
|
filter.Add("label", dockerStackNamespace)
|
|
|
|
services, err := cl.ServiceList(ctx, types.ServiceListOptions{Filters: filter})
|
|
|
|
if err != nil {
|
|
|
|
ch <- status{services: []swarm.Service{}, err: err}
|
|
|
|
return
|
|
|
|
}
|
2021-07-27 10:52:09 +00:00
|
|
|
ch <- status{services: services, err: nil}
|
|
|
|
}(server)
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:25:08 +00:00
|
|
|
statuses := map[string]string{}
|
2021-07-27 10:52:09 +00:00
|
|
|
for range servers {
|
|
|
|
status := <-ch
|
|
|
|
for _, service := range status.services {
|
2021-07-27 19:25:08 +00:00
|
|
|
name := service.Spec.Labels[dockerStackNamespace]
|
2021-07-27 10:52:09 +00:00
|
|
|
if _, ok := statuses[name]; !ok {
|
|
|
|
statuses[name] = "deployed"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2021-07-31 10:47:09 +00:00
|
|
|
func CopyAppEnvSample(appType, appName, server string) error {
|
|
|
|
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))
|
2021-07-31 13:50:04 +00:00
|
|
|
if _, err := os.Stat(appEnvPath); err == nil {
|
|
|
|
return fmt.Errorf("%s already exists?", appEnvPath)
|
|
|
|
}
|
|
|
|
|
2021-07-31 10:47:09 +00:00
|
|
|
err = ioutil.WriteFile(appEnvPath, envSample, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-19 13:36:00 +00:00
|
|
|
// TODO: maybe better names than read and get
|
|
|
|
|
2021-07-19 06:04:37 +00:00
|
|
|
func readAppFile(appFile AppFile, name AppName) (App, error) {
|
2021-07-30 20:55:00 +00:00
|
|
|
env, err := ReadEnv(appFile.Path)
|
2021-07-19 11:47:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
|
|
|
|
}
|
2021-07-26 16:23:28 +00:00
|
|
|
app, err := makeApp(env, name, appFile)
|
2021-07-18 09:49:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
|
|
|
}
|
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:55:00 +00:00
|
|
|
func ReadEnv(filePath string) (AppEnv, error) {
|
2021-07-19 06:04:37 +00:00
|
|
|
var envFile AppEnv
|
|
|
|
envFile, err := godotenv.Read(filePath)
|
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return envFile, nil
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 16:23:28 +00:00
|
|
|
func makeApp(env AppEnv, name string, appFile AppFile) (App, error) {
|
2021-07-21 07:38:13 +00:00
|
|
|
// Checking for type as it is required - apps wont work without it
|
|
|
|
domain := env["DOMAIN"]
|
2021-07-18 09:49:31 +00:00
|
|
|
apptype, ok := env["TYPE"]
|
|
|
|
if !ok {
|
|
|
|
return App{}, errors.New("missing TYPE variable")
|
|
|
|
}
|
|
|
|
return App{
|
|
|
|
Name: name,
|
|
|
|
Domain: domain,
|
|
|
|
Type: apptype,
|
|
|
|
Env: env,
|
2021-07-26 16:23:28 +00:00
|
|
|
File: appFile,
|
2021-07-18 09:49:31 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:47:46 +00:00
|
|
|
func ReadServerNames() ([]string, error) {
|
|
|
|
serverNames, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return serverNames, nil
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getAllFilesInDirectory returns filenames of all files in directory
|
2021-07-19 11:47:46 +00:00
|
|
|
func getAllFilesInDirectory(directory string) ([]fs.FileInfo, error) {
|
2021-07-19 07:36:38 +00:00
|
|
|
var realFiles []fs.FileInfo
|
|
|
|
files, err := ioutil.ReadDir(directory)
|
2021-07-18 03:21:26 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
2021-07-19 07:36:38 +00:00
|
|
|
for _, file := range files {
|
|
|
|
// Follow any symlinks
|
|
|
|
filePath := path.Join(directory, file.Name())
|
|
|
|
realPath, err := filepath.EvalSymlinks(filePath)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warningf("broken symlink in your abra config folders: '%s'", filePath)
|
|
|
|
} else {
|
|
|
|
realFile, err := os.Stat(realPath)
|
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
|
|
|
if !realFile.IsDir() {
|
|
|
|
realFiles = append(realFiles, file)
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 03:21:26 +00:00
|
|
|
|
2021-07-19 07:36:38 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return realFiles, nil
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getAllFoldersInDirectory returns both folder and symlink paths
|
2021-07-19 11:47:46 +00:00
|
|
|
func getAllFoldersInDirectory(directory string) ([]string, error) {
|
2021-07-19 06:04:37 +00:00
|
|
|
var folders []string
|
|
|
|
files, err := ioutil.ReadDir(directory)
|
2021-07-18 03:21:26 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, err
|
2021-07-19 06:04:37 +00:00
|
|
|
}
|
|
|
|
if len(files) == 0 {
|
2021-07-19 11:47:46 +00:00
|
|
|
return nil, fmt.Errorf("directory is empty: '%s'", directory)
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
|
|
|
for _, file := range files {
|
2021-07-19 07:36:38 +00:00
|
|
|
// Check if file is directory or symlink
|
2021-07-18 03:21:26 +00:00
|
|
|
if file.IsDir() || file.Mode()&fs.ModeSymlink != 0 {
|
2021-07-19 06:04:37 +00:00
|
|
|
filePath := path.Join(directory, file.Name())
|
|
|
|
realDir, err := filepath.EvalSymlinks(filePath)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warningf("broken symlink in your abra config folders: '%s'", filePath)
|
|
|
|
} else if stat, err := os.Stat(realDir); err == nil && stat.IsDir() {
|
|
|
|
// path is a directory
|
|
|
|
folders = append(folders, file.Name())
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
2021-07-19 11:47:46 +00:00
|
|
|
return folders, nil
|
2021-07-18 03:21:26 +00:00
|
|
|
}
|
2021-07-28 20:10:42 +00:00
|
|
|
|
|
|
|
func EnsureAbraDirExists() error {
|
|
|
|
if _, err := os.Stat(ABRA_DIR); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir(ABRA_DIR, 0777); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|