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'
path = require 'path'
config = require './config'
{docker} = require './docker-utils'
dockerUtils = require './docker-utils'
PUBNUB = require 'pubnub'
Promise = require 'bluebird'
JSONStream = require 'JSONStream'
PlatformAPI = require 'resin-platform-api/request'
utils = require './utils'
tty = require './tty'
{docker} = dockerUtils
PLATFORM_ENDPOINT = url.resolve(config.apiEndpoint, '/ewa/')
resinAPI = new PlatformAPI(PLATFORM_ENDPOINT)
@ -98,18 +99,7 @@ exports.start = start = (app) ->
utils.mixpanelTrack('Application install', app)
logSystemEvent('Installing application ' + app.imageId)
updateDeviceInfo(status: 'Downloading')
docker.createImageAsync(fromImage: 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)
dockerUtils.fetchImage(app.imageId)
.then ->
console.log('Creating container:', app.imageId)
updateDeviceInfo(status: 'Starting')

View File

@ -1,6 +1,8 @@
Docker = require 'dockerode'
Promise = require 'bluebird'
config = require './config'
JSONStream = require 'JSONStream'
es = require 'event-stream'
docker = Promise.promisifyAll(new Docker(socketPath: config.dockerSocket))
# Hack dockerode to promisify internal classes' prototypes
@ -8,3 +10,16 @@ Promise.promisifyAll(docker.getImage().__proto__)
Promise.promisifyAll(docker.getContainer().__proto__)
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'
{docker} = require './docker-utils'
dockerUtils = require './docker-utils'
Promise = require 'bluebird'
_ = require 'lodash'
es = require 'event-stream'
fs = Promise.promisifyAll(require('fs'))
{docker} = dockerUtils
localImage = config.localImage
remoteImage = config.remoteImage
@ -93,7 +95,6 @@ currentConfigPromise = currentContainerPromise.then (container) ->
# This is a promise that resolves when we have fully initialised.
exports.initialised = currentConfigPromise.then (currentSupervisor) ->
utils = require './utils'
JSONStream = require 'JSONStream'
supervisorUpdating = Promise.resolve()
exports.update = ->
@ -102,18 +103,7 @@ exports.initialised = currentConfigPromise.then (currentSupervisor) ->
supervisorUpdating = supervisorUpdating.then ->
utils.mixpanelTrack('Supervisor update check')
console.log('Fetching supervisor:', remoteImage)
docker.createImageAsync(fromImage: 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)
dockerUtils.fetchImage(remoteImage)
.then ->
console.log('Inspecting new supervisor:', remoteImage)
docker.getImage(remoteImage).inspectAsync()