1 | ---
|
2 | language: en
|
3 | layout: page
|
4 | title: "Node Mecano: Common functions for system deployment"
|
5 | date: 2012-11-29T13:21:58.642Z
|
6 | comments: false
|
7 | sharing: false
|
8 | footer: false
|
9 | github: https://github.com/wdavidw/node-mecano
|
10 | ---
|
11 | Mecano gather a set of functions usually used during system deployment. All the functions share a
|
12 | common API with flexible options.
|
13 |
|
14 | `cp` `copy(options, callback)`
|
15 | ------------------------------
|
16 |
|
17 | Copy a file.
|
18 |
|
19 | `options` Command options include:
|
20 |
|
21 | * `source` The file or directory to copy.
|
22 | * `destination` Where the file or directory is copied.
|
23 | * `force` Copy the file even if one already exists.
|
24 | * `not_if_exists` Equals destination if true.
|
25 | * `chmod` Permissions of the file or the parent directory
|
26 |
|
27 | `callback` Received parameters are:
|
28 |
|
29 | * `err` Error object if any.
|
30 | * `copied` Number of files or parent directories copied.
|
31 |
|
32 | todo:
|
33 | * deal with directories
|
34 | * preserve permissions if `chmod` is `true`
|
35 | * Compare files with checksum
|
36 |
|
37 | `download(options, callback)`
|
38 | -----------------------------
|
39 |
|
40 | Download files using various protocols. The excellent
|
41 | [open-uri](https://github.com/publicclass/open-uri) module provides support for HTTP(S),
|
42 | file and FTP. All the options supported by open-uri are passed to it.
|
43 |
|
44 | Note, GIT is not yet supported but documented as a wished feature.
|
45 |
|
46 | `options` Command options include:
|
47 |
|
48 | * `source` File, HTTP URL, FTP, GIT repository. File is the default protocol if source is provided without a scheme.
|
49 | * `destination` Path where the file is downloaded.
|
50 | * `force` Overwrite destination file if it exists.
|
51 |
|
52 | `callback` Received parameters are:
|
53 |
|
54 | * `err` Error object if any.
|
55 | * `downloaded` Number of downloaded files
|
56 |
|
57 | Basic example:
|
58 | ```coffeescript
|
59 | mecano.download
|
60 | source: 'https://github.com/wdavidw/node-sigar/tarball/v0.0.1'
|
61 | destination: 'node-sigar.tgz'
|
62 | , (err, downloaded) ->
|
63 | fs.exists 'node-sigar.tgz', (exists) ->
|
64 | assert.ok exists
|
65 | ```
|
66 | `exec` `execute([goptions], options, callback)`
|
67 | -----------------------------------------------
|
68 | Run a command locally or with ssh if the `host` is provided. Global options is
|
69 | optional and is used in case where options is defined as an array of
|
70 | multiple commands. Note, `opts` inherites all the properties of `goptions`.
|
71 |
|
72 | `goptions` Global options includes:
|
73 |
|
74 | * `parallel` Wether the command are run in sequential, parallel
|
75 | or limited concurrent mode. See the `node-each` documentation for more
|
76 | details. Default to sequential (false).
|
77 |
|
78 | `options` Include all conditions as well as:
|
79 |
|
80 | * `cmd` String, Object or array; Command to execute.
|
81 | * `env` Environment variables, default to `process.env`.
|
82 | * `cwd` Current working directory.
|
83 | * `uid` Unix user id.
|
84 | * `gid` Unix group id.
|
85 | * `code` Expected code(s) returned by the command, int or array of int, default to 0.
|
86 | * `host` SSH host or IP address.
|
87 | * `username` SSH host or IP address.
|
88 | * `stdout` Writable EventEmitter in which command output will be piped.
|
89 | * `stderr` Writable EventEmitter in which command error will be piped.
|
90 |
|
91 | `callback` Received parameters are:
|
92 |
|
93 | * `err` Error if any.
|
94 | * `executed` Number of executed commandes.
|
95 | * `stdout` Stdout value(s) unless `stdout` option is provided.
|
96 | * `stderr` Stderr value(s) unless `stderr` option is provided.
|
97 |
|
98 | `extract(options, callback)`
|
99 | ----------------------------
|
100 |
|
101 | Extract an archive. Multiple compression types are supported. Unless
|
102 | specified asan option, format is derived from the source extension. At the
|
103 | moment, supported extensions are '.tgz', '.tar.gz' and '.zip'.
|
104 |
|
105 | `options` Command options include:
|
106 |
|
107 | * `source` Archive to decompress.
|
108 | * `destination` Default to the source parent directory.
|
109 | * `format` One of 'tgz' or 'zip'.
|
110 | * `creates` Ensure the given file is created or an error is send in the callback.
|
111 | * `not_if_exists` Cancel extraction if file exists.
|
112 |
|
113 | `callback` Received parameters are:
|
114 |
|
115 | * `err` Error object if any.
|
116 | * `extracted` Number of extracted archives.
|
117 |
|
118 | `git`
|
119 | -----
|
120 |
|
121 | `options` Command options include:
|
122 |
|
123 | * `source` Git source repository address.
|
124 | * `destination` Directory where to clone the repository.
|
125 | * `revision` Git revision, branch or tag.
|
126 |
|
127 | `ln` `link(options, callback)`
|
128 | ------------------------------
|
129 | Create a symbolic link and it's parent directories if they don't yet
|
130 | exist.
|
131 |
|
132 | `options` Command options include:
|
133 |
|
134 | * `source` Referenced file to be linked.
|
135 | * `destination` Symbolic link to be created.
|
136 | * `exec` Create an executable file with an `exec` command.
|
137 | * `chmod` Default to 0755.
|
138 |
|
139 | `callback` Received parameters are:
|
140 |
|
141 | * `err` Error object if any.
|
142 | * `linked` Number of created links.
|
143 |
|
144 | `mkdir(options, callback)`
|
145 | --------------------------
|
146 |
|
147 | Recursively create a directory. The behavior is similar to the Unix command `mkdir -p`.
|
148 | It supports an alternative syntax where options is simply the path of the directory
|
149 | to create.
|
150 |
|
151 | `options` Command options include:
|
152 |
|
153 | * `source` Path or array of paths.
|
154 | * `directory` Shortcut for `source`
|
155 | * `exclude` Regular expression.
|
156 | * `chmod` Default to 0755.
|
157 | * `cwd` Current working directory for relative paths.
|
158 |
|
159 | `callback` Received parameters are:
|
160 |
|
161 | * `err` Error object if any.
|
162 | * `created` Number of created directories
|
163 |
|
164 | Simple usage:
|
165 | ```coffeescript
|
166 |
|
167 | mecano.mkdir './some/dir', (err, created) ->
|
168 | console.log err?.message ? created
|
169 | ```
|
170 | `rm` `remove(options, callback)`
|
171 | --------------------------------
|
172 |
|
173 | Recursively remove a file or directory. Internally, the function
|
174 | use the [rimraf](https://github.com/isaacs/rimraf) library.
|
175 |
|
176 | `options` Command options include:
|
177 |
|
178 | * `source` File or directory.
|
179 |
|
180 | `callback` Received parameters are:
|
181 |
|
182 | * `err` Error object if any.
|
183 | * `deleted` Number of deleted sources.
|
184 |
|
185 | Example
|
186 | ```coffeescript
|
187 |
|
188 | mecano.rm './some/dir', (err, removed) ->
|
189 | console.log "#{removed} dir removed"
|
190 |
|
191 | ```Removing a directory unless a given file exists
|
192 | ```coffeescript
|
193 |
|
194 | mecano.rm
|
195 | source: './some/dir'
|
196 | not_if_exists: './some/file'
|
197 | , (err, removed) ->
|
198 | console.log "#{removed} dir removed"
|
199 |
|
200 | ```Removing multiple files and directories
|
201 | ```coffeescript
|
202 |
|
203 | mecano.rm [
|
204 | { source: './some/dir', not_if_exists: './some/file' }
|
205 | './some/file'
|
206 | ], (err, removed) ->
|
207 | console.log "#{removed} dirs removed"
|
208 | ```
|
209 | `render(options, callback)`
|
210 | ---------------------------
|
211 |
|
212 | Render a template file At the moment, only the
|
213 | [ECO](http://github.com/sstephenson/eco) templating engine is integrated.
|
214 |
|
215 | `options` Command options include:
|
216 |
|
217 | * `engine` Template engine to use, default to "eco"
|
218 | * `content` Templated content, bypassed if source is provided.
|
219 | * `source` File path where to extract content from.
|
220 | * `destination` File path where to write content to.
|
221 | * `context` Map of key values to inject into the template.
|
222 |
|
223 | `callback` Received parameters are:
|
224 |
|
225 | * `err` Error object if any.
|
226 | * `rendered` Number of rendered files.
|