add generate and read code from claude
This commit is contained in:
39
git-sync-steps.txt
Normal file
39
git-sync-steps.txt
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
Clone fork
|
||||||
|
Using git clone command
|
||||||
|
after cloning, cd into the cloned project, check if .npmrc exists. If not pause the script, ask user to add it, continue after pause
|
||||||
|
Before running setup-docker.sh script, we need to copy docker env file
|
||||||
|
inside of the project, we have this path .docker/config/.env.local, we need to copy it to project root .env file
|
||||||
|
run setup-docker.sh
|
||||||
|
Inside of cloned project, should be docker-setup.sh script, if it exists, we need to run it with sh command
|
||||||
|
If it doesn't exist, pause and notify the user, the same like we did in clone fork step
|
||||||
|
From the server copy .env -> .server.env
|
||||||
|
Using scp command, copy .env file from the server project directory, into .server.env
|
||||||
|
Mark .server.env as assume unchanged in the git repo
|
||||||
|
Modify current local .env file based on the .sever.env
|
||||||
|
We need to add APP_NAME="" from .server.env into the current .env file
|
||||||
|
Add APP_KEY from .server.env into the .env file
|
||||||
|
Create .env.prod.local from the server.env and host and user
|
||||||
|
We need following variables in the .env.prod.local
|
||||||
|
PROD_HOST="" Get this from users initial input
|
||||||
|
PROD_USER="" Get this from users initial input
|
||||||
|
PROD_DB_NAME="" Get this from .server.env (DB_DATABASE)
|
||||||
|
PROD_DB_USER="" Get this from .server.env (DB_USERNAME)
|
||||||
|
PROD_DB_PASS="" Get this from .server.env (DB_PASSWORD)
|
||||||
|
Add few helper scripts (mark them in gitignore)
|
||||||
|
I need ssh.sh script added into local project directory
|
||||||
|
Add it the same way we are doing it in sync-project.sh, by having array of scripts, then outputing variables, and then outputing the script itself
|
||||||
|
Upload lazygit to server, so that I can use ./ssh.sh --git (it opens lazygit on the server, for merging)
|
||||||
|
Now we need to add sshkey to the server
|
||||||
|
First we need to check if server has public_key and private_key already created on the server (~/.ssh/id_rsa and ~/.ssh/id_rsa.pub)
|
||||||
|
If the server has only public key, then we need to create our own private key
|
||||||
|
Create keys in our ~/.ssh/<PROJECT_NAME>
|
||||||
|
Then upload private and public key to the server, overwriting id_rsa.pub
|
||||||
|
Add our newely created id_rsa.pub to the authorised_hosts file
|
||||||
|
If server has private and public key already setup
|
||||||
|
We need to make sure id_rsa.pub is already inside of authorized_keys file
|
||||||
|
If its not then we add it there
|
||||||
|
Copy private key to our local machine into ~/.ssh/<PROJECT_NAME>
|
||||||
|
Any errors should appear in the console, with clear instructions of what happened, and what needs to be done so the script can continue. Pause the script so user can solve the issue
|
||||||
|
After that we need to use ssh-add command for our synced private key, so that we can procceed with the next step
|
||||||
|
After that we need to run php artisan db:wipe inside of running docker container
|
||||||
|
After that we can use make db-sync command to pull database from staging server
|
||||||
44
shared.sh
44
shared.sh
@@ -11,3 +11,47 @@ NC='\033[0m' # No Color
|
|||||||
|
|
||||||
# SSH options - auto-accept new host keys
|
# SSH options - auto-accept new host keys
|
||||||
SSH_OPTS="-o StrictHostKeyChecking=accept-new"
|
SSH_OPTS="-o StrictHostKeyChecking=accept-new"
|
||||||
|
|
||||||
|
# Pause and wait for user to resolve an issue
|
||||||
|
pause_for_user() {
|
||||||
|
echo -e "${YELLOW}$1${NC}"
|
||||||
|
read -p "Press Enter to continue after resolving..."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Extract value from .env file
|
||||||
|
# Usage: get_env_value <file> <key>
|
||||||
|
get_env_value() {
|
||||||
|
local file="$1"
|
||||||
|
local key="$2"
|
||||||
|
grep "^${key}=" "$file" 2>/dev/null | head -1 | cut -d'=' -f2- | tr -d '"' | tr -d "'"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add or update value in .env file
|
||||||
|
# Usage: set_env_value <file> <key> <value>
|
||||||
|
set_env_value() {
|
||||||
|
local file="$1"
|
||||||
|
local key="$2"
|
||||||
|
local value="$3"
|
||||||
|
if grep -q "^${key}=" "$file" 2>/dev/null; then
|
||||||
|
sed -i "s|^${key}=.*|${key}=\"${value}\"|" "$file"
|
||||||
|
else
|
||||||
|
echo "${key}=\"${value}\"" >> "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add entry to .gitignore if not already present
|
||||||
|
# Usage: add_to_gitignore <entry>
|
||||||
|
add_to_gitignore() {
|
||||||
|
local entry="$1"
|
||||||
|
if [ -f .gitignore ]; then
|
||||||
|
if ! grep -q "^${entry}$" .gitignore 2>/dev/null; then
|
||||||
|
echo "$entry" >> .gitignore
|
||||||
|
echo -e "${BLUE}Added $entry to .gitignore${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}$entry already in .gitignore${NC}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "$entry" > .gitignore
|
||||||
|
echo -e "${BLUE}Created .gitignore with $entry${NC}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|||||||
394
sync-git-project.sh
Normal file → Executable file
394
sync-git-project.sh
Normal file → Executable file
@@ -1,7 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# This script is used to clone a project from a remote server
|
# This script is used to clone a project from a remote server
|
||||||
# and setup lazygit for remote git operations.
|
# and perform full project setup including Docker, environment,
|
||||||
|
# SSH keys, and database synchronization.
|
||||||
# Called by sync-project.sh with --git flag.
|
# Called by sync-project.sh with --git flag.
|
||||||
|
|
||||||
# Get current script real directory (follow symlinks)
|
# Get current script real directory (follow symlinks)
|
||||||
@@ -23,33 +24,227 @@ if [ -z "$SERVER_HOST" ] || [ -z "$SSH_PASSWORD" ] || [ -z "$SERVER_PROJECT_PATH
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Clone repository
|
# Extract username and host from SERVER_HOST
|
||||||
echo "Cloning repository..."
|
USERNAME="${SERVER_HOST%@*}"
|
||||||
|
HOST="${SERVER_HOST#*@}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 1: Clone Fork
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 1: Cloning Repository ===${NC}"
|
||||||
|
|
||||||
GIT_SSH_COMMAND="sshpass -p '${SSH_PASSWORD}' ssh $SSH_OPTS" \
|
GIT_SSH_COMMAND="sshpass -p '${SSH_PASSWORD}' ssh $SSH_OPTS" \
|
||||||
git clone ${SERVER_HOST}:${SERVER_PROJECT_PATH} "${LOCAL_FOLDER}"
|
git clone ${SERVER_HOST}:${SERVER_PROJECT_PATH} "${LOCAL_FOLDER}"
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo -e "${RED}Failed to clone repository${NC}"
|
echo -e "${RED}Failed to clone repository${NC}"
|
||||||
|
echo -e "${YELLOW}Check that:${NC}"
|
||||||
|
echo " - SSH credentials are correct"
|
||||||
|
echo " - Repository exists at ${SERVER_PROJECT_PATH}"
|
||||||
|
echo " - You have access to the server"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "${GREEN}Repository cloned successfully${NC}"
|
echo -e "${GREEN}Repository cloned successfully${NC}"
|
||||||
|
|
||||||
# Extract username and host from SERVER_HOST
|
# Change into project directory
|
||||||
USERNAME="${SERVER_HOST%@*}"
|
|
||||||
HOST="${SERVER_HOST#*@}"
|
|
||||||
|
|
||||||
cd "${LOCAL_FOLDER}"
|
cd "${LOCAL_FOLDER}"
|
||||||
|
|
||||||
# Upload lazygit to server
|
# Check for .npmrc
|
||||||
echo "Uploading lazygit to server..."
|
if [ ! -f ".npmrc" ]; then
|
||||||
|
pause_for_user ".npmrc file not found! Please add .npmrc file to the project root before continuing."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 1 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 2: Copy Docker Env File
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 2: Copying Docker Env File ===${NC}"
|
||||||
|
|
||||||
|
if [ -f ".docker/config/.env.local" ]; then
|
||||||
|
cp ".docker/config/.env.local" ".env"
|
||||||
|
echo -e "${GREEN}.env copied from .docker/config/.env.local${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${RED}.docker/config/.env.local not found!${NC}"
|
||||||
|
pause_for_user "Please ensure .docker/config/.env.local exists before continuing."
|
||||||
|
|
||||||
|
# Re-check after user resolves
|
||||||
|
if [ -f ".docker/config/.env.local" ]; then
|
||||||
|
cp ".docker/config/.env.local" ".env"
|
||||||
|
echo -e "${GREEN}.env copied from .docker/config/.env.local${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${RED}Still no .docker/config/.env.local - continuing without it${NC}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 2 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 3: Clean Docker Volumes
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 3: Cleaning Docker Volumes ===${NC}"
|
||||||
|
|
||||||
|
echo "Stopping all containers..."
|
||||||
|
docker stop $(docker ps -aq) 2>/dev/null || true
|
||||||
|
|
||||||
|
echo "Removing all containers..."
|
||||||
|
docker rm $(docker ps -aq) 2>/dev/null || true
|
||||||
|
|
||||||
|
echo "Removing all volumes..."
|
||||||
|
docker volume rm $(docker volume ls -q) 2>/dev/null || true
|
||||||
|
|
||||||
|
echo -e "${GREEN}Docker volumes cleaned${NC}"
|
||||||
|
echo -e "${GREEN}Step 3 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 4: Run docker-setup.sh
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 4: Running docker-setup.sh ===${NC}"
|
||||||
|
|
||||||
|
if [ -f "docker-setup.sh" ]; then
|
||||||
|
echo "Running docker-setup.sh..."
|
||||||
|
sh docker-setup.sh
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${YELLOW}docker-setup.sh exited with non-zero status${NC}"
|
||||||
|
pause_for_user "Please check docker-setup.sh output and resolve any issues."
|
||||||
|
else
|
||||||
|
echo -e "${GREEN}docker-setup.sh completed successfully${NC}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}docker-setup.sh not found in project!${NC}"
|
||||||
|
pause_for_user "No docker-setup.sh found. Please set up Docker manually if needed."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 4 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 5: Copy Server .env
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 5: Copying Server .env ===${NC}"
|
||||||
|
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS ${SERVER_HOST}:${SERVER_PROJECT_PATH}/.env .server.env
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${RED}Failed to copy server .env file${NC}"
|
||||||
|
pause_for_user "Please ensure .env exists on server at ${SERVER_PROJECT_PATH}/.env"
|
||||||
|
|
||||||
|
# Retry after user resolves
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS ${SERVER_HOST}:${SERVER_PROJECT_PATH}/.env .server.env
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${RED}Still cannot copy server .env - continuing without it${NC}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f ".server.env" ]; then
|
||||||
|
git update-index --assume-unchanged .server.env 2>/dev/null || true
|
||||||
|
echo -e "${GREEN}Server .env copied to .server.env and marked as assume-unchanged${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 5 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 6: Modify Local .env
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 6: Modifying Local .env ===${NC}"
|
||||||
|
|
||||||
|
if [ -f ".server.env" ] && [ -f ".env" ]; then
|
||||||
|
# Extract values from server env
|
||||||
|
APP_NAME=$(get_env_value ".server.env" "APP_NAME")
|
||||||
|
APP_KEY=$(get_env_value ".server.env" "APP_KEY")
|
||||||
|
|
||||||
|
if [ -n "$APP_NAME" ]; then
|
||||||
|
set_env_value ".env" "APP_NAME" "$APP_NAME"
|
||||||
|
echo -e "${GREEN}Set APP_NAME=${APP_NAME}${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}APP_NAME not found in .server.env${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$APP_KEY" ]; then
|
||||||
|
set_env_value ".env" "APP_KEY" "$APP_KEY"
|
||||||
|
echo -e "${GREEN}Set APP_KEY${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}APP_KEY not found in .server.env${NC}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}Skipping .env modification - missing .server.env or .env${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 6 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 7: Create .env.prod.local
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 7: Creating .env.prod.local ===${NC}"
|
||||||
|
|
||||||
|
if [ -f ".server.env" ]; then
|
||||||
|
# Extract database credentials from server env
|
||||||
|
DB_DATABASE=$(get_env_value ".server.env" "DB_DATABASE")
|
||||||
|
DB_USERNAME=$(get_env_value ".server.env" "DB_USERNAME")
|
||||||
|
DB_PASSWORD=$(get_env_value ".server.env" "DB_PASSWORD")
|
||||||
|
|
||||||
|
cat > .env.prod.local << EOF
|
||||||
|
PROD_HOST="${HOST}"
|
||||||
|
PROD_USER="${USERNAME}"
|
||||||
|
PROD_DB_NAME="${DB_DATABASE}"
|
||||||
|
PROD_DB_USER="${DB_USERNAME}"
|
||||||
|
PROD_DB_PASS="${DB_PASSWORD}"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
echo -e "${GREEN}.env.prod.local created with production credentials${NC}"
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}Skipping .env.prod.local creation - .server.env not found${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 7 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 8: Add Helper Scripts
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 8: Adding Helper Scripts ===${NC}"
|
||||||
|
|
||||||
|
# Define scripts to generate
|
||||||
|
scripts=("ssh.sh")
|
||||||
|
|
||||||
|
for script in "${scripts[@]}"; do
|
||||||
|
# Create script with variables header
|
||||||
|
cat > "${script}" << EOF
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
SERVER_USER="${USERNAME}"
|
||||||
|
SERVER_HOST="${HOST}"
|
||||||
|
SERVER_PASSWORD="${SSH_PASSWORD}"
|
||||||
|
SERVER_PATH="${SERVER_PROJECT_PATH}/"
|
||||||
|
REMOTE_PROGRESS=".remote-in-progress"
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Append the template content
|
||||||
|
cat "${SCRIPT_DIR}/scripts/${script}" >> "${script}"
|
||||||
|
|
||||||
|
# Make executable
|
||||||
|
chmod +x "${script}"
|
||||||
|
|
||||||
|
echo -e "${GREEN}${script} created successfully${NC}"
|
||||||
|
|
||||||
|
# Add to .gitignore
|
||||||
|
add_to_gitignore "${script}"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 8 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 9: Upload lazygit
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 9: Uploading lazygit ===${NC}"
|
||||||
|
|
||||||
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS "${SCRIPT_DIR}/lazygit" ${SERVER_HOST}:${SERVER_PROJECT_PATH}
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS "${SCRIPT_DIR}/lazygit" ${SERVER_HOST}:${SERVER_PROJECT_PATH}
|
||||||
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
echo -e "${RED}Failed to upload lazygit${NC}"
|
echo -e "${RED}Failed to upload lazygit${NC}"
|
||||||
exit 1
|
pause_for_user "Please check server connectivity and permissions."
|
||||||
fi
|
else
|
||||||
|
|
||||||
echo -e "${GREEN}lazygit uploaded successfully${NC}"
|
echo -e "${GREEN}lazygit uploaded successfully${NC}"
|
||||||
|
|
||||||
# Mark lazygit as assume-unchanged on server
|
# Mark lazygit as assume-unchanged on server
|
||||||
@@ -63,23 +258,168 @@ GIT_SSH_COMMAND="sshpass -p '${SSH_PASSWORD}' ssh $SSH_OPTS" git pull
|
|||||||
git update-index --assume-unchanged lazygit
|
git update-index --assume-unchanged lazygit
|
||||||
|
|
||||||
echo -e "${GREEN}lazygit configured successfully${NC}"
|
echo -e "${GREEN}lazygit configured successfully${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Generate ssh.sh script
|
echo -e "${GREEN}Step 9 complete${NC}"
|
||||||
echo "
|
|
||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Configuration
|
# ============================================================
|
||||||
SERVER_USER=\"${USERNAME}\"
|
# STEP 10: SSH Key Management
|
||||||
SERVER_HOST=\"${HOST}\"
|
# ============================================================
|
||||||
SERVER_PASSWORD=\"${SSH_PASSWORD}\"
|
echo -e "${BLUE}=== Step 10: SSH Key Management ===${NC}"
|
||||||
SERVER_PATH=\"${SERVER_PROJECT_PATH}/\"
|
|
||||||
REMOTE_PROGRESS=\".remote-in-progress\"
|
|
||||||
" > "ssh.sh"
|
|
||||||
|
|
||||||
# Append the ssh.sh template
|
# Check server state for SSH keys
|
||||||
cat "${SCRIPT_DIR}/scripts/ssh.sh" >> "ssh.sh"
|
HAS_PRIVATE=$(sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "test -f ~/.ssh/id_rsa && echo 'yes' || echo 'no'")
|
||||||
echo -e "${GREEN}ssh.sh created successfully${NC}"
|
HAS_PUBLIC=$(sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "test -f ~/.ssh/id_rsa.pub && echo 'yes' || echo 'no'")
|
||||||
|
|
||||||
|
echo "Server SSH key state: private=$HAS_PRIVATE, public=$HAS_PUBLIC"
|
||||||
|
|
||||||
|
if [ "$HAS_PRIVATE" = "no" ] && [ "$HAS_PUBLIC" = "yes" ]; then
|
||||||
|
# Case A: Server has ONLY public key (no private)
|
||||||
|
echo -e "${YELLOW}Case A: Server has only public key - generating new keypair${NC}"
|
||||||
|
|
||||||
|
# Generate new keypair locally
|
||||||
|
ssh-keygen -t rsa -b 4096 -f staging.key -N "" -q
|
||||||
|
|
||||||
|
# Upload both keys to server
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS staging.key ${SERVER_HOST}:~/.ssh/id_rsa
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS staging.key.pub ${SERVER_HOST}:~/.ssh/id_rsa.pub
|
||||||
|
|
||||||
|
# Set correct permissions on server
|
||||||
|
sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "chmod 600 ~/.ssh/id_rsa && chmod 644 ~/.ssh/id_rsa.pub"
|
||||||
|
|
||||||
|
# Add public key to authorized_keys
|
||||||
|
sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} \
|
||||||
|
"cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
|
||||||
|
|
||||||
|
# Clean up local public key
|
||||||
|
rm -f staging.key.pub
|
||||||
|
|
||||||
|
echo -e "${GREEN}New keypair generated and uploaded to server${NC}"
|
||||||
|
|
||||||
|
elif [ "$HAS_PRIVATE" = "yes" ] && [ "$HAS_PUBLIC" = "yes" ]; then
|
||||||
|
# Case B: Server has BOTH keys
|
||||||
|
echo -e "${YELLOW}Case B: Server has both keys - downloading private key${NC}"
|
||||||
|
|
||||||
|
# Check if public key is in authorized_keys
|
||||||
|
IN_AUTH=$(sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} \
|
||||||
|
"grep -qf ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys 2>/dev/null && echo 'yes' || echo 'no'")
|
||||||
|
|
||||||
|
if [ "$IN_AUTH" = "no" ]; then
|
||||||
|
echo "Adding public key to authorized_keys..."
|
||||||
|
sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} \
|
||||||
|
"cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Download private key
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS ${SERVER_HOST}:~/.ssh/id_rsa staging.key
|
||||||
|
chmod 600 staging.key
|
||||||
|
|
||||||
|
echo -e "${GREEN}Private key downloaded as staging.key${NC}"
|
||||||
|
|
||||||
|
else
|
||||||
|
# Case C: Server has NEITHER key (or only private, which is unusual)
|
||||||
|
echo -e "${YELLOW}Case C: Server missing keys - generating new keypair${NC}"
|
||||||
|
|
||||||
|
# Generate new keypair locally
|
||||||
|
ssh-keygen -t rsa -b 4096 -f staging.key -N "" -q
|
||||||
|
|
||||||
|
# Create .ssh directory on server if needed
|
||||||
|
sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "mkdir -p ~/.ssh && chmod 700 ~/.ssh"
|
||||||
|
|
||||||
|
# Upload both keys to server
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS staging.key ${SERVER_HOST}:~/.ssh/id_rsa
|
||||||
|
sshpass -p "${SSH_PASSWORD}" scp $SSH_OPTS staging.key.pub ${SERVER_HOST}:~/.ssh/id_rsa.pub
|
||||||
|
|
||||||
|
# Set correct permissions on server
|
||||||
|
sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} "chmod 600 ~/.ssh/id_rsa && chmod 644 ~/.ssh/id_rsa.pub"
|
||||||
|
|
||||||
|
# Add public key to authorized_keys (create if doesn't exist)
|
||||||
|
sshpass -p "${SSH_PASSWORD}" ssh $SSH_OPTS ${SERVER_HOST} \
|
||||||
|
"cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
|
||||||
|
|
||||||
|
# Clean up local public key
|
||||||
|
rm -f staging.key.pub
|
||||||
|
|
||||||
|
echo -e "${GREEN}New keypair generated and uploaded to server${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Add staging.key to .gitignore and mark as assume-unchanged
|
||||||
|
if [ -f "staging.key" ]; then
|
||||||
|
add_to_gitignore "staging.key"
|
||||||
|
git update-index --assume-unchanged staging.key 2>/dev/null || true
|
||||||
|
echo -e "${GREEN}staging.key marked as assume-unchanged${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 10 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 11: SSH-Add
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 11: Adding SSH Key to Agent ===${NC}"
|
||||||
|
|
||||||
|
if [ -f "staging.key" ]; then
|
||||||
|
ssh-add ./staging.key 2>/dev/null
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${YELLOW}Failed to add staging.key to SSH agent${NC}"
|
||||||
|
echo "This may happen if ssh-agent is not running."
|
||||||
|
echo "You can start it with: eval \$(ssh-agent -s)"
|
||||||
|
echo "Then run: ssh-add ./staging.key"
|
||||||
|
pause_for_user "Please ensure SSH agent is running and add the key manually if needed."
|
||||||
|
else
|
||||||
|
echo -e "${GREEN}staging.key added to SSH agent${NC}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo -e "${YELLOW}staging.key not found - skipping ssh-add${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 11 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# STEP 12: Database Setup
|
||||||
|
# ============================================================
|
||||||
|
echo -e "${BLUE}=== Step 12: Database Setup ===${NC}"
|
||||||
|
|
||||||
|
# Wipe local database
|
||||||
|
echo "Wiping local database..."
|
||||||
|
docker exec mtc-pharmacy-web php artisan db:wipe
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${YELLOW}Failed to wipe database${NC}"
|
||||||
|
echo "This may happen if Docker container is not running."
|
||||||
|
pause_for_user "Please ensure mtc-pharmacy-web container is running."
|
||||||
|
|
||||||
|
# Retry
|
||||||
|
docker exec mtc-pharmacy-web php artisan db:wipe
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Sync database from staging
|
||||||
|
echo "Syncing database from staging..."
|
||||||
|
make db-sync
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo -e "${YELLOW}make db-sync failed${NC}"
|
||||||
|
pause_for_user "Please check Makefile and database connection settings."
|
||||||
|
else
|
||||||
|
echo -e "${GREEN}Database synced successfully${NC}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}Step 12 complete${NC}"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# COMPLETE
|
||||||
|
# ============================================================
|
||||||
|
echo ""
|
||||||
|
echo -e "${GREEN}========================================${NC}"
|
||||||
|
echo -e "${GREEN} Project Setup Complete!${NC}"
|
||||||
|
echo -e "${GREEN}========================================${NC}"
|
||||||
|
echo ""
|
||||||
|
echo "Project cloned to: ${LOCAL_FOLDER}"
|
||||||
|
echo "SSH key: ./staging.key"
|
||||||
|
echo ""
|
||||||
|
echo "Available commands:"
|
||||||
|
echo " ./ssh.sh - SSH into server"
|
||||||
|
echo " ./ssh.sh --git - Run lazygit on server"
|
||||||
|
echo " ./ssh.sh --db - Forward database port"
|
||||||
|
echo ""
|
||||||
|
|
||||||
echo -e "${GREEN}Git clone complete!${NC}"
|
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
@@ -38,18 +38,6 @@ for arg in "$@"; do
|
|||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# Function to add entry to .gitignore if not already present
|
|
||||||
add_to_gitignore() {
|
|
||||||
local entry="$1"
|
|
||||||
cat .gitignore | grep "$entry" >/dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "$entry" >>.gitignore
|
|
||||||
echo -e "${BLUE}Added $entry to .gitignore${NC}"
|
|
||||||
else
|
|
||||||
echo -e "${YELLOW}$entry already is inside of .gitignore file${NC}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
echo -e "${BLUE}=== Project Sync Configuration ===${NC}"
|
echo -e "${BLUE}=== Project Sync Configuration ===${NC}"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
|
|||||||
333
verification.md
Normal file
333
verification.md
Normal file
@@ -0,0 +1,333 @@
|
|||||||
|
# Verification Steps for sync-git-project.sh
|
||||||
|
|
||||||
|
This document outlines testing steps and edge cases for verifying the enhanced `sync-git-project.sh` script.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
Before testing, ensure you have:
|
||||||
|
- A test server with SSH access
|
||||||
|
- A git repository on the server
|
||||||
|
- `sshpass` installed locally
|
||||||
|
- Docker installed and running
|
||||||
|
- `make` available
|
||||||
|
|
||||||
|
## Test Cases
|
||||||
|
|
||||||
|
### 1. Clone Test
|
||||||
|
**Purpose:** Verify git clone works with credentials
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run `./sync-project.sh --git`
|
||||||
|
2. Provide valid server credentials
|
||||||
|
3. Verify repository is cloned to LOCAL_FOLDER
|
||||||
|
|
||||||
|
**Expected:** Repository cloned successfully, cd into project works
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- Wrong SSH credentials → Should show error and exit
|
||||||
|
- Repository doesn't exist → Should show clear error message
|
||||||
|
- LOCAL_FOLDER already exists → Git will fail with appropriate error
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. .npmrc Check
|
||||||
|
**Purpose:** Test with/without .npmrc file
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Clone a project without .npmrc
|
||||||
|
2. Verify script pauses and prompts user
|
||||||
|
3. Add .npmrc file
|
||||||
|
4. Press Enter to continue
|
||||||
|
|
||||||
|
**Expected:** Script pauses with clear message, continues after file added
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. Docker Env Copy
|
||||||
|
**Purpose:** Verify .docker/config/.env.local exists and copies correctly
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Ensure .docker/config/.env.local exists in repo
|
||||||
|
2. Run setup
|
||||||
|
3. Verify .env created in project root
|
||||||
|
|
||||||
|
**Expected:** `.env` copied from `.docker/config/.env.local`
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- .docker/config/.env.local missing → Pause and prompt user
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. Clean Docker Volumes
|
||||||
|
**Purpose:** Verify Docker volumes are cleaned before setup
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Have existing Docker containers and volumes running
|
||||||
|
2. Run setup
|
||||||
|
3. Verify all containers stopped and removed
|
||||||
|
4. Verify all volumes removed
|
||||||
|
|
||||||
|
**Expected:**
|
||||||
|
- `docker ps -a` shows no containers
|
||||||
|
- `docker volume ls` shows no volumes
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- No containers running → Commands succeed silently
|
||||||
|
- No volumes exist → Commands succeed silently
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. Docker Setup
|
||||||
|
**Purpose:** Test with/without docker-setup.sh present
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Test with project containing docker-setup.sh
|
||||||
|
2. Test with project missing docker-setup.sh
|
||||||
|
|
||||||
|
**Expected:**
|
||||||
|
- With script: Runs `sh docker-setup.sh`
|
||||||
|
- Without script: Pauses and notifies user
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 6. Server .env Copy
|
||||||
|
**Purpose:** Verify scp copies .env to .server.env
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Ensure .env exists on server
|
||||||
|
2. Run setup
|
||||||
|
3. Verify .server.env created locally
|
||||||
|
4. Run `git ls-files -v | grep .server.env` to verify assume-unchanged
|
||||||
|
|
||||||
|
**Expected:** .server.env copied and marked with 'h' (assume-unchanged)
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- Server .env missing → Pause and prompt user
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 7. .env Modification
|
||||||
|
**Purpose:** Check APP_NAME/APP_KEY extraction and insertion
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Verify .server.env contains APP_NAME and APP_KEY
|
||||||
|
2. Run setup
|
||||||
|
3. Check local .env for updated values
|
||||||
|
|
||||||
|
**Expected:** APP_NAME and APP_KEY from .server.env appear in local .env
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- APP_NAME missing in .server.env → Yellow warning, continues
|
||||||
|
- APP_KEY missing in .server.env → Yellow warning, continues
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 8. .env.prod.local Creation
|
||||||
|
**Purpose:** Verify all 5 variables populated correctly
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run setup with valid server credentials
|
||||||
|
2. Check .env.prod.local contents
|
||||||
|
|
||||||
|
**Expected file contents:**
|
||||||
|
```
|
||||||
|
PROD_HOST="<extracted from SERVER_HOST>"
|
||||||
|
PROD_USER="<extracted from SERVER_HOST>"
|
||||||
|
PROD_DB_NAME="<DB_DATABASE from .server.env>"
|
||||||
|
PROD_DB_USER="<DB_USERNAME from .server.env>"
|
||||||
|
PROD_DB_PASS="<DB_PASSWORD from .server.env>"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 9. Helper Scripts
|
||||||
|
**Purpose:** Verify ssh.sh created correctly
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run setup
|
||||||
|
2. Check ssh.sh exists with executable permission
|
||||||
|
3. Verify ssh.sh contains correct variables
|
||||||
|
4. Check .gitignore includes ssh.sh
|
||||||
|
|
||||||
|
**Expected:** ssh.sh created with embedded credentials, added to .gitignore
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 10. Lazygit Upload
|
||||||
|
**Purpose:** Verify upload and assume-unchanged on both server and local
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run setup
|
||||||
|
2. SSH to server, check lazygit exists in project
|
||||||
|
3. On server: `git ls-files -v | grep lazygit` shows 'h'
|
||||||
|
4. Locally: `git ls-files -v | grep lazygit` shows 'h'
|
||||||
|
|
||||||
|
**Expected:** lazygit uploaded and marked assume-unchanged on both ends
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 11. SSH Keys - Case A
|
||||||
|
**Purpose:** Server with only public key
|
||||||
|
|
||||||
|
**Setup:**
|
||||||
|
```bash
|
||||||
|
# On server
|
||||||
|
rm ~/.ssh/id_rsa
|
||||||
|
# Keep ~/.ssh/id_rsa.pub
|
||||||
|
```
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run setup
|
||||||
|
2. Verify staging.key created locally
|
||||||
|
3. Verify both keys uploaded to server
|
||||||
|
4. Verify public key added to authorized_keys
|
||||||
|
|
||||||
|
**Expected:** New keypair generated, uploaded, authorized
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 12. SSH Keys - Case B
|
||||||
|
**Purpose:** Server with both keys
|
||||||
|
|
||||||
|
**Setup:**
|
||||||
|
```bash
|
||||||
|
# Server has both ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub
|
||||||
|
```
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run setup
|
||||||
|
2. Verify staging.key downloaded (matches server's id_rsa)
|
||||||
|
3. Verify public key in authorized_keys
|
||||||
|
|
||||||
|
**Expected:** Private key downloaded as staging.key
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 13. SSH Keys - Case C
|
||||||
|
**Purpose:** Server with neither key
|
||||||
|
|
||||||
|
**Setup:**
|
||||||
|
```bash
|
||||||
|
# On server
|
||||||
|
rm -rf ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
|
||||||
|
# or rm -rf ~/.ssh
|
||||||
|
```
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Run setup
|
||||||
|
2. Verify ~/.ssh created on server
|
||||||
|
3. Verify both keys uploaded
|
||||||
|
4. Verify authorized_keys created/updated
|
||||||
|
|
||||||
|
**Expected:** New keypair generated, ~/.ssh created, keys uploaded
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 14. staging.key Assume-Unchanged
|
||||||
|
**Purpose:** Verify git tracking status
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. After setup, run: `git ls-files -v | grep staging.key`
|
||||||
|
|
||||||
|
**Expected:** Output shows 'h staging.key' (lowercase h = assume-unchanged)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 15. SSH-Add
|
||||||
|
**Purpose:** Verify key added to agent
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Ensure ssh-agent is running: `eval $(ssh-agent -s)`
|
||||||
|
2. Run setup
|
||||||
|
3. Check: `ssh-add -l` should show staging.key
|
||||||
|
|
||||||
|
**Expected:** staging.key fingerprint shown in ssh-add -l
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- ssh-agent not running → Pause with instructions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 16. db:wipe
|
||||||
|
**Purpose:** Verify database wipe works
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Ensure mtc-pharmacy-web container is running
|
||||||
|
2. Run setup
|
||||||
|
3. Verify db:wipe command executes
|
||||||
|
|
||||||
|
**Expected:** Database wiped successfully
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- Container not running → Pause with instructions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 17. db-sync
|
||||||
|
**Purpose:** Verify make db-sync runs
|
||||||
|
|
||||||
|
**Steps:**
|
||||||
|
1. Ensure Makefile exists with db-sync target
|
||||||
|
2. Ensure .env.prod.local has correct values
|
||||||
|
3. Run setup
|
||||||
|
|
||||||
|
**Expected:** Database synced from staging
|
||||||
|
|
||||||
|
**Edge cases:**
|
||||||
|
- Makefile missing → Error from make
|
||||||
|
- db-sync target missing → Error from make
|
||||||
|
- Connection fails → Pause with instructions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Edge Cases Summary
|
||||||
|
|
||||||
|
| Scenario | Expected Behavior |
|
||||||
|
|----------|-------------------|
|
||||||
|
| Clone fails (wrong credentials) | Red error, exit |
|
||||||
|
| Clone fails (repo doesn't exist) | Red error, exit |
|
||||||
|
| .npmrc missing | Yellow pause, wait for user |
|
||||||
|
| .docker/config/.env.local missing | Red error, pause, retry |
|
||||||
|
| No Docker containers/volumes | Commands succeed silently |
|
||||||
|
| docker-setup.sh missing | Yellow pause, notify user |
|
||||||
|
| Server .env missing | Red error, pause, retry |
|
||||||
|
| .server.env malformed | get_env_value returns empty, yellow warning |
|
||||||
|
| ~/.ssh doesn't exist on server | Create directory (Case C) |
|
||||||
|
| authorized_keys doesn't exist | Create with public key |
|
||||||
|
| staging.key assume-unchanged fails | Suppress error, continue |
|
||||||
|
| ssh-agent not running | Yellow warning, instructions, pause |
|
||||||
|
| Docker container not running | Yellow warning, pause, retry |
|
||||||
|
| make db-sync fails | Yellow warning, pause |
|
||||||
|
|
||||||
|
## Manual Verification Checklist
|
||||||
|
|
||||||
|
- [ ] Clone test passes
|
||||||
|
- [ ] .npmrc check pauses correctly
|
||||||
|
- [ ] Docker env copied
|
||||||
|
- [ ] Docker volumes cleaned
|
||||||
|
- [ ] docker-setup.sh runs or notifies
|
||||||
|
- [ ] Server .env copied to .server.env
|
||||||
|
- [ ] .server.env marked assume-unchanged
|
||||||
|
- [ ] APP_NAME/APP_KEY extracted and set
|
||||||
|
- [ ] .env.prod.local has all 5 variables
|
||||||
|
- [ ] ssh.sh created with correct variables
|
||||||
|
- [ ] ssh.sh in .gitignore
|
||||||
|
- [ ] staging.key in .gitignore
|
||||||
|
- [ ] lazygit uploaded and assume-unchanged
|
||||||
|
- [ ] SSH key management works (test appropriate case)
|
||||||
|
- [ ] staging.key marked assume-unchanged
|
||||||
|
- [ ] ssh-add succeeds
|
||||||
|
- [ ] db:wipe runs
|
||||||
|
- [ ] db-sync runs
|
||||||
|
|
||||||
|
## Cleanup After Testing
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Remove test project locally
|
||||||
|
rm -rf LOCAL_FOLDER
|
||||||
|
|
||||||
|
# On server, if you modified SSH keys:
|
||||||
|
# Restore original keys or regenerate
|
||||||
|
|
||||||
|
# Remove any test databases
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user