Skip to content

Commit

Permalink
added many improvements and some helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
frankcorneliusmartin committed Jan 10, 2024
1 parent b327c16 commit 13157ec
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ This repository contains convenience scripts for installing vantage6 nodes at da
## TODO

- [ ] Autmaticaly after installation?
- [ ] Add `start-node.sh` and `stop-node.sh` scripts
- [ ] Add a job to the crontab to start the node on boot
- [ ] Add option to use Docker version of the OMOP database (no SSH tunneling required)
- [ ] Add check that we are indeed using the bash shell
- [ ] Update this README

## Getting Started
Expand Down
3 changes: 3 additions & 0 deletions attach.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash
SCRIPT_DIR=$(realpath "$(dirname "$0")")
source $SCRIPT_DIR/node.sh attach blueberry
13 changes: 13 additions & 0 deletions create-cronjob.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Get the directory of the current script
SCRIPT_DIR=$(dirname "$(realpath "$0")")
source $SCRIPT_DIR/utils.sh

# Add a cron job that runs the start.sh script at startup
print_step "Adding cron job"
if ! check_command "crontab"; then
print_error "Failed to add cron job."
else
(crontab -l 2>/dev/null; echo "@reboot $SCRIPT_DIR/start.sh") | crontab -
fi
2 changes: 1 addition & 1 deletion create-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ print_step "TUNNEL_REMOTE_PORT: $TUNNEL_REMOTE_PORT"
# # Create the config file
print_step "Creating the config file"
mkdir -p $HOME/.config/vantage6/node
CONFIG_FILE=$HOME/.config/vantage6/node/blueberry.yml
CONFIG_FILE=$HOME/.config/vantage6/node/blueberry.yaml

if [ -f "$HOME/.config/vantage6/node/blueberry.yml" ]; then
print_warning "Config file already exists at $CONFIG_FILE"
Expand Down
20 changes: 11 additions & 9 deletions create-ssh-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
source $SCRIPT_DIR/utils.sh

# Generate a new SSH key pair
NEW_USER_HOME=/home/$NEW_USER
sudo mkdir -p $NEW_USER_HOME/.ssh
PRIVATE_KEY_FILE=$NEW_USER_HOME/.ssh/id_rsa

PRIVATE_KEY_FILE=$HOME/tunnel_id_rsa

# we need to do this ugly as we do not have permissions to check if the file exists
if sudo sh -c "[ ! -f \"$PRIVATE_KEY_FILE\" ]"; then
if [ ! -f "$PRIVATE_KEY_FILE" ]; then
print_step "$PRIVATE_KEY_FILE does not exist"
print_step "Generating a new SSH key pair"
sudo ssh-keygen -t rsa -b 4096 -f $PRIVATE_KEY_FILE -N "" -C "$NEW_USER"
sudo ssh-keygen -t rsa -b 4096 -f $PRIVATE_KEY_FILE -N "" -C "vantage6-tunnel"
else
print_warning "SSH key already exists at $PRIVATE_KEY_FILE"
fi

# Add the public key to the authorized_keys file
print_step "Adding the public key to the authorized_keys file"
print_step "Adding the public key to the authorized_keys file of the SSH user"
NEW_USER_HOME=/home/$NEW_USER
sudo mkdir -p $NEW_USER_HOME/.ssh
PUBLIC_KEY=$(sudo cat $PRIVATE_KEY_FILE.pub)
sudo touch $NEW_USER_HOME/.ssh/authorized_keys
if ! sudo grep -q "$PUBLIC_KEY" $NEW_USER_HOME/.ssh/authorized_keys; then
sudo echo $PUBLIC_KEY >> $NEW_USER_HOME/.ssh/authorized_keys
echo $PUBLIC_KEY | sudo tee -a $NEW_USER_HOME/.ssh/authorized_keys > /dev/null
else
print_warning "Public key already exists in the authorized_keys file"
fi
Expand All @@ -29,8 +31,8 @@ fi
print_step "Setting the correct file permissions"
sudo chmod 700 $NEW_USER_HOME/.ssh
sudo chmod 600 $NEW_USER_HOME/.ssh/authorized_keys
sudo chmod 600 $NEW_USER_HOME/.ssh/id_rsa
sudo chmod 644 $NEW_USER_HOME/.ssh/id_rsa.pub
sudo chmod 777 $HOME/tunnel_id_rsa
sudo chmod 644 $HOME/tunnel_id_rsa.pub

# Change the owner to $NEW_USER
print_step "Changing the owner of the files to $NEW_USER"
Expand Down
12 changes: 12 additions & 0 deletions install-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ source $SCRIPT_DIR/install-vantage6.sh
print_header "Creating vantage6-node"
source $SCRIPT_DIR/create-node.sh

