portainer-devtool/build_manager.sh

332 lines
7.7 KiB
Bash
Raw Permalink Normal View History

2022-03-16 14:31:06 +11:00
#!/bin/bash
set -eu
source ./utils/common.sh
2022-03-16 14:31:06 +11:00
WORKDIR=/home/oscarzhou/source/github.com/portainer
2022-03-17 19:28:40 +11:00
GLOBAL_VOLUME=/home/oscarzhou/volumes
2022-03-16 14:31:06 +11:00
TRUE=0;
FALSE=1;
# PORTAINER_FLAGS=
# PORTAINER_FLAGS=--enable-init true
2022-03-17 19:28:40 +11:00
function choose_repo() {
2022-05-19 10:36:38 +10:00
read -p "Choose the working project EE/(CE)/(k8s)/(agent):" REPO
2022-03-16 14:31:06 +11:00
if [ -z "$REPO" ]; then
REPO="EE";
fi
if [[ "${REPO}" == "ee" || "${REPO}" == "EE" ]]; then
2022-03-17 19:28:40 +11:00
cd ${WORKDIR}/portainer-ee
2022-03-16 14:31:06 +11:00
elif [[ "${REPO}" == "ce" || "${REPO}" == "CE" ]]; then
2022-03-17 19:28:40 +11:00
cd ${WORKDIR}/portainer
elif [[ "${REPO}" == "k8s" || "${REPO}" == "ks" ]]; then
cd ${WORKDIR}/k8s
2022-05-19 10:36:38 +10:00
elif [[ "${REPO}" == "ag" || "${REPO}" == "agent" ]]; then
cd ${WORKDIR}/agent
2022-03-16 14:31:06 +11:00
fi
}
function is_root() {
if [ "$EUID" -ne 0 ]; then
printf "${HIGHLIGHT_COLOR}Please run as root${NO_COLOR}\n"
exit;
fi
}
function check_branch() {
printf "Your current checkout branch \n$(git branch)\n"
read -p "Continue N/(Y)?" CORRECT_BRANCH
if [ -z "$CORRECT_BRANCH" ]; then
CORRECT_BRANCH="N";
fi
if [[ "${CORRECT_BRANCH}" == "N" || "${CORRECT_BRANCH}" == "n" ]]; then
printf "${ERROR_COLOR}Exit.\n";
return $FALSE;
fi
return $TRUE;
}
2022-03-17 19:28:40 +11:00
function choose_export_volume() {
read -p "Choose the volume EE/CE/Temp-data(TD):" DEST
if [ -z "$DEST" ]; then
DEST="EE";
fi
if [[ "${DEST}" == "ee" || "${DEST}" == "EE" ]]; then
VOLUME=~/volumes/portainer-ee-data
elif [[ "${DEST}" == "ce" || "${DEST}" == "CE" ]]; then
2022-03-16 14:31:06 +11:00
VOLUME=~/volumes/portainer-ce-data
2022-03-17 19:28:40 +11:00
elif [[ "${DEST}" == "td" || "${DEST}" == "TD" ]]; then
VOLUME=~/volumes/temp-data
2022-03-16 14:31:06 +11:00
fi
export PORTAINER_DATA=${VOLUME}
printf "${HIGHLIGHT_COLOR}export PORTAINER_DATA=${PORTAINER_DATA}${NO_COLOR}\n"
}
function cleanup_temporary_volume() {
printf "${HIGHLIGHT_COLOR}Clean temporary data${NO_COLOR}\n"
2022-03-17 19:28:40 +11:00
local VOLUME=~/volumes/temp-data
if [ -d ${VOLUME} ]; then
printf "The current volume is ${VOLUME}. "
2022-03-17 19:28:40 +11:00
read -p "Do you want to clean up the existing data N/(Y)?" CLEAN_DATA
if [[ "${CLEAN_DATA}" == "y" || "${CLEAN_DATA}" == "Y" ]]; then
rm -rvf ${VOLUME}/*
fi
else
mkdir ${VOLUME}
fi
}
2022-03-16 14:31:06 +11:00
function build_portainer_frontend_without_prompt() {
printf "${HIGHLIGHT_COLOR}Build Portainer Frontend${NO_COLOR}\n"
yarn
2022-03-16 14:31:06 +11:00
yarn start:client
}
function build_portainer_backend_without_prompt() {
printf "${HIGHLIGHT_COLOR}Build Portainer Backend${NO_COLOR}\n"
if [ -z dist/portainer ]; then
rm dist/portainer;
fi
yarn start:server
}
function build_portainer_frontend() {
2022-03-17 19:28:40 +11:00
choose_repo
2022-03-16 14:31:06 +11:00
if ! check_branch; then
exit;
fi
build_portainer_frontend_without_prompt
}
function build_portainer_backend() {
2022-03-17 19:28:40 +11:00
choose_repo
choose_export_volume
2022-03-16 14:31:06 +11:00
if ! check_branch; then
exit;
fi
build_portainer_backend_without_prompt
}
2022-05-19 10:36:38 +10:00
function build_portainer_agent() {
choose_repo
if ! check_branch; then
exit;
fi
./dev.sh compile
}
2022-03-16 14:31:06 +11:00
function build_portainer_all() {
printf "${HIGHLIGHT_COLOR}Build Portainer all${NO_COLOR}\n"
2022-03-17 19:28:40 +11:00
choose_repo
choose_export_volume
2022-03-16 14:31:06 +11:00
if ! check_branch; then
exit;
fi
build_portainer_backend_without_prompt
2022-03-17 06:59:53 +11:00
build_portainer_frontend_without_prompt
2022-03-16 14:31:06 +11:00
}
function run_before_commit() {
printf "${HIGHLIGHT_COLOR}Run before commit${NO_COLOR}\n"
2022-03-17 19:28:40 +11:00
choose_repo
2022-03-16 14:31:06 +11:00
if ! check_branch; then
exit;
fi
input "1. Only frontend 2. Only backend 3. Both" option
if [ option == "1" ]; then
print_highlight "yarn test";
yarn test:client
print_highlight "yarn format";
yarn format:client
print_highlight "yarn lint"
yarn lint:client
elif [ option == "2" ]; then
print_highlight "yarn test";
yarn test:server
print_highlight "yarn format";
yarn format:server
print_highlight "yarn lint"
yarn lint:server
2022-03-16 14:31:06 +11:00
elif [ option == "3" ]; then
print_highlight "yarn test";
yarn test
print_highlight "yarn format";
yarn format
print_highlight "yarn lint"
yarn lint
else
print_highlight "yarn test";
yarn test:client
print_highlight "yarn format";
yarn format:client
print_highlight "yarn lint"
yarn lint:client
fi
2022-03-16 14:31:06 +11:00
}
function run_before_commit_k8s() {
printf "${HIGHLIGHT_COLOR}Run before commit${NO_COLOR}\n"
choose_repo
if ! check_branch; then
exit;
fi
printf "${HIGHLIGHT_COLOR}chart-testing ct lint${NO_COLOR}\n"
docker run --rm -it -w /repo -v `pwd`:/repo quay.io/helmpack/chart-testing ct lint --all --config=.ci/ct-config.yaml
}
function generate_portainer_jwt() {
printf "${HIGHLIGHT_COLOR}Generate Portainter JWT${NO_COLOR}\n"
read -p "Username(admin):" username
if [ -z "$username" ]; then
username="admin";
fi
read -p "Password(****):" password
read -p "Address(127.0.0.1):" address
if [ -z "$address" ]; then
address="127.0.0.1";
fi
payload="{\"username\":\"${username}\",\"password\":\"${password}\"}"
curl -d ${payload} -H 'Content-Type: application/json' "http://${address}:9000/api/auth"
}
function get_portainer_ce_api_reference() {
printf "${HIGHLIGHT_COLOR}Get the reference of Portainer CE API${NO_COLOR}\n"
cd ${WORKDIR}/portainer
if ! check_branch; then
exit;
fi
read -p "Commit(HEAD):" commit
if [ -z "$commit" ]; then
commit=$(git rev-parse HEAD)
fi
printf "${HIGHLIGHT_COLOR}Installing github.com/portainer/portainer/api@${commit}${NO_COLOR}\n"
output=$(go install github.com/portainer/portainer/api@${commit}) | while IFS= read -r line; do
echo "$line"
done
# result="
# go: downloading github.com/portainer/portainer/api v0.0.0-20220622202437-f0ca3e63db9d
# go: downloading github.com/portainer/portainer v0.6.1-0.20220622202437-f0ca3e63db9d
# package github.com/portainer/portainer/api is not a main package
# "
# while IFS= read -r line; do
# echo "$line"
# done <<< $(echo ${result})
}
2022-03-16 14:31:06 +11:00
function menu() {
PS3='Please select the option: '
OPTIONS=(
2022-05-19 10:36:38 +10:00
'Build Portainer EE/CE All'
'Build Portainer EE/CE Frontend'
'Build Portainer EE/CE Backend'
'Generate Portainer EE/CE JWT'
2022-05-19 10:36:38 +10:00
'Run Before Commit [Portainer EE/CE]'
'Get Portainer CE API Reference'
2022-05-19 10:36:38 +10:00
'Run Before Commit [k8s]'
'Build Portainer Agent'
'Cleanup Temporary Volume'
2022-03-16 14:31:06 +11:00
'Quit'
)
select opt in "${OPTIONS[@]}"
do
case $opt in
2022-05-19 10:36:38 +10:00
'Build Portainer EE/CE All')
2022-03-16 14:31:06 +11:00
build_portainer_all
;;
2022-05-19 10:36:38 +10:00
'Build Portainer EE/CE Frontend')
2022-03-16 14:31:06 +11:00
build_portainer_frontend
;;
2022-05-19 10:36:38 +10:00
'Build Portainer EE/CE Backend')
2022-03-16 14:31:06 +11:00
build_portainer_backend
;;
'Generate Portainer EE/CE JWT')
generate_portainer_jwt
;;
2022-05-19 10:36:38 +10:00
'Run Before Commit [Portainer EE/CE]')
2022-03-16 14:31:06 +11:00
run_before_commit
;;
'Get Portainer CE API Reference')
get_portainer_ce_api_reference
;;
2022-05-19 10:36:38 +10:00
'Run Before Commit [k8s]')
run_before_commit_k8s
;;
'Build Portainer Agent')
build_portainer_agent
;;
'Cleanup Temporary Volume')
cleanup_temporary_volume
;;
2022-03-16 14:31:06 +11:00
'Quit')
break
;;
esac
done
}
# check if the function exists (bash specific)
if [ "$#" -eq 0 ]; then
menu
else
"$@"
fi