Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Refactor config command to prevent double-argv parsing (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn authored Jan 17, 2019
1 parent 96acb75 commit 28a9fc6
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 33 deletions.
54 changes: 40 additions & 14 deletions apps/ewallet/lib/ewallet/release_tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@ defmodule EWallet.ReleaseTasks do
end
end

defp give_up do
IO.puts("Error: unknown error occured in release tasks. This is probably a bug.")
IO.puts("Please file a bug report at https://github.com/omisego/ewallet/issues/new")
:init.stop(1)
end

#
# Seed
#

@seed_start_apps [:crypto, :ssl, :postgrex, :ecto, :cloak, :ewallet]
@seed_std_spec [{:ewallet_config, :seeds_settings}, {:ewallet_db, :seeds}]
@seed_e2e_spec [{:ewallet_config, :seeds_settings}, {:ewallet_db, :seeds_test}]
@seed_sample_spec [
{:ewallet_config, :seeds_settings},
{:ewallet_db, :seeds},
{:ewallet_db, :seeds_sample}
]

def seed, do: seed_with(@seed_std_spec)
def seed_e2e, do: seed_with(@seed_e2e_spec)
def seed_sample, do: seed_with(@seed_sample_spec)

defp seed_with(spec) do
Enum.each(@seed_start_apps, &Application.ensure_all_started/1)
Expand Down Expand Up @@ -101,37 +113,51 @@ defmodule EWallet.ReleaseTasks do
@config_start_apps [:crypto, :ssl, :postgrex, :ecto, :cloak, :ewallet]
@config_apps [:activity_logger, :ewallet_config]

def config do
def config_base64 do
case :init.get_plain_arguments() do
[key, value] ->
Enum.each(@config_start_apps, &Application.ensure_all_started/1)
Enum.each(@config_apps, &ensure_app_started/1)
config_update(key, value)
config_base64(key, value)

_ ->
IO.puts("Usage: bin/ewallet config KEY VALUE")
:init.stop(1)
give_up()
end
end

def config_update(key, value) when not is_binary(key), do: config_update(to_string(key), value)
defp config_base64(k, v) when is_list(k) do
case Base.decode64(to_string(k)) do
{:ok, key} ->
config_base64(key, v)

_ ->
give_up()
end
end

def config_update(key, value) when not is_binary(value),
do: config_update(key, to_string(value))
defp config_base64(k, v) when is_list(v) do
case Base.decode64(to_string(v)) do
{:ok, value} ->
config_base64(k, value)

_ ->
give_up()
end
end

defp config_base64(key, value) do
Enum.each(@config_start_apps, &Application.ensure_all_started/1)
Enum.each(@config_apps, &ensure_app_started/1)

def config_update(key, value) do
case Config.update(%{key => value, originator: %System{}}) do
{:ok, [{key, {:ok, _}}]} ->
IO.puts("Successfully updated #{key} to #{value}")
IO.puts("Successfully updated \"#{key}\" to \"#{value}\"")
:init.stop()

{:ok, [{key, {:error, :setting_not_found}}]} ->
IO.puts("Error: #{key} is not a valid settings")
IO.puts("Error: \"#{key}\" is not a valid settings")
:init.stop(1)

_ ->
IO.puts("Error: unknown error")
:init.stop(1)
give_up()
end
end
end
24 changes: 15 additions & 9 deletions docker-gen.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#!/bin/sh

OPTS=hdi:n:p:k:K:f:
ARGS=$(getopt $OPTS "$*" 2>/dev/null)

print_usage() {
printf "Usage: %s [-%s]\\n" "$0" "$OPTS"
printf "Usage: %s [CONFIG..] [OPTS]\\n" "$0"
printf "\\n"
printf "Generates a Docker-Compose configuration overrides for various\\n"
printf "purposes. This script will output to STDOUT, it is expected that\\n"
printf "a user will pipe its output into a file. For example:\\n"
printf "\\n"
printf " %s > docker-compose.override.yml\\n" "$0"
printf "\\n"
printf " -h Print this help.\\n"
printf " -d Generate a development override.\\n"
printf "OPTS:\\n"
printf "\\n"
printf "Config:\\n"
printf " -h Prints this help.\\n"
printf " -d Generates a development override.\\n"
printf "\\n"
printf "CONFIG:\\n"
printf "\\n"
printf " -i image Specify an alternative eWallet image name.\\n"
printf " -n network Specify an external network.\\n"
Expand All @@ -20,14 +25,15 @@ print_usage() {
printf "\\n"
}

ARGS=$(getopt -s sh hdi:n:p:k:K:f: "$@" 2>/dev/null)

# shellcheck disable=SC2181
if [ $? != 0 ]; then
print_usage
exit 1
fi

# shellcheck disable=SC2086
set -- $ARGS
eval set -- "$ARGS"

IMAGE_NAME=""
POSTGRES_PASSWORD=""
Expand Down
49 changes: 47 additions & 2 deletions rel/commands/config.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
#!/bin/sh

# shellcheck disable=SC2068
"$RELEASE_ROOT_DIR/bin/ewallet" command Elixir.EWallet.ReleaseTasks config $@
print_usage() {
printf "Usage: bin/ewallet config [OPTS] KEY VALUE\\n"
printf "\\n"
printf "Update the eWallet configuration in the database.\\n"
printf "\\n"
printf "This command uses POSIX sh shell escaping to handle argument\\n"
printf "splitting, this means you MUST escape quotes if it was intended\\n"
printf "as part of a configuration value. For example:\\n"
printf "\\n"
printf " bin/ewallet config example \"{\\\"key\\\": \\\"value\\\"}\"\\n"
printf "\\n"
printf "Alternatively, use a single quote:\\n"
printf "\\n"
printf " bin/ewallet config example '{\"key\": \"value\"}'\\n"
printf "\\n"
printf "OPTS:\\n"
printf "\\n"
printf " -h Prints this help.\\n"
printf "\\n"
}

ARGS=$(getopt -s sh h "$@" 2>/dev/null)

# shellcheck disable=SC2181
if [ $? != 0 ]; then
print_usage
exit 1
fi

eval set -- "$ARGS"

while true; do
case "$1" in
-h ) print_usage; exit 2;;
-- ) shift; break;;
* ) break;;
esac
done

