Skip to content

Commit

Permalink
Improve run container scripting and add initial readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Jun 11, 2024
1 parent 70922c9 commit 247744a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 50 deletions.
46 changes: 46 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# Apache Celix Development Container

## Intro

TODO add intro, explain difference build, dev images and apt/conan variant.

## VSCode Usage

Support for development containers in VSCode is built-in. Just startup VSCode using the celix workspace folder,
and you will be prompted to open the workspace in a container.

VSCode will ensure that your host .gitconfig file, .gnupg dir, and ssh agent forwarding is available in the container.

## CLion Usage

Support for devcontainers in CLion is not built-in and currently not fully supported.
For CLion you can instead use remote development via ssh.
The `.devcontainer/run-dev-container.sh` script can be used to start a container with sshd running and interactively
setup .gitconfig, .gnupg, and ssh agent forwarding.
Before `.devcontainer/run-dev-container.sh` can be used, a `celix-conan-dev` image must be build.

```bash
cd ${CELIX_ROOT}
./.devcontainer/build-dev-container.sh
./.devcontainer/run-dev-container.sh
ssh -p 2233 celixdev@localhost
```

In Clion open the Remote Development window using "File -> Remote Development ..." and add a new configuration.
5 changes: 2 additions & 3 deletions .devcontainer/build-container-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Build a Celix dev container with all needed dependencies pre-installed.

SCRIPT_LOCATION=$(realpath "$(dirname "${BASH_SOURCE[0]}")")
CELIX_REPO_ROOT=$(realpath "${SCRIPT_LOCATION}/..")
IMAGE_TARGET="${1:-conan-dev}"

# Check which container engine is available.
# Check for podman first, because the 'podman-docker' package might be installed providing a dummy 'docker' command.
Expand All @@ -31,4 +30,4 @@ else
fi

cd "${SCRIPT_LOCATION}"
${CONTAINER_ENGINE} build -t apache/celix-dev:ubuntu-latest -f Containerfile.ubuntu .
${CONTAINER_ENGINE} build -t apache/celix-${IMAGE_TARGET} -f Containerfile --target ${IMAGE_TARGET} .
113 changes: 66 additions & 47 deletions .devcontainer/run-dev-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CELIX_REPO_ROOT=$(realpath "${SCRIPT_LOCATION}/..")

CONTAINER_COMMAND_DEFAULT="sudo /usr/sbin/sshd -D -e -p 2233"
CONTAINER_COMMAND=${1:-${CONTAINER_COMMAND_DEFAULT}}
CONTAINER_NAME="celixdev"

# Check which container engine is available.
# Check for podman first, because the 'podman-docker' package might be installed providing a dummy 'docker' command.
Expand All @@ -34,37 +35,63 @@ else
CONTAINER_ENGINE="docker"
fi

# Check if container celix-dev already exists
if [ "$(${CONTAINER_ENGINE} ps -a --format '{{.Names}}' | grep celix-dev)" ]; then
echo "Container 'celix-dev' already exists. Do you want to remove it?"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Removing container celix-dev"; ${CONTAINER_ENGINE} rm -f celix-dev; break;;
No ) exit;;
esac
done
echo ""
fi
ask_and_execute() {
local QUESTION="$1"
local FUNC_NAME="$2"

ADDITIONAL_ARGS=""
echo "Do you want to mount the .ssh directory to the container (as an overlayfs)?"
select yn in "Yes" "No"; do
# Prompt the user with the question
while true; do
read -p "${QUESTION} (yes/no): " yn
case $yn in
Yes ) echo "Add .ssh directory mount arguments"; ADDITIONAL_ARGS="--volume ${HOME}/.ssh:/home/celixdev/.ssh:O"; break;;
No ) break;;
[Yy]* )
# Call the provided function if the answer is yes
${FUNC_NAME}
echo ""
break;;
[Nn]* )
# Exit if the answer is no
break;;
* )
echo "Please answer yes or no.";;
esac
done
echo ""
done
}

