<commit_before>#!/bin/bash

# This file is responsible for building rpm and deb package for jfrog-cli installer

# This will contain hold the list of supported architectures which can be built by default.
# Although by passing a different --rpm-build-image or --rpm-build-image, artifacts of different architectures can be built
SUPPORTED_DEFAULT_ARCH_LIST="x86_64"

JFROG_CLI_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")/../" && pwd)"
JFROG_CLI_PKG="$JFROG_CLI_HOME/pkg"
JFROG_CLI_PREFIX="jfrog-cli"

RPM_BUILDER_NAME="jfrog-cli-rpm-builder"
RPM_IMAGE_ROOT_DIR="/opt/jfrog-cli"
RPM_DEPS="rpm-build"

DEB_BUILDER_NAME="jfrog-cli-deb-builder_3"
DEB_IMAGE_ROOT_DIR="/opt/jfrog-cli"
DEB_DEPS="devscripts build-essential lintian debhelper"

usage () {
    echo "Usage: ${0}"
    cat << END_USAGE

-b | --binary                                     : [mandatory] jfrog cli binary to be packaged in rpm/deb
-v | --version                                    : [mandatory] version of rpm/deb to be built
-t | --test                                       : [optional] test the generated rpm and deb
--rpm-build-image                                 : [optional] docker image to be used to build rpm
--deb-build-image                                 : [optional] docker image to be used to build deb
--rpm-test-image                                  : [optional] docker image to be used to test the generated rpm
--deb-test-image                                  : [optional] docker image to be used to test the generated deb
-f | --flavour                                    : [optional] flavours to be built (default : "rpm deb")
-h | --help                                       : [optional] display usage

END_USAGE
    sleep .5
}

log(){
	echo "$1"
}

errorExit() {
    echo -e "\033[31mERROR: $1 \033[0m"; echo
    exit 1
}

checkDockerAccess() {
	log "Checking docker" "DEBUG"
	docker -v > /dev/null 2>&1 && docker ps > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    errorExit "Must run as user that can execute docker commands"
	fi
	log "Docker is avaiable" "DEBUG"
}

exitWithUsage(){
	log "ERROR : $1"
	usage
	exit 1
}

createDEBPackage(){
	local flavour="deb"

	# cleanup old files and containers
	rm -f ${JFROG_CLI_PKG}/${JFROG_CLI_PREFIX}*${VERSION_FORMATTED}*.${flavour}
	docker rm -f "${RPM_BUILDER_NAME}" 2>/dev/null

	log "Building ${JFROG_CLI_PREFIX} ${flavour} ${JFROG_CLI_VERSION} on ${DEB_BUILD_IMAGE} image"

    docker run -it --rm -v "${JFROG_CLI_HOME}/${flavour}":${DEB_IMAGE_ROOT_DIR}/src \
					-v "${JFROG_CLI_PKG}":${DEB_IMAGE_ROOT_DIR}/pkg \
					--name ${DEB_BUILDER_NAME} \
							${DEB_BUILD_IMAGE} bash -c "\
										\
										echo '' && echo '' && \
										apt-get update && \
										apt-get install -y fakeroot && \
										DEBIAN_FRONTEND=noninteractive apt-get install -y ${DEB_DEPS} \
										--no-install-recommends tzdata && \
										echo '' && echo '' && \
									\
									\
										cp -fr ${DEB_IMAGE_ROOT_DIR}/src ${DEB_IMAGE_ROOT_DIR}-build && \
										cd ${DEB_IMAGE_ROOT_DIR}-build && \
										sed -i -e 's|__VERSION__|'${JFROG_CLI_VERSION}'|g' ${DEB_IMAGE_ROOT_DIR}-build/debian/changelog && \
										\
										\
										debuild -us -uc && \
										\
										\
										mkdir -p ${DEB_IMAGE_ROOT_DIR}/pkg && \
										cp -f ${DEB_IMAGE_ROOT_DIR}-build/../${JFROG_CLI_PREFIX}*.${flavour} \
											  ${DEB_IMAGE_ROOT_DIR}/pkg/${JFROG_CLI_PREFIX}-${VERSION_FORMATTED}.${flavour} && \
									\
									\
										echo '' && echo '' && \
										echo '############ Build successful ###################' && \
										ls -ltr ${DEB_IMAGE_ROOT_DIR}/pkg/ | grep ${flavour} && \
										echo '#################################################' || \
										exit 1 \
									" || errorExit "------------- Build Failed ! ------------------"
}

