Skip to content

Commit

Permalink
WIP #222: script inserts azure credentials into ckan.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
KatiRG committed Jun 20, 2022
1 parent 62c518f commit 14654ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
4 changes: 2 additions & 2 deletions config/cloudstorage/azure_snippet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ckanext.cloudstorage.container_name = <blob name>

ckanext.cloudstorage.driver_options = {
"key": "<storage container name>",
"secret": "<access key 1>"
}
"secret":
}

ckanext.cloudstorage.use_secure_urls = 1
ckanext.cloudstorage.max_multipart_lifetime = 7
22 changes: 18 additions & 4 deletions scripts/helper_functions.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/bash

# function to convert a string into a Sed escaped string.
# Function to convert a string into a Sed escaped string.
# This string can be used in sed replace command.
# arguments: 1 String to be modified with escape chars
# return: Modified String with escape chars
Expand All @@ -18,7 +18,21 @@ function str_to_sed_str(){
# example:
# replace_str_in_ckan_ini "replace this" "by this"
function replace_str_in_ckan_ini() {
ORIGINAL_STR = "$1"
REPLACEMENT_STR = "$2"
ORIGINAL_STR="$1"
REPLACEMENT_STR="$2"
sed -i -r 's/'"$ORIGINAL_STR"'/'"$REPLACEMENT_STR"'/' $CKANINIPATH
}
}

# replaces a given string with the replacement string inside
# a specified file.
# arguments: string needing replacement, replacement string, file
# return: none
# example:
# replace_str_in_file "replace this" "by this" file
function replace_str_in_file() {
ORIGINAL_STR="$1"
REPLACEMENT_STR="$2"
FILE="$3"
echo $REPLACEMENT_STR
sed -i -r 's/'"$ORIGINAL_STR"'/'"$REPLACEMENT_STR"'/' $FILE
}
26 changes: 17 additions & 9 deletions scripts/setup_cloudstorage.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
source ./helper_functions.sh

# Assume that the sudo password has been exported as an environment variable
# export SUDOPASS="mypassword"
Expand Down Expand Up @@ -29,23 +30,30 @@ pip3 install zope.interface==5.0.0
# Make additional py3 upgrades
cd /usr/lib/ckan/default/src/ckanext-cloudstorage
git checkout -b ckan2.9
sed -i -r 's/'"$PYLONSIMPORT"'/'"$PYLONSIMPORT_REPLACEMENT"'/g' $STORAGEPY
sed -i -r 's/'"$PYLONSIMPORT"'/'"$PYLONSIMPORT_REPLACEMENT"'/g' $MULTIPARTPY
replace_str_in_file "$PYLONSIMPORT" "$PYLONSIMPORT_REPLACEMENT" "$STORAGEPY"
replace_str_in_file "$PYLONSIMPORT" "$PYLONSIMPORT_REPLACEMENT" "$MULTIPARTPY"

python3 setup.py develop
python3 setup.py install

# Add cloudstorage to ckan.plugins in ckan.ini
sed -i -r 's/'"$CKANPLUGINS"'/'"$CKANPLUGINS_REPLACEMENT"'/g' $CKANINI
replace_str_in_file "$CKANPLUGINS" "$CKANPLUGINS_REPLACEMENT" "$CKANINI"

# Add Azure params to snippet
sed -i '/<blob name>/ r '"$AZUREBLOB"'' $AZURESNIPPET
sed -i '/<storage container name>/ r '"$AZURECONTAINER"'' $AZURESNIPPET
sed -i '/<access key 1>/ r '"$AZURESECRET"'' $AZURESNIPPET

# Add cloudstorage params to ckan.ini
# Add Azure snippet to ckan.ini
sed -i '/'"$CKANPLUGINS_REPLACEMENT"'/ r '"$AZURESNIPPET"'' $CKANINI

# Replace Azure placeholders with pre-defined values
replace_str_in_file "<blob name>" "$AZUREBLOB" "$CKANINI"
replace_str_in_file "<storage container name>" "$AZURECONTAINER" "$CKANINI"
# Escape special characters in Azure secret with str_to_sed_str()
# and duplicate any % chars
AZURESECRET_FORMATTED=$(str_to_sed_str $(sed '/%/s/$/%%/' <<<$AZURESECRET))
# Store formatted Azure secret in a text file
echo "\"${AZURESECRET_FORMATTED}\"" > secret_snippet.txt
# Add secret to ckan.ini by appending text file
sed -i '/"secret":/ r secret_snippet.txt' $CKANINI
rm secret_snippet.txt

echo $SUDOPASS | sudo -S service supervisor restart

deactivate

0 comments on commit 14654ed

Please sign in to comment.