Directories in init hook not being created in container

I have a plan which is currently running very happily (in production) with hab on linux (no container).

I can also run it perfectly happily in my studio. The directories are made, squid starts, a pid is written, and log files are written.

I’ve exported it to a Docker container, and I see what look to be errors with the subdirectories created under pkg.svc_var_path in my init hook.

The init hook looks like this:

#!/bin/sh

# create required directories
mkdir -p {{pkg.svc_var_path}}/{logs,cache,run}
chown {{pkg.svc_group}}:{{pkg.svc_group}} {{pkg.svc_var_path}}/{logs,cache,run}
chmod o+w /dev/shm

But when I start the container, I see:

hab-sup(MR): Starting atalanta/squid
squid.default(UCW): Watching user.toml
hab-sup(MR): Starting gossip-listener on 0.0.0.0:9638
hab-sup(MR): Starting http-gateway on 0.0.0.0:9631
squid.default(HK): init, compiled to /hab/svc/squid/hooks/init
squid.default(HK): Hooks compiled
squid.default(SR): Hooks recompiled
default(CF): Updated squid.conf 45161070dfdbdf66382c916c0b0047fc269c770e6c9bc6920cf5bb018e93fe48
squid.default(SR): Configuration recompiled
squid.default(SR): Initializing
squid.default(SV): Starting service as user=root, group=hab
squid.default(O): WARNING: Cannot write log file: /hab/svc/squid/var/logs/cache.log
squid.default(O): /hab/svc/squid/var/logs/cache.log: No such file or directory
squid.default(O):          messages will be sent to 'stderr'.
squid.default(O): 2018/04/17 23:13:17| Current Directory is /hab/svc/squid
squid.default(O): WARNING: Cannot write log file: /hab/svc/squid/var/logs/cache.log
squid.default(O): /hab/svc/squid/var/logs/cache.log: No such file or directory
squid.default(O):          messages will be sent to 'stderr'.
squid.default(O): 2018/04/17 23:13:17| WARNING: Closing open FD    2
squid.default(O): 2018/04/17 23:13:17| Starting Squid Cache version 3.5.27 for x86_64-pc-linux-gnu...
squid.default(O): 2018/04/17 23:13:17| Service Name: squid
squid.default(O): 2018/04/17 23:13:17| Process ID 43
squid.default(O): 2018/04/17 23:13:17| Process Roles: master worker
squid.default(O): 2018/04/17 23:13:17| With 1048576 file descriptors available
squid.default(O): 2018/04/17 23:13:17| Initializing IP Cache...
squid.default(O): 2018/04/17 23:13:17| DNS Socket created at [::], FD 9
squid.default(O): 2018/04/17 23:13:17| DNS Socket created at 0.0.0.0, FD 10
squid.default(O): 2018/04/17 23:13:17| Adding nameserver 67.207.67.3 from /etc/resolv.conf
squid.default(O): 2018/04/17 23:13:17| Adding nameserver 67.207.67.2 from /etc/resolv.conf
squid.default(O): 2018/04/17 23:13:17| Logfile: opening log daemon:/hab/svc/squid/var/logs/access.log
squid.default(O): 2018/04/17 23:13:17| Logfile Daemon: opening log /hab/svc/squid/var/logs/access.log
squid.default(O): 2018/04/17 23:13:17| Store logging disabled
squid.default(O): 2018/04/17 23:13:17| Swap maxSize 0 + 262144 KB, estimated 20164 objects
squid.default(O): 2018/04/17 23:13:17| Target number of buckets: 1008
squid.default(O): 2018/04/17 23:13:17| Using 8192 Store buckets
squid.default(O): 2018/04/17 23:13:17| Max Mem  size: 262144 KB
squid.default(O): 2018/04/17 23:13:17| Max Swap size: 0 KB
squid.default(O): 2018/04/17 23:13:17| Using Least Load store dir selection
squid.default(O): 2018/04/17 23:13:17| Current Directory is /hab/svc/squid
squid.default(O): 2018/04/17 23:13:17| Finished loading MIME types and icons.
squid.default(O): 2018/04/17 23:13:17| HTCP Disabled.
squid.default(O): 2018/04/17 23:13:17| /hab/svc/squid/var/run/squid.pid: (2) No such file or directory
squid.default(O): 2018/04/17 23:13:17| WARNING: Could not write pid file
squid.default(O): 2018/04/17 23:13:17| Squid plugin modules loaded: 0
squid.default(O): 2018/04/17 23:13:17| Adaptation support is off.
squid.default(O): 2018/04/17 23:13:17| Accepting HTTP Socket connections at local=[::]:3128 remote=[::] FD 13 flags=9
squid.default(O): fopen: No such file or directory
squid.default(O): 2018/04/17 23:13:18| logfileHandleWrite: daemon:/hab/svc/squid/var/logs/access.log: error writing ((32) Broken pipe)
squid.default(O): 2018/04/17 23:13:18| Closing HTTP port [::]:3128
squid.default(O): 2018/04/17 23:13:18| storeDirWriteCleanLogs: Starting...
squid.default(O): 2018/04/17 23:13:18|   Finished.  Wrote 0 entries.
squid.default(O): 2018/04/17 23:13:18|   Took 0.00 seconds (  0.00 entries/sec).
squid.default(O): FATAL: I don't handle this error well!
squid.default(O): Squid Cache (Version 3.5.27): Terminated abnormally.
squid.default(O): CPU Usage: 0.076 seconds = 0.016 user + 0.060 sys
squid.default(O): Maximum Resident Size: 585600 KB
squid.default(O): Page faults with physical i/o: 51
hab-launch(SV): Child for service 'squid.default' with PID 43 exited with code exit code: 1

It looks like we can’t write logs or write a pid file.

Am I missing something?

OK, so I think this is because my init hook uses bash syntax:

mkdir -p {{pkg.svc_var_path}}/{logs,cache,run}

In the studio, and on a CentOS box, /bin/sh is bash:

# stat /bin/sh
  File: '/bin/sh' -> '/hab/pkgs/core/bash/4.3.42/20170513213519/bin/sh'

In a container it’s busybox sh:

$ docker exec -it 84938876a0ec /bin/stat /bin/sh
  File: '/bin/sh' -> '/hab/pkgs/core/busybox-static/1.24.2/20170513215502/bin/sh'

Lesson: Either don’t use bashisms in your hooks, or explicitly make bash a runtime dependency.

This is absolutely correct. I think we should discuss throwing together a portable shell scripting guide for folks or link to one in the docs so people know what strange things to avoid.

In ubuntu and debian /bin/sh is actually dash so theres one more wonderful variation for you.

I think my plans in the future i’m just always going to dep on ksh and use that as my interpreter :rofl:

We could argue that core/bash should be the default shell for Hooks by default. It’s the lowest-common-denominator and the syntax used in Plans.

1 Like