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

Symlink /app to real app dir #1

Open
jkutner opened this issue Mar 26, 2019 · 14 comments
Open

Symlink /app to real app dir #1

jkutner opened this issue Mar 26, 2019 · 14 comments

Comments

@jkutner
Copy link
Contributor

jkutner commented Mar 26, 2019

The cnb-shim should create a symlink from /app to /workspace/app (or wherever the real app directory is). This will prevent problems with buildpacks that hard code references to /app

@hawksight
Copy link

@jkutner I'm posting here the full output for you from my usage of pack based on your gist

Current output:

❯ pack build lighthouse --builder heroku/buildpacks --buildpack /Users/pfiddes/GitHub/elixir-cnb

Defaulting app directory to current working directory /Users/pfiddes/Projects/lighthouse (use --path to override)
Using user-provided builder image heroku/buildpacks
Pulling builder image heroku/buildpacks (use --no-pull flag to skip this step)
Selected run image heroku/pack:18 from stack heroku-18
Pulling run image heroku/pack:18 (use --no-pull flag to skip this step)
Using cache volume pack-cache-95b5ff50c5895704f955b55ea458f332
===> DETECTING
Using manually-provided group
[detector] 2019/03/26 17:28:30 Trying group of 1...
[detector] 2019/03/26 17:28:30 ======== Output: Elixir ========
[detector] Elixir
[detector] 2019/03/26 17:28:30 ======== Results ========
[detector] 2019/03/26 17:28:30 Elixir: pass
===> ANALYZING
Reading information from previous image for possible re-use
[analyzer] 2019/03/26 17:28:31 WARNING: image 'lighthouse' not found or requires authentication to access
[analyzer] 2019/03/26 17:28:31 WARNING: image 'lighthouse' has incompatible 'io.buildpacks.lifecycle.metadata' label
[analyzer] 2019/03/26 17:28:31 using cached layer 'hashnuke.elixir:shim'
[analyzer] 2019/03/26 17:28:31 using cached layer 'hashnuke.elixir:shi'
===> BUILDING
[builder] mkdir: cannot create directory '/workspace/app/.platform_tools': File exists
[builder] -----> Checking Erlang and Elixir versions
[builder]        Will use the following versions:
[builder]        * Stack heroku-18
[builder]        * Erlang 20.3.6
[builder]        * Elixir 1.6
[builder] -----> Will export the following config vars:
[builder]        * MIX_ENV=prod
[builder] -----> Using cached Erlang 20.3.6
[builder] -----> Installing Erlang 20.3.6
[builder]
[builder] -----> Using cached Elixir v1.6
[builder] -----> Installing Elixir v1.6
[builder] replace bin/elixir? [y]es, [n]o, [A]ll, [N]one, [r]ename:  NULL
[builder] (EOF or read error, treating as "[N]one" ...)
[builder] -----> Installing Hex
[builder] /workspace/app/.platform_tools/erlang/bin/erl: 29: exec: /app/.platform_tools/erlang/erts-9.1/bin/erlexec: not found
[builder] -----> Installing rebar
[builder] /workspace/app/.platform_tools/erlang/bin/erl: 29: exec: /app/.platform_tools/erlang/erts-9.1/bin/erlexec: not found
[builder] -----> Fetching app dependencies with mix
[builder] /workspace/app/.platform_tools/erlang/bin/erl: 29: exec: /app/.platform_tools/erlang/erts-9.1/bin/erlexec: not found
[builder] 2019/03/26 17:28:36 Error: failed to : exit status 1
ERROR: running builder in container: failed with status code: 7

Stacks

❯ pack stacks

Stack ID                                 Build Image             Run Image(s)
--------                                 -----------             ------------
io.buildpacks.stacks.bionic (default)    packs/build:v3alpha2    packs/run:v3alpha2
heroku-18                                heroku/pack:18-build    heroku/pack:18

The elixir_buildpack.config

# Erlang version
erlang_version=20.3.6

# Elixir version
elixir_version=1.6

# Set the path the app is run from
runtime_path="/worksapce/app"

Note that runtime_path i didn't have originally, just playing to see if it solved the issue.

Finally the buildpack.toml from the elixir-cnb project:

[buildpack]
id = "hashnuke.elixir"
version = "0.1"
name = "Elixir"

[[stacks]]
id = "heroku-18"

I thought I'd share the config so that you can see if I've misconfigured anything, and that it might be useful for debugging.

Does pack have a global logging or debug flag?
eg.

pack --debug <COMMAND> <ARGS>
# or
pack --log-level debug <COMMAND> <ARGS>

@jkutner
Copy link
Contributor Author

jkutner commented Mar 27, 2019

@hawksight i wasn't able to reproduce this with my sample app but I don't know much about elixir. I cloned the heroku-buildpack-elixir-test app, and added an elixir_buildpack.config like the one you showed. It built successfully.

Do you have an app I can clone and run against?

@hawksight
Copy link

