Skip to content

Instantly share code, notes, and snippets.

@corydodt
Last active May 6, 2020 23:02
Show Gist options
  • Save corydodt/7652476 to your computer and use it in GitHub Desktop.
Save corydodt/7652476 to your computer and use it in GitHub Desktop.
===================
Brightfab gist repo
===================
This is the fabric code for managing Brightmd's developer automation.
It bootstraps all dependencies and code into the current directory, under Brightmd/ then moves itself into that directory.
**Note** It is not possible to create directories in a gist.
"""
Automations for Aorta, the main webserver of Bright.md
"""
from . import brightmd
venv=$(echo ${VIRTUAL_ENV} | sed s:'.*/'::)
npmbin() {
## ret=`npm bin`
# macosx npm bin is broken. @#$#@@!!
pwd=`pwd`
echo $pwd/node_module/bin:$pwd/node_modules/.bin
}
if [[ "${venv}" != "Brightmd" ]]; then
echo "############## Brightmd env ##" 1>&2
pwd=`pwd`
pwdOrig=$pwd
while ! [[ -e $pwd/bin/activate ]]; do
builtin cd ..
pwd=`pwd`
done
builtin cd $pwdOrig
. $pwd/bin/activate
export PATH="$PATH:`npmbin`"
fi
#!/bin/bash
set -e
echo "** DO NOT USE THIS - install with virtualenvwrapper" 1>&2
exit 1
unset CDPATH
realpath() {
echo $(cd $(dirname "$1"); pwd)/$(basename "$1")
}
executable=$(realpath "$0")
eval wd=~/Brightmd/fabfile
cd ~
# - Install pip, so we can do everything else this script does
# - Install mongodb, which always runs as an os service
# - Install brew on mac, so we can use it.
gitLinux() {
sudo apt-get update
sudo apt-get install -y git
}
gitDarwin() {
echo '** Type "git" into the terminal. If a dialog pops up to install XCode, click Install.'
echo '** When done installing, rerun the command that got you this far.'
exit 2
}
onceLinux() {
sudo apt-get update
sudo apt-get install -y python-pip mongodb \
hyperestraier python-dev libyaml-dev mailutils \
libestraier-dev libffi-dev libssl-dev libjpeg-dev libtiff-dev \
libwebp-dev rlwrap p7zip-full links libhtml-format-perl
}
mongoOnDarwin() {
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
}
onceDarwin() {
curl -L 'https://bootstrap.pypa.io/ez_setup.py' -o - | sudo python
sudo easy_install pip
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" || true
brew install libyaml mongodb hyperestraier rlwrap gpg links
mkdir -p ~/Library/LaunchAgents
mongoOnDarwin || echo "MongoDB may already be installed -- ignore if so"
}
step2() {
header "II. Do all one-time setup for $(uname)"
eval once$(uname)
sudo pip install -U paramiko==1.10.0 fabric==1.8.1
sudo pip install -U virtualenv
sudo pip install -U autoenv
}
step1() {
if ! test -e "$wd/brightmd.py"; then
header "I. Install git, and clone the source for this installer"
which git || eval git$(uname)
echo "Creating a Brightmd/ directory with bootstrap source in Brightmd/fabfile/"
git clone https://gist.github.com/7652476.git "$wd"
else
header "I(b). Update the source for this installer"
updateMySource "$wd"
fi
}
updateMySource() {
pushd "$1" >/dev/null
git pull
popd >/dev/null
}
header() {
message="$@"
echo
echo
echo "$message"
eval printf '=%.0s' {1..${#message}}
echo
echo
}
case "$1" in
upgrade)
export -f header updateMySource
exec "$wd/upgrade" go
;;
step2)
step2
exec "$wd/bootstrap" upgrade
;;
*)
step1
exec "$wd/bootstrap" step2
;;
esac
#!/bin/bash
set -e
unset CDPATH
realpath() {
echo $(cd $(dirname "$1"); pwd)/$(basename "$1")
}
executable=$(realpath "$0")
eval wd=~/Brightmd/fabfile
cd ~
# - Install pip, so we can do everything else this script does
# - Install mongodb, which always runs as an os service
gitLinux() {
sudo apt-get update
sudo apt-get install -y git
}
onceLinux() {
sudo apt-get update
sudo apt-get install -y python-pip mongodb \
hyperestraier python-dev libyaml-dev mailutils \
libestraier-dev libffi-dev libssl-dev libjpeg-dev libtiff-dev \
libwebp-dev rlwrap p7zip-full links libhtml-format-perl virtualenvwrapper
# fix up node bullshit on kubuntu 16.04
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo npm install npm -g
sudo npm install grunt
sudo npm install phantomjs-prebuilt
}
step2() {
header "II. Do all one-time setup for $(uname)"
eval once$(uname)
sudo pip install -U paramiko>=1.10.0 fabric>=1.8.1
sudo pip install -U virtualenv
}
step1() {
if ! test -e "$wd/brightmd.py"; then
header "I. Install git, and clone the source for this installer"
which git || eval git$(uname)
echo "Creating a Brightmd/ directory with bootstrap source in Brightmd/fabfile/"
git clone https://gist.github.com/7652476.git "$wd"
else
header "I(b). Update the source for this installer"
updateMySource "$wd"
fi
}
updateMySource() {
pushd "$1" >/dev/null
git pull
popd >/dev/null
}
header() {
message="$@"
echo
echo
echo "$message"
eval printf '=%.0s' {1..${#message}}
echo
echo
}
case "$1" in
upgrade)
export -f header updateMySource
exec "$wd/upgrade-virtualenvwrapper" go
;;
step2)
step2
exec "$wd/bootstrap-virtualenvwrapper" upgrade
;;
*)
step1
exec "$wd/bootstrap-virtualenvwrapper" step2
;;
esac
#!/usr/bin/env python
"""
Maintenance tasks for Bright.md and in particular the developers
"""
import os
from os.path import exists, abspath, expanduser
from contextlib import contextmanager
from functools import wraps
import json
from fabric.api import task, local, execute, prefix, shell_env, settings, env
class chdir(object):
"""
Contextmanager/Decorator for running a function or a with block from a
particular local os directory
"""
def __init__(self, d):
self.d = d
def __call__(self, fn):
"""
Decorate fn by using the contextmanager represented by self
"""
@wraps(fn)
def impl(*a, **kw):
with self:
return fn(*a, **kw)
return impl
def __enter__(self):
self._orig = os.getcwd()
os.chdir(self.d)
def __exit__(self, *a):
os.chdir(self._orig)
env.setdefault('nodeStableVersion', '5.3.0')
inBrightmd = chdir(expanduser('~/Brightmd'))
@contextmanager
def virtualenv(d):
"""
Wrap the commands in the virtualenv at directory `d'
"""
vcwd = abspath(d)
with chdir(d), prefix("source %s/bin/activate" % vcwd):
yield
def L(*a, **kw):
"""
Run local() commands with bash
"""
kw.update({'shell': '/bin/bash'})
return local(*a, **kw)
@task(default=True)
@inBrightmd
def upgradeDeveloper():
"""
Do tasks for a developer installation of aorta
"""
if not exists('bin/python'):
L("virtualenv .")
execute(_setupCortex)
execute(_setupAorta)
@inBrightmd
def npm(package):
"""
Install a package with npm install, adding necessary flags
"""
def check():
with settings(warn_only=True):
return L("npm ls %s" % package).succeeded
if check():
data = L("npm outdated --json %s" % package, capture=True)
data = json.loads(data)
if data:
L("npm update %s" % package)
else:
L("npm install %s --save-dev --prefix `pwd`/node_modules" % package)
@task
@inBrightmd
def cortex():
"""
Only install Cortex - either for the first time or upgrade
"""
if not exists('bin/python'):
L("virtualenv .")
execute(_setupCortex)
@inBrightmd
def _setupCortex():
"""
Install dependencies and do setup/upgrades necessary for the Cortex
content
"""
if exists("Cortex"):
with chdir("Cortex"):
L("git pull .")
else:
L("git clone ssh://git@github.com/Brightmd/Cortex Cortex")
L("mkdir -p Cortex/.git/hooks/".format(**env))
L("cp -v fabfile/post-checkout Cortex/.git/hooks/".format(**env))
@inBrightmd
def _setupAorta():
"""
Install dependencies and do setup/upgrades necessary for the aorta core app
"""
operatingSystem, _, _, _, arch = os.uname()
if operatingSystem == 'Linux':
flags = {}
elif operatingSystem == 'Darwin':
# A recent Xtools patch broke some compiler command-line arguments,
# workaround that.
# Another apple patch killed openssl, so also fix that. Fuck off,
# Apple.
flags = dict(
CFLAGS="-Qunused-arguments -I/usr/local/include -I/usr/local/opt/openssl/include",
CPPFLAGS="-Qunused-arguments -I/usr/local/include",
ARCHFLAGS='-Wno-error=unused-command-line-argument-hard-error-in-future',
LDFLAGS="-L/usr/local/opt/openssl/lib",
)
aortaName = env['aortaName']
if exists(aortaName):
with chdir(aortaName):
L("git pull .")
else:
L("git clone ssh://git@github.com/Brightmd/Aorta {aortaName}".format(**env))
L("mkdir -p {aortaName}/.git/hooks/".format(**env))
L("cp -v fabfile/post-checkout {aortaName}/.git/hooks/".format(**env))
with shell_env(**flags), virtualenv("."):
L("pip install -U -r fabfile/requirements-all.txt")
if operatingSystem == 'Darwin':
L("pip install -U macfsevents")
# install node inside a nodeenv (similar to a virtualenv, and in this
# case it's the same directory)
env['nodeenvPrebuilt'] = '--prebuilt'
L("nodeenv -v {nodeenvPrebuilt} -p -n {nodeStableVersion}".format(**env))
# Installing grunt is insane. You have to install the command line
# tools globally, then install it AGAIN inside of a project where a
# Gruntfile.js lives.
#
# This is moronic.
#
# (NOTE: for us, because we are using nodeenv, "globally" means in the
# Brightmd directory, and when we install it again that means in the
# Brightmd/Aorta subdirectory.)
npm("-g grunt grunt-cli")
with chdir(aortaName):
if exists("{aortaName}/node_modules".format(**env)):
L("npm update")
else:
L("npm install .")
# download rx indices
L("curl http://cloudfront-appdev.bright.md/rxnorm.tgz | tar xz")
#!/bin/bash
#
# For useful information on loading your RxNorm subset
# into a MySQL database, please consult the on-line
# documentation at:
#
# http://www.nlm.nih.gov/research/umls/rxnorm/docs/index.html
#
#
# Database connection parameters
# Please edit these variables to reflect your environment
#
set -e
if [ "$#" -lt 1 ]; then
echo "Use: MYSQL_PWD=':secret#password3' $0 <username>" 1>&2
exit 1
fi
cloudfront='http://cloudfront-appdev.bright.md/rxnorm'
user="$1"
if [ -z "$MYSQL_PWD" ]; then
passwordOption='-p'
mysqlEnv=''
else
passwordOption=''
mysqlEnv='MYSQL_PWD='"$MYSQL_PWD"
fi
fab brightmd.cortex
mysql="env $mysqlEnv mysql -vvv -u$user $passwordOption -hlocalhost --local-infile=1 "
pushd $(dirname $0); pushd ..; brightmd=`pwd`; popd; popd
scripts="$brightmd/Cortex/3p/rxnorm/mysql/"
logfile="$scripts/mysql.log"
ef=0
echo "See $logfile for output"
load() {
pushd "$scripts" >/dev/null
echo " Installing rxnorm"
$mysql mysql <<< "create database rxnorm"
$mysql rxnorm < "$scripts/Table_scripts_mysql_rxn.sql"
if [ $? -ne 0 ]; then ef=1; fi
if [ $ef -ne 1 ]; then
echo -n Uncompressing..
curl -L -o- "$cloudfront/RXNCONSO.RRF.bz2?2014" | bzip2 -d -c > /tmp/RXNCONSO.RRF
echo -n .
curl -L -o- "$cloudfront/RXNREL.RRF.bz2?2014" | bzip2 -d -c > /tmp/RXNREL.RRF
echo -n .
curl -L -o- "$cloudfront/RXNSAT.RRF.bz2?2014" | bzip2 -d -c > /tmp/RXNSAT.RRF
echo .
echo " Load Data ... `/bin/date`"
$mysql rxnorm < "$scripts/Load_scripts_mysql_rxn_unix.sql"
if [ $? -ne 0 ]; then ef=1; fi
fi
if [ $ef -eq 1 ]; then
echo "There were one or more errors. Please reference the $logfile file for details."
else
echo "Completed without errors."
fi
echo "Finished ... `/bin/date`"
popd > /dev/null
rm -vf /tmp/*.RRF
}
load 2>&1 | tee -a "$logfile"
#!/bin/bash
set -e
cd ./$(git rev-parse --show-cdup)
cmd="find -name '*.pyc' -delete"
echo "$cmd"; eval $cmd
#!/bin/bash
set -e
unset CDPATH
eval brightmd=~/Brightmd
wd=${brightmd}/fabfile
if [ "$1" != "go" ]; then
exec "$wd/bootstrap" upgrade
fi
header "III. Do repeatable setup and update"
pushd "$wd" > /dev/null
updateMySource "$wd"
# Run fabric which installs everything else
fab --show debug brightmd --set aortaName="${AORTA_NAME:-Aorta}"
# Set up autoenv
cp "$wd/autoenv.env" "$brightmd/.env"
for loc in /usr/local/bin /usr/local/opt/autoenv; do
if [ -x $loc/activate.sh ]; then
activate_path=$loc/activate.sh
fi
done
. $activate_path && autoenv_authorize_env `pwd`/.env && autoenv_authorize_env `pwd`/Aorta/.env
echo "##################################################################"
echo " You must add this to your .bash_profile or shell environment:"
echo
echo " . $activate_path"
echo
ln -sf "$wd"/upgrade "$brightmd"/bin/upgrade
popd
#!/bin/bash
set -e
unset CDPATH
eval brightmd=~/Brightmd
wd=${brightmd}/fabfile
. /usr/share/virtualenvwrapper/virtualenvwrapper.sh
if [ "$1" != "go" ]; then
exec "$wd/bootstrap-virtualenvwrapper" upgrade
fi
header "III. Do repeatable setup and update"
pushd "$wd" > /dev/null
updateMySource "$wd"
# Run fabric which installs everything else
fab --show debug brightmd --set aortaName="${AORTA_NAME:-Aorta}"
# Set up virtualenvwrapper
mkvirtualenv -a $HOME/Brightmd/Aorta -r $HOME/Brightmd/fabfile/requirements-all.txt Aorta
postactivateSetup() {
echo > $HOME/.virtualenvs/Aorta/bin/postactivate << EOF
cd ~/Brightmd/Aorta
export PATH=`pwd`/bin:$PATH:`pwd`/node_modules/.bin
export PYTHONPATH=`pwd`:$PYTHONPATH
EOF
chmod +x $HOME/.virtualenvs/Aorta/bin/postactivate
}
if ! [ -x $HOME/.virtualenvs/Aorta/bin/postactivate ]; then
postactivateSetup
fi
ln -sf "$wd"/upgrade-virtualenvwrapper "$brightmd"/bin/upgrade-virtualenvwrapper
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment