Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating rosetta dockerfile #563

Merged
merged 2 commits into from
May 10, 2021
Merged

Conversation

wileyj
Copy link
Contributor

@wileyj wileyj commented Apr 21, 2021

  • Refactoring to use runit vs custom script
  • clone the latest releases vs static versions

Description

This change refactors the rosetta Dockerfile to use runit as a scheduler vs a customized shell script. This change was motivated by discussions with another team trying to use this Dockerfile, and realizing the static version was very out of date. Further work was added to standardize the Dockerfile, so there weren't things like custom users/scripts (particularly in the case of postgresql).

The builder images are also refactored to more closely resemble the default Dockerfiles, as well as cloning the latest release for each of stacks-blockchain-api and stacks-blockchain repos.

Other changes are more use of ARG and ENV variables to create the scripts, i.e.

printf '#!/bin/sh
exec svlogd -tt %s/postgresql' ${STACKS_LOG_DIR} > ${STACKS_SVC_DIR}/postgresql/log/run

Type of Change

  • New feature
  • Bug fix
  • API reference/documentation update
  • Other

- Refactoring to use runit vs custom script
- clone the latest releases vs static versions
@wileyj wileyj requested a review from zone117x April 21, 2021 19:13
@codecov
Copy link

codecov bot commented Apr 21, 2021

Codecov Report

Merging #563 (41f0ffe) into master (2c16f70) will increase coverage by 1.75%.
The diff coverage is 87.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #563      +/-   ##
==========================================
+ Coverage   65.14%   66.89%   +1.75%     
==========================================
  Files          59       76      +17     
  Lines        4963     8407    +3444     
  Branches      856     1494     +638     
==========================================
+ Hits         3233     5624    +2391     
- Misses       1729     2781    +1052     
- Partials        1        2       +1     
Impacted Files Coverage Δ
src/api/routes/rosetta/construction.ts 77.27% <83.33%> (+2.00%) ⬆️
src/api/rosetta-constants.ts 94.87% <100.00%> (-1.36%) ⬇️
src/api/routes/debug.ts 23.25% <100.00%> (ø)
src/api/routes/faucets.ts 38.62% <100.00%> (ø)
src/core-rpc/client.ts 75.34% <100.00%> (+1.05%) ⬆️
src/rosetta-helpers.ts 66.66% <100.00%> (-5.75%) ⬇️
src/api/routes/rosetta/account.ts 58.51% <0.00%> (-10.73%) ⬇️
src/datastore/common.ts 76.34% <0.00%> (-1.44%) ⬇️
... and 34 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e625623...41f0ffe. Read the comment docs.

@wileyj
Copy link
Contributor Author

wileyj commented Apr 23, 2021

ping @zone117x


ENV MAINNET_STACKS_CHAIN_ID=0x00000001
ENV TESTNET_STACKS_CHAIN_ID=0x80000000
ENV V2_POX_MIN_AMOUNT_USTX=90000000260
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is handled automatically now. @zone117x can you confirm?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this env var is no longer used anywhere.

## Build the stacks-blockchain-api
FROM node:lts-buster as stacks-blockchain-api-build
ARG STACKS_API_REPO=blockstack/stacks-blockchain-api
ENV STACKS_API_REPO=${STACKS_API_REPO}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this ENV var can be removed. ${STACKS_API_REPO} will just use the ARG like it's an ENV var. The ENV var directive should be used when the env variable needs to be available in the resulting image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless the repo name changes, in which case this is a single change or a build-arg vs multiple changes in the file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm saying you can delete just this ENV var and the image will work the same as it did before. There's no benefit to declaring an ARG then setting an ENV var to it if the env var won't be used in the final image because it's redundant. If the repo name changes, you'd update the ARG default value.

Comment on lines 53 to 69
ENV PG_DATA=${PG_DATA}
ENV PG_VERSION=${PG_VERSION}
ENV STACKS_SVC_DIR=${STACKS_SVC_DIR}
ENV STACKS_BLOCKCHAIN_DIR=${STACKS_BLOCKCHAIN_DIR}
ENV STACKS_BLOCKCHAIN_API_DIR=${STACKS_BLOCKCHAIN_API_DIR}
ENV STACKS_NETWORK=${STACKS_NETWORK}
ENV STACKS_LOG_DIR=${STACKS_LOG_DIR}
ENV STACKS_CORE_EVENT_PORT=3700
ENV STACKS_CORE_EVENT_HOST=127.0.0.1
ENV STACKS_NETWORK=$STACKS_NETWORK

