Skip to content

Commit

Permalink
Adding helm reelase init code
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpro committed Jul 9, 2022
1 parent 3a186f7 commit ea2e641
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/api/helm-ops.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
function pull_chart_from_release() {
local msg="usage: helm release pull <RELEASE NAME>"
if [[ $# < 1 ]]; then exit_with_help "$msg"; fi

RELEASE=$1
NAMESPACE=$HELM_NAMESPACE

RELEASE_STATUS=$(helm -n $NAMESPACE status $RELEASE -o json)
if [ $? -eq 0 ]; then
echo $RELEASE_STATUS | jq > $(dirname -- "$0")/data/release-$RELEASE-status.json;
VERSION=$(cat $(dirname -- "$0")/data/release-$RELEASE-status.json | jq -r .version)

RELEASE_CONTENT=$(kubectl \
get secrets sh.helm.release.v1.$RELEASE.v$VERSION \
-n $NAMESPACE \
-o jsonpath='{.data.release}' \
| base64 -d | base64 -d | gzip -d | jq | tee $(dirname -- "$0")/data/release-$RELEASE-content.json)

CHART_NAME=$(cat $(dirname -- "$0")/data/release-$RELEASE-content.json | jq -r .chart.metadata.name)
CHART_VERSION=$(cat $(dirname -- "$0")/data/release-$RELEASE-content.json | jq -r .chart.metadata.version)
CHART_DIR=$CHART_NAME-$CHART_VERSION

mkdir -p $CHART_DIR

# sudo wget https://github.com/mikefarah/yq/releases/download/v4.25.3/yq_linux_amd64 -O /usr/bin/yq
echo $RELEASE_CONTENT | jq -r '.chart.metadata' | yq e -P - > $CHART_DIR/Chart.yaml
echo $RELEASE_CONTENT | jq -r '.chart.values' | yq e -P - > $CHART_DIR/values.yaml

# Create Chart Templates
for row in $(echo $RELEASE_CONTENT | jq -r '.chart.templates[] | @base64'); do
#echo $row
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
filename=$(echo $(_jq '.name'))
data=$(echo $(_jq '.data') )
mkdir -p $(dirname $CHART_DIR/$filename)
echo $data | base64 -d > $CHART_DIR/$filename
done
# Create Chart files
for row in $(echo $RELEASE_CONTENT | jq -r '.chart.files[] | @base64'); do
#echo $row
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
filename=$(echo $(_jq '.name'))
data=$(echo $(_jq '.data') )
mkdir -p $(dirname $CHART_DIR/$filename)
echo $data | base64 -d > $CHART_DIR/$filename
done
fi
rm_dir './data'
return 0;
}
17 changes: 17 additions & 0 deletions lib/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

# Error Handling
function on_error() { echo "error: [ ${BASH_SOURCE[1]} at line ${BASH_LINENO[0]} ]"; }
set -o errtrace
trap on_error ERR

# Alias Expansion
shopt -s expand_aliases
alias kwargs='(( $# )) && local'

# Kill Script Function
trap exit TERM
alias exit_script='kill -s TERM $$'

# Set Script Globals
_ROOT="$( cd "$(dirname "$0")" ; pwd -P )"
29 changes: 29 additions & 0 deletions lib/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
source $(dirname -- "$0")/lib/config.sh
source $(dirname -- "$0")/lib/utility.sh
source $(dirname -- "$0")/lib/api/helm-ops.sh


function main() {

init_dir './data'

declare -A -x command_table=(
['pull']="pull_chart_from_release"
)

local commands="${!command_table[@]}"
local msg="usage: ./release.sh [ $commands ]"
if [[ $# < 1 ]]; then exit_with_help "$msg"; fi

local command=${1}; shift
local fn_name=${command_table[$command]}

if [[ $fn_name == '' ]]; then exit_with_help "$msg"; fi
if $fn_name $@; then
rm -Rf $(dirname -- "$0")/data;
return 0;
else
return 1;
fi
}
15 changes: 15 additions & 0 deletions lib/utility.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

function exit_with_help() { echo $1 && exit_script; }

function get_timestamp() { printf $(date '+%Y-%m-%d_%H%M%S'); }

function init_dir() { [[ -d $_ROOT'/'$1 ]] || mkdir $_ROOT'/'$1; }

function rm_dir() { [[ -d $_ROOT'/'$1 ]] || rm -rf $_ROOT'/'$1; }

function str_split() {

declare -a -x -g str_split_result=()
IFS="$2"; read -ra str_split_result <<< $1; IFS=' '
}
6 changes: 6 additions & 0 deletions plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Name: "release"
version: "0.1.0"
usage: "pull or update Helm Releases"
description: |-
Update values of a releases, pull charts from releases
command: "$HELM_PLUGIN_DIR/release.sh"
7 changes: 7 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

# import library code
source $(dirname -- "$0")/lib/main.sh

# pass args to main()
main $@

0 comments on commit ea2e641

Please sign in to comment.