internal/component => internal/dis/component
This commit is contained in:
parent
9443217441
commit
b5b1ce2340
123 changed files with 76 additions and 76 deletions
65
internal/dis/component/instances/create.go
Normal file
65
internal/dis/component/instances/create.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package instances
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/stringparser"
|
||||
)
|
||||
|
||||
var errInvalidSlug = errors.New("not a valid slug")
|
||||
|
||||
// Create fills the struct for a new WissKI instance.
|
||||
// It validates that slug is a valid name for an instance.
|
||||
//
|
||||
// It does not perform any checks if the instance already exists, or does the creation in the database.
|
||||
func (instances *Instances) Create(slug string) (wissKI *wisski.WissKI, err error) {
|
||||
|
||||
// make sure that the slug is valid!
|
||||
slug, err = stringparser.ParseSlug(instances.Environment, slug)
|
||||
if err != nil {
|
||||
return nil, errInvalidSlug
|
||||
}
|
||||
|
||||
wissKI = new(wisski.WissKI)
|
||||
instances.use(wissKI)
|
||||
|
||||
wissKI.Instance.Slug = slug
|
||||
wissKI.Instance.FilesystemBase = filepath.Join(instances.Path(), wissKI.Domain())
|
||||
|
||||
wissKI.Instance.OwnerEmail = ""
|
||||
wissKI.Instance.AutoBlindUpdateEnabled = true
|
||||
|
||||
// sql
|
||||
|
||||
wissKI.Instance.SqlDatabase = instances.Config.MysqlDatabasePrefix + slug
|
||||
wissKI.Instance.SqlUsername = instances.Config.MysqlUserPrefix + slug
|
||||
|
||||
wissKI.Instance.SqlPassword, err = instances.Config.NewPassword()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// triplestore
|
||||
|
||||
wissKI.Instance.GraphDBRepository = instances.Config.GraphDBRepoPrefix + slug
|
||||
wissKI.Instance.GraphDBUsername = instances.Config.GraphDBUserPrefix + slug
|
||||
|
||||
wissKI.Instance.GraphDBPassword, err = instances.Config.NewPassword()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// drupal
|
||||
|
||||
wissKI.DrupalUsername = "admin" // TODO: Change this!
|
||||
|
||||
wissKI.DrupalPassword, err = instances.Config.NewPassword()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// store the instance in the object and return it!
|
||||
return wissKI, nil
|
||||
}
|
||||
174
internal/dis/component/instances/instances.go
Normal file
174
internal/dis/component/instances/instances.go
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
package instances
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/exporter/logger"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/triplestore"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// Instances manages multiple WissKI Instances.
|
||||
type Instances struct {
|
||||
component.Base
|
||||
|
||||
TS *triplestore.Triplestore
|
||||
SQL *sql.SQL
|
||||
Meta *meta.Meta
|
||||
ExporterLog *logger.Logger
|
||||
}
|
||||
|
||||
func (instances *Instances) Path() string {
|
||||
return filepath.Join(instances.Still.Config.DeployRoot, "instances")
|
||||
}
|
||||
|
||||
// ErrWissKINotFound is returned when a WissKI is not found
|
||||
var ErrWissKINotFound = errors.New("WissKI not found")
|
||||
|
||||
var errSQL = exit.Error{
|
||||
Message: "Unknown SQL Error %s",
|
||||
ExitCode: exit.ExitGeneric,
|
||||
}
|
||||
|
||||
// use uses the non-nil wisski instance with this instances
|
||||
func (instances *Instances) use(wisski *wisski.WissKI) {
|
||||
wisski.Core = instances.Still
|
||||
wisski.SQL = instances.SQL
|
||||
wisski.TS = instances.TS
|
||||
wisski.Meta = instances.Meta
|
||||
wisski.ExporterLog = instances.ExporterLog
|
||||
}
|
||||
|
||||
// WissKI returns the WissKI with the provided slug, if it exists.
|
||||
// It the WissKI does not exist, returns ErrWissKINotFound.
|
||||
func (instances *Instances) WissKI(slug string) (wissKI *wisski.WissKI, err error) {
|
||||
sql := instances.SQL
|
||||
if err := sql.WaitQueryTable(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
table, err := sql.QueryTable(false, models.InstanceTable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create a struct
|
||||
wissKI = new(wisski.WissKI)
|
||||
|
||||
// find the instance by slug
|
||||
query := table.Where(&models.Instance{Slug: slug}).Find(&wissKI.Instance)
|
||||
switch {
|
||||
case query.Error != nil:
|
||||
return nil, errSQL.WithMessageF(query.Error)
|
||||
case query.RowsAffected == 0:
|
||||
return nil, ErrWissKINotFound
|
||||
}
|
||||
|
||||
// use the wissKI instance
|
||||
instances.use(wissKI)
|
||||
return wissKI, nil
|
||||
}
|
||||
|
||||
// Instance is a convenience function to return an instance based on a model slug.
|
||||
// When the instance does not exist, returns nil.
|
||||
func (instances *Instances) Instance(instance models.Instance) *wisski.WissKI {
|
||||
wissKI, err := instances.WissKI(instance.Slug)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return wissKI
|
||||
}
|
||||
|
||||
// Has checks if a WissKI with the provided slug exists inside the database.
|
||||
// It does not perform any checks on the WissKI itself.
|
||||
func (instances *Instances) Has(slug string) (ok bool, err error) {
|
||||
sql := instances.SQL
|
||||
if err := sql.WaitQueryTable(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
table, err := sql.QueryTable(false, models.InstanceTable)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
query := table.Select("count(*) > 0").Where("slug = ?", slug).Find(&ok)
|
||||
if query.Error != nil {
|
||||
return false, errSQL.WithMessageF(query.Error)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// All returns all instances of the WissKI Distillery in consistent order.
|
||||
//
|
||||
// There is no guarantee that this order remains identical between different api releases; however subsequent invocations are guaranteed to return the same order.
|
||||
func (instances *Instances) All() ([]*wisski.WissKI, error) {
|
||||
return instances.find(true, func(table *gorm.DB) *gorm.DB {
|
||||
return table
|
||||
})
|
||||
}
|
||||
|
||||
// WissKIs returns the WissKI instances with the provides slugs.
|
||||
// If a slug does not exist, it is omitted from the result.
|
||||
func (instances *Instances) WissKIs(slugs ...string) ([]*wisski.WissKI, error) {
|
||||
return instances.find(true, func(table *gorm.DB) *gorm.DB {
|
||||
return table.Where("slug IN ?", slugs)
|
||||
})
|
||||
}
|
||||
|
||||
// Load is like All, except that when no slugs are provided, it calls All.
|
||||
func (instances *Instances) Load(slugs ...string) ([]*wisski.WissKI, error) {
|
||||
if len(slugs) == 0 {
|
||||
return instances.All()
|
||||
}
|
||||
return instances.WissKIs(slugs...)
|
||||
}
|
||||
|
||||
// find finds instances based on the provided query
|
||||
func (instances *Instances) find(order bool, query func(table *gorm.DB) *gorm.DB) (results []*wisski.WissKI, err error) {
|
||||
sql := instances.SQL
|
||||
if err := sql.WaitQueryTable(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// open the bookkeeping table
|
||||
table, err := sql.QueryTable(false, models.InstanceTable)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// prepare a query
|
||||
find := table
|
||||
if order {
|
||||
find = find.Order(clause.OrderByColumn{Column: clause.Column{Name: "slug"}, Desc: false})
|
||||
}
|
||||
if query != nil {
|
||||
find = query(find)
|
||||
}
|
||||
|
||||
// fetch bookkeeping instances
|
||||
var bks []models.Instance
|
||||
find = find.Find(&bks)
|
||||
if find.Error != nil {
|
||||
return nil, errSQL.WithMessageF(find.Error)
|
||||
}
|
||||
|
||||
// make proper instances
|
||||
results = make([]*wisski.WissKI, len(bks))
|
||||
for i, bk := range bks {
|
||||
results[i] = new(wisski.WissKI)
|
||||
results[i].Instance = bk
|
||||
instances.use(results[i])
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
30
internal/dis/component/instances/runtime.go
Normal file
30
internal/dis/component/instances/runtime.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package instances
|
||||
|
||||
import (
|
||||
"embed"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
var errBootstrapFailedRuntime = exit.Error{
|
||||
Message: "failed to update runtime",
|
||||
ExitCode: exit.ExitGeneric,
|
||||
}
|
||||
|
||||
// Runtime contains runtime resources to be installed into any instance
|
||||
//
|
||||
//go:embed all:runtime
|
||||
var runtimeResources embed.FS
|
||||
|
||||
// Update installs or updates runtime components needed by this component.
|
||||
func (instances *Instances) Update(stream stream.IOStream) error {
|
||||
err := unpack.InstallDir(instances.Still.Environment, instances.Config.RuntimeDir(), "runtime", runtimeResources, func(dst, src string) {
|
||||
stream.Printf("[copy] %s\n", dst)
|
||||
})
|
||||
if err != nil {
|
||||
return errBootstrapFailedRuntime.Wrap(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
2
internal/dis/component/instances/runtime/README
Normal file
2
internal/dis/component/instances/runtime/README
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Files in this folder are utility scripts to be used from within individual WissKI instances.
|
||||
They are mounted under runtime/ and should be used with care.
|
||||
16
internal/dis/component/instances/runtime/blind_update.sh
Normal file
16
internal/dis/component/instances/runtime/blind_update.sh
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#!/bin/bash
|
||||
|
||||
# This utility script can be used to blindly update all dependencies to their latest versions.
|
||||
# It does not perform any checking whatsoever.
|
||||
|
||||
# update the main modules
|
||||
cd /var/www/data/project || exit 1
|
||||
chmod u+rw web/sites/default/
|
||||
composer update
|
||||
|
||||
# update the db
|
||||
drush -y updatedb
|
||||
|
||||
# update the wisski dependencies
|
||||
cd /var/www/data/project/web/modules/contrib/wisski || exit 1
|
||||
composer update
|
||||
25
internal/dis/component/instances/runtime/create_admin.sh
Normal file
25
internal/dis/component/instances/runtime/create_admin.sh
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# read user
|
||||
USER=$1
|
||||
if [ -z "$USER" ]; then
|
||||
echo "Usage: create_admin.sh USERNAME"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# read password
|
||||
echo "Enter Password for $USER:"
|
||||
read -s PASS
|
||||
echo "Enter the same password again:"
|
||||
read -s PASS2
|
||||
|
||||
if [ "$PASS" != "$PASS2" ]; then
|
||||
echo "Passwords not equal"
|
||||
exit 1
|
||||
fi;
|
||||
|
||||
# create the user and add the admin role
|
||||
cd /var/www/data/project/
|
||||
drush user:create "$USER" --password="$PASS"
|
||||
drush user-add-role administrator "$USER"
|
||||
8
internal/dis/component/instances/runtime/cron.sh
Executable file
8
internal/dis/component/instances/runtime/cron.sh
Executable file
|
|
@ -0,0 +1,8 @@
|
|||
#!/bin/bash
|
||||
|
||||
# This utility script can be used to run all cron tasks.
|
||||
|
||||
cd /var/www/data/project || exit 1
|
||||
export PATH=/var/www/data/project/vendor/bin:$PATH
|
||||
|
||||
drush core-cron
|
||||
22
internal/dis/component/instances/runtime/install_colorbox.sh
Normal file
22
internal/dis/component/instances/runtime/install_colorbox.sh
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# make a temporary directory and cd into it
|
||||
TEMPDIR=$(mktemp -d)
|
||||
pushd "$TEMPDIR"
|
||||
|
||||
# curl the colorbox zip and unpack it
|
||||
curl -L https://github.com/jackmoore/colorbox/archive/master.zip --output master.zip
|
||||
unzip master.zip
|
||||
|
||||
# make the directory for libraries, and remove the old colorbox installation
|
||||
chmod u+rw /var/www/data/project/web/sites/default/
|
||||
mkdir -p /var/www/data/project/web/sites/default/libraries/
|
||||
rm -rf /var/www/data/project/web/sites/default/libraries/colorbox
|
||||
|
||||
# copy over the new installation
|
||||
mv colorbox-master/ /var/www/data/project/web/sites/default/libraries/colorbox
|
||||
|
||||
# cleanup
|
||||
popd
|
||||
rm -rf "$TEMPDIR"
|
||||
6
internal/dis/component/instances/runtime/patch_easyrdf.sh
Executable file
6
internal/dis/component/instances/runtime/patch_easyrdf.sh
Executable file
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/sh
|
||||
|
||||
# This script can be used to repatch EasyRDF when needed.
|
||||
cd /var/www/data/project/web/modules/contrib/wisski || exit 1
|
||||
EASYRDF_RESPONSE="./vendor/easyrdf/easyrdf/lib/EasyRdf/Http/Response.php"
|
||||
patch -N "$EASYRDF_RESPONSE" < "/patch/easyrdf.patch"
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#!/bin/sh
|
||||
|
||||
# This script can be used to repatch EasyRDF when needed.
|
||||
cd /var/www/data/project/web/modules/contrib/wisski/ || exit 1
|
||||
TRIPLESTABCONTROLLER="./wisski_adapter_sparql11_pb/src/Controller/Sparql11TriplesTabController.php"
|
||||
patch -N "$TRIPLESTABCONTROLLER" < "/patch/triples.patch"
|
||||
22
internal/dis/component/instances/runtime/use_wisski.sh
Normal file
22
internal/dis/component/instances/runtime/use_wisski.sh
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# read user
|
||||
VERSION=$1
|
||||
if [ -z "$VERSION" ]; then
|
||||
echo "Usage: use_wisski.sh VERSION"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# update the main modules
|
||||
cd /var/www/data/project
|
||||
chmod u+rw web/sites/default/
|
||||
composer require "drupal/wisski:$VERSION"
|
||||
|
||||
# update the wisski dependencies
|
||||
pushd /var/www/data/project/web/modules/contrib/wisski
|
||||
composer update
|
||||
popd
|
||||
|
||||
# update the db
|
||||
drush -y updatedb
|
||||
26
internal/dis/component/instances/runtime/wisski_2x_3x.sh
Normal file
26
internal/dis/component/instances/runtime/wisski_2x_3x.sh
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# temporarily extend permissions
|
||||
chmod 777 web/sites/default
|
||||
chmod 666 web/sites/default/*settings.php
|
||||
chmod 666 web/sites/default/*services.yml
|
||||
|
||||
# update the core itself
|
||||
composer require 'drupal/internal/core-recommended:^9' 'drupal/internal/core-composer-scaffold:^9' 'drupal/internal/core-project-message:^9' --update-with-dependencies --no-update
|
||||
composer update
|
||||
composer require 'drupal/wisski'
|
||||
|
||||
# update requirements for wisski!
|
||||
pushd web/modules/contrib/wisski || exit 1
|
||||
composer update
|
||||
popd || exit 1
|
||||
|
||||
# run the update and clear the cache!
|
||||
drush updatedb --yes
|
||||
# drush cc
|
||||
|
||||
# and reset everything back to normal
|
||||
chmod 755 web/sites/default
|
||||
chmod 644 web/sites/default/*settings.php
|
||||
chmod 644 web/sites/default/*services.yml
|
||||
Loading…
Add table
Add a link
Reference in a new issue