Skip to content

Commit

Permalink
first version of virgo-bundles.sh script
Browse files Browse the repository at this point in the history
  • Loading branch information
darklynx committed Jan 11, 2014
1 parent c22cca5 commit bd0d0eb
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 darklynx
Copyright (c) 2014 Vladimir Lyubitelev

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
242 changes: 242 additions & 0 deletions virgo-bundles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
#!/bin/bash

###########################################################
#
# CLI tool to manage Virgo OSGi bundles
# Version: 1.0.0
#
# License: MIT (see http://choosealicense.com/licenses/mit/)
# Author: Vladimir Lyubitelev
#

VERSION=1.0.0
VIRGO_URL=http://localhost:8080
VIRGO_USER=admin:springsource

BUNDLE_FILE=
BUNDLE_NAME=
BUNDLE_VERSION=
BUNDLE_TYPE=bundle

VERBOSE=false

# First parameter is always a command
COMMAND=$1
shift

# Parse arguments
while (($# > 0)); do
case $1 in
-f)
BUNDLE_FILE=$2
shift;
;;
-n)
BUNDLE_NAME=$2
shift;
;;
-v)
BUNDLE_VERSION=$2
shift;
;;
-t)
BUNDLE_TYPE=$2
shift;
;;
-user)
VIRGO_USER=$2
shift;
;;
-url)
VIRGO_URL=$2
shift;
;;
-verbose)
VERBOSE=true
;;
*)
echo "Unknown parameter: $1"
;;
esac
shift
done

SILENT="-s"
if [ "${VERBOSE}" = "true" ]; then
SILENT=""
fi

############################################
# Upload and deploy bundle
if [ "${COMMAND}" = "deploy" ]; then
if [ -z "${BUNDLE_FILE}" ]; then
echo "Error: location of OSGi bundle is not specified, use option -f"
exit 1
fi

if [ "${VERBOSE}" = "true" ]; then
echo "Uploading and deploying bundle: ${BUNDLE_FILE}"
fi

# command
RESULT=`curl ${SILENT} -u ${VIRGO_USER} -F "file=@${BUNDLE_FILE}" ${VIRGO_URL}/admin/upload`

if [ "${VERBOSE}" = "true" ]; then
echo "-------------------------"
echo "Virgo response: ${RESULT}"
echo "-------------------------"
fi

# analyze result
MESSAGE=`echo "$RESULT" | grep -oEi "<ol id=\"uploadResults\"><li>.+</li>" | cut -c 28- | rev | cut -c 6- | rev`
if [ -z "${RESULT}" ]; then
echo "Failure"
echo "Details: received empty result"
exit 2
elif [ -z "${MESSAGE}" ]; then
echo "Failure"
echo "Details: unexpected format of result: ${RESULT}"
exit 2
else
if [ -z "`echo ${MESSAGE} | grep ".* deployed as bundle .*"`" ]; then
echo "Failure"
echo "Details: ${MESSAGE}"
exit 2
else
DEPLOYED_NAME=`echo ${MESSAGE} | grep -oEi "as bundle - .+:" | cut -c 13- | rev | cut -c 2- | rev`
DEPLOYED_VERSION=`echo ${MESSAGE} | grep -oEi ": .+" | cut -c 3-`
echo "Deployed: name=${DEPLOYED_NAME}, version=${DEPLOYED_VERSION}"
fi
fi

############################################
# Check bundle status
elif [ "${COMMAND}" = "status" ]; then
if [ -z "${BUNDLE_NAME}" ]; then
echo "Error: bundle symbolic name is not specified, use option -n"
exit 1
fi
if [ -z "${BUNDLE_VERSION}" ]; then
echo "Error: bundle version is not specified, use option -v"
exit 1
fi

if [ "${VERBOSE}" = "true" ]; then
echo "Checking status of bundle: ${BUNDLE_NAME} version: ${BUNDLE_VERSION}"
fi

# command
RESULT=`curl ${SILENT} -u ${VIRGO_USER} ${VIRGO_URL}/admin/jolokia/read/org.eclipse.virgo.kernel:artifact-type=${BUNDLE_TYPE},name=${BUNDLE_NAME},region=org.eclipse.virgo.region.user,type=ArtifactModel,version=${BUNDLE_VERSION}`

if [ "${VERBOSE}" = "true" ]; then
echo "-------------------------"
echo "Virgo response: ${RESULT}"
echo "-------------------------"
fi

