Node Mecano: Common functions for system deployment
Mecano gather a set of functions usually used during system deployment. All the functions share a common API with flexible options.
cp copy(options, callback) Copy a file
options Command options includes:
sourceThe file or directory to copy.destinationWhere the file or directory is copied.forceCopy the file even if one already exists.not_if_existsEquals destination if true.chmodPermissions of the file or the parent directory
callback Received parameters are:
errError object if any.copiedNumber of files or parent directories copied.
todo:
* deal with directories
* preserve permissions if chmod is true
* Compare files with checksum
download(options, callback) Download files using various protocols
The excellent open-uri module provides support for HTTP(S), file and FTP. All the options supported by open-uri are passed to it.
Note, GIT is not yet supported but documented as a wished feature.
options Command options includes:
sourceFile, HTTP URL, FTP, GIT repository. File is the default protocol if source is provided without a scheme.destinationPath where the file is downloaded.forceOverwrite destination file if it exists.
callback Received parameters are:
errError object if any.downloadedNumber of downloaded files
Basic example:
1 2 3 4 5 6 | |
exec execute([goptions], options, callback)` Run a command locally or with ssh
Command is send over ssh if the host is provided. Global options is
optional and is used in case where options is defined as an array of
multiple commands. Note, opts inherites all the properties of goptions.
goptions Global options includes:
parallelWether the command are run in sequential, parallel or limited concurrent mode. See thenode-eachdocumentation for more details. Default to sequential (false).
options Include all conditions as well as:
cmdString, Object or array; Command to execute.envEnvironment variables, default toprocess.env.cwdCurrent working directory.uidUnix user id.gidUnix group id.codeExpected code(s) returned by the command, int or array of int, default to 0.hostSSH host or IP address.usernameSSH host or IP address.stdoutWritable EventEmitter in which command output will be piped.stderrWritable EventEmitter in which command error will be piped.
callback Received parameters are:
errError if any.executedNumber of executed commandes.stdoutStdout value(s) unlessstdoutoption is provided.stderrStderr value(s) unlessstderroption is provided.
extract(options, callback) Extract an archive
Multiple compression types are supported. Unless specified as an option, format is derived from the source extension. At the moment, supported extensions are ‘.tgz’, ‘.tar.gz’ and ‘.zip’.
options Command options includes:
sourceArchive to decompress.destinationDefault to the source parent directory.formatOne of ‘tgz’ or ‘zip’.createsEnsure the given file is created or an error is send in the callback.not_if_existsCancel extraction if file exists.
callback Received parameters are:
errError object if any.extractedNumber of extracted archives.
git
options Command options includes:
sourceGit source repository address.destinationDirectory where to clone the repository.revisionGit revision, branch or tag.
ln link(options, callback) Create a symbolic link
options Command options includes:
sourceReferenced file to be linked.destinationSymbolic link to be created.execCreate an executable file with anexeccommand.chmodDefault to 0755.
callback Received parameters are:
errError object if any.linkedNumber of created links.
mkdir(options, callback) Recursively create a directory
The behavior is similar to the Unix command mkdir -p. It supports
an alternative syntax where options is simply the path of the directory
to create.
options Command options includes:
sourcePath or array of paths.directoryShortcut forsourceexcludeRegular expression.chmodDefault to 0755.cwdCurrent working directory for relative paths.
callback Received parameters are:
errError object if any.createdNumber of created directories
Simple usage:
1 2 | |
rm remove(options, callback) Recursively remove a file or directory
Internally, the function use the rimraf library.
options Command options includes:
sourceFile or directory.
callback Received parameters are:
errError object if any.deletedNumber of deleted sources.
Example
1 2 | |
Removing a directory unless a given file exists
1 2 3 4 5 | |
Removing multiple files and directories
1 2 3 4 5 | |
render(options, callback) Render a template file
At the moment, only the ECO templating engine is integrated.
options Command options includes:
engineTemplate engine to use, default to “eco”contentTemplated content, bypassed if source is provided.sourceFile path where to extract content from.destinationFile path where to write content to.contextMap of key values to inject into the template.
callback Received parameters are:
errError object if any.renderedNumber of rendered files.