Sample STAX

   1: <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
   2: <!DOCTYPE stax SYSTEM "stax.dtd"> 
   3: <stax> 
   4: <script> 
   5:     from ConfigParser import ConfigParser 
   6: </script> 
   7:  
   8: <defaultcall function="main"/> 
   9:  
  10: <function name="main"> 
  11:   <function-map-args>
  12:     <function-optional-arg name="type" default="'win03uepinstall'"/>
  13:   </function-map-args>
  14: <sequence> 
  15:  
  16: <script> 
  17:     result = 1 
  18:     filename = 'c:/STAF/services/custom/test/sample.cfg'
  19:     file=open(filename)
  20:     config = ConfigParser() 
  21:     config.read(filename) 
  22:     vmfile = config.get(type, 'VM_FILE')
  23:     vmhost = config.get(type, 'VM_HOST')
  24:     vmsnapshot = config.get(type, 'VM_SNAPSHOT')
  25:     host = config.get(type, 'HOST') 
  26:     content = file.readlines()
  27: </script> 
  28:     
  29: <process name="'revertSnapShot'"> 
  30:     <location>vmhost</location> 
  31:     <command>'vmrun revertToSnapshot "%s" "%s"' %(vmfile,vmsnapshot)</command>
  32: </process>
  33: <if expr="RC != 0">
  34:     <return>STAXResult[0][1]</return>
  35: </if>
  36:     
  37: <process name="'startSnapShot'"> 
  38:     <location>vmhost</location> 
  39:     <command>'vmrun start "%s"' %(vmfile)</command>
  40: </process>
  41: <if expr="RC != 0">
  42:     <return>STAXResult[0][1]</return>
  43: </if>
  44:  
  45: <script>pingRC = 1</script>
  46: <loop until="pingRC == 0">
  47:     <sequence>
  48:         <stafcmd>
  49:             <location>'local'</location>
  50:             <service>'delay'</service>
  51:             <request>'delay 3000'</request>
  52:         </stafcmd>    
  53:         <if expr="RC != 0">
  54:             <return>STAFResult[0][1]</return>
  55:         </if>
  56:         <stafcmd>
  57:             <location>vmhost</location>
  58:             <service>'ping'</service>
  59:             <request>'ping'</request>
  60:         </stafcmd>
  61:         <script>pingRC = RC</script>    
  62:     </sequence>
  63: </loop>
  64:     
  65: <return>'Bingo!'</return> 
  66: </sequence> 
  67: </function> 
  68:  
  69: </stax> 
<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!DOCTYPE stax SYSTEM "stax.dtd"> 
<stax> 
 
<defaultcall function="main"/> 
    
<script> 
    projectPath = "C:/Documents and Settings/pyin/workspace_Europa"
    tomachine = "10.42.97.208"
    todirectory = "c:/tmp"
    zipfile = "C:/Documents and Settings/pyin/workspace_Europa/project.zip"
    tozipfile = "C:/tmp/project.zip"
</script> 
 
<function name="main"> 
  <function-map-args>
    <function-optional-arg name="projectPath" default='projectPath'/>
    <function-optional-arg name="tomachine" default='tomachine'/>
    <function-optional-arg name="todirectory" default='todirectory'/>
    <function-optional-arg name="zipfile" default='zipfile'/>
  </function-map-args>
<sequence> 
 
    
    <stafcmd>
        <location>'local'</location>
        <service>'zip'</service>
        <request>'add zipfile "%s" directory "%s" recurse relativeto "%s"' %(zipfile, projectPath,projectPath)</request>
    </stafcmd>    
    <if expr="RC != 0">
        <return>STAFResult</return>
    </if>
    
    <stafcmd>
        <location>'local'</location>
        <service>'fs'</service>
        <request>'copy file "%s" tomachine "%s" tofile "%s"' %(zipfile, tomachine, tozipfile)</request>
    </stafcmd>    
    <if expr="RC != 0">
        <return>STAFResult</return>
    </if>
    
    <stafcmd>
        <location>tomachine</location>
        <service>'zip'</service>
        <request>'unzip zipfile "%s" todirectory "%s"' %(tozipfile, todirectory)</request>
    </stafcmd>    
    <if expr="RC != 0">
        <return>STAFResult</return>
    </if>
    
<return>'Bingo!'</return> 
</sequence> 
</function> 
 
</stax> 

Finally get STAF installed on Ubuntu

I’ve been suffered a lot in trying to install STAF on Ubuntu, it turns out it was all worth those hardness, because it make me understand some key concepts of Linux.
1. In installation path:
The default the path is /usr/local/staf, which still confused me. As the execution of STAF will eventually create lots of files, such opertions will have permission constraint. Of cause, this can be overcomed by using "sudo STAF", which get the root provilledge. But in Ubuntu, the use of sudo will switch the shell environment to root’s configuration, this will bring difficulties in setting PATH for STAF executables. As a result, after I change the installation path to ~/staf, where I have write permission, everything works without problem.
2. Reuse of Java
The version of STAF I choosed is without JVM, this means I have to point STAF an existing JVM when I need to add services based on JSTAF. This is done by specified the JVM option:

SERVICE Cron LIBRARY JSTAF EXECUTE {STAF/Config/STAFRoot}/services/cron/STAFCron.jar  
OPTION JVM=/opt/jdk50/bin/java