# analyze result
RESULT_STATUS=`echo "$RESULT" | grep -oEi ",\"status\":[0-9]*," | cut -c 11- | rev | cut -c 2- | rev`
if [ -z "${RESULT}" ]; then
echo "Failure"
echo "Details: received empty result"
exit 2
elif [ "${RESULT_STATUS}" = "200" ]; then
BUNDLE_STATE=`echo "$RESULT" | grep -oPi ",\"State\":\".+?\"," | cut -c 11- | rev | cut -c 3- | rev`
echo "Bundle state: ${BUNDLE_STATE}"
else
echo "Failure"
echo "Details: ${RESULT}"
exit 2
fi

############################################
# Execute command: stop, start, uninstall, refresh
elif [[ "${COMMAND}" = "stop" || "${COMMAND}" = "start" || "${COMMAND}" = "uninstall" || "${COMMAND}" = "refresh" ]]; then
if [ -z "${BUNDLE_NAME}" ]; then
echo "Error: bundle symbolic name is not specified, use option -n"
exit 1
fi
if [ -z "${BUNDLE_VERSION}" ]; then
echo "Error: bundle version is not specified, use option -v"
exit 1
fi

if [ "${VERBOSE}" = "true" ]; then
echo "Stopping bundle: ${BUNDLE_NAME} version: ${BUNDLE_VERSION}"
fi

# command
RESULT=`curl ${SILENT} -u ${VIRGO_USER} ${VIRGO_URL}/admin/jolokia/exec/org.eclipse.virgo.kernel:artifact-type=${BUNDLE_TYPE},name=${BUNDLE_NAME},region=org.eclipse.virgo.region.user,type=ArtifactModel,version=${BUNDLE_VERSION}/${COMMAND}`

if [ "${VERBOSE}" = "true" ]; then
echo "-------------------------"
echo "Virgo response: ${RESULT}"
echo "-------------------------"
fi

# analyze result
RESULT_STATUS=`echo "$RESULT" | grep -oEi ",\"status\":[0-9]*," | cut -c 11- | rev | cut -c 2- | rev`
if [ -z "${RESULT}" ]; then
echo "Failure"
echo "Details: received empty result"
exit 2
elif [ "${RESULT_STATUS}" = "200" ]; then
case ${COMMAND} in
stop)
echo "Stopped"
;;
start)
echo "Started"
;;
uninstall)
echo "Uninstalled"
;;
refresh)
echo "Refreshed"
;;
*)
echo "Something new: ${COMMAND}"
;;
esac
else
echo "Failure"
echo "Details: ${RESULT}"
exit 2
fi

############################################
# Display help
else
if [[ "${COMMAND}" != "help" && "${COMMAND}" != "" ]]; then
echo "Error: unknown command /${COMMAND}/"
fi
echo "Virgo OSGi bundels management tool. Version ${VERSION}"
echo
echo "Usage:"
echo " $0 <command> [options]"
echo
echo "Commands:"
echo " deploy - upload and deploy OSGi bundle to Virgo server, required options: -f"
echo " status - check status of bundle at Virgo server, required options: -n, -v"
echo " stop - stop bundle at Virgo server, required options: -n, -v"
echo " start - start bundle at Virgo server, required options: -n, -v"
echo " refresh - refresh bundle at Virgo server, required options: -n, -v"
echo " uninstall - uninstall bundle from Virgo server, required options: -n, -v"
echo " help - display this help"
echo
echo "Options:"
echo " -f <path> - location of OSGi bundle to upload, e.g. /opt/repo/org.slf4j.api-1.7.2.jar"
echo " -n <name> - bundle symbolic name, e.g. org.slf4j.api"
echo " -v <ver> - bundle version, e.g. 1.7.2"
echo " -t <type> - bundle type, possible types: bundle, plan, ??"
echo " -user <*> - user name and password for basic auth, e.g. admin:passwd"
echo " -url <*> - Virgo server URL, e.g. http://virgo.internal:7070"
echo " -verbose - enable verbose output"
echo
echo "Examples:"
echo " $0 deploy -f ~/dev/virgo-test-1.0.0-SNAPSHOT.jar"
echo " $0 status -n virgo-test -v 1.0.0.SNAPSHOT"
echo " $0 stop -n virgo-test -v 1.0.0.SNAPSHOT -url http://localhost:8081"
echo " $0 start -n virgo-test -v 1.0.0.SNAPSHOT -verbose"
echo " $0 uninstall -n virgo-test -v 1.0.0.SNAPSHOT"
fi

0 comments on commit bd0d0eb

Please sign in to comment.