From aa76d3c6dc25f7323db234a4b59d5050738c2051 Mon Sep 17 00:00:00 2001 From: Teofilo Sibileau Date: Thu, 14 Jul 2016 16:44:44 -0300 Subject: [PATCH] Issue #CIVIC-3071: + Docker image with the library available for development + Ahoy commands to handle docker management + Ahoy commands to handle python usecases + Documentation for installation and usage --- .ahoy.yml | 9 +++++ .ahoy/.gitignore | 1 + .ahoy/api.ahoy.yml | 43 +++++++++++++++++++++ .ahoy/docker.ahoy.yml | 57 ++++++++++++++++++++++++++++ Dockerfile | 10 +++++ README.md | 67 +++++++++++++++++++++++++++++++-- docker-compose.yml | 4 ++ examples/attach_file_to_node.py | 6 +-- examples/create_node.py | 3 -- examples/delete_node.py | 3 -- examples/list_nodes.py | 3 -- examples/update_node.py | 3 -- 12 files changed, 190 insertions(+), 19 deletions(-) create mode 100644 .ahoy.yml create mode 100644 .ahoy/.gitignore create mode 100644 .ahoy/api.ahoy.yml create mode 100644 .ahoy/docker.ahoy.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.ahoy.yml b/.ahoy.yml new file mode 100644 index 0000000..9842614 --- /dev/null +++ b/.ahoy.yml @@ -0,0 +1,9 @@ +ahoyapi: v1 +usage: DKAN cli app for development using ahoy. +commands: + docker: + usage: A series of docker commands (experimental) + import: .ahoy/docker.ahoy.yml + api: + usage: + import: .ahoy/api.ahoy.yml diff --git a/.ahoy/.gitignore b/.ahoy/.gitignore new file mode 100644 index 0000000..68ef598 --- /dev/null +++ b/.ahoy/.gitignore @@ -0,0 +1 @@ +.dkan \ No newline at end of file diff --git a/.ahoy/api.ahoy.yml b/.ahoy/api.ahoy.yml new file mode 100644 index 0000000..2bec526 --- /dev/null +++ b/.ahoy/api.ahoy.yml @@ -0,0 +1,43 @@ +ahoyapi: v1 +version: 0.0.0 +commands: + dkan: + usage: Sets DKAN URI environment variable + cmd: | + FILE=".ahoy/.dkan" + if [ -f $FILE ]; then + cat $FILE + exit 0 + else + echo "DKAN uri not set. Type it followed by [ENTER]:" + read URI + echo "$URI" > .ahoy/.dkan + exit 0 + fi + python: + usage: + cmd: | + FILE=".ahoy/.dkan" + if [ ! -f $FILE ]; then + ahoy api dkan + fi + DKAN_URI=`cat $FILE` + ahoy docker compose run -e DKAN_URI=$DKAN_URI pydkan ipython {{args}} + prompt: + usage: Steps in the pydkan container + cmd: | + FILE=".ahoy/.dkan" + if [ ! -f $FILE ]; then + ahoy api dkan + fi + DKAN_URI=`cat $FILE` + ahoy docker compose run -e DKAN_URI=$DKAN_URI pydkan bash + tests: + usage: Runs nosetests + cmd: | + FILE=".ahoy/.dkan" + if [ ! -f $FILE ]; then + ahoy api dkan + fi + DKAN_URI=`cat $FILE` + ahoy docker compose run -e DKAN_URI=$DKAN_URI pydkan nosetests --verbose diff --git a/.ahoy/docker.ahoy.yml b/.ahoy/docker.ahoy.yml new file mode 100644 index 0000000..5de32da --- /dev/null +++ b/.ahoy/docker.ahoy.yml @@ -0,0 +1,57 @@ +ahoyapi: v1 +version: 0.0.0 +commands: + env: + cmd: | + cat << EOF + # To initialize your environment to use docker. + # Run: eval \$(ahoy docker env) + # OR + # Add the following to your ~/.bashrc + which docker || (echo "you don't seem to have docker installed. Exiting."; exit 1) + which docker-machine || (echo "you don't seem to have docker-machine installed. Exiting."; exit 1) + which docker-compose || (echo "you don't seem to have docker-compose installed. Exiting."; exit 1) + echo "Setting up docker environment" + export AHOY_CMD_PROXY=DOCKER + docker-machine start default + $(docker-machine env default) + ahoy docker build + EOF + usage: Outputs the commands needed to setup a docker environment. + stop: + cmd: "ahoy docker compose stop" + usage: Stop the docker-compose containers (non-destructive). + ps: + cmd: "ahoy docker compose ps" + usage: List the running docker-compose containers. + ip: + cmd: "docker-machine ip default" + usage: Show the ip address f the default docker machine VM + reset: + cmd: "ahoy docker compose stop && ahoy docker compose rm && ahoy docker compose up -d" + usage: Destroy and then restart the docker compose containers. + destroy: + cmd: "ahoy docker compose stop && ahoy docker compose rm" + usage: Destroy all the docker compose containers. (use before deleting folder) + build: + usage: Rebuild containers images + cmd: | + ahoy docker compose build {{args}} + compose: + usage: Abstraction for docker-compose + cmd: | + docker-compose -f docker-compose.yml -p "${PWD##*/}" {{args}} + cleanup: + usage: Clean ups docker unused images and volumes. See http://blog.yohanliyanage.com/2015/05/docker-clean-up-after-yourself/. + cmd: | + EXITED=`docker ps -a -q -f status=exited` + DANGLING=`docker images -f "dangling=true" -q` + if [ ! -z "$EXITED" ] + then + docker rm -v $EXITED + fi + if [ ! -z "$DANGLING" ] + then + docker rmi $DANGLING + fi + docker run -v /var/run/docker.sock:/var/run/docker.sock -v /var/lib/docker:/var/lib/docker --rm martin/docker-cleanup-volumes \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bee2aeb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:2.7 +ENV PYTHONUNBUFFERED 1 +RUN DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* +RUN mkdir /opt/pydkan +WORKDIR /opt/pydkan +ADD requirements.txt /opt/pydkan/ +RUN pip install -r requirements.txt +RUN pip install ipython +ENV PYTHONPATH=${PYTHONPATH:-/opt/pydkan/} diff --git a/README.md b/README.md index b728210..3a592c5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,66 @@ -This is the very beggining of a python client to interface with the [dkan_dataset_rest_api](https://github.com/NuCivic/dkan_dataset/tree/7.x-1.x/modules/dkan_dataset_rest_api) web service. +# PyDKAN -### Usage +This is a python client to interface with the [dkan_dataset_rest_api](https://github.com/NuCivic/dkan_dataset/tree/7.x-1.x/modules/dkan_dataset_rest_api) web service. -look inside the examples folder \ No newline at end of file +## Installation + +### As a Library + +```bash +# To install the latest of the latest +$ pip install git+git://github.com/NuCivic/pydkan.git@master#egg=pydkan +# To install a release +$ pip install git+git://github.com/NuCivic/pydkan.git@0.3#egg=pydkan +``` + +### For development with docker and ahoy + ++ Install docker ++ Install ahoy (See https://github.com/devinci-code/ahoy#installation) ++ Run the following: + +``` +$ ahoy docker build +``` + +That's it. + +## Usage + +### As a library + +Check the examples folder, there are snippets for pretty much everything you can do with this library. + +### Docker + Ahoy setup + +#### Prompt into a container with the dev environment + +``` +ahoy api prompt +``` + +#### Prompt into an ipython console: + +``` +ahoy api python +``` + +#### Run a script + +``` +ahoy api python examples/list_nodes.py +``` + +#### Run the testsuite + +```` +ahoy api tests +```` + +#### Rebuild the image + +From time to time you'll need to rebuild the docker image. For instance, if you add a requirement to the requirements.txt file and you'll need it installed. Yoy can do that by: + +``` +ahoy docker build --no-cache +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..039eca5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,4 @@ +pydkan: + build: ./ + volumes: + - ".:/opt/pydkan" diff --git a/examples/attach_file_to_node.py b/examples/attach_file_to_node.py index 1b05acc..7b3d5fa 100644 --- a/examples/attach_file_to_node.py +++ b/examples/attach_file_to_node.py @@ -1,6 +1,3 @@ -import sys -sys.path.append('..') - import os import json from dkan.client import DatasetAPI @@ -15,7 +12,8 @@ nodes = api.node(params=payload).json() resource = nodes[0] print resource - csv = os.path.join('.', 'data', 'tension_sample_data.csv') + csv = os.path.join(os.path.dirname(os.path.abspath(__file__)), '.', + 'data', 'tension_sample_data.csv') # Attach the file to the resource node r = api.attach_file_to_node(csv, resource['nid'], 'field_upload') print r.status_code diff --git a/examples/create_node.py b/examples/create_node.py index 976c926..beb880a 100644 --- a/examples/create_node.py +++ b/examples/create_node.py @@ -1,6 +1,3 @@ -import sys -sys.path.append('..') - import os import json from dkan.client import DatasetAPI diff --git a/examples/delete_node.py b/examples/delete_node.py index 2fb3e28..eeb260c 100644 --- a/examples/delete_node.py +++ b/examples/delete_node.py @@ -1,6 +1,3 @@ -import sys -sys.path.append('..') - import os import json from dkan.client import DatasetAPI diff --git a/examples/list_nodes.py b/examples/list_nodes.py index 4ae523b..471db34 100644 --- a/examples/list_nodes.py +++ b/examples/list_nodes.py @@ -1,6 +1,3 @@ -import sys -sys.path.append('..') - import os import json from dkan.client import DatasetAPI diff --git a/examples/update_node.py b/examples/update_node.py index c24b528..bd309a7 100644 --- a/examples/update_node.py +++ b/examples/update_node.py @@ -1,6 +1,3 @@ -import sys -sys.path.append('..') - import os import json from dkan.client import DatasetAPI