Share the code for fetching an image.

This commit is contained in:
Pagan Gazzard 2014-10-17 17:30:28 +01:00 committed by Pablo Carranza Vélez
parent 67c9db9fce
commit 07a4df1d05
3 changed files with 23 additions and 28 deletions

View File

@ -4,14 +4,15 @@ url = require 'url'
knex = require './db' knex = require './db'
path = require 'path' path = require 'path'
config = require './config' config = require './config'
{docker} = require './docker-utils' dockerUtils = require './docker-utils'
PUBNUB = require 'pubnub' PUBNUB = require 'pubnub'
Promise = require 'bluebird' Promise = require 'bluebird'
JSONStream = require 'JSONStream'
PlatformAPI = require 'resin-platform-api/request' PlatformAPI = require 'resin-platform-api/request'
utils = require './utils' utils = require './utils'
tty = require './tty' tty = require './tty'
{docker} = dockerUtils
PLATFORM_ENDPOINT = url.resolve(config.apiEndpoint, '/ewa/') PLATFORM_ENDPOINT = url.resolve(config.apiEndpoint, '/ewa/')
resinAPI = new PlatformAPI(PLATFORM_ENDPOINT) resinAPI = new PlatformAPI(PLATFORM_ENDPOINT)
@ -98,18 +99,7 @@ exports.start = start = (app) ->
utils.mixpanelTrack('Application install', app) utils.mixpanelTrack('Application install', app)
logSystemEvent('Installing application ' + app.imageId) logSystemEvent('Installing application ' + app.imageId)
updateDeviceInfo(status: 'Downloading') updateDeviceInfo(status: 'Downloading')
docker.createImageAsync(fromImage: app.imageId) dockerUtils.fetchImage(app.imageId)
.then (stream) ->
return new Promise (resolve, reject) ->
if stream.headers['content-type'] is 'application/json'
stream.pipe(JSONStream.parse('error'))
.pipe(es.mapSync(reject))
else
stream.pipe es.wait (error, text) ->
if error
reject(text)
stream.on('end', resolve)
.then -> .then ->
console.log('Creating container:', app.imageId) console.log('Creating container:', app.imageId)
updateDeviceInfo(status: 'Starting') updateDeviceInfo(status: 'Starting')

View File

@ -1,6 +1,8 @@
Docker = require 'dockerode' Docker = require 'dockerode'
Promise = require 'bluebird' Promise = require 'bluebird'
config = require './config' config = require './config'
JSONStream = require 'JSONStream'
es = require 'event-stream'
docker = Promise.promisifyAll(new Docker(socketPath: config.dockerSocket)) docker = Promise.promisifyAll(new Docker(socketPath: config.dockerSocket))
# Hack dockerode to promisify internal classes' prototypes # Hack dockerode to promisify internal classes' prototypes
@ -8,3 +10,16 @@ Promise.promisifyAll(docker.getImage().__proto__)
Promise.promisifyAll(docker.getContainer().__proto__) Promise.promisifyAll(docker.getContainer().__proto__)
exports.docker = docker exports.docker = docker
exports.fetchImage = (image) ->
docker.createImageAsync(fromImage: image)
.then (stream) ->
return new Promise (resolve, reject) ->
if stream.headers['content-type'] is 'application/json'
stream.pipe(JSONStream.parse('error'))
.pipe(es.mapSync(reject))
else
stream.pipe es.wait (error, text) ->
if error
reject(text)
stream.on('end', resolve)

View File

@ -1,10 +1,12 @@
config = require './config' config = require './config'
{docker} = require './docker-utils' dockerUtils = require './docker-utils'
Promise = require 'bluebird' Promise = require 'bluebird'
_ = require 'lodash' _ = require 'lodash'
es = require 'event-stream' es = require 'event-stream'
fs = Promise.promisifyAll(require('fs')) fs = Promise.promisifyAll(require('fs'))
{docker} = dockerUtils
localImage = config.localImage localImage = config.localImage
remoteImage = config.remoteImage remoteImage = config.remoteImage
@ -93,7 +95,6 @@ currentConfigPromise = currentContainerPromise.then (container) ->
# This is a promise that resolves when we have fully initialised. # This is a promise that resolves when we have fully initialised.
exports.initialised = currentConfigPromise.then (currentSupervisor) -> exports.initialised = currentConfigPromise.then (currentSupervisor) ->
utils = require './utils' utils = require './utils'
JSONStream = require 'JSONStream'
supervisorUpdating = Promise.resolve() supervisorUpdating = Promise.resolve()
exports.update = -> exports.update = ->
@ -102,18 +103,7 @@ exports.initialised = currentConfigPromise.then (currentSupervisor) ->
supervisorUpdating = supervisorUpdating.then -> supervisorUpdating = supervisorUpdating.then ->
utils.mixpanelTrack('Supervisor update check') utils.mixpanelTrack('Supervisor update check')
console.log('Fetching supervisor:', remoteImage) console.log('Fetching supervisor:', remoteImage)
docker.createImageAsync(fromImage: remoteImage) dockerUtils.fetchImage(remoteImage)
.then (stream) ->
return new Promise (resolve, reject) ->
if stream.headers['content-type'] is 'application/json'
stream.pipe(JSONStream.parse('error'))
.pipe(es.mapSync(reject))
else
stream.pipe es.wait (error, text) ->
if error
reject(text)
stream.on('end', resolve)
.then -> .then ->
console.log('Inspecting new supervisor:', remoteImage) console.log('Inspecting new supervisor:', remoteImage)
docker.getImage(remoteImage).inspectAsync() docker.getImage(remoteImage).inspectAsync()