Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Forked from mark-cooper/ascli
Last active March 28, 2022 19:49
Show Gist options
  • Save DonRichards/c3c6a54d69a57ccfd86839a78e453d57 to your computer and use it in GitHub Desktop.
Save DonRichards/c3c6a54d69a57ccfd86839a78e453d57 to your computer and use it in GitHub Desktop.
Load ArchivesSpace backend in a pry session

Useful Notes for developers

When starting run the below script then

 ./build/run backend:devserver  <-- 1st, Must be ran first and wait for this messsage
       ...
       [java] Mizuno 9.4.44 (Jetty 9.4.44.v20210927) listening on 0.0.0.0:4567

 ./build/run frontend:devserver <-- 2nd No need to wait to run the next
 ./build/run public:devserver   <-- 3rd No need to wait to run the next
 ./build/run indexer            <-- 4th

Tests

Useful Links

Selenium IDE Logo Selenium IDE for recording and replaying tasks.

Run the test as Chrome

SELENIUM_CHROME=true ./build/run frontend:selenium -Dexample="can import accession and show type"

Unset headless for selenium to see the tests

This setting is found with frontend/spec/rails_helper.rb & public/spec/rails_helper.rb

CHROME_OPTS  = ENV.fetch('CHROME_OPTS', '--headless,--disable-gpu,--window-size=1920x1080,--no-sandbox,--disable-dev-shm-usage,--remote-debugging-port=9222').split(',')

# Remove ---headless,
CHROME_OPTS  = ENV.fetch('CHROME_OPTS', '--disable-gpu,--window-size=1920x1080,--no-sandbox,--disable-dev-shm-usage,--remote-debugging-port=9222').split(',')

Running in non-headless mode gives you the ability to see where the failure occurs and in real time.

To Pause

Pause tests for 60 seconds add this line within _spec.rb file to pause between steps to inspect the browser's progress.