ENV STACKS_EVENT_OBSERVER=127.0.0.1:3700

ENV STACKS_BLOCKCHAIN_API_PORT=3999
ENV STACKS_BLOCKCHAIN_API_HOST=0.0.0.0

ENV STACKS_CORE_RPC_HOST=127.0.0.1
ENV STACKS_CORE_RPC_PORT=20443

### Startup script & coordinator
RUN printf '#!/bin/bash\n\
trap "exit" INT TERM\n\
trap "kill 0" EXIT\n\
echo Your container args are: "$@"\n\
tail --retry -F stacks-api.log stacks-node.log 2>&1 &\n\
while true\n\
do\n\
pg_start\n\
stacks_api &> stacks-api.log &\n\
stacks_api_pid=$!\n\
if [ $STACKS_NETWORK = "mocknet" -o $STACKS_NETWORK = "dev" ]; then\n\
stacks-node start --config=/data/stacky/Stacks-${STACKS_NETWORK}.toml &> stacks-node.log &\n\
elif [ $STACKS_NETWORK = "testnet"]; then \n\
stacks-node start --config=/data/stacky/Stacks-mocknet.toml &> stacks-node.log &\n\
else\n\
stacks-node mainnet &> stacks-node.log &\n\
fi\n\
stacks_node_pid=$!\n\
wait $stacks_node_pid\n\
echo "node exit, restarting..."\n\
rkill -9 $stacks_api_pid\n\
pg_stop\n\
rm -rf $PGDATA\n\
sleep 5\n\
done\n\
' >> run.sh && chmod +x run.sh

ENV MAINNET_STACKS_CHAIN_ID=0x00000001
ENV TESTNET_STACKS_CHAIN_ID=0x80000000
ENV V2_POX_MIN_AMOUNT_USTX=90000000260
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we combine all of these ENV directives into one directive? It'll result in fewer layers in the image and decrease its size. Also if any of them aren't used in the final image's runtime, you can just use their ARG counterpart due to the same reason in my other comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it would also be incredibly hard to read. the goal here isn't a small image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine if you want to keep it that way, I don't find it difficult to read with one directive. Also it seems ENV directives don't create additional layers anymore either which is nice.


###################################
## runit service files
RUN printf '#!/bin/sh\nexec 2>&1\n[ ! -d %s ] && mkdir -p %s && chown -R postgres:postgres %s && gosu postgres /usr/lib/postgresql/%s/bin/pg_ctl init -D %s\nexec gosu postgres /usr/lib/postgresql/%s/bin/postmaster -D %s' ${PG_DATA} ${PG_DATA} ${PG_DATA} ${PG_VERSION} ${PG_DATA} ${PG_VERSION} ${PG_DATA} > ${STACKS_SVC_DIR}/postgresql/run \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered committing the ruinit service files to this repo, and COPYing them into the image? Might be easier to maintain and read that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible, sure - but changes to the repo would require a full rebuild since the cache layers would no longer be used.
Since there are many parts of this process, the goal was to create an image that can be built using cache layers as quickly as is feasible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but changes to the repo would require a full rebuild since the cache layers would no longer be used.

If you COPY only those ruinit files, changes to the repo wouldn't require a full rebuild. That only happens when you write COPY . . for example. Either way will work the same, it's just difficult to read or maintain the scripts when they're embedded.

ENV MAINNET_STACKS_CHAIN_ID=0x00000001
ENV TESTNET_STACKS_CHAIN_ID=0x80000000
ENV V2_POX_MIN_AMOUNT_USTX=90000000260
RUN apt-get update \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combining this RUN command with the following three will further decrease the number of layers and size of the resulting image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above - any change in the process/dockerfile would invalidate the cache layers and cause a longer rebuild.
Can they all be combined? yes - iniitially that's what i was doing. but any small change required a full rebuild vs updating a single layer or two.

Copy link
Member

@CharlieC3 CharlieC3 Apr 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any change in the process/dockerfile would invalidate the cache layers and cause a longer rebuild.

Only true if you COPY the dockerfile into the image. For RUN directives the docker engine only compares the text of the old RUN vs the text of the new RUN to determine if it can be reused from cache. So if there's a change in the command, it won't use the cache either way you configure it. These are only minor suggestions, so feel free to do what you like.

