-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adds download-addrbook.sh script * feat: adds addrbook download * fix(CosmosFullNode): updates CRD with addrbook
- Loading branch information
1 parent
0bdbc03
commit e00e01f
Showing
8 changed files
with
248 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
package fullnode | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
|
||
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" | ||
) | ||
|
||
var ( | ||
//go:embed script/download-addrbook.sh | ||
scriptDownloadAddrbook string | ||
) | ||
|
||
const addrbookScriptWrapper = `ls $CONFIG_DIR/addrbook.json 1> /dev/null 2>&1 | ||
ADDRBOOK_EXISTS=$? | ||
if [ $ADDRBOOK_EXISTS -eq 0 ]; then | ||
echo "Address book already exists" | ||
exit 0 | ||
fi | ||
ls -l $CONFIG_DIR/addrbook.json | ||
%s | ||
ls -l $CONFIG_DIR/addrbook.json | ||
echo "Address book $ADDRBOOK_FILE downloaded" | ||
` | ||
|
||
// DownloadGenesisCommand returns a proper address book command for use in an init container. | ||
func DownloadAddrbookCommand(cfg cosmosv1.ChainSpec) (string, []string) { | ||
args := []string{"-c"} | ||
switch { | ||
case cfg.AddrbookScript != nil: | ||
args = append(args, fmt.Sprintf(addrbookScriptWrapper, *cfg.AddrbookScript)) | ||
case cfg.AddrbookURL != nil: | ||
args = append(args, fmt.Sprintf(addrbookScriptWrapper, scriptDownloadAddrbook), "-s", *cfg.AddrbookURL) | ||
default: | ||
args = append(args, "echo Using default address book") | ||
} | ||
return "sh", args | ||
} |
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,72 @@ | ||
package fullnode | ||
|
||
import ( | ||
"testing" | ||
|
||
cosmosv1 "github.com/strangelove-ventures/cosmos-operator/api/v1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDownloadAddrbookCommand(t *testing.T) { | ||
t.Parallel() | ||
|
||
requireValidScript := func(t *testing.T, script string) { | ||
t.Helper() | ||
require.NotEmpty(t, script) | ||
require.Contains(t, script, `if [ $ADDRBOOK_EXISTS -eq 0 ]`) | ||
} | ||
|
||
t.Run("default", func(t *testing.T) { | ||
var cfg cosmosv1.ChainSpec | ||
|
||
cmd, args := DownloadAddrbookCommand(cfg) | ||
require.Equal(t, "sh", cmd) | ||
|
||
require.Len(t, args, 2) | ||
|
||
require.Equal(t, "-c", args[0]) | ||
|
||
got := args[1] | ||
require.NotContains(t, got, "ADDRBOOK_EXISTS") | ||
require.Contains(t, got, "Using default address book") | ||
}) | ||
|
||
t.Run("download", func(t *testing.T) { | ||
cfg := cosmosv1.ChainSpec{ | ||
AddrbookURL: ptr("https://example.com/addrbook.json"), | ||
} | ||
cmd, args := DownloadAddrbookCommand(cfg) | ||
require.Equal(t, "sh", cmd) | ||
|
||
require.Len(t, args, 4) | ||
|
||
require.Equal(t, "-c", args[0]) | ||
got := args[1] | ||
requireValidScript(t, got) | ||
require.Contains(t, got, `ADDRBOOK_URL`) | ||
require.Contains(t, got, "download_json") | ||
|
||
require.Equal(t, "-s", args[2]) | ||
require.Equal(t, "https://example.com/addrbook.json", args[3]) | ||
}) | ||
|
||
t.Run("custom", func(t *testing.T) { | ||
cfg := cosmosv1.ChainSpec{ | ||
// Keeping this to assert that custom script takes precedence. | ||
AddrbookURL: ptr("https://example.com/addrbook.json"), | ||
AddrbookScript: ptr("echo hi"), | ||
} | ||
cmd, args := DownloadAddrbookCommand(cfg) | ||
require.Equal(t, "sh", cmd) | ||
|
||
require.Len(t, args, 2) | ||
|
||
require.Equal(t, "-c", args[0]) | ||
|
||
got := args[1] | ||
requireValidScript(t, got) | ||
|
||
require.NotContains(t, got, "ADDRBOOK_URL") | ||
require.Contains(t, got, "echo hi") | ||
}) | ||
} |
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,53 @@ | ||
set -eu | ||
|
||
# $ADDRBOOK_FILE and $CONFIG_DIR already set via pod env vars. | ||
|
||
ADDRBOOK_URL="$1" | ||
|
||
echo "Downloading address book file $ADDRBOOK_URL to $ADDRBOOK_FILE..." | ||
|
||
download_json() { | ||
echo "Downloading plain json..." | ||
wget -c -O "$ADDRBOOK_FILE" "$ADDRBOOK_URL" | ||
} | ||
|
||
download_jsongz() { | ||
echo "Downloading json.gz..." | ||
wget -c -O - "$ADDRBOOK_URL" | gunzip -c >"$ADDRBOOK_FILE" | ||
} | ||
|
||
download_tar() { | ||
echo "Downloading and extracting tar..." | ||
wget -c -O - "$ADDRBOOK_URL" | tar -x -C "$CONFIG_DIR" | ||
} | ||
|
||
download_targz() { | ||
echo "Downloading and extracting compressed tar..." | ||
wget -c -O - "$ADDRBOOK_URL" | tar -xz -C "$CONFIG_DIR" | ||
} | ||
|
||
download_zip() { | ||
echo "Downloading and extracting zip..." | ||
wget -c -O tmp_genesis.zip "$ADDRBOOK_URL" | ||
unzip tmp_genesis.zip | ||
rm tmp_genesis.zip | ||
mv genesis.json "$ADDRBOOK_FILE" | ||
} | ||
|
||
rm -f "$ADDRBOOK_FILE" | ||
|
||
case "$ADDRBOOK_URL" in | ||
*.json.gz) download_jsongz ;; | ||
*.json) download_json ;; | ||
*.tar.gz) download_targz ;; | ||
*.tar.gzip) download_targz ;; | ||
*.tar) download_tar ;; | ||
*.zip) download_zip ;; | ||
*) | ||
echo "Unable to handle file extension for $ADDRBOOK_URL" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
echo "Saved address book file to $ADDRBOOK_FILE." | ||
echo "Download address book file complete." |