kudulab / dojo

Containerize your development and operations environment
Apache License 2.0
296 stars 19 forks source link

dojo command crashes when bash is not installed #22

Closed xdivby0 closed 3 years ago

xdivby0 commented 3 years ago

What I tried to do

I was trying to use dojo to build a custom environment which worked. Dojofile: DOJO_DOCKER_IMAGE="xdivby0/mynode:0.1.0" Dockerfile that build the specified image:

FROM debian:9

ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini

# Install common Dojo scripts
ENV DOJO_VERSION=0.10.0

RUN apt-get update && \
  DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
  sudo git ca-certificates && \
  git clone --depth 1 -b ${DOJO_VERSION} https://github.com/kudulab/dojo.git /tmp/dojo_git &&\
  /tmp/dojo_git/image_scripts/src/install.sh && \
  rm -r /tmp/dojo_git

# CUSTOM TOOLS FOR THIS DOJO:
RUN apt install -y curl

ENV NODE_VERSION=12.x
RUN curl -sL https://deb.nodesource.com/setup_${NODE_VERSION} | bash -
RUN apt-get install -y nodejs

RUN curl -L https://npmjs.org/install.sh | sudo sh

# Optional scripts to run on container start
#COPY etc_dojo.d/scripts/* /etc/dojo.d/scripts/
# Optional environment variables to source on container start
#COPY etc_dojo.d/variables/* /etc/dojo.d/variables/

COPY profile /home/dojo/.profile
COPY bashrc /home/dojo/.bashrc
RUN chown dojo:dojo /home/dojo/.profile /home/dojo/.bashrc

ENTRYPOINT ["/tini", "-g", "--", "/usr/bin/entrypoint.sh"]
CMD ["/bin/bash"]

Then I wrote a .gitlab-ci.yml to automate the dev-environment on the gitlab runner:

default:
  image: docker:19.03.0-dind
  script:
    - apk update
    - apk add wget
    - wget -O dojo_bin https://github.com/kudulab/dojo/releases/download/0.10.0/dojo_linux_amd64
    - mv ./dojo_bin /bin/dojo
    - chmod +x /bin/dojo
    - cd dojofiles
    - chmod -R 777 /root
    - dojo
variables:
  DOCKER_HOST: tcp://docker:2375

This results in a SIGSEGV (stacktrace at the bottom of this issue).

Minimal Setup for Reproducing

You can reproduce this by running an alpine docker image with docker run -it --rm alpine /bin/ash and then inside the alpine run following commands that reflect what the .gitlab-ci.yml does:

apk update && \
apk add wget && \
wget -O dojo_bin https://github.com/kudulab/dojo/releases/download/0.10.0/dojo_linux_amd64 && \
mv ./dojo_bin /bin/dojo && \
chmod +x /bin/dojo && \
apk add --update docker openrc &&\
echo "DOJO_DOCKER_IMAGE=\"xdivby0/mynode:0.1.0\"" > Dojofile &&\
dojo

The resulting segmentation violation:

2020/10/09 13:05:03 [ 1]  INFO: (main.main) Dojo version 0.10.0
2020/10/09 13:05:03 [ 4]  WARN: (main.warnGeneral) WorkDirOuter: / is owned by root, which is not recommended
2020/10/09 13:05:03 [ 4]  WARN: (main.warnGeneral) Current user is root, which is not recommended
2020/10/09 13:05:03 [ 4]  INFO: (main.DockerDriver.HandleRun) docker command will be:
 docker run --rm -v /:/dojo/work -v /root:/dojo/identity:ro -v /tmp/dojo-environment-multiline-dojo--2020-10-09_13-05-03-26891746:/etc/dojo.d/variables/00-multiline-vars.sh -v /tmp/dojo-environment-bash-functions-dojo--2020-10-09_13-05-03-26891746:/etc/dojo.d/variables/01-bash-functions.sh --env-file=/tmp/dojo-environment-dojo--2020-10-09_13-05-03-26891746 -ti --name=dojo--2020-10-09_13-05-03-26891746 xdivby0/mynode:0.1.0
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x4ccbba]

