-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_github.sh~
More file actions
executable file
·208 lines (178 loc) · 6.77 KB
/
Copy pathsetup_github.sh~
File metadata and controls
executable file
·208 lines (178 loc) · 6.77 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
# Input arguments
email=${1:-"kitfrasertaliente@gmail.com"}
name=${2:-"Kit FT"}
github_url=${3:-""}
# Define persistent SSH directory for RunPod
PERSISTENT_SSH_DIR="/workspace/kitf/.ssh"
HOME_SSH_DIR="$HOME/.ssh"
echo "Current user: $(whoami)"
echo "HOME directory: $HOME"
# Check if running in RunPod environment
if [ -d "/workspace/kitf" ]; then
echo "📁 RunPod environment detected. Using persistent storage at /workspace/kitf/"
# Add GH_CONFIG_DIR to shell configs for persistence
export GH_CONFIG_DIR="/workspace/kitf/.config/gh"
# Add to .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
if ! grep -q "export GH_CONFIG_DIR=" "$HOME/.bashrc"; then
echo 'export GH_CONFIG_DIR="/workspace/kitf/.config/gh"' >> "$HOME/.bashrc"
fi
fi
# Add to .zshrc if it exists
if [ -f "$HOME/.zshrc" ]; then
if ! grep -q "export GH_CONFIG_DIR=" "$HOME/.zshrc"; then
echo 'export GH_CONFIG_DIR="/workspace/kitf/.config/gh"' >> "$HOME/.zshrc"
fi
fi
if [ -f "/workspace/kitf/dotfiles/config/zshrc.sh" ]; then
if ! grep -q "export GH_CONFIG_DIR=" "/workspace/kitf/dotfiles/config/zshrc.sh"; then
echo 'export GH_CONFIG_DIR="/workspace/kitf/.config/gh"' >> "/workspace/kitf/dotfiles/config/zshrc.sh"
fi
fi
echo "ℹ️ Added GH_CONFIG_DIR to shell configs."
echo "ℹ️ To use gh in this session, run: export GH_CONFIG_DIR=\"/workspace/kitf/.config/gh\""
fi
# 0) Setup git
git config --global user.email "$email"
git config --global user.name "$name"
# 1) Setup GitHub CLI authentication FIRST
echo "Setting up GitHub CLI authentication..."
if command -v gh &> /dev/null; then
mkdir -p "$GH_CONFIG_DIR"
if gh auth status > /dev/null 2>&1; then
echo "✅ Already authenticated with GitHub CLI!"
else
echo "🔐 Logging in with GitHub CLI..."
gh auth login
if gh auth status > /dev/null 2>&1; then
echo "✅ Successfully authenticated with GitHub!"
else
echo "❌ GitHub authentication failed."
fi
fi
# Configure GitHub CLI to use SSH
gh config set git_protocol ssh
else
echo "❌ GitHub CLI not found. Please install it first."
fi
# 2) Handle SSH keys
mkdir -p "$PERSISTENT_SSH_DIR"
mkdir -p "$HOME_SSH_DIR"
PERSISTENT_KEY_PATH="$PERSISTENT_SSH_DIR/id_ed25519"
HOME_KEY_PATH="$HOME_SSH_DIR/id_ed25519"
ROOT_KEY_PATH="/root/.ssh/id_ed25519"
echo "Checking for SSH keys in:"
echo " - Persistent: $PERSISTENT_SSH_DIR"
echo " - Home: $HOME_SSH_DIR"
echo " - Root: /root/.ssh"
# Check all possible locations for existing keys
key_found=false
if [ -f "$PERSISTENT_KEY_PATH" ]; then
echo "✅ Found existing SSH key in persistent storage"
if [ ! -f "$HOME_KEY_PATH" ]; then
cp "$PERSISTENT_KEY_PATH" "$HOME_KEY_PATH"
cp "$PERSISTENT_KEY_PATH.pub" "$HOME_KEY_PATH.pub"
chmod 600 "$HOME_KEY_PATH"
chmod 644 "$HOME_KEY_PATH.pub"
fi
key_found=true
elif [ -f "$HOME_KEY_PATH" ]; then
echo "✅ Found existing SSH key in home directory"
if [ ! -f "$PERSISTENT_KEY_PATH" ] && [ -d "/workspace/kitf" ]; then
cp "$HOME_KEY_PATH" "$PERSISTENT_KEY_PATH"
cp "$HOME_KEY_PATH.pub" "$PERSISTENT_KEY_PATH.pub"
chmod 600 "$PERSISTENT_KEY_PATH"
chmod 644 "$PERSISTENT_KEY_PATH.pub"
fi
key_found=true
elif [ -f "$ROOT_KEY_PATH" ] && [ "$HOME" != "/root" ]; then
echo "✅ Found existing SSH key in /root/.ssh"
# Copy from root to persistent and home
cp "$ROOT_KEY_PATH" "$HOME_KEY_PATH"
cp "$ROOT_KEY_PATH.pub" "$HOME_KEY_PATH.pub"
chmod 600 "$HOME_KEY_PATH"
chmod 644 "$HOME_KEY_PATH.pub"
if [ -d "/workspace/kitf" ]; then
cp "$ROOT_KEY_PATH" "$PERSISTENT_KEY_PATH"
cp "$ROOT_KEY_PATH.pub" "$PERSISTENT_KEY_PATH.pub"
chmod 600 "$PERSISTENT_KEY_PATH"
chmod 644 "$PERSISTENT_KEY_PATH.pub"
fi
key_found=true
fi
# If no key found, create one automatically
if [ "$key_found" = false ]; then
echo "No SSH key found. Creating new SSH key for GitHub..."
# Generate key in persistent location if available, otherwise in home
if [ -d "/workspace/kitf" ]; then
target_key="$PERSISTENT_KEY_PATH"
else
target_key="$HOME_KEY_PATH"
fi
ssh-keygen -t ed25519 -C "$email" -f "$target_key" -N ""
# Copy to both locations
if [ "$target_key" = "$PERSISTENT_KEY_PATH" ] && [ ! -f "$HOME_KEY_PATH" ]; then
cp "$PERSISTENT_KEY_PATH" "$HOME_KEY_PATH"
cp "$PERSISTENT_KEY_PATH.pub" "$HOME_KEY_PATH.pub"
chmod 600 "$HOME_KEY_PATH"
chmod 644 "$HOME_KEY_PATH.pub"
elif [ "$target_key" = "$HOME_KEY_PATH" ] && [ -d "/workspace/kitf" ]; then
cp "$HOME_KEY_PATH" "$PERSISTENT_KEY_PATH"
cp "$HOME_KEY_PATH.pub" "$PERSISTENT_KEY_PATH.pub"
chmod 600 "$PERSISTENT_KEY_PATH"
chmod 644 "$PERSISTENT_KEY_PATH.pub"
fi
echo "📋 Your NEW SSH public key:"
cat "${target_key}.pub"
echo ""
echo "⚠️ Add this key to https://github.com/settings/keys"
echo "Press Enter when you've added the key to GitHub..."
read -r
fi
# Start SSH agent and add key if it exists
if [ -f "$HOME_KEY_PATH" ]; then
eval "$(ssh-agent -s)"
ssh-add "$HOME_KEY_PATH"
# Test connection
echo "Testing SSH connection to GitHub..."
ssh -T git@github.com -o StrictHostKeyChecking=no || true
fi
# 2) Project specific setup (if github_url is provided)
if [ -n "$github_url" ]; then
# If authenticated with GitHub CLI, use it for cloning
if command -v gh &> /dev/null && gh auth status > /dev/null 2>&1; then
repo_name=$(basename "$github_url" .git)
org_repo=${github_url#*github.com/}
org_repo=${org_repo%.git}
echo "Cloning $org_repo using GitHub CLI..."
gh repo clone "$org_repo" || git clone "$github_url"
else
git clone "$github_url"
fi
repo_name=$(basename "$github_url" .git)
if [ -d "$repo_name" ]; then
cd "$repo_name"
if [ -f "requirements.txt" ]; then
echo "Installing requirements..."
if command -v uv &> /dev/null; then
uv pip install -r requirements.txt
else
pip install -r requirements.txt
fi
else
echo "No requirements.txt found."
fi
else
echo "Failed to clone repository."
fi
fi
echo "setting credential store"
git config --global credential.helper store
echo "GitHub setup complete!"
if [ -d "/workspace/kitf" ]; then
echo ""
echo "⚠️ IMPORTANT: To use GitHub CLI in this session, run:"
echo " export GH_CONFIG_DIR=\"/workspace/kitf/.config/gh\""
echo "Or start a new shell session for the changes to take effect."
fi