remove_celixdev_container() {
echo "Removing container '${CONTAINER_NAME}'"
${CONTAINER_ENGINE} rm -f ${CONTAINER_NAME}
}
# Check if container celixdev already exists
if [ "$(${CONTAINER_ENGINE} ps -a --format '{{.Names}}' | grep ${CONTAINER_NAME})" ]; then
ask_and_execute "Container '${CONTAINER_NAME}' already exists. Do you want to remove it?" remove_celixdev_container
fi

ADDITIONAL_ARGS=""

add_gnupg_mount() {
echo "Adding .gnupg directory mount arguments"
echo "TODO does not work yet, maybe a gpg agent can be used instead"
ADDITIONAL_ARGS="--volume ${HOME}/.gnupg:/home/celixdev/.gnupg:O"
}
if [ -e "${HOME}/.gnupg" ]; then
echo "Do you want to mount the .gnupg directory to the container (as an overlayfs)?"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Add .gnupg directory mount arguments"; ADDITIONAL_ARGS="--volume ${HOME}/.gnupg:/home/celixdev/.gnupg:O"; break;;
No ) break;;
esac
done
echo ""
ask_and_execute "Do you want to mount the .gnupg directory to the container (as an overlayfs)?" add_gnupg_mount
fi

add_gitconfig_mount() {
echo "Adding .gitconfig file mount arguments"
ADDITIONAL_ARGS="--volume ${HOME}/.gitconfig:/home/celixdev/.gitconfig:ro"
}
if [ -e "${HOME}/.gitconfig" ]; then
ask_and_execute "Do you want to mount the .gitconfig file to the container (as read-only)?" add_gitconfig_mount
fi

add_sshagent_forward()
{
echo "Adding SSH agent forwarding"
ADDITIONAL_ARGS="${ADDITIONAL_ARGS} --volume ${SSH_AUTH_SOCK}:/ssh-agent --env SSH_AUTH_SOCK=/ssh-agent"
}
if [ -n "${SSH_AUTH_SOCK}" ]; then
ask_and_execute "Do you want to forward the SSH agent to the container?" add_sshagent_forward
fi

# Start a container with all the Celix dependencies pre-installed
Expand All @@ -74,10 +101,10 @@ fi
# --volume & --workdir are set to the Celix repo root (to allow building and editing of the Celix repo)
# --security-opt disables SELinux for the container
# -d runs the container in detached mode
echo "Starting container 'celix-dev' with command: ${CONTAINER_COMMAND}"
echo "Starting container '${CONTAINER_NAME}' with command: ${CONTAINER_COMMAND}"
${CONTAINER_ENGINE} run -it --rm --privileged -d \
--name celix-dev \
--userns keep-id \
--name ${CONTAINER_NAME} \
--userns=keep-id \
--net=host \
${ADDITIONAL_ARGS} \
--volume "${CELIX_REPO_ROOT}":"${CELIX_REPO_ROOT}" \
Expand All @@ -86,24 +113,16 @@ ${CONTAINER_ENGINE} run -it --rm --privileged -d \
apache/celix-conan-dev:latest bash -c "${CONTAINER_COMMAND}"
echo ""

echo "Do you want to setup the git user and email in the container?"
USER_NAME=$(git config user.name)
USER_EMAIL=$(git config user.email)
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Setting up git user and email"; ${CONTAINER_ENGINE} exec celix-dev bash -c "git config --global user.email '${USER_EMAIL}' && git config --global user.name '${USER_NAME}'"; break;;
No ) break;;
esac
done
echo ""
#copy_ssh_id() {
# echo "Copying ssh key (password: celixdev)"
# ssh-copy-id -p 2233 celixdev@localhost
#}
#ask_and_execute "Do you want to copy the ssh key to the container?" copy_ssh_id

echo "Do you want to copy the ssh key to the container?"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Copying ssh key (password: celixdev)"; ssh-copy-id -p 2233 celixdev@localhost; break;;
No ) break;;
esac
done
echo ""
build_conan_deps() {
echo "Building Celix dependencies with Conan"
${CONTAINER_ENGINE} exec -it ${CONTAINER_NAME} bash -c "cd ${CELIX_REPO_ROOT} && .devcontainer/setup-project-with-conan.sh"
}
ask_and_execute "Do you want to build Celix dependencies with Conan?" build_conan_deps

echo "Done."

0 comments on commit 247744a

Please sign in to comment.