From 86a433479646007c21b19b9d468a0401241f227b Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Sun, 11 Sep 2022 17:00:34 +0200 Subject: [PATCH] Remove embed package This commit finally removes the embed package in favor of more specific resource packages --- cmd/blind_update.go | 2 +- cmd/bootstrap.go | 23 +++--- cmd/cron.go | 2 +- cmd/system_update.go | 4 +- .../triplestore/create-repo.ttl | 0 component/triplestore/database.go | 68 ----------------- component/triplestore/provision.go | 75 +++++++++++++++++++ core/bootstrap.go | 15 ++++ .../templates => core}/bootstrap/env | 0 core/bootstrap/global_authorized_keys | 2 + .../bootstrap/overrides.json | 0 core/runtime.go | 10 +++ .../runtime/utils => core/runtime}/README | 2 +- .../utils => core/runtime}/blind_update.sh | 0 .../utils => core/runtime}/create_admin.sh | 0 .../runtime/utils => core/runtime}/cron.sh | 0 .../runtime}/install_colorbox.sh | 0 .../utils => core/runtime}/patch_easyrdf.sh | 0 .../utils => core/runtime}/patch_triples.sh | 0 .../utils => core/runtime}/use_wisski.sh | 0 .../utils => core/runtime}/wisski_2x_3x.sh | 0 embed/legacy.go | 10 --- embed/resources/.dockerignore | 1 - .../bootstrap/global_authorized_keys | 2 - env/constants.go | 33 -------- env/distillery.go | 68 +++++------------ env/init.go | 62 +++++++++++++++ env/instances.go | 23 ++++-- .../barrel => env/instances/barrel.env | 2 +- .../instances}/barrel/.dockerignore | 0 .../instances}/barrel/.env.sample | 0 .../instances}/barrel/Dockerfile | 0 .../instances}/barrel/conf/ports.conf | 0 .../instances}/barrel/conf/wisski.conf | 0 .../instances}/barrel/conf/wisski.ini | 0 .../instances}/barrel/docker-compose.yml | 2 +- .../instances}/barrel/patch/easyrdf.patch | 0 .../instances}/barrel/patch/triples.patch | 0 .../instances}/barrel/scripts/entrypoint.sh | 0 .../barrel/scripts/provision_container.sh | 0 .../instances}/barrel/scripts/user_shell.sh | 0 .../barrel/wisskiutils/create_adapter.php | 0 .../barrel/wisskiutils/export_pathbuilder.php | 0 .../barrel/wisskiutils/list_uri_prefixes.php | 0 .../barrel/wisskiutils/set_trusted_host.sh | 0 .../barrel/wisskiutils/settings_php_get.sh | 0 .../barrel/wisskiutils/settings_php_set.sh | 0 .../reserve => env/instances/reserve.env | 0 .../instances}/reserve/docker-compose.yml | 0 .../instances}/reserve/index.html | 0 env/runtime.go | 5 -- 51 files changed, 220 insertions(+), 191 deletions(-) rename embed/resources/templates/repository/graphdb-repo.ttl => component/triplestore/create-repo.ttl (100%) create mode 100644 component/triplestore/provision.go create mode 100644 core/bootstrap.go rename {embed/resources/templates => core}/bootstrap/env (100%) create mode 100644 core/bootstrap/global_authorized_keys rename {embed/resources/templates => core}/bootstrap/overrides.json (100%) create mode 100644 core/runtime.go rename {embed/resources/runtime/utils => core/runtime}/README (59%) rename {embed/resources/runtime/utils => core/runtime}/blind_update.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/create_admin.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/cron.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/install_colorbox.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/patch_easyrdf.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/patch_triples.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/use_wisski.sh (100%) rename {embed/resources/runtime/utils => core/runtime}/wisski_2x_3x.sh (100%) delete mode 100644 embed/legacy.go delete mode 100644 embed/resources/.dockerignore delete mode 100644 embed/resources/templates/bootstrap/global_authorized_keys delete mode 100644 env/constants.go create mode 100644 env/init.go rename embed/resources/templates/docker-env/barrel => env/instances/barrel.env (88%) rename {embed/resources/compose => env/instances}/barrel/.dockerignore (100%) rename {embed/resources/compose => env/instances}/barrel/.env.sample (100%) rename {embed/resources/compose => env/instances}/barrel/Dockerfile (100%) rename {embed/resources/compose => env/instances}/barrel/conf/ports.conf (100%) rename {embed/resources/compose => env/instances}/barrel/conf/wisski.conf (100%) rename {embed/resources/compose => env/instances}/barrel/conf/wisski.ini (100%) rename {embed/resources/compose => env/instances}/barrel/docker-compose.yml (96%) rename {embed/resources/compose => env/instances}/barrel/patch/easyrdf.patch (100%) rename {embed/resources/compose => env/instances}/barrel/patch/triples.patch (100%) rename {embed/resources/compose => env/instances}/barrel/scripts/entrypoint.sh (100%) rename {embed/resources/compose => env/instances}/barrel/scripts/provision_container.sh (100%) rename {embed/resources/compose => env/instances}/barrel/scripts/user_shell.sh (100%) rename {embed/resources/compose => env/instances}/barrel/wisskiutils/create_adapter.php (100%) rename {embed/resources/compose => env/instances}/barrel/wisskiutils/export_pathbuilder.php (100%) rename {embed/resources/compose => env/instances}/barrel/wisskiutils/list_uri_prefixes.php (100%) rename {embed/resources/compose => env/instances}/barrel/wisskiutils/set_trusted_host.sh (100%) rename {embed/resources/compose => env/instances}/barrel/wisskiutils/settings_php_get.sh (100%) rename {embed/resources/compose => env/instances}/barrel/wisskiutils/settings_php_set.sh (100%) rename embed/resources/templates/docker-env/reserve => env/instances/reserve.env (100%) rename {embed/resources/compose => env/instances}/reserve/docker-compose.yml (100%) rename {embed/resources/compose => env/instances}/reserve/index.html (100%) diff --git a/cmd/blind_update.go b/cmd/blind_update.go index 06dd01c..55d8e48 100644 --- a/cmd/blind_update.go +++ b/cmd/blind_update.go @@ -45,7 +45,7 @@ func (bu blindUpdate) Run(context wisski_distillery.Context) error { } context.EPrintf("Updating instance %s\n", instance.Slug) - code, err := instance.Shell(context.IOStream, "/utils/blind_update.sh") + code, err := instance.Shell(context.IOStream, "/runtime/blind_update.sh") if err != nil { return errBlindUpdateFailed.WithMessageF(instance.Slug, execx.ExecCommandError) } diff --git a/cmd/bootstrap.go b/cmd/bootstrap.go index 08574bf..12cbece 100644 --- a/cmd/bootstrap.go +++ b/cmd/bootstrap.go @@ -1,13 +1,13 @@ package cmd import ( + "bytes" "io/fs" "os" "path/filepath" wisski_distillery "github.com/FAU-CDI/wisski-distillery" "github.com/FAU-CDI/wisski-distillery/core" - "github.com/FAU-CDI/wisski-distillery/embed" cfg "github.com/FAU-CDI/wisski-distillery/internal/config" "github.com/FAU-CDI/wisski-distillery/internal/fsx" "github.com/FAU-CDI/wisski-distillery/internal/hostname" @@ -124,8 +124,12 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error { return errBootstrapWriteConfig.WithMessageF(err) } - if err := unpack.InstallTemplate( - envPath, + env, err := os.Create(envPath) + if err != nil { + return errBootstrapWriteConfig.WithMessageF(err) + } + if err := unpack.WriteTemplate( + env, map[string]string{ "DEPLOY_ROOT": root, "DEFAULT_DOMAIN": domain, @@ -138,8 +142,7 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error { "MYSQL_ADMIN_USER": "admin", "MYSQL_ADMIN_PASSWORD": password[64:], }, - filepath.Join("resources", "templates", "bootstrap", "env"), - embed.ResourceEmbed, + bytes.NewReader(core.ConfigFileTemplate), ); err != nil { return errBootstrapWriteConfig.WithMessageF(err) } @@ -152,17 +155,19 @@ func (bs bootstrap) Run(context wisski_distillery.Context) error { if err := logging.LogOperation(func() error { context.Println(overridesPath) - if err := unpack.InstallFile( + if err := os.WriteFile( overridesPath, - fsx.OpenFS(filepath.Join("resources", "templates", "bootstrap", "overrides.json"), embed.ResourceEmbed), + core.DefaultOverridesJSON, + fs.ModePerm, ); err != nil { return errBootstrapCreateFile.WithMessageF(err) } context.Println(authorizedKeysFile) - if err := unpack.InstallFile( + if err := os.WriteFile( authorizedKeysFile, - fsx.OpenFS(filepath.Join("resources", "templates", "bootstrap", "global_authorized_keys"), embed.ResourceEmbed), + core.DefaultAuthorizedKeys, + fs.ModePerm, ); err != nil { return errBootstrapCreateFile.WithMessageF(err) } diff --git a/cmd/cron.go b/cmd/cron.go index 9747d9a..06421e1 100644 --- a/cmd/cron.go +++ b/cmd/cron.go @@ -40,7 +40,7 @@ func (cr cron) Run(context wisski_distillery.Context) error { // iterate over the instances and store the last value of error for _, instance := range instances { logging.LogOperation(func() error { - code, err := instance.Shell(context.IOStream, "/utils/cron.sh") + code, err := instance.Shell(context.IOStream, "/runtime/cron.sh") if err != nil { context.EPrintln(err) } diff --git a/cmd/system_update.go b/cmd/system_update.go index 149ddbd..6609e21 100644 --- a/cmd/system_update.go +++ b/cmd/system_update.go @@ -2,12 +2,10 @@ package cmd import ( "os" - "path/filepath" wisski_distillery "github.com/FAU-CDI/wisski-distillery" "github.com/FAU-CDI/wisski-distillery/component" "github.com/FAU-CDI/wisski-distillery/core" - "github.com/FAU-CDI/wisski-distillery/embed" "github.com/FAU-CDI/wisski-distillery/internal/execx" "github.com/FAU-CDI/wisski-distillery/internal/logging" "github.com/FAU-CDI/wisski-distillery/internal/unpack" @@ -144,7 +142,7 @@ func (si systemupdate) Run(context wisski_distillery.Context) error { } if err := logging.LogOperation(func() error { - return unpack.InstallResource(dis.RuntimeDir(), filepath.Join("resources", "runtime"), embed.ResourceEmbed, func(dst, src string) { + return unpack.InstallResource(dis.RuntimeDir(), "runtime", core.Runtime, func(dst, src string) { context.Printf("[copy] %s\n", dst) }) }, context.IOStream, "Unpacking Runtime Components"); err != nil { diff --git a/embed/resources/templates/repository/graphdb-repo.ttl b/component/triplestore/create-repo.ttl similarity index 100% rename from embed/resources/templates/repository/graphdb-repo.ttl rename to component/triplestore/create-repo.ttl diff --git a/component/triplestore/database.go b/component/triplestore/database.go index 54a3318..1c61bbb 100644 --- a/component/triplestore/database.go +++ b/component/triplestore/database.go @@ -11,13 +11,9 @@ import ( "os" "path/filepath" - "github.com/FAU-CDI/wisski-distillery/embed" - "github.com/FAU-CDI/wisski-distillery/internal/fsx" "github.com/FAU-CDI/wisski-distillery/internal/logging" - "github.com/FAU-CDI/wisski-distillery/internal/unpack" "github.com/FAU-CDI/wisski-distillery/internal/wait" "github.com/pkg/errors" - "github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/stream" ) @@ -99,70 +95,6 @@ func (ts Triplestore) Wait() error { }, ts.PollInterval, ts.PollContext) } -var errTripleStoreFailedRepository = exit.Error{ - Message: "Failed to create repository: %s", - ExitCode: exit.ExitGeneric, -} - -func (ts Triplestore) Provision(name, domain, user, password string) error { - if err := ts.Wait(); err != nil { - return err - } - - // prepare the create repo request - // TODO: Move this into a seperate file - createRepo, _, err := unpack.UnpackTemplate( - map[string]string{ - "GRAPHDB_REPO": name, - "INSTANCE_DOMAIN": domain, - }, - fsx.OpenFS(filepath.Join("resources", "templates", "repository", "graphdb-repo.ttl"), embed.ResourceEmbed), - ) - if err != nil { - return err - } - - // do the create! - { - res, err := ts.OpenRaw("POST", "/rest/repositories", createRepo, "config", "") - if err != nil { - return errTripleStoreFailedRepository.WithMessageF(err) - } - defer res.Body.Close() - if res.StatusCode != http.StatusCreated { - return errTripleStoreFailedRepository.WithMessageF("Repo create did not return status code 201") - } - } - - // create the user and grant them access - { - res, err := ts.OpenRaw("POST", "/rest/security/users/"+user, TriplestoreUserPayload{ - Password: password, - AppSettings: TriplestoreUserAppSettings{ - DefaultInference: true, - DefaultVisGraphSchema: true, - DefaultSameas: true, - IgnoreSharedQueries: false, - ExecuteCount: true, - }, - GrantedAuthorities: []string{ - "ROLE_USER", - "READ_REPO_" + name, - "WRITE_REPO_" + name, - }, - }, "", "") - if err != nil { - return errTripleStoreFailedRepository.WithMessageF(err) - } - defer res.Body.Close() - if res.StatusCode != http.StatusCreated { - return errTripleStoreFailedRepository.WithMessageF("User create did not return status code 201") - } - } - - return nil -} - // TriplestorePurgeUser deletes the specified user from the triplestore func (ts Triplestore) PurgeUser(user string) error { res, err := ts.OpenRaw("DELETE", "/rest/security/users/"+user, nil, "", "") diff --git a/component/triplestore/provision.go b/component/triplestore/provision.go new file mode 100644 index 0000000..38f1a11 --- /dev/null +++ b/component/triplestore/provision.go @@ -0,0 +1,75 @@ +package triplestore + +import ( + "bytes" + "net/http" + + _ "embed" + + "github.com/FAU-CDI/wisski-distillery/internal/unpack" + "github.com/tkw1536/goprogram/exit" +) + +var errTripleStoreFailedRepository = exit.Error{ + Message: "Failed to create repository: %s", + ExitCode: exit.ExitGeneric, +} + +//go:embed create-repo.ttl +var createRepoTTL []byte + +func (ts Triplestore) Provision(name, domain, user, password string) error { + if err := ts.Wait(); err != nil { + return err + } + + // prepare the create repo request + var createRepo bytes.Buffer + err := unpack.WriteTemplate(&createRepo, map[string]string{ + "GRAPHDB_REPO": name, + "INSTANCE_DOMAIN": domain, + }, bytes.NewReader(createRepoTTL)) + if err != nil { + return err + } + + // do the create! + { + res, err := ts.OpenRaw("POST", "/rest/repositories", createRepo.Bytes(), "config", "") + if err != nil { + return errTripleStoreFailedRepository.WithMessageF(err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusCreated { + return errTripleStoreFailedRepository.WithMessageF("Repo create did not return status code 201") + } + } + + // create the user and grant them access + { + res, err := ts.OpenRaw("POST", "/rest/security/users/"+user, TriplestoreUserPayload{ + Password: password, + AppSettings: TriplestoreUserAppSettings{ + DefaultInference: true, + DefaultVisGraphSchema: true, + DefaultSameas: true, + IgnoreSharedQueries: false, + ExecuteCount: true, + }, + GrantedAuthorities: []string{ + "ROLE_USER", + "READ_REPO_" + name, + "WRITE_REPO_" + name, + }, + }, "", "") + if err != nil { + return errTripleStoreFailedRepository.WithMessageF(err) + } + defer res.Body.Close() + if res.StatusCode != http.StatusCreated { + return errTripleStoreFailedRepository.WithMessageF("User create did not return status code 201") + } + } + + return nil +} diff --git a/core/bootstrap.go b/core/bootstrap.go new file mode 100644 index 0000000..a3519d8 --- /dev/null +++ b/core/bootstrap.go @@ -0,0 +1,15 @@ +package core + +import _ "embed" + +// DefaultOverridesJSON contains a template for a new 'overrides.json' file +//go:embed bootstrap/overrides.json +var DefaultOverridesJSON []byte + +// DefaultAuthorizedKeys contains a template for a new 'global_authorized_keys' file +//go:embed bootstrap/global_authorized_keys +var DefaultAuthorizedKeys []byte + +// ConfigFileTemplate contains a template for a new configuration file +//go:embed bootstrap/env +var ConfigFileTemplate []byte diff --git a/embed/resources/templates/bootstrap/env b/core/bootstrap/env similarity index 100% rename from embed/resources/templates/bootstrap/env rename to core/bootstrap/env diff --git a/core/bootstrap/global_authorized_keys b/core/bootstrap/global_authorized_keys new file mode 100644 index 0000000..8e6cb01 --- /dev/null +++ b/core/bootstrap/global_authorized_keys @@ -0,0 +1,2 @@ +# This file contains authorized_keys files valid for every repository in the distillery +# The syntax of this file is easy, one key per line, empty lines or those starting with '#' are ignored diff --git a/embed/resources/templates/bootstrap/overrides.json b/core/bootstrap/overrides.json similarity index 100% rename from embed/resources/templates/bootstrap/overrides.json rename to core/bootstrap/overrides.json diff --git a/core/runtime.go b/core/runtime.go new file mode 100644 index 0000000..c38de9d --- /dev/null +++ b/core/runtime.go @@ -0,0 +1,10 @@ +package core + +import ( + "embed" + _ "embed" +) + +// Runtime contains runtime resources to be installed into any instance +//go:embed all:runtime +var Runtime embed.FS diff --git a/embed/resources/runtime/utils/README b/core/runtime/README similarity index 59% rename from embed/resources/runtime/utils/README rename to core/runtime/README index 8613aba..bce620a 100644 --- a/embed/resources/runtime/utils/README +++ b/core/runtime/README @@ -1,2 +1,2 @@ Files in this folder are utility scripts to be used from within individual WissKI instances. -They are mounted under /utils/ and should be used with care. +They are mounted under runtime/ and should be used with care. diff --git a/embed/resources/runtime/utils/blind_update.sh b/core/runtime/blind_update.sh similarity index 100% rename from embed/resources/runtime/utils/blind_update.sh rename to core/runtime/blind_update.sh diff --git a/embed/resources/runtime/utils/create_admin.sh b/core/runtime/create_admin.sh similarity index 100% rename from embed/resources/runtime/utils/create_admin.sh rename to core/runtime/create_admin.sh diff --git a/embed/resources/runtime/utils/cron.sh b/core/runtime/cron.sh similarity index 100% rename from embed/resources/runtime/utils/cron.sh rename to core/runtime/cron.sh diff --git a/embed/resources/runtime/utils/install_colorbox.sh b/core/runtime/install_colorbox.sh similarity index 100% rename from embed/resources/runtime/utils/install_colorbox.sh rename to core/runtime/install_colorbox.sh diff --git a/embed/resources/runtime/utils/patch_easyrdf.sh b/core/runtime/patch_easyrdf.sh similarity index 100% rename from embed/resources/runtime/utils/patch_easyrdf.sh rename to core/runtime/patch_easyrdf.sh diff --git a/embed/resources/runtime/utils/patch_triples.sh b/core/runtime/patch_triples.sh similarity index 100% rename from embed/resources/runtime/utils/patch_triples.sh rename to core/runtime/patch_triples.sh diff --git a/embed/resources/runtime/utils/use_wisski.sh b/core/runtime/use_wisski.sh similarity index 100% rename from embed/resources/runtime/utils/use_wisski.sh rename to core/runtime/use_wisski.sh diff --git a/embed/resources/runtime/utils/wisski_2x_3x.sh b/core/runtime/wisski_2x_3x.sh similarity index 100% rename from embed/resources/runtime/utils/wisski_2x_3x.sh rename to core/runtime/wisski_2x_3x.sh diff --git a/embed/legacy.go b/embed/legacy.go deleted file mode 100644 index 9a93665..0000000 --- a/embed/legacy.go +++ /dev/null @@ -1,10 +0,0 @@ -// Package embed contains embedded resources -package embed - -import ( - "embed" -) - -// ResourceEmbed contains all the resources required by the WissKI-Distillery package. -//go:embed all:resources -var ResourceEmbed embed.FS diff --git a/embed/resources/.dockerignore b/embed/resources/.dockerignore deleted file mode 100644 index adbb97d..0000000 --- a/embed/resources/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -data/ \ No newline at end of file diff --git a/embed/resources/templates/bootstrap/global_authorized_keys b/embed/resources/templates/bootstrap/global_authorized_keys deleted file mode 100644 index 08826ed..0000000 --- a/embed/resources/templates/bootstrap/global_authorized_keys +++ /dev/null @@ -1,2 +0,0 @@ -# This file contains authorized_keys files valid for every repository in the distillery. -# To add a key, add one file per line. diff --git a/env/constants.go b/env/constants.go deleted file mode 100644 index dfe6a41..0000000 --- a/env/constants.go +++ /dev/null @@ -1,33 +0,0 @@ -package env - -import ( - "os" - "path/filepath" - - "github.com/FAU-CDI/wisski-distillery/core" - "github.com/FAU-CDI/wisski-distillery/internal/fsx" -) - -// ExecutablePath returns the path to the executable of this distillery. -func (dis *Distillery) ExecutablePath() string { - return filepath.Join(dis.Config.DeployRoot, core.Executable) -} - -// UsingDistilleryExecutable checks if the current process -func (dis *Distillery) UsingDistilleryExecutable() bool { - exe, err := os.Executable() - if err != nil { - return false - } - return fsx.SameFile(exe, dis.ExecutablePath()) -} - -// CurrentExecutable returns the path to the current executable being used. -// When it does not exist, falls back to the default executable. -func (dis *Distillery) CurrentExecutable() string { - exe, err := os.Executable() - if err != nil || !fsx.IsFile(exe) { - return dis.ExecutablePath() - } - return exe -} diff --git a/env/distillery.go b/env/distillery.go index d7381f9..8fc902e 100644 --- a/env/distillery.go +++ b/env/distillery.go @@ -3,10 +3,11 @@ package env import ( "context" "os" + "path/filepath" "github.com/FAU-CDI/wisski-distillery/core" "github.com/FAU-CDI/wisski-distillery/internal/config" - "github.com/tkw1536/goprogram/exit" + "github.com/FAU-CDI/wisski-distillery/internal/fsx" ) // Distillery represents a running instance for the distillery @@ -26,55 +27,26 @@ func (dis Distillery) Context() context.Context { return context.Background() } -var errNoConfigFile = exit.Error{ - ExitCode: exit.ExitGeneralArguments, - Message: "Configuration File does not exist", +// ExecutablePath returns the path to the executable of this distillery. +func (dis *Distillery) ExecutablePath() string { + return filepath.Join(dis.Config.DeployRoot, core.Executable) } -var errOpenConfig = exit.Error{ - ExitCode: exit.ExitGeneralArguments, - Message: "error loading configuration file: %s", -} - -// NewDistillery creates a new distillery object from a set of parameters and requirements -func NewDistillery(params core.Params, flags core.Flags, req core.Requirements) (env *Distillery, err error) { - env = &Distillery{ - Upstream: Upstream{ - SQL: "127.0.0.1:3306", - Triplestore: "127.0.0.1:7200", - }, - } - - if flags.InternalInDocker { - env.Upstream.SQL = "sql:3306" - env.Upstream.Triplestore = "triplestore:7200" - } - - // if we don't need to load the config, there is nothing to do - if !req.NeedsDistillery { - return - } - - // try to find the configuration file - cfg := flags.ConfigPath // command line flags first - if cfg == "" { - cfg = params.ConfigPath // then globally provided files - } - if cfg == "" { - return nil, errNoConfigFile - } - - // open the config file! - f, err := os.Open(params.ConfigPath) +// UsingDistilleryExecutable checks if the current process +func (dis *Distillery) UsingDistilleryExecutable() bool { + exe, err := os.Executable() if err != nil { - return nil, errOpenConfig.WithMessageF(err) + return false } - defer f.Close() - - // unmarshal the config - env.Config = &config.Config{ - ConfigPath: cfg, - } - err = env.Config.Unmarshal(f) - return + return fsx.SameFile(exe, dis.ExecutablePath()) +} + +// CurrentExecutable returns the path to the current executable being used. +// When it does not exist, falls back to the default executable. +func (dis *Distillery) CurrentExecutable() string { + exe, err := os.Executable() + if err != nil || !fsx.IsFile(exe) { + return dis.ExecutablePath() + } + return exe } diff --git a/env/init.go b/env/init.go new file mode 100644 index 0000000..f87689b --- /dev/null +++ b/env/init.go @@ -0,0 +1,62 @@ +package env + +import ( + "os" + + "github.com/FAU-CDI/wisski-distillery/core" + "github.com/FAU-CDI/wisski-distillery/internal/config" + "github.com/tkw1536/goprogram/exit" +) + +var errNoConfigFile = exit.Error{ + ExitCode: exit.ExitGeneralArguments, + Message: "Configuration File does not exist", +} + +var errOpenConfig = exit.Error{ + ExitCode: exit.ExitGeneralArguments, + Message: "error loading configuration file: %s", +} + +// NewDistillery creates a new distillery object from a set of parameters and requirements +func NewDistillery(params core.Params, flags core.Flags, req core.Requirements) (env *Distillery, err error) { + env = &Distillery{ + Upstream: Upstream{ + SQL: "127.0.0.1:3306", + Triplestore: "127.0.0.1:7200", + }, + } + + if flags.InternalInDocker { + env.Upstream.SQL = "sql:3306" + env.Upstream.Triplestore = "triplestore:7200" + } + + // if we don't need to load the config, there is nothing to do + if !req.NeedsDistillery { + return + } + + // try to find the configuration file + cfg := flags.ConfigPath // command line flags first + if cfg == "" { + cfg = params.ConfigPath // then globally provided files + } + if cfg == "" { + return nil, errNoConfigFile + } + + // open the config file! + f, err := os.Open(params.ConfigPath) + if err != nil { + return nil, errOpenConfig.WithMessageF(err) + } + defer f.Close() + + // unmarshal the config + env.Config = &config.Config{ + ConfigPath: cfg, + } + err = env.Config.Unmarshal(f) + return +} diff --git a/env/instances.go b/env/instances.go index 199b202..f0bb776 100644 --- a/env/instances.go +++ b/env/instances.go @@ -2,6 +2,7 @@ package env import ( "bytes" + "embed" "encoding/json" "fmt" "io" @@ -12,7 +13,6 @@ import ( "strings" "github.com/FAU-CDI/wisski-distillery/component" - "github.com/FAU-CDI/wisski-distillery/embed" "github.com/FAU-CDI/wisski-distillery/internal/bookkeeping" "github.com/FAU-CDI/wisski-distillery/internal/fsx" "github.com/alessio/shellescape" @@ -228,16 +228,20 @@ func (instance Instance) URL() *url.URL { return url } +//go:embed all:instances/barrel instances/barrel.env +var barrelResources embed.FS + // Stack represents a stack representing this instance func (instance Instance) Stack() component.Installable { return component.Installable{ Stack: component.Stack{ Dir: instance.FilesystemBase, }, - Resources: embed.ResourceEmbed, // TODO: Move this over - ContextPath: filepath.Join("resources", "compose", "barrel"), - EnvPath: filepath.Join("resources", "templates", "docker-env", "barrel"), + Resources: barrelResources, + ContextPath: filepath.Join("instances", "barrel"), + EnvPath: filepath.Join("instances", "barrel.env"), + EnvContext: map[string]string{ "DATA_PATH": filepath.Join(instance.FilesystemBase, "data"), @@ -247,7 +251,7 @@ func (instance Instance) Stack() component.Installable { "LETSENCRYPT_HOST": instance.dis.IfHttps(instance.Domain()), "LETSENCRYPT_EMAIL": instance.dis.IfHttps(instance.dis.Config.CertbotEmail), - "UTILS_DIR": instance.dis.RuntimeUtilsDir(), + "RUNTIME_DIR": instance.dis.RuntimeDir(), "GLOBAL_AUTHORIZED_KEYS_FILE": instance.dis.Config.GlobalAuthorizedKeysFile, }, @@ -260,14 +264,19 @@ func (instance Instance) Stack() component.Installable { } } +//go:embed all:instances/reserve instances/reserve.env +var reserveResources embed.FS + func (instance Instance) ReserveStack() component.Installable { return component.Installable{ Stack: component.Stack{ Dir: instance.FilesystemBase, }, - ContextPath: filepath.Join("resources", "compose", "reserve"), - EnvPath: filepath.Join("resources", "templates", "docker-env", "reserve"), + Resources: reserveResources, + ContextPath: filepath.Join("instances", "reserve"), + EnvPath: filepath.Join("instances", "reserve.env"), + EnvContext: map[string]string{ "VIRTUAL_HOST": instance.Domain(), diff --git a/embed/resources/templates/docker-env/barrel b/env/instances/barrel.env similarity index 88% rename from embed/resources/templates/docker-env/barrel rename to env/instances/barrel.env index 89c8c51..9d93502 100644 --- a/embed/resources/templates/docker-env/barrel +++ b/env/instances/barrel.env @@ -1,5 +1,5 @@ DATA_PATH=${DATA_PATH} -UTILS_DIR=${UTILS_DIR} +RUNTIME_DIR=${RUNTIME_DIR} SLUG=${SLUG} VIRTUAL_HOST=${VIRTUAL_HOST} diff --git a/embed/resources/compose/barrel/.dockerignore b/env/instances/barrel/.dockerignore similarity index 100% rename from embed/resources/compose/barrel/.dockerignore rename to env/instances/barrel/.dockerignore diff --git a/embed/resources/compose/barrel/.env.sample b/env/instances/barrel/.env.sample similarity index 100% rename from embed/resources/compose/barrel/.env.sample rename to env/instances/barrel/.env.sample diff --git a/embed/resources/compose/barrel/Dockerfile b/env/instances/barrel/Dockerfile similarity index 100% rename from embed/resources/compose/barrel/Dockerfile rename to env/instances/barrel/Dockerfile diff --git a/embed/resources/compose/barrel/conf/ports.conf b/env/instances/barrel/conf/ports.conf similarity index 100% rename from embed/resources/compose/barrel/conf/ports.conf rename to env/instances/barrel/conf/ports.conf diff --git a/embed/resources/compose/barrel/conf/wisski.conf b/env/instances/barrel/conf/wisski.conf similarity index 100% rename from embed/resources/compose/barrel/conf/wisski.conf rename to env/instances/barrel/conf/wisski.conf diff --git a/embed/resources/compose/barrel/conf/wisski.ini b/env/instances/barrel/conf/wisski.ini similarity index 100% rename from embed/resources/compose/barrel/conf/wisski.ini rename to env/instances/barrel/conf/wisski.ini diff --git a/embed/resources/compose/barrel/docker-compose.yml b/env/instances/barrel/docker-compose.yml similarity index 96% rename from embed/resources/compose/barrel/docker-compose.yml rename to env/instances/barrel/docker-compose.yml index 34ca677..04dddab 100644 --- a/embed/resources/compose/barrel/docker-compose.yml +++ b/env/instances/barrel/docker-compose.yml @@ -25,7 +25,7 @@ services: - ${DATA_PATH}/.composer:/var/www/.composer - ${DATA_PATH}/data:/var/www/data - ${DATA_PATH}/authorized_keys:/var/www/.ssh/authorized_keys - - ${UTILS_DIR}:/utils:ro + - ${RUNTIME_DIR}:/runtime:ro networks: default: diff --git a/embed/resources/compose/barrel/patch/easyrdf.patch b/env/instances/barrel/patch/easyrdf.patch similarity index 100% rename from embed/resources/compose/barrel/patch/easyrdf.patch rename to env/instances/barrel/patch/easyrdf.patch diff --git a/embed/resources/compose/barrel/patch/triples.patch b/env/instances/barrel/patch/triples.patch similarity index 100% rename from embed/resources/compose/barrel/patch/triples.patch rename to env/instances/barrel/patch/triples.patch diff --git a/embed/resources/compose/barrel/scripts/entrypoint.sh b/env/instances/barrel/scripts/entrypoint.sh similarity index 100% rename from embed/resources/compose/barrel/scripts/entrypoint.sh rename to env/instances/barrel/scripts/entrypoint.sh diff --git a/embed/resources/compose/barrel/scripts/provision_container.sh b/env/instances/barrel/scripts/provision_container.sh similarity index 100% rename from embed/resources/compose/barrel/scripts/provision_container.sh rename to env/instances/barrel/scripts/provision_container.sh diff --git a/embed/resources/compose/barrel/scripts/user_shell.sh b/env/instances/barrel/scripts/user_shell.sh similarity index 100% rename from embed/resources/compose/barrel/scripts/user_shell.sh rename to env/instances/barrel/scripts/user_shell.sh diff --git a/embed/resources/compose/barrel/wisskiutils/create_adapter.php b/env/instances/barrel/wisskiutils/create_adapter.php similarity index 100% rename from embed/resources/compose/barrel/wisskiutils/create_adapter.php rename to env/instances/barrel/wisskiutils/create_adapter.php diff --git a/embed/resources/compose/barrel/wisskiutils/export_pathbuilder.php b/env/instances/barrel/wisskiutils/export_pathbuilder.php similarity index 100% rename from embed/resources/compose/barrel/wisskiutils/export_pathbuilder.php rename to env/instances/barrel/wisskiutils/export_pathbuilder.php diff --git a/embed/resources/compose/barrel/wisskiutils/list_uri_prefixes.php b/env/instances/barrel/wisskiutils/list_uri_prefixes.php similarity index 100% rename from embed/resources/compose/barrel/wisskiutils/list_uri_prefixes.php rename to env/instances/barrel/wisskiutils/list_uri_prefixes.php diff --git a/embed/resources/compose/barrel/wisskiutils/set_trusted_host.sh b/env/instances/barrel/wisskiutils/set_trusted_host.sh similarity index 100% rename from embed/resources/compose/barrel/wisskiutils/set_trusted_host.sh rename to env/instances/barrel/wisskiutils/set_trusted_host.sh diff --git a/embed/resources/compose/barrel/wisskiutils/settings_php_get.sh b/env/instances/barrel/wisskiutils/settings_php_get.sh similarity index 100% rename from embed/resources/compose/barrel/wisskiutils/settings_php_get.sh rename to env/instances/barrel/wisskiutils/settings_php_get.sh diff --git a/embed/resources/compose/barrel/wisskiutils/settings_php_set.sh b/env/instances/barrel/wisskiutils/settings_php_set.sh similarity index 100% rename from embed/resources/compose/barrel/wisskiutils/settings_php_set.sh rename to env/instances/barrel/wisskiutils/settings_php_set.sh diff --git a/embed/resources/templates/docker-env/reserve b/env/instances/reserve.env similarity index 100% rename from embed/resources/templates/docker-env/reserve rename to env/instances/reserve.env diff --git a/embed/resources/compose/reserve/docker-compose.yml b/env/instances/reserve/docker-compose.yml similarity index 100% rename from embed/resources/compose/reserve/docker-compose.yml rename to env/instances/reserve/docker-compose.yml diff --git a/embed/resources/compose/reserve/index.html b/env/instances/reserve/index.html similarity index 100% rename from embed/resources/compose/reserve/index.html rename to env/instances/reserve/index.html diff --git a/env/runtime.go b/env/runtime.go index 31c2472..4b784d8 100644 --- a/env/runtime.go +++ b/env/runtime.go @@ -6,8 +6,3 @@ import "path/filepath" func (dis Distillery) RuntimeDir() string { return filepath.Join(dis.Config.DeployRoot, "runtime") } - -// RuntimeUtilsDir returns the path to the runtime utility dir -func (dis Distillery) RuntimeUtilsDir() string { - return filepath.Join(dis.Config.DeployRoot, "runtime", "utils") -}