Wednesday 31 May 2017

Running scripts against the Oracle ZFS appliance

So you want to run scripts against the ZFS appliance and you've read through the Oracle document titled "Effectively Managing Oracle ZFS Storage Appliances with Scripting" and realised it's not as straightforward as it should be.

I mean I use similar ZFS commands on the OS all the time, why not just have a terminal interface that uses the same commands? Nope - Oracle don't play that.

First thing you have to do set up key authentication from where you want to write the scripts. Simply done, just log onto the ZFS appliance and go into the shell by running "shell" and set it up the way you would normally between UNIX servers.

Once that works, here is a script to snapshot a filesystem:

user=root
appliance=zfsappliance.name.here
pool=poolname
project=projectname
snapshot=snapname

ssh $user@$appliance 2>/dev/null << EOF
script
var MyArguments = {
  pool:         '$pool',
  project:      '$project',
  snapshot:     '$snapshot',
}
var MyErrors = {
  PoolNotFound: '$PoolNotFound',
  ProjectNotFound: '$ProjectNotFound',
  UnKnownError: '$UnknownError',
}
function CreateSnapshot (Arg) {
  run('cd /'); // Make sure we are at root child context level
  run('shares');
  try {
    run('set pool=' + Arg.pool);
  } catch (err) {
    printf("ERROR Specified pool %s not found\n ",Arg.pool);
    return;
  }
  try {
    run('select ' + Arg.project);
  } catch (err) {
      printf("ERROR Specified project %s not found\n ",Arg.project);
      return;
  }
  try {
    run('snapshots');
  } catch (err) {
      printf("ERROR Snapshot %s not found\n ",Arg.project);
      return;
  }
  try {
    run('snapshot ' + Arg.snapshot);
  } catch(err) {
    printf("ERROR Unable snapshot %s\n ",Arg.project);
    return;
  }
  return;
}
CreateSnapshot(MyArguments);
.
EOF

As you can see, most steps of the Javascript is checked to see if it worked. It's good practice because Javascript will give you zero output. Zero.

Zero output is especially irritating when the main reason for the script is to retrieve some output e.g. when you want to get a list of snapshots.

So here's an example (to list snapshots) where the output is returned:

user=root
appliance=zfsappliance.name.here 
pool=poolname
project=projectname

ssh $user@$appliance 2>/dev/null << EOF script var MyArguments = {

  pool:         '$pool',
  project:      '$project'
}
function ListFS (Arg) {
  run('cd /');
  run('shares');
  run('set pool=' + Arg.pool);
  run('select ' + Arg.project);
  fs=list();
  for (j=0; j<fs.length; j++)
  {
    printf(fs[j]);
    printf("\n");
  }
}
ListFS(MyArguments);
.
EOF

I've left the error checking out of this one for a leaner script. As a side note, remember for some commands (like deletions) you'll need the "confirm" command as part of it.

Anyway, that should be enough to get you started. Happy scripting.