#!/bin/bash
# This script runs python to download manifest from git.

echo '#$0: Installing GitPython and pyyaml'
#2019-02-17 python error in gitpython library

python3 -m pip install --user gitpython
python3 -m pip install --user pyyaml

# for az_script-20b-pem.py
python3 -m pip install --user pem

python3 -V
echo



echo "#$0: needs System.AccessToken=$SYSTEM_ACCESSTOKEN and System.DefaultWorkingDirectory=$SYSTEM_DEFAULTWORKINGDIRECTORY as param."

echo "The End. - python3 pip done - Do nothing - currently git manifest python in inline code in pipeline."
exit 0

cat <<EOF >> ./python-9spokes.py
# 20200217 Pieter python3 - test for 2nd dir
# 202001 Hiran add script
import os
import sys
import yaml
from git import Repo

SYSTEM_DEFAULTWORKINGDIRECTORY=sys.argv[1]
SYSTEM_ACCESSTOKEN=sys.argv[2]
artifactfile = SYSTEM_DEFAULTWORKINGDIRECTORY + \
                "/Drop/drop/build.manifest/artifacts.yaml"

if not os.path.isfile(artifactfile):
    print(" artifacts.yaml not found in ./Drop/drop/build.manifest/artifacts.yaml ")
    artifactfile = SYSTEM_DEFAULTWORKINGDIRECTORY + \
                    "/_manifest-docker/drop/build.manifest/artifacts.yaml"
    if not os.path.isfile(artifactfile):
        print(" artifacts.yaml not found in ./_manifest-docker/drop/build.manifest/artifacts.yaml ")
        sys.exit(1)

print("found artifactfile:", artifactfile )
stream = open(artifactfile, 'r')
data = yaml.load(stream)
helmchart = data['images']['helmchart']['SourceVersion']
helmchartversion = helmchart.split(":",1)[1]
git_url = "https://"+ SYSTEM_ACCESSTOKEN + \
            "@dev.azure.com/9spokes/Infrastructure/_git/9spokeshelmchart"
#
repo_dir = "9spokeshelmchart"
print( "git_url:", git_url )
repo = Repo.clone_from(git_url, repo_dir, branch="master")
git = repo.git
print("now checkout ",helmchartversion)
git.checkout(helmchartversion)

print( "repo_dir:", repo_dir, "  git commit:", repo.head.commit.hexsha )
print("The End.")

EOF

./python-9spokes.py