September 22, 2015

How to pass arguments to a Bash-script

1. Using getopts

1. Create new Shell Script

$ vi test.sh

2. Copy the following line into test.sh file

while getopts p:u:f:n:r: option
do
  case "${option}" in
    p) weblogic_password=${OPTARG};;
    u) weblogic_username=${OPTARG};;
    f) war_file=${OPTARG};;
    n) ws_name=${OPTARG};;
    r) ROLE_NAME=${OPTARG};;
  esac
done

echo ${weblogic_password}
echo $weblogic_username
echo $war_file
echo $ws_name
echo $ROLE_NAME

3.Run the script

$ chmod +x test.sh
$ ./test.sh -p weblogic_password -u weblogic_username -f war_file -n web_name -r role_name

2. Using environment variables

See the example below:
#!/bin/sh

OIFS=$IFS;
IFS=",";

usage() {
    cat <<EOF
Usage: $0 [options] [--] [file...]

Arguments:

  -h, --help
    Display this usage message and exit.

  -f <val>, --foo <val>, --foo=<val>
    Documentation goes here.

  -b <val>, --bar <val>, --bar=<val>
    Documentation goes here.

  --
    Treat the remaining arguments as file names.  Useful if the first
    file name might begin with '-'.

  file...
    Optional list of file names.  If the first file name in the list
    begins with '-', it will be treated as an option unless it comes
    after the '--' option.
EOF
}

# handy logging and error handling functions
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$*"; exit 1; }
usage_fatal() { error "$*"; usage >&2; exit 1; }

# parse options
foo="foo default value goes here"
bar="bar,default,value,goes,here"
barArray=($bar)

while [ "$#" -gt 0 ]; do
    arg=$1
    case $1 in
        # convert "--opt=the value" to --opt "the value".
        # the quotes around the equals sign is to work around a
        # bug in emacs' syntax parsing
        --*'='*) shift; set -- "${arg%%=*}" "${arg#*=}" "$@"; continue;;
        -f|--foo) shift; foo=$1;;
        -b|--bar) shift;
                  bar=$1;
                  barArray=($bar);
                  ;;
        -h|--help) usage; exit 0;;
        --) shift; break;;
        -*) usage_fatal "unknown option: '$1'";;
        *) break;; # reached the list of file names
    esac
    shift || usage_fatal "option '${arg}' requires a value"
done
#arguments are now the file names


