From 6ffc2ac6e66ac2d47c09379ca8523e0cfd7c4ac4 Mon Sep 17 00:00:00 2001 From: mikessh Date: Fri, 8 Apr 2016 23:16:29 +0300 Subject: [PATCH] Add shell script that facilitates running migec CLI --- migec | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 migec diff --git a/migec b/migec new file mode 100644 index 0000000..f2f98e0 --- /dev/null +++ b/migec @@ -0,0 +1,144 @@ +#!/bin/bash + +java="java" + +sedString="s/.*1\.\(.*\)\..*/\1/" +jVersion=$($java -version 2>&1 | grep version | awk '{ print $3 }' | sed $sedString) +if [[ $jVersion -lt 7 ]]; +then + echo "Wrong java version. Please use Java 7 or higher." + exit 1 +fi + +# Linux readlink -f alternative for Mac OS X +function readlinkUniversal() { + targetFile=$1 + + cd `dirname $targetFile` + targetFile=`basename $targetFile` + + # iterate down a (possible) chain of symlinks + while [ -L "$targetFile" ] + do + targetFile=`readlink $targetFile` + cd `dirname $targetFile` + targetFile=`basename $targetFile` + done + + # compute the canonicalized name by finding the physical path + # for the directory we're in and appending the target file. + phys_dir=`pwd -P` + result=$phys_dir/$targetFile + echo $result +} + +os=`uname` +delta=100 + +dir="" + +case $os in + Darwin) + freeBlocks=$(vm_stat | grep free | awk '{ print $3 }' | sed 's/\.//') + inactiveBlocks=$(vm_stat | grep inactive | awk '{ print $3 }' | sed 's/\.//') + speculativeBlocks=$(vm_stat | grep speculative | awk '{ print $3 }' | sed 's/\.//') + freeMb=$((($freeBlocks+$speculativeBlocks)*4096/1048576)) + inactiveMb=$(($inactiveBlocks*4096/1048576)) + maxMb=$((($freeMb+$inactiveMb-$delta))) + dir=$(dirname "$(readlinkUniversal "$0")") + ;; + Linux) + rFreeMb=$(free -m | head -n 3 | tail -n 1 | awk '{ print $4 }') + maxMb=$(($rFreeMb-$delta)) + dir="$(dirname "$(readlink -f "$0")")" + ;; + *) + echo "Unknown OS." + exit 1 + ;; +esac + +migec=${dir}/migec + +migecArgs=() +javaArgs=() + +needXmxXms=true +otherJar="" + +while [[ $# > 0 ]] +do + key="$1" + shift + case $key in + -D*|-X*|-ea|-agentlib*) + javaArgs+=(${key}) + + case $key in + -Xmx*|-Xms*) + needXmxXms=false + ;; + esac + + ;; + -jar|-V) + otherJar="$1" + shift + ;; + *) + migecArgs+=(${key}) + ;; + esac +done + +if [[ ${needXmxXms} == true ]] +then + targetXmx=12000 + + if [[ $targetXmx -gt $maxMb ]]; + then + targetXmx=$maxMb + fi + + javaArgs+=("-Xmx${targetXmx}m") + + targetXms=$((${targetXmx}*2/3)) + + if [[ $targetXms -lt 2000 ]]; + then + targetXms=$targetXmx + fi + + javaArgs+=("-Xms${targetXms}m") +fi + +jar="" + +if [[ -z "$otherJar" ]]; +then + for j in "$dir/../jar/migec.jar" "$dir/migec.jar" $(ls -d -1 $dir/target/* 2> /dev/null | grep distribution.jar) + do + if [[ -e "$j" ]]; + then + jar=$j + break + fi + done +else + for j in $(ls -1 ${dir}/* | grep "migec" | grep ".jar" | grep ${otherJar}); + do + if [[ -e "$j" ]]; + then + jar=$j + break + fi + done +fi + +if [[ "$jar" == "" ]]; +then + echo "No jar." + exit 1 +fi + +$java -XX:+AggressiveOpts "${javaArgs[@]}" -jar $jar "${migecArgs[@]}" \ No newline at end of file