diff --git a/.travis.yml b/.travis.yml index 34cdabe..2e73132 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,10 @@ sudo: required dist: xenial # ubuntu 16.04 distro script: - # runs only when PR is merged - - make versionmaker + # Install dependencies + - make deps + # It will ignore if travis ci committed, else works as it is. + - author=`git log -1 --pretty=format:'%an'` && if [[ "$author" != "Travis CI" ]]; then make versionmaker; fi - make combineExpCR after_success: # runs only when PR is merged and push the version file to master version diff --git a/Makefile b/Makefile index f81845a..473f7f5 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,23 @@ -# Makefile for building Chaos Exporter +# Makefile for building Chaos charts # Reference Guide - https://www.gnu.org/software/make/manual/make.html +.PHONY: deps +deps: + @echo "-----Install dependencies-----" + sudo apt-get update + sudo apt-get install python3 + sudo apt-get install python3-pip -y + pip3 install packaging + .PHONY: versionmaker versionmaker: @echo "-----version maker-----" - bash ./scripts/version_maker.sh + bash ./scripts/version/version_maker.sh + +.PHONY: combineExpCR +combineExpCR: + @echo "--------Combining Experiments CR-------" + bash ./scripts/combine_all_cr.sh .PHONY: combineExpCR combineExpCR: @@ -14,4 +27,4 @@ combineExpCR: .PHONY: push push: @echo "---------git push to master-------" - bash ./scripts/push.sh + bash ./scripts/version/push.sh diff --git a/charts/openebs/pod_delete/experiment.yaml b/charts/openebs/pod_delete/experiment.yaml index cd39d9f..24ba876 100644 --- a/charts/openebs/pod_delete/experiment.yaml +++ b/charts/openebs/pod_delete/experiment.yaml @@ -5,8 +5,8 @@ description: Deletes a pod belonging to a deployment/statefulset/daemonset kind: ChaosExperiment metadata: - name: kubernetes-state-pod-delete-v0.1.0 - version: v0.1.0 + name: kubernetes-state-pod-delete-0.1.0 + version: 0.1.0 spec: definition: args: diff --git a/scripts/check_csv.sh b/scripts/check_csv.sh deleted file mode 100644 index bd2dde8..0000000 --- a/scripts/check_csv.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -echo "test the chart service version file for sanity.." diff --git a/scripts/combine_all_cr.sh b/scripts/combine_all_cr.sh index 3c5356f..70b2989 100644 --- a/scripts/combine_all_cr.sh +++ b/scripts/combine_all_cr.sh @@ -16,4 +16,3 @@ echo "validating combine charts for generic" cat ./charts/generic/experiments.yaml echo "validating combine charts for openebs" cat ./charts/openebs/experiments.yaml - diff --git a/scripts/validate_version.py b/scripts/validate_version.py deleted file mode 100644 index 4f94743..0000000 --- a/scripts/validate_version.py +++ /dev/null @@ -1,13 +0,0 @@ -import re -import sys - -# semantic version regex -regex = "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" -result = re.search(regex, sys.argv[1]) - -if result: - print("version validation successful") - sys.exit(0) # on success -else: - print("version validation failed") - sys.exit(1) #on failure \ No newline at end of file diff --git a/scripts/push.sh b/scripts/version/push.sh similarity index 95% rename from scripts/push.sh rename to scripts/version/push.sh index 1f5322c..c3aaadb 100644 --- a/scripts/push.sh +++ b/scripts/version/push.sh @@ -1,5 +1,4 @@ #!/bin/sh - setup_git() { git config --global user.email "travis@travis-ci.org" git config --global user.name "Travis CI" @@ -20,5 +19,5 @@ upload_files() { } setup_git -commit_website_files +commit_updated_changes upload_files diff --git a/scripts/version/readme.md b/scripts/version/readme.md new file mode 100644 index 0000000..e67af6f --- /dev/null +++ b/scripts/version/readme.md @@ -0,0 +1,11 @@ +## Version + +## Implementation +Whenever there is any commit in charts it will increment the patch version by one unit + +Steps: +1. version_maker.sh will take the second last commit because the latest commit is the changes which are done by the user by which Travis is triggered. + +2. It compares the changes with the second last commit and stores the changed file to an array. + +3. It will go through each file and increment a patch version to each changed file \ No newline at end of file diff --git a/scripts/version/version_maker.sh b/scripts/version/version_maker.sh new file mode 100644 index 0000000..0a6a7ed --- /dev/null +++ b/scripts/version/version_maker.sh @@ -0,0 +1,78 @@ +#! /bin/sh + +# Retrive the last pushed commit from the repo +second_last_commit_hash=`git log -n 2 --pretty=format:"%H" | tail -1` +echo "Second Last commit hash: $second_last_commit_hash" + +# # This function is used to parse the yaml file. +function yaml_parser() { + local prefix=$2 + local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') + sed -ne "s|^\($s\):|\1|" \ + -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ + -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | + awk -F$fs '{ + indent = length($1)/2; + vname[indent] = $2; + for (i in vname) {if (i > indent) {delete vname[i]}} + if (length($3) > 0) { + vn=""; for (i=0; i> temp.yaml` + + eval $(yaml_parser './temp.yaml') + if [[ $? == 0 ]] + then + oldversion=$metadata_version + echo "Oldversion : $oldversion" + sudo python3 scripts/version/version_validator.py $existing_version $oldversion + ret_code=$? + + if [[ $ret_code == 0 ]]; then + echo "$file's version updated from $oldversion to $existing_version" + elif [[ $ret_code == 2 ]]; then + # storing version to an array + versions=( ${oldversion//./ } ) + ((versions[2]++)) # Increment the patch version by one unit + newversion="${versions[0]}.${versions[1]}.${versions[2]}" + + `sed -i "s/$existing_version/$newversion/" $file` + echo "$file's version updated from $oldversion to $existing_version" + fi + fi + # deleting the temporary file + rm './temp.yaml' + fi +} + +# compare and retrive the changed files +check_diff=`git diff ${second_last_commit_hash} --name-only` +files=$(echo $check_diff | tr " " "\n") + +for file in $files +do + echo $file + # For chart service version or experiment + if [[ "$file" =~ \version.yaml$ ]] || [[ "$file" =~ \experiment.yaml$ ]]; then + versionInc $file + fi +done + diff --git a/scripts/version/version_validator.py b/scripts/version/version_validator.py new file mode 100644 index 0000000..b857dbe --- /dev/null +++ b/scripts/version/version_validator.py @@ -0,0 +1,22 @@ +import re +import sys +from packaging import version +# semantic version regex +regex = "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" +existing_version = sys.argv[1] +old_version = sys.argv[2] +result = re.search(regex, existing_version) + +if result: + if version.parse(existing_version) > version.parse(old_version): + print("version validation successful") + sys.exit(0) # on success + elif version.parse(existing_version) == version.parse(old_version): + print("versions are equal") + sys.exit(2) # on success + else: + print("version validation failed") + sys.exit(1) #on failure +else: + print("version validation failed") + sys.exit(1) #on failure \ No newline at end of file diff --git a/scripts/version_maker.sh b/scripts/version_maker.sh deleted file mode 100644 index 516d321..0000000 --- a/scripts/version_maker.sh +++ /dev/null @@ -1,80 +0,0 @@ -#! /bin/bash - -# parsing yaml file -function yaml() { - local prefix=$2 - local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034') - sed -ne "s|^\($s\):|\1|" \ - -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ - -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | - awk -F$fs '{ - indent = length($1)/2; - vname[indent] = $2; - for (i in vname) {if (i > indent) {delete vname[i]}} - if (length($3) > 0) { - vn=""; for (i=0; i