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.
Functions include “copy”, “download”, “exec”, “extract”, “git”, “link”, “mkdir”, “move”, “remove”, “render”, “service”, “write”. They all share common usages and philosophies:
* Run actions both locally and remotely over SSH.
* Ability to see if an action had an effect through the second argument provided in the callback.
* Common API with options and callback arguments and calling the callback with an error and the number of affected actions.
* Run one or multiple actions depending on option argument being an object or an array of objects.
cp copy(options, callback)
Copy a file. The behavior is similar to the one of the cp
Unix utility. Copying a file over an existing file will
overwrite it.
options Command options include:
sourceThe file or directory to copy.destinationWhere the file or directory is copied.not_if_existsEquals destination if true.chmodPermissions of the file or the parent directorysshRun the action on a remote server using SSH, an ssh2 instance or an configuration object used to initialize the SSH connection.
callback Received parameters are:
errError object if any.copiedNumber of files or parent directories copied.
todo:
* preserve permissions if chmod is true
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 include:
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 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.code_skippedExpected code(s) returned by the command if it has no effect, executed will not be incremented, int or array of int.stdoutWritable EventEmitter in which command output will be piped.stderrWritable EventEmitter in which command error will be piped.sshRun the action on a remote server using SSH, an ssh2 instance or an configuration object used to initialize the SSH connection.
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 include:
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.sshRun the action on a remote server using SSH, an ssh2 instance or an configuration object used to initialize the SSH connection.
callback Received parameters are:
errError object if any.extractedNumber of extracted archives.
git
options Command options include:
sourceGit source repository address.destinationDirectory where to clone the repository.revisionGit revision, branch or tag.sshRun the action on a remote server using SSH, an ssh2 instance or an configuration object used to initialize the SSH connection.stdoutWritable EventEmitter in which command output will be piped.stderrWritable EventEmitter in which command error will be piped.
ln link(options, callback)
Create a symbolic link and it’s parent directories if they don’t yet exist.
options Command options include:
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 include:
sourcePath or array of paths.directoryAlias forsourcedestinationAlias 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 | |
mv move(options, callback)
More files and directories.
options Command options include:
sourceFile or directory to move.destinationFinal name of the moved resource.
callback Received parameters are:
errError object if any.movedNumber of moved resources.
Example
mecano.mv
1 2 3 4 | |
rm remove(options, callback)
Recursively remove files, directories and links. Internally, the function use the rimraf library.
options Command options include:
sourceFile, directory or pattern.destinationAlias for “source”.
callback Received parameters are:
errError object if any.removedNumber of removed 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 include:
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 or a callback.contextMap of key values to inject into the template.local_sourceTreat the source as local instead of remote, only apply with “ssh” option.
callback Received parameters are:
errError object if any.renderedNumber of rendered files.
If destination is a callback, it will be called multiple times with the
generated content as its first argument.
service(options, callback)
Install a service. For now, only yum over SSH.
options Command options include:
- name Package name.
- startup Run service daemon on startup.
- yum_name Name used by the yum utility, default to “name”.
- chk_name Name used by the chkconfig utility, default to “srv_name” and “name”.
- srv_name Name used by the service utility, default to “name”.
- start Ensure the service is started, a boolean.
- stop Ensure the service is stopped, a boolean.
callback Received parameters are:
errError object if any.modifiedNumber of action taken (installed, updated, started or stoped).upload(options, callback)
Upload a file to a remote location. Options are identical to the “write” function with the addition of the “binary” option.
options Command options include:
binaryFast upload implementation, discard all the other option and use its own stream based implementation.fromReplace from after this marker, a string or a regular expression.toReplace to before this marker, a string or a regular expression.matchReplace this marker, a string or a regular expression.replaceThe content to be inserted, used conjointly with the from, to or match options.contentText to be written.sourceFile path from where to extract the content, do not use conjointly with content.destinationFile path where to write content to.backupCreate a backup, append a provided string to the filename extension or a timestamp if value is not a string.
callback Received parameters are:
errError object if any.renderedNumber of rendered files.write(options, callback)
Write a file or a portion of an existing file.
options Command options include:
fromReplace from after this marker, a string or a regular expression.toReplace to before this marker, a string or a regular expression.matchReplace this marker, a string or a regular expression.replaceThe content to be inserted, used conjointly with the from, to or match options.contentText to be written, an alternative to source which reference a file.sourceFile path from where to extract the content, do not use conjointly with content.destinationFile path where to write content to.backupCreate a backup, append a provided string to the filename extension or a timestamp if value is not a string.appendAppend the content to the destination file if it exists.sshRun the action on a remote server using SSH, an ssh2 instance or an configuration object used to initialize the SSH connection.
callback Received parameters are:
errError object if any.renderedNumber of rendered files.
Example replacing part of a file using from and to markers
1 2 3 4 5 6 7 8 | |
Example replacing part of a file using a regular expression
connect host: ‘localhost’, (err, ssh) ->
1 2 3 4 5 6 7 | |
Example replacing with the global and multiple lines options
1 2 3 4 5 6 7 | |