-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,46 @@ | |
set -e | ||
set -o pipefail | ||
|
||
if [ $# -ne 1 ]; then | ||
echo "Usage: $0 \"ssh_command\"" | ||
# Default remote name | ||
REMOTE_NAME="best-hn" | ||
|
||
# Function to display usage | ||
usage() { | ||
echo "Usage: $0 [--remote-name REMOTE_NAME] \"ssh_command\"" | ||
exit 1 | ||
} | ||
|
||
# Parse arguments | ||
if [ "$#" -lt 1 ]; then | ||
usage | ||
fi | ||
|
||
ssh_command="$1" | ||
while [[ "$#" -gt 0 ]]; do | ||
case "$1" in | ||
--remote-name|-n) | ||
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then | ||
REMOTE_NAME="$2" | ||
shift 2 | ||
else | ||
echo "Error: --remote-name requires a non-empty value." | ||
usage | ||
fi | ||
;; | ||
-*) | ||
echo "Unknown option: $1" | ||
usage | ||
;; | ||
*) | ||
ssh_command="$1" | ||
shift | ||
;; | ||
esac | ||
done | ||
|
||
if [ -z "$ssh_command" ]; then | ||
echo "Error: Missing ssh_command." | ||
usage | ||
fi | ||
|
||
# Extract relevant information from the SSH command | ||
host=$(echo "$ssh_command" | awk '{print $2}' | cut -d@ -f2) | ||
|
@@ -18,29 +52,35 @@ port=$(echo "$ssh_command" | awk '{print $4}') | |
config_file="$HOME/.ssh/config" | ||
temp_file=$(mktemp) | ||
|
||
awk ' | ||
/^# START best-hn config$/,/^# END best-hn config$/ {next} | ||
{print} | ||
awk -v remote_name="$REMOTE_NAME" -v host="$host" -v port="$port" ' | ||
BEGIN { in_block = 0 } | ||
/^# START/ && $0 ~ remote_name { in_block = 1; next } | ||
/^# END/ && $0 ~ remote_name { in_block = 0; next } | ||
!in_block { print } | ||
END { | ||
print "# START best-hn config" | ||
print "Host best-hn" | ||
print " HostName '"$host"'" | ||
print " Port '"$port"'" | ||
print "# START " remote_name " config" | ||
print "Host " remote_name | ||
print " HostName " host | ||
print " Port " port | ||
print " User root" | ||
print "# END best-hn config" | ||
print "# END " remote_name " config" | ||
} | ||
' "$config_file" > "$temp_file" | ||
|
||
mv "$temp_file" "$config_file" | ||
chmod 600 "$config_file" | ||
|
||
# Copy remote_lowtrust keypair to the remote system and rename to id_rsa | ||
scp -P "$port" ~/.ssh/remote_lowtrust "root@$host:~/.ssh/id_rsa" | ||
scp -P "$port" ~/.ssh/remote_lowtrust.pub "root@$host:~/.ssh/id_rsa.pub" | ||
|
||
# Copy the local .env file to the remote repository | ||
scp .env "${REMOTE_NAME}:/workspace/.env" | ||
|
||
local_name="$(git config --get user.name)" | ||
local_email="$(git config --get user.email)" | ||
|
||
# Clone the repository and run install_deps.sh on the remote system | ||
# Clone the repository and run prepare_env.sh on the remote system | ||
ssh -p "$port" "root@$host" << EOF | ||
set -e | ||
set -o pipefail | ||
|
@@ -54,15 +94,12 @@ ssh -p "$port" "root@$host" << EOF | |
mkdir -p ~/.ssh | ||
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts | ||
# Clone the repository | ||
mkdir -p /workspace | ||
cd /workspace | ||
GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no" git clone [email protected]:OpenPipe/best-hn.git | ||
cd best-hn/ | ||
./prepare_env.sh | ||
EOF | ||
|
||
# Copy the local .env file to the remote repository | ||
scp .env "best-hn:/workspace/.env" | ||
|
||
echo "Remote setup completed successfully!" | ||
echo "Remote setup completed successfully!" |