Skip to content

Commit

Permalink
Fix workspace sync
Browse files Browse the repository at this point in the history
  • Loading branch information
robballantyne committed Nov 1, 2023
1 parent dffef82 commit 0e9aa88
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 41 deletions.
1 change: 1 addition & 0 deletions build/COPY_ROOT/opt/ai-dock/bin/build/layer0/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ $APT_INSTALL \
dos2unix \
fuse3 \
git \
git-lfs \
gpg \
jq \
less \
Expand Down
56 changes: 30 additions & 26 deletions build/COPY_ROOT/opt/ai-dock/bin/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ function init_cleanup() {

function init_main() {
init_set_envs "$@"
init_cloud_context
init_set_ssh_keys
init_set_web_credentials
init_create_directories
init_create_logfiles
init_set_ssh_keys
init_set_web_credentials
init_cloud_context
init_set_workspace
init_count_gpus
init_count_quicktunnels
Expand Down Expand Up @@ -113,16 +113,21 @@ function init_set_ssh_keys() {

init_set_web_credentials() {
if [[ -z $WEB_USER ]]; then
export WEB_USER=user
WEB_USER=user
fi

if [[ -z $WEB_PASSWORD_HASH ]]; then
if [[ -z $WEB_PASSWORD ]]; then
WEB_PASSWORD=password
fi
export WEB_PASSWORD_HASH=$(hash-password.sh -p $WEB_PASSWORD -r 15)
export WEB_PASSWORD="******"

if [[ -z $WEB_PASSWORD && -z $WEB_PASSWORD_HASH ]]; then
WEB_PASSWORD=password
elif [[ -z $WEB_PASSWORD ]]; then
WEB_PASSWORD="********"
fi

if [[ $WEB_PASSWORD != "********" ]]; then
WEB_PASSWORD_HASH=$(hash-password.sh -p $WEB_PASSWORD -r 15)
export WEB_PASSWORD="********"
fi

printf "%s %s" "$WEB_USER" "$WEB_PASSWORD_HASH" > /opt/caddy/etc/basicauth
}

function init_count_gpus() {
Expand Down Expand Up @@ -189,7 +194,7 @@ function init_sync_mamba_envs() {
if [[ ${SERVERLESS,,} != 'true' ]]; then
printf "Moving mamba environments to ${WORKSPACE}...\n"
rm -rf ${WORKSPACE}micromamba
rsync -az --info=progress2 /opt/micromamba ${WORKSPACE} && \
rsync -az --info=progress2 /opt/micromamba "${WORKSPACE}" >> /var/log/sync.log 2>&1 && \
rm -rf /opt/micromamba/* && \
printf 1 > ${WORKSPACE}micromamba/.move_complete && \
link-mamba-envs.sh
Expand All @@ -198,16 +203,15 @@ function init_sync_mamba_envs() {
}

init_sync_opt() {
IFS=: read -r -d '' -a path_array < <(printf '/opt/%s:\0' "$OPT_SYNC")
IFS=: read -r -d '' -a path_array < <(printf '%s:\0' "$OPT_SYNC")
for item in "${path_array[@]}"; do
dir="$(basename $item)"
if [[ ! -d $item || $dir = 'opt' ]]; then
opt_dir="/opt/${item}"
if [[ ! -d $opt_dir || $opt_dir = "/opt/" ]]; then
continue
fi

ws_dir=${WORKSPACE}${dir}
ws_dir=${WORKSPACE}${item}
ws_backup_link=${ws_dir}-link
opt_dir="/opt/${dir}"

# Restarting stopped container
if [[ -d $ws_dir && -L $opt_dir && ${WORKSPACE_SYNC,,} != "false" ]]; then
Expand All @@ -216,8 +220,8 @@ init_sync_opt() {
fi

# Reset symlinks first
if [[ -L $opt_dir ]]; then rm $opt_dir; fi
if [[ -L $ws_dir ]]; then rm $ws_dir ${ws_dir}-link; fi
if [[ -L $opt_dir ]]; then rm "$opt_dir"; fi
if [[ -L $ws_dir ]]; then rm "$ws_dir" "${ws_dir}-link"; fi

# Sanity check
# User broke something - Container requires tear-down & restart
Expand All @@ -232,16 +236,16 @@ init_sync_opt() {
if [[ -d $ws_dir && -f $ws_dir/.move_complete ]]; then
# Delete the container copy
if [[ -d $opt_dir && ! -L $opt_dir ]]; then
rm -rf ${opt_dir}
rm -rf "$opt_dir"
fi
# No/incomplete workspace copy
else
# Complete the copy if not serverless
if [[ ${SERVERLESS,,} != 'true' ]]; then
printf "Moving %s to %s\n" $opt_dir $ws_dir
rsync -az --info=progress2 $opt_dir $WORKSPACE && \
rsync -az --info=progress2 "$opt_dir" "$WORKSPACE" >> /var/log/sync.log 2>&1 && \
printf 1 > $ws_dir/.move_complete && \
rm -rf $opt_dir
rm -rf "$opt_dir"
fi
fi
fi
Expand All @@ -250,15 +254,15 @@ init_sync_opt() {
# Use container version over existing workspace version
if [[ -d $opt_dir && -d $ws_dir ]]; then
printf "Ignoring %s and creating symlink to %s at %s\n" $ws_dir $opt_dir $ws_backup_link
ln -s $opt_dir $ws_backup_link
ln -s "$opt_dir" "$ws_backup_link"
# Use container version
elif [[ -d $opt_dir ]]; then
printf "Creating symlink to %s at %s\n" $opt_dir $ws_dir
ln -s $opt_dir $ws_dir
ln -s "$opt_dir" "$ws_dir"
# Use workspace version
elif [[ -d $ws_dir ]]; then
printf "Creating symlink to %s at %s\n" $ws_dir $opt_dir
ln -s $ws_dir $opt_dir
ln -s "$ws_dir" "$opt_dir"
fi
done
}
Expand All @@ -276,7 +280,6 @@ init_set_workspace_permissions() {
fi
}


function init_set_cf_tunnel_wanted() {
if [[ -n $CF_TUNNEL_TOKEN ]]; then
export SUPERVISOR_START_CLOUDFLARED=1
Expand Down Expand Up @@ -328,6 +331,7 @@ function init_cloud_context() {

function init_create_directories() {
mkdir -p /run/http_ports
mkdir -p /opt/caddy/etc
}

# Ensure the files logtail needs to display during init
Expand Down
15 changes: 15 additions & 0 deletions build/COPY_ROOT/opt/ai-dock/bin/set-web-credentials.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

if [[ -z $2 ]]; then
printf "Usage: set-web-credentials.sh username password\n"
exit 1
fi

WEB_USER=$1
WEB_PASSWORD_HASH=$(hash-password.sh -p $2 -r 15)
export WEB_PASSWORD="********"

printf "Setting credentials and restarting proxy server...\n"
printf "%s %s" "$WEB_USER" "$WEB_PASSWORD_HASH" > /opt/caddy/etc/basicauth
supervisorctl restart caddy

1 change: 0 additions & 1 deletion build/COPY_ROOT/opt/ai-dock/bin/supervisor-caddy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function start() {
sleep 2
port_files="/run/http_ports/*"

mkdir -p /opt/caddy/etc/
cp -f /opt/caddy/share/base_config /opt/caddy/etc/Caddyfile

for service in $port_files; do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@
<div id="stripe">
<div class="one-row">
<ul id=github-buttons>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}" data-icon="octicon-star" data-show-count="true" aria-label="Star ai-dock/{{ context.urlslug }} on GitHub">Star</a></li>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}/issues" data-icon="octicon-issue-opened" data-show-count="true" aria-label="Issue ai-dock/{{ context.urlslug }} on GitHub">Issues</a></li>
<li><a class="github-button" href="https://github.com/orgs/ai-dock/discussions" data-icon="octicon-comment-discussion" aria-label="Discuss ai-dock on GitHub">Discuss</a></li>
<li><a class="github-button" href="https://github.com/sponsors/ai-dock" data-icon="octicon-heart" aria-label="Sponsor @ai-dock on GitHub">Sponsor</a></li>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}" target="_blank" "octicon-star" data-show-count="true" aria-label="Star ai-dock/{{ context.urlslug }} on GitHub">Star</a></li>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}/issues" target="_blank" data-icon="octicon-issue-opened" data-show-count="true" aria-label="Issue ai-dock/{{ context.urlslug }} on GitHub">Issues</a></li>
<li><a class="github-button" href="https://github.com/orgs/ai-dock/discussions" target="_blank" data-icon="octicon-comment-discussion" aria-label="Discuss ai-dock on GitHub">Discuss</a></li>
<li><a class="github-button" href="https://github.com/sponsors/ai-dock" target="_blank" data-icon="octicon-heart" aria-label="Sponsor @ai-dock on GitHub">Sponsor</a></li>
</ul>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@
<div id="stripe">
<div class="one-row">
<ul id=github-buttons>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}" data-icon="octicon-star" data-show-count="true" aria-label="Star ai-dock/{{ context.urlslug }} on GitHub">Star</a></li>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}/issues" data-icon="octicon-issue-opened" data-show-count="true" aria-label="Issue ai-dock/{{ context.urlslug }} on GitHub">Issues</a></li>
<li><a class="github-button" href="https://github.com/orgs/ai-dock/discussions" data-icon="octicon-comment-discussion" aria-label="Discuss ai-dock on GitHub">Discuss</a></li>
<li><a class="github-button" href="https://github.com/sponsors/ai-dock" data-icon="octicon-heart" aria-label="Sponsor @ai-dock on GitHub">Sponsor</a></li>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}" target="_blank" data-icon="octicon-star" data-show-count="true" aria-label="Star ai-dock/{{ context.urlslug }} on GitHub">Star</a></li>
<li><a class="github-button" href="https://github.com/ai-dock/{{ context.urlslug }}/issues" target="_blank" data-icon="octicon-issue-opened" data-show-count="true" aria-label="Issue ai-dock/{{ context.urlslug }} on GitHub">Issues</a></li>
<li><a class="github-button" href="https://github.com/orgs/ai-dock/discussions" target="_blank" data-icon="octicon-comment-discussion" aria-label="Discuss ai-dock on GitHub">Discuss</a></li>
<li><a class="github-button" href="https://github.com/sponsors/ai-dock" target="_blank" data-icon="octicon-heart" aria-label="Sponsor @ai-dock on GitHub">Sponsor</a></li>
</ul>
</div>
</div>
Expand Down Expand Up @@ -193,13 +193,13 @@ <h1 id="status">Service List</h1>
<div>
<ul>
{% if context.namedtunnels == True %}
<li><i class="fa-solid fa-fw fa-link"></i> <a href="/namedtunnel/{{ service["proxy_port"] }}">Cloudflare Named Tunnel</a></li>
<li><i class="fa-solid fa-fw fa-link"></i> <a href="/namedtunnel/{{ service["proxy_port"] }}" target="_blank">Cloudflare Named Tunnel</a></li>
{% endif %}
{% if context.quicktunnels == True %}
<li><i class="fa-solid fa-fw fa-link"></i> <a href="/quicktunnel/{{ service["proxy_port"] }}">Cloudflare Quick Tunnel</a></li>
<li><i class="fa-solid fa-fw fa-link"></i> <a href="/quicktunnel/{{ service["proxy_port"] }}" target="_blank">Cloudflare Quick Tunnel</a></li>
{% endif %}
{% if context.cloud != "paperspace.com" %}
<li><i class="fa-solid fa-fw fa-link"></i> <a href="/direct/{{ service["proxy_port"] }}">Direct/Default</a></li>
<li><i class="fa-solid fa-fw fa-link"></i> <a href="/direct/{{ service["proxy_port"] }}" target="_blank">Direct/Default</a></li>
{% endif %}
</ul>
</div>
Expand Down
4 changes: 2 additions & 2 deletions build/COPY_ROOT/opt/caddy/share/base_config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
auto_https off
auto_https off
log {
level warn
}
Expand All @@ -14,7 +14,7 @@

:88 {
basicauth * {
{$WEB_USER} {$WEB_PASSWORD_HASH}
import /opt/caddy/etc/basicauth
}
}

Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ENV SHELL="/bin/bash"
ENV DEBIAN_FRONTEND=noninteractive
ENV PATH=/opt/ai-dock/bin:/opt/micromamba/bin:/opt/caddy/bin:$PATH
ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
ENV OPT_SYNC=serverless
ENV OPT_SYNC=
ENV PYTHONUNBUFFERED=true
ENV APT_INSTALL="apt-get install -y --no-install-recommends"
ENV PIP_INSTALL="pip install --no-cache-dir"
Expand Down

0 comments on commit 0e9aa88

Please sign in to comment.