Last active
March 23, 2017 17:24
-
-
Save jimbaker/9089e381af38d9db6bc4af59baed6d47 to your computer and use it in GitHub Desktop.
Wrappers for working with Craton: setting up dev servers, getting env vars, and using the REST API. Simply source, and use like so:$ craton-get v1/hosts. Etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example usage. Note that we simply pass through httpie conventions, | |
# such as nested JSON with := | |
# (https://github.com/jkbrzt/httpie#non-string-json-fields) | |
# | |
# $ craton-post v1/regions name=HKG | |
# $ craton-get v1/hosts | |
# $ craton-put v1/hosts/3 device_type=container | |
# $ craton-put v1/hosts/3/variables foo=47 bar:='["a", "b", "c"]' | |
# $ craton-delete v1/hosts/4 | |
# | |
# Additional commands provided to manage with respect to environment | |
# variables, docker startup, etc. | |
# NOTE assumes the installation of httpie so that the http command is available! | |
fix-url() { | |
[[ "$1" =~ ^http ]] && echo $1 || echo "${CRATON_URL}/${1}" | |
} | |
craton-get() { | |
http "$(fix-url $1)" \ | |
"Content-Type:application/json" \ | |
"X-Auth-Token:${OS_PASSWORD}" \ | |
"X-Auth-User:${OS_USERNAME}" \ | |
"X-Auth-Project:${OS_PROJECT_ID}" | |
} | |
craton-post() { | |
http POST "$(fix-url $1)" \ | |
"Content-Type:application/json" \ | |
"X-Auth-Token:${OS_PASSWORD}" \ | |
"X-Auth-User:${OS_USERNAME}" \ | |
"X-Auth-Project:${OS_PROJECT_ID}" \ | |
"${@:2}" | |
} | |
craton-put() { | |
http PUT "$(fix-url $1)" \ | |
"Content-Type:application/json" \ | |
"X-Auth-Token:${OS_PASSWORD}" \ | |
"X-Auth-User:${OS_USERNAME}" \ | |
"X-Auth-Project:${OS_PROJECT_ID}" \ | |
"${@:2}" | |
} | |
craton-delete() { | |
http DELETE "$(fix-url $1)" \ | |
"Content-Type:application/json" \ | |
"X-Auth-Token:${OS_PASSWORD}" \ | |
"X-Auth-User:${OS_USERNAME}" \ | |
"X-Auth-Project:${OS_PROJECT_ID}" \ | |
"${@:2}" | |
} | |
_craton-extract-env() { | |
echo OS_PROJECT_ID=$(echo "$1" | grep 'ProjectId' | awk '{print $2}' | tr -d '\r') | |
echo OS_USERNAME=$(echo "$1" | grep 'Username' | awk '{print $2}' | tr -d '\r') | |
echo OS_PASSWORD=$(echo "$1" | grep 'APIKey' | awk '{print $2}' | tr -d '\r') | |
} | |
craton-docker-env() { | |
_craton-extract-env "$(docker logs craton-api)" | |
} | |
# FIXME(jimbaker) Currently hardcode location of config file; the | |
# reason is that this conf file in turn hard codes the location o the | |
# paste config | |
# | |
## Paste Config file to use | |
## api_paste_config=/craton/etc/craton-api-paste.ini | |
# | |
# Fix by creating temporary files, edited with sed. Future work! | |
craton-direct-env() { | |
_craton-extract-env "$(cd .. && craton-dbsync --config-file=~/fleetdev/etc/craton-api.conf bootstrap)" | |
} | |
craton-start-direct() { | |
# NOTE(jimbaker) Assumes MySQL user 'craton' is setup with password 'craton'!!! | |
cat <<EOF | mysql --user=craton --password=craton | |
drop database craton; | |
create database craton; | |
EOF | |
pip install -e . | |
pip install pymysql | |
(cd .. && craton-dbsync --config-file=~/fleetdev/etc/craton-api.conf upgrade) | |
echo "Starting API server..." | |
(cd .. && craton-api --config-file=~/fleetdev/etc/craton-api.conf) | |
} | |
craton-reload() { | |
(cd .. && craton-api --config-file=~/fleetdev/etc/craton-api.conf) | |
} | |
craton-fake-data() { | |
until craton-get v1/regions 2>/dev/null | |
do | |
echo "Waiting for API server"; sleep 1 | |
done | |
python tools/generate_fake_data.py \ | |
--url $CRATON_URL/v1 \ | |
--user $OS_USERNAME --project $OS_PROJECT_ID --key $OS_PASSWORD | |
craton-get v1/regions | |
} | |
craton-start-docker() { | |
echo "Starting Craton docker container..." | |
docker rm -f craton-api 2>/dev/null || true | |
docker build --pull -t craton-api:latest . | |
docker run -t --name craton-api -p 127.0.0.1:7780:7780 -d craton-api:latest | |
OS_PROJECT_ID=probe | |
OS_USERNAME=probe | |
OS_PASSWORD=probe | |
until craton-get v1/regions 2>/dev/null | |
do | |
echo "Waiting for API server"; sleep 1 | |
done | |
eval $(craton-docker-env) | |
craton-fake-data | |
docker logs -f craton-api | |
} |
Resolved absolute vs local URL detection in latest version of code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should detect if the arg ($1) is a fully qualified URL, or needs to be appended with $CRATON_URL. This would be useful for working with links. Future work!