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

fix(envbuilder): make init command more readable #413

Closed
wants to merge 2 commits into from

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Nov 8, 2024

Makes output of long init scripts much more readable, but potentially much more verbose.
I'm open to making it a log.Debug instead.

Before:

$ docker run -it --rm -v $PWD:/workspaces/empty --net=host -e ENVBUILDER_CACHE_REPO=localhost:5000/cache -e ENVBUILDER_INIT_COMMAND=/usr/bin/bash -e ENVBUILDER_INIT_SCRIPT="$(cat scripts/build.sh)"  ghcr.io/coder/envbuilder:latest
envbuilder v1.0.5+dc6aa54 - Build development environments from repositories in a container
[...]
=== Running init command as user "root": ["/usr/bin/bash" "-c" "#!/usr/bin/env bash\n\ncd \"$(dirname \"${BASH_SOURCE[0]}\")\"\nset -exuo pipefail\n\narchs=()\npush=false\nbase=\"envbuilder\"\ntag=\"\"\n\nfor arg in \"$@\"; do\n  if [[ $arg == --arch=* ]]; then\n    arch=\"${arg#*=}\"\n    archs+=( \"$arch\" )\n  elif [[ $arg == --push ]]; then\n    push=true\n  elif [[ $arg == --base=* ]]; then\n    base=\"${arg#*=}\"\n  elif [[ $arg == --tag=* ]]; then\n    tag=\"${arg#*=}\"\n  else\n    echo \"Unknown argument: $arg\"\n    exit 1\n  fi\ndone\n\ncurrent=$(go env GOARCH)\nif [ ${#archs[@]} -eq 0 ]; then\n  echo \"No architectures specified. Defaulting to $current...\"\n  archs=( \"$current\" ) \nfi\n\nif [[ -z \"${tag}\"  ]]; then\n\ttag=$(./version.sh)\nfi\n\n# We have to use docker buildx to tag multiple images with\n# platforms tragically, so we have to create a builder.\nBUILDER_NAME=\"envbuilder\"\nBUILDER_EXISTS=$(docker buildx ls | grep $BUILDER_NAME || true)\n\n# If builder doesn't exist, create it\nif [ -z \"$BUILDER_EXISTS\" ]; then\n  echo \"Creating dockerx builder $BUILDER_NAME...\"\n  docker buildx create --use --platform=linux/arm64,linux/amd64,linux/arm/v7 --name $BUILDER_NAME\nelse\n  echo \"Builder $BUILDER_NAME already exists. Using it.\"\nfi\n\n# Ensure the builder is bootstrapped and ready to use\ndocker buildx inspect --bootstrap &> /dev/null\n\nldflags=(-X \"'github.com/coder/envbuilder/buildinfo.tag=$tag'\")\n\nfor arch in \"${archs[@]}\"; do\n  echo \"Building for $arch...\"\n  GOARCH=$arch CGO_ENABLED=0 go build -ldflags=\"${ldflags[*]}\" -o \"./envbuilder-${arch}\" ../cmd/envbuilder &\ndone\nwait\n\nargs=()\nfor arch in \"${archs[@]}\"; do\n  args+=( --platform \"linux/${arch}\" )\ndone\nif [ \"$push\" = true ]; then\n  args+=( --push )\nelse\n  args+=( --load )\nfi\n\n# coerce semver build tags into something docker won't complain about\ntag=\"${tag//\\+/-}\"\ndocker buildx build --builder $BUILDER_NAME \"${args[@]}\" -t \"${base}:${tag}\" -t \"${base}:latest\" -f Dockerfile .\n\n# Check if archs contains the current. If so, then output a message!\nif [[ -z \"${CI:-}\" ]] && [[ \" ${archs[*]} \" =~ ${current} ]]; then\n  docker tag \"${base}:${tag}\" envbuilder:latest\n  echo \"Tagged $current as ${base}:${tag} ${base}:latest!\"\nfi"]
+ archs=()
+ push=false
+ base=envbuilder
+ tag=
++ go env GOARCH
+ current=amd64
+ '[' 0 -eq 0 ']'
+ echo 'No architectures specified. Defaulting to amd64...'
No architectures specified. Defaulting to amd64...
+ archs=("$current")
+ [[ -z '' ]]
++ ./version.sh
/usr/bin/bash: line 34: ./version.sh: No such file or directory

After:

$ docker run -it --rm -v $PWD:/workspaces/empty --net=host -e ENVBUILDER_CACHE_REPO=localhost:5000/cache -e ENVBUILDER_EXIT_ON_PUSH_FAILURE=1 -e ENVBUILD
ER_INIT_COMMAND=/usr/bin/bash -e ENVBUILDER_INIT_SCRIPT="$(cat scripts/build.sh)"  envbuilder:latest
envbuilder v1.0.5+dev-8635f11-dirty - Build development environments from repositories in a container
[...]
=== Running init command as user "root":
/usr/bin/bash -c #!/usr/bin/env bash

cd "$(dirname "${BASH_SOURCE[0]}")"
set -exuo pipefail

archs=()
push=false
base="envbuilder"
tag=""

for arg in "$@"; do
  if [[ $arg == --arch=* ]]; then
    arch="${arg#*=}"
    archs+=( "$arch" )
  elif [[ $arg == --push ]]; then
    push=true
  elif [[ $arg == --base=* ]]; then
    base="${arg#*=}"
  elif [[ $arg == --tag=* ]]; then
    tag="${arg#*=}"
  else
    echo "Unknown argument: $arg"
    exit 1
  fi
done

current=$(go env GOARCH)
if [ ${#archs[@]} -eq 0 ]; then
  echo "No architectures specified. Defaulting to $current..."
  archs=( "$current" ) 
fi

if [[ -z "${tag}"  ]]; then
        tag=$(./version.sh)
fi

# We have to use docker buildx to tag multiple images with
# platforms tragically, so we have to create a builder.
BUILDER_NAME="envbuilder"
BUILDER_EXISTS=$(docker buildx ls | grep $BUILDER_NAME || true)

# If builder doesn't exist, create it
if [ -z "$BUILDER_EXISTS" ]; then
  echo "Creating dockerx builder $BUILDER_NAME..."
  docker buildx create --use --platform=linux/arm64,linux/amd64,linux/arm/v7
--name $BUILDER_NAME
else
  echo "Builder $BUILDER_NAME already exists. Using it."
fi

# Ensure the builder is bootstrapped and ready to use
docker buildx inspect --bootstrap &> /dev/null

ldflags=(-X "'github.com/coder/envbuilder/buildinfo.tag=$tag'")

for arch in "${archs[@]}"; do
  echo "Building for $arch..."
  GOARCH=$arch CGO_ENABLED=0 go build -ldflags="${ldflags[*]}" -o
"./envbuilder-${arch}" ../cmd/envbuilder &
done
wait

args=()
for arch in "${archs[@]}"; do
  args+=( --platform "linux/${arch}" )
done
if [ "$push" = true ]; then
  args+=( --push )
else
  args+=( --load )
fi

# coerce semver build tags into something docker won't complain about
tag="${tag//\+/-}"
docker buildx build --builder $BUILDER_NAME "${args[@]}" -t "${base}:${tag}" -t
"${base}:latest" -f Dockerfile .

# Check if archs contains the current. If so, then output a message!
if [[ -z "${CI:-}" ]] && [[ " ${archs[*]} " =~ ${current} ]]; then
  docker tag "${base}:${tag}" envbuilder:latest
  echo "Tagged $current as ${base}:${tag} ${base}:latest!"
fi
===
+ archs=()
+ push=false
+ base=envbuilder
+ tag=
++ go env GOARCH
+ current=amd64
+ '[' 0 -eq 0 ']'
+ echo 'No architectures specified. Defaulting to amd64...'
No architectures specified. Defaulting to amd64...
+ archs=("$current")
+ [[ -z '' ]]
++ ./version.sh
/usr/bin/bash: line 34: ./version.sh: No such file or directory
+ tag=

@johnstcn johnstcn self-assigned this Nov 8, 2024
@johnstcn johnstcn marked this pull request as ready for review November 8, 2024 18:01
@johnstcn johnstcn requested review from mafredri and SasSwart November 8, 2024 18:01
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

I’m wondering what prompted this change? Not opposed to prettifying the output, but I’d like for us to rethink it a bit (see comment).

@@ -104,7 +105,10 @@ func Run(ctx context.Context, opts options.Options, preExec ...func()) error {
return fmt.Errorf("set uid: %w", err)
}

opts.Logger(log.LevelInfo, "=== Running init command as user %q: %q", args.UserInfo.user.Username, append([]string{opts.InitCommand}, args.InitArgs...))
initCmd := strings.Join(append([]string{opts.InitCommand}, args.InitArgs...), " ")
Copy link
Member

Choose a reason for hiding this comment

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

An unfortunate side-effect of this is that it’s lossy. We’re printing ”/bin/bash -c #!… which is actually not true without quotes, so you can’t copy paste the output and run it like you could previously (as an example).

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea came from a customer call. An alternative option is to soft wrap in the UI but I figured it would also be cheap enough to do on the envbuilder end. I didn't think about the copy-pastability of the output!

@johnstcn
Copy link
Member Author

I'm going to leave this PR aside for now. If anyone has strong feelings either way, would be great to have a comment here.

@johnstcn johnstcn closed this Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants