<commit_before>#!/bin/sh

set -e
#set -x

DIR=$(realpath $(dirname $0))/../
BUILDDIR=$(realpath $DIR)/build

# Ensure we're on the master branch
check_branch() {
    version=$1
    curr_branch=$(git rev-parse --abbrev-ref HEAD)
    if [ $curr_branch != "v${version}" ]
    then
	echo "Not on v${version} branch."
	exit 1
    fi
}

# Ensure working dir is clean
check_clean() {    
    if ! git diff-index --quiet HEAD --
    then
	echo "Unclean tree"
	exit 1
    fi
}

# Build CLI binaries for mac/linux/windows
build_all_cli() {
    build_cli "linux" "linux"
    build_cli "darwin" "osx"
    build_cli "windows" "windows"
}

# Build cli binary for one OS, and put it in $BUILDDIR/cli/<os>/
build_cli() {
    os=$1
    osName=$2
    arch="amd64" # parameterize if/when we need to
    
    pushd $DIR/fission

    if [ "$osName" == "windows" ]
    then
	binary=fission-cli-${osName}.exe
    else
	binary=fission-cli-${osName}
    fi

    GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o $binary .

    outdir=$BUILDDIR/cli/$osName/
    mkdir -p $outdir
    mv $binary $outdir
    
    popd
}

# Build fission-bundle image
build_fission_bundle_image() {
    version=$1
    tag=fission/fission-bundle:$version

    pushd $DIR/fission-bundle

    ./build.sh
    docker build -t $tag .
   
    popd
}

# Push fission-bundle image
push_fission_bundle_image() {
    version=$1
    tag=fission/fission-bundle:$version
    docker push $tag
}

build_fetcher_image() {
    version=$1
    tag=fission/fetcher:$version

    pushd $DIR/environments/fetcher/cmd

    ./build.sh
    docker build -t $tag .

    popd    
}

push_fetcher_image() {
    version=$1
    tag=fission/fetcher:$version
    docker push $tag
}

build_builder_image() {
    version=$1
    tag=fission/builder:$version

    pushd $DIR/builder/cmd

    ./build.sh
    docker build -t $tag .

    popd
}

push_builder_image() {
    version=$1
    tag=fission/builder:$version
    docker push $tag
}

build_logger_image() {
    version=$1
    tag=fission/fluentd:$version

    pushd $DIR/logger/fluentd

    docker build -t $tag .

    popd
}

push_logger_image() {
    version=$1
    tag=fission/fluentd:$version
    docker push $tag
}

build_and_push_logger_image() {
    build_logger_image $1
    push_logger_image $1
}

build_and_push_env_image() {
    version=$1
    envdir=$2
    imgnamebase=$3
    imgvariant=$4

    if [ -z "$imgvariant" ]
    then 
        # no variant specified, just use the base name
        imgname=$imgnamebase
        dockerfile="Dockerfile"
    else 
        # variant specified - append variant to image name and assume dockerfile 
        # exists with same suffix (e.g. image node-env-debian built from Dockerfile-debian)
        imgname="$imgname-$imgvariant"
        dockerfile="Dockerfile-$imgvariant"
    fi
    echo "Building $envdir -> $imgname:$version using $dockerfile"
    
    pushd $DIR/environments/$envdir
    if [ -f build.sh ]
    then
       ./build.sh
    fi  
    docker build -t fission/$imgname:$version -f $dockerfile .
    docker tag fission/$imgname:$version fission/$imgname:latest
    docker push fission/$imgname:$version
    docker push fission/$imgname:latest
    popd
}

build_and_push_all_envs() {
    version=$1

    # call with version, env dir, image name base, image name variant
    build_and_push_env_image "$version" "nodejs"   "node-env"     ""
    build_and_push_env_image "$version" "nodejs"   "node-env"     "debian"
    build_and_push_env_image "$version" "binary"   "binary-env"   ""
    build_and_push_env_image "$version" "dotnet"   "dotnet-env"   ""
    build_and_push_env_image "$version" "dotnet20" "dotnet20-env" ""    
    build_and_push_env_image "$version" "go"       "go-env"       ""
    build_and_push_env_image "$version" "perl"     "perl-env"     ""
    build_and_push_env_image "$version" "php7"     "php-env"      ""
    build_and_push_env_image "$version" "python"   "python-env"   ""
    build_and_push_env_image "$version" "python"   "python-env"   "2.7"
    build_and_push_env_image "$version" "ruby"     "ruby-env"     ""
}

build_and_push_env_builder_image() {
    version=$1
    envdir=$2
    imgnamebase=$3
    imgvariant=$4

    if [ -z "$imgvariant" ]
    then
        # no variant specified, just use the base name
        imgname=$imgnamebase
        dockerfile="Dockerfile"
    else
        # variant specified - append variant to image name and assume dockerfile
        # exists with same suffix (e.g. image node-env-debian built from Dockerfile-debian)
        imgname="$imgname-$imgvariant"
        dockerfile="Dockerfile-$imgvariant"
    fi
    echo "Building $envdir -> $imgname:$version using $dockerfile"

    pushd $DIR/environments/$envdir/builder
    docker build -t fission/$imgname:$version -f $dockerfile .
    docker tag fission/$imgname:$version fission/$imgname:latest
    docker push fission/$imgname:$version
    docker push fission/$imgname:latest
    popd
}

build_and_push_all_env_builders() {
    version=$1

    # call with version, env dir, image name base, image name variant
    build_and_push_env_builder_image "$version" "python"   "python-builder"   ""
    build_and_push_env_builder_image "$version" "binary"   "binary-builder"   ""
    build_and_push_env_builder_image "$version" "go"       "go-builder"       ""
}

build_charts() {
    version=$1
    mkdir -p $BUILDDIR/charts
    pushd $DIR/charts
    find . -iname *.~?~ | xargs rm
    for c in fission-all fission-core
    do
	helm package $c/
	mv *.tgz $BUILDDIR/charts/
    done
    popd
}

build_all() {
    version=$1
    if [ -z "$version" ]
    then
	echo "Version unspecified"
	exit 1
    fi
    
    if [ -e $BUILDDIR ]
    then
	echo "Removing existing build dir ($BUILDDIR)."
	rm -rf $BUILDDIR
    fi
    
    mkdir -p $BUILDDIR
    
    build_fission_bundle_image $version
    build_fetcher_image $version
    build_builder_image $version
    build_logger_image $version
    build_all_cli
    build_charts $version
}

push_all() {
    push_fission_bundle_image $version
    push_fetcher_image $version
    push_builder_image $version
    push_logger_image $version
}

tag_and_release() {
    version=$1
    gittag=$version

    # tag the release
    git tag $gittag
    
    # push tag
    git push --tags

    # create gh release
    gothub release \
	   --user fission \<commit_msg>Latest tags for fetcher and fission-bundle (#509)<commit_after>#!/bin/sh

set -e
#set -x

DIR=$(realpath $(dirname $0))/../
BUILDDIR=$(realpath $DIR)/build

# Ensure we're on the master branch
check_branch() {
    version=$1
    curr_branch=$(git rev-parse --abbrev-ref HEAD)
    if [ $curr_branch != "v${version}" ]
    then
	echo "Not on v${version} branch."
	exit 1
    fi
}

# Ensure working dir is clean
check_clean() {    
    if ! git diff-index --quiet HEAD --
    then
	echo "Unclean tree"
	exit 1
    fi
}

# Build CLI binaries for mac/linux/windows
build_all_cli() {
    build_cli "linux" "linux"
    build_cli "darwin" "osx"
    build_cli "windows" "windows"
}

# Build cli binary for one OS, and put it in $BUILDDIR/cli/<os>/
build_cli() {
    os=$1
    osName=$2
    arch="amd64" # parameterize if/when we need to
    
    pushd $DIR/fission

    if [ "$osName" == "windows" ]
    then
	binary=fission-cli-${osName}.exe
    else
	binary=fission-cli-${osName}
    fi

    GOOS=$os GOARCH=$arch go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o $binary .

    outdir=$BUILDDIR/cli/$osName/
    mkdir -p $outdir
    mv $binary $outdir
    
    popd
}

# Build fission-bundle image
build_fission_bundle_image() {
    version=$1
    tag=fission/fission-bundle:$version

    pushd $DIR/fission-bundle

    ./build.sh
    docker build -t $tag .
    docker tag $tag fission/fission-bundle:latest
   
    popd
}

# Push fission-bundle image
push_fission_bundle_image() {
    version=$1
    tag=fission/fission-bundle:$version
    docker push $tag
}

build_fetcher_image() {
    version=$1
    tag=fission/fetcher:$version

    pushd $DIR/environments/fetcher/cmd

    ./build.sh
    docker build -t $tag .
    docker tag $tag fission/fetcher:latest

    popd    
}

push_fetcher_image() {
    version=$1
    tag=fission/fetcher:$version
    docker push $tag
}

build_builder_image() {
    version=$1
    tag=fission/builder:$version

    pushd $DIR/builder/cmd

    ./build.sh
    docker build -t $tag .

    popd
}

push_builder_image() {
    version=$1
    tag=fission/builder:$version
    docker push $tag
}

build_logger_image() {
    version=$1
    tag=fission/fluentd:$version

    pushd $DIR/logger/fluentd

    docker build -t $tag .

    popd
}

push_logger_image() {
    version=$1
    tag=fission/fluentd:$version
    docker push $tag
}

build_and_push_logger_image() {
    build_logger_image $1
    push_logger_image $1
}

build_and_push_env_image() {
    version=$1
    envdir=$2
    imgnamebase=$3
    imgvariant=$4

    if [ -z "$imgvariant" ]
    then 
        # no variant specified, just use the base name
        imgname=$imgnamebase
        dockerfile="Dockerfile"
    else 
        # variant specified - append variant to image name and assume dockerfile 
        # exists with same suffix (e.g. image node-env-debian built from Dockerfile-debian)
        imgname="$imgname-$imgvariant"
        dockerfile="Dockerfile-$imgvariant"
    fi
    echo "Building $envdir -> $imgname:$version using $dockerfile"
    
    pushd $DIR/environments/$envdir
    if [ -f build.sh ]
    then
       ./build.sh
    fi  
    docker build -t fission/$imgname:$version -f $dockerfile .
    docker tag fission/$imgname:$version fission/$imgname:latest
    docker push fission/$imgname:$version
    docker push fission/$imgname:latest
    popd
}

build_and_push_all_envs() {
    version=$1

    # call with version, env dir, image name base, image name variant
    build_and_push_env_image "$version" "nodejs"   "node-env"     ""
    build_and_push_env_image "$version" "nodejs"   "node-env"     "debian"
    build_and_push_env_image "$version" "binary"   "binary-env"   ""
    build_and_push_env_image "$version" "dotnet"   "dotnet-env"   ""
    build_and_push_env_image "$version" "dotnet20" "dotnet20-env" ""    
    build_and_push_env_image "$version" "go"       "go-env"       ""
    build_and_push_env_image "$version" "perl"     "perl-env"     ""
    build_and_push_env_image "$version" "php7"     "php-env"      ""
    build_and_push_env_image "$version" "python"   "python-env"   ""
    build_and_push_env_image "$version" "python"   "python-env"   "2.7"
    build_and_push_env_image "$version" "ruby"     "ruby-env"     ""
}

build_and_push_env_builder_image() {
    version=$1
    envdir=$2
    imgnamebase=$3
    imgvariant=$4

    if [ -z "$imgvariant" ]
    then
        # no variant specified, just use the base name
        imgname=$imgnamebase
        dockerfile="Dockerfile"
    else
        # variant specified - append variant to image name and assume dockerfile
        # exists with same suffix (e.g. image node-env-debian built from Dockerfile-debian)
        imgname="$imgname-$imgvariant"
        dockerfile="Dockerfile-$imgvariant"
    fi
    echo "Building $envdir -> $imgname:$version using $dockerfile"

    pushd $DIR/environments/$envdir/builder
    docker build -t fission/$imgname:$version -f $dockerfile .
    docker tag fission/$imgname:$version fission/$imgname:latest
    docker push fission/$imgname:$version
    docker push fission/$imgname:latest
    popd
}

build_and_push_all_env_builders() {
    version=$1

    # call with version, env dir, image name base, image name variant
    build_and_push_env_builder_image "$version" "python"   "python-builder"   ""
    build_and_push_env_builder_image "$version" "binary"   "binary-builder"   ""
    build_and_push_env_builder_image "$version" "go"       "go-builder"       ""
}

build_charts() {
    version=$1
    mkdir -p $BUILDDIR/charts
    pushd $DIR/charts
    find . -iname *.~?~ | xargs rm
    for c in fission-all fission-core
    do
	helm package $c/
	mv *.tgz $BUILDDIR/charts/
    done
    popd
}

build_all() {
    version=$1
    if [ -z "$version" ]
    then
	echo "Version unspecified"
	exit 1
    fi
    
    if [ -e $BUILDDIR ]
    then
	echo "Removing existing build dir ($BUILDDIR)."
	rm -rf $BUILDDIR
    fi
    
    mkdir -p $BUILDDIR
    
    build_fission_bundle_image $version
    build_fetcher_image $version
    build_builder_image $version
    build_logger_image $version
    build_all_cli
    build_charts $version
}

push_all() {
    push_fission_bundle_image $version
    push_fission_bundle_image latest
    push_fetcher_image $version
    push_fetcher_image latest
    push_builder_image $version
    push_logger_image $version
}

tag_and_release() {
    version=$1
    gittag=$version

    # tag the release
    git tag $gittag
    
    # push tag
    git push --tags

    # create gh release
    gothub release \
	   --user fission \
{'fraction_non_alphanumeric': 0.11057098765432098, 'fraction_numerical': 0.00625, 'mean_word_length': 2.806461086637298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6765047', 'n_tokens_mistral': 4603, 'n_tokens_neox': 4378, 'n_words': 1042}