createRPMPackage(){
	local flavour="rpm"

	# cleanup old files and containers
	rm -f ${JFROG_CLI_PKG}/${JFROG_CLI_PREFIX}*${VERSION_FORMATTED}*.${flavour}
	docker rm -f "${RPM_BUILDER_NAME}" 2>/dev/null

	log "Building ${JFROG_CLI_PREFIX} ${flavour} ${JFROG_CLI_VERSION} on ${RPM_BUILD_IMAGE} image"

    docker run -it --rm -v "${JFROG_CLI_HOME}/${flavour}":${RPM_IMAGE_ROOT_DIR}/src \
					-v "${JFROG_CLI_PKG}":${RPM_IMAGE_ROOT_DIR}/pkg \
					--name ${RPM_BUILDER_NAME} \
							${RPM_BUILD_IMAGE} bash -c "\
										echo '' && echo '' && \
										yum install -y ${RPM_DEPS} && \
										echo '' && echo '' && \
									\
									\
										rpmbuild -bb \
											--define='_tmppath ${RPM_IMAGE_ROOT_DIR}/tmp' \
											--define='_topdir ${RPM_IMAGE_ROOT_DIR}' \
											--define='_rpmdir ${RPM_IMAGE_ROOT_DIR}/pkg' \
											--define='buildroot ${RPM_IMAGE_ROOT_DIR}/BUILDROOT' \
											--define='_sourcedir ${RPM_IMAGE_ROOT_DIR}/src' \
											--define='cli_version '${JFROG_CLI_VERSION} \
											--define='cli_release '${JFROG_CLI_RELEASE_VERSION} \
											--define='filename_prefix '${JFROG_CLI_PREFIX} \
											--define='build_arch '${JFROG_CLI_RPM_ARCH} \
											--define='full_version '${VERSION_FORMATTED} \
											${RPM_IMAGE_ROOT_DIR}/src/jfrog-cli.spec && \<commit_msg>Fix docker commands in pack.sh<commit_after>#!/bin/bash

# This file is responsible for building rpm and deb package for jfrog-cli installer

# This will contain hold the list of supported architectures which can be built by default.
# Although by passing a different --rpm-build-image or --rpm-build-image, artifacts of different architectures can be built
SUPPORTED_DEFAULT_ARCH_LIST="x86_64"

JFROG_CLI_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")/../" && pwd)"
JFROG_CLI_PKG="$JFROG_CLI_HOME/pkg"
JFROG_CLI_PREFIX="jfrog-cli"

RPM_BUILDER_NAME="jfrog-cli-rpm-builder"
RPM_IMAGE_ROOT_DIR="/opt/jfrog-cli"
RPM_DEPS="rpm-build"

DEB_BUILDER_NAME="jfrog-cli-deb-builder_3"
DEB_IMAGE_ROOT_DIR="/opt/jfrog-cli"
DEB_DEPS="devscripts build-essential lintian debhelper"

usage () {
    echo "Usage: ${0}"
    cat << END_USAGE

-b | --binary                                     : [mandatory] jfrog cli binary to be packaged in rpm/deb
-v | --version                                    : [mandatory] version of rpm/deb to be built
-t | --test                                       : [optional] test the generated rpm and deb
--rpm-build-image                                 : [optional] docker image to be used to build rpm
--deb-build-image                                 : [optional] docker image to be used to build deb
--rpm-test-image                                  : [optional] docker image to be used to test the generated rpm
--deb-test-image                                  : [optional] docker image to be used to test the generated deb
-f | --flavour                                    : [optional] flavours to be built (default : "rpm deb")
-h | --help                                       : [optional] display usage

END_USAGE
    sleep .5
}

log(){
	echo "$1"
}

errorExit() {
    echo -e "\033[31mERROR: $1 \033[0m"; echo
    exit 1
}

checkDockerAccess() {
	log "Checking docker" "DEBUG"
	docker -v > /dev/null 2>&1 && docker ps > /dev/null 2>&1
	if [ $? -ne 0 ]; then
	    errorExit "Must run as user that can execute docker commands"
	fi
	log "Docker is avaiable" "DEBUG"
}

exitWithUsage(){
	log "ERROR : $1"
	usage
	exit 1
}