print_header "Post installation steps"
if confirm "Do you want to create a cronjob to automatically start the vantage6-node on boot?"; then
source $SCRIPT_DIR/create-cronjob.sh
fi

if confirm "Do you want to start the vantage6-node now?"; then
source $SCRIPT_DIR/node.sh start blueberry
fi




print_outro
27 changes: 27 additions & 0 deletions node.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#! /bin/bash
SCRIPT_DIR=$(realpath "$(dirname "$0")")
COMMAND=$1
NODE_NAME=$2

source $SCRIPT_DIR/utils.sh

print_header "$COMMAND the vantage6-node"
print_step "Checking if conda is installed"
if ! check_command "conda"; then
print_error "Did you run the installation script? Exiting..."
exit 1
fi

print_step "Checking if the vantage6 environment exists"
if ! check_env "vantage6"; then
print_error "Did you run the installation script? Exiting..."
exit 1
fi

print_step "Activating the vantage6 environment"
source $HOME/miniconda/etc/profile.d/conda.sh
conda activate vantage6

# start the node
print_step "$COMMAND the $NODE_NAME node"
vnode $COMMAND --name $NODE_NAME
33 changes: 16 additions & 17 deletions node.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ databases:

- label: omop
uri: jdbc:postgresql://${OMOP_HOST}:${OMOP_PORT}/${OMOP_DATABASE}
type: omop
# additional environment variables that are passed to the algorithm
# containers (or their wrapper). This can be used to for usernames
# and passwords for example. Note that these environment variables are
Expand Down Expand Up @@ -139,22 +140,21 @@ ssh-tunnels:
username: ${SSH_USERNAME}
key: ${SSH_KEY}

# Once the SSH connection is established, a tunnel is created to
# forward traffic from the local machine to the remote machine.
tunnel:

# The port and ip on the tunnel container. The ip is always
# 0.0.0.0 as we want the algorithm container to be able to
# connect.
bind:
ip: ${TUNNEL_BIND_IP}
port: ${TUNNEL_BIND_PORT}

# The port and ip on the remote machine. If the data source runs
# on this machine, the ip most likely is 127.0.0.1.
dest:
ip: ${TUNNEL_REMOTE_IP}
port: ${TUNNEL_REMOTE_PORT}
# Once the SSH connection is established, a tunnel is created to
# forward traffic from the local machine to the remote machine.
tunnel:

# The port and ip on the tunnel container. The ip is always
# 0.0.0.0 as we want the algorithm container to be able to
# connect.
bind:
ip: ${TUNNEL_BIND_IP}
port: ${TUNNEL_BIND_PORT}
# The port and ip on the remote machine. If the data source runs
# on this machine, the ip most likely is 127.0.0.1.
dest:
ip: ${TUNNEL_REMOTE_IP}
port: ${TUNNEL_REMOTE_PORT}

# # Whitelist URLs and/or IP addresses that the algorithm containers are
# # allowed to reach using the http protocol.
Expand Down Expand Up @@ -219,7 +219,6 @@ debug:
# into debug mode
proxy_server: false


# directory where local task files (input/output) are stored
task_dir: ${TASK_DIR}

Expand Down
3 changes: 3 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash
SCRIPT_DIR=$(realpath "$(dirname "$0")")
source $SCRIPT_DIR/node.sh start blueberry
3 changes: 3 additions & 0 deletions stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /bin/bash
SCRIPT_DIR=$(realpath "$(dirname "$0")")
source $SCRIPT_DIR/node.sh stop blueberry
34 changes: 30 additions & 4 deletions utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ print_outro(){
echo ""
echo " conda activate vantage6"
echo ""
echo "Make sure the OMOP database is running and listening on 127.0.0.1:5432 "
echo "Make sure the OMOP database is running and listening on $TUNNEL_REMOTE_IP:$TUNNEL_REMOTE_PORT "
echo "before starting the vantage6-node."
print_divider
}

confirm() {
read -p " ? $1 (y/n) " response
echo -e -n "\e[32m ? $1 (y/n) \e[0m"
read response
case "$response" in
[yY][eE][sS]|[yY])
true
Expand All @@ -90,8 +91,33 @@ confirm_or_exit() {
fi
}

function create_config_file() {
create_config_file() {
envsubst < "$1/node.tpl" > "$2"
print_step "Config file created at $HOME/.config/vantage6/node/blueberry.yml"
print_step "Config file created at $2"
}


check_command() {
local command_name="$1"

# Check if the command is installed
if ! command -v "$command_name" &> /dev/null; then
print_error "$command_name is not installed" >&2
return 1
fi

return 0
}

check_env() {
local env_name="$1"

# Check if the environment exists
if ! conda env list | grep -q "^$env_name "; then
print_error "conda environment '$env_name' does not exist" >&2
return 1
fi

return 0
}

0 comments on commit 13157ec

Please sign in to comment.