diff --git a/scripts/alphanet.sh b/scripts/alphanet.sh index 23ee1eac2..9d29be55e 100755 --- a/scripts/alphanet.sh +++ b/scripts/alphanet.sh @@ -2,11 +2,16 @@ set -e -if ! docker > /dev/null 2>&1 ; then +if ! which docker > /dev/null 2>&1 ; then echo "Docker does not seem to be installed." exit 1 fi +if ! which docker-compose > /dev/null 2>&1 ; then + echo "Docker-compose does not seem to be installed." + exit 1 +fi + docker_version="$(docker version -f "{{ .Server.Version }}")" docker_major="$(echo "$docker_version" | cut -d . -f 1)" docker_minor="$(echo "$docker_version" | cut -d . -f 2)" @@ -22,197 +27,131 @@ current_dir="$(pwd -P)" src_dir="$(cd "$(dirname "$0")" && echo "$current_dir/")" cd "$src_dir" -default_port=9732 -port="$default_port" +update_compose_file() { -docker_image=docker.io/tezos/tezos:alphanet -docker_volume=tezos-alphanet-data$suffix -suffix= - -data_dir="$HOME/.tezos-alphanet$suffix" -docker_container="tezos-alphanet$suffix" - -if [ ! -z "$ALPHANET_EMACS" ]; then - interactive_flags="-t" -else - interactive_flags="-it" -fi - - -## Saving state ############################################################ - -save_identity() { - if [ ! -f "$data_dir/identity.json" ]; then - echo "Saving the generated identity into '$data_dir/identity.json'..." - mkdir -p "$data_dir/" + if [ "$#" -ge 2 ] && [ "$1" = "--rpc-port" ] ; then + export_rpc=" + - \"$1:8732\"" + shift 2 fi - docker cp "$docker_container:var/run/tezos/node/identity.json" \ - "$data_dir/" + + cat > "$docker_compose_yml" < "$docker_pull_timestamp" } -check_container() { - res=$(docker inspect \ - --format="{{ .State.Running }}" \ - --type=container "$docker_container" 2>/dev/null \ - || echo false) - [ "$res" = true ] -} - -check_volume() { - docker volume inspect "$docker_volume" > /dev/null 2>&1 -} - -clear_volume() { - if check_volume ; then - docker volume rm "$docker_volume" > /dev/null - echo -e "\033[32mThe blockchain data has been removed from the disk.\033[0m" - else - echo -e "\033[32mNo remaining data to be removed from the disk.\033[0m" +may_pull_image() { + if [ ! -f "$docker_pull_timestamp" ] \ + || [ 3600 -le $(($(date "+%s") - $(cat $docker_pull_timestamp))) ]; then + pull_image fi } uptodate_container() { running_image=$(docker inspect \ --format="{{ .Image }}" \ - --type=container "$docker_container") + --type=container "$1") latest_image=$(docker inspect \ --format="{{ .Id }}" \ --type=image "$docker_image") [ "$latest_image" = "$running_image" ] } -assert_container_uptodate() { - pull_image > /dev/null - if ! uptodate_container; then - echo "The current container is not the latest available." - echo "Please restart." - exit 1 - fi -} - assert_container() { - if ! check_container; then - echo -e "\033[31mNo container currently running!\033[0m" - exit 1 - fi + call_docker_compose up --no-start } -start_container() { - if [ "$#" -ge 2 ] && [ "$1" = "--rpc-port" ] ; then - docker_export_rpc="-p $2:8732" - fi - if check_container; then - assert_container_uptodate - else - if ! check_volume; then - docker volume create "$docker_volume" - fi - docker rm "$docker_container" || true > /dev/null 2>&1 - echo "Launching the docker container..." - docker run --rm -dit -p "$port:$port" $docker_export_rpc \ - -v "$docker_volume:/var/run/tezos" \ - --entrypoint /bin/sh \ - --name "$docker_container" \ - "$docker_image" > /dev/null - may_restore_identity - may_restore_accounts - fi -} - -stop_container() { - if ! check_container; then - echo -e "\033[31mNo container to kill!\033[0m" - exit 1 - fi - save_identity ## Saving again, just in case... - save_accounts - printf "Stopping the container... " - docker stop "$docker_container" >/dev/null - echo " done" -} - - ## Node #################################################################### -init_node() { - docker exec "$docker_container" tezos init \ - "$@" --net-addr "[::]:$port" - save_identity +check_node_volume() { + docker volume inspect "$docker_node_volume" > /dev/null 2>&1 +} + +clear_node_volume() { + if check_node; then + echo -e "\033[31mCannot clear data while the node is running.\033[0m" + exit 1 + fi + if check_node_volume ; then + docker volume rm "$docker_node_volume" > /dev/null + echo -e "\033[32mThe chain data has been removed from the disk.\033[0m" + else + echo -e "\033[32mNo remaining data to be removed from the disk.\033[0m" + fi } check_node() { - check_container && docker exec "$interactive_flags" "$docker_container" tezos check_node + res=$(docker inspect \ + --format="{{ .State.Running }}" \ + --type=container "$docker_node_container" 2>/dev/null \ + || echo false) + [ "$res" = true ] } assert_node() { @@ -222,42 +161,67 @@ assert_node() { fi } +warn_node_uptodate() { + if ! uptodate_container "$docker_node_container"; then + echo -e "\033[33mThe current node is not the latest available.\033[0m" + fi +} + +assert_node_uptodate() { + may_pull_image + assert_node + if ! uptodate_container "$docker_node_container"; then + echo -e "\033[33mThe current node is not the latest available.\033[0m" + exit 1 + fi +} + status_node() { + may_pull_image if check_node; then echo -e "\033[32mNode is running\033[0m" + warn_node_uptodate else echo -e "\033[33mNode is not running\033[0m" fi } start_node() { + pull_image if check_node; then - echo -e "\033[31mCannot run two nodes in the same container!\033[0m" + echo -e "\033[31mNode is already running\033[0m" exit 1 fi - if $docker_1_13; then - tezos_log_env="-eTEZOS_LOG=${TEZOS_LOG:=* -> info}" - fi - docker exec -d "${tezos_log_env}" \ - "$docker_container" tezos run_node - sleep 1 - docker exec "$docker_container" tezos wait_node + update_compose_file "$@" + call_docker_compose up --no-start + call_docker_compose start node echo -e "\033[32mThe node is now running.\033[0m" } log_node() { - docker exec "$interactive_flags" "$docker_container" tezos log_node + may_pull_image + assert_node_uptodate + call_docker_compose logs -f node } stop_node() { - docker exec "$docker_container" tezos stop_node + if ! check_node; then + echo -e "\033[31mNo node to kill!\033[0m" + exit 1 + fi + echo -e "\033[32mStopping the node...\033[0m" + call_docker_compose stop node } ## Baker ################################################################### check_baker() { - check_node && docker exec "$interactive_flags" "$docker_container" tezos check_baker + res=$(docker inspect \ + --format="{{ .State.Running }}" \ + --type=container "$docker_baker_container" 2>/dev/null \ + || echo false) + [ "$res" = true ] } assert_baker() { @@ -267,9 +231,25 @@ assert_baker() { fi } +warn_baker_uptodate() { + if ! uptodate_container "$docker_baker_container"; then + echo -e "\033[33mThe current baker is not the latest available.\033[0m" + fi +} + +assert_baker_uptodate() { + assert_baker + if ! uptodate_container "$docker_baker_container"; then + echo -e "\033[33mThe current baker is not the latest available.\033[0m" + exit 1 + fi +} + status_baker() { if check_baker; then echo -e "\033[32mBaker is running\033[0m" + may_pull_image + warn_baker_uptodate else echo -e "\033[33mBaker is not running\033[0m" fi @@ -277,26 +257,38 @@ status_baker() { start_baker() { if check_baker; then - echo -e "\033[31mCannot run two bakers in the same container!\033[0m" + echo -e "\033[31mBaker is already running\033[0m" exit 1 fi - TEZOS_LOG="${TEZOS_LOG:=* -> info}" - docker exec -d "$docker_container" tezos run_baker + pull_image + assert_node_uptodate + call_docker_compose start baker echo -e "\033[32mThe baker is now running.\033[0m" } log_baker() { - docker exec "$interactive_flags" "$docker_container" tezos log_baker + may_pull_image + assert_baker_uptodate + call_docker_compose logs -f baker } stop_baker() { - docker exec "$docker_container" tezos stop_baker + if ! check_baker; then + echo -e "\033[31mNo baker to kill!\033[0m" + exit 1 + fi + echo -e "\033[32mStopping the baker...\033[0m" + call_docker_compose stop baker } -## Baker ################################################################### +## Endorser ################################################################### check_endorser() { - check_node && docker exec "$interactive_flags" "$docker_container" tezos check_endorser + res=$(docker inspect \ + --format="{{ .State.Running }}" \ + --type=container "$docker_endorser_container" 2>/dev/null \ + || echo false) + [ "$res" = true ] } assert_endorser() { @@ -306,9 +298,25 @@ assert_endorser() { fi } +warn_endorser_uptodate() { + if ! uptodate_container "$docker_endorser_container"; then + echo -e "\033[33mThe current endorser is not the latest available.\033[0m" + fi +} + +assert_endorser_uptodate() { + assert_endorser + if ! uptodate_container "$docker_endorser_container"; then + echo -e "\033[33mThe current endorser is not the latest available.\033[0m" + exit 1 + fi +} + status_endorser() { if check_endorser; then echo -e "\033[32mEndorser is running\033[0m" + may_pull_image + warn_endorser_uptodate else echo -e "\033[33mEndorser is not running\033[0m" fi @@ -316,109 +324,103 @@ status_endorser() { start_endorser() { if check_endorser; then - echo -e "\033[31mCannot run two endorsers in the same container!\033[0m" + echo -e "\033[31mEndorser is already running\033[0m" exit 1 fi - TEZOS_LOG="${TEZOS_LOG:=* -> info}" - docker exec -d "$docker_container" tezos run_endorser + pull_image + assert_node_uptodate + call_docker_compose start endorser echo -e "\033[32mThe endorser is now running.\033[0m" } log_endorser() { - docker exec "$interactive_flags" "$docker_container" tezos log_endorser + may_pull_image + assert_endorser_uptodate + call_docker_compose logs -f endorser } stop_endorser() { - docker exec "$docker_container" tezos stop_endorser + if ! check_baker; then + echo -e "\033[31mNo baker to kill!\033[0m" + exit 1 + fi + echo -e "\033[32mStopping the baker...\033[0m" + call_docker_compose stop endorser } ## Misc #################################################################### run_client() { + assert_node_uptodate declare -a container_args=(); + tmpdir=$(exec_docker mktemp) for arg in "$@"; do if [[ "$arg" == 'container:'* ]]; then local_path="${arg#container:}" if [[ "$local_path" != '/'* ]]; then local_path="$current_dir/$local_path" fi - docker exec "$docker_container" mkdir -p -m 777 /tmp/copied/ + docker exec "$docker_container" mkdir -p -m 777 "$tmpdir" file_name=$(basename "${local_path}") - docker_path="/tmp/copied/$file_name" - docker cp "${local_path}" "$docker_container:${docker_path}" - docker exec "$docker_container" sudo chmod 644 "${docker_path}" + docker_path="$tmpdir/$file_name" + docker cp "${local_path}" "$docker_node_container:${docker_path}" + exec_docker chmod 644 "${docker_path}" container_args+=("file:$docker_path"); else container_args+=("${arg}"); fi done - docker exec "$interactive_flags" "$docker_container" tezos client "${container_args[@]}" - docker exec "$docker_container" rm -rf /tmp/copied # Remove copied files - save_accounts + exec_docker tezos-client "${container_args[@]}" + exec_docker rm -rf $tmpdir # Remove copied files } run_shell() { + assert_node_uptodate if [ $# -eq 0 ]; then - docker exec -it "$docker_container" bash + exec_docker /bin/sh else - docker exec -it "$docker_container" bash -c "$@" + exec_docker /bin/sh -c "$@" fi - save_accounts } display_head() { - docker exec "$interactive_flags" "$docker_container" tezos \ - client rpc call /blocks/head with '{}' - docker exec "$interactive_flags" "$docker_container" tezos \ - client rpc call /blocks/head/proto/context/level with '{}' + assert_node_uptodate + exec_docker tezos-client rpc call /blocks/head with '{}' + exec_docker tezos-client rpc call /blocks/head/proto/context/level with '{}' } ## Main #################################################################### start() { pull_image - start_container "$@" - init_node "$@" - start_node - start_baker - start_endorser - save_accounts + update_compose_file "$@" + call_docker_compose up -d warn_script_uptodate } -go_alpha_go() { - docker exec "$interactive_flags" "$docker_container" tezos client \ - activate \ - protocol ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK \ - with fitness 1 \ - and key dictator +stop() { + call_docker_compose down } -stop() { - stop_node || true - stop_container +kill_() { + call_docker_compose kill + stop } status() { - pull_image - if ! uptodate_container; then - echo -e "\033[31mThe container is running but not the latest available.\033[0m" - exit 1 - fi - echo -e "\033[32mThe container is running and up to date.\033[0m" - warn_script_uptodate verbose status_node status_baker status_endorser + warn_script_uptodate verbose } warn_script_uptodate() { if [[ $ALPHANET_EMACS ]]; then return fi - docker cp "$docker_container:home/tezos/scripts/alphanet.sh" \ - ".alphanet.sh.new" + docker run --entrypoint /bin/cat "$docker_image" \ + "/usr/local/share/tezos/alphanet.sh" > ".alphanet.sh.new" if ! diff .alphanet.sh.new "$0" >/dev/null 2>&1 ; then echo -e "\033[33mWarning: the container contains a new version of 'alphanet.sh'.\033[0m" echo -e "\033[33mYou might run '$0 update_script' to synchronize.\033[0m" @@ -428,17 +430,9 @@ warn_script_uptodate() { rm .alphanet.sh.new } -assert_uptodate() { - assert_container - assert_container_uptodate - warn_script_uptodate -} - update_script() { - pull_image - tmp="$(docker run --rm -dit --entrypoint /bin/sleep "$docker_image" 20)" - docker cp "$tmp:home/tezos/scripts/alphanet.sh" ".alphanet.sh.new" - docker stop "$tmp" > /dev/null + docker run --entrypoint /bin/cat "$docker_image" \ + "/usr/local/share/tezos/alphanet.sh" > ".alphanet.sh.new" if ! diff .alphanet.sh.new "$0" >/dev/null 2>&1 ; then mv .alphanet.sh.new "$0" echo -e "\033[32mThe script has been updated.\033[0m" @@ -482,7 +476,6 @@ usage() { echo " $0 update_script" echo " Replace 'alphanet.sh' with the one found in the docker image." echo " Advanced commands:" - echo " $0 container " echo " $0 node " echo " $0 baker " echo " $0 endorser " @@ -502,13 +495,57 @@ usage() { if [ "$#" -ge 2 ] && [ "$1" = "--port" ] ; then port="$2" - suffix=".$port" + suffix="$port" shift 2 fi command="$1" if [ "$#" -eq 0 ] ; then usage ; exit 1; else shift ; fi +case $(basename $0) in + localnet.sh) + docker_base_dir="$HOME/.tezos-localnet" + docker_image=tezos:latest + docker_compose_base_name=localnet + default_port=14732 + ;; + zeronet.sh) + docker_base_dir="$HOME/.tezos-zeronet" + docker_image=tezos/tezos:zeronet + docker_compose_base_name=zeronet + default_port=19732 + ;; + *) + docker_base_dir="$HOME/.tezos-alphanet" + docker_image=tezos:latest + docker_compose_base_name="alphanet" + default_port=9732 + ;; +esac + +if [ -n "$suffix" ] ; then + mkdir -p "$docker_base_dir" + echo "$port" > "$docker_base_dir/default_port" +elif [ -f "$docker_base_dir/default_port" ]; then + port=$(cat "$docker_base_dir/default_port") + suffix="$port" +else + port=$default_port +fi + +docker_dir="$docker_base_dir$suffix" +docker_compose_yml="$docker_dir/docker-compose.yml" +docker_pull_timestamp="$docker_dir/docker_pull.timestamp" +docker_compose_name="$docker_compose_base_name$suffix" + +docker_node_container=${docker_compose_name}_node_1 +docker_baker_container=${docker_compose_name}_baker_1 +docker_endorser_container=${docker_compose_name}_endorser_1 + +docker_node_volume=${docker_compose_name}_node_data +docker_client_volume=${docker_compose_name}_client_data + +mkdir -p "$docker_dir" case "$command" in @@ -518,57 +555,24 @@ case "$command" in start "$@" ;; restart) - if check_container; then - stop_container - fi + stop update_script export TEZOS_ALPHANET_DO_NOT_PULL=yes exec "$0" start "$@" ;; clear) - if check_container; then - echo -e "\033[31mCannot clear data while the container is running.\033[0m" - exit 1 - fi - clear_volume + clear_node_volume ;; status) - assert_container status ;; stop) - assert_container stop ;; kill) - stop_container + kill_ ;; - ## Container - - container) - subcommand="$1" - if [ "$#" -eq 0 ] ; then usage ; exit 1 ; else shift ; fi - case "$subcommand" in - start) - start_container "$@" - warn_script_uptodate - ;; - status) - if check_container; then - echo -e "\033[32mContainer is running\033[0m" - else - echo -e "\033[33mContainer is not running\033[0m" - fi - ;; - stop) - stop_container - ;; - *) - usage - exit 1 - esac ;; - ## Node node) @@ -576,19 +580,15 @@ case "$command" in if [ "$#" -eq 0 ] ; then usage ; exit 1; else shift ; fi case "$subcommand" in start) - assert_uptodate - start_node + start_node "$@" ;; status) - assert_uptodate status_node ;; log) - assert_uptodate log_node ;; stop) - assert_uptodate stop_node ;; *) @@ -602,20 +602,15 @@ case "$command" in if [ "$#" -eq 0 ] ; then usage ; exit 1; else shift ; fi case "$subcommand" in status) - assert_uptodate status_baker ;; start) - assert_uptodate - assert_node start_baker ;; log) - assert_uptodate log_baker ;; stop) - assert_uptodate stop_baker ;; *) @@ -630,20 +625,15 @@ case "$command" in if [ "$#" -eq 0 ] ; then usage ; exit 1; else shift ; fi case "$subcommand" in status) - assert_uptodate status_endorser ;; start) - assert_uptodate - assert_node start_endorser ;; log) - assert_uptodate log_endorser ;; stop) - assert_uptodate stop_endorser ;; *) @@ -654,26 +644,16 @@ case "$command" in ## Misc. head) - assert_uptodate - assert_node display_head ;; - go_alpha_go) - assert_uptodate - assert_node - go_alpha_go - ;; shell) - assert_uptodate run_shell "$@" ;; client) - assert_uptodate - assert_node run_client "$@" ;; check_script) - assert_uptodate + warn_script_uptodate verbose ;; update_script) update_script diff --git a/scripts/ci/create_docker_image.minimal.sh b/scripts/ci/create_docker_image.minimal.sh index 621357b33..a17ad864f 100755 --- a/scripts/ci/create_docker_image.minimal.sh +++ b/scripts/ci/create_docker_image.minimal.sh @@ -31,12 +31,14 @@ cp -a "$build_dir"/leveldb-$leveldb_version-r0.apk \ "$tmp_dir" mkdir -p "$tmp_dir"/bin +mkdir -p "$tmp_dir"/scripts container=$(docker create $build_image_name) for bin in tezos-client tezos-node; do docker cp -L $container:/home/opam/tezos/$bin "$tmp_dir"/bin done -cp -a "$script_dir"/docker_entrypoint.sh "$tmp_dir"/bin/tezos -cp -a "$script_dir"/docker_entrypoint.inc.sh "$tmp_dir"/bin/ +cp -a "$script_dir"/docker "$tmp_dir"/scripts/ +cp "$script_dir"/alphanet.sh "$tmp_dir"/scripts/ +cp "$script_dir"/alphanet_version "$tmp_dir"/scripts/ echo echo "### Building minimal docker image..." @@ -49,37 +51,32 @@ LABEL distro_style="apk" distro="alpine" distro_long="alpine-$alpine_version" ar COPY keys /etc/apk/keys/ COPY leveldb-$leveldb_version-r0.apk . -COPY bin . + +RUN apk --no-cache add \ + libssl1.0 libsodium libev gmp \ + leveldb-1.18-r0.apk && \ + rm leveldb-$leveldb_version-r0.apk + +COPY bin/tezos-node \ + bin/tezos-client \ + scripts/docker/entrypoint.sh \ + scripts/docker/entrypoint.inc.sh \ + /usr/local/bin/ + +COPY scripts/alphanet_version \ + scripts/alphanet.sh \ + /usr/local/share/tezos/ RUN adduser -S tezos && \ - adduser tezos abuild && \ - apk add --no-cache sudo bash \ - libssl1.0 libsodium libev gmp git snappy \ - leveldb-$leveldb_version-r0.apk && \ - echo 'tezos ALL=(ALL:ALL) NOPASSWD:ALL' > /etc/sudoers.d/tezos && \ - chmod 440 /etc/sudoers.d/tezos && \ - chown root:root /etc/sudoers.d/tezos && \ - sed -i 's/^Defaults.*requiretty//g' /etc/sudoers + mkdir -p /var/run/tezos/node /var/run/tezos/client && \ + chown -R tezos /var/run/tezos USER tezos -COPY . /home/tezos +VOLUME /var/run/tezos/node +VOLUME /var/run/tezos/client -WORKDIR /home/tezos - -RUN sudo chown root:root bin/* && \ - sudo chmod a+rx bin/* && \ - sudo mv bin/* /usr/local/bin && \ - rmdir bin - -RUN sudo mkdir -p /var/run/tezos && \ - sudo chown tezos /var/run/tezos - -ENV EDITOR=vi - -VOLUME /var/run/tezos - -ENTRYPOINT [ "/usr/local/bin/tezos" ] +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] EOF docker build -t "$image_name:$image_version" "$tmp_dir" diff --git a/scripts/docker/entrypoint.inc.sh b/scripts/docker/entrypoint.inc.sh new file mode 100644 index 000000000..b0a6cf4b0 --- /dev/null +++ b/scripts/docker/entrypoint.inc.sh @@ -0,0 +1,118 @@ +#!/bin/sh + +configure_client() { + + local client_config="$HOME/.tezos-client/config" + mkdir -p "$client_dir" "$HOME/.tezos-client" + + if [ ! -f "$client_config" ]; then + "$client" --base-dir "$client_dir" \ + --addr "$NODE_HOST" --port "$RPC_PORT" \ + config init --output "$client_config" >/dev/null 2>&1 + else + "$client" --base-dir "$client_dir" \ + --addr "$NODE_HOST" --port "$RPC_PORT" \ + config update >/dev/null 2>&1 + fi + +} + +wait_for_the_node_to_be_ready() { + local count=0 + if "$client" rpc call /blocks/head/hash >/dev/null 2>&1; then return; fi + printf "Waiting for the node to initialize..." + sleep 1 + while ! "$client" rpc call /blocks/head/hash >/dev/null 2>&1 + do + count=$((count+1)) + if [ "$count" -ge 30 ]; then + echo " timeout." + exit 2 + fi + printf "." + sleep 1 + done + echo " done." +} + +wait_for_the_node_to_be_bootstraped() { + wait_for_the_node_to_be_ready + echo "Waiting for the node to synchronize with the network..." + "$client" bootstrapped +} + +launch_node() { + + mkdir -p "$node_dir" + + if [ ! -f "$node_dir/config.json" ]; then + echo "Configuring the node..." + "$node" config init \ + --data-dir "$node_dir" \ + --net-addr ":$P2P_PORT" \ + --rpc-addr ":$RPC_PORT" \ + "$@" + else + echo "Updating the node configuration..." + "$node" config update \ + --data-dir "$node_dir" \ + --net-addr ":$port" \ + --rpc-addr ":$RPC_PORT" \ + "$@" + fi + + for i in "$@"; do + if [ "$i" = "--help" ] ; then exit 0; fi + done + + # Check if we have to reset the chain because the image we want to + # run has a incompatible version with the blockchain we have stored + # locally on disk + + local image_version="$(cat "/usr/local/share/tezos/alphanet_version")" + echo "Current public chain: $image_version." + if [ -f "$DATA_DIR/alphanet_version" ]; then + local local_data_version="$(cat "$DATA_DIR/alphanet_version")" + echo "Local chain data: $local_data_version." + if [ "$local_data_version" != "$image_version" ]; then + echo "Removing outdated chain data..." + if [ -f "$node_dir/identities.json" ]; then \ + mv "$node_dir/identities.json" /tmp + fi + rm -rf "$node_dir/*" + ## TODO also remove stored nonces and endorsement... + if [ -f "/tmp/identities.json" ]; then \ + mv /tmp/identities.json "$node_dir/" + fi + cp "/usr/local/share/tezos/alphanet_version" \ + "$DATA_DIR/alphanet_version" + fi + fi + + # Generate a new identity if not present + + if [ ! -f "$node_dir/identity.json" ]; then + echo "Generating a new node identity..." + "$node" identity generate 24. \ + --data-dir "$node_dir" + fi + + configure_client + + # Launching the node + + exec "$node" run --data-dir "$node_dir" + +} + +launch_baker() { + configure_client + wait_for_the_node_to_be_bootstraped + exec "$client" launch daemon --baking "$@" +} + +launch_endorser() { + configure_client + wait_for_the_node_to_be_bootstraped + exec "$client" launch daemon --endorsement "$@" +} diff --git a/scripts/docker/entrypoint.sh b/scripts/docker/entrypoint.sh new file mode 100755 index 000000000..1f78dbcc4 --- /dev/null +++ b/scripts/docker/entrypoint.sh @@ -0,0 +1,54 @@ +#!/bin/sh + +set -e + +bin_dir="$(cd "$(dirname "$0")" && echo "$(pwd -P)/")" + +: ${DATA_DIR:="/var/run/tezos"} + +: ${NODE_HOST:="node"} + +: ${P2P_PORT:="9732"} +: ${RPC_PORT:="8732"} + +node="/usr/local/bin/tezos-node" +client="/usr/local/bin/tezos-client" +admin_client="/usr/local/bin/tezos-admin-client" + +client_dir="$DATA_DIR/client" +node_dir="$DATA_DIR/node" + +. "$bin_dir/entrypoint.inc.sh" + +command=${1:-tezos-node} +shift 1 + +case $command in + tezos-node) + launch_node "$@" + ;; + tezos-baker) + launch_baker "$@" + ;; + tezos-endorser) + launch_endorser "$@" + ;; + tezos-client) + configure_client + exec "$client" "$@" + ;; + tezos-admin-client) + configure_client + exec "$admin_client" "$@" + ;; + *) + cat < /dev/null; then - echo "Ignoring the current peer identity (not enough proof of work)." - rm "$node_dir/identity.json" - fi - fi - if [ ! -f "$node_dir/identity.json" ]; then - "$node" identity generate \ - --data-dir "$node_dir" - fi -} - -check_node() { - pgrep -x tezos-node > /dev/null 2>&1 -} - -run_node() { - TEZOS_LOG="${TEZOS_LOG:='* -> info'}" - if check_node; then - echo "Cannot run two node instances in the same container." - exit 1 - fi - ## Temporary hack until Pierre has debugged Lwt... - export LWT_ASYNC_METHOD=none - ## END of temporary hack - exec "$node" run "$@" --data-dir "$node_dir" -} - -stop_node() { - if ! check_node; then - echo "No tezos node to kill!" - exit 1 ; - fi - printf "Killing the tezos node..." - pkill -x tezos-node - sleep 1 - while check_node; do - printf "." - sleep 1 - done - echo " done." -} - -log_node() { - if ! check_node ; then - echo - echo "#############################################" - echo "## ##" - echo "## Warning: The tezos node is not running! ##" - echo "## ##" - echo "#############################################" - echo - tail "$node_dir/log" - else - tail -f "$node_dir/log" - fi -} - -run_client() { - exec $client "$@" -} diff --git a/scripts/docker_entrypoint.sh b/scripts/docker_entrypoint.sh deleted file mode 100755 index eefff8237..000000000 --- a/scripts/docker_entrypoint.sh +++ /dev/null @@ -1,87 +0,0 @@ -#! /usr/bin/env bash - -set -e - -script_dir="$(cd "$(dirname "$0")" && echo "$(pwd -P)/")" -src_dir="$(dirname "$script_dir")" -cd "$src_dir" - -data_dir="/var/run/tezos" -export node_dir="$data_dir/node" -export client_dir="$data_dir/client" -export node="tezos-node" -export client="tezos-client -base-dir $client_dir" - -. tezos-init-sandboxed-client.sh -. "${script_dir}"/docker_entrypoint.inc.sh - -usage() { - echo "$0 command [options]" - echo - echo "where 'command' is one of:" - echo " - run_node: ... " - echo " - log_node: ... " - echo " - run_baker: ... " - echo " - log_baker: ... " - echo " - run_endorser: ... " - echo " - log_endorser: ... " -} - -command="$1" -shift - -case "$command" in - init) - init "$@" - ;; - run_node) - run_node "$@" - ;; - check_node) - check_node - ;; - wait_node) - if ! check_node; then - echo "The tezos node is not running!" - exit 1 - fi - wait_for_the_node_to_be_bootstraped - may_create_identity - ;; - stop_node) - stop_node - ;; - log_node) - log_node - ;; - run_baker) - run_baker "$@" - ;; - check_baker) - check_baker - ;; - stop_baker) - stop_baker - ;; - log_baker) - log_baker - ;; - run_endorser) - run_endorser "$@" - ;; - check_endorser) - check_endorser - ;; - stop_endorser) - stop_endorser - ;; - log_endorser) - log_endorser - ;; - client) - run_client "$@" - ;; - *) - usage - exit 1 -esac diff --git a/scripts/localnet.sh b/scripts/localnet.sh new file mode 120000 index 000000000..34a626507 --- /dev/null +++ b/scripts/localnet.sh @@ -0,0 +1 @@ +alphanet.sh \ No newline at end of file diff --git a/scripts/zeronet.patch b/scripts/zeronet.patch index 321914168..3b007905b 100644 --- a/scripts/zeronet.patch +++ b/scripts/zeronet.patch @@ -1,36 +1,11 @@ -diff --git a/scripts/alphanet.sh b/scripts/alphanet.sh ---- a/scripts/alphanet.sh -+++ b/scripts/alphanet.sh -@@ -21,15 +21,15 @@ fi - src_dir="$(cd "$(dirname "$0")" && echo "$(pwd -P)/")" - cd "$src_dir" - --default_port=9732 -+default_port=19732 - port="$default_port" - --docker_image=docker.io/tezos/tezos:alphanet --docker_volume=tezos-alphanet-data$suffix -+docker_image=docker.io/tezos/tezos:zeronet -+docker_volume=tezos-zeronet-data$suffix - suffix= - --data_dir="$HOME/.tezos-alphanet$suffix" --docker_container="tezos-alphanet$suffix" -+data_dir="$HOME/.tezos-zeronet$suffix" -+docker_container="tezos-zeronet$suffix" - - if [ $ALPHANET_EMACS ]; then - interactive_flags="-t" -diff --git a/src/bin_node/node_config_file.ml b/src/bin_node/node_config_file.ml ---- a/src/bin_node/node_config_file.ml -+++ b/src/bin_node/node_config_file.ml -@@ -16,7 +16,7 @@ let home = - with Not_found -> "/root" - - let default_data_dir = home // ".tezos-node" --let default_net_port = 9732 -+let default_net_port = 19732 - let default_rpc_port = 8732 - - type t = { +-diff --git a/src/bin_node/node_config_file.ml b/src/bin_node/node_config_file.ml +---- a/src/bin_node/node_config_file.ml +-+++ b/src/bin_node/node_config_file.ml +-@@ -16,7 +16,7 @@ let home = +- with Not_found -> "/root" +- +- let default_data_dir = home // ".tezos-node" +--let default_net_port = 9732 +-+let default_net_port = 19732 +- let default_rpc_port = 8732 +- diff --git a/scripts/zeronet.sh b/scripts/zeronet.sh new file mode 120000 index 000000000..34a626507 --- /dev/null +++ b/scripts/zeronet.sh @@ -0,0 +1 @@ +alphanet.sh \ No newline at end of file