If you use GNU bash, you can also use client-side macros to access the CA AppLogic® shell commands directly from CLI commands on your own Linux machine or a VDS-type Linux server running on the grid.
Client-side macros for GNU Bash and OpenSSH client provide easy access to all the functionality of 3tshell - the CA AppLogic® command line interface, directly from your local shell prompt.
Features:
To use this file on your system:
You can also perform this steps in the .bashrc file so that it is available to every interactive shell instance that you start.
Once the rmacros.sh file is loaded, the following commands become available from the shell prompt. The same commands can also be used in a non-interactive script by simply including the rmacros.sh file from your script with the "source" command:
Set the hostname of the controller to which to direct subsequent commands. This must be done before using any of the other commands.
Disconnect from controller. This closes the master ssh session with the controller.
This is the same as typing the command at the CA AppLogic® shell prompt. This form is useful for simple commands, and for unattended batch operation. use 3t -t for commands that require user interaction (for example, vol manage).
Same as 3t any 3tshell command, but enables interactive mode, by running the CA AppLogic® shell with the ssh pseudo-terminal enabled.
Invoke the interactive CA AppLogic® shell.
Set the current application. This works the same way as the 'ca' command in the interactive shell. Executing 3t ca myapp won't work because a new instance of the CA AppLogic® shell is started by each invocation of the '3t' command.
Print the name of the current application (as set by ca)
Open an interactive shell to a running appliance instance in the current application, or in the specified application. component is the instance's full name in the application hierarchy. The name of the top-level assembly (main.) can be omitted, for example, the following are equivalent:
assh the-app:main.web.srv1 ca the-app ; assh web.srv1
Run a non-interactive command on an appliance instance. NOTE: on older (1.x) versions of CA AppLogic®, the command name and the arguments cannot contain spaces, that is, this will not work as expected:
assh web.srv1 cat "the file with spaces in its name".
If you must do this on an older grid, make a short script and copy it to the running appliance using the ascp command below, then start that script.
Copy a file to/from a running appliance. One of the src or dest parameters must be the name of a local file or directory. The other specifies the remote appliance and file name in a form similar to that of the 'scp' command:
comp.name:filename
The component name is the full path of the component in the application assembly (as with assh, the main. prefix can be omitted). This command requires that the current application be set with the ca command.
Shortcut. same as 3t app list.
Shortcut. Same as 3t app start `pwa`.
Shortcut. Same as 3t app stop `pwa`.
Not supported in CA AppLogic® 2.3 and later. Use the new vol manage operation instead.
Not supported in CA AppLogic® 2.3 and later.
Configuring Custom SSH Options
Other than the normal way of setting SSH options (which is to put them in your ~/.ssh/config file), the commands provided by rmacros.sh also accept an environment variable setting with options that affect only ssh invocations generated by the macros, but not any other ssh commands. This is useful if you need a different set up for working with the CA AppLogic® grid(s) and for other ssh sessions, and the per-host method supported by the .ssh/config file is not suitable to separate the different options. This includes the case when you also have "maintainer" access to the CA AppLogic® grid (for example, if you installed that grid) - in such a case you have an SSH key that provides root access to the controller - not what is needed to work as a normal "client".
To provide additional options to the remote access macros in rmacros.sh, set the AL_SSH_OPT variable. It should contain options in the form accepted by ssh and all of its companion programs (scp, sftp, and so on): -o OptionName=option-value. Do not use shorthand options like -i, -t, and so on - they work with ssh, but not with scp.
Example: export AL_SSH_OPT='-o IdentityFile=/home/lk/t-key -o IdentitiesOnly=yes -o StrictHostKeyChecking=no'
This tells SSH: Use the key /home/lk/t-key for public-key authentication; do not use any keys served by ssh-agent; disable strict host identity checking (use the last one only if you frequently re-install grids from scratch, causing the SSH Id of the controller to change every time).
Fast and Easy Access with Master SSH Session
The functions defined in rmacros.sh use the Master Session feature of the OpenSSH client. The first command that is run will start a background ssh client in 'master' mode, and all subsequent commands use the session maintained by the 'master' to run additional ssh sessions over the same secure channel.
This provides two benefits: first, typing a passphrase to access the SSH private key is only necessary once - after that, the session is maintained open and no additional logins are made.
Second, this makes new invocations of 'ssh' as fast as typing commands to an already open SSH session - faster, in fact - because the command is sent once, when you press Enter, while an interactive session uses character-by-character echo as you type the command.
Also, running the commands locally from your shell and not from the 3tshell console allows one to use the full power of the shell, to make up commands that are not possible with an interactive 3tshell session, for example:
Get a list of running applications:
3t app list | grep running
Save volume list to a file:
3t vol list --all >file
Transfer configuration from one application to another:
3t app config app1 --batch | 3t app config app2 --stdin
Note: the code provided here is an example, illustrating a possible way to enhance and simplify access to the AppLogic CLI using ssh.It is provided as-is and is not part of the AppLogic supported code.CA Support will not provide help, updates or hotfixes for it. Errors and omissions found here can be reported to CA and this file will be corrected if needed, as part of the CA standard online documentation update cycle.
#!/bin/bash
# Client-side macros for access to AppLogic grid controller
# (C) L.Kalev 2006 (lion//3tera.com)
# temp dir for master-session sockets
export CSPATH=/tmp/apl_lnkroot
# controller name
export AL_CTL
export AL_CTLSOCKOPT
export PWA
ssh_cmd="ssh -l root $AL_SSH_OPT"
# !! _FS must match the value set in '3trsh' for this to work!
_FS='`'
function pack()
{
local i="$IFS"
IFS="$_FS"
ARGS="$*$_FS"
IFS="$i"
return 0
}
## unpack
#OLD_IFS="$IFS"
#if [[ "$T" = *$_FS* ]] ; then IFS="$_FS" ; fi
#set $T
#IFS="$OLD_IFS"
function controller ()
{
[[ -z "$1" ]] && { echo $AL_CTL ; return 0 ; }
local OPT_FC=
if [ "$1" = '-f' ] ; then OPT_FC=1 ; shift ; fi
[[ "$1" != "$AL_CTL" ]] && PWA=
# close old connection, if needed
if [[ -n "$AL_CTL" ]] && [[ -S $CSPATH/$AL_CTL ]] ; then
if [[ -z "$OPT_FC" ]] && [[ "$1" != "-" ]] ; then
AL_CTL=$1
AL_CTLSOCKOPT="-o ControlPath=$CSPATH/$AL_CTL"
return 0 # don't re-connect if connection is open
fi
echo "Closing old connection to $AL_CTL" >&2
kill $(<$CSPATH/$AL_CTL.pid)
fi
AL_CTL=$1
if [ "$AL_CTL" = '-' ] ; then AL_CTL= ; return 0 ; fi # disconnect only
# make sure it has no spaces or other junk
set -- $AL_CTL
[[ "$1" != "$AL_CTL" ]] && { echo "invalid hostname" >&2 ; return 1 ; }
ping -c 1 -n -q "$AL_CTL" >/dev/null || echo "warning: ping $AL_CTL failed"
AL_CTLSOCKOPT="-o ControlPath=$CSPATH/$AL_CTL"
}
function sock-chk ()
{
[[ -z "$AL_CTL" ]] && { echo "controller name not set, use the 'controller' command to set it" >&2 ; return 1 ; }
if [[ -S $CSPATH/$AL_CTL ]] ; then return 0 ; fi
mkdir -p $CSPATH
# redirect all handles of the background ssh process, in case our
# parent waits for eof on a std handle (it won't get it, if the handle
# gets dup()ed into the forked ssh)
$ssh_cmd -M -N -f $AL_CTLSOCKOPT $AL_CTL >/dev/null </dev/null 2>$CSPATH/$AL_CTL.2
local R=$?
if [[ $R != 0 ]] ; then
cat $CSPATH/$AL_CTL.2 >&2
return $R
fi
T=`ps -C ssh -o pid,args | grep -e "-M -N -f $AL_CTLSOCKOPT"`
set -- $T
echo $1 >$CSPATH/$AL_CTL.pid
return 0
}
function 3t ()
{
sock-chk || return 1
local T=
local A=
[[ $# == 0 ]] && T=-t
if [[ "$1" = '-t' ]] ; then T=-t ; shift ; fi
[[ -n "$PWA" ]] && A="=$PWA"
local TC1 TC2
TC1=`stat --format '%Y' $CSPATH/$AL_CTL.2`
ARGS="$A"
if [ $# != 0 ] ; then pack $A "$@" ; fi
$ssh_cmd $T -o LogLevel=ERROR $AL_CTLSOCKOPT $AL_CTL "$ARGS"
A=$?
TC2=`stat --format '%Y' $CSPATH/$AL_CTL.2`
if [[ "$TC1" != "$TC2" ]] && ! [[ -S $CSPATH/$AL_CTL ]] ; then
echo "Connection to controller lost, re-run the command to reconnect"
return 1
fi
return $A
}
function ca ()
{
[[ -n "$1" ]] || { PWA= ; return 0 ; }
# [[ -n "$1" ]] || { echo "use: ca app-name" >&2; return 1; }
3t app info "$1" >/dev/null || return $?
export PWA=$1
return 0
}
function pwa ()
{
echo $PWA
}
function assh ()
{
[[ -z "$1" ]] && { echo "use: assh [app:]comp" >&2 ; return 1 ; }
local APP
# this can be removed when we fix the curr. app support in 3trsh
if [[ $1 == *:* ]] ; then
APP=$1
else
[[ -z "$PWA" ]] && { echo "current app not set, use full name" >&2; return 1; }
APP=$PWA:$1
fi
sock-chk || return 1;
shift
local T=
if [[ -z "$@" && -t 0 && -t 1 ]] ; then
# echo "using tty"
T=-t
fi
local TC1 TC2
TC1=`stat --format '%Y' $CSPATH/$AL_CTL.2`
ARGS="sh $APP"
if [ $# != 0 ] ; then pack sh $APP "$@" ; fi
$ssh_cmd $T $AL_CTLSOCKOPT $AL_CTL "$ARGS"
TC2=`stat --format '%Y' $CSPATH/$AL_CTL.2`
if [[ "$TC1" != "$TC2" ]] && ! [[ -S $CSPATH/$AL_CTL ]] ; then
echo "Connection to controller lost, re-run the command to reconnect"
return 1
fi
}
function ascp ()
{
# temporary - operation w/o current app can wait
[[ -n "$PWA" ]] || { echo "current app. not set" >&2; return 1; }
sock-chk || return 1
# NB: 'local' and the {#} substr match are specific to 'bash', won't work on std bourne shell
local A1=$1
local P1=${1%%:*}
local P2=
local S1=
if [[ "$P1" != $1 ]] ; then # $1 is remote
S1=${1#*:}
[[ $S1 == /* ]] || S1=/root/$S1
[[ -n $P1 ]] || return 1;
A1="root@$AL_CTL:/app/$PWA/$P1$S1"
fi
local A2=$2
P1=${2%%:*}
if [[ "$P1" != $2 ]] ; then # $2 is remote
S1=${2#*:}
[[ $S1 == /* ]] || S1=/root/$S1
[[ -n $P1 ]] || return 1;
A2="root@$AL_CTL:/app/$PWA/$P1$S1"
fi
# should have the same options as $ssh_cmd
scp $AL_SSH_OPT $AL_CTLSOCKOPT "$A1" "$A2"
}
# ---------- various shortcuts
function als ()
{
3t app list
}
function astop ()
{
3t app stop "$@"
}
function astart ()
{
3t app start "$@"
}
# not very useful for now (need to get it to mount on OUR system)
function vmnt ()
{
3t vol mount "$@"
}
function vumnt ()
{
3t vol unmount "$@"
}
# ssh $T -S /tmp/opty3sk -i ~/t-key -o IdentitiesOnly=yes
# master start
# ssh -A -i ~/t-key -M -S /tmp/opty3sk -N -f -o IdentitiesOnly=yes $AL_CTL
|
Copyright © 2012 CA.
All rights reserved.
|
|