sleep(60)
#!/bin/env bash
stty -echoctl
# ~/.zshrc adjustments needed for ASDF
# fpath=(${ASDF_DIR}/completions $fpath)
# plugins=(git docker gradle zsh-syntax-highlighting zsh-pentest nmap quiver web-search)
# autoload -Uz compinit && compinit
# source $ZSH/oh-my-zsh.sh
# # asdf-vm.com
# . $HOME/.asdf/asdf.sh
# . $HOME/.asdf/completions/asdf.bash
# export PATH=$PATH:$HOME/.asdf/
# This line is probably necessary if these aren't already installed.
# sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev shared-mime-info
# Chrome is needed for the Selenium tests.
# wget -N http://chromedriver.storage.googleapis.com/2.33/chromedriver_linux64.zip -P ~/
# unzip chromedriver_linux64.zip -d ~/
# rm chromedriver_linux64.zip
# sudo mv -f chromedriver /usr/bin/chromedriver
# sudo chmod +x /usr/local/share/chromedriver
# sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
# chromedriver -v
# Reset all gems if needed.
# gem uninstall -aIx
cat ~/.gemrc | grep -F "no-document" || echo 'gem: --no-document' >> ~/.gemrc
cd /var/www/html/archivesspace || exit
trap ctrl_c INT
################################################################################
# Functions #
################################################################################
function clean() {
git stash
git clean -xfd
git stash
}
function check_ports() {
# Check Ports incase of conflicts.
# sudo lsof -i -P -n | grep LISTEN
sudo lsof -i :3307 -t && echo "Port 3307 is in use"
sudo lsof -i :8983 -t && echo "Port 8983 is in use"
sudo lsof -i :3306 -t && echo "Port 3306 is in use"
sudo lsof -i :8984 -t && echo "Port 8984 is in use"
sudo lsof -i :4567 -t && echo "Port 4567 is in use"
sudo lsof -i :3000 -t && echo "Port 3000 is in use"
if $(sudo lsof -i :3307 -i :8983 -i :3306 -i :8984 -i :4567 -i :3000 -i 3001 -i 3636 -i 4545 -t); then
return 1
else
return 0
fi
# Test server needs ports 3636 & 4545
}
function ctrl_c() {
echo "Killing all services"
docker-compose -f docker-compose-dev.yml down
wait
if [ -e "../pids.pid" ]; then
while read pid; do
if [ "$pid" != "" ]; then
sudo kill -9 $pid
else
echo "No more running processes"
fi
done <../pids.pid
fi
echo '' > ../pids.pid
sudo kill -9 $(sudo lsof -i :3307 -i :8983 -i :3306 -i :8984 -i :4567 -i :3000 -t)
exit
}
help()
{
# Display Help
echo
echo "Syntax: scriptTemplate [-h|down|ports|skip|kill|clean]"
echo "options:
"
echo " -h Print this Help."
echo " down Turn off docker containers."
echo " ports Check to see if ports are in use."
echo " skip Skip the start of the docker containers."
echo " kill Kill all running docker containers and services that this script started."
echo " clean Runs git stash then git clean -xfd and then runs git stash pop to restore progress while removing files that were generated by the build process."
echo
}
################################################################################
# Script Arguements (-h|down|ports|skip|kill) #
################################################################################
if [ "$1" == "-h" ]; then
help
exit
fi
if [ "$1" == "down" ]; then
docker-compose -f docker-compose-dev.yml down
wait
exit
fi
if [ "$1" == "kill" ]; then
ctrl_c
fi
if [ "$1" == "ports" ]; then
check_ports
exit
fi
if [ "$1" == "clean" ] || [ "$1" == "reset" ] ; then
ctrl_c
clean
exit
fi
if [ "$1" != "skip" ]; then
docker-compose -f docker-compose-dev.yml build
docker-compose -f docker-compose-dev.yml down
wait
if [ check_ports == 1 ]; then
echo "Ports are in use. Please close them and try again. To see them use this command to see what is running."
echo "sudo lsof -i :3307 -i :8983 -i :3306 -i :8984 -i :4567 -i :3000"
exit
fi
docker-compose -f docker-compose-dev.yml build
docker-compose -f docker-compose-dev.yml up -d
else
echo "Skipping docker rebuild."
fi
################################################################################
# Waiting #
################################################################################
echo "Wait for stats to be below 25% to avoid race conditions"
while [ $(docker stats as_dev_solr --no-stream --format "{{.CPUPerc}}" | sed 's/\.[^[:blank:]]*//' ) -gt 25 ]; do
echo "Waiting for Solr's stats to be below 25% to indicate that Solr is ready and idle"
sleep 1
done
while [ $(docker stats as_dev_db --no-stream --format "{{.CPUPerc}}" | sed 's/\.[^[:blank:]]*//' ) -gt 25 ]; do
echo "Waiting for the database's stats to be below 25% to indicate that db is ready and idle"
sleep 1
done
################################################################################
# Prerequisites software checks #
################################################################################
if ! java -version 2>&1 | grep 'openjdk version "11"\|openjdk version "8"' > /dev/null 2>&1; then
echo "Java version is not '11'."
asdf plugin-add java
asdf install java openjdk-11
asdf global java openjdk-11
if ! java -version 2>&1 | grep 'openjdk version "11"\|openjdk version "8"' > /dev/null 2>&1; then
echo "Java failed to install."
exit 1
fi
fi
if ! ruby -v | grep 'jruby 9.2.20.1' > /dev/null 2>&1; then
echo "Ruby version is not 'jruby 9.2.20.1'"
asdf plugin-add ruby
asdf install ruby jruby-9.2.20.1
asdf global ruby jruby-9.2.20.1
fi
if ! gem spec nokogiri > /dev/null 2>&1; then
echo "Gem nokogiri is missing and should be installed!"
asdf exec gem install nokogiri -v 1.12.5
if ! gem spec nokogiri > /dev/null 2>&1; then
echo "Gem nokogiri failed to install properly!"
exit
fi
fi
if ! gem spec rails > /dev/null 2>&1; then
echo "Gem rails is missing and should be installed!"
asdf exec gem install rails -v 5.2.5
if ! gem spec rails > /dev/null 2>&1; then
echo "Gem rails failed to install properly!"
exit
fi
asdf reshim ruby
fi
if ! gem spec bundler > /dev/null 2>&1; then
echo "Gem bundler is missing and should be installed!"
asdf exec gem install bundler -v 2.3.8
if ! gem spec bundler > /dev/null 2>&1; then
echo "Gem bundler failed to install properly!"
exit
fi
fi
if ! gem spec pry > /dev/null 2>&1; then
echo "Gem pry is missing and should be installed!"
asdf exec gem install pry
if ! gem spec pry > /dev/null 2>&1; then
echo "Gem pry failed to install properly!"
exit
fi
fi
MYSQL_CONNECTOR_JAVA_FILE=common/lib/mysql-connector-java-8.0.23.jar
if ! test -f "$MYSQL_CONNECTOR_JAVA_FILE" ; then
ls -lah common/lib/mysql-connector-java-8.0.23.jar
echo "MySQL Connector is missing and should be installed!"
wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.23/mysql-connector-java-8.0.23.jar
mv mysql-connector-java-8.0.23.jar common/lib/
else
echo "$MYSQL_CONNECTOR_JAVA_FILE exists."
fi
################################################################################
# Build & Migrate #
################################################################################
echo "Running bootstrap"
./build/run bootstrap
echo "Clearing local indexer state"
./build/run solr:reset
echo "Wait for stats to be below 25% to avoid race conditions"
while [ $(docker stats as_dev_solr --no-stream --format "{{.CPUPerc}}" | sed 's/\.[^[:blank:]]*//' ) -gt 25 ]; do
echo "Waiting for Solr's stats to be below 25% to indicate that Solr is ready and idle"
sleep 1
done
echo "Running database migrations"
./build/run db:migrate
echo "Wait for stats to be below 25% to avoid race conditions"
while [ $(docker stats as_dev_solr --no-stream --format "{{.CPUPerc}}" | sed 's/\.[^[:blank:]]*//' ) -gt 25 ]; do
echo "Waiting for Solr's stats to be below 25% to indicate that Solr is ready and idle"
sleep 1
done
while [ $(docker stats as_dev_db --no-stream --format "{{.CPUPerc}}" | sed 's/\.[^[:blank:]]*//' ) -gt 25 ]; do
echo "Waiting for the database's stats to be below 25% to indicate that db is ready and idle"
sleep 1
done
echo "Moving on..."
# SETUP: requires asdf || rbenv
# asdf install ruby jruby-9.2.20.1 && asdf local ruby jruby-9.2.20.1 && gem install bundler pry
# rbenv install jruby-9.2.20.1 && rbenv local jruby-9.2.20.1 && gem install bundler pry
# chmod u+x ./ascli
# ./build/run bootstrap && ./build/run db:migrate
# ./ascli (default asdf) # ./ascli rbenv
# gem pristine --all
export ASCLI_RUNNER=${1:-asdf} # or rbenv
export RUBYLIB=$PWD/common/
export CLASSPATH=$PWD/build/*:$PWD/common/lib/*
export GEM_HOME=$PWD/build/gems/jruby/2.5.0/gems
export BUNDLE_PATH=$GEM_HOME
export BUNDLE_GEMFILE=$PWD/backend/Gemfile
JAVA_OPTS="$JAVA_OPTS -Daspace.config.search_user_secret=devserver -Daspace.config.public_user_secret=devserver"
JAVA_OPTS="$JAVA_OPTS -Daspace.config.staff_user_secret=devserver -Daspace.config.frontend_cookie_secret=devserver"
JAVA_OPTS="$JAVA_OPTS -Daspace.config.public_cookie_secret=devserver -Daspace.config.solr_url=http://localhost:8983/solr/#/archivesspace/core-overview"
JAVA_OPTS="$JAVA_OPTS -Daspace.config.data_directory=$PWD/build -Dfile.encoding=UTF-8 -Daspace.devserver=true"
# JAVA_OPTS="$JAVA_OPTS -XX:MaxPermSize=200m -Xmx512m -Xss512m"
JAVA_OPTS="$JAVA_OPTS -Xmx512m -Xss512m"
export JAVA_OPTS
PATH=$(dirname $($ASCLI_RUNNER which gem)):$PATH
ruby -e 'puts $:'
bundle install
echo "Resetting gems folder permissions"
sudo chown -R $USER: $GEM_HOME && sudo chmod -R 755 $GEM_HOME
sudo chown -R $USER: $BUNDLE_GEMFILE && sudo chmod -R 755 $BUNDLE_GEMFILE
################################################################################
# Start Background Services Manually and with PRY for debugging #
################################################################################
: '
# Run these after this script is done loading, each on in their own terminal:
./build/run backend:devserver
./build/run frontend:devserver
./build/run public:devserver
./build/run indexer
'
# echo -e "\nStarting Pry:\n\n"
# bundle exec pry -I ./common/ -r ./backend/app/main.rb
# RequestContext.open(repo_id: 2) { Accession.find(1) }
################################################################################
# Start Background Services with supervisord #
################################################################################
# This doesn't seem to work with supervisord.
# https://github.com/Supervisor/initscripts/blob/master/ubuntu
# supervisord --logfile super.log -c supervisord/archivesspace.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment