lib/containers/Boxes.js   A
last analyzed

Complexity

Total Complexity 12
Complexity/F 4

Size

Lines of Code 66
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 12
c 1
b 0
f 0
nc 8
mnd 2
bc 10
fnc 3
dl 0
loc 66
rs 10
bpm 3.3333
cpm 4
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Boxes.create 0 49 1
1
'use strict'
2
3
const fs = require('fs')
4
const util = require('../util')
5
const Module = require('./Module')
6
const ContainerConfig = require('./ContainerConfig')
7
8
var Boxes = {}
9
10
/**
11
 * Create a new Includable cloud instance (dev mode).
12
 *
13
 * @param {RunOptions} options
14
 * @param {function(Error, BoxCreateResult)} callback
15
 */
16
Boxes.create = function (options, callback) {
17
  // First read module.json
18
  Module.getManifest(function (module) {
19
    // Create request
20
    var r = util.request('create', {
21
      'slug': module.slug
22
    })
23
24
    // Optionally add recipe
25
    if (options.recipe && options.recipe.length) {
26
      try {
27
        var recipe = fs.readFileSync(options.recipe, 'utf8')
28
        r = r.field('recipe', recipe)
29
      } catch (e) {
30
        callback(new Error('Error reading recipe: ' + e.message), null)
31
        return
32
      }
33
    }
34
35
    // Send request
36
    r.end(function (response) {
37
      var result = response.body
38
      if (typeof result === 'string') {
39
        callback(new Error('Unexpected output: ' + result), null)
40
        return
41
      }
42
      if ('error' in result) {
43
        if (typeof result.error !== 'string' && 'description' in result.error) {
44
          result.error = result.error.description
45
        }
46
        callback(new Error(result.error), null)
47
        return
48
      }
49
      if (!('host' in result)) {
50
        callback(new Error('unknown error creating container'), null)
51
        return
52
      }
53
54
      ContainerConfig.write(options.beta ? 'beta' : 'default', {
55
        version: '1.0',
56
        containerToken: result.container_token,
57
        containerEndpoint: global.containerEndpoint,
58
        containerEndpointCheckSSL: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0'
59
      })
60
61
      callback(null, result)
62
    })
63
  })
64
}
65
66
module.exports = Boxes
67