apiVersion: v1
kind: Secret
metadata:

  name: ckan-ext-multiple

  annotations:
    description: |-
      bash script to provide complete procedure for insallation of multiple CKAN extensions
      Optionally: other files that are used by the main install script/process

      Note: This separate config file is similar in structure to the one inside the template.
      It is provided as a convenient way to edit and load the configuration settings herein.
  labels:
    ckan.component: ckan-ext
    app: ckan
type: Opaque
stringData:
  install_ckan_ext.sh: |-
    #!/bin/bash

    # This script is called inside the DOcker build procedure
    # It will be executed in the home/root directory of the virtual environment which is $APP_ROOT/src
    # The environment is activated
    # The $APP_ROOT/src folder contains at least the "ckan/" folder where the base CKAN is installed
    #
    # The script is ran by user id 1001 with temporary name "default" -> this ensures git clone works
    # Invoke git clone while keeping in mind the current location / root for CKAN: $APP_ROOT/src
    #
    # Options described below are relative to the ones in the CKAN docs for adding extensions
    # https://docs.ckan.org/en/2.8/maintaining/installing/install-from-docker-compose.html#add-extensions
    #
    # In the case of private repos accessed via private ssh-keys that are also injected via the Secret,
    # we should define/keep the "setup_private_key" and "remove_private_key" functions below and
    # the process should be:
    #     setup_private_key /path/to/private_keyfile
    #     clone source/repo in desired location
    #     install extension
    #     remove_private_key /path/to/private_keyfile
    # this ensures that no sensitive information is kept inside the running container

    function setup_private_key() {
      echo "---> Setting up 0400 permissions for the private key file $1"
      chmod 400 "$1"
    }

    function remove_private_key() {
      echo "---> Removing private key file $1"
      rm -f "$1"
    }

    set -e

    # Option 3: Pip install from PyPi (multiple extensions)

    CKAN_EXT_LIST="ckanext-geoview ckanext-deadoralive ckanext-eurovoc"
    pip install ${CKAN_EXT_LIST}

    # Option 2: Pip install from GitHub

    pip install -e "git+https://github.com/ckan/ckanext-showcase.git#egg=ckanext-showcase"

    # Option 1: From source
    # 1.a. public repository

    git clone https://github.com/ckan/ckanext-mapviews.git
    cd ckanext-mapviews
    python setup.py install
    cd ..

    # 1.b private repository with username and password

    user="myuser"
    pass="mypass"
    repo_host="gitlab.myentity.net"
    repo="Project/My_CKAN_ext.git"
    git clone https://$user:$pass@$repo_host/$repo
    cd ckanext-civity
    git checkout develop
    git pull
    pip install .
    cd ..

    # 1.c private repo with(password-less) ssh-key
    # need to setup the private ssh-key injected from the Secret
    # use of ssh-agent to work with temporary private ssh-keys

    repo_host="gitlab.myentity.net"
    repo="Project/My_CKAN_ext.git"

    setup_private_key "$APP_ROOT/config/git_priv_key1"
    # !!! the second command is needed in order for the git-ssh auth to function!!!
    #   its purpuse is to add the "StrictHostKeyChecking=no" option by doing a connection test
    ssh-agent bash -c "ssh-add $APP_ROOT/config/git_priv_key1; \
      ssh -o StrictHostKeyChecking=no -T git@$repo_host; \
      git clone git@$repo_host$repo alt-ckanext-civity"
    cd alt-ckanext-civity
    git checkout develop
    ssh-agent bash -c "ssh-add $APP_ROOT/config/git_priv_key1; \
      ssh -o StrictHostKeyChecking=no -T git@$repo_host; \
      git pull"
    pip install .
    remove_private_key "$APP_ROOT/config/git_priv_key1"
    cd ..
  git_priv_key1: |-
    -----BEGIN RSA PRIVATE KEY-----
    MII.........
    ..........
    -----END RSA PRIVATE KEY-----
