react-native-dockerized/dockman.sh

163 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# Find the project root directory (i.e., the directory containing .dockerizedroot)
dir=$(pwd)
while [ "$dir" != "" ] && [ ! -f "$dir/.dockerizedroot" ]; do
dir=${dir%/*}
done
# Source the .env file
if [ "$dir" != "" ]; then
echo "Sourcing .env file from $dir"
source "$dir/.env"
fi
# Validate the container name
if [[ -z "${CONTAINER_NAME}" ]]; then
echo "Please set the CONTAINER_NAME environment variable before running the script."
exit 1
fi
# Define the project directory
PROJECT_DIR="/home/$RN_PROJECT_FOLDER_NAME"
# Docker command handling
case $1 in
up)
# List of required environment variables
REQUIRED_VARS=("SSH_KEY" "GH_TOKEN" "NPM_TOKEN" "NODE_VERSION" "RN_PROJECT_FOLDER_NAME" "CONTAINER_NAME")
for VAR in ${REQUIRED_VARS[@]}; do
if [[ -z "${!VAR}" ]]; then
echo "Error: Required environment variable $VAR is not set"
exit 1
fi
done
# If we get here, all variables are set
docker-compose up
;;
stop-metro)
docker exec -it ${CONTAINER_NAME} supervisorctl stop npm
;;
start-metro)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npm install" || { echo 'npm install failed' ; exit 1; }
docker exec ${CONTAINER_NAME} supervisorctl start npm
docker logs -f ${CONTAINER_NAME}
;;
metro-logs)
echo "Press CTRL + C to safely exit. It won't stop the server."
docker exec -it ${CONTAINER_NAME} supervisorctl tail -f npm stdout
;;
connect-android)
docker exec ${CONTAINER_NAME} adb connect host.docker.internal
;;
run-android)
docker exec ${CONTAINER_NAME} adb connect host.docker.internal
docker exec -ti ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npx react-native run-android"
;;
reinstall-node-modules)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && rm -rf node_modules && npm install" || { echo 'npm install failed' ; exit 1; }
;;
shell)
docker exec -it ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && exec /bin/bash"
;;
init-ssh)
if [[ -z "${SSH_KEY}" ]]; then
echo "Please set the SSH_KEY environment variable before running the script."
exit 1
fi
SSH_KEY_PATH="$HOME/.ssh/id_docker_dev"
# generate ssh key
if [ ! -f "${SSH_KEY_PATH}" ]; then
ssh-keygen -t ed25519 -f ${SSH_KEY_PATH} -N ""
echo "SSH key generated at ${SSH_KEY_PATH}"
else
echo "SSH key already exists at ${SSH_KEY_PATH}"
fi
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo "Docker is not running. Please start Docker and try again."
exit 1
fi
# Check if the specific container is running
if [ "$(docker inspect -f '{{.State.Running}}' ${CONTAINER_NAME})" != "true" ]; then
echo "The container ${CONTAINER_NAME} does not exist or is not running."
exit 1
fi
# get docker container's IP
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
# check if the docker IP is already in the ssh config
if grep -q "${CONTAINER_IP}" ~/.ssh/config
then
echo "SSH config already contains the Docker IP."
else
echo "Adding Docker IP to SSH config..."
echo -e "Host ${CONTAINER_IP}\n\tPort 22\n\tUser root\n\tPubkeyAuthentication yes\n\tIdentityFile ${SSH_KEY_PATH}" >> ~/.ssh/config
echo "Docker IP added to SSH config."
fi
;;
clear-known-hosts)
echo "This will clear known hosts."
# EUID may not be available in all environments, instead you can use id -u to get the user ID
if [ "$(id -u)" -ne 0 ]
then
echo "Please run as root or use sudo"
exit 1
fi
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
# Define known_hosts and temporary file path
KNOWN_HOSTS_PATH="$HOME/.ssh/known_hosts"
TEMP_FILE="$HOME/.ssh/temp"
# Check if the .ssh directory exists and is writable
if [ ! -d "$(dirname ${KNOWN_HOSTS_PATH})" ] || [ ! -w "$(dirname ${KNOWN_HOSTS_PATH})" ]; then
echo "The .ssh directory does not exist or is not writable"
exit 1
fi
# Check if the known_hosts file exists, if not, exit early as there's nothing to clear
if [ ! -f ${KNOWN_HOSTS_PATH} ]; then
echo "The known_hosts file does not exist. Nothing to clear."
exit 0
fi
# Create a temporary file and remove the line with CONTAINER_IP. If grep doesn't find a match, it will still create an empty file.
grep -v "${CONTAINER_IP}" ${KNOWN_HOSTS_PATH} > ${TEMP_FILE} || true
mv ${TEMP_FILE} ${KNOWN_HOSTS_PATH}
;;
*)
echo "Invalid command"
echo "Usage: $0 {stop-metro|start-metro|metro-logs|connect-android|run-android|reinstall-node-modules|shell|init-ssh|clear-known-hosts}"
;;
esac