项目作者: edin-m

项目描述 :
Node js wrapper for vagrant CLI - command line tool.
高级语言: JavaScript
项目地址: git://github.com/edin-m/node-vagrant.git
创建时间: 2015-02-21T11:41:21Z
项目社区:https://github.com/edin-m/node-vagrant

开源协议:ISC License

下载


node-vagrant

Node js wrapper for vagrant CLI - command line tool.

This is light wrapper around vagrant CLI.
It uses spawn process, and every command requiring user input
such as init and destroy is created with switch —force or -f.

npm version
Build Status

Installation

  1. $ npm install node-vagrant --save

Usage

All callbacks are node style:

  1. function(err, out)

where err is stderr if exit code != 0 and out is stdout if exit code == 0


Other commands

  1. // import vagrant
  2. var vagrant = require('node-vagrant');
  3. // view version
  4. vagrant.version(function(err, out) {});
  5. // or --version ; out = { status: '2.0.3', major: 2, minor: 0, patch: 3 }
  6. vagrant.versionStatus(function(err, out) {});
  7. // view global status
  8. // you can specify '--prune' as additional argument. By default global-status is based on a cache,
  9. // prune removes invalid entries from the list.
  10. // Note that this is much more time consuming than simply listing the entries.
  11. vagrant.globalStatus(function(err, out) {});
  12. vagrant.globalStatus('--prune', function(err, out) {});
  13. // vagrant machine
  14. // create machine - does not run command or init machine
  15. // you can specify directory where Vagrantfile will be located
  16. // and machine instanced
  17. var machine = vagrant.create({ cwd: <String>, env: <Object> }) // cwd and env default to process' values
  18. // init machine
  19. // you can specify additional arguments by using array (applicable to other functions)
  20. machine.init('ubuntu/trusty64', function(err, out) {});
  21. machine.init(['ubuntu/trusty64'], function(err, out) {});
  22. // -f is set by default
  23. machine.init(['ubuntu/trusty64', '-f'], function(err, out) {});
  24. // up
  25. machine.up(function(err, out) {})
  26. // status
  27. machine.status(function(err, out) {});
  28. // get ssh config - useful to retrieve private and connect to machine with ssh2
  29. // out is an array of objects [{}] with properties: port, hostname, user, private_key
  30. machine.sshConfig(function(err, out) {});
  31. // execute an ssh command on the machine
  32. machine.on('ssh-out', console.log);
  33. machine.on('ssh-err', console.error);
  34. machine.sshCommand('echo "a bash command"');
  35. // provision
  36. machine.provision(function(err, out) {});
  37. // suspend
  38. machine.suspend(function(err, out) {});
  39. // resume
  40. machine.resume(function(err, out) {});
  41. // reload
  42. machine.reload(function(err, out) {});
  43. // halt
  44. machine.halt(function(err, out) {});
  45. // destroy
  46. // uses -f by default
  47. machine.destroy(function(err, out) {});
  48. // snapshots
  49. // push, pop, save, delete, restore, list and a snapshot() function.
  50. // example:
  51. machine.snapshots().push(cb);
  52. // box repackage
  53. // must be specific to a vagrant environment hence location in machine
  54. machine.boxRepackage(name, provider, version, function(err, out) {})
  55. // plugins
  56. // expunge, install, uninstall, repair, update, list and a plugin() function.
  57. // example:
  58. machine.plugin().expunge(args, cb);
  59. // DEPRECATED! For backward compatibility only
  60. machine.pluginUpdate(function(err, out) {});
  61. machine.pluginRepair(function(err, out) {});
  62. // boxes
  63. // box add
  64. // uses -f by default
  65. // depending on type of box provided (name,address,file,json) missing information may be prompted.
  66. // please ensure that your add metheod is specific.
  67. vagrant.boxAdd(box, args, function(err, out) {})
  68. .on('progress', function(out) {});
  69. // box list
  70. // out is an array of objects [{}] with properties: name, provider, version
  71. vagrant.boxList(args, function(err, out) {});
  72. // box outdated
  73. // --global is used by default
  74. // out is an array of objects [{}] with properties: name, status, currentVersion, latestVersion
  75. // status can be 'up to date' 'out of date' 'unknown'
  76. // if status is unknown currentVersion and latestVersion will be null
  77. vagrant.boxOutdated(args, function(err, out) {});
  78. // box prune
  79. // uses -f by defaultprune
  80. vagrant.boxPrune(args, function(err, out) {});
  81. // box remove
  82. // uses -f by default
  83. vagrant.boxRemove(name, args, function(err, out) {});
  84. // box repackage
  85. // avalible in machine
  86. // box update
  87. // uses the --box and --provider flags by default
  88. // provider can be null and in that case no --provider arg is added
  89. vagrant.boxUpdate(box, provider, function(err, out) {});
  90. .on('progress', function(out) {});
  91. // args
  92. // should be array of args or a string for single flag see --prune abov
  93. // ie
  94. vagrant.boxAdd('ubuntu/trusty64', ['--clean', '--provider', 'virtualbox'], function(err, out) {})
  95. //or simply
  96. vagrant.boxAdd('ubuntu/trusty64', '--clean', function(err, out) {})

Events

  1. .on('up-progress', function(out) {}); // receive stdout progress from up of vagrant
  2. .on('progress', function(out) {}); // receive stdout box download progress

Receive any stdout/stderr output from a child subprocess. These work only on a Machine instance:

  1. machine.on('stdout', function(data) {}); // data is a Buffer
  2. machine.on('stderr', function(data) {}); // data is a Buffer

Example

Example script of a usage is in example/example.js

  1. $ npm run example

Flags & env vars

Debug the commands sent to vagrant:

  1. $ NODE_DEBUG=1 node example.js
  2. node-vagrant command: [ 'global-status' ]
  3. node-vagrant command: [ 'version' ]

Disable the debug:

  1. $ NODE_DEBUG=1 NODE_VAGRANT_DISABLE_DEBUG=1 node example.js

Custom vagrant location:

  1. $ VAGRANT_DIR=/custom/path node example.js

Promises

  1. var vagrant = require('../index');
  2. vagrant.promisify();
  3. vagrant.init('ubuntu/trusty64').then(successCb, errorCb);

TODO

  • multi-machine
  • more detail vagrant file settings
    • firewall
    • networking
  • boxing
  • provisoning
  • providers
  • (native) promises (if available)
  • use ES6 (after which will become version 2.x.x)