Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some sort of base, to get list of filesystems and properties.

  • Loading branch information...
commit fc0ba7dc03e16c1fc541db7ed62f3a4dced605f0 0 parents
@calmh authored
Showing with 222 additions and 0 deletions.
  1. +102 −0 lib/zfs.js
  2. +31 −0 package.json
  3. +19 −0 test/zfs
  4. +70 −0 test/zfs.js
102 lib/zfs.js
@@ -0,0 +1,102 @@
+var _ = require('underscore');
+var spawn = require('child_process').spawn;
+
+exports.unittest = Object.create({});
+
+function ZFS(info) {
+ "use strict";
+
+ var obj = Object.create({});
+ if (typeof info === 'string') {
+ info = info.split(/\s+/);
+ }
+
+ if (info.length !== 5) {
+ return null;
+ }
+
+ obj.name = info[0];
+ obj.used = parseInt(info[1], 10);
+ obj.avail = parseInt(info[2], 10);
+ obj.refer = parseInt(info[3], 10);
+ obj.mountpoint = info[4];
+
+ Object.freeze(obj);
+ return obj
+}
+
+function Property(info) {
+ "use strict";
+
+ var obj = Object.create({});
+ if (typeof info === 'string') {
+ info = info.split(/\s+/, 4);
+ }
+
+ if (info.length !== 4) {
+ return null;
+ }
+
+ obj.name = info[0];
+ obj.property = info[1];
+ obj.value = info[2];
+ obj.source = info[3];
+
+ Object.freeze(obj);
+ return obj
+}
+
+exports.unittest.getZfsList = getZfsList;
+function getZfsList(data) {
+ "use strict";
+
+ var lines = _.compact(data.split('\n'))
+ return lines.map(function (x) { return new ZFS(x); });
+}
+
+exports.unittest.getZfsGet = getZfsGet;
+function getZfsGet(data) {
+ "use strict";
+
+ var lines = _.compact(data.split('\n'))
+ return lines.map(function (x) { return new Property(x); });
+}
+
+exports.list = list;
+function list(cb) {
+ "use strict";
+
+ var buf = '';
+ var cp = spawn('zfs', ['list', '-pH']);
+
+ cp.stdout.on('data', function (data) {
+ buf += data;
+ });
+ cp.on('exit', function (code) {
+ var list = getZfsList(buf);
+ cb(null, list);
+ });
+}
+
+exports.get = get;
+function get(opts, cb) {
+ "use strict";
+
+ var params = [ 'get', '-pH' ];
+ if (opts.source) {
+ params.push('-s');
+ params.push(opts.source);
+ }
+ params.push(opts.property);
+
+ var buf = '';
+ var cp = spawn('zfs', params);
+
+ cp.stdout.on('data', function (data) {
+ buf += data;
+ });
+ cp.on('exit', function (code) {
+ console.log(getZfsGet(buf));
+ });
+}
+
31 package.json
@@ -0,0 +1,31 @@
+{
+ "name": "zfs",
+ "description": "A Node.js wrapper around zfs/zpool",
+ "author": "Jakob Borg <jakob@nym.se> (http://nym.se/)",
+ "keywords": ["solaris", "zfs", "illumos", "openindiana", "smartos"],
+ "homepage": "http://nym.se/node-zfs/docs/",
+ "version": "0.0.1",
+ "main" : "lib/zfs.js",
+ "scripts": {
+ "test": "NODE_PATH=lib PATH=test:$PATH node_modules/.bin/mocha -R spec",
+ "hint": "jshint *.js lib/*.js",
+ "doc": "docco lib/* example.js 2>/dev/null",
+ "cov": "jscoverage lib lib-cov && EXPRESS_COV=1 NODE_PATH=lib-cov mocha -R html-cov > docs/coverage.html"
+ },
+ "dependencies": {
+ "underscore": "1.3.x"
+ },
+ "devDependencies": {
+ "jshint": "*",
+ "mocha": "*",
+ "should": "*"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/calmh/node-zfs.git"
+ },
+ "bugs": {
+ "url": "http://github.com/calmh/node-zfs/issues",
+ "email": "node-zfs@nym.se"
+ }
+}
19 test/zfs
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+case $1 in
+ list)
+ cat <<EOD
+zones 1004799731712 956926580736 328704 /zones
+zones/f78f9208-9c26-47f7-9e03-881a96d17c04 590843904 10146574336 694054400 /zones/f78f9208-9c26-47f7-9e03-881a96d17c04
+zones/f78f9208-9c26-47f7-9e03-881a96d17c04/data 73728 10146574336 31744 /data
+EOD
+ ;;
+ get)
+ cat <<EOD
+zones compression on local
+zones/f78f9208-9c26-47f7-9e03-881a96d17c04 compression on inherited from zones
+zones/f78f9208-9c26-47f7-9e03-881a96d17c04/data compression on inherited from zones
+zones/f78f9208-9c26-47f7-9e03-881a96d17c04/data@daily-20120430 compression - -
+EOD
+ ;;
+esac
70 test/zfs.js
@@ -0,0 +1,70 @@
+var assert = require('assert');
+var should = require('should');
+var zfs = require('zfs');
+
+describe('zfs', function () {
+ describe('list', function () {
+ it('returns a list of ZFS objects', function (done) {
+ var expected = [ {
+ name: 'zones',
+ used: 1004799731712,
+ avail: 956926580736,
+ refer: 328704,
+ mountpoint: '/zones'
+ },
+ {
+ name: 'zones/f78f9208-9c26-47f7-9e03-881a96d17c04',
+ used: 590843904,
+ avail: 10146574336,
+ refer: 694054400,
+ mountpoint: '/zones/f78f9208-9c26-47f7-9e03-881a96d17c04'
+ },
+ {
+ name: 'zones/f78f9208-9c26-47f7-9e03-881a96d17c04/data',
+ used: 73728,
+ avail: 10146574336,
+ refer: 31744,
+ mountpoint: '/data'
+ } ];
+ zfs.list(function (err, list) {
+ should.not.exist(err);
+ list.should.eql(expected);
+ done();
+ });
+ });
+ });
+ describe('get', function (done) {
+ it('returns a list of property', function () {
+ var expected = [ {
+ name: 'zones',
+ property: 'compression',
+ value: 'on',
+ source: 'local'
+ },
+ {
+ name: 'zones/f78f9208-9c26-47f7-9e03-881a96d17c04',
+ property: 'compression',
+ value: 'on',
+ source: 'inherited'
+ },
+ {
+ name: 'zones/f78f9208-9c26-47f7-9e03-881a96d17c04/data',
+ property: 'compression',
+ value: 'on',
+ source: 'inherited'
+ },
+ {
+ name: 'zones/f78f9208-9c26-47f7-9e03-881a96d17c04/data@daily-20120430',
+ property: 'compression',
+ value: '-',
+ source: '-'
+ } ];
+ zfs.get({}, function (err, list) {
+ should.not.exist(err);
+ list.should.eql(expected);
+ done();
+ });
+ });
+ });
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.