Unfortunately all the apps are private so I can't share anything at the moment. But the app I've chosen is the simplest we have and as close to the sample app as I could get.
I just tried re-creating the elixir-cnb directory and also used the sample app with the same elixix_buildpack.config file. It worked first time.

I'll do some digging, there must be some reason why it fails to install hex & rebar.

@hawksight
Copy link

Ok not sure what I did but something's working now, or at least more than it did before!
Also there was a type on runtime_path before, so corrected and it work and removed and it also worked.. up to this point:

[builder] -----> Fetching app dependencies with mix
[builder] Resolving Hex dependencies...
[builder] Dependency resolution completed:
[builder] Unchanged:
[builder]   certifi 2.0.0
[builder]   cowboy 1.1.2
[builder]   cowlib 1.0.2
[builder]   hackney 1.10.1
[builder]   httpoison 0.13.0
[builder]   idna 5.1.0
[builder]   metrics 1.0.1
[builder]   mime 1.3.0
[builder]   mimerl 1.0.2
[builder]   plug 1.7.1
[builder]   plug_cowboy 1.0.0
[builder]   plug_crypto 1.0.0
[builder]   poison 3.1.0
[builder]   ranch 1.3.2
[builder]   sentry 6.4.2
[builder]   ssl_verify_fun 1.1.1
[builder]   unicode_util_compat 0.3.1
[builder] All dependencies up to date
[builder] -----> Copying hex from /root/.mix/archives/hex-0.19.0
[builder] -----> Compiling
[builder] ** (Mix) You're trying to run :lighthouse on Elixir v1.5.0 but it has declared in its mix.exs file it supports only Elixir ~> 1.6.4
[builder] 2019/03/27 16:23:28 Error: failed to : exit status 1
ERROR: running builder in container: failed with status code: 7

I tried adding a version check before compiling and got:

[builder] All dependencies up to date
[builder] -----> Copying hex from /root/.mix/archives/hex-0.19.0
[builder] -----> Executing hook before compile: iex --version
[builder] IEx 1.5.0
[builder] -----> Compiling
[builder] ** (Mix) You're trying to run :lighthouse on Elixir v1.5.0 but it has declared in its mix.exs file it supports only Elixir ~> 1.6.4
[builder] 2019/03/27 16:30:46 Error: failed to : exit status 1
ERROR: running builder in container: failed with status code: 7

So seems like the builder image needs to have get the same elixir version? Although It does install those versions previously, so seems a bit off that it returns to the 1.5 version.

@hawksight
Copy link

hawksight commented Mar 27, 2019

Actually please ignore that last update.. I just cleaned up the cached volumes and it seemed to work perfectly!

docker volume prune

I rebuilt everything and the version check was spot on this time. So it must have been caching.

@jkutner
Copy link
Contributor Author

jkutner commented Mar 28, 2019

@hawksight do you think the runtime_path config fixed it (and the cache prevented it from working at first)?

@hawksight
Copy link

Im not sure if runtime_path has done anything. I actually removed thar value, pruned the caches and it rebuilt fine.
What I am seeing is that running the output image is that elixir won't run because it can't find erlang and neither of them are in the path. So its building but not entirely useful at the moment.

For example here's me shelling in to see whats going on:

❯ docker run -ti --entrypoint=/bin/sh lighthouse:latest                                                                      ⏎

$ exec bash
heroku@bda04172a607:/$ ls -lrt /workspace/app/.platform_tools/elixir/bin/elixir
-rwxr-xr-x 1 heroku pack 3882 Jan  1  1970 /workspace/app/.platform_tools/elixir/bin/elixir
heroku@bda04172a607:/$ /workspace/app/.platform_tools/elixir/bin/elixir --verison
/workspace/app/.platform_tools/elixir/bin/elixir: 127: exec: erl: not found
heroku@bda04172a607:/$ exit

I'll try and do some more debugging and see if I can get the app to run and elixir working inside the container.

@pclalv
Copy link

pclalv commented Sep 5, 2019

i've been playing around with this cnb-shim and the hashnuke-elixir buildpack.

i was able to fix the erl: not found error by making the following
the changes:

having fixed that, you'll see a new error:

cnb@ac6a5af85afe:/workspace$ mix
/workspace/.platform_tools/erlang/bin/erl: 29: exec: /workspace/app/.platform_tools/erlang/erts-9.1/bin/erlexec: not found

the erlexec binary we expected to find can't be found because of a broken symlink:

cnb@ac6a5af85afe:/workspace$ ls -la /workspace/app/.platform_tools/
total 8
drwxr-xr-x 2 cnb cnb 4096 Jan  1  1980 .
drwxr-xr-x 3 cnb cnb 4096 Jan  1  1980 ..
lrwxrwxrwx 1 cnb cnb   35 Jan  1  1980 erlang -> /layers/hashnuke.elixir/shim/erlang

