2017-08-04 12:53:31 +00:00
|
|
|
###
|
|
|
|
Copyright 2016-2017 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
dockerUtils = require('../utils/docker')
|
|
|
|
|
|
|
|
LATEST = 'latest'
|
|
|
|
|
2017-08-22 13:33:03 +00:00
|
|
|
getApplicationsWithSuccessfulBuilds = (deviceType) ->
|
2017-08-24 10:39:17 +00:00
|
|
|
preload = require('resin-preload')
|
2018-01-30 17:31:49 +00:00
|
|
|
resin = require('resin-sdk').fromSharedOptions()
|
2017-08-22 13:33:03 +00:00
|
|
|
|
2017-08-04 12:53:31 +00:00
|
|
|
resin.pine.get
|
|
|
|
resource: 'my_application'
|
|
|
|
options:
|
|
|
|
filter:
|
|
|
|
device_type: deviceType
|
2018-01-30 17:31:49 +00:00
|
|
|
owns__release:
|
2017-08-04 12:53:31 +00:00
|
|
|
$any:
|
2018-01-30 17:31:49 +00:00
|
|
|
$alias: 'r'
|
2017-08-04 12:53:31 +00:00
|
|
|
$expr:
|
2018-01-30 17:31:49 +00:00
|
|
|
r:
|
2017-08-04 12:53:31 +00:00
|
|
|
status: 'success'
|
2017-08-24 10:39:17 +00:00
|
|
|
expand: preload.applicationExpandOptions
|
2018-01-30 17:31:49 +00:00
|
|
|
select: [ 'id', 'app_name', 'device_type', 'commit', 'should_track_latest_release' ]
|
2017-08-04 12:53:31 +00:00
|
|
|
orderby: 'app_name asc'
|
|
|
|
|
2017-08-22 13:33:03 +00:00
|
|
|
selectApplication = (deviceType) ->
|
|
|
|
visuals = require('resin-cli-visuals')
|
|
|
|
form = require('resin-cli-form')
|
|
|
|
{ expectedError } = require('../utils/patterns')
|
|
|
|
|
2018-01-30 17:31:49 +00:00
|
|
|
applicationInfoSpinner = new visuals.Spinner('Downloading list of applications and releases.')
|
2017-08-22 13:33:03 +00:00
|
|
|
applicationInfoSpinner.start()
|
|
|
|
|
|
|
|
getApplicationsWithSuccessfulBuilds(deviceType)
|
2017-08-04 12:53:31 +00:00
|
|
|
.then (applications) ->
|
2017-08-22 13:33:03 +00:00
|
|
|
applicationInfoSpinner.stop()
|
2017-08-04 12:53:31 +00:00
|
|
|
if applications.length == 0
|
2018-01-30 17:31:49 +00:00
|
|
|
expectedError("You have no apps with successful releases for a '#{deviceType}' device type.")
|
2017-08-04 12:53:31 +00:00
|
|
|
form.ask
|
|
|
|
message: 'Select an application'
|
|
|
|
type: 'list'
|
|
|
|
choices: applications.map (app) ->
|
|
|
|
name: app.app_name
|
|
|
|
value: app
|
|
|
|
|
2018-01-30 17:31:49 +00:00
|
|
|
selectApplicationCommit = (releases) ->
|
2017-08-22 13:33:03 +00:00
|
|
|
form = require('resin-cli-form')
|
|
|
|
{ expectedError } = require('../utils/patterns')
|
|
|
|
|
2018-01-30 17:31:49 +00:00
|
|
|
if releases.length == 0
|
|
|
|
expectedError('This application has no successful releases.')
|
2018-01-04 14:07:55 +00:00
|
|
|
DEFAULT_CHOICE = { 'name': LATEST, 'value': LATEST }
|
2018-01-30 17:31:49 +00:00
|
|
|
choices = [ DEFAULT_CHOICE ].concat releases.map (release) ->
|
|
|
|
name: "#{release.end_timestamp} - #{release.commit}"
|
|
|
|
value: release.commit
|
2017-08-04 12:53:31 +00:00
|
|
|
return form.ask
|
2018-01-30 17:31:49 +00:00
|
|
|
message: 'Select a release'
|
2017-08-04 12:53:31 +00:00
|
|
|
type: 'list'
|
|
|
|
default: LATEST
|
|
|
|
choices: choices
|
|
|
|
|
2017-08-22 13:33:03 +00:00
|
|
|
offerToDisableAutomaticUpdates = (application, commit) ->
|
|
|
|
Promise = require('bluebird')
|
2018-01-30 17:31:49 +00:00
|
|
|
resin = require('resin-sdk').fromSharedOptions()
|
2017-08-22 13:33:03 +00:00
|
|
|
form = require('resin-cli-form')
|
|
|
|
|
2017-08-04 12:53:31 +00:00
|
|
|
if commit == LATEST or not application.should_track_latest_release
|
|
|
|
return Promise.resolve()
|
|
|
|
message = '''
|
|
|
|
|
|
|
|
This application is set to automatically update all devices to the latest available version.
|
|
|
|
This might be unexpected behaviour: with this enabled, the preloaded device will still
|
2018-01-30 17:31:49 +00:00
|
|
|
download and install the latest release once it is online.
|
2017-08-04 12:53:31 +00:00
|
|
|
|
|
|
|
Do you want to disable automatic updates for this application?
|
|
|
|
'''
|
|
|
|
form.ask
|
|
|
|
message: message,
|
|
|
|
type: 'confirm'
|
|
|
|
.then (update) ->
|
|
|
|
if not update
|
|
|
|
return
|
|
|
|
resin.pine.patch
|
|
|
|
resource: 'application'
|
|
|
|
id: application.id
|
|
|
|
body:
|
|
|
|
should_track_latest_release: false
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
signature: 'preload <image>'
|
2017-10-06 09:05:24 +00:00
|
|
|
description: '(beta) preload an app on a disk image (or Edison zip archive)'
|
2017-08-04 12:53:31 +00:00
|
|
|
help: '''
|
|
|
|
Warning: "resin preload" requires Docker to be correctly installed in
|
|
|
|
your shell environment. For more information (including Windows support)
|
|
|
|
please check the README here: https://github.com/resin-io/resin-cli .
|
|
|
|
|
2017-10-06 09:05:24 +00:00
|
|
|
Use this command to preload an application to a local disk image (or
|
2018-01-30 17:31:49 +00:00
|
|
|
Edison zip archive) with a built release from Resin.io.
|
2017-08-04 12:53:31 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
$ resin preload resin.img --app 1234 --commit e1f2592fc6ee949e68756d4f4a48e49bff8d72a0 --splash-image some-image.png
|
|
|
|
$ resin preload resin.img
|
|
|
|
'''
|
|
|
|
permission: 'user'
|
|
|
|
primary: true
|
|
|
|
options: dockerUtils.appendConnectionOptions [
|
|
|
|
{
|
|
|
|
signature: 'app'
|
|
|
|
parameter: 'appId'
|
|
|
|
description: 'id of the application to preload'
|
|
|
|
alias: 'a'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'commit'
|
|
|
|
parameter: 'hash'
|
2017-10-25 09:54:03 +00:00
|
|
|
description: '''
|
2018-01-30 17:31:49 +00:00
|
|
|
the commit hash for a specific application release to preload, use "latest" to specify the latest release
|
2017-10-25 09:54:03 +00:00
|
|
|
(ignored if no appId is given)
|
|
|
|
'''
|
2017-08-04 12:53:31 +00:00
|
|
|
alias: 'c'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
signature: 'splash-image'
|
|
|
|
parameter: 'splashImage.png'
|
|
|
|
description: 'path to a png image to replace the splash screen'
|
|
|
|
alias: 's'
|
|
|
|
}
|
2017-08-25 10:27:38 +00:00
|
|
|
{
|
|
|
|
signature: 'dont-check-device-type'
|
|
|
|
boolean: true
|
|
|
|
description: 'Disables check for matching device types in image and application'
|
|
|
|
}
|
2017-08-04 12:53:31 +00:00
|
|
|
]
|
|
|
|
action: (params, options, done) ->
|
|
|
|
_ = require('lodash')
|
|
|
|
Promise = require('bluebird')
|
2018-01-30 17:31:49 +00:00
|
|
|
resin = require('resin-sdk').fromSharedOptions()
|
2017-08-04 12:53:31 +00:00
|
|
|
preload = require('resin-preload')
|
2017-08-22 13:33:03 +00:00
|
|
|
visuals = require('resin-cli-visuals')
|
2017-10-06 09:05:24 +00:00
|
|
|
nodeCleanup = require('node-cleanup')
|
2017-08-04 12:53:31 +00:00
|
|
|
{ expectedError } = require('../utils/patterns')
|
|
|
|
|
2017-10-06 09:05:24 +00:00
|
|
|
progressBars = {}
|
|
|
|
|
|
|
|
progressHandler = (event) ->
|
|
|
|
progressBar = progressBars[event.name]
|
|
|
|
if not progressBar
|
|
|
|
progressBar = progressBars[event.name] = new visuals.Progress(event.name)
|
|
|
|
progressBar.update(percentage: event.percentage)
|
|
|
|
|
|
|
|
spinners = {}
|
|
|
|
|
|
|
|
spinnerHandler = (event) ->
|
|
|
|
spinner = spinners[event.name]
|
|
|
|
if not spinner
|
|
|
|
spinner = spinners[event.name] = new visuals.Spinner(event.name)
|
|
|
|
if event.action == 'start'
|
|
|
|
spinner.start()
|
|
|
|
else
|
|
|
|
console.log()
|
|
|
|
spinner.stop()
|
2017-08-22 13:33:03 +00:00
|
|
|
|
2017-08-04 12:53:31 +00:00
|
|
|
options.image = params.image
|
|
|
|
options.appId = options.app
|
|
|
|
delete options.app
|
|
|
|
|
2017-10-06 09:14:46 +00:00
|
|
|
options.splashImage = options['splash-image']
|
|
|
|
delete options['splash-image']
|
|
|
|
|
2018-01-30 17:31:49 +00:00
|
|
|
options.dontCheckDeviceType = options['dont-check-device-type']
|
|
|
|
delete options['dont-check-device-type']
|
|
|
|
if options.dontCheckDeviceType and not options.appId
|
2017-10-06 09:05:24 +00:00
|
|
|
expectedError('You need to specify an app id if you disable the device type check.')
|
|
|
|
|
2017-08-04 12:53:31 +00:00
|
|
|
# Get a configured dockerode instance
|
|
|
|
dockerUtils.getDocker(options)
|
|
|
|
.then (docker) ->
|
|
|
|
|
2017-10-06 09:05:24 +00:00
|
|
|
preloader = new preload.Preloader(
|
2018-01-30 17:31:49 +00:00
|
|
|
resin
|
|
|
|
docker
|
|
|
|
options.appId
|
|
|
|
options.commit
|
|
|
|
options.image
|
|
|
|
options.splashImage
|
|
|
|
options.proxy
|
|
|
|
options.dontCheckDeviceType
|
2017-10-06 09:05:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
gotSignal = false
|
|
|
|
|
|
|
|
nodeCleanup (exitCode, signal) ->
|
|
|
|
if signal
|
|
|
|
gotSignal = true
|
|
|
|
nodeCleanup.uninstall() # don't call cleanup handler again
|
|
|
|
preloader.cleanup()
|
|
|
|
.then ->
|
|
|
|
# calling process.exit() won't inform parent process of signal
|
|
|
|
process.kill(process.pid, signal)
|
|
|
|
return false
|
2017-08-22 13:33:03 +00:00
|
|
|
|
|
|
|
if process.env.DEBUG
|
2017-10-06 09:05:24 +00:00
|
|
|
preloader.stderr.pipe(process.stderr)
|
2017-08-17 12:58:45 +00:00
|
|
|
|
2017-10-06 09:05:24 +00:00
|
|
|
preloader.on('progress', progressHandler)
|
|
|
|
preloader.on('spinner', spinnerHandler)
|
|
|
|
|
|
|
|
return new Promise (resolve, reject) ->
|
|
|
|
preloader.on('error', reject)
|
|
|
|
|
2018-01-30 17:31:49 +00:00
|
|
|
preloader.prepare()
|
2017-10-06 09:05:24 +00:00
|
|
|
.then ->
|
2018-01-30 17:31:49 +00:00
|
|
|
# If no appId was provided, show a list of matching apps
|
|
|
|
Promise.try ->
|
|
|
|
if not preloader.appId
|
|
|
|
selectApplication(preloader.config.deviceType)
|
|
|
|
.then (application) ->
|
|
|
|
preloader.setApplication(application)
|
2017-10-06 09:05:24 +00:00
|
|
|
.then ->
|
2018-01-30 17:31:49 +00:00
|
|
|
# Use the commit given as --commit or show an interactive commit selection menu
|
2017-10-06 09:05:24 +00:00
|
|
|
Promise.try ->
|
2018-01-30 17:31:49 +00:00
|
|
|
if options.commit
|
|
|
|
if options.commit == LATEST and preloader.application.commit
|
|
|
|
# handle `--commit latest`
|
|
|
|
return LATEST
|
|
|
|
release = _.find preloader.application.owns__release, (release) ->
|
|
|
|
release.commit.startsWith(options.commit)
|
|
|
|
if not release
|
|
|
|
expectedError('There is no release matching this commit')
|
|
|
|
return release.commit
|
|
|
|
selectApplicationCommit(preloader.application.owns__release)
|
|
|
|
.then (commit) ->
|
|
|
|
if commit == LATEST
|
|
|
|
preloader.commit = preloader.application.commit
|
|
|
|
else
|
|
|
|
preloader.commit = commit
|
|
|
|
|
|
|
|
# Propose to disable automatic app updates if the commit is not the latest
|
|
|
|
offerToDisableAutomaticUpdates(preloader.application, commit)
|
|
|
|
.then ->
|
|
|
|
# All options are ready: preload the image.
|
|
|
|
preloader.preload()
|
|
|
|
.catch(resin.errors.ResinError, expectedError)
|
2017-10-06 09:05:24 +00:00
|
|
|
.then(resolve)
|
|
|
|
.catch(reject)
|
|
|
|
.then(done)
|
|
|
|
.finally ->
|
|
|
|
if not gotSignal
|
|
|
|
preloader.cleanup()
|