# We don't want to deal with argument parsing again in Elixir. No, just no.
# Let's sidestep all issues by passing the already-parsed value as Base64
# and let the release task decode it.

KEY="$(printf "%s" "$1" | base64)"; shift
VALUE="$(printf "%s" "$1" | base64)"; shift

exec "$RELEASE_ROOT_DIR/bin/ewallet" command Elixir.EWallet.ReleaseTasks config_base64 "$KEY" "$VALUE"
30 changes: 29 additions & 1 deletion rel/commands/initdb.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
#!/bin/sh

"$RELEASE_ROOT_DIR/bin/ewallet" command Elixir.EWallet.ReleaseTasks initdb
print_usage() {
printf "Usage: bin/ewallet initdb [OPTS]\\n"
printf "\\n"
printf "Create and upgrade the database.\\n"
printf "\\n"
printf "OPTS:\\n"
printf "\\n"
printf " -h Prints this help.\\n"
printf "\\n"
}

ARGS=$(getopt -s sh h "$@" 2>/dev/null)

# shellcheck disable=SC2181
if [ $? != 0 ]; then
print_usage
exit 1
fi

eval set -- "$ARGS"

while true; do
case "$1" in
-h ) print_usage; exit 2;;
* ) break;;
esac
done

exec "$RELEASE_ROOT_DIR/bin/ewallet" command Elixir.EWallet.ReleaseTasks initdb
42 changes: 36 additions & 6 deletions rel/commands/seed.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
#!/bin/sh

seed_spec=seed
print_usage() {
printf "Usage: bin/ewallet seed [OPTS]\\n"
printf "\\n"
printf "Seeds the database with initial data for production, testing\\n"
printf "and evaluation purpose. This command does not attempt to\\n"
printf "detect whether the database has already been seeded or not and\\n"
printf "is expected to be run only once after running initdb.\\n"
printf "\\n"
printf "OPTS:\\n"
printf "\\n"
printf " -h Prints this help.\\n"
printf " -e --e2e Seeds the end-to-end testing data.\\n"
printf " -s --sample Seeds the sample data.\\n"
printf "\\n"
}

while [ "$#" -gt 0 ]; do case $1 in
-e|--e2e) seed_spec=seed_e2e;;
*) echo "$0: illegal option -- $1"; exit 1;;
esac; shift; done
ARGS=$(getopt -s sh -l e2e -l sample hes "$@" 2>/dev/null)

"$RELEASE_ROOT_DIR/bin/ewallet" command Elixir.EWallet.ReleaseTasks "${seed_spec}"
# shellcheck disable=SC2181
if [ $? != 0 ]; then
print_usage
exit 1
fi

eval set -- "$ARGS"

SEED_SPEC=seed

while true; do
case "$1" in
-e | --e2e ) SEED_SPEC=seed_e2e; shift;;
-s | --sample ) SEED_SPEC=seed_sample; shift;;
-h ) print_usage; exit 2;;
* ) break;;
esac
done

exec "$RELEASE_ROOT_DIR/bin/ewallet" command Elixir.EWallet.ReleaseTasks "$SEED_SPEC"
1 change: 1 addition & 0 deletions rootfs/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ s6-env REPLACE_OS_VARS=yes
##
ifelse { test $1 == "initdb" } { /app/bin/ewallet $@ }
ifelse { test $1 == "seed" } { /app/bin/ewallet $@ }
ifelse { test $1 == "config" } { /app/bin/ewallet $@ }

ifelse { test $1 == "foreground" } {
importas -D true SERVE_ENDPOINTS SERVE_ENDPOINTS
Expand Down
4 changes: 3 additions & 1 deletion update-versions.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/sh

BASE_DIR=$(cd "$(dirname "$0")" || exit; pwd -P)
FILE_LIST="/tmp/update_version_files"

if [ -z "$1" ]; then
printf "Usage: %s NEW_VERSION\\n\\n" "$0"
printf "Usage: %s NEW_VERSION\\n" "$0"
printf "\\n"
printf "Update version string in mix.exs and config.exs with NEW_VERSION\\n"
printf "by scanning lines with version: string. This script will try its\\n"
printf "best to retain the indention of the original string.\\n"
Expand Down

0 comments on commit 28a9fc6

Please sign in to comment.