that /layers/hashnuke.elixir/shim/ directory is not present at
launch because it's marked by the shim with cache = true but
(implicitly) launch = false. hacking the shim script to set launch = true yields yet another error:

cnb@fa78b3081c0d:/workspace$ mix
warning: the VM is running with native name encoding of latin1 which may cause Elixir to malfunction as it expects utf8. Please ensure your locale is set to UTF-8 (which can be verified by running "locale" in your shell)
Could not find Hex, which is needed to build dependency :arc
Shall I install Hex? (if running non-interactively, use "mix local.hex --force") [Yn]

17:31:44.428 [error] Unable to load crypto library. Failed with error:
":load_failed, Failed to load NIF library /workspace/app/.platform_tools/erlang/lib/crypto-4.1/priv/lib/crypto: 'libcrypto.so.1.0.0: cannot open shared object file: No such file or directory'"
OpenSSL might not be installed on this system.


17:31:44.431 [warn]  The on_load function for module crypto returned:
{:error, {:load_failed, 'Failed to load NIF library /workspace/app/.platform_tools/erlang/lib/crypto-4.1/priv/lib/crypto: \'libcrypto.so.1.0.0: cannot open shared object file: No such file or directory\''}}

** (Mix) httpc request failed with: {:failed_connect, [{:to_address, {'repo.hex.pm', 443}}, {:inet, [:inet], {:eoptions, {:undef, [{:crypto, :supports, [], []}, {:tls_record, :supported_protocol_versions, 1, [file: 'tls_record.erl', line: 407]}, {:tls_record, :supported_protocol_versions, 0, [file: 'tls_record.erl', line: 247]}, {:ssl, :handle_options, 3, [file: 'ssl.erl', line: 709]}, {:ssl, :connect, 4, [file: 'ssl.erl', line: 115]}, {:http_transport, :connect, 4, [file: 'http_transport.erl', line: 109]}, {:httpc_handler, :connect, 4, [file: 'httpc_handler.erl', line: 790]}, {:httpc_handler, :connect_and_send_first_request, 3, [file: 'httpc_handler.erl', line: 802]}]}}}]}

Could not install Hex because Mix could not download metadata at https://repo.hex.pm/installs/hex-1.x.csv.

at this point i've decided to roll my own elixir CNB buildback, but i
hope that this information might be useful in improving the shim's
compatibility with the hashnuke builidpack.

@kamaln7
Copy link
Contributor

kamaln7 commented Feb 18, 2021

Sounds like Stack Buildpacks will allow us to create/modify the /app symlink on both the build and run images (with root privileges)?

@kamaln7
Copy link
Contributor

kamaln7 commented Mar 15, 2021

@hone we addressed this by modifying cnb-shim to add a profile.d script that, if needed, creates a /app/.heroku symlink pointing to <app dir>/.heroku. By being specific about the .heroku dir it can be done as a non-root user, assuming the user can write to /app. I can create a PR if that's a solution you're interested in

@Haegi
Copy link

Haegi commented Apr 7, 2021

@kamaln7 we are facing a similar problem and we would be interested in your solution.
Do have a branch where we could take a look?

@kamaln7
Copy link
Contributor

kamaln7 commented Apr 7, 2021

@Haegi sure, here you go: https://github.com/kamaln7/cnb-shim/blob/773a0821c2e94423c53c6249f95b0a393030a1f6/bin/build#L40-L44

Can you let me know if it works for you please?

@Haegi
Copy link

Haegi commented Apr 19, 2021

@kamaln7 thanks for sending me the snippet, unfortunately, it didn't work in my case. I also tried just adding a file with the same method but the I couldn't find the file.
But maybe I just missed something.

Snippet to add a file:

cat <<'EOF' >"${profile_dir}/profile.d/cnb_shim_symlink.sh"
touch $(pwd)/text.txt
EOF

To see if there is any file:

# build
pack build <my-path> --path <path-to-artefact> --builder heroku/buildpacks --buildpack ~/path/to/cnb

# test
docker run -it --rm --entrypoint /bin/bash <my-app>

@kamaln7
Copy link
Contributor

kamaln7 commented Apr 19, 2021

@Haegi profile.d scripts are run by the CNB launcher, so you'll need to use it as the entrypoint:

docker run -it --rm --entrypoint launcher <my-app> bash

# that should work, but if not, try the absolute path
docker run -it --rm --entrypoint /cnb/lifecycle/launcher <my-app> bash

That way, launcher will source the profile.d scripts and then run bash.


Update: another issue with the fix I linked earlier that @Haegi ran into was that the buildpack they used didn't write profile scripts. The if [[ -d .profile.d ]]; check failed and the profile.d layer wasn't created, so the script that creates the symlink wasn't added. If cnb-shim ends up adopting that fix, we'll want to make sure we write the profile.d layer for all shimmed buildpacks so they can include the symlink fix.

kamaln7 referenced this issue in digitalocean/cnb-shim Nov 22, 2022
create /app/.heroku symlink if needed
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

No branches or pull requests

5 participants