Skip to content

Instantly share code, notes, and snippets.

@artkpv
Last active November 5, 2024 16:03
Show Gist options
  • Save artkpv/c5ae0dd8609a934ca0a1fc64df8269ad to your computer and use it in GitHub Desktop.
Save artkpv/c5ae0dd8609a934ca0a1fc64df8269ad to your computer and use it in GitHub Desktop.
Provision at vast.ai
#!/bin/bash
# Expected to run on vastai instance start.
env | grep _ >> /etc/environment # To use env for non shell apps
echo 'starting up'
# git config --global credential.helper 'cache --timeout=172800' # 48 hours
# git config --global --add user.name ArtyomK
# git config --global --add user.email [email protected]
#!/bin/bash
# Buys a machine with GPUs at vast.ai.
# Published at https://gist.github.com/artkpv/c5ae0dd8609a934ca0a1fc64df8269ad
usage() {
cat <<-_EOF_
Usage:
1) vastai_create --filter <vast ai filter>
Creates an instance and provisions it.
2) vastai_create <instance id>
Provisions an instance with ID.
vast ai filter:
- dph - dollar per hour
- gpu_ram - VRAM
_EOF_
}
[[ "$1" =~ -h|--help || $# -eq 0 || $# -gt 2 ]] && { usage; exit 1 ; }
find_instance_id() {
filter=""
if [[ "$1" == "--filter" && -n "$2" ]] ; then
filter="$2"
fi
search_str="reliability > 0.8 inet_down >= 800.0 num_gpus=1 disk_space >= 110 duration >= 1 cuda_vers >= 12.0 ${filter} "
echo "$search_str":
instances=$( vastai search offers "$search_str" -o dlperf_usd )
echo "$instances"
IID=$( echo "$instances" | tail -1 | cut -d ' ' -f 1 )
echo "Enter ID for the instance to create ($IID)?"
read -r ans
[[ "$ans" =~ ^[0-9]+$ ]] && IID=$ans
return
}
provision() {
create_instance
create_environment
}
create_instance() {
[[ -n "$IID" ]] || { echo 'Need IID' ; exit 1 ; }
res=$( vastai create instance "$IID" \
--image "pytorch/pytorch:2.2.0-cuda12.1-cudnn8-devel" \
--disk 180 \
--ssh \
--onstart ~/bin/vastai_onstart.sh \
--env "-e DATA_DIRECTORY=/workspace/ -e JUPYTER_DIR=/ -e GITHUB_TOKEN=$GITHUB_TOKEN -e HF_TOKEN=$HF_TOKEN -e WANDB_API_KEY=$WANDB_API_KEY -e OPENAI_API_KEY=$OPENAI_API_KEY"
)
[[ $? -eq 0 ]] || { echo 'Failed to create' ; exit 1 ; }
echo "$res"
INS_ID=$( echo "$res" | grep -o "[0-9]\+" )
echo "$INS_ID" | xclip -selection clipboard
return
}
create_environment() {
[[ -n "$INS_ID" ]] || { echo 'Need INS_ID' ; exit 1 ; }
echo 'Waiting for the instance to be running...'
while ! ( vastai show instances | grep "${INS_ID}.*running" > /dev/null ) ; do
sleep 1
done
echo 'Getting ssh url'
SSHURL=$( vastai ssh-url "$INS_ID" )
echo "$SSHURL"
echo "$SSHURL" | xclip -selection clipboard
SCPURL="${SSHURL/ssh/scp}"
scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ~/bin/vastai_create_env.sh "${SCPURL}//workspace/vastai_create_env.sh"
# ( sleep 15 ; code --folder-uri "vscode-remote://ssh-remote+${SSHURL#ssh://}/workspace/rl_for_steg" ; ) &
echo "Done. Next run /workspace/vastai_create_env.sh"
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$SSHURL" -tt '$SHELL -i ' <<- _EOF_
cd /workspace
( . ./vastai_create_env.sh ; )
exit 0
_EOF_
return
}
if [[ $# -eq 2 ]] ; then
find_instance_id "$@"
provision
elif [[ $# -eq 1 ]] ; then
INS_ID=$1
create_environment
fi
#!/bin/bash
# Creates dev environment
echo Installing and updating packages...
apt-get --yes update
packages=(
bat
dstat
entr
fzf
git
git-annex
git-lfs
git-repair
htop
make
neovim
netbase
nnn
sudo
tmux
tree
zsh
zoxide
# python
#git-delta
#lf
#tree-sitter-cli
#z
#zsh-completions
# fzf-tab-git
# git-filter-repo
# oh-my-zsh-git
)
apt install --yes "${packages[@]}"
dotfiles() {
/usr/bin/git --git-dir="$HOME/.dotfiles/" --work-tree="$HOME" "$@"
}
if [[ ! -e $HOME/.dotfiles ]] ; then
echo 'Installing dotfiles...'
DOTFILES=https://oauth2:${GITHUB_TOKEN}@github.com/artkpv/dotfiles.git
cd "$HOME" || exit 1
git clone --bare "$DOTFILES" "$HOME/.dotfiles"
for file in $( dotfiles checkout 2>&1 | grep -E "\s+\." | cut -f 2 ) ; do
echo "Backing up $file"
mkdir --parents ".dotfiles-backup/$( dirname "$file" )"
mv "$file" ".dotfiles-backup/$file"
done
dotfiles checkout bare_main
dotfiles config --local status.showUntrackedFiles no
dotfiles submodule init
dotfiles submodule update
echo 'dotfiles installed'
fi
function create_repo()
{
URL=$1
DIR=$2
BRANCH=$3
[[ -e $DIR ]] || {
echo "Creating repo: $URL ($BRANCH) at $DIR "
git clone --branch "$BRANCH" --recurse-submodules "$URL" "$DIR" ;
}
cd "$DIR" || { echo 'Failed to clone' ; exit 1; }
python -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install -r requirements.txt
pip install vastai
}
create_repo "https://oauth2:${GITHUB_TOKEN}@github.com/artkpv/rl_for_steganography" /workspace/rl_for_steg dev/direct-stego
#[[ ! $SHELL =~ zsh ]] && chsh -s /usr/bin/zsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment