build.sh 7.28 KB
Newer Older
Misagh Moayyed's avatar
Misagh Moayyed committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
#!/bin/bash

function copy() {
	echo -e "Creating configuration directory under /etc/cas"
	mkdir -p /etc/cas/config

	echo -e "Copying configuration files from etc/cas to /etc/cas"
	cp -rfv ./etc/cas/* /etc/cas
}

function help() {
	clear
	echo "******************************************************************"
	echo "Apereo CAS"
	echo "Enterprise Single SignOn for all earthlings and beyond"
	echo "- https://github.com/apereo/cas"
	echo "- https://apereo.github.io/cas"
	echo "******************************************************************"

	echo -e "Usage: build.sh [maven|gradle] [copy|clean|package|run|update|debug|tomcat|gencert]\n"
	echo "	clean: Clean Maven build directory"
	echo "	cli: Run the CAS command line shell and pass commands"
	echo "	copy: Copy config from the project's local etc/cas/config directory to the root /etc/cas/config"
	echo "	debug: Run cas.war and listen for Java debugger on port 5000"
	echo "	dependencies: Get a report of all dependencies configured in the build"
	echo "	gencert: Create keystore with SSL certificate in location where CAS looks by default"
	echo "	getview: Ask for a view name to be included in the overlay for customizations"
	echo "	listviews: List all CAS views that ship with the web application and can be customized in the overlay"
	echo "	package: Clean and build CAS war"
	echo "	run: Build and run cas.war via Java as an executable war"
	echo "	runalone: Build and run cas.war on its own as a standalone executable"
	echo "	tomcat: Deploy the CAS web application to an external Apache Tomcat server"
	echo "	update: Package the CAS overlay by force-updating dependencies and SNAPSHOT versions"
}

function clean() {
	if [ "$buildTool" = "maven" ]; then
		./mvnw clean "$@"
	else
		./gradlew clean "$@"
	fi
}

function package() {
	clean
	if [ "$buildTool" = "maven" ]; then
		./mvnw package "$@"
	else
		./gradlew build "$@"
	fi
}

function update() {
	clean
	if [ "$buildTool" = "maven" ]; then
		./mvnw package -U "$@"
	else
		./gradlew build --refresh-dependencies "$@"
	fi
}

function dependencies() {
	if [ "$buildTool" = "maven" ]; then
		./mvnw dependency:analyze "$@"
	else
		./gradlew allDependencies
	fi
}

function tomcat() {
	export CATALINA_HOME=./apache-tomcat/
    echo "Attempting to shutdown Apache Tomcat..."
    ./apache-tomcat/bin/shutdown.sh 2>/dev/null
	ps -ef | grep tomcat

    rm -Rf ./apache-tomcat
	./mvnw clean package -P external -T 5 "$@" && cp target/cas.war apache-tomcat/webapps/
	chmod +x ./apache-tomcat/bin/*.sh
	./apache-tomcat/bin/startup.sh
	tail -F ./apache-tomcat/logs/catalina.out
}

function debug() {
	if [ "$buildTool" = "maven" ]; then
		casWar="target/cas.war"
	else
		casWar="build/libs/cas.war"
	fi
	package && java -Xdebug -Xrunjdwp:transport=dt_socket,address=5000,server=y,suspend=n -jar $casWar
}

function run() {
	if [ "$buildTool" = "maven" ]; then
		casWar="target/cas.war"
	else
		casWar="build/libs/cas.war"
	fi
	package && java -XX:TieredStopAtLevel=1 -Xverify:none -jar $casWar
}

function runalone() {
	if [ "$buildTool" = "maven" ]; then
		./mvnw clean package -P default,exec  "$@"
		casWar="target/cas.war"
	else
		./gradlew clean build -Pexecutable=true "$@"
		casWar="build/libs/cas.war"
	fi
	chmod +x $casWar
   	$casWar
}

function listviews() {
	explodeapp
	if [ "$buildTool" = "maven" ]; then
		explodedDir=target/cas
	else
		explodedDir=build/cas
	fi
	find $explodedDir -type f -name "*.html" | xargs -n 1 basename | sort | more
}

function explodeapp() {
	if [ "$buildTool" = "maven" ]; then
		if [ ! -d $PWD/target/cas ];then
			echo "Building the CAS web application and exploding the final war file..."
			./mvnw clean package war:exploded "$@"
		fi
	else
		./gradlew explodeWar
	fi
	echo "Exploded the CAS web application file."
}

function getview() {
	explodeapp
	echo "Searching for view name $@..."
	if [ "$buildTool" = "maven" ]; then
		explodedDir=target/cas
	else
		explodedDir=build/cas
	fi

	results=`find $explodedDir -type f -name "*.html" | grep -i "$@"`

	count=`wc -w <<< "$results"`

	if [ "$count" -eq 0 ];then
		echo "No views could be found matching $@"
		exit 1
	fi
	echo -e "Found view(s): \n$results"
	if [ "$count" -eq 1 ];then
		if [ "$buildTool" = "maven" ]; then
			fromFile="target/cas/WEB-INF/classes"
		else
			fromFile="build/cas/WEB-INF/classes"
		fi
		toFile="src/main/resources"

		overlayfile=`echo "${results/$fromFile/$toFile}"`
		overlaypath=`dirname "${overlayfile}"`
		# echo "Overlay file is $overlayfile to be created at $overlaypath"
		mkdir -p $overlaypath
		cp $results $overlaypath
		echo "Created view at $overlayfile"
		ls $overlayfile
	else
		echo "More than one view file is found. Narrow down the search query..."
	fi
}

function gencert() {
	if [[ ! -d /etc/cas ]] ; then
		copy
	fi
	which keytool
	if [[ $? -ne 0 ]] ; then
		echo Error: Java JDK \'keytool\' is not installed or is not in the path
		exit 1
	fi
	# override DNAME and CERT_SUBJ_ALT_NAMES before calling or use dummy values
	DNAME="${DNAME:-CN=cas.example.org,OU=Example,OU=Org,C=US}"
	CERT_SUBJ_ALT_NAMES="${CERT_SUBJ_ALT_NAMES:-dns:example.org,dns:localhost,ip:127.0.0.1}"
	echo "Generating keystore for CAS with DN ${DNAME}"
	keytool -genkeypair -alias cas -keyalg RSA -keypass changeit -storepass changeit -keystore /etc/cas/thekeystore -dname ${DNAME} -ext SAN=${CERT_SUBJ_ALT_NAMES}
	keytool -exportcert -alias cas -storepass changeit -keystore /etc/cas/thekeystore -file /etc/cas/cas.cer
}

function cli() {
	rm -f *.log

	if [ "$buildTool" = "maven" ]; then
		CAS_VERSION=$(./mvnw -q -Dexec.executable="echo" -Dexec.args='${cas.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec 2>/dev/null)
	else
		CAS_VERSION=$(./gradlew casVersion --quiet)
	fi

	echo "CAS version: $CAS_VERSION"
	JAR_FILE_NAME="cas-server-support-shell-${CAS_VERSION}.jar"
	echo "JAR name: $JAR_FILE_NAME"
	JAR_PATH="org/apereo/cas/cas-server-support-shell/${CAS_VERSION}/${JAR_FILE_NAME}"
	echo "JAR path: $JAR_PATH"

	JAR_FILE_LOCAL="$HOME/.m2/repository/$JAR_PATH";
	echo "Local JAR file path: $JAR_FILE_LOCAL";
	if [ -f "$JAR_FILE_LOCAL" ]; then
		echo "Using JAR file locally at $JAR_FILE_LOCAL"
		java -jar $JAR_FILE_LOCAL "$@"
		exit 0;
	fi

	if [ "$buildTool" = "maven" ]; then
		DOWNLOAD_DIR=./target
	else
		DOWNLOAD_DIR=./build/libs
	fi

	COMMAND_FILE="${DOWNLOAD_DIR}/${JAR_FILE_NAME}"
	if [ ! -f "$COMMAND_FILE" ]; then
		mkdir -p $DOWNLOAD_DIR
		wget "https://repo1.maven.org/maven2/${JAR_PATH}" -P ./target
	fi

	echo "Running $COMMAND_FILE"
	java -jar $COMMAND_FILE "$@"
	exit 0;

}

buildTool=$1
command=$2

if [ -z "$command" ]; then
    echo "No commands provided. Defaulting to [run]"
	command="run"
fi

if [ -z "$buildTool" ]; then
	buildTool="maven"
	echo "Build tool type is unspecified. Defaulting to [$buildTool]"
else
	echo "Using build tool [$buildTool] to [$command] the CAS overlay"
fi

shift 2

case "$command" in
"copy")
    copy
    ;;
"help")
    help
    ;;
*)
	if [ "$buildTool" = "maven" ]; then
		pushd maven-overlay
	else
		pushd gradle-overlay
	fi

	case "$command" in
	"clean")
		clean "$@"
		;;
	"package"|"build")
		package "$@"
		;;
	"debug")
		debug "$@"
		;;
	"run")
		run "$@"
		;;
	"gencert")
		gencert "$@"
		;;
	"cli")
		cli "$@"
		;;
	"update")
		update "$@"
		;;
	"dependencies")
		update "$@"
		;;
	"runalone")
		runalone "$@"
		;;
	"listviews")
		listviews "$@"
		;;
	"getview")
		getview "$@"
		;;
	"tomcat")
		tomcat
		;;
	esac
	popd
esac