Notice the JSTAF must be capitalized to ensure the name of a lib file libJSTAF.co is correct constructed.


Import Python lib in STAX service

Lots of thanks to Dave, I really learned a lot in how to run and debug Python code in STAX xml. 

https://sourceforge.net/forum/forum.php?thread_id=3272091&forum_id=104045


File path in STAX

The file path style in STAF on windows is kind of weird, when we query the registered services, it is very possible we have below result:

STAF local service list

Response
--------
Name      Library    Executable
--------- ---------- ----------------------------------
CRON      JSTAF      C:STAF/services/STAFCron.jar
DELAY     <Internal> <None>
DIAG      <Internal> <None>
ECHO      <Internal> <None>
FS        <Internal> <None>
FTP       JSTAF      C:STAF/services/ftp/STAFFTP.jar
HANDLE    <Internal> <None>
HELP      <Internal> <None>
HTTP      JSTAF      C:STAF/services/http/STAFHTTP.jar
LIFECYCLE <Internal> <None>
LOG       STAFLog    <None>
MISC      <Internal> <None>
PING      <Internal> <None>
PROCESS   <Internal> <None>
QUEUE     <Internal> <None>
SEM       <Internal> <None>
SERVICE   <Internal> <None>
SHUTDOWN  <Internal> <None>
STAX      JSTAF      C:STAF/services/stax/STAX.jar
TRACE     <Internal> <None>
TRUST     <Internal> <None>
VAR       <Internal> <None>

Unfortunately, Microsoft style is not work in STAX python code, I personally recommend to use Unix style all the time.

...
<script>
    f = open('c:/staf/services/custom/test/workfile.txt', 'r')
    content = f.readlines()
</script>
...

Don’t hard reboot Ubuntu installed by Wubi

My ubuntu behaves abnormally after a hard reboot this morning, after searching for solution in ubuntu community, I’m redirected to the WubiGuide:

Corrupted NTFS filesystem

All reported cases of damaged filesystems so far were from people that hard rebooted (pulling the plug).

When you hard reboot, you can always damage your filesystem whether you use wubi or not. What happens is that new users sometimes get stacked with wubi/ubuntu and since they do not know what to do they tend to hard-reboot more often than necessary. Sometimes they get lucky, sometimes they do not. Since wubi sits on top of ntfs of course when they do not get lucky, ntfs gets corrupted. Sometimes people blame Wubi for that even though a quick googling will show you that there are tons of people experiencing ntfs corruption without having ever used wubi or ntfs-3g (and a full software industry lurking on that…), most of them after a hard reboot…

If ntfs filesystem gets corrupted you have to run chkdsk /r from the windows recovery console on the Windows CD (or other recovery CD available on the web) or in the msdos console (if you can boot into Windows). At the moment there is no fsck for ntfs on the Linux side, otherwise it would be possible to fix errors automatically within Linux itself, as it happens for other filesystems, without having to rely on Windows tools.

Best advise is to simply avoid hard rebooting. Whatever the OS.

So, if you still insist to hard reboot your ubuntu from time to time, try to install it yourself.

UPDATE: I got no lucky in doing chkdsk in windows, but finally got ubuntu back to work, still with problems, by remounting the filesystem with write and read mode:

mount -n -o remount,rw /


Run command in STAX Service

First, I would like to thank Dave and Sharon, who helped me out from deep trouble. You guys are really kind and helpful!

Run a command with quote from STAX service is easier than from Process service, Sharon’s has explained it quite clear.

The equivalent element in a STAX job for a PROCESS START WAIT request is the <process> element, not the <stafcmd> element. The <stafcmd> element is used for STAF service requests other than a PROCESS START WAIT request. Using the <process> element will eliminate most of the quoting issues. Please see the STAX User’s Guide for detailed information on the <process> element and examples. Also, note that in Python, if you enclose a literal using a single quote, then it can contain double quotes without having to escape them (and vice versa).

   1: <process name="'ListSnapShot'"> 
   2:     <location>'VMHost'</location> 
   3:     <command>'vmrun listSnapshots "C:\VM\win2003_standard\Windows Server 2003 Standard Edition.vmx"' </command>
   4:     <stderr mode="'stdout'"/> 
   5:     <returnstdout/> 
   6: </process> 

 

Or

   1: <script> 
   2: vmxFileName = 'C:\VM\win2003_standard\Windows Server 2003 Standard Edition.vmx' 
   3: </script> 
   4:  
   5: <process name="'ListSnapShot'"> 
   6:     <location>'VMHost'</location> 
   7:     <command>'vmrun listSnapshots "%s"' % (vmxFileName)</command> 
   8:     <stderr mode="'stdout'"/> 
   9:     <returnstdout/> 
  10: </process> 

 

Notice: The #7 line in the latter example show the way of using one string parameter, for parameters more than one, should be like this:

<command>'vmrun start "%s" "%s"' % (vmfile,snapshot)</command>

When running STAX service with map-type argument, especially with quotes in one of the values, simply follow this format:

STAF local STAX execute file test.xml ARGS "{’key1′:’value1′, ‘key2’:’value need to be quoted’}" wait returnresult

Notice the outer quote is double-quote and inner quotes are single-quote.