createDEBPackage(){
	local flavour="deb"

	# cleanup old files and containers
	rm -f ${JFROG_CLI_PKG}/${JFROG_CLI_PREFIX}*${VERSION_FORMATTED}*.${flavour}
	docker rm -f "${RPM_BUILDER_NAME}" 2>/dev/null

	log "Building ${JFROG_CLI_PREFIX} ${flavour} ${JFROG_CLI_VERSION} on ${DEB_BUILD_IMAGE} image"

    docker run -t --rm -v "${JFROG_CLI_HOME}/${flavour}":${DEB_IMAGE_ROOT_DIR}/src \
					-v "${JFROG_CLI_PKG}":${DEB_IMAGE_ROOT_DIR}/pkg \
					--name ${DEB_BUILDER_NAME} \
							${DEB_BUILD_IMAGE} bash -c "\
										\
										echo '' && echo '' && \
										apt-get update && \
										apt-get install -y fakeroot && \
										DEBIAN_FRONTEND=noninteractive apt-get install -y ${DEB_DEPS} \
										--no-install-recommends tzdata && \
										echo '' && echo '' && \
									\
									\
										cp -fr ${DEB_IMAGE_ROOT_DIR}/src ${DEB_IMAGE_ROOT_DIR}-build && \
										cd ${DEB_IMAGE_ROOT_DIR}-build && \
										sed -i -e 's|__VERSION__|'${JFROG_CLI_VERSION}'|g' ${DEB_IMAGE_ROOT_DIR}-build/debian/changelog && \
										\
										\
										debuild -us -uc && \
										\
										\
										mkdir -p ${DEB_IMAGE_ROOT_DIR}/pkg && \
										cp -f ${DEB_IMAGE_ROOT_DIR}-build/../${JFROG_CLI_PREFIX}*.${flavour} \
											  ${DEB_IMAGE_ROOT_DIR}/pkg/${JFROG_CLI_PREFIX}-${VERSION_FORMATTED}.${flavour} && \
									\
									\
										echo '' && echo '' && \
										echo '############ Build successful ###################' && \
										ls -ltr ${DEB_IMAGE_ROOT_DIR}/pkg/ | grep ${flavour} && \
										echo '#################################################' || \
										exit 1 \
									" || errorExit "------------- Build Failed ! ------------------"
}

createRPMPackage(){
	local flavour="rpm"

	# cleanup old files and containers
	rm -f ${JFROG_CLI_PKG}/${JFROG_CLI_PREFIX}*${VERSION_FORMATTED}*.${flavour}
	docker rm -f "${RPM_BUILDER_NAME}" 2>/dev/null

	log "Building ${JFROG_CLI_PREFIX} ${flavour} ${JFROG_CLI_VERSION} on ${RPM_BUILD_IMAGE} image"

    docker run -t --rm -v "${JFROG_CLI_HOME}/${flavour}":${RPM_IMAGE_ROOT_DIR}/src \
					-v "${JFROG_CLI_PKG}":${RPM_IMAGE_ROOT_DIR}/pkg \
					--name ${RPM_BUILDER_NAME} \
							${RPM_BUILD_IMAGE} bash -c "\
										echo '' && echo '' && \
										yum install -y ${RPM_DEPS} && \
										echo '' && echo '' && \
									\
									\
										rpmbuild -bb \
											--define='_tmppath ${RPM_IMAGE_ROOT_DIR}/tmp' \
											--define='_topdir ${RPM_IMAGE_ROOT_DIR}' \
											--define='_rpmdir ${RPM_IMAGE_ROOT_DIR}/pkg' \
											--define='buildroot ${RPM_IMAGE_ROOT_DIR}/BUILDROOT' \
											--define='_sourcedir ${RPM_IMAGE_ROOT_DIR}/src' \
											--define='cli_version '${JFROG_CLI_VERSION} \
											--define='cli_release '${JFROG_CLI_RELEASE_VERSION} \
											--define='filename_prefix '${JFROG_CLI_PREFIX} \
											--define='build_arch '${JFROG_CLI_RPM_ARCH} \
											--define='full_version '${VERSION_FORMATTED} \
											${RPM_IMAGE_ROOT_DIR}/src/jfrog-cli.spec && \
{'fraction_non_alphanumeric': 0.16235840836479815, 'fraction_numerical': 0.005808887598024978, 'mean_word_length': 2.1551618814905313, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3500091', 'n_tokens_mistral': 4596, 'n_tokens_neox': 3519, 'n_words': 740}