@wileyj
Copy link
Contributor Author

wileyj commented Apr 27, 2021

ping @zone117x

@zone117x
Copy link
Member

Thanks @wileyj this looks good to me. Do you think we should merge this to master now, or wait until we merge API develop branch to master?

@wileyj
Copy link
Contributor Author

wileyj commented Apr 27, 2021

Thanks @wileyj this looks good to me. Do you think we should merge this to master now, or wait until we merge API develop branch to master?

Up to you! I've shared the file from my branch with the folks who needed it, so there's no rush - pinging just to get it off my plate.

@zone117x
Copy link
Member

Up to you! I've shared the file from my branch with the folks who needed it, so there's no rush - pinging just to get it off my plate.

Alright, there could be some changes required once develop branch is ready, and that's expected this week. So I think holding off until then makes sense.

@wileyj wileyj changed the base branch from master to develop April 27, 2021 14:59
@wileyj
Copy link
Contributor Author

wileyj commented Apr 27, 2021

Up to you! I've shared the file from my branch with the folks who needed it, so there's no rush - pinging just to get it off my plate.

Alright, there could be some changes required once develop branch is ready, and that's expected this week. So I think holding off until then makes sense.

👍 changed the base branch to develop as well.

Base automatically changed from develop to master April 29, 2021 09:30
@wileyj
Copy link
Contributor Author

wileyj commented May 6, 2021

ping @zone117x

@zone117x
Copy link
Member

zone117x commented May 6, 2021

@wileyj the only concern I have about this PR is the move away from pinned versions (of the stacks-node repo at least).
See this discussion for more context on versioning issues: #567

I do think this dockerfile change is a step in the right direction, because the hardcoded versions tend to go stale.

Should we merge this PR with the possibility that this dockerfile could build images with stacks-api and stacks-node versions that are incompatible with each other (when the release cadences don't match up)?

@wileyj
Copy link
Contributor Author

wileyj commented May 6, 2021

#567

Interesting...I don't have any opinion directly on that discussion, but also - nothing prevents us from setting specific versions in the Dockerfile vs trying to dynamically retrieve the version through github api.

The reason I made the Dockerfile this way was to prevent having to manually update the version numbers each time a
new release is cut for either repo (stacks-blockchain or stacks-blockchain-api). If that's not preferred, I can roll back that idea to hardcode the versions.

Based on the discussion you linked, is there a pair of versions you'd like to start with initially?
i.e.
https://github.com/blockstack/stacks-blockchain-api/releases/tag/v0.58.0
https://github.com/blockstack/stacks-blockchain/releases/tag/2.0.11.0.0

Once I have the specific versions I can update the Dockerfile and we can get this off of our plate.
The caveat being we'll need to ensure we keep the file up to date with current versions.

@zone117x
Copy link
Member

zone117x commented May 7, 2021

Based on the discussion you linked, is there a pair of versions you'd like to start with initially?
i.e.
https://github.com/blockstack/stacks-blockchain-api/releases/tag/v0.58.0
https://github.com/blockstack/stacks-blockchain/releases/tag/2.0.11.0.0

Yes these would be good versions to set the dockerfile to for now 👍

The caveat being we'll need to ensure we keep the file up to date with current versions.

Yeah, this is definitely a pain, hopefully something we can figure out in #567 eventually.

hardcoding versions
little bit of cleanup
@wileyj
Copy link
Contributor Author

wileyj commented May 8, 2021

Based on the discussion you linked, is there a pair of versions you'd like to start with initially?
i.e.
https://github.com/blockstack/stacks-blockchain-api/releases/tag/v0.58.0
https://github.com/blockstack/stacks-blockchain/releases/tag/2.0.11.0.0

Yes these would be good versions to set the dockerfile to for now 👍

The caveat being we'll need to ensure we keep the file up to date with current versions.

Yeah, this is definitely a pain, hopefully something we can figure out in #567 eventually.

Updated to use hardcoded versions!

@zone117x zone117x merged commit 9039c20 into master May 10, 2021
@zone117x zone117x deleted the feature/cleanup-rosetta-dockerfile branch May 10, 2021 10:40
@blockstack-devops
Copy link

🎉 This PR is included in version 0.59.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants