This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
/
local-resin-os-device.js
124 lines (108 loc) · 4.85 KB
/
local-resin-os-device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Generated by CoffeeScript 1.11.1
/*
Copyright 2016 Resin.io
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var DEVICE_SSH_PORT, Promise, RdtDockerUtils, SpinnerPromise, buildRsyncCommand, path, ref, shell, shellwords, startContainerAfterErrorSpinner, startContainerSpinner, stopContainerSpinner;
path = require('path');
Promise = require('bluebird');
shellwords = require('shellwords');
shell = require('../shell');
RdtDockerUtils = require('../docker-utils');
SpinnerPromise = require('resin-cli-visuals').SpinnerPromise;
buildRsyncCommand = require('../rsync').buildRsyncCommand;
ref = require('../utils'), startContainerSpinner = ref.startContainerSpinner, stopContainerSpinner = ref.stopContainerSpinner, startContainerAfterErrorSpinner = ref.startContainerAfterErrorSpinner;
DEVICE_SSH_PORT = 22222;
/**
* @summary Run rsync on a local resinOS device
* @function sync
*
* @param {Object} options - options
* @param {String} options.deviceIp - Destination device ip/host
* @param {String} options.baseDir - Project base dir
* @param {String} options.appName - Application container name
* @param {String} options.destination - Sync destination folder in container
* @param {String} [options.before] - Action to execute locally before sync
* @param {String} [options.after] - Action to execute locally after sync
* @param {String} [options.progress=false] - Show progress
* @param {String} [options.verbose=false] - Show progress
* @param {String} [options.skipGitignore=false] - Skip .gitignore parsing
* @param {String} [options.ignore] - rsync ignore list
*
* @returns {}
* @throws Exception on error
*
* @example
* sync()
*/
exports.sync = function(arg) {
var after, appName, baseDir, before, destination, deviceIp, docker, ignore, progress, ref1, ref2, ref3, ref4, skipGitignore, verbose;
ref1 = arg != null ? arg : {}, deviceIp = ref1.deviceIp, baseDir = ref1.baseDir, appName = ref1.appName, destination = ref1.destination, before = ref1.before, after = ref1.after, progress = (ref2 = ref1.progress) != null ? ref2 : false, verbose = (ref3 = ref1.verbose) != null ? ref3 : false, skipGitignore = (ref4 = ref1.skipGitignore) != null ? ref4 : false, ignore = ref1.ignore;
if (destination == null) {
throw new Error("'destination' is a required sync option");
}
if (deviceIp == null) {
throw new Error("'deviceIp' is a required sync option");
}
if (appName == null) {
throw new Error("'app-name' is a required sync option");
}
docker = new RdtDockerUtils(deviceIp);
return Promise["try"](function() {
if (before != null) {
return shell.runCommand(before, baseDir);
}
}).then(function() {
return docker.containerRootDir(appName, deviceIp, DEVICE_SSH_PORT).then(function(containerRootDirLocation) {
var command, rsyncDestination, syncOptions;
rsyncDestination = path.join(containerRootDirLocation, destination);
syncOptions = {
username: 'root',
host: deviceIp,
port: DEVICE_SSH_PORT,
progress: progress,
ignore: ignore,
skipGitignore: skipGitignore,
verbose: verbose,
source: baseDir,
destination: shellwords.escape(rsyncDestination),
rsyncPath: "mkdir -p \"" + rsyncDestination + "\" && nsenter --target $(pidof docker) --mount rsync"
};
command = buildRsyncCommand(syncOptions);
return docker.checkForRunningContainer(appName).then(function(isContainerRunning) {
if (!isContainerRunning) {
throw new Error("Container must be running before attempting 'sync' action");
}
return new SpinnerPromise({
promise: shell.runCommand(command, {
cwd: baseDir
}),
startMessage: "Syncing to " + destination + " on '" + appName + "'...",
stopMessage: "Synced " + destination + " on '" + appName + "'."
});
});
}).then(function() {
return stopContainerSpinner(docker.stopContainer(appName));
}).then(function() {
return startContainerSpinner(docker.startContainer(appName));
}).then(function() {
if (after != null) {
return shell.runCommand(after, baseDir);
}
})["catch"](function(err) {
return startContainerAfterErrorSpinner(docker.startContainer(appName))["catch"](function(err) {
return console.log('Could not start application container', err);
})["finally"](function() {
throw err;
});
});
});
};