react-native-dockerized/dockman.sh

149 lines
4.8 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/app"
# Docker command handling
case $1 in
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 "echo \"//github.com/:_authToken=$GH_TOKEN\" > ${PROJECT_DIR}/.npmrc"
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npm install" || { echo 'npm install failed' ; exit 1; }
docker exec ${CONTAINER_NAME} /bin/bash -c "rm -f ${PROJECT_DIR}/.npmrc"
docker exec ${CONTAINER_NAME} supervisorctl start npm -- --reset-cache
docker logs -f ${CONTAINER_NAME}
;;
metro-logs)
docker exec -it ${CONTAINER_NAME} cat /var/log/app/npm.log
;;
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
# generate ssh key
if [ ! -f "${SSH_KEY}" ]; then
ssh-keygen -t ed25519 -f "${SSH_KEY}" -N ""
echo "SSH key generated at ${SSH_KEY}"
else
echo "SSH key already exists at ${SSH_KEY}"
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}" >> ~/.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