-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.js
131 lines (116 loc) · 2.86 KB
/
container.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
125
126
127
128
129
130
131
const { LXDClient } = require('./lxd')
const Resource = require('nanoresource')
const assert = require('assert')
const debug = require('debug')('little-container-box:lxd:container')
const ready = require('nanoresource-ready')
// quick util
const errback = (p, cb) => p.then((r) => cb(null, r), cb).catch(cb).catch(debug)
/**
* The `Container` class represents a `nanoresource` mapped to
* a Linux container managed by the `LXDClient` interface to `lxd`.
* @public
* @class
* @extends {nanoresource}
* @see {@link https://github.com/lxc/lxd/blob/master/doc/rest-api.md#10containers}
*/
class Container extends Resource {
/**
* `Container` class constructor.
* @param {LXDClient} client
* @param {String} name
* @param {?(Object)} opts
*/
constructor(client, name, opts) {
super()
if (!opts || 'object' !== typeof opts) {
opts = {}
}
assert(client instanceof LXDClient,
'Expecting client to be an `LXDClient` instance.')
assert(name && 'string' === typeof name,
'Expecting container name to be a string.')
this.ref = null
this.name = name
this.image = opts.image
this.client = client
this.config = opts.config
this.profile = opts.profile
this.createIfNotExists = 'boolean' === typeof opts.createIfNotExists
? opts.createIfNotExists
: true
}
/**
* Implements `_open()` for `nanoresource`.
* @protected
* @abstract
* @param {Function} callback
*/
_open(callback) {
this.stat((err, info) => {
if (err) {
// create if not exists
if ('HTTPError' === err.name && this.createIfNotExists) {
console.log(err);
this.client.launch(this.name, this, (err, info) => {
if (err) {
callback(err)
} else {
this.ref = info
callback(null)
}
})
} else {
callback(err)
}
} else {
this.ref = info
if ('running' === info.metadata.status.toLowerCase()) {
callback(null)
} else {
errback(this.ref.start(), callback)
}
}
})
}
/**
* Implements `_close()` for `nanoresource`.
* @protected
* @abstract
* @param {Function} callback
*/
_close(callback) {
process.nextTick(callback, null)
}
/**
* Waits for `nanoresource` instance to be ready (opened)
* and then calls `callback()` upon success or error.
* @param {Function} callback
*/
ready(callback) {
ready(this, callback)
}
/**
* Probes LXD for run time information.
* @param {Function} callback
*/
stat(callback) {
const { client, name } = this
client.stat(name, callback)
}
/**
*/
start(callback) {
return this.open(callback)
}
/**
*/
stop(callback) {
return this.close(callback)
}
}
/**
* Module exports.
*/
module.exports = {
Container
}