forked from toolshed/abra
@ -13,6 +13,7 @@ import (
|
||||
loader "coopcloud.tech/abra/pkg/client/stack"
|
||||
stack "coopcloud.tech/abra/pkg/client/stack"
|
||||
composetypes "github.com/docker/cli/cli/compose/types"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Type aliases to make code hints easier to understand
|
||||
@ -93,10 +94,14 @@ func readAppEnvFile(appFile AppFile, name AppName) (App, error) {
|
||||
if err != nil {
|
||||
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
|
||||
}
|
||||
|
||||
logrus.Debugf("read env '%s' from '%s'", env, appFile.Path)
|
||||
|
||||
app, err := newApp(env, name, appFile)
|
||||
if err != nil {
|
||||
return App{}, fmt.Errorf("env file for '%s' has issues: %s", name, err.Error())
|
||||
}
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
@ -108,6 +113,7 @@ func newApp(env AppEnv, name string, appFile AppFile) (App, error) {
|
||||
if !ok {
|
||||
return App{}, errors.New("missing TYPE variable")
|
||||
}
|
||||
|
||||
return App{
|
||||
Name: name,
|
||||
Domain: domain,
|
||||
@ -131,6 +137,9 @@ func LoadAppFiles(servers ...string) (AppFiles, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Debugf("collecting metadata from '%v' servers: '%s'", len(servers), servers)
|
||||
|
||||
for _, server := range servers {
|
||||
serverDir := path.Join(ABRA_SERVER_FOLDER, server)
|
||||
files, err := getAllFilesInDirectory(serverDir)
|
||||
@ -157,16 +166,19 @@ func GetApp(apps AppFiles, name AppName) (App, error) {
|
||||
if !exists {
|
||||
return App{}, fmt.Errorf("cannot find app with name '%s'", name)
|
||||
}
|
||||
|
||||
app, err := readAppEnvFile(appFile, name)
|
||||
if err != nil {
|
||||
return App{}, err
|
||||
}
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
// GetApps returns a slice of Apps with their env files read from a given slice of AppFiles
|
||||
func GetApps(appFiles AppFiles) ([]App, error) {
|
||||
var apps []App
|
||||
|
||||
for name := range appFiles {
|
||||
app, err := GetApp(appFiles, name)
|
||||
if err != nil {
|
||||
@ -174,14 +186,17 @@ func GetApps(appFiles AppFiles) ([]App, error) {
|
||||
}
|
||||
apps = append(apps, app)
|
||||
}
|
||||
|
||||
return apps, nil
|
||||
}
|
||||
|
||||
// GetAppNames retrieves a list of app names.
|
||||
func GetAppNames() ([]string, error) {
|
||||
appFiles, err := LoadAppFiles("")
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
apps, err := GetApps(appFiles)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
@ -213,6 +228,8 @@ func CopyAppEnvSample(appType, appName, server string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Debugf("copied '%s' to '%s'", envSamplePath, appEnvPath)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -240,6 +257,8 @@ func GetAppStatuses(appFiles AppFiles) (map[string]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Debugf("retrieved app statuses: '%s'", statuses)
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
@ -261,6 +280,9 @@ func GetAppComposeFiles(recipe string, appEnv AppEnv) ([]string, error) {
|
||||
path := fmt.Sprintf("%s/%s/%s", APPS_DIR, recipe, file)
|
||||
composeFiles = append(composeFiles, path)
|
||||
}
|
||||
|
||||
logrus.Debugf("retrieved '%s' configs for '%s'", strings.Join(composeFiles, ", "), recipe)
|
||||
|
||||
return composeFiles, nil
|
||||
}
|
||||
|
||||
@ -272,5 +294,8 @@ func GetAppComposeConfig(recipe string, opts stack.Deploy, appEnv AppEnv) (*comp
|
||||
if err != nil {
|
||||
return &composetypes.Config{}, err
|
||||
}
|
||||
|
||||
logrus.Debugf("retrieved '%s' for '%s'", compose, recipe)
|
||||
|
||||
return compose, nil
|
||||
}
|
||||
|
@ -20,8 +20,10 @@ var APPS_JSON = path.Join(ABRA_DIR, "apps.json")
|
||||
var APPS_DIR = path.Join(ABRA_DIR, "apps")
|
||||
var REPOS_BASE_URL = "https://git.coopcloud.tech/coop-cloud"
|
||||
|
||||
// GetServers retrieves all servers.
|
||||
func (a AppFiles) GetServers() []string {
|
||||
var unique []string
|
||||
|
||||
servers := make(map[string]struct{})
|
||||
for _, appFile := range a {
|
||||
if _, ok := servers[appFile.Server]; !ok {
|
||||
@ -29,33 +31,48 @@ func (a AppFiles) GetServers() []string {
|
||||
unique = append(unique, appFile.Server)
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Debugf("retrieved servers: '%s'", unique)
|
||||
|
||||
return unique
|
||||
}
|
||||
|
||||
// ReadEnv loads an app envivornment into a map.
|
||||
func ReadEnv(filePath string) (AppEnv, error) {
|
||||
var envFile AppEnv
|
||||
|
||||
envFile, err := godotenv.Read(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logrus.Debugf("read '%s' from '%s'", envFile, filePath)
|
||||
|
||||
return envFile, nil
|
||||
}
|
||||
|
||||
// ReadServerNames retrieves all server names.
|
||||
func ReadServerNames() ([]string, error) {
|
||||
serverNames, err := getAllFoldersInDirectory(ABRA_SERVER_FOLDER)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logrus.Debugf("read '%s' from '%s'", strings.Join(serverNames, ","), ABRA_SERVER_FOLDER)
|
||||
|
||||
return serverNames, nil
|
||||
}
|
||||
|
||||
// getAllFilesInDirectory returns filenames of all files in directory
|
||||
func getAllFilesInDirectory(directory string) ([]fs.FileInfo, error) {
|
||||
var realFiles []fs.FileInfo
|
||||
|
||||
files, err := ioutil.ReadDir(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
// Follow any symlinks
|
||||
filePath := path.Join(directory, file.Name())
|
||||
@ -71,14 +88,15 @@ func getAllFilesInDirectory(directory string) ([]fs.FileInfo, error) {
|
||||
realFiles = append(realFiles, file)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return realFiles, nil
|
||||
}
|
||||
|
||||
// getAllFoldersInDirectory returns both folder and symlink paths
|
||||
func getAllFoldersInDirectory(directory string) ([]string, error) {
|
||||
var folders []string
|
||||
|
||||
files, err := ioutil.ReadDir(directory)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -86,6 +104,7 @@ func getAllFoldersInDirectory(directory string) ([]string, error) {
|
||||
if len(files) == 0 {
|
||||
return nil, fmt.Errorf("directory is empty: '%s'", directory)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
// Check if file is directory or symlink
|
||||
if file.IsDir() || file.Mode()&fs.ModeSymlink != 0 {
|
||||
@ -99,12 +118,14 @@ func getAllFoldersInDirectory(directory string) ([]string, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return folders, nil
|
||||
}
|
||||
|
||||
// EnsureAbraDirExists checks for the abra config folder and throws error if not
|
||||
func EnsureAbraDirExists() error {
|
||||
if _, err := os.Stat(ABRA_DIR); os.IsNotExist(err) {
|
||||
logrus.Debugf("'%s' does not exist, creating it", ABRA_DIR)
|
||||
if err := os.Mkdir(ABRA_DIR, 0777); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -112,6 +133,7 @@ func EnsureAbraDirExists() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadAbraShEnvVars reads env vars from an abra.sh recipe file.
|
||||
func ReadAbraShEnvVars(abraSh string) (map[string]string, error) {
|
||||
envVars := make(map[string]string)
|
||||
|
||||
@ -137,5 +159,7 @@ func ReadAbraShEnvVars(abraSh string) (map[string]string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Debugf("read '%s' from '%s'", envVars, abraSh)
|
||||
|
||||
return envVars, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user