-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(meilisearch): Add Meilisearch support
- Loading branch information
Showing
5 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env sh | ||
set -euo pipefail | ||
|
||
curl_api() { | ||
curl -sSfX "${2:-GET}" -H "Authorization: Bearer $MEILI_MASTER_KEY" "$API_HOST/$1" | ||
} | ||
|
||
get_dump_uid() { | ||
curl_api "tasks/$task_uid" | grep -Eo '"dumpUid":"[^,}]+' | cut -d: -f2 | cut -d\" -f2 | ||
} | ||
|
||
echo 'Creating dump' >&2 | ||
if command -v meilitool &>/dev/null; then | ||
meilitool export-a-dump | ||
dump="dumps/$(ls -t1 dumps | head -n1)" | ||
else | ||
task_uid="$(curl_api dumps POST | grep -Eo '"taskUid":[^,}]+' | cut -d: -f2)" | ||
echo 'Waiting for dump to complete' >&2 | ||
while [ -z "${dump_uid:-}" ]; do | ||
dump_uid="$(get_dump_uid || true)" | ||
sleep 1 | ||
done | ||
dump="dumps/$dump_uid.dump" | ||
fi | ||
|
||
echo Downloading dump >&2 | ||
cat "$dump" | ||
echo 'Cleaning up' >&2 | ||
rm -f "$dump" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package meilisearch | ||
|
||
import ( | ||
_ "embed" | ||
"strconv" | ||
|
||
"github.com/clevyr/kubedb/internal/command" | ||
"github.com/clevyr/kubedb/internal/config" | ||
"github.com/clevyr/kubedb/internal/database/sqlformat" | ||
"github.com/clevyr/kubedb/internal/kubernetes" | ||
) | ||
|
||
var ( | ||
_ config.DatabaseDump = Meilisearch{} | ||
_ config.DatabaseRestore = Meilisearch{} | ||
_ config.DatabasePort = Meilisearch{} | ||
_ config.DatabasePassword = Meilisearch{} | ||
_ config.DatabaseDisableJob = Meilisearch{} | ||
) | ||
|
||
type Meilisearch struct{} | ||
|
||
func (Meilisearch) Name() string { | ||
return "meilisearch" | ||
} | ||
|
||
func (Meilisearch) PortEnvNames() kubernetes.ConfigLookups { | ||
return kubernetes.ConfigLookups{} | ||
} | ||
|
||
func (Meilisearch) DefaultPort() uint16 { | ||
return 7700 | ||
} | ||
|
||
func (Meilisearch) PodLabels() []kubernetes.LabelQueryable { | ||
return []kubernetes.LabelQueryable{ | ||
kubernetes.LabelQuery{Name: "app.kubernetes.io/name", Value: "meilisearch"}, | ||
} | ||
} | ||
|
||
func (Meilisearch) PasswordEnvNames(c config.Global) kubernetes.ConfigLookups { | ||
return kubernetes.ConfigLookups{ | ||
kubernetes.LookupEnv{"MEILI_MASTER_KEY"}, | ||
kubernetes.LookupNop{}, | ||
} | ||
} | ||
|
||
//go:embed dump.sh | ||
var dumpScript string | ||
|
||
func (Meilisearch) DumpCommand(conf config.Dump) *command.Builder { | ||
url := "http://" + conf.Host + ":" + strconv.Itoa(int(conf.Port)) | ||
cmd := command.NewBuilder( | ||
command.NewEnv("API_HOST", url), | ||
"sh", "-c", dumpScript, | ||
) | ||
if conf.Password != "" { | ||
cmd.Unshift(command.NewEnv("MEILI_MASTER_KEY", conf.Password)) | ||
} | ||
return cmd | ||
} | ||
|
||
//go:embed restore.sh | ||
var restoreScript string | ||
|
||
func (Meilisearch) RestoreCommand(conf config.Restore, _ sqlformat.Format) *command.Builder { | ||
cmd := command.NewBuilder("sh", "-c", restoreScript) | ||
if conf.Password != "" { | ||
cmd.Unshift(command.NewEnv("MEILI_MASTER_KEY", conf.Password)) | ||
} | ||
return cmd | ||
} | ||
|
||
func (Meilisearch) Formats() map[sqlformat.Format]string { | ||
return map[sqlformat.Format]string{ | ||
sqlformat.Plain: ".dump", | ||
sqlformat.Gzip: ".dump.gz", | ||
} | ||
} | ||
|
||
func (Meilisearch) DisableJob() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env sh | ||
set -euo pipefail | ||
|
||
cleanup_fs() { | ||
echo 'Cleaning up' >&2 | ||
rm -rf data.ms.restore | ||
if [[ -n "$dump" ]]; then | ||
rm -f "$dump" | ||
fi | ||
} | ||
|
||
trap 'cleanup_fs' EXIT | ||
|
||
echo 'Uploading dump' >&2 | ||
now="$(date +%Y%m%d-%H%M%S)" | ||
dump="dumps/$now.dump" | ||
cat > "$dump" | ||
|
||
restore="restore_${now}_data.ms" | ||
printf 'Creating new database "%s" from dump\n' "$restore" >&2 | ||
port="$(( RANDOM + 32767 ))" | ||
meilisearch --import-dump "$dump" --db-path "$restore" --http-addr 127.0.0.1:"$port" & | ||
restore_pid="$!" | ||
while ! nc -z 127.0.0.1 "$port"; do | ||
if ! kill -0 "$restore_pid"; then | ||
exit 1 | ||
fi | ||
sleep 1 | ||
done | ||
echo 'Restore finished' >&2 | ||
|
||
echo 'Moving "data.ms" to "old_data.ms"' >&2 | ||
rm -rf old_data.ms | ||
mv data.ms old_data.ms | ||
printf 'Moving "%s" to "data.ms"\n' "$restore" >&2 | ||
mv "$restore" data.ms | ||
trap - EXIT | ||
cleanup_fs | ||
echo 'Restarting Meilisearch' >&2 | ||
kill 1 |