From ad08241d76b8f7ce479b5a07088116190bbb1a93 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Wed, 7 Jun 2017 16:30:35 +0200 Subject: [PATCH] library/roles/ckan, library/roles/smartgears/ckan_connector: Now it is possible to choose to not initialize the database and create the admin user. --- ckan/ckan/defaults/main.yml | 3 ++ ckan/ckan/tasks/ckan.yml | 1 + smartgears/ckan_connector/tasks/main.yml | 3 +- .../templates/ckan-connector-fixer.sh.j2 | 35 +++++++++++++++++-- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ckan/ckan/defaults/main.yml b/ckan/ckan/defaults/main.yml index c6f7b984..ae73470b 100644 --- a/ckan/ckan/defaults/main.yml +++ b/ckan/ckan/defaults/main.yml @@ -25,6 +25,9 @@ ckan_logdir: /var/log/ckan ckan_db_name: ckan ckan_db_user: ckan +# By default, initialize the db and solr. Disable if you want to reinstall and maintain the old data +ckan_init_db_and_solr: True + # CKAN plugins ckan_plugins_state: latest # Order is important diff --git a/ckan/ckan/tasks/ckan.yml b/ckan/ckan/tasks/ckan.yml index 3e37110b..7eb10265 100644 --- a/ckan/ckan/tasks/ckan.yml +++ b/ckan/ckan/tasks/ckan.yml @@ -37,6 +37,7 @@ args: creates: '{{ ckan_libdir }}/.ckan_db_initialized' ignore_errors: True + when: ckan_init_db_and_solr tags: ckan - name: Create the pip cache directory with the right permissions diff --git a/smartgears/ckan_connector/tasks/main.yml b/smartgears/ckan_connector/tasks/main.yml index a0c004a7..d61093a5 100644 --- a/smartgears/ckan_connector/tasks/main.yml +++ b/smartgears/ckan_connector/tasks/main.yml @@ -1,6 +1,5 @@ --- - block: - - name: Remove the installed CKAN connector before upgrading file: dest={{ item }} state=absent with_items: @@ -25,7 +24,7 @@ - name: Fix the CKAN connector web.xml become: False - shell: /usr/local/bin/ckan-connector-fixer + shell: /usr/local/bin/ckan-connector-fixer init args: creates: '{{ smartgears_instance_path }}/webapps/ckan-connector/WEB-INF/.web.xml.configured' notify: Restart smartgears diff --git a/smartgears/ckan_connector/templates/ckan-connector-fixer.sh.j2 b/smartgears/ckan_connector/templates/ckan-connector-fixer.sh.j2 index de17df2c..992e2e82 100644 --- a/smartgears/ckan_connector/templates/ckan-connector-fixer.sh.j2 +++ b/smartgears/ckan_connector/templates/ckan-connector-fixer.sh.j2 @@ -1,5 +1,12 @@ #!/bin/bash +if [ $# -ne 1 ] ; then + logger 'ckan-connector-fixer: at least one argument must be passed' + echo "at leas one argument must be passed, init or update" + exit 1 +fi + +ARG=$1 CK_WEB_DIR={{ smartgears_instance_path }}/webapps/ckan-connector/WEB-INF CK_WEB_XML=$CK_WEB_DIR/.web.xml.tpl CK_WEB_XML_TMP=$CK_WEB_DIR/.web.xml.tmp @@ -13,20 +20,43 @@ CK_INI=/etc/ckan/default/production.ini API_KEY= CKAN_KEY= SQL_FILE=$( mktemp /tmp/XXXXXX.sql ) +CKAN_CREATE_USER={{ ckan_init_db_and_solr }} + +trap "logger 'ckan-connector-fixer: trap intercepted, exiting.' ; cleanup" SIGHUP SIGINT SIGTERM + +function cleanup() { + rm -f $SQL_FILE + rm -f $CK_WEB_XML_TMP +} + + +if [ -f $CK_WEB_DIR/.web.xml.configured ] ; then + if [ "$ARG" != "update" ] ; then + logger 'ckan-connector-fixer: service already configured, doing nothing.' + echo "service already configured. Use 'update' as argument to force the execution" + exit 0 + fi +fi {% raw %} # Set the CKAN python virtualenv . /usr/lib/ckan/default/bin/activate cd /usr/lib/ckan/default/src/ckan + +if [ "$CKAN_CREATE_USER" == "True" ] ; then # Create the admin user -paster sysadmin add $CK_ADMIN -c $CK_INI << EOF + paster sysadmin add $CK_ADMIN -c $CK_INI << EOF + y $CK_ADMIN_PWD $CK_ADMIN_PWD + EOF +fi + # Get the admin key echo "update \"user\" set state = 'active' where name = '${CK_ADMIN}';" > $SQL_FILE echo "select apikey from \"user\" where name = '${CK_ADMIN}' and state = 'active';" >> $SQL_FILE @@ -59,10 +89,9 @@ mv $CK_WEB_XML_TMP $CK_WEB_XML_DEST chown {{ smartgears_user }}:{{ smartgears_user }} $CK_WEB_XML_DEST chmod 440 $CK_WEB_XML_DEST -rm -f $SQL_FILE touch $CK_WEB_DIR/.web.xml.configured - +trap cleanup EXIT exit 0