From a3d8a64b5a9d80c2da754db4041a70860c045028 Mon Sep 17 00:00:00 2001 From: Marco Vermeulen Date: Wed, 26 Sep 2012 21:28:37 +0100 Subject: [PATCH] Add srv folder with templates, scripts and server. --- srv/scripts/gvm | 223 +++++++++++++++++++++++++++++++++++ srv/scripts/gvm-init.sh | 12 ++ srv/scripts/install.sh | 139 ++++++++++++++++++++++ srv/server.groovy | 113 ++++++++++++++++++ srv/templates/broadcast.gtpl | 10 ++ srv/templates/list.gtpl | 21 ++++ srv/templates/upgrade.gtpl | 5 + 7 files changed, 523 insertions(+) create mode 100755 srv/scripts/gvm create mode 100755 srv/scripts/gvm-init.sh create mode 100755 srv/scripts/install.sh create mode 100644 srv/server.groovy create mode 100644 srv/templates/broadcast.gtpl create mode 100644 srv/templates/list.gtpl create mode 100644 srv/templates/upgrade.gtpl diff --git a/srv/scripts/gvm b/srv/scripts/gvm new file mode 100755 index 00000000..6cb1b56a --- /dev/null +++ b/srv/scripts/gvm @@ -0,0 +1,223 @@ +#!/bin/bash +GVM_VERSION="0.1" +echo "Groovy Version Manager $GVM_VERSION" + +# +# function definitions +# + +function help { + echo "" + echo "Usage: gvm [version]" + echo "" + echo " command : install, uninstall, list, use or help" + echo " candidate : $CANDIDATES" + echo " version : optional, defaults to latest stable if not provided" + echo "" + echo "eg: gvm install grails 2.1.0" +} + +function determine_candidate_version { + if [ -z "$1" ]; then + VERSION=$(curl -s "$GVM_SERVICE/candidates/$CANDIDATE/default") + else + VERSION=$(curl -s "$GVM_SERVICE/candidates/$CANDIDATE/$1") + fi + + if [ "$VERSION" == "invalid" ]; then + echo "" + echo "Stop! $1 is not a valid grails version." + exit 0 + fi +} + +function check_installed { + if [ -d "$GVM_DIR/$CANDIDATE/$VERSION" ]; then + echo "" + echo "Stop! $CANDIDATE $VERSION is already installed." + exit 0 + fi +} + +function check_not_installed { + if [ ! -d "$GVM_DIR/$CANDIDATE/$VERSION" ]; then + echo "" + echo "Stop! $CANDIDATE $VERSION is not installed." + exit 0 + fi +} + +function check_candidate_present { + if [ ! -d "$GVM_DIR/$CANDIDATE" ]; then + echo "" + echo "Stop! $CANDIDATE has never been installed." + exit 0 + fi +} + +function build_version_csv { + CANDIDATE="$1" + check_candidate_present "$CANDIDATE" + + CSV="" + for version in $(ls -1 $GVM_DIR/$CANDIDATE); do + if [ $version != 'current' ]; then + CSV="$version,$CSV" + fi + done + CSV=${CSV%?} +} + +function determine_current_version { + CANDIDATE="$1" + CURRENT=$(readlink "$GVM_DIR/$CANDIDATE/current" | sed -e "s_$GVM_DIR/$CANDIDATE/__g") +} + +function download { + CANDIDATE="$1" + VERSION="$2" + mkdir -p "$GVM_DIR/archives" + if [ ! -f "$GVM_DIR/archives/$CANDIDATE-$VERSION.zip" ]; then + DOWNLOAD_URL=$(curl -s "$GVM_SERVICE/download/$CANDIDATE/$VERSION") + echo "" + echo "Downloading: $CANDIDATE $VERSION from: $DOWNLOAD_URL" + echo "" + curl -L "$DOWNLOAD_URL" >> "$GVM_DIR/archives/$CANDIDATE-$VERSION.zip" + else + echo "" + echo "Found a previously downloaded $CANDIDATE $VERSION archive." + echo "Not downloading it again..." + fi + echo "" +} + +function server_down { + echo "------------------------------------------" + echo " This is serious! Service is down! " + echo " Please notify: @freshgroovy immediately! " + echo "------------------------------------------" + exit 0 +} + +# +# Various sanity checks and default settings +# + +if [ ! $(which curl) ]; then + echo "Please install curl through your package manager before using this tool." + exit 0 +fi + +if [ ! "$GVM_SERVICE" ]; then + GVM_SERVICE="http://localhost:8080" +fi + +if [ ! "$GVM_DIR" ]; then + GVM_DIR="$HOME/.gvm" +fi + +if [ ! -d "$GVM_DIR" ]; then + mkdir "$GVM_DIR" +fi + +BROADCAST=$(curl -s "$GVM_SERVICE/app/alive/$GVM_VERSION") +if [ "$BROADCAST" ]; then + echo "$BROADCAST" +else + server_down +fi + +CANDIDATES=$(curl -s "$GVM_SERVICE/candidates") +if [ "$1" == "help" -o -z "$1" -o -z "$2" ]; then + help + exit 0 +fi + +if [ "$1" != "install" -a "$1" != "use" -a "$1" != "uninstall" -a "$1" != "list" -a "$1" != "help" -a "$1" != "selfupdate" ]; then + echo "Invalid command: $1" + help + exit 0 +fi + +CANDIDATE_VALID=$(curl -s "$GVM_SERVICE/candidates/$2") +if [ "$CANDIDATE_VALID" == 'invalid' ]; then + echo "Invalid candidate: $2" + help + exit 0 +fi + +# +# Command functions +# + +function gvm-install { + CANDIDATE="$1" + determine_candidate_version "$2" + check_installed + + download "$CANDIDATE" "$VERSION" + echo "Installing: $CANDIDATE $VERSION" + + mkdir -p "$GVM_DIR/$CANDIDATE" + + unzip -q "$GVM_DIR/archives/$CANDIDATE-$VERSION.zip" -d "/tmp/" + mv "/tmp/$CANDIDATE-$VERSION" "$GVM_DIR/$CANDIDATE/$VERSION" + echo "Done installing!" + echo "" + echo "Start using $CANDIDATE $VERSION by issuing the following command:" + echo " gvm use $CANDIDATE $VERSION" +} + +function gvm-use { + CANDIDATE="$1" + determine_candidate_version "$2" + check_not_installed + + if [ -L "$GVM_DIR/$CANDIDATE/current" ]; then + unlink "$GVM_DIR/$CANDIDATE/current" + fi + ln -s "$GVM_DIR/$CANDIDATE/$VERSION" "$GVM_DIR/$CANDIDATE/current" + echo "" + echo Using "$CANDIDATE" version "$VERSION" +} + +function gvm-list { + CANDIDATE="$1" + build_version_csv "$CANDIDATE" + determine_current_version "$CANDIDATE" + FRAGMENT=$(curl -s "$GVM_SERVICE/candidates/$CANDIDATE/list?current=$CURRENT&installed=$CSV") + echo "$FRAGMENT" +} + +function gvm-uninstall { + CANDIDATE="$1" + VERSION="$2" + CURRENT=$(readlink "$GVM_DIR/$CANDIDATE/current" | sed -e "s_$GVM_DIR/$CANDIDATE/__g") + if [ -h "$GVM_DIR/$CANDIDATE/current" -a "$VERSION" == "$CURRENT" ]; then + echo "" + echo "Unselecting $CANDIDATE $VERSION..." + unlink "$GVM_DIR/$CANDIDATE/current" + fi + echo "" + if [ -d "$GVM_DIR/$CANDIDATE/$VERSION" ]; then + echo "Uninstalling $CANDIDATE $VERSION..." + rm -rf "$GVM_DIR/$CANDIDATE/$VERSION" + else + echo "$CANDIDATE $VERSION is not installed." + fi +} + +function gvm-selfupdate { + curl -s "$GVM_SERVICE/res/init" > "$HOME/.gvm/bin/gvm-init.sh" + curl -s "$GVM_SERVICE/res/gvm" > "$HOME/.gvm/bin/gvm" + + chmod +x "$HOME/.gvm/bin/gvm-init.sh" + chmod +x "$HOME/.gvm/bin/gvm" + + echo "Successfully upgraded GVM." + echo "" +} + +# Main command + +gvm-"$1" "$2" "$3" diff --git a/srv/scripts/gvm-init.sh b/srv/scripts/gvm-init.sh new file mode 100755 index 00000000..230b5db0 --- /dev/null +++ b/srv/scripts/gvm-init.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +export GVM_SERVICE="http://get.freshgroovy.org" + +PATH="$HOME/.gvm/bin:$PATH" + +GROOVY_HOME="$HOME/.gvm/groovy/current" +GRAILS_HOME="$HOME/.gvm/grails/current" +GRIFFON_HOME="$HOME/.gvm/griffon/current" +GRADLE_HOME="$HOME/.gvm/gradle/current" + +export PATH="$GROOVY_HOME/bin:$GRAILS_HOME/bin:$GRIFFON_HOME/bin:$GRADLE_HOME/bin:$PATH" diff --git a/srv/scripts/install.sh b/srv/scripts/install.sh new file mode 100755 index 00000000..c6e3ed44 --- /dev/null +++ b/srv/scripts/install.sh @@ -0,0 +1,139 @@ +#!/bin/bash + +GVM_SERVICE="http://get.freshgroovy.org" + +echo ' ' +echo 'Thanks for using ' +echo ' ' +echo '_____/\\\\\\\\\\\\__/\\\________/\\\__/\\\\____________/\\\\_ ' +echo ' ___/\\\//////////__\/\\\_______\/\\\_\/\\\\\\________/\\\\\\_ ' +echo ' __/\\\_____________\//\\\______/\\\__\/\\\//\\\____/\\\//\\\_ ' +echo ' _\/\\\____/\\\\\\\__\//\\\____/\\\___\/\\\\///\\\/\\\/_\/\\\_ ' +echo ' _\/\\\___\/////\\\___\//\\\__/\\\____\/\\\__\///\\\/___\/\\\_ ' +echo ' _\/\\\_______\/\\\____\//\\\/\\\_____\/\\\____\///_____\/\\\_ ' +echo ' _\/\\\_______\/\\\_____\//\\\\\______\/\\\_____________\/\\\_ ' +echo ' _\//\\\\\\\\\\\\/_______\//\\\_______\/\\\_____________\/\\\_ ' +echo ' __\////////////__________\///________\///______________\///__' +echo ' ' +echo ' Will now attempt installing...' +echo ' ' + +echo "Looking for JAVA_HOME..." +if [ -z "$JAVA_HOME" ]; then + echo "Not found." + echo "" + echo "======================================================================================================" + echo " Please ensure that you have a Java SDK installed and that JAVA_HOME environment variable is set." + echo " accordingly." + echo "" + echo " Java can be found here:" + echo " http://www.oracle.com/technetwork/java/javase/downloads/index.html" + echo "" + echo " Set JAVA_HOME by editing your ~/.profile file and adding:" + echo " export JAVA_HOME=\"/path/to/my/jdk\"" + echo "======================================================================================================" + echo "" + exit 0 +fi + +echo "Validating JAVA_HOME..." +if [ ! -f "$JAVA_HOME/bin/java" ]; then + echo "Invalid." + echo "" + echo "======================================================================================================" + echo " Please ensure that your JAVA_HOME points to a valid Java SDK." + echo " You are currently pointing to:" + echo "" + echo " $JAVA_HOME" + echo "" + echo " This does not seem to be valid. Please rectify and restart." + echo "======================================================================================================" + echo "" + exit 0 +fi + +echo "Checking for previous versions of Grails..." +if [ ! -z "$GRAILS_HOME" ]; then + echo "GRAILS_HOME found." + echo "" + echo "======================================================================================================" + echo " You already have a GRAILS_HOME defined. This will cause problems with GVM." + echo " GRAILS_HOME is currently pointing to:" + echo "" + echo " $GRAILS_HOME" + echo "" + echo " Please remove it from your PATH and restart." + echo "======================================================================================================" + echo "" + exit 0 +fi + +if [ ! -z $(which grails) ]; then + echo "Grails found." + echo "" + echo "======================================================================================================" + echo " You already have Grails installed. This will cause problems with GVM." + echo " Grails was found at:" + echo "" + echo " $(which grails)" + echo "" + echo " Please remove it from your PATH and restart." + echo "======================================================================================================" + echo "" + exit 0 +fi + +echo "Looking for curl..." +if [ -z $(which curl) ]; then + echo "Not found." + echo "" + echo "======================================================================================================" + echo " Please install curl on your system using your favourite package manager." + echo "" + echo " What is 'curl' you might ask?" + echo "" + echo " From the curl manpage:" + echo "" + echo " curl is a tool to transfer data from or to a server, using one of the supported protocols. The" + echo " command is designed to work without user interaction. curl offers a busload of useful tricks like" + echo " proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer" + echo " resume, Metalink, and more. the number of features will make your head spin!" + echo "" + echo " GVM uses curl for crucial interactions with it's backend server." + echo "" + echo " Please install curl and restart." + echo "======================================================================================================" + echo "" + exit 0 +fi + +echo "Installing gvm scripts..." +mkdir -p "$HOME/.gvm/bin" +curl -s "$GVM_SERVICE/res/init" > "$HOME/.gvm/bin/gvm-init.sh" +curl -s "$GVM_SERVICE/res/gvm" > "$HOME/.gvm/bin/gvm" +chmod +x "$HOME/.gvm/bin/gvm-init.sh" +chmod +x "$HOME/.gvm/bin/gvm" + +SNIPPET='[[ -s "$HOME/.gvm/bin/gvm-init.sh" ]] && . "$HOME/.gvm/bin/gvm-init.sh"' +echo "Attempting to update bash profile..." +if [ ! -f "$HOME/.profile" -a ! -f "$HOME/.bashrc" ]; then + echo "#!/bin/bash" > "$HOME/.profile" + echo "$SNIPPET" >> "$HOME/.profile" + echo "Created and updated .profile" +else + if [ -f "$HOME/.profile" ]; then + echo "" >> "$HOME/.profile" + echo "$SNIPPET" >> "$HOME/.profile" + echo "Updated existing .profile" + else + echo "" >> "$HOME/.bashrc" + echo "$SNIPPET" >> "$HOME/.bashrc" + echo "Updated existing .bashrc" + fi +fi + +echo "All done!" + +echo "" +echo "Please open a new terminal and enter 'gvm help' to get started." +echo "Enjoy!!!" diff --git a/srv/server.groovy b/srv/server.groovy new file mode 100644 index 00000000..d697d033 --- /dev/null +++ b/srv/server.groovy @@ -0,0 +1,113 @@ +import groovy.text.SimpleTemplateEngine +import org.vertx.groovy.core.http.RouteMatcher + +def grails = ['1.3.7','1.3.8','1.3.9','2.0.0','2.0.1','2.0.2','2.0.3','2.0.4','2.1.0','2.1.1'] +def groovy = ['1.8.8','2.0.0','2.0.1','2.0.2'] +def griffon = ['1.0.0','1.0.1','1.0.2'] +//def candidates = [grails:grails, groovy:groovy, griffon:griffon] +def candidates = [grails:grails] +//def defaults = [grails:'2.1.1', groovy:'2.0.2', griffon:'1.0.2'] +def defaults = [grails:'2.1.1'] + +def gvmVersion = '0.1' +def serverVersion = '0.1' +def vertxVersion = '1.2.3.final' + +def templateEngine = new SimpleTemplateEngine() + +def rm = new RouteMatcher() + +rm.get("/") { req -> + addPlainTextHeader req + req.response.sendFile('scripts/install.sh') +} + +rm.get("/res/init") { req -> + addPlainTextHeader req + req.response.sendFile('scripts/gvm-init.sh') +} + +rm.get("/res/gvm") { req -> + addPlainTextHeader req + req.response.sendFile('scripts/gvm.sh') +} + +rm.get("/candidates") { req -> + addPlainTextHeader req + req.response.end buildCsv(candidates.keySet()) +} + +rm.get("/candidates/:candidate") { req -> + def candidate = req.params['candidate'] + def versions = buildCsv(candidates[candidate])?.toString() + addPlainTextHeader req + req.response.end (versions ?: "") +} + +rm.get("/candidates/:candidate/default") { req -> + def candidate = req.params['candidate'] + addPlainTextHeader req + req.response.end defaults[candidate] +} + +rm.get("/candidates/:candidate/list") { req -> + def candidate = req.params['candidate'] + def current = req.params['current'] + def installed = req.params['installed'] + def gtplFile = new File('templates/list.gtpl') + def binding = [candidate:candidate, available:grails, current:current, installed:installed] + def template = templateEngine.createTemplate(gtplFile).make(binding) + addPlainTextHeader req + req.response.end template.toString() +} + +rm.get("/candidates/:candidate/:version") { req -> + def candidate = req.params['candidate'] + def version = req.params['version'] + def found = (candidates[candidate].find { it == version}) ? version : 'invalid' + addPlainTextHeader req + req.response.end found +} + +rm.get("/download/:candidate/:version") { req -> + def candidate = req.params['candidate'] + def version = req.params['version'] + addPlainTextHeader req + req.response.end "http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/${candidate}-${version}.zip" +} + +rm.get("/app/version") { req -> + addPlainTextHeader req + req.response.end '0.1' +} + +rm.get("/app/alive/:version") { req -> + addPlainTextHeader req + def version = req.params['version'] + def gtpFile, binding + if(gvmVersion == version){ + gtplFile = new File('templates/broadcast.gtpl') + binding = [server:serverVersion, vertx:vertxVersion] + } else { + gtplFile = new File('templates/upgrade.gtpl') + binding = [version:gvmVersion] + } + def template = templateEngine.createTemplate(gtplFile).make(binding) + req.response.end template.toString() +} + +private addPlainTextHeader(req){ + req.response.putHeader("Content-Type", "text/plain") +} + +def port = System.getenv('PORT') ?: 8080 +def host = System.getenv('PORT') ? '0.0.0.0' : 'localhost' +println "Starting vertx on $host:$port" +vertx.createHttpServer().requestHandler(rm.asClosure()).listen(port as int, host) + +private buildCsv(list){ + if(!list) return "" + def csv = '' + list.each { csv += "$it," } + csv[0..-2] +} diff --git a/srv/templates/broadcast.gtpl b/srv/templates/broadcast.gtpl new file mode 100644 index 00000000..89a3b94c --- /dev/null +++ b/srv/templates/broadcast.gtpl @@ -0,0 +1,10 @@ +GVM server ${server} on vertx ${vertx} + +=== BROADCAST ========================================== +This application is currently in beta. If you experience +any problems then please raise an issue on GitHub at: +https://github.com/freshgroovy/gvm/issues + +Only Grails is available in this beta release. Groovy, +Griffon and Gradle coming soon! +======================================================== \ No newline at end of file diff --git a/srv/templates/list.gtpl b/srv/templates/list.gtpl new file mode 100644 index 00000000..beb71601 --- /dev/null +++ b/srv/templates/list.gtpl @@ -0,0 +1,21 @@ + +-------------------------------------------------------- +Available ${candidate.capitalize()} Versions +-------------------------------------------------------- +<% + available.each { version -> + print "\n" + if(current == version) { + print ' > ' + } else if(installed.contains(version)){ + print ' * ' + } else { + print ' ' + } + print version + } +%> +-------------------------------------------------------- +* - installed +> - currently in use +-------------------------------------------------------- diff --git a/srv/templates/upgrade.gtpl b/srv/templates/upgrade.gtpl new file mode 100644 index 00000000..47fa41bf --- /dev/null +++ b/srv/templates/upgrade.gtpl @@ -0,0 +1,5 @@ + +=============================================== + Your version of gvm is out of date! + Please upgrade to the latest version: ${version} +===============================================