-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_remote.sh
executable file
·105 lines (87 loc) · 2.67 KB
/
setup_remote.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
set -e
set -o pipefail
# 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
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)
port=$(echo "$ssh_command" | awk '{print $4}')
# Update ~/.ssh/config
config_file="$HOME/.ssh/config"
temp_file=$(mktemp)
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 " remote_name " config"
print "Host " remote_name
print " HostName " host
print " Port " port
print " User root"
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 prepare_env.sh on the remote system
ssh -p "$port" "root@$host" << EOF
set -e
set -o pipefail
# Set git username and email based on local configuration
echo "Setting git user.name to $local_name and user.email to $local_email"
git config --global user.name "$local_name"
git config --global user.email "$local_email"
# Add GitHub's SSH key to known_hosts
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
echo "Remote setup completed successfully!"