forked from coder/deploy-code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
Β·129 lines (109 loc) Β· 4.72 KB
/
entrypoint.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
START_DIR="${START_DIR:-/home/coder/project}"
PREFIX="deploy-code-server"
mkdir -p $START_DIR
# function to clone the git repo or add a user's first file if no repo was specified.
project_init () {
echo "[$PREFIX] GIT_REPOS:${GIT_REPOS}"
if [ -z "${GIT_REPOS}" ]; then
echo "[$PREFIX] No GIT_REPOS specified. Creating a default file."
echo "Example file. Have questions? Join us at https://community.coder.com" > $START_DIR/coder.txt
else
echo "[$PREFIX] Cloning repositor(y|ies)"
IFS=' ' read -r -a repos <<< "${GIT_REPOS}"
for repo in "${repos[@]}"; do
# Check if GITHUB_PAT is set and use it in the repo URL if available
if [ -n "${GITHUB_PAT}" ]; then
repo_url=$(echo "$repo" | sed "s#https://#https://$GITHUB_PAT@#")
else
repo_url="$repo"
fi
repo_name=$(basename -s .git "$repo_url")
target_dir="$START_DIR/$repo_name"
echo "[$PREFIX] Cloning $repo_url into $target_dir"
git clone "$repo_url" "$target_dir"
done
fi
}
# add rclone config and start rclone, if supplied
if [[ -z "${RCLONE_DATA}" ]]; then
echo "[$PREFIX] RCLONE_DATA is not specified. Files will not persist"
# start the project
project_init
else
echo "[$PREFIX] Copying rclone config..."
mkdir -p /home/coder/.config/rclone/
touch /home/coder/.config/rclone/rclone.conf
echo $RCLONE_DATA | base64 -d > /home/coder/.config/rclone/rclone.conf
# default to true
RCLONE_VSCODE_TASKS="${RCLONE_VSCODE_TASKS:-true}"
RCLONE_AUTO_PUSH="${RCLONE_AUTO_PUSH:-true}"
RCLONE_AUTO_PULL="${RCLONE_AUTO_PULL:-true}"
if [ $RCLONE_VSCODE_TASKS = "true" ]; then
# copy our tasks config to VS Code
echo "[$PREFIX] Applying VS Code tasks for rclone"
cp /tmp/rclone-tasks.json /home/coder/.local/share/code-server/User/tasks.json
# install the extension to add to menu bar
code-server --install-extension actboy168.tasks&
else
# user specified they don't want to apply the tasks
echo "[$PREFIX] Skipping VS Code tasks for rclone"
fi
# Full path to the remote filesystem
RCLONE_REMOTE_PATH=${RCLONE_REMOTE_NAME:-code-server-remote}:${RCLONE_DESTINATION:-code-server-files}
RCLONE_SOURCE_PATH=${RCLONE_SOURCE:-$START_DIR}
echo "rclone sync $RCLONE_SOURCE_PATH $RCLONE_REMOTE_PATH $RCLONE_FLAGS -vv" > /home/coder/push_remote.sh
echo "rclone sync $RCLONE_REMOTE_PATH $RCLONE_SOURCE_PATH $RCLONE_FLAGS -vv" > /home/coder/pull_remote.sh
chmod +x push_remote.sh pull_remote.sh
echo "[$PREFIX] rclone config complete"
if [[ $(rclone ls $RCLONE_REMOTE_PATH) ]]; then
echo "[$PREFIX] remote path has files:"
rclone ls $RCLONE_REMOTE_PATH
if [ $RCLONE_AUTO_PULL = "true" ]; then
# grab the files from the remote instead of running project_init()
echo "[$PREFIX] Pulling existing files from remote..."
/home/coder/pull_remote.sh&
else
# user specified they don't want to apply the tasks
echo "[$PREFIX] Auto-pull is disabled"
fi
else
echo "[$PREFIX] remote path empty"
if [ $RCLONE_AUTO_PUSH = "true" ]; then
# we need to clone the git repo and sync
echo "[$PREFIX] Pushing initial files to remote..."
project_init
/home/coder/push_remote.sh&
else
# user specified they don't want to apply the tasks
echo "[$PREFIX] Auto-push is disabled"
fi
fi
fi
# Start continuous sync in the background when auto_push is true
# Note this seems to be crashing railway
# if [[ ! -z "${RCLONE_DATA}" && "${RCLONE_AUTO_PUSH}" == "true" ]]; then
# inotifywait -m -r -e modify,create,delete,move $RCLONE_SOURCE_PATH |
# while read -r directory events filename; do
# echo "Change detected: $events in $directory$filename"
# rclone sync $RCLONE_REMOTE_PATH $RCLONE_SOURCE_PATH $RCLONE_FLAGS
# done &
# fi
# Add dotfiles, if set
if [ -n "$DOTFILES_REPO" ]; then
# grab the files from the remote instead of running project_init()
echo "[$PREFIX] Cloning dotfiles..."
mkdir -p $HOME/dotfiles
git clone $DOTFILES_REPO $HOME/dotfiles
DOTFILES_SYMLINK="${RCLONE_AUTO_PULL:-true}"
# symlink repo to $HOME
if [ $DOTFILES_SYMLINK = "true" ]; then
shopt -s dotglob
ln -sf source_file $HOME/dotfiles/* $HOME
fi
# run install script, if it exists
[ -f "$HOME/dotfiles/install.sh" ] && $HOME/dotfiles/install.sh
fi
echo "[$PREFIX] Starting code-server..."
# Now we can run code-server with the default entrypoint
/usr/bin/entrypoint.sh --bind-addr 0.0.0.0:8080 $START_DIR