goroutine 4 [running]:
os.(*ProcessState).sys(...)
        /usr/local/go/src/os/exec_posix.go:82
os.(*ProcessState).Sys(...)
        /usr/local/go/src/os/exec.go:159
main.BashShellService.RunInteractive(0xc000094180, 0xc000102080, 0x8, 0x8, 0xc00013a000, 0x1a8, 0xc000010501, 0x4e6d60, 0xc000010550)
        /dojo/work/src/dojo/shell.go:65 +0x16a
main.DockerDriver.HandleRun(0x537680, 0xc0000ac040, 0x538020, 0xc000094180, 0xc000094180, 0x511949, 0x3, 0x512144, 0x8, 0x511da8, ...)
        /dojo/work/src/dojo/docker_driver.go:90 +0x6f4
main.main.func1(0x537a80, 0xc0000ae180, 0xc0000c8000, 0xc0000bc210, 0x22, 0xc0000ac080, 0xc0000de0e0)
        /dojo/work/src/dojo/main.go:106 +0xce
created by main.main
        /dojo/work/src/dojo/main.go:104 +0x66f
tomzo commented 3 years ago

Hi @xdivby0

Thanks for reporting the problem in great detail. 👍

There are 2 parts to it:

  1. The Dojo is crashing which should never happen and it's a bug to fix.
  2. At the moment, we don't support running Dojo as root, which is why you are seeing the WARN logs about directories being owned by root. The reasoning behind this is to limit the access of what CI agent can do to itself.

For now, I'd recommend that you tweak your gitlab script to not run dojo as root. I've used the following snippet in the past to run dojo on gitlab:

image: docker:latest

services:
  - docker:dind

before_script:
  - apk add make shadow bash sudo
  - export DOJO_VERSION=0.10.0
  - wget -O dojo https://github.com/kudulab/dojo/releases/download/${DOJO_VERSION}/dojo_linux_amd64
  - chmod +x dojo
  - mv dojo /usr/bin
  - useradd --home-dir /home/ci --shell /bin/bash ci
  - usermod -a -G ci ci
  - mkdir -p /home/ci
  - chown ci:ci /home/ci
  - chown ci:ci -R .

stages:
  - build

build:
  stage: build
  script:
    - sudo -EH -u ci bash -c 'dojo "<command to run in dojo>"'

Let me know if this helps.

xdivby0 commented 3 years ago

@tomzo I can confirm that using your gitlab yml works. Thank you very much! I am not sure wether I should click close with comment or only comment, since the actual bug is not resolved but my problem is solved. Feel free to close.

For anyone stumbling over this: For <command to run in dojo> I had to put in dojo "npm i", not npm i.

tomzo commented 3 years ago

Thanks for confirmation.

We'll keep the issue open as the crash still needs to be fixed.

xmik commented 3 years ago

Hi @xdivby0. Your reproduction helped a lot! The problem was actually that Dojo requires Bash. This info was written in the readme, but this readme is quite long, so this info is easily overlooked.

@tomzo, great that you had a working gitlab yaml. I would only suggest to not use the latest tag of docker images. But, I'm happy that the yaml helped.

Definitely, there should be no crash, like the one presented in the first message of this issue. There should be a pretty error printed, informing that Bash should be installed. This will be fixed in Dojo 0.10.1 by this PR. Thanks @xdivby0 for finding that problem. Now it will be easier to successfully run Dojo :)

xmik commented 3 years ago

Summary:


Proof: let's try running Dojo in alpine:3.9, which does not have Bash (or even Docker) installed.

$ docker run -ti --rm -v ${PWD}/bin/dojo:/usr/bin/dojo alpine:3.9 
/ # whoami
root
/ # /usr/bin/dojo
2020/10/25 14:09:58 [ 1]  INFO: (main.main) Dojo version 0.10.1
2020/10/25 14:09:58 [ 1] ERROR: (main.verifyBashInstalled) Error while verifying if Bash is installed. Please make sure Bash is installed. Error: exec: "bash": executable file not found in $PATH
/ # echo $?
1
/ # exit