echo "foo=" $foo
echo "bar=" $bar
echo "barArray size=" ${#barArray[@]}
for ((i=0; i<${#barArray[@]}; ++i)); do
  echo "bar $i: ${barArray[$i]}";
done

IFS=$OIFS;

September 17, 2015

Creating WebLogic DataSource Using WLST and Properties File

How to Configure WebLogic DataSource using WLST?

Step 1. Create working directory and Server properties file

Create a directory at the machine that has the Weblogic Server installed on.
Example: (Linux OS) create directory called /home/create_DS
place a properties file like below in this directory with name "server.properties"

domain.name=7001_Domain
admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=weblogicPassword

datasource.name=jdbc/DS_Name
datasource.database.name=demo

datasource.targets_server=AdminServer
datasource.targets_cluster=cluster_server_1,cluster_server_2

datasource.filename=jdbc_DS_Name.xml
datasource.jndiname=DS_Name
datasource.driver.class=oracle.jdbc.OracleDriver
datasource.url=jdbc:oracle:thin:@192.168.1.1:1521/DB_NAME
datasource.username=DB_Username
datasource.password=DB_Password

datasource.test.query=SQL SELECT * FROM DUAL

Step 2. Create createDataSource.py

Now in the same directory write the following WLST Script “createDataSource.py” like following:

from java.io import FileInputStream

propInputStream = FileInputStream("server.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")

dsName=configProps.get("datasource.name")
dsFileName=configProps.get("datasource.filename")
dsDatabaseName=configProps.get("datasource.database.name")

dsJNDIName=configProps.get("datasource.jndiname")
dsDriverName=configProps.get("datasource.driver.class")
dsURL=configProps.get("datasource.url")
dsUserName=configProps.get("datasource.username")
dsPassword=configProps.get("datasource.password")
dsTestQuery=configProps.get("datasource.test.query")

try:
  connect(adminUserName, adminPassword, adminURL)
  edit()
  startEdit()
  cd('/')
  cmo.createJDBCSystemResource(dsName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName)
  cmo.setName(dsName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
  set('JNDINames',jarray.array([String(dsName)], String))

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName )
  cmo.setUrl(dsURL)
  cmo.setDriverName( dsDriverName )
  cmo.setPassword(dsPassword)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCConnectionPoolParams/' + dsName )
  cmo.setTestTableName(dsTestQuery)
  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
  cmo.createProperty('user')

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/user')
  cmo.setValue(dsUserName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
  cmo.createProperty('databaseName')

  #cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/databaseName')
  #cmo.setValue(dsDatabaseName)

  cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
  cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

  cd('/SystemResources/' + dsName )

  targets=PyList()
  datasource_targets_server=configProps.get("datasource.targets_server").split(",")
  datasource_targets_cluster=configProps.get("datasource.targets_cluster").split(",")

  for datasourceTarget in datasource_targets_server:
    targetStr='com.bea:Name='+datasourceTarget+',Type=Server'
    targets.append(ObjectName(targetStr))
  
  for datasourceTarget in datasource_targets_cluster:
    targetStr='com.bea:Name='+datasourceTarget+',Type=Cluster'
    targets.append(ObjectName(targetStr))

  set('Targets',jarray.array(targets,ObjectName))

  save()
  activate()
except Exception, e:
  print 'ERROR:'
  print e
  print 'Operation was canceled.'
  undo('true','y')
  cancelEdit('y')
  exit()

Step 3. Running setWLSEnv.sh script

Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the /wlserver_10.3/server/bin) then run the following command…. . ./setWLSEnv.sh
Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step 4. Running the createDataSource.py script

Run the Above WLST Script like following:
java weblogic.WLST createDataSource.py

OR
java -cp /<WL_HOME>/wlserver/server/lib/weblogic.jar weblogic.WLST createDataSource.py

Make sure that the Database is running while you are creating the DataSource.

September 9, 2015

Connecting Remotly to Open Shift via mysql workbench

Connecting Remotly to Open Shift via mysql workbench

1. Connect to Server using the following command from command prompt

$ rhc ssh primefaces

Results:
Connecting to 556d735ee0b8cd6557000004@primefaces-sawalha.rhcloud.com ...

    *********************************************************************

    You are accessing a service that is for use only by authorized users.
    If you do not have authorization, discontinue use at once.
    Any use of the services is subject to the applicable terms of the
    agreement which can be found at:
    https://www.openshift.com/legal

    *********************************************************************

    Welcome to OpenShift shell

    This shell will assist you in managing OpenShift applications.

    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!
    Shell access is quite powerful and it is possible for you to
    accidentally damage your application.  Proceed with care!
    If worse comes to worst, destroy your application with "rhc app delete"
    and recreate it
    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!

    Type "help" for more info.

2. Get the MySQL parameters using the following command:

> export | grep mysql

Results:
declare -x LD_LIBRARY_PATH="/opt/rh/mysql55/root/usr/lib64:"
declare -x MANPATH="/opt/rh/mysql55/root/usr/share/man::"
declare -x OPENSHIFT_MYSQL_DB_SOCKET="/var/lib/openshift/556d735ee0b8cd6557000004/mysql//socket/mysql.sock"
declare -x OPENSHIFT_MYSQL_DB_URL="mysql://mysql_username:mysql_password@127.7.235.2:3306/"
declare -x OPENSHIFT_MYSQL_DIR="/var/lib/openshift/556d735ee0b8cd6557000004/mysql/"
declare -x OPENSHIFT_MYSQL_IDENT="redhat:mysql:5.5:0.2.20"
declare -x OPENSHIFT_MYSQL_LD_LIBRARY_PATH_ELEMENT="/opt/rh/mysql55/root/usr/lib64"

3. Copy the following:


SSH Hostname: primefaces-sawalha.rhcloud.com
SSH Username: 556d735ee0b8cd6557000004
MySQL username and password

4. Open MySQL workbench and create new connection




Read also...

Using FileZilla and SFTP on Windows with OpenShift

Create Connection to server using CyberDuck