chore: format all code (#2632)

This commit is contained in:
Julien Fontanet 2018-02-09 17:56:03 +01:00 committed by GitHub
parent 8bf35b2a63
commit 7cb720b11f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
170 changed files with 5802 additions and 4801 deletions

View File

@ -18,9 +18,10 @@ class Job {
}
const scheduleNext = () => {
const delay = schedule._nextDelay()
this._timeout = delay < MAX_DELAY
? setTimeout(wrapper, delay)
: setTimeout(scheduleNext, MAX_DELAY)
this._timeout =
delay < MAX_DELAY
? setTimeout(wrapper, delay)
: setTimeout(scheduleNext, MAX_DELAY)
}
this._scheduleNext = scheduleNext

View File

@ -9,7 +9,7 @@ const NEXT_MAPPING = {
minute: { hour: 1 },
}
const getFirst = values => values !== undefined ? values[0] : 0
const getFirst = values => (values !== undefined ? values[0] : 0)
const setFirstAvailable = (date, unit, values) => {
if (values === undefined) {

View File

@ -90,7 +90,7 @@ const createParser = ({ fields: [...fields], presets: { ...presets } }) => {
if (!match('/')) {
return
}
[start, end] = field.range
;[start, end] = field.range
step = parseInteger()
} else {
start = parseValue()

View File

@ -28,9 +28,7 @@ describe('parse()', () => {
})
it('reports missing integer', () => {
expect(() => parse('*/a')).toThrow(
'minute: missing integer at character 2'
)
expect(() => parse('*/a')).toThrow('minute: missing integer at character 2')
expect(() => parse('*')).toThrow('hour: missing integer at character 1')
})

View File

@ -325,7 +325,10 @@ class P {
value.push(result.value)
pos = result.pos
}
while (i < max && (result = this._parse(input, pos, end)) instanceof Success) {
while (
i < max &&
(result = this._parse(input, pos, end)) instanceof Success
) {
++i
value.push(result.value)
pos = result.pos
@ -359,8 +362,9 @@ P.eof = new P(
const parser = P.grammar({
default: r =>
P.seq(r.ws, r.term.repeat(), P.eof)
.map(([, terms]) => (terms.length === 0 ? new Null() : new And(terms))),
P.seq(r.ws, r.term.repeat(), P.eof).map(
([, terms]) => (terms.length === 0 ? new Null() : new And(terms))
),
quotedString: new P((input, pos, end) => {
if (input[pos] !== '"') {
return new Failure(pos, '"')
@ -416,7 +420,7 @@ const parser = P.grammar({
? new StringNode(str)
: new NumberNode(asNum)
})
),
)
).skip(r.ws),
ws: P.regex(/\s*/),
}).default
@ -476,17 +480,19 @@ export const getPropertyClausesStrings = node => {
// -------------------------------------------------------------------
export const setPropertyClause = (node, name, child) => {
const property = child && new Property(
name,
typeof child === 'string' ? new StringNode(child) : child
)
const property =
child &&
new Property(
name,
typeof child === 'string' ? new StringNode(child) : child
)
if (node === undefined) {
return property
}
const children = (node instanceof And ? node.children : [node]).filter(child =>
!(child instanceof Property && child.name === name)
const children = (node instanceof And ? node.children : [node]).filter(
child => !(child instanceof Property && child.name === name)
)
if (property !== undefined) {
children.push(property)

View File

@ -49,13 +49,15 @@ describe('Number', () => {
describe('setPropertyClause', () => {
it('creates a node if none passed', () => {
expect(setPropertyClause(undefined, 'foo', 'bar').toString()).toBe('foo:bar')
expect(setPropertyClause(undefined, 'foo', 'bar').toString()).toBe(
'foo:bar'
)
})
it('adds a property clause if there was none', () => {
expect(
setPropertyClause(parse('baz'), 'foo', 'bar').toString()
).toBe('baz foo:bar')
expect(setPropertyClause(parse('baz'), 'foo', 'bar').toString()).toBe(
'baz foo:bar'
)
})
it('replaces the property clause if there was one', () => {

View File

@ -26,13 +26,16 @@ type ObjectPattern = { [string]: Pattern }
type ArrayPattern = Array<Pattern>
// value equals the pattern
type ValuePattern = bool | number | string
type ValuePattern = boolean | number | string
const match = (pattern: Pattern, value: any) => {
if (Array.isArray(pattern)) {
return Array.isArray(value) && pattern.every((subpattern, i) =>
// FIXME: subpatterns should match different subvalues
value.some(subvalue => match(subpattern, subvalue))
return (
Array.isArray(value) &&
pattern.every((subpattern, i) =>
// FIXME: subpatterns should match different subvalues
value.some(subvalue => match(subpattern, subvalue))
)
)
}
@ -41,7 +44,7 @@ const match = (pattern: Pattern, value: any) => {
const { length } = keys
if (length === 1) {
const [ key ] = keys
const [key] = keys
if (key === '__and') {
const andPattern: AndPattern = (pattern: any)
return andPattern.__and.every(subpattern => match(subpattern, value))
@ -74,4 +77,5 @@ const match = (pattern: Pattern, value: any) => {
return pattern === value
}
export const createPredicate = (pattern: Pattern) => (value: any) => match(pattern, value)
export const createPredicate = (pattern: Pattern) => (value: any) =>
match(pattern, value)

View File

@ -36,11 +36,13 @@ const fuFooter = fu.struct([
fu.char('creatorApplication', 4), // 28
fu.uint32('creatorVersion'), // 32
fu.uint32('creatorHostOs'), // 36
fu.struct('originalSize', [ // At the creation, current size of the hard disk.
fu.struct('originalSize', [
// At the creation, current size of the hard disk.
fu.uint32('high'), // 40
fu.uint32('low'), // 44
]),
fu.struct('currentSize', [ // Current size of the virtual disk. At the creation: currentSize = originalSize.
fu.struct('currentSize', [
// Current size of the virtual disk. At the creation: currentSize = originalSize.
fu.uint32('high'), // 48
fu.uint32('low'), // 52
]),
@ -60,11 +62,9 @@ const FOOTER_SIZE = fuFooter.size
const fuHeader = fu.struct([
fu.char('cookie', 8),
fu.struct('dataOffset', [
fu.uint32('high'),
fu.uint32('low'),
]),
fu.struct('tableOffset', [ // Absolute byte offset of the Block Allocation Table.
fu.struct('dataOffset', [fu.uint32('high'), fu.uint32('low')]),
fu.struct('tableOffset', [
// Absolute byte offset of the Block Allocation Table.
fu.uint32('high'),
fu.uint32('low'),
]),
@ -76,16 +76,21 @@ const fuHeader = fu.struct([
fu.uint32('parentTimestamp'),
fu.byte('reserved1', 4),
fu.char16be('parentUnicodeName', 512),
fu.struct('parentLocatorEntry', [
fu.uint32('platformCode'),
fu.uint32('platformDataSpace'),
fu.uint32('platformDataLength'),
fu.uint32('reserved'),
fu.struct('platformDataOffset', [ // Absolute byte offset of the locator data.
fu.uint32('high'),
fu.uint32('low'),
]),
], 8),
fu.struct(
'parentLocatorEntry',
[
fu.uint32('platformCode'),
fu.uint32('platformDataSpace'),
fu.uint32('platformDataLength'),
fu.uint32('reserved'),
fu.struct('platformDataOffset', [
// Absolute byte offset of the locator data.
fu.uint32('high'),
fu.uint32('low'),
]),
],
8
),
fu.byte('reserved2', 256),
])
const HEADER_SIZE = fuHeader.size
@ -98,10 +103,10 @@ const SIZE_OF_32_BITS = Math.pow(2, 32)
const uint32ToUint64 = fu => fu.high * SIZE_OF_32_BITS + fu.low
// Returns a 32 bits integer corresponding to a Vhd version.
const getVhdVersion = (major, minor) => (major << 16) | (minor & 0x0000FFFF)
const getVhdVersion = (major, minor) => (major << 16) | (minor & 0x0000ffff)
// bytes[] bit manipulation
const testBit = (map, bit) => map[bit >> 3] & 1 << (bit & 7)
const testBit = (map, bit) => map[bit >> 3] & (1 << (bit & 7))
const setBit = (map, bit) => {
map[bit >> 3] |= 1 << (bit & 7)
}
@ -109,98 +114,95 @@ const unsetBit = (map, bit) => {
map[bit >> 3] &= ~(1 << (bit & 7))
}
const addOffsets = (...offsets) => offsets.reduce(
(a, b) => b == null
? a
: typeof b === 'object'
? { bytes: a.bytes + b.bytes, bits: a.bits + b.bits }
: { bytes: a.bytes + b, bits: a.bits },
{ bytes: 0, bits: 0 }
)
const addOffsets = (...offsets) =>
offsets.reduce(
(a, b) =>
b == null
? a
: typeof b === 'object'
? { bytes: a.bytes + b.bytes, bits: a.bits + b.bits }
: { bytes: a.bytes + b, bits: a.bits },
{ bytes: 0, bits: 0 }
)
const pack = (field, value, buf, offset) => {
field.pack(
value,
buf,
addOffsets(field.offset, offset)
)
field.pack(value, buf, addOffsets(field.offset, offset))
}
const unpack = (field, buf, offset) =>
field.unpack(
buf,
addOffsets(field.offset, offset)
)
field.unpack(buf, addOffsets(field.offset, offset))
// ===================================================================
const streamToNewBuffer = stream => new Promise((resolve, reject) => {
const chunks = []
let length = 0
const streamToNewBuffer = stream =>
new Promise((resolve, reject) => {
const chunks = []
let length = 0
const onData = chunk => {
chunks.push(chunk)
length += chunk.length
}
stream.on('data', onData)
const onData = chunk => {
chunks.push(chunk)
length += chunk.length
}
stream.on('data', onData)
const clean = () => {
stream.removeListener('data', onData)
stream.removeListener('end', onEnd)
stream.removeListener('error', onError)
}
const onEnd = () => {
resolve(Buffer.concat(chunks, length))
clean()
}
stream.on('end', onEnd)
const onError = error => {
reject(error)
clean()
}
stream.on('error', onError)
})
const clean = () => {
stream.removeListener('data', onData)
stream.removeListener('end', onEnd)
stream.removeListener('error', onError)
}
const onEnd = () => {
resolve(Buffer.concat(chunks, length))
clean()
}
stream.on('end', onEnd)
const onError = error => {
reject(error)
clean()
}
stream.on('error', onError)
})
const streamToExistingBuffer = (
stream,
buffer,
offset = 0,
end = buffer.length
) => new Promise((resolve, reject) => {
assert(offset >= 0)
assert(end > offset)
assert(end <= buffer.length)
) =>
new Promise((resolve, reject) => {
assert(offset >= 0)
assert(end > offset)
assert(end <= buffer.length)
let i = offset
let i = offset
const onData = chunk => {
const prev = i
i += chunk.length
const onData = chunk => {
const prev = i
i += chunk.length
if (i > end) {
return onError(new Error('too much data'))
if (i > end) {
return onError(new Error('too much data'))
}
chunk.copy(buffer, prev)
}
stream.on('data', onData)
chunk.copy(buffer, prev)
}
stream.on('data', onData)
const clean = () => {
stream.removeListener('data', onData)
stream.removeListener('end', onEnd)
stream.removeListener('error', onError)
}
const onEnd = () => {
resolve(i - offset)
clean()
}
stream.on('end', onEnd)
const onError = error => {
reject(error)
clean()
}
stream.on('error', onError)
})
const clean = () => {
stream.removeListener('data', onData)
stream.removeListener('end', onEnd)
stream.removeListener('error', onError)
}
const onEnd = () => {
resolve(i - offset)
clean()
}
stream.on('end', onEnd)
const onError = error => {
reject(error)
clean()
}
stream.on('error', onError)
})
// ===================================================================
@ -214,7 +216,11 @@ const computeChecksum = (struct, buf, offset = 0) => {
for (let i = offset, n = checksumOffset; i < n; ++i) {
sum += buf[i]
}
for (let i = checksumOffset + checksumField.size, n = offset + struct.size; i < n; ++i) {
for (
let i = checksumOffset + checksumField.size, n = offset + struct.size;
i < n;
++i
) {
sum += buf[i]
}
@ -222,7 +228,8 @@ const computeChecksum = (struct, buf, offset = 0) => {
}
const verifyChecksum = (struct, buf, offset) =>
unpack(struct.fields.checksum, buf, offset) === computeChecksum(struct, buf, offset)
unpack(struct.fields.checksum, buf, offset) ===
computeChecksum(struct, buf, offset)
const getParentLocatorSize = parentLocatorEntry => {
const { platformDataSpace } = parentLocatorEntry
@ -231,15 +238,13 @@ const getParentLocatorSize = parentLocatorEntry => {
return platformDataSpace * SECTOR_SIZE
}
return (platformDataSpace % SECTOR_SIZE === 0)
? platformDataSpace
: 0
return platformDataSpace % SECTOR_SIZE === 0 ? platformDataSpace : 0
}
// ===================================================================
// Euclidean division, returns the quotient and the remainder of a / b.
const div = (a, b) => [ Math.floor(a / b), a % b ]
const div = (a, b) => [Math.floor(a / b), a % b]
export default class Vhd {
constructor (handler, path) {
@ -263,13 +268,22 @@ export default class Vhd {
assert(begin >= 0)
assert(length > 0)
return this._handler.createReadStream(this._path, {
end: begin + length - 1,
start: begin,
}).then(buf
? stream => streamToExistingBuffer(stream, buf, offset, (offset || 0) + length)
: streamToNewBuffer
)
return this._handler
.createReadStream(this._path, {
end: begin + length - 1,
start: begin,
})
.then(
buf
? stream =>
streamToExistingBuffer(
stream,
buf,
offset,
(offset || 0) + length
)
: streamToNewBuffer
)
}
// - if `buffer`: it is filled with 0 starting from `offset`, and
@ -296,7 +310,7 @@ export default class Vhd {
assert(block < this._header.maxTableEntries)
const blockAddr = this._blockAllocationTable[block]
if (blockAddr !== 0xFFFFFFFF) {
if (blockAddr !== 0xffffffff) {
return blockAddr * SECTOR_SIZE
}
}
@ -325,7 +339,8 @@ export default class Vhd {
assert(sectorsPerBlock % 1 === 0)
// 1 bit per sector, rounded up to full sectors
this._blockBitmapSize = Math.ceil(sectorsPerBlock / 8 / SECTOR_SIZE) * SECTOR_SIZE
this._blockBitmapSize =
Math.ceil(sectorsPerBlock / 8 / SECTOR_SIZE) * SECTOR_SIZE
assert(this._blockBitmapSize === SECTOR_SIZE)
this._footer = footer
@ -368,10 +383,10 @@ export default class Vhd {
const blockBitmapSize = this._blockBitmapSize
const parent = this._parent
if (blockAddr && (
!parent ||
testBit(await this._read(blockAddr, blockBitmapSize), sector)
)) {
if (
blockAddr &&
(!parent || testBit(await this._read(blockAddr, blockBitmapSize), sector))
) {
return this._read(
blockAddr + blockBitmapSize + sector * SECTOR_SIZE + begin,
length,
@ -402,12 +417,17 @@ export default class Vhd {
}
if (!parent) {
return this._read(blockAddr + this._blockBitmapSize + begin, length, buf, offset)
return this._read(
blockAddr + this._blockBitmapSize + begin,
length,
buf,
offset
)
}
// FIXME: we should read as many sectors in a single pass as
// possible for maximum perf.
const [ sector, beginInSector ] = div(begin, SECTOR_SIZE)
const [sector, beginInSector] = div(begin, SECTOR_SIZE)
return this._readBlockSector(
block,
sector,
@ -428,7 +448,7 @@ export default class Vhd {
}
const { blockSize } = this._header
const [ block, beginInBlock ] = div(begin, blockSize)
const [block, beginInBlock] = div(begin, blockSize)
return this._readBlock(
block,

View File

@ -29,13 +29,15 @@ exports.createOutputStream = path => {
exports.resolveRef = (xapi, type, refOrUuidOrNameLabel) =>
isOpaqueRef(refOrUuidOrNameLabel)
? refOrUuidOrNameLabel
: xapi.call(`${type}.get_by_uuid`, refOrUuidOrNameLabel).catch(
() => xapi.call(`${type}.get_by_name_label`, refOrUuidOrNameLabel).then(
refs => {
: xapi.call(`${type}.get_by_uuid`, refOrUuidOrNameLabel).catch(() =>
xapi
.call(`${type}.get_by_name_label`, refOrUuidOrNameLabel)
.then(refs => {
if (refs.length === 1) {
return refs[0]
}
throw new Error(`no single match for ${type} with name label ${refOrUuidOrNameLabel}`)
}
)
throw new Error(
`no single match for ${type} with name label ${refOrUuidOrNameLabel}`
)
})
)

View File

@ -56,7 +56,7 @@ const main = async args => {
let auth
if (opts._.length > 1) {
const [ , user, password = await askPassword() ] = opts._
const [, user, password = await askPassword()] = opts._
auth = { user, password }
}
@ -86,11 +86,11 @@ const main = async args => {
// Make the REPL waits for promise completion.
repl.eval = (evaluate => (cmd, context, filename, cb) => {
fromCallback(cb => {
;fromCallback(cb => {
evaluate.call(repl, cmd, context, filename, cb)
}).then(value =>
isArray(value) ? Promise.all(value) : value
)::asCallback(cb)
})
.then(value => (isArray(value) ? Promise.all(value) : value))
::asCallback(cb)
})(repl.eval)
await eventToPromise(repl, 'exit')

View File

@ -55,7 +55,7 @@ const NETWORK_ERRORS = {
ETIMEDOUT: true,
}
const isNetworkError = ({code}) => NETWORK_ERRORS[code]
const isNetworkError = ({ code }) => NETWORK_ERRORS[code]
// -------------------------------------------------------------------
@ -64,17 +64,17 @@ const XAPI_NETWORK_ERRORS = {
HOST_HAS_NO_MANAGEMENT_IP: true,
}
const isXapiNetworkError = ({code}) => XAPI_NETWORK_ERRORS[code]
const isXapiNetworkError = ({ code }) => XAPI_NETWORK_ERRORS[code]
// -------------------------------------------------------------------
const areEventsLost = ({code}) => code === 'EVENTS_LOST'
const areEventsLost = ({ code }) => code === 'EVENTS_LOST'
const isHostSlave = ({code}) => code === 'HOST_IS_SLAVE'
const isHostSlave = ({ code }) => code === 'HOST_IS_SLAVE'
const isMethodUnknown = ({code}) => code === 'MESSAGE_METHOD_UNKNOWN'
const isMethodUnknown = ({ code }) => code === 'MESSAGE_METHOD_UNKNOWN'
const isSessionInvalid = ({code}) => code === 'SESSION_INVALID'
const isSessionInvalid = ({ code }) => code === 'SESSION_INVALID'
// -------------------------------------------------------------------
@ -93,8 +93,9 @@ class XapiError extends BaseError {
export const wrapError = error => {
let code, params
if (isArray(error)) { // < XenServer 7.3
[ code, ...params ] = error
if (isArray(error)) {
// < XenServer 7.3
;[code, ...params] = error
} else {
code = error.message
params = error.data
@ -111,7 +112,7 @@ const parseUrl = url => {
throw new Error('invalid URL: ' + url)
}
const [ , protocol = 'https:', username, password, hostname, port ] = matches
const [, protocol = 'https:', username, password, hostname, port] = matches
return { protocol, username, password, hostname, port }
}
@ -128,17 +129,13 @@ const {
const OPAQUE_REF_PREFIX = 'OpaqueRef:'
export const isOpaqueRef = value =>
typeof value === 'string' &&
startsWith(value, OPAQUE_REF_PREFIX)
typeof value === 'string' && startsWith(value, OPAQUE_REF_PREFIX)
// -------------------------------------------------------------------
const RE_READ_ONLY_METHOD = /^[^.]+\.get_/
const isReadOnlyCall = (method, args) => (
args.length === 1 &&
isOpaqueRef(args[0]) &&
RE_READ_ONLY_METHOD.test(method)
)
const isReadOnlyCall = (method, args) =>
args.length === 1 && isOpaqueRef(args[0]) && RE_READ_ONLY_METHOD.test(method)
// Prepare values before passing them to the XenAPI:
//
@ -178,17 +175,17 @@ const EMPTY_ARRAY = freezeObject([])
const getTaskResult = (task, onSuccess, onFailure) => {
const { status } = task
if (status === 'cancelled') {
return [ onFailure(new Cancel('task canceled')) ]
return [onFailure(new Cancel('task canceled'))]
}
if (status === 'failure') {
return [ onFailure(wrapError(task.error_info)) ]
return [onFailure(wrapError(task.error_info))]
}
if (status === 'success') {
// the result might be:
// - empty string
// - an opaque reference
// - an XML-RPC value
return [ onSuccess(task.result) ]
return [onSuccess(task.result)]
}
}
@ -209,7 +206,7 @@ export class Xapi extends EventEmitter {
this._pool = null
this._readOnly = Boolean(opts.readOnly)
this._sessionId = null
const url = this._url = parseUrl(opts.url)
const url = (this._url = parseUrl(opts.url))
if (this._auth === undefined) {
const user = url.username
@ -224,9 +221,7 @@ export class Xapi extends EventEmitter {
}
if (opts.watchEvents !== false) {
this._debounce = opts.debounce == null
? 200
: opts.debounce
this._debounce = opts.debounce == null ? 200 : opts.debounce
this._eventWatchers = createObject(null)
@ -237,7 +232,7 @@ export class Xapi extends EventEmitter {
this._nTasks = 0
const objects = this._objects = new Collection()
const objects = (this._objects = new Collection())
objects.getKey = getKey
this._objectsByRefs = createObject(null)
@ -286,13 +281,7 @@ export class Xapi extends EventEmitter {
get status () {
const id = this._sessionId
return id
? (
id === CONNECTING
? CONNECTING
: CONNECTED
)
: DISCONNECTED
return id ? (id === CONNECTING ? CONNECTING : CONNECTED) : DISCONNECTED
}
get _humanId () {
@ -305,36 +294,46 @@ export class Xapi extends EventEmitter {
barrier (ref) {
const eventWatchers = this._eventWatchers
if (eventWatchers === undefined) {
return Promise.reject(new Error('Xapi#barrier() requires events watching'))
return Promise.reject(
new Error('Xapi#barrier() requires events watching')
)
}
const key = `xo:barrier:${Math.random().toString(36).slice(2)}`
const key = `xo:barrier:${Math.random()
.toString(36)
.slice(2)}`
const poolRef = this._pool.$ref
const { promise, resolve } = defer()
eventWatchers[key] = resolve
return this._sessionCall(
'pool.add_to_other_config',
[ poolRef, key, '' ]
).then(() => promise.then(() => {
this._sessionCall('pool.remove_from_other_config', [ poolRef, key ]).catch(noop)
return this._sessionCall('pool.add_to_other_config', [
poolRef,
key,
'',
]).then(() =>
promise.then(() => {
this._sessionCall('pool.remove_from_other_config', [
poolRef,
key,
]).catch(noop)
if (ref === undefined) {
return
}
if (ref === undefined) {
return
}
// support legacy params (type, ref)
if (arguments.length === 2) {
ref = arguments[1]
}
// support legacy params (type, ref)
if (arguments.length === 2) {
ref = arguments[1]
}
return this.getObjectByRef(ref)
}))
return this.getObjectByRef(ref)
})
)
}
connect () {
const {status} = this
const { status } = this
if (status === CONNECTED) {
return Promise.reject(new Error('already connected'))
@ -378,7 +377,7 @@ export class Xapi extends EventEmitter {
return Promise.reject(new Error('already disconnected'))
}
this._transportCall('session.logout', [ this._sessionId ]).catch(noop)
this._transportCall('session.logout', [this._sessionId]).catch(noop)
this._sessionId = null
@ -434,13 +433,10 @@ export class Xapi extends EventEmitter {
// this lib), UUID (unique identifier that some objects have) or
// opaque reference (internal to XAPI).
getObject (idOrUuidOrRef, defaultValue) {
const object = typeof idOrUuidOrRef === 'string'
? (
// if there is an UUID, it is also the $id.
this._objects.all[idOrUuidOrRef] ||
this._objectsByRefs[idOrUuidOrRef]
)
: this._objects.all[idOrUuidOrRef.$id]
const object =
typeof idOrUuidOrRef === 'string'
? this._objects.all[idOrUuidOrRef] || this._objectsByRefs[idOrUuidOrRef]
: this._objects.all[idOrUuidOrRef.$id]
if (object) return object
@ -479,158 +475,147 @@ export class Xapi extends EventEmitter {
}
@cancelable
getResource ($cancelToken, pathname, {
host,
query,
task,
}) {
return this._autoTask(
task,
`Xapi#getResource ${pathname}`
).then(taskRef => {
query = { ...query, session_id: this.sessionId }
let taskResult
if (taskRef !== undefined) {
query.task_id = taskRef
taskResult = this.watchTask(taskRef)
getResource ($cancelToken, pathname, { host, query, task }) {
return this._autoTask(task, `Xapi#getResource ${pathname}`).then(
taskRef => {
query = { ...query, session_id: this.sessionId }
let taskResult
if (taskRef !== undefined) {
query.task_id = taskRef
taskResult = this.watchTask(taskRef)
if (typeof $cancelToken.addHandler === 'function') {
$cancelToken.addHandler(() => taskResult)
if (typeof $cancelToken.addHandler === 'function') {
$cancelToken.addHandler(() => taskResult)
}
}
}
let promise = httpRequest(
$cancelToken,
this._url,
host && {
hostname: this.getObject(host).address,
},
{
pathname,
query,
rejectUnauthorized: !this._allowUnauthorized,
}
)
if (taskResult !== undefined) {
promise = promise.then(response => {
response.task = taskResult
return response
})
}
return promise
})
}
@cancelable
putResource ($cancelToken, body, pathname, {
host,
query,
task,
} = {}) {
if (this._readOnly) {
return Promise.reject(new Error(new Error('cannot put resource in read only mode')))
}
return this._autoTask(
task,
`Xapi#putResource ${pathname}`
).then(taskRef => {
query = { ...query, session_id: this.sessionId }
let taskResult
if (taskRef !== undefined) {
query.task_id = taskRef
taskResult = this.watchTask(taskRef)
if (typeof $cancelToken.addHandler === 'function') {
$cancelToken.addHandler(() => taskResult)
}
}
const headers = {}
// Xen API does not support chunk encoding.
const isStream = typeof body.pipe === 'function'
const { length } = body
if (isStream && length === undefined) {
// add a fake huge content length (1 PiB)
headers['content-length'] = '1125899906842624'
}
const doRequest = override => httpRequest.put(
$cancelToken,
this._url,
host && {
hostname: this.getObject(host).address,
},
{
body,
headers,
pathname,
query,
rejectUnauthorized: !this._allowUnauthorized,
},
override
)
const promise = isStream
// dummy request to probe for a redirection before consuming body
? doRequest({
body: '',
// omit task_id because this request will fail on purpose
query: 'task_id' in query
? omit(query, 'task_id')
: query,
maxRedirects: 0,
}).then(
response => {
response.req.abort()
return doRequest()
let promise = httpRequest(
$cancelToken,
this._url,
host && {
hostname: this.getObject(host).address,
},
error => {
let response
if (error != null && (response = error.response) != null) {
response.req.abort()
const { headers: { location }, statusCode } = response
if (statusCode === 302 && location !== undefined) {
return doRequest(location)
}
}
throw error
{
pathname,
query,
rejectUnauthorized: !this._allowUnauthorized,
}
)
// http-request-plus correctly handle redirects if body is not a stream
: doRequest()
return promise.then(response => {
const { req } = response
if (taskResult !== undefined) {
taskResult = taskResult.catch(error => {
error.url = response.url
throw error
promise = promise.then(response => {
response.task = taskResult
return response
})
}
if (req.finished) {
req.abort()
return taskResult
return promise
}
)
}
@cancelable
putResource ($cancelToken, body, pathname, { host, query, task } = {}) {
if (this._readOnly) {
return Promise.reject(
new Error(new Error('cannot put resource in read only mode'))
)
}
return this._autoTask(task, `Xapi#putResource ${pathname}`).then(
taskRef => {
query = { ...query, session_id: this.sessionId }
let taskResult
if (taskRef !== undefined) {
query.task_id = taskRef
taskResult = this.watchTask(taskRef)
if (typeof $cancelToken.addHandler === 'function') {
$cancelToken.addHandler(() => taskResult)
}
}
return fromEvents(req, ['close', 'finish']).then(() => {
req.abort()
return taskResult
const headers = {}
// Xen API does not support chunk encoding.
const isStream = typeof body.pipe === 'function'
const { length } = body
if (isStream && length === undefined) {
// add a fake huge content length (1 PiB)
headers['content-length'] = '1125899906842624'
}
const doRequest = override =>
httpRequest.put(
$cancelToken,
this._url,
host && {
hostname: this.getObject(host).address,
},
{
body,
headers,
pathname,
query,
rejectUnauthorized: !this._allowUnauthorized,
},
override
)
// if a stream, sends a dummy request to probe for a
// redirection before consuming body
const promise = isStream
? doRequest({
body: '',
// omit task_id because this request will fail on purpose
query: 'task_id' in query ? omit(query, 'task_id') : query,
maxRedirects: 0,
}).then(
response => {
response.req.abort()
return doRequest()
},
error => {
let response
if (error != null && (response = error.response) != null) {
response.req.abort()
const { headers: { location }, statusCode } = response
if (statusCode === 302 && location !== undefined) {
return doRequest(location)
}
}
throw error
}
)
: doRequest()
return promise.then(response => {
const { req } = response
if (taskResult !== undefined) {
taskResult = taskResult.catch(error => {
error.url = response.url
throw error
})
}
if (req.finished) {
req.abort()
return taskResult
}
return fromEvents(req, ['close', 'finish']).then(() => {
req.abort()
return taskResult
})
})
})
})
}
)
}
watchTask (ref) {
@ -692,22 +677,24 @@ export class Xapi extends EventEmitter {
newArgs.push.apply(newArgs, args)
}
return this._transportCall(method, newArgs)
::pCatch(isSessionInvalid, () => {
return this._transportCall(method, newArgs)::pCatch(
isSessionInvalid,
() => {
// XAPI is sometimes reinitialized and sessions are lost.
// Try to login again.
debug('%s: the session has been reinitialized', this._humanId)
this._sessionId = null
return this.connect().then(() => this._sessionCall(method, args))
})
}
)
} catch (error) {
return Promise.reject(error)
}
}
_addObject (type, ref, object) {
const {_objectsByRefs: objectsByRefs} = this
const { _objectsByRefs: objectsByRefs } = this
const reservedKeys = {
id: true,
@ -715,9 +702,8 @@ export class Xapi extends EventEmitter {
ref: true,
type: true,
}
const getKey = (key, obj) => reservedKeys[key] && obj === object
? `$$${key}`
: `$${key}`
const getKey = (key, obj) =>
reservedKeys[key] && obj === object ? `$$${key}` : `$${key}`
// Creates resolved properties.
forEach(object, function resolveObject (value, key, object) {
@ -736,7 +722,7 @@ export class Xapi extends EventEmitter {
} else if (isOpaqueRef(value[0])) {
// This is an array of refs.
defineProperty(object, getKey(key, object), {
get: () => freezeObject(map(value, (ref) => objectsByRefs[ref])),
get: () => freezeObject(map(value, ref => objectsByRefs[ref])),
})
freezeObject(value)
@ -836,38 +822,40 @@ export class Xapi extends EventEmitter {
}
_watchEvents () {
const loop = () => this.status === CONNECTED && this._sessionCall('event.from', [
['*'],
this._fromToken,
60 + 0.1, // Force float.
]).then(onSuccess, onFailure)
const loop = () =>
this.status === CONNECTED &&
this._sessionCall('event.from', [
['*'],
this._fromToken,
60 + 0.1, // Force float.
]).then(onSuccess, onFailure)
const onSuccess = ({ events, token, valid_ref_counts: { task } }) => {
this._fromToken = token
this._processEvents(events)
if (task !== this._nTasks) {
this._sessionCall('task.get_all_records').then(tasks => {
const toRemove = new Set()
forEach(this.objects.all, object => {
if (object.$type === 'task') {
toRemove.add(object.$ref)
}
this._sessionCall('task.get_all_records')
.then(tasks => {
const toRemove = new Set()
forEach(this.objects.all, object => {
if (object.$type === 'task') {
toRemove.add(object.$ref)
}
})
forEach(tasks, (task, ref) => {
toRemove.delete(ref)
this._addObject('task', ref, task)
})
toRemove.forEach(ref => {
this._removeObject('task', ref)
})
})
forEach(tasks, (task, ref) => {
toRemove.delete(ref)
this._addObject('task', ref, task)
})
toRemove.forEach(ref => {
this._removeObject('task', ref)
})
}).catch(noop)
.catch(noop)
}
const debounce = this._debounce
return debounce != null
? pDelay(debounce).then(loop)
: loop()
return debounce != null ? pDelay(debounce).then(loop) : loop()
}
const onFailure = error => {
if (areEventsLost(error)) {
@ -906,41 +894,43 @@ export class Xapi extends EventEmitter {
::/\.get_all_records$/.test
)
return Promise.all(map(
getAllRecordsMethods,
method => this._sessionCall(method).then(
objects => {
const type = method.slice(0, method.indexOf('.')).toLowerCase()
forEach(objects, (object, ref) => {
this._addObject(type, ref, object)
})
},
error => {
if (error.code !== 'MESSAGE_REMOVED') {
throw error
return Promise.all(
map(getAllRecordsMethods, method =>
this._sessionCall(method).then(
objects => {
const type = method.slice(0, method.indexOf('.')).toLowerCase()
forEach(objects, (object, ref) => {
this._addObject(type, ref, object)
})
},
error => {
if (error.code !== 'MESSAGE_REMOVED') {
throw error
}
}
}
)
)
))
)
})
}
const watchEvents = () => this._sessionCall('event.register', [ ['*'] ]).then(loop)
const watchEvents = () =>
this._sessionCall('event.register', [['*']]).then(loop)
const loop = () => this.status === CONNECTED && this._sessionCall('event.next').then(onSuccess, onFailure)
const loop = () =>
this.status === CONNECTED &&
this._sessionCall('event.next').then(onSuccess, onFailure)
const onSuccess = events => {
this._processEvents(events)
const debounce = this._debounce
return debounce == null
? loop()
: pDelay(debounce).then(loop)
return debounce == null ? loop() : pDelay(debounce).then(loop)
}
const onFailure = error => {
if (areEventsLost(error)) {
return this._sessionCall('event.unregister', [ ['*'] ]).then(watchEvents)
return this._sessionCall('event.unregister', [['*']]).then(watchEvents)
}
throw error
@ -950,85 +940,106 @@ export class Xapi extends EventEmitter {
}
}
Xapi.prototype._transportCall = reduce([
function (method, args) {
return this._call(method, args).catch(error => {
if (!(error instanceof Error)) {
error = wrapError(error)
}
error.method = method
throw error
})
},
call => function () {
let iterator // lazily created
const loop = () => call.apply(this, arguments)
::pCatch(isNetworkError, isXapiNetworkError, error => {
if (iterator === undefined) {
iterator = fibonacci().clamp(undefined, 60).take(10).toMs()
Xapi.prototype._transportCall = reduce(
[
function (method, args) {
return this._call(method, args).catch(error => {
if (!(error instanceof Error)) {
error = wrapError(error)
}
const cursor = iterator.next()
if (!cursor.done) {
// TODO: ability to cancel the connection
// TODO: ability to force immediate reconnection
const delay = cursor.value
debug('%s: network error %s, next try in %s ms', this._humanId, error.code, delay)
return pDelay(delay).then(loop)
}
debug('%s: network error %s, aborting', this._humanId, error.code)
// mark as disconnected
this.disconnect()::pCatch(noop)
error.method = method
throw error
})
return loop()
},
call => function loop () {
return call.apply(this, arguments)
::pCatch(isHostSlave, ({params: [master]}) => {
debug('%s: host is slave, attempting to connect at %s', this._humanId, master)
},
call =>
function () {
let iterator // lazily created
const loop = () =>
call
.apply(this, arguments)
::pCatch(isNetworkError, isXapiNetworkError, error => {
if (iterator === undefined) {
iterator = fibonacci()
.clamp(undefined, 60)
.take(10)
.toMs()
}
const newUrl = {
...this._url,
hostname: master,
}
this.emit('redirect', newUrl)
this._url = newUrl
const cursor = iterator.next()
if (!cursor.done) {
// TODO: ability to cancel the connection
// TODO: ability to force immediate reconnection
return loop.apply(this, arguments)
})
},
call => function (method) {
const startTime = Date.now()
return call.apply(this, arguments).then(
result => {
debug(
'%s: %s(...) [%s] ==> %s',
this._humanId,
method,
ms(Date.now() - startTime),
kindOf(result)
)
return result
const delay = cursor.value
debug(
'%s: network error %s, next try in %s ms',
this._humanId,
error.code,
delay
)
return pDelay(delay).then(loop)
}
debug('%s: network error %s, aborting', this._humanId, error.code)
// mark as disconnected
this.disconnect()::pCatch(noop)
throw error
})
return loop()
},
error => {
debug(
'%s: %s(...) [%s] =!> %s',
this._humanId,
method,
ms(Date.now() - startTime),
error
call =>
function loop () {
return call
.apply(this, arguments)
::pCatch(isHostSlave, ({ params: [master] }) => {
debug(
'%s: host is slave, attempting to connect at %s',
this._humanId,
master
)
const newUrl = {
...this._url,
hostname: master,
}
this.emit('redirect', newUrl)
this._url = newUrl
return loop.apply(this, arguments)
})
},
call =>
function (method) {
const startTime = Date.now()
return call.apply(this, arguments).then(
result => {
debug(
'%s: %s(...) [%s] ==> %s',
this._humanId,
method,
ms(Date.now() - startTime),
kindOf(result)
)
return result
},
error => {
debug(
'%s: %s(...) [%s] =!> %s',
this._humanId,
method,
ms(Date.now() - startTime),
error
)
throw error
}
)
throw error
}
)
},
], (call, decorator) => decorator(call))
},
],
(call, decorator) => decorator(call)
)
// ===================================================================

View File

@ -5,7 +5,7 @@ import { delay as pDelay } from 'promise-toolbox'
import { createClient } from './'
const xapi = (() => {
const [ , , url, user, password ] = process.argv
const [, , url, user, password] = process.argv
return createClient({
auth: { user, password },
@ -14,16 +14,19 @@ const xapi = (() => {
})
})()
xapi.connect()
xapi
.connect()
// Get the pool record's ref.
.then(() => xapi.call('pool.get_all'))
// Injects lots of events.
.then(([ poolRef ]) => {
const loop = () => xapi.call('event.inject', 'pool', poolRef)
::pDelay(10) // A small delay is required to avoid overloading the Xen API.
.then(loop)
.then(([poolRef]) => {
const loop = () =>
xapi
.call('event.inject', 'pool', poolRef)
::pDelay(10) // A small delay is required to avoid overloading the Xen API.
.then(loop)
return loop()
})

View File

@ -14,7 +14,7 @@ setInterval(() => {
)
}, 1e2)
const [ , , url, user, password ] = process.argv
const [, , url, user, password] = process.argv
createClient({
auth: { user, password },
readOnly: true,

View File

@ -3,7 +3,7 @@ import xmlRpc from './xml-rpc'
import xmlRpcJson from './xml-rpc-json'
import { UnsupportedTransport } from './_utils'
const factories = [ jsonRpc, xmlRpcJson, xmlRpc ]
const factories = [jsonRpc, xmlRpcJson, xmlRpc]
const { length } = factories
export default opts => {
@ -14,18 +14,18 @@ export default opts => {
const current = factories[i++](opts)
if (i < length) {
const currentI = i
call = (method, args) => current(method, args).catch(
error => {
call = (method, args) =>
current(method, args).catch(error => {
if (error instanceof UnsupportedTransport) {
if (currentI === i) { // not changed yet
if (currentI === i) {
// not changed yet
create()
}
return call(method, args)
}
throw error
}
)
})
} else {
call = current
}

View File

@ -4,35 +4,40 @@ import { format, parse } from 'json-rpc-protocol'
import { UnsupportedTransport } from './_utils'
export default ({ allowUnauthorized, url }) => {
return (method, args) => httpRequestPlus.post(url, {
rejectUnauthorized: !allowUnauthorized,
body: format.request(0, method, args),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
path: '/jsonrpc',
}).readAll('utf8').then(
text => {
let response
try {
response = parse(text)
} catch (error) {
throw new UnsupportedTransport()
}
return (method, args) =>
httpRequestPlus
.post(url, {
rejectUnauthorized: !allowUnauthorized,
body: format.request(0, method, args),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
path: '/jsonrpc',
})
.readAll('utf8')
.then(
text => {
let response
try {
response = parse(text)
} catch (error) {
throw new UnsupportedTransport()
}
if (response.type === 'response') {
return response.result
}
if (response.type === 'response') {
return response.result
}
throw response.error
},
error => {
if (error.response !== undefined) { // HTTP error
throw new UnsupportedTransport()
}
throw response.error
},
error => {
if (error.response !== undefined) {
// HTTP error
throw new UnsupportedTransport()
}
throw error
}
)
throw error
}
)
}

View File

@ -20,10 +20,7 @@ const SPECIAL_CHARS = {
'\r': '\\r',
'\t': '\\t',
}
const SPECIAL_CHARS_RE = new RegExp(
Object.keys(SPECIAL_CHARS).join('|'),
'g'
)
const SPECIAL_CHARS_RE = new RegExp(Object.keys(SPECIAL_CHARS).join('|'), 'g')
const parseResult = result => {
const status = result.Status
@ -78,11 +75,7 @@ export default ({
allowUnauthorized,
url: { hostname, path, port, protocol },
}) => {
const client = (
protocol === 'https:'
? createSecureClient
: createClient
)({
const client = (protocol === 'https:' ? createSecureClient : createClient)({
host: hostname,
path: '/json',
port,
@ -90,8 +83,5 @@ export default ({
})
const call = promisify(client.methodCall, client)
return (method, args) => call(method, args).then(
parseResult,
logError
)
return (method, args) => call(method, args).then(parseResult, logError)
}

View File

@ -34,19 +34,12 @@ export default ({
allowUnauthorized,
url: { hostname, path, port, protocol },
}) => {
const client = (
protocol === 'https:'
? createSecureClient
: createClient
)({
const client = (protocol === 'https:' ? createSecureClient : createClient)({
host: hostname,
port,
rejectUnauthorized: !allowUnauthorized,
})
const call = promisify(client.methodCall, client)
return (method, args) => call(method, args).then(
parseResult,
logError
)
return (method, args) => call(method, args).then(parseResult, logError)
}

View File

@ -8,7 +8,8 @@ let getObject
const authorized = () => true // eslint-disable-line no-unused-vars
const forbiddden = () => false // eslint-disable-line no-unused-vars
const and = (...checkers) => (object, permission) => { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
const and = (...checkers) => (object, permission) => {
for (const checker of checkers) {
if (!checker(object, permission)) {
return false
@ -17,7 +18,8 @@ const and = (...checkers) => (object, permission) => { // eslint-disable-line no
return true
}
const or = (...checkers) => (object, permission) => { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
const or = (...checkers) => (object, permission) => {
for (const checker of checkers) {
if (checker(object, permission)) {
return true
@ -28,7 +30,7 @@ const or = (...checkers) => (object, permission) => { // eslint-disable-line no-
// -------------------------------------------------------------------
const checkMember = (memberName) => (object, permission) => {
const checkMember = memberName => (object, permission) => {
const member = object[memberName]
return member !== object.id && checkAuthorization(member, permission)
}
@ -36,10 +38,7 @@ const checkMember = (memberName) => (object, permission) => {
const checkSelf = ({ id }, permission) => {
const permissionsForObject = permissionsByObject[id]
return (
permissionsForObject &&
permissionsForObject[permission]
)
return permissionsForObject && permissionsForObject[permission]
}
// ===================================================================
@ -102,12 +101,7 @@ function checkAuthorization (objectId, permission) {
// -------------------------------------------------------------------
export default (
permissionsByObject_,
getObject_,
permissions,
permission
) => {
export default (permissionsByObject_, getObject_, permissions, permission) => {
// Assign global variables.
permissionsByObject = permissionsByObject_
getObject = getObject_

View File

@ -5,7 +5,7 @@ const __TEST__ = NODE_ENV === 'test'
module.exports = {
comments: !__PROD__,
compact: __PROD__,
ignore: __TEST__ ? undefined : [ /\.spec\.js$/ ],
ignore: __TEST__ ? undefined : [/\.spec\.js$/],
plugins: ['lodash'],
presets: [
[
@ -14,9 +14,7 @@ module.exports = {
debug: !__TEST__,
loose: true,
shippedProposals: true,
targets: __PROD__
? { node: '6' }
: { node: 'current' },
targets: __PROD__ ? { node: '6' } : { node: 'current' },
useBuiltIns: 'usage',
},
],

View File

@ -19,11 +19,13 @@ const configFile = configPath + '/config.json'
// ===================================================================
const load = exports.load = function () {
return readFile(configFile).then(JSON.parse).catch(function () {
return {}
})
}
const load = (exports.load = function () {
return readFile(configFile)
.then(JSON.parse)
.catch(function () {
return {}
})
})
exports.get = function (path) {
return load().then(function (config) {
@ -31,11 +33,11 @@ exports.get = function (path) {
})
}
const save = exports.save = function (config) {
const save = (exports.save = function (config) {
return mkdirp(configPath).then(function () {
return writeFile(configFile, JSON.stringify(config))
})
}
})
exports.set = function (data) {
return load().then(function (config) {

View File

@ -108,14 +108,16 @@ const humanFormatOpts = {
function printProgress (progress) {
if (progress.length) {
console.warn('%s% of %s @ %s/s - ETA %s',
console.warn(
'%s% of %s @ %s/s - ETA %s',
Math.round(progress.percentage),
humanFormat(progress.length, humanFormatOpts),
humanFormat(progress.speed, humanFormatOpts),
prettyMs(progress.eta * 1e3)
)
} else {
console.warn('%s @ %s/s',
console.warn(
'%s @ %s/s',
humanFormat(progress.transferred, humanFormatOpts),
humanFormat(progress.speed, humanFormatOpts)
)
@ -130,8 +132,10 @@ function wrap (val) {
// ===================================================================
const help = wrap((function (pkg) {
return require('strip-indent')(`
const help = wrap(
(function (pkg) {
return require('strip-indent')(
`
Usage:
$name --register [--expiresIn duration] <XO-Server URL> <username> [<password>]
@ -162,18 +166,20 @@ const help = wrap((function (pkg) {
Executes a command on the current XO instance.
$name v$version
`).replace(/<([^>]+)>|\$(\w+)/g, function (_, arg, key) {
if (arg) {
return '<' + chalk.yellow(arg) + '>'
}
`
).replace(/<([^>]+)>|\$(\w+)/g, function (_, arg, key) {
if (arg) {
return '<' + chalk.yellow(arg) + '>'
}
if (key === 'name') {
return chalk.bold(pkg[key])
}
if (key === 'name') {
return chalk.bold(pkg[key])
}
return pkg[key]
})
})(require('../package')))
return pkg[key]
})
})(require('../package'))
)
// -------------------------------------------------------------------
@ -230,10 +236,7 @@ async function register (args) {
exports.register = register
function unregister () {
return config.unset([
'server',
'token',
])
return config.unset(['server', 'token'])
}
exports.unregister = unregister
@ -284,11 +287,7 @@ async function listCommands (args) {
str.push(
name,
'=<',
type == null
? 'unknown type'
: isArray(type)
? type.join('|')
: type,
type == null ? 'unknown type' : isArray(type) ? type.join('|') : type,
'>'
)
@ -347,10 +346,7 @@ async function call (args) {
const result = await xo.call(method, params)
let keys, key, url
if (
isObject(result) &&
(keys = getKeys(result)).length === 1
) {
if (isObject(result) && (keys = getKeys(result)).length === 1) {
key = keys[0]
if (key === '$getFrom') {
@ -359,16 +355,19 @@ async function call (args) {
const progress = progressStream({ time: 1e3 }, printProgress)
return eventToPromise(nicePipe([
got.stream(url).on('response', function (response) {
const length = response.headers['content-length']
if (length !== undefined) {
progress.length(length)
}
}),
progress,
output,
]), 'finish')
return eventToPromise(
nicePipe([
got.stream(url).on('response', function (response) {
const length = response.headers['content-length']
if (length !== undefined) {
progress.length(length)
}
}),
progress,
output,
]),
'finish'
)
}
if (key === '$sendTo') {
@ -379,10 +378,13 @@ async function call (args) {
const input = nicePipe([
createReadStream(file),
progressStream({
length: length,
time: 1e3,
}, printProgress),
progressStream(
{
length: length,
time: 1e3,
},
printProgress
),
])
const response = await got.post(url, {

View File

@ -1,17 +1,14 @@
import kindOf from 'kindof'
import {BaseError} from 'make-error'
import {EventEmitter} from 'events'
import {forEach} from 'lodash'
import { BaseError } from 'make-error'
import { EventEmitter } from 'events'
import { forEach } from 'lodash'
import isEmpty from './is-empty'
import isObject from './is-object'
// ===================================================================
const {
create: createObject,
prototype: { hasOwnProperty },
} = Object
const { create: createObject, prototype: { hasOwnProperty } } = Object
export const ACTION_ADD = 'add'
export const ACTION_UPDATE = 'update'
@ -189,7 +186,7 @@ export default class Collection extends EventEmitter {
// -----------------------------------------------------------------
createIndex (name, index) {
const {_indexes: indexes} = this
const { _indexes: indexes } = this
if (hasOwnProperty.call(indexes, name)) {
throw new DuplicateIndex(name)
}
@ -201,7 +198,7 @@ export default class Collection extends EventEmitter {
}
deleteIndex (name) {
const {_indexes: indexes} = this
const { _indexes: indexes } = this
if (!hasOwnProperty.call(indexes, name)) {
throw new NoSuchIndex(name)
}
@ -218,7 +215,7 @@ export default class Collection extends EventEmitter {
// -----------------------------------------------------------------
* [Symbol.iterator] () {
const {_items: items} = this
const { _items: items } = this
for (const key in items) {
yield [key, items[key]]
@ -226,7 +223,7 @@ export default class Collection extends EventEmitter {
}
* keys () {
const {_items: items} = this
const { _items: items } = this
for (const key in items) {
yield key
@ -234,7 +231,7 @@ export default class Collection extends EventEmitter {
}
* values () {
const {_items: items} = this
const { _items: items } = this
for (const key in items) {
yield items[key]
@ -259,7 +256,7 @@ export default class Collection extends EventEmitter {
return
}
const {_buffer: buffer} = this
const { _buffer: buffer } = this
// Due to deduplication there could be nothing in the buffer.
if (isEmpty(buffer)) {
@ -354,7 +351,8 @@ export default class Collection extends EventEmitter {
} else {
this._buffer[key] = ACTION_REMOVE
}
} else { // update
} else {
// update
if (!this._buffer[key]) {
this._buffer[key] = ACTION_UPDATE
}

View File

@ -3,15 +3,15 @@
import eventToPromise from 'event-to-promise'
import { forEach } from 'lodash'
import Collection, {DuplicateItem, NoSuchItem} from './collection'
import Collection, { DuplicateItem, NoSuchItem } from './collection'
// ===================================================================
function waitTicks (n = 2) {
const {nextTick} = process
const { nextTick } = process
return new Promise(function (resolve) {
(function waitNextTick () {
;(function waitNextTick () {
// The first tick is handled by Promise#then()
if (--n) {
nextTick(waitNextTick)
@ -34,16 +34,16 @@ describe('Collection', function () {
it('is iterable', function () {
const iterator = col[Symbol.iterator]()
expect(iterator.next()).toEqual({done: false, value: ['bar', 0]})
expect(iterator.next()).toEqual({done: true, value: undefined})
expect(iterator.next()).toEqual({ done: false, value: ['bar', 0] })
expect(iterator.next()).toEqual({ done: true, value: undefined })
})
describe('#keys()', function () {
it('returns an iterator over the keys', function () {
const iterator = col.keys()
expect(iterator.next()).toEqual({done: false, value: 'bar'})
expect(iterator.next()).toEqual({done: true, value: undefined})
expect(iterator.next()).toEqual({ done: false, value: 'bar' })
expect(iterator.next()).toEqual({ done: true, value: undefined })
})
})
@ -51,8 +51,8 @@ describe('Collection', function () {
it('returns an iterator over the values', function () {
const iterator = col.values()
expect(iterator.next()).toEqual({done: false, value: 0})
expect(iterator.next()).toEqual({done: true, value: undefined})
expect(iterator.next()).toEqual({ done: false, value: 0 })
expect(iterator.next()).toEqual({ done: true, value: undefined })
})
})
@ -70,7 +70,7 @@ describe('Collection', function () {
// Async event.
return eventToPromise(col, 'add').then(function (added) {
expect(Object.keys(added)).toEqual([ 'foo' ])
expect(Object.keys(added)).toEqual(['foo'])
expect(added.foo).toBe(true)
})
})
@ -216,7 +216,7 @@ describe('Collection', function () {
})
it('accepts an object with an id property', function () {
col.unset({id: 'bar'})
col.unset({ id: 'bar' })
expect(col.has('bar')).toBe(false)
@ -235,7 +235,7 @@ describe('Collection', function () {
return waitTicks().then(() => {
col.touch(foo)
return eventToPromise(col, 'update', (items) => {
return eventToPromise(col, 'update', items => {
expect(Object.keys(items)).toEqual(['foo'])
expect(items.foo).toBe(foo)
})
@ -249,7 +249,7 @@ describe('Collection', function () {
expect(col.size).toBe(0)
return eventToPromise(col, 'remove').then((items) => {
return eventToPromise(col, 'remove').then(items => {
expect(Object.keys(items)).toEqual(['bar'])
expect(items.bar).toBeUndefined()
})
@ -257,84 +257,69 @@ describe('Collection', function () {
})
describe('deduplicates events', function () {
forEach({
'add & update → add': [
[
['add', 'foo', 0],
['update', 'foo', 1],
],
{
add: {
foo: 1,
forEach(
{
'add & update → add': [
[['add', 'foo', 0], ['update', 'foo', 1]],
{
add: {
foo: 1,
},
},
},
],
'add & remove → ∅': [
[
['add', 'foo', 0],
['remove', 'foo'],
],
{},
],
'update & update → update': [
[
['update', 'bar', 1],
['update', 'bar', 2],
],
{
update: {
bar: 2,
'add & remove → ∅': [[['add', 'foo', 0], ['remove', 'foo']], {}],
'update & update → update': [
[['update', 'bar', 1], ['update', 'bar', 2]],
{
update: {
bar: 2,
},
},
},
],
'update & remove → remove': [
[
['update', 'bar', 1],
['remove', 'bar'],
],
{
remove: {
bar: undefined,
},
},
],
'remove & add → update': [
[
['remove', 'bar'],
['add', 'bar', 0],
'update & remove → remove': [
[['update', 'bar', 1], ['remove', 'bar']],
{
remove: {
bar: undefined,
},
},
],
{
update: {
bar: 0,
'remove & add → update': [
[['remove', 'bar'], ['add', 'bar', 0]],
{
update: {
bar: 0,
},
},
},
],
}, ([operations, results], label) => {
it(label, function () {
forEach(operations, ([method, ...args]) => {
col[method](...args)
})
],
},
([operations, results], label) => {
it(label, function () {
forEach(operations, ([method, ...args]) => {
col[method](...args)
})
const spies = Object.create(null)
forEach(['add', 'update', 'remove'], event => {
col.on(event, (spies[event] = jest.fn()))
})
const spies = Object.create(null)
forEach(['add', 'update', 'remove'], event => {
col.on(event, (spies[event] = jest.fn()))
})
return waitTicks().then(() => {
forEach(spies, (spy, event) => {
const items = results[event]
if (items) {
expect(spy.mock.calls).toEqual([ [ items ] ])
} else {
expect(spy).not.toHaveBeenCalled()
}
return waitTicks().then(() => {
forEach(spies, (spy, event) => {
const items = results[event]
if (items) {
expect(spy.mock.calls).toEqual([[items]])
} else {
expect(spy).not.toHaveBeenCalled()
}
})
})
})
})
})
}
)
})
})

View File

@ -3,11 +3,7 @@ import { bind, iteratee } from 'lodash'
import clearObject from './clear-object'
import isEmpty from './is-empty'
import NotImplemented from './not-implemented'
import {
ACTION_ADD,
ACTION_UPDATE,
ACTION_REMOVE,
} from './collection'
import { ACTION_ADD, ACTION_UPDATE, ACTION_REMOVE } from './collection'
// ===================================================================
@ -34,7 +30,7 @@ export default class Index {
// Remove empty items lists.
sweep () {
const {_itemsByHash: itemsByHash} = this
const { _itemsByHash: itemsByHash } = this
for (const hash in itemsByHash) {
if (isEmpty(itemsByHash[hash])) {
delete itemsByHash[hash]
@ -86,14 +82,11 @@ export default class Index {
const hash = computeHash(value, key)
if (hash != null) {
(
itemsByHash[hash] ||
;(itemsByHash[hash] ||
// FIXME: We do not use objects without prototype for now
// because it breaks Angular in xo-web, change it back when
// this is fixed.
(itemsByHash[hash] = {})
)[key] = value
(itemsByHash[hash] = {}))[key] = value
keysToHash[key] = hash
}
@ -118,12 +111,9 @@ export default class Index {
// Inserts item into the new hash's list if any.
if (hash != null) {
(
itemsByHash[hash] ||
;(itemsByHash[hash] ||
// FIXME: idem: change back to Object.create(null)
(itemsByHash[hash] = {})
)[key] = value
(itemsByHash[hash] = {}))[key] = value
keysToHash[key] = hash
} else {
@ -133,10 +123,7 @@ export default class Index {
}
_onRemove (items) {
const {
_itemsByHash: itemsByHash,
_keysToHash: keysToHash,
} = this
const { _itemsByHash: itemsByHash, _keysToHash: keysToHash } = this
for (const key in items) {
const prev = keysToHash[key]

View File

@ -9,10 +9,10 @@ import Index from './index'
// ===================================================================
const waitTicks = (n = 2) => {
const {nextTick} = process
const { nextTick } = process
return new Promise(resolve => {
(function waitNextTick () {
;(function waitNextTick () {
// The first tick is handled by Promise#then()
if (--n) {
nextTick(waitNextTick)

View File

@ -1,3 +1,3 @@
export default function isObject (value) {
return (value !== null) && (typeof value === 'object')
return value !== null && typeof value === 'object'
}

View File

@ -1,4 +1,4 @@
import {BaseError} from 'make-error'
import { BaseError } from 'make-error'
export default class NotImplemented extends BaseError {
constructor (message) {

View File

@ -2,11 +2,7 @@ import { bind, iteratee } from 'lodash'
import clearObject from './clear-object'
import NotImplemented from './not-implemented'
import {
ACTION_ADD,
ACTION_UPDATE,
ACTION_REMOVE,
} from './collection'
import { ACTION_ADD, ACTION_UPDATE, ACTION_REMOVE } from './collection'
// ===================================================================
@ -108,10 +104,7 @@ export default class UniqueIndex {
}
_onRemove (items) {
const {
_itemByHash: itemByHash,
_keysToHash: keysToHash,
} = this
const { _itemByHash: itemByHash, _keysToHash: keysToHash } = this
for (const key in items) {
const prev = keysToHash[key]

View File

@ -9,10 +9,10 @@ import Index from './unique-index'
// ===================================================================
const waitTicks = (n = 2) => {
const {nextTick} = process
const { nextTick } = process
return new Promise(resolve => {
(function waitNextTick () {
;(function waitNextTick () {
// The first tick is handled by Promise#then()
if (--n) {
nextTick(waitNextTick)

View File

@ -7,7 +7,7 @@ import View from './view'
// Create the collection.
const users = new Collection()
users.getKey = (user) => user.name
users.getKey = user => user.name
// Inserts some data.
users.add({

View File

@ -54,7 +54,7 @@ export default class View extends Collection {
}
_onAdd (items) {
const {_predicate: predicate} = this
const { _predicate: predicate } = this
forEach(items, (value, key) => {
if (predicate(value, key, this)) {
@ -67,7 +67,7 @@ export default class View extends Collection {
}
_onUpdate (items) {
const {_predicate: predicate} = this
const { _predicate: predicate } = this
forEach(items, (value, key) => {
if (predicate(value, key, this)) {

View File

@ -10,36 +10,53 @@ const xo = new Xo({
url: 'localhost:9000',
})
xo.open().then(function () {
return xo.call('acl.get', {}).then(function (result) {
console.log('success:', result)
}).catch(function (error) {
console.log('failure:', error)
xo
.open()
.then(function () {
return xo
.call('acl.get', {})
.then(function (result) {
console.log('success:', result)
})
.catch(function (error) {
console.log('failure:', error)
})
})
}).then(function () {
return xo.signIn({
email: 'admin@admin.net',
password: 'admin',
}).then(function () {
console.log('connected as ', xo.user)
}).catch(function (error) {
console.log('failure:', error)
.then(function () {
return xo
.signIn({
email: 'admin@admin.net',
password: 'admin',
})
.then(function () {
console.log('connected as ', xo.user)
})
.catch(function (error) {
console.log('failure:', error)
})
})
}).then(function () {
return xo.signIn({
email: 'tom',
password: 'tom',
}).then(function () {
console.log('connected as', xo.user)
.then(function () {
return xo
.signIn({
email: 'tom',
password: 'tom',
})
.then(function () {
console.log('connected as', xo.user)
return xo.call('acl.get', {}).then(function (result) {
console.log('success:', result)
}).catch(function (error) {
console.log('failure:', error)
})
}).catch(function (error) {
console.log('failure', error)
return xo
.call('acl.get', {})
.then(function (result) {
console.log('success:', result)
})
.catch(function (error) {
console.log('failure:', error)
})
})
.catch(function (error) {
console.log('failure', error)
})
})
.then(function () {
return xo.close()
})
}).then(function () {
return xo.close()
})

View File

@ -1,7 +1,4 @@
import JsonRpcWebSocketClient, {
OPEN,
CLOSED,
} from 'jsonrpc-websocket-client'
import JsonRpcWebSocketClient, { OPEN, CLOSED } from 'jsonrpc-websocket-client'
import { BaseError } from 'make-error'
import { startsWith } from 'lodash'
@ -20,7 +17,7 @@ export default class Xo extends JsonRpcWebSocketClient {
const url = opts != null ? opts.url : '.'
super(`${url === '/' ? '' : url}/api/`)
this._credentials = (opts != null ? opts.credentials : null)
this._credentials = opts != null ? opts.credentials : null
this._user = null
this.on(OPEN, () => {
@ -45,12 +42,13 @@ export default class Xo extends JsonRpcWebSocketClient {
}
const promise = super.call(method, args)
promise.retry = (predicate) => promise.catch((error) => {
i = (i || 0) + 1
if (predicate(error, i)) {
return this.call(method, args, i)
}
})
promise.retry = predicate =>
promise.catch(error => {
i = (i || 0) + 1
if (predicate(error, i)) {
return this.call(method, args, i)
}
})
return promise
}

View File

@ -3,10 +3,13 @@ import map from 'lodash/map'
import trim from 'lodash/trim'
import trimStart from 'lodash/trimStart'
const sanitizePath = (...paths) => filter(map(paths, s => s && filter(map(s.split('/'), trim)).join('/'))).join('/')
const sanitizePath = (...paths) =>
filter(map(paths, s => s && filter(map(s.split('/'), trim)).join('/'))).join(
'/'
)
export const parse = string => {
const object = { }
const object = {}
const [type, rest] = string.split('://')
if (type === 'file') {
@ -36,7 +39,7 @@ export const parse = string => {
return object
}
export const format = ({type, host, path, username, password, domain}) => {
export const format = ({ type, host, path, username, password, domain }) => {
type === 'local' && (type = 'file')
let string = `${type}://`
if (type === 'nfs') {

View File

@ -1,4 +1,4 @@
import {Strategy} from 'passport-github'
import { Strategy } from 'passport-github'
// ===================================================================
@ -27,18 +27,23 @@ class AuthGitHubXoPlugin {
}
load () {
const {_xo: xo} = this
const { _xo: xo } = this
xo.registerPassportStrategy(new Strategy(this._conf, async (accessToken, refreshToken, profile, done) => {
try {
done(null, await xo.registerUser('github', profile.username))
} catch (error) {
done(error.message)
}
}))
xo.registerPassportStrategy(
new Strategy(
this._conf,
async (accessToken, refreshToken, profile, done) => {
try {
done(null, await xo.registerUser('github', profile.username))
} catch (error) {
done(error.message)
}
}
)
)
}
}
// ===================================================================
export default ({xo}) => new AuthGitHubXoPlugin(xo)
export default ({ xo }) => new AuthGitHubXoPlugin(xo)

View File

@ -7,7 +7,8 @@ export const configurationSchema = {
properties: {
callbackURL: {
type: 'string',
description: 'Must be exactly the same as specified on the Google developer console.',
description:
'Must be exactly the same as specified on the Google developer console.',
},
clientID: {
type: 'string',
@ -18,8 +19,8 @@ export const configurationSchema = {
scope: {
default: 'https://www.googleapis.com/auth/plus.login',
description: 'Note that changing this value will break existing users.',
enum: [ 'https://www.googleapis.com/auth/plus.login', 'email' ],
enumNames: [ 'Google+ name', 'Simple email address' ],
enum: ['https://www.googleapis.com/auth/plus.login', 'email'],
enumNames: ['Google+ name', 'Simple email address'],
},
},
required: ['callbackURL', 'clientID', 'clientSecret'],
@ -41,18 +42,23 @@ class AuthGoogleXoPlugin {
const conf = this._conf
const xo = this._xo
xo.registerPassportStrategy(new Strategy(conf, async (accessToken, refreshToken, profile, done) => {
try {
done(null, await xo.registerUser(
'google',
conf.scope === 'email'
? profile.emails[0].value
: profile.displayName
))
} catch (error) {
done(error.message)
}
}))
xo.registerPassportStrategy(
new Strategy(conf, async (accessToken, refreshToken, profile, done) => {
try {
done(
null,
await xo.registerUser(
'google',
conf.scope === 'email'
? profile.emails[0].value
: profile.displayName
)
)
} catch (error) {
done(error.message)
}
})
)
}
}

View File

@ -10,15 +10,16 @@ import { readFile } from 'fs'
// ===================================================================
const VAR_RE = /\{\{([^}]+)\}\}/g
const evalFilter = (filter, vars) => filter.replace(VAR_RE, (_, name) => {
const value = vars[name]
const evalFilter = (filter, vars) =>
filter.replace(VAR_RE, (_, name) => {
const value = vars[name]
if (value === undefined) {
throw new Error('invalid variable: ' + name)
}
if (value === undefined) {
throw new Error('invalid variable: ' + name)
}
return escape(value)
})
return escape(value)
})
export const configurationSchema = {
type: 'object',
@ -39,7 +40,8 @@ If not specified, it will use a default set of well-known CAs.
},
},
checkCertificate: {
description: 'Enforce the validity of the server\'s certificates. You can disable it when connecting to servers that use a self-signed certificate.',
description:
"Enforce the validity of the server's certificates. You can disable it when connecting to servers that use a self-signed certificate.",
type: 'boolean',
default: true,
},
@ -58,14 +60,16 @@ For Microsoft Active Directory, it can also be \`<user>@<domain>\`.
type: 'string',
},
password: {
description: 'Password of the user permitted of search the LDAP directory.',
description:
'Password of the user permitted of search the LDAP directory.',
type: 'string',
},
},
required: ['dn', 'password'],
},
base: {
description: 'The base is the part of the description tree where the users are looked for.',
description:
'The base is the part of the description tree where the users are looked for.',
type: 'string',
},
filter: {
@ -116,25 +120,21 @@ class AuthLdap {
}
async configure (conf) {
const clientOpts = this._clientOpts = {
const clientOpts = (this._clientOpts = {
url: conf.uri,
maxConnections: 5,
tlsOptions: {},
}
})
{
const {
bind,
checkCertificate = true,
certificateAuthorities,
} = conf
const { bind, checkCertificate = true, certificateAuthorities } = conf
if (bind) {
clientOpts.bindDN = bind.dn
clientOpts.bindCredentials = bind.password
}
const {tlsOptions} = clientOpts
const { tlsOptions } = clientOpts
tlsOptions.rejectUnauthorized = checkCertificate
if (certificateAuthorities) {
@ -192,7 +192,7 @@ class AuthLdap {
// Bind if necessary.
{
const {_credentials: credentials} = this
const { _credentials: credentials } = this
if (credentials) {
logger(`attempting to bind with as ${credentials.dn}...`)
await bind(credentials.dn, credentials.password)
@ -216,7 +216,7 @@ class AuthLdap {
entries.push(entry.json)
})
const {status} = await eventToPromise(response, 'end')
const { status } = await eventToPromise(response, 'end')
if (status) {
throw new Error('unexpected search response status: ' + status)
}
@ -229,7 +229,11 @@ class AuthLdap {
try {
logger(`attempting to bind as ${entry.objectName}`)
await bind(entry.objectName, password)
logger(`successfully bound as ${entry.objectName} => ${username} authenticated`)
logger(
`successfully bound as ${
entry.objectName
} => ${username} authenticated`
)
return { username }
} catch (error) {
logger(`failed to bind as ${entry.objectName}: ${error.message}`)
@ -246,4 +250,4 @@ class AuthLdap {
// ===================================================================
export default ({xo}) => new AuthLdap(xo)
export default ({ xo }) => new AuthLdap(xo)

View File

@ -8,48 +8,61 @@ const EMPTY_OBJECT = Object.freeze({ __proto__: null })
const _extractValue = ({ value }) => value
export const confirm = (message, {
default: defaultValue = null,
} = EMPTY_OBJECT) => prompt({
default: defaultValue,
export const confirm = (
message,
name: 'value',
type: 'confirm',
}).then(_extractValue)
{ default: defaultValue = null } = EMPTY_OBJECT
) =>
prompt({
default: defaultValue,
message,
name: 'value',
type: 'confirm',
}).then(_extractValue)
export const input = (message, {
default: defaultValue = null,
filter = undefined,
validate = undefined,
} = EMPTY_OBJECT) => prompt({
default: defaultValue,
export const input = (
message,
name: 'value',
type: 'input',
validate,
}).then(_extractValue)
{
default: defaultValue = null,
filter = undefined,
validate = undefined,
} = EMPTY_OBJECT
) =>
prompt({
default: defaultValue,
message,
name: 'value',
type: 'input',
validate,
}).then(_extractValue)
export const list = (message, choices, {
default: defaultValue = null,
} = EMPTY_OBJECT) => prompt({
default: defaultValue,
export const list = (
message,
choices,
message,
name: 'value',
type: 'list',
}).then(_extractValue)
{ default: defaultValue = null } = EMPTY_OBJECT
) =>
prompt({
default: defaultValue,
choices,
message,
name: 'value',
type: 'list',
}).then(_extractValue)
export const password = (message, {
default: defaultValue = null,
filter = undefined,
validate = undefined,
} = EMPTY_OBJECT) => prompt({
default: defaultValue,
export const password = (
message,
name: 'value',
type: 'password',
validate,
}).then(_extractValue)
{
default: defaultValue = null,
filter = undefined,
validate = undefined,
} = EMPTY_OBJECT
) =>
prompt({
default: defaultValue,
message,
name: 'value',
type: 'password',
validate,
}).then(_extractValue)
// ===================================================================
@ -69,25 +82,25 @@ const promptByType = {
items[i] = await promptGeneric(
itemSchema,
defaultValue[i],
path
? `${path} [${i}]`
: `[${i}]`
path ? `${path} [${i}]` : `[${i}]`
)
++i
}
let n = schema.minItems || 0
while (i < n) { // eslint-disable-line no-unmodified-loop-condition
// eslint-disable-next-line no-unmodified-loop-condition
while (i < n) {
await promptItem()
}
n = schema.maxItems || Infinity
while (
i < n && // eslint-disable-line no-unmodified-loop-condition
await confirm('additional item?', {
// eslint-disable-next-line no-unmodified-loop-condition
i < n &&
(await confirm('additional item?', {
default: false,
})
}))
) {
await promptItem()
}
@ -95,33 +108,38 @@ const promptByType = {
return items
},
boolean: (schema, defaultValue, path) => confirm(path, {
default: defaultValue != null ? defaultValue : schema.default,
}),
boolean: (schema, defaultValue, path) =>
confirm(path, {
default: defaultValue != null ? defaultValue : schema.default,
}),
enum: (schema, defaultValue, path) => list(path, schema.enum, {
defaultValue: defaultValue || schema.defaultValue,
}),
enum: (schema, defaultValue, path) =>
list(path, schema.enum, {
defaultValue: defaultValue || schema.defaultValue,
}),
integer: (schema, defaultValue, path) => input(path, {
default: defaultValue || schema.default,
filter: input => +input,
validate: input => isInteger(+input),
}),
integer: (schema, defaultValue, path) =>
input(path, {
default: defaultValue || schema.default,
filter: input => +input,
validate: input => isInteger(+input),
}),
number: (schema, defaultValue, path) => input(path, {
default: defaultValue || schema.default,
filter: input => +input,
validate: input => isFinite(+input),
}),
number: (schema, defaultValue, path) =>
input(path, {
default: defaultValue || schema.default,
filter: input => +input,
validate: input => isFinite(+input),
}),
object: async (schema, defaultValue, path) => {
const value = {}
const required = {}
schema.required && forEach(schema.required, name => {
required[name] = true
})
schema.required &&
forEach(schema.required, name => {
required[name] = true
})
const promptProperty = async (schema, name) => {
const subpath = path
@ -130,9 +148,9 @@ const promptByType = {
if (
required[name] ||
await confirm(`fill optional ${subpath}?`, {
(await confirm(`fill optional ${subpath}?`, {
default: Boolean(defaultValue && name in defaultValue),
})
}))
) {
value[name] = await promptGeneric(
schema,
@ -147,15 +165,14 @@ const promptByType = {
return value
},
string: (schema, defaultValue, path) => input(path, {
default: defaultValue || schema.default,
}),
string: (schema, defaultValue, path) =>
input(path, {
default: defaultValue || schema.default,
}),
}
export default function promptGeneric (schema, defaultValue, path) {
const type = schema.enum
? 'enum'
: schema.type
const type = schema.enum ? 'enum' : schema.type
const prompt = promptByType[type.toLowerCase()]
if (!prompt) {

View File

@ -5,13 +5,8 @@ import { bind } from 'lodash'
import { fromCallback } from 'promise-toolbox'
import { readFile, writeFile } from 'fs'
import promptSchema, {
input,
password,
} from './prompt-schema'
import createPlugin, {
configurationSchema,
} from './'
import promptSchema, { input, password } from './prompt-schema'
import createPlugin, { configurationSchema } from './'
// ===================================================================
@ -27,7 +22,9 @@ execPromise(async args => {
() => ({})
)
)
await fromCallback(cb => writeFile(CACHE_FILE, JSON.stringify(config, null, 2), cb)).then(
await fromCallback(cb =>
writeFile(CACHE_FILE, JSON.stringify(config, null, 2), cb)
).then(
() => {
console.log('configuration saved in %s', CACHE_FILE)
},
@ -40,10 +37,13 @@ execPromise(async args => {
const plugin = createPlugin({})
await plugin.configure(config)
await plugin._authenticate({
username: await input('Username', {
validate: input => !!input.length,
}),
password: await password('Password'),
}, bind(console.log, console))
await plugin._authenticate(
{
username: await input('Username', {
validate: input => !!input.length,
}),
password: await password('Password'),
},
bind(console.log, console)
)
})

View File

@ -1,4 +1,4 @@
import {Strategy} from 'passport-saml'
import { Strategy } from 'passport-saml'
// ===================================================================
@ -38,19 +38,21 @@ class AuthSamlXoPlugin {
load () {
const xo = this._xo
xo.registerPassportStrategy(new Strategy(this._conf, async (profile, done) => {
const name = profile[this._usernameField]
if (!name) {
done('no name found for this user')
return
}
xo.registerPassportStrategy(
new Strategy(this._conf, async (profile, done) => {
const name = profile[this._usernameField]
if (!name) {
done('no name found for this user')
return
}
try {
done(null, await xo.registerUser('saml', name))
} catch (error) {
done(error.message)
}
}))
try {
done(null, await xo.registerUser('saml', name))
} catch (error) {
done(error.message)
}
})
)
}
}

View File

@ -37,15 +37,17 @@ const ICON_FAILURE = '🚨'
const ICON_SUCCESS = '✔'
const DATE_FORMAT = 'dddd, MMMM Do YYYY, h:mm:ss a'
const createDateFormater = timezone => timezone !== undefined
? timestamp => moment(timestamp).tz(timezone).format(DATE_FORMAT)
: timestamp => moment(timestamp).format(DATE_FORMAT)
const createDateFormater = timezone =>
timezone !== undefined
? timestamp =>
moment(timestamp)
.tz(timezone)
.format(DATE_FORMAT)
: timestamp => moment(timestamp).format(DATE_FORMAT)
const formatDuration = milliseconds =>
moment.duration(milliseconds).humanize()
const formatDuration = milliseconds => moment.duration(milliseconds).humanize()
const formatMethod = method =>
startCase(method.slice(method.indexOf('.') + 1))
const formatMethod = method => startCase(method.slice(method.indexOf('.') + 1))
const formatSize = bytes =>
humanFormat(bytes, {
@ -83,7 +85,9 @@ class BackupReportsXoPlugin {
}
_wrapper (status) {
return new Promise(resolve => resolve(this._listener(status))).catch(logError)
return new Promise(resolve => resolve(this._listener(status))).catch(
logError
)
}
_listener (status) {
@ -114,8 +118,7 @@ class BackupReportsXoPlugin {
}
const reportOnFailure =
reportWhen === 'fail' || // xo-web < 5
reportWhen === 'failure' // xo-web >= 5
reportWhen === 'fail' || reportWhen === 'failure' // xo-web < 5 // xo-web >= 5
let globalMergeSize = 0
let globalTransferSize = 0
@ -152,11 +155,7 @@ class BackupReportsXoPlugin {
const { message } = error
failedBackupsText.push(
...text,
`- **Error**: ${message}`,
''
)
failedBackupsText.push(...text, `- **Error**: ${message}`, '')
nagiosText.push(
`[ ${vm !== undefined ? vm.name_label : 'undefined'} : ${message} ]`
@ -169,22 +168,25 @@ class BackupReportsXoPlugin {
globalTransferSize += transferSize
text.push(
`- **Transfer size**: ${formatSize(transferSize)}`,
`- **Transfer speed**: ${formatSpeed(transferSize, returnedValue.transferDuration)}`
`- **Transfer speed**: ${formatSpeed(
transferSize,
returnedValue.transferDuration
)}`
)
}
if (mergeSize !== undefined) {
globalMergeSize += mergeSize
text.push(
`- **Merge size**: ${formatSize(mergeSize)}`,
`- **Merge speed**: ${formatSpeed(mergeSize, returnedValue.mergeDuration)}`
`- **Merge speed**: ${formatSpeed(
mergeSize,
returnedValue.mergeDuration
)}`
)
}
}
successfulBackupText.push(
...text,
''
)
successfulBackupText.push(...text, '')
}
})
@ -208,14 +210,10 @@ class BackupReportsXoPlugin {
`- **Successes**: ${nSuccesses} / ${nCalls}`,
]
if (globalTransferSize !== 0) {
markdown.push(
`- **Transfer size**: ${formatSize(globalTransferSize)}`
)
markdown.push(`- **Transfer size**: ${formatSize(globalTransferSize)}`)
}
if (globalMergeSize !== 0) {
markdown.push(
`- **Merge size**: ${formatSize(globalMergeSize)}`
)
markdown.push(`- **Merge size**: ${formatSize(globalMergeSize)}`)
}
markdown.push('')
@ -239,38 +237,40 @@ class BackupReportsXoPlugin {
)
}
markdown.push(
'---',
'',
`*${pkg.name} v${pkg.version}*`
)
markdown.push('---', '', `*${pkg.name} v${pkg.version}*`)
markdown = markdown.join('\n')
const xo = this._xo
return Promise.all([
xo.sendEmail !== undefined && xo.sendEmail({
to: this._mailsReceivers,
subject: `[Xen Orchestra] ${
globalSuccess ? 'Success' : 'Failure'
} Backup report for ${tag} ${
globalSuccess ? ICON_SUCCESS : ICON_FAILURE
}`,
markdown,
}),
xo.sendToXmppClient !== undefined && xo.sendToXmppClient({
to: this._xmppReceivers,
message: markdown,
}),
xo.sendSlackMessage !== undefined && xo.sendSlackMessage({
message: markdown,
}),
xo.sendPassiveCheck !== undefined && xo.sendPassiveCheck({
status: globalSuccess ? 0 : 2,
message: globalSuccess
? `[Xen Orchestra] [Success] Backup report for ${tag}`
: `[Xen Orchestra] [Failure] Backup report for ${tag} - VMs : ${nagiosText.join(' ')}`,
}),
xo.sendEmail !== undefined &&
xo.sendEmail({
to: this._mailsReceivers,
subject: `[Xen Orchestra] ${
globalSuccess ? 'Success' : 'Failure'
} Backup report for ${tag} ${
globalSuccess ? ICON_SUCCESS : ICON_FAILURE
}`,
markdown,
}),
xo.sendToXmppClient !== undefined &&
xo.sendToXmppClient({
to: this._xmppReceivers,
message: markdown,
}),
xo.sendSlackMessage !== undefined &&
xo.sendSlackMessage({
message: markdown,
}),
xo.sendPassiveCheck !== undefined &&
xo.sendPassiveCheck({
status: globalSuccess ? 0 : 2,
message: globalSuccess
? `[Xen Orchestra] [Success] Backup report for ${tag}`
: `[Xen Orchestra] [Failure] Backup report for ${tag} - VMs : ${nagiosText.join(
' '
)}`,
}),
])
}
}

View File

@ -27,7 +27,8 @@ class XoServerCloud {
getResourceCatalog.description = 'Get the list of all available resources'
getResourceCatalog.permission = 'admin'
const registerResource = ({ namespace }) => this._registerResource(namespace)
const registerResource = ({ namespace }) =>
this._registerResource(namespace)
registerResource.description = 'Register a resource via cloud plugin'
registerResource.params = {
namespace: {
@ -42,21 +43,22 @@ class XoServerCloud {
registerResource,
},
})
this._unsetRequestResource = this._xo.defineProperty('requestResource', this._requestResource, this)
this._unsetRequestResource = this._xo.defineProperty(
'requestResource',
this._requestResource,
this
)
const updater = this._updater = new Client(`${UPDATER_URL}:${WS_PORT}`)
const connect = () => updater.open(createBackoff()).catch(
error => {
const updater = (this._updater = new Client(`${UPDATER_URL}:${WS_PORT}`))
const connect = () =>
updater.open(createBackoff()).catch(error => {
console.error('xo-server-cloud: fail to connect to updater', error)
return connect()
}
)
updater
.on('closed', connect)
.on('scheduledAttempt', ({ delay }) => {
console.warn('xo-server-cloud: next attempt in %s ms', delay)
})
updater.on('closed', connect).on('scheduledAttempt', ({ delay }) => {
console.warn('xo-server-cloud: next attempt in %s ms', delay)
})
connect()
}
@ -138,13 +140,15 @@ class XoServerCloud {
throw new Error('cannot get download token')
}
const req = request.get(`${UPDATER_URL}:${HTTP_PORT}/`)
const req = request
.get(`${UPDATER_URL}:${HTTP_PORT}/`)
.set('Authorization', `Bearer ${downloadToken}`)
// Impossible to pipe the response directly: https://github.com/visionmedia/superagent/issues/1187
const pt = new PassThrough()
req.pipe(pt)
pt.length = (await eventToPromise(req, 'response')).headers['content-length']
const { headers } = await eventToPromise(req, 'response')
pt.length = headers['content-length']
return pt
}

View File

@ -7,8 +7,9 @@ import { debug } from './utils'
export default class DensityPlan extends Plan {
_checkRessourcesThresholds (objects, averages) {
return filter(objects, object =>
averages[object.id].memoryFree > this._thresholds.memoryFree.low
return filter(
objects,
object => averages[object.id].memoryFree > this._thresholds.memoryFree.low
)
}
@ -19,27 +20,17 @@ export default class DensityPlan extends Plan {
return
}
const {
hosts,
toOptimize,
} = results
const { hosts, toOptimize } = results
let {
averages: hostsAverages,
} = results
let { averages: hostsAverages } = results
const pools = await this._getPlanPools()
let optimizationsCount = 0
for (const hostToOptimize of toOptimize) {
const {
id: hostId,
$poolId: poolId,
} = hostToOptimize
const { id: hostId, $poolId: poolId } = hostToOptimize
const {
master: masterId,
} = pools[poolId]
const { master: masterId } = pools[poolId]
// Avoid master optimization.
if (masterId === hostId) {
@ -58,10 +49,7 @@ export default class DensityPlan extends Plan {
const otherHosts = []
for (const dest of hosts) {
const {
id: destId,
$poolId: destPoolId,
} = dest
const { id: destId, $poolId: destPoolId } = dest
// Destination host != Host to optimize!
if (destId === hostId) {
@ -83,12 +71,7 @@ export default class DensityPlan extends Plan {
const simulResults = await this._simulate({
host: hostToOptimize,
destinations: [
[ poolMaster ],
poolHosts,
masters,
otherHosts,
],
destinations: [[poolMaster], poolHosts, masters, otherHosts],
hostsAverages: clone(hostsAverages),
})
@ -115,15 +98,15 @@ export default class DensityPlan extends Plan {
for (const vm of vms) {
if (!vm.xenTools) {
debug(`VM (${vm.id}) of Host (${hostId}) does not support pool migration.`)
debug(
`VM (${vm.id}) of Host (${hostId}) does not support pool migration.`
)
return
}
}
// Sort vms by amount of memory. (+ -> -)
vms.sort((a, b) =>
vmsAverages[b.id].memory - vmsAverages[a.id].memory
)
vms.sort((a, b) => vmsAverages[b.id].memory - vmsAverages[a.id].memory)
const simulResults = {
hostsAverages,
@ -162,15 +145,11 @@ export default class DensityPlan extends Plan {
// Test if a VM migration on a destination (of a destinations set) is possible.
_testMigration ({ vm, destinations, hostsAverages, vmsAverages }) {
const {
_thresholds: {
critical: criticalThreshold,
},
} = this
const { _thresholds: { critical: criticalThreshold } } = this
// Sort the destinations by available memory. (- -> +)
destinations.sort((a, b) =>
hostsAverages[a.id].memoryFree - hostsAverages[b.id].memoryFree
destinations.sort(
(a, b) => hostsAverages[a.id].memoryFree - hostsAverages[b.id].memoryFree
)
for (const destination of destinations) {
@ -204,13 +183,18 @@ export default class DensityPlan extends Plan {
await Promise.all(
mapToArray(moves, move => {
const {
vm,
destination,
} = move
const { vm, destination } = move
const xapiDest = this.xo.getXapi(destination)
debug(`Migrate VM (${vm.id}) to Host (${destination.id}) from Host (${vm.$container}).`)
return xapiDest.migrateVm(vm._xapiId, this.xo.getXapi(destination), destination._xapiId)
debug(
`Migrate VM (${vm.id}) to Host (${destination.id}) from Host (${
vm.$container
}).`
)
return xapiDest.migrateVm(
vm._xapiId,
this.xo.getXapi(destination),
destination._xapiId
)
})
)

View File

@ -9,10 +9,7 @@ import {
DEFAULT_CRITICAL_THRESHOLD_CPU,
DEFAULT_CRITICAL_THRESHOLD_MEMORY_FREE,
} from './plan'
import {
EXECUTION_DELAY,
debug,
} from './utils'
import { EXECUTION_DELAY, debug } from './utils'
// ===================================================================
@ -41,7 +38,7 @@ export const configurationSchema = {
},
mode: {
enum: [ 'Performance mode', 'Density mode' ],
enum: ['Performance mode', 'Density mode'],
title: 'Mode',
},
@ -85,7 +82,7 @@ export const configurationSchema = {
},
},
required: [ 'name', 'mode', 'pools' ],
required: ['name', 'mode', 'pools'],
},
minItems: 1,
@ -115,7 +112,10 @@ const makeJob = (cronPattern, fn) => {
try {
await fn()
} catch (error) {
console.error('[WARN] scheduled function:', (error && error.stack) || error)
console.error(
'[WARN] scheduled function:',
(error && error.stack) || error
)
} finally {
job.running = false
job.emitter.emit('finish')
@ -133,7 +133,10 @@ const makeJob = (cronPattern, fn) => {
class LoadBalancerPlugin {
constructor (xo) {
this.xo = xo
this._job = makeJob(`*/${EXECUTION_DELAY} * * * *`, this._executePlans.bind(this))
this._job = makeJob(
`*/${EXECUTION_DELAY} * * * *`,
this._executePlans.bind(this)
)
}
async configure ({ plans }) {
@ -154,7 +157,10 @@ class LoadBalancerPlugin {
if (plans) {
for (const plan of plans) {
this._addPlan(plan.mode === 'Performance mode' ? PERFORMANCE_MODE : DENSITY_MODE, plan)
this._addPlan(
plan.mode === 'Performance mode' ? PERFORMANCE_MODE : DENSITY_MODE,
plan
)
}
}
@ -180,18 +186,17 @@ class LoadBalancerPlugin {
}
this._poolIds = this._poolIds.concat(pools)
this._plans.push(mode === PERFORMANCE_MODE
? new PerformancePlan(this.xo, name, pools, options)
: new DensityPlan(this.xo, name, pools, options)
this._plans.push(
mode === PERFORMANCE_MODE
? new PerformancePlan(this.xo, name, pools, options)
: new DensityPlan(this.xo, name, pools, options)
)
}
_executePlans () {
debug('Execute plans!')
return Promise.all(
mapToArray(this._plans, plan => plan.execute())
)
return Promise.all(mapToArray(this._plans, plan => plan.execute()))
}
}

View File

@ -35,7 +35,10 @@ export default class PerformancePlan extends Plan {
try {
await Promise.all(
mapToArray(
filter(this._getHosts({ powerState: 'Halted' }), host => host.powerOnMode !== ''),
filter(
this._getHosts({ powerState: 'Halted' }),
host => host.powerOnMode !== ''
),
host => {
const { id } = host
return this.xo.getXapi(id).powerOnHost(id)
@ -52,17 +55,14 @@ export default class PerformancePlan extends Plan {
return
}
const {
averages,
toOptimize,
} = results
const { averages, toOptimize } = results
let { hosts } = results
toOptimize.sort((a, b) => {
a = averages[a.id]
b = averages[b.id]
return (b.cpu - a.cpu) || (a.memoryFree - b.memoryFree)
return b.cpu - a.cpu || a.memoryFree - b.memoryFree
})
for (const exceededHost of toOptimize) {
@ -85,9 +85,7 @@ export default class PerformancePlan extends Plan {
const vmsAverages = await this._getVmsAverages(vms, exceededHost)
// Sort vms by cpu usage. (lower to higher)
vms.sort((a, b) =>
vmsAverages[b.id].cpu - vmsAverages[a.id].cpu
)
vms.sort((a, b) => vmsAverages[b.id].cpu - vmsAverages[a.id].cpu)
const exceededAverages = hostsAverages[exceededHost.id]
const promises = []
@ -95,11 +93,15 @@ export default class PerformancePlan extends Plan {
const xapiSrc = this.xo.getXapi(exceededHost)
let optimizationsCount = 0
const searchFunction = (a, b) => hostsAverages[b.id].cpu - hostsAverages[a.id].cpu
const searchFunction = (a, b) =>
hostsAverages[b.id].cpu - hostsAverages[a.id].cpu
for (const vm of vms) {
// Search host with lower cpu usage in the same pool first. In other pool if necessary.
let destination = searchBestObject(find(hosts, host => host.$poolId === vm.$poolId), searchFunction)
let destination = searchBestObject(
find(hosts, host => host.$poolId === vm.$poolId),
searchFunction
)
if (!destination) {
destination = searchBestObject(hosts, searchFunction)
@ -110,7 +112,8 @@ export default class PerformancePlan extends Plan {
// Unable to move the vm.
if (
exceededAverages.cpu - vmAverages.cpu < destinationAverages.cpu + vmAverages.cpu ||
exceededAverages.cpu - vmAverages.cpu <
destinationAverages.cpu + vmAverages.cpu ||
destinationAverages.memoryFree > vmAverages.memory
) {
continue
@ -122,15 +125,27 @@ export default class PerformancePlan extends Plan {
exceededAverages.memoryFree += vmAverages.memory
destinationAverages.memoryFree -= vmAverages.memory
debug(`Migrate VM (${vm.id}) to Host (${destination.id}) from Host (${exceededHost.id}).`)
debug(
`Migrate VM (${vm.id}) to Host (${destination.id}) from Host (${
exceededHost.id
}).`
)
optimizationsCount++
promises.push(
xapiSrc.migrateVm(vm._xapiId, this.xo.getXapi(destination), destination._xapiId)
xapiSrc.migrateVm(
vm._xapiId,
this.xo.getXapi(destination),
destination._xapiId
)
)
}
await Promise.all(promises)
debug(`Performance mode: ${optimizationsCount} optimizations for Host (${exceededHost.id}).`)
debug(
`Performance mode: ${optimizationsCount} optimizations for Host (${
exceededHost.id
}).`
)
}
}

View File

@ -1,9 +1,6 @@
import { filter, includes, map as mapToArray } from 'lodash'
import {
EXECUTION_DELAY,
debug,
} from './utils'
import { EXECUTION_DELAY, debug } from './utils'
const MINUTES_OF_HISTORICAL_DATA = 30
@ -20,7 +17,7 @@ const LOW_THRESHOLD_FACTOR = 0.25
const HIGH_THRESHOLD_MEMORY_FREE_FACTOR = 1.25
const LOW_THRESHOLD_MEMORY_FREE_FACTOR = 20.0
const numberOrDefault = (value, def) => (value >= 0) ? value : def
const numberOrDefault = (value, def) => (value >= 0 ? value : def)
// ===================================================================
// Averages.
@ -69,10 +66,12 @@ function computeRessourcesAverageWithWeight (averages1, averages2, ratio) {
const averages = {}
for (const id in averages1) {
const objectAverages = averages[id] = {}
const objectAverages = (averages[id] = {})
for (const averageName in averages1[id]) {
objectAverages[averageName] = averages1[id][averageName] * ratio + averages2[id][averageName] * (1 - ratio)
objectAverages[averageName] =
averages1[id][averageName] * ratio +
averages2[id][averageName] * (1 - ratio)
}
}
@ -89,20 +88,24 @@ function setRealCpuAverageOfVms (vms, vmsAverages, nCpus) {
// ===================================================================
export default class Plan {
constructor (xo, name, poolIds, {
excludedHosts,
thresholds,
} = {}) {
constructor (xo, name, poolIds, { excludedHosts, thresholds } = {}) {
this.xo = xo
this._name = name
this._poolIds = poolIds
this._excludedHosts = excludedHosts
this._thresholds = {
cpu: {
critical: numberOrDefault(thresholds && thresholds.cpu, DEFAULT_CRITICAL_THRESHOLD_CPU),
critical: numberOrDefault(
thresholds && thresholds.cpu,
DEFAULT_CRITICAL_THRESHOLD_CPU
),
},
memoryFree: {
critical: numberOrDefault(thresholds && thresholds.memoryFree, DEFAULT_CRITICAL_THRESHOLD_MEMORY_FREE) * 1024,
critical:
numberOrDefault(
thresholds && thresholds.memoryFree,
DEFAULT_CRITICAL_THRESHOLD_MEMORY_FREE
) * 1024,
},
}
@ -143,8 +146,16 @@ export default class Plan {
}
// Check in the last 30 min interval with ratio.
const avgBefore = computeRessourcesAverage(hosts, hostsStats, MINUTES_OF_HISTORICAL_DATA)
const avgWithRatio = computeRessourcesAverageWithWeight(avgNow, avgBefore, 0.75)
const avgBefore = computeRessourcesAverage(
hosts,
hostsStats,
MINUTES_OF_HISTORICAL_DATA
)
const avgWithRatio = computeRessourcesAverageWithWeight(
avgNow,
avgBefore,
0.75
)
toOptimize = this._checkRessourcesThresholds(toOptimize, avgWithRatio)
@ -185,19 +196,23 @@ export default class Plan {
// Compute hosts for each pool. They can change over time.
_getHosts ({ powerState = 'Running' } = {}) {
return filter(this.xo.getObjects(), object => (
object.type === 'host' &&
includes(this._poolIds, object.$poolId) &&
object.power_state === powerState &&
!includes(this._excludedHosts, object.id)
))
return filter(
this.xo.getObjects(),
object =>
object.type === 'host' &&
includes(this._poolIds, object.$poolId) &&
object.power_state === powerState &&
!includes(this._excludedHosts, object.id)
)
}
async _getVms (hostId) {
return filter(this.xo.getObjects(), object =>
object.type === 'VM' &&
object.power_state === 'Running' &&
object.$container === hostId
return filter(
this.xo.getObjects(),
object =>
object.type === 'VM' &&
object.power_state === 'Running' &&
object.$container === hostId
)
}
@ -208,15 +223,17 @@ export default class Plan {
async _getHostsStats (hosts, granularity) {
const hostsStats = {}
await Promise.all(mapToArray(hosts, host =>
this.xo.getXapiHostStats(host, granularity).then(hostStats => {
hostsStats[host.id] = {
nPoints: hostStats.stats.cpus[0].length,
stats: hostStats.stats,
averages: {},
}
})
))
await Promise.all(
mapToArray(hosts, host =>
this.xo.getXapiHostStats(host, granularity).then(hostStats => {
hostsStats[host.id] = {
nPoints: hostStats.stats.cpus[0].length,
stats: hostStats.stats,
averages: {},
}
})
)
)
return hostsStats
}
@ -224,15 +241,17 @@ export default class Plan {
async _getVmsStats (vms, granularity) {
const vmsStats = {}
await Promise.all(mapToArray(vms, vm =>
this.xo.getXapiVmStats(vm, granularity).then(vmStats => {
vmsStats[vm.id] = {
nPoints: vmStats.stats.cpus[0].length,
stats: vmStats.stats,
averages: {},
}
})
))
await Promise.all(
mapToArray(vms, vm =>
this.xo.getXapiVmStats(vm, granularity).then(vmStats => {
vmsStats[vm.id] = {
nPoints: vmStats.stats.cpus[0].length,
stats: vmStats.stats,
averages: {},
}
})
)
)
return vmsStats
}

View File

@ -27,9 +27,7 @@ module.exports = {
debug: !__TEST__,
loose: true,
shippedProposals: true,
targets: __PROD__
? { node: nodeCompat }
: { node: 'current' },
targets: __PROD__ ? { node: nodeCompat } : { node: 'current' },
useBuiltIns: '@babel/polyfill' in (pkg.dependencies || {}) && 'usage',
},
],

View File

@ -91,9 +91,7 @@ const HOST_FUNCTIONS = {
unit: '% used',
comparator: '>',
createParser: (legend, threshold) => {
const memoryKBytesLegend = legend.find(
l => l.name === 'memory_total_kib'
)
const memoryKBytesLegend = legend.find(l => l.name === 'memory_total_kib')
const memoryKBytesFreeLegend = legend.find(
l => l.name === 'memory_free_kib'
)

View File

@ -37,7 +37,6 @@ exports.default = function (opts) {
// For simplicity's sake, this plugin returns a plain object, but
// usually it returns a new instance of an existing class.
return {
// This (optional) method is called each time the plugin is
// (re-)configured.
//

View File

@ -16,7 +16,10 @@ const removeUndefined = obj => {
const markdownCompiler = nodemailerMarkdown()
const logAndRethrow = error => {
console.error('[WARN] plugin transport-email:', (error && error.stack) || error)
console.error(
'[WARN] plugin transport-email:',
(error && error.stack) || error
)
throw error
}
@ -59,7 +62,7 @@ export const configurationSchema = {
},
secure: {
default: false,
enum: [ false, 'force', 'disabled', true ],
enum: [false, 'force', 'disabled', true],
enumNames: [
'auto (uses STARTTLS if available)',
'force (requires STARTTLS or fail)',
@ -70,7 +73,8 @@ export const configurationSchema = {
},
ignoreUnauthorized: {
type: 'boolean',
description: 'ignore certificates error (e.g. self-signed certificate)',
description:
'ignore certificates error (e.g. self-signed certificate)',
},
// FIXME: xo-web does not support edition of too nested
@ -138,18 +142,11 @@ class TransportEmailPlugin {
configure ({
from,
transport: {
ignoreUnauthorized,
password,
secure,
user,
...transportConf
},
transport: { ignoreUnauthorized, password, secure, user, ...transportConf },
}) {
if (ignoreUnauthorized != null) {
(
transportConf.tls ||
(transportConf.tls = {})
;(
transportConf.tls || (transportConf.tls = {})
).rejectUnauthorized = !ignoreUnauthorized
}
@ -159,11 +156,14 @@ class TransportEmailPlugin {
switch (secure) {
case true:
transportConf.secure = true; break
transportConf.secure = true
break
case 'disabled':
transportConf.ignoreTLS = true; break
transportConf.ignoreTLS = true
break
case 'required':
transportConf.requireTLS = true; break
transportConf.requireTLS = true
break
}
const transport = createTransport(transportConf, { from })
@ -180,7 +180,7 @@ class TransportEmailPlugin {
this._unset()
}
test ({to}) {
test ({ to }) {
return this._sendEmail({
to,
subject: '[Xen Orchestra] Test of transport-email plugin',
@ -188,29 +188,27 @@ class TransportEmailPlugin {
The transport-email plugin for Xen Orchestra server seems to be working fine, nicely done :)
`,
attachments: [ {
filename: 'example.txt',
content: 'Attachments are working too, great!\n',
} ],
attachments: [
{
filename: 'example.txt',
content: 'Attachments are working too, great!\n',
},
],
})
}
_sendEmail ({
from,
to, cc, bcc,
subject,
markdown,
attachments,
}) {
return this._send(removeUndefined({
from,
to,
cc,
bcc,
subject,
markdown,
attachments,
})).catch(logAndRethrow)
_sendEmail ({ from, to, cc, bcc, subject, markdown, attachments }) {
return this._send(
removeUndefined({
from,
to,
cc,
bcc,
subject,
markdown,
attachments,
})
).catch(logAndRethrow)
}
}

View File

@ -35,18 +35,12 @@ export const configurationSchema = {
// ===================================================================
const bind = (fn, thisArg) => function __bound__ () {
return fn.apply(thisArg, arguments)
}
const bind = (fn, thisArg) =>
function __bound__ () {
return fn.apply(thisArg, arguments)
}
function nscaPacketBuilder ({
host,
iv,
message,
service,
status,
timestamp,
}) {
function nscaPacketBuilder ({ host, iv, message, service, status, timestamp }) {
// Building NSCA packet
const SIZE = 720
const packet = Buffer.alloc(SIZE)
@ -112,15 +106,13 @@ class XoServerNagios {
test () {
return this._sendPassiveCheck({
message: 'The server-nagios plugin for Xen Orchestra server seems to be working fine, nicely done :)',
message:
'The server-nagios plugin for Xen Orchestra server seems to be working fine, nicely done :)',
status: OK,
})
}
_sendPassiveCheck ({
message,
status,
}) {
_sendPassiveCheck ({ message, status }) {
return new Promise((resolve, reject) => {
if (/\r|\n/.test(message)) {
throw new Error('the message must not contain a line break')
@ -145,13 +137,7 @@ class XoServerNagios {
// 1) Using xor between the NSCA packet and the initialization vector
// 2) Using xor between the result of the first operation and the encryption key
const xorPacketBuffer = xor(
xor(
packet,
iv
),
this._key
)
const xorPacketBuffer = xor(xor(packet, iv), this._key)
client.write(xorPacketBuffer, res => {
client.destroy()

View File

@ -4,7 +4,10 @@ import { promisify } from 'promise-toolbox'
// ===================================================================
const logAndRethrow = error => {
console.error('[WARN] plugin transport-slack:', (error != null && error.stack) || error)
console.error(
'[WARN] plugin transport-slack:',
(error != null && error.stack) || error
)
throw error
}
@ -48,10 +51,7 @@ class XoServerTransportSlack {
this._send = null
}
configure ({
webhookUri,
...conf
}) {
configure ({ webhookUri, ...conf }) {
const slack = new Slack()
slack.setWebhook(webhookUri)
this._conf = conf
@ -74,9 +74,7 @@ The transport-slack plugin for Xen Orchestra server seems to be working fine, ni
})
}
_sendSlack ({
message,
}) {
_sendSlack ({ message }) {
// TODO: handle errors
return this._send({ ...this._conf, text: message }).catch(logAndRethrow)
}

View File

@ -68,13 +68,15 @@ class TransportXmppPlugin {
this._unset = this._client = null
}
_sendToXmppClient ({to, message}) {
_sendToXmppClient ({ to, message }) {
for (const receiver of to) {
this._client.send(
new XmppClient.Stanza('message', {
to: receiver,
type: 'chat',
}).c('body').t(message)
})
.c('body')
.t(message)
)
}
}

View File

@ -14,13 +14,8 @@ import {
values,
zipObject,
} from 'lodash'
import {
promisify,
} from 'promise-toolbox'
import {
readFile,
writeFile,
} from 'fs'
import { promisify } from 'promise-toolbox'
import { readFile, writeFile } from 'fs'
// ===================================================================
@ -41,9 +36,9 @@ const mibPower = Math.pow(2, 20)
const kibPower = Math.pow(2, 10)
let template = null
pReadFile(`${__dirname}/../report.html.tpl`, 'utf8')
.then(tpl => {
template = Handlebars.compile(minify(tpl, {
pReadFile(`${__dirname}/../report.html.tpl`, 'utf8').then(tpl => {
template = Handlebars.compile(
minify(tpl, {
collapseBooleanAttributes: true,
collapseWhitespace: true,
minifyCSS: true,
@ -51,14 +46,14 @@ pReadFile(`${__dirname}/../report.html.tpl`, 'utf8')
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
}))
})
})
)
})
let imgXo = null
pReadFile(`${__dirname}/../images/xo.png`, 'base64')
.then(data => {
imgXo = `data:image/png;base64,${data}`
})
pReadFile(`${__dirname}/../images/xo.png`, 'base64').then(data => {
imgXo = `data:image/png;base64,${data}`
})
// ===================================================================
@ -75,26 +70,36 @@ export const configurationSchema = {
periodicity: {
type: 'string',
enum: ['monthly', 'weekly'],
description: 'If you choose weekly you will receive the report every sunday and if you choose monthly you will receive it every first day of the month.',
description:
'If you choose weekly you will receive the report every sunday and if you choose monthly you will receive it every first day of the month.',
},
},
additionalProperties: false,
required: [ 'emails', 'periodicity' ],
required: ['emails', 'periodicity'],
}
// ===================================================================
Handlebars.registerHelper('compare', function (lvalue, operator, rvalue, options) {
Handlebars.registerHelper('compare', function (
lvalue,
operator,
rvalue,
options
) {
if (arguments.length < 3) {
throw new Error('Handlerbars Helper "compare" needs 2 parameters')
}
if (!compareOperators[operator]) {
throw new Error(`Handlerbars Helper "compare" doesn't know the operator ${operator}`)
throw new Error(
`Handlerbars Helper "compare" doesn't know the operator ${operator}`
)
}
return compareOperators[operator](lvalue, rvalue) ? options.fn(this) : options.inverse(this)
return compareOperators[operator](lvalue, rvalue)
? options.fn(this)
: options.inverse(this)
})
Handlebars.registerHelper('math', function (lvalue, operator, rvalue, options) {
@ -103,7 +108,9 @@ Handlebars.registerHelper('math', function (lvalue, operator, rvalue, options) {
}
if (!mathOperators[operator]) {
throw new Error(`Handlerbars Helper "math" doesn't know the operator ${operator}`)
throw new Error(
`Handlerbars Helper "math" doesn't know the operator ${operator}`
)
}
return mathOperators[operator](+lvalue, +rvalue)
@ -135,24 +142,24 @@ const computeDoubleMean = val => computeMean(val.map(computeMean))
function computeMeans (objects, options) {
return zipObject(
options,
map(
options,
opt => round(computeMean(map(objects, opt)), 2)
)
map(options, opt => round(computeMean(map(objects, opt)), 2))
)
}
function getTop (objects, options) {
return zipObject(
options,
map(
options,
opt => map(
orderBy(objects, object => {
const value = object[opt]
map(options, opt =>
map(
orderBy(
objects,
object => {
const value = object[opt]
return isNaN(value) ? -Infinity : value
}, 'desc').slice(0, 3),
return isNaN(value) ? -Infinity : value
},
'desc'
).slice(0, 3),
obj => ({
uuid: obj.uuid,
name: obj.name,
@ -168,7 +175,10 @@ function conputePercentage (curr, prev, options) {
options,
map(
options,
opt => prev[opt] === 0 ? 'NONE' : `${round((curr[opt] - prev[opt]) * 100 / prev[opt], 2)}`
opt =>
prev[opt] === 0
? 'NONE'
: `${round((curr[opt] - prev[opt]) * 100 / prev[opt], 2)}`
)
)
}
@ -182,48 +192,42 @@ function getDiff (oldElements, newElements) {
// ===================================================================
function getVmsStats ({
runningVms,
xo,
}) {
return Promise.all(map(runningVms, async vm => {
const vmStats = await xo.getXapiVmStats(vm, 'days')
return {
uuid: vm.uuid,
name: vm.name_label,
cpu: computeDoubleMean(vmStats.stats.cpus),
ram: computeMean(vmStats.stats.memoryUsed) / gibPower,
diskRead: computeDoubleMean(values(vmStats.stats.xvds.r)) / mibPower,
diskWrite: computeDoubleMean(values(vmStats.stats.xvds.w)) / mibPower,
netReception: computeDoubleMean(vmStats.stats.vifs.rx) / kibPower,
netTransmission: computeDoubleMean(vmStats.stats.vifs.tx) / kibPower,
}
}))
function getVmsStats ({ runningVms, xo }) {
return Promise.all(
map(runningVms, async vm => {
const vmStats = await xo.getXapiVmStats(vm, 'days')
return {
uuid: vm.uuid,
name: vm.name_label,
cpu: computeDoubleMean(vmStats.stats.cpus),
ram: computeMean(vmStats.stats.memoryUsed) / gibPower,
diskRead: computeDoubleMean(values(vmStats.stats.xvds.r)) / mibPower,
diskWrite: computeDoubleMean(values(vmStats.stats.xvds.w)) / mibPower,
netReception: computeDoubleMean(vmStats.stats.vifs.rx) / kibPower,
netTransmission: computeDoubleMean(vmStats.stats.vifs.tx) / kibPower,
}
})
)
}
function getHostsStats ({
runningHosts,
xo,
}) {
return Promise.all(map(runningHosts, async host => {
const hostStats = await xo.getXapiHostStats(host, 'days')
return {
uuid: host.uuid,
name: host.name_label,
cpu: computeDoubleMean(hostStats.stats.cpus),
ram: computeMean(hostStats.stats.memoryUsed) / gibPower,
load: computeMean(hostStats.stats.load),
netReception: computeDoubleMean(hostStats.stats.pifs.rx) / kibPower,
netTransmission: computeDoubleMean(hostStats.stats.pifs.tx) / kibPower,
}
}))
function getHostsStats ({ runningHosts, xo }) {
return Promise.all(
map(runningHosts, async host => {
const hostStats = await xo.getXapiHostStats(host, 'days')
return {
uuid: host.uuid,
name: host.name_label,
cpu: computeDoubleMean(hostStats.stats.cpus),
ram: computeMean(hostStats.stats.memoryUsed) / gibPower,
load: computeMean(hostStats.stats.load),
netReception: computeDoubleMean(hostStats.stats.pifs.rx) / kibPower,
netTransmission: computeDoubleMean(hostStats.stats.pifs.tx) / kibPower,
}
})
)
}
function computeGlobalVmsStats ({
haltedVms,
vmsStats,
xo,
}) {
function computeGlobalVmsStats ({ haltedVms, vmsStats, xo }) {
const allVms = concat(
map(vmsStats, vm => ({
uuid: vm.uuid,
@ -235,17 +239,23 @@ function computeGlobalVmsStats ({
}))
)
return assign(computeMeans(vmsStats, ['cpu', 'ram', 'diskRead', 'diskWrite', 'netReception', 'netTransmission']), {
number: allVms.length,
allVms,
})
return assign(
computeMeans(vmsStats, [
'cpu',
'ram',
'diskRead',
'diskWrite',
'netReception',
'netTransmission',
]),
{
number: allVms.length,
allVms,
}
)
}
function computeGlobalHostsStats ({
haltedHosts,
hostsStats,
xo,
}) {
function computeGlobalHostsStats ({ haltedHosts, hostsStats, xo }) {
const allHosts = concat(
map(hostsStats, host => ({
uuid: host.uuid,
@ -257,52 +267,65 @@ function computeGlobalHostsStats ({
}))
)
return assign(computeMeans(hostsStats, ['cpu', 'ram', 'load', 'netReception', 'netTransmission']), {
number: allHosts.length,
allHosts,
})
}
function getTopVms ({
vmsStats,
xo,
}) {
return getTop(vmsStats, ['cpu', 'ram', 'diskRead', 'diskWrite', 'netReception', 'netTransmission'])
}
function getTopHosts ({
hostsStats,
xo,
}) {
return getTop(hostsStats, ['cpu', 'ram', 'load', 'netReception', 'netTransmission'])
}
function getMostAllocatedSpaces ({
disks,
xo,
}) {
return map(
orderBy(disks, ['size'], ['desc']).slice(0, 3), disk => ({
uuid: disk.uuid,
name: disk.name_label,
size: round(disk.size / gibPower, 2),
}))
}
async function getHostsMissingPatches ({
runningHosts,
xo,
}) {
const hostsMissingPatches = await Promise.all(map(runningHosts, async host => {
const hostsPatches = await xo.getXapi(host).listMissingPoolPatchesOnHost(host._xapiId)
if (hostsPatches.length > 0) {
return {
uuid: host.uuid,
name: host.name_label,
patches: map(hostsPatches, 'name'),
}
return assign(
computeMeans(hostsStats, [
'cpu',
'ram',
'load',
'netReception',
'netTransmission',
]),
{
number: allHosts.length,
allHosts,
}
)
}
function getTopVms ({ vmsStats, xo }) {
return getTop(vmsStats, [
'cpu',
'ram',
'diskRead',
'diskWrite',
'netReception',
'netTransmission',
])
}
function getTopHosts ({ hostsStats, xo }) {
return getTop(hostsStats, [
'cpu',
'ram',
'load',
'netReception',
'netTransmission',
])
}
function getMostAllocatedSpaces ({ disks, xo }) {
return map(orderBy(disks, ['size'], ['desc']).slice(0, 3), disk => ({
uuid: disk.uuid,
name: disk.name_label,
size: round(disk.size / gibPower, 2),
}))
}
async function getHostsMissingPatches ({ runningHosts, xo }) {
const hostsMissingPatches = await Promise.all(
map(runningHosts, async host => {
const hostsPatches = await xo
.getXapi(host)
.listMissingPoolPatchesOnHost(host._xapiId)
if (hostsPatches.length > 0) {
return {
uuid: host.uuid,
name: host.name_label,
patches: map(hostsPatches, 'name'),
}
}
})
)
return filter(hostsMissingPatches, host => host !== undefined)
}
@ -310,17 +333,11 @@ function getAllUsersEmail (users) {
return map(users, 'email')
}
async function storeStats ({
data,
storedStatsPath,
}) {
async function storeStats ({ data, storedStatsPath }) {
await pWriteFile(storedStatsPath, JSON.stringify(data))
}
async function computeEvolution ({
storedStatsPath,
...newStats
}) {
async function computeEvolution ({ storedStatsPath, ...newStats }) {
try {
const oldStats = JSON.parse(await pReadFile(storedStatsPath, 'utf8'))
const newStatsVms = newStats.vms
@ -332,16 +349,35 @@ async function computeEvolution ({
const vmsEvolution = {
number: newStatsVms.number - oldStatsVms.number,
...conputePercentage(newStatsVms, oldStatsVms, ['cpu', 'ram', 'diskRead', 'diskWrite', 'netReception', 'netTransmission']),
...conputePercentage(newStatsVms, oldStatsVms, [
'cpu',
'ram',
'diskRead',
'diskWrite',
'netReception',
'netTransmission',
]),
}
const hostsEvolution = {
number: newStatsHosts.number - oldStatsHosts.number,
...conputePercentage(newStatsHosts, oldStatsHosts, ['cpu', 'ram', 'load', 'netReception', 'netTransmission']),
...conputePercentage(newStatsHosts, oldStatsHosts, [
'cpu',
'ram',
'load',
'netReception',
'netTransmission',
]),
}
const vmsRessourcesEvolution = getDiff(oldStatsVms.allVms, newStatsVms.allVms)
const hostsRessourcesEvolution = getDiff(oldStatsHosts.allHosts, newStatsHosts.allHosts)
const vmsRessourcesEvolution = getDiff(
oldStatsVms.allVms,
newStatsVms.allVms
)
const hostsRessourcesEvolution = getDiff(
oldStatsHosts.allHosts,
newStatsHosts.allHosts
)
const usersEvolution = getDiff(oldStats.users, newStats.users)
@ -358,29 +394,41 @@ async function computeEvolution ({
}
}
async function dataBuilder ({
xo,
storedStatsPath,
}) {
async function dataBuilder ({ xo, storedStatsPath }) {
const xoObjects = values(xo.getObjects())
const runningVms = filter(xoObjects, {type: 'VM', power_state: 'Running'})
const haltedVms = filter(xoObjects, {type: 'VM', power_state: 'Halted'})
const runningHosts = filter(xoObjects, {type: 'host', power_state: 'Running'})
const haltedHosts = filter(xoObjects, {type: 'host', power_state: 'Halted'})
const disks = filter(xoObjects, {type: 'SR'})
const [users, vmsStats, hostsStats, topAllocation, hostsMissingPatches] = await Promise.all([
const runningVms = filter(xoObjects, { type: 'VM', power_state: 'Running' })
const haltedVms = filter(xoObjects, { type: 'VM', power_state: 'Halted' })
const runningHosts = filter(xoObjects, {
type: 'host',
power_state: 'Running',
})
const haltedHosts = filter(xoObjects, { type: 'host', power_state: 'Halted' })
const disks = filter(xoObjects, { type: 'SR' })
const [
users,
vmsStats,
hostsStats,
topAllocation,
hostsMissingPatches,
] = await Promise.all([
xo.getAllUsers(),
getVmsStats({xo, runningVms}),
getHostsStats({xo, runningHosts}),
getMostAllocatedSpaces({xo, disks}),
getHostsMissingPatches({xo, runningHosts}),
getVmsStats({ xo, runningVms }),
getHostsStats({ xo, runningHosts }),
getMostAllocatedSpaces({ xo, disks }),
getHostsMissingPatches({ xo, runningHosts }),
])
const [globalVmsStats, globalHostsStats, topVms, topHosts, usersEmail] = await Promise.all([
computeGlobalVmsStats({xo, vmsStats, haltedVms}),
computeGlobalHostsStats({xo, hostsStats, haltedHosts}),
getTopVms({xo, vmsStats}),
getTopHosts({xo, hostsStats}),
const [
globalVmsStats,
globalHostsStats,
topVms,
topHosts,
usersEmail,
] = await Promise.all([
computeGlobalVmsStats({ xo, vmsStats, haltedVms }),
computeGlobalHostsStats({ xo, hostsStats, haltedHosts }),
getTopVms({ xo, vmsStats }),
getTopHosts({ xo, hostsStats }),
getAllUsersEmail(users),
])
const evolution = await computeEvolution({
@ -419,7 +467,7 @@ async function dataBuilder ({
// ===================================================================
class UsageReportPlugin {
constructor ({xo, getDataDir}) {
constructor ({ xo, getDataDir }) {
this._xo = xo
this._dir = getDataDir
// Defined in configure().
@ -430,7 +478,8 @@ class UsageReportPlugin {
this._conf = configuration
this._job = new CronJob({
cronTime: configuration.periodicity === 'monthly' ? '00 06 1 * *' : '00 06 * * 0',
cronTime:
configuration.periodicity === 'monthly' ? '00 06 1 * *' : '00 06 * * 0',
onTick: () => this._sendReport(),
start: false,
})
@ -467,10 +516,12 @@ class UsageReportPlugin {
Please, find the attached report.
best regards.`,
attachments: [{
filename: `xoReport_${currDate}.html`,
content: template(data),
}],
attachments: [
{
filename: `xoReport_${currDate}.html`,
content: template(data),
},
],
}),
storeStats({
data,

View File

@ -8,25 +8,21 @@ try {
const filtered = frames.filter(function (frame) {
const name = frame && frame.getFileName()
return (
// has a filename
return (// has a filename
name &&
// contains a separator (no internal modules)
name.indexOf(sep) !== -1 &&
// does not start with `internal`
name.lastIndexOf('internal', 0) !== -1
)
name.lastIndexOf('internal', 0) !== -1)
})
// depd (used amongst other by express requires at least 3 frames
// in the stack.
return filtered.length > 2
? filtered
: frames
return filtered.length > 2 ? filtered : frames
})
} catch (_) {}
// Source maps.
try { require('julien-f-source-map-support/register') } catch (_) {}
try {
require('julien-f-source-map-support/register')
} catch (_) {}

View File

@ -14,11 +14,12 @@ export async function getCurrentPermissions () {
getCurrentPermissions.permission = ''
getCurrentPermissions.description = 'get (explicit) permissions by object for the current user'
getCurrentPermissions.description =
'get (explicit) permissions by object for the current user'
// -------------------------------------------------------------------
export async function add ({subject, object, action}) {
export async function add ({ subject, object, action }) {
await this.addAcl(subject, object, action)
}
@ -34,7 +35,7 @@ add.description = 'add a new ACL entry'
// -------------------------------------------------------------------
export async function remove ({subject, object, action}) {
export async function remove ({ subject, object, action }) {
await this.removeAcl(subject, object, action)
}

View File

@ -42,46 +42,57 @@ scanFiles.params = {
// -------------------------------------------------------------------
function handleFetchFiles (req, res, { remote, disk, partition, paths, format: archiveFormat }) {
this.fetchFilesInDiskBackup(remote, disk, partition, paths).then(files => {
res.setHeader('content-disposition', 'attachment')
res.setHeader('content-type', 'application/octet-stream')
function handleFetchFiles (
req,
res,
{ remote, disk, partition, paths, format: archiveFormat }
) {
this.fetchFilesInDiskBackup(remote, disk, partition, paths)
.then(files => {
res.setHeader('content-disposition', 'attachment')
res.setHeader('content-type', 'application/octet-stream')
const nFiles = paths.length
const nFiles = paths.length
// Send lone file directly
if (nFiles === 1) {
files[0].pipe(res)
return
}
// Send lone file directly
if (nFiles === 1) {
files[0].pipe(res)
return
}
const archive = archiver(archiveFormat)
archive.on('error', error => {
const archive = archiver(archiveFormat)
archive.on('error', error => {
console.error(error)
res.end(format.error(0, error))
})
forEach(files, file => {
archive.append(file, { name: basename(file.path) })
})
archive.finalize()
archive.pipe(res)
})
.catch(error => {
console.error(error)
res.writeHead(500)
res.end(format.error(0, error))
})
forEach(files, file => {
archive.append(file, { name: basename(file.path) })
})
archive.finalize()
archive.pipe(res)
}).catch(error => {
console.error(error)
res.writeHead(500)
res.end(format.error(0, error))
})
}
export async function fetchFiles ({ format = 'zip', ...params }) {
const fileName = params.paths.length > 1
? `restore_${new Date().toJSON()}.${format}`
: basename(params.paths[0])
const fileName =
params.paths.length > 1
? `restore_${new Date().toJSON()}.${format}`
: basename(params.paths[0])
return this.registerHttpRequest(handleFetchFiles, { ...params, format }, {
suffix: encodeURI(`/${fileName}`),
}).then(url => ({ $getFrom: url }))
return this.registerHttpRequest(
handleFetchFiles,
{ ...params, format },
{
suffix: encodeURI(`/${fileName}`),
}
).then(url => ({ $getFrom: url }))
}
fetchFiles.permission = 'admin'

View File

@ -8,9 +8,11 @@ export async function create ({ name, size, sr, vm, bootable, position, mode })
let resourceSet
if (attach && (resourceSet = vm.resourceSet) != null) {
await this.checkResourceSetConstraints(resourceSet, this.user.id, [ sr.id ])
await this.checkResourceSetConstraints(resourceSet, this.user.id, [sr.id])
await this.allocateLimitsInResourceSet({ disk: size }, resourceSet)
} else if (!(await this.hasPermissions(this.user.id, [ [ sr.id, 'administrate' ] ]))) {
} else if (
!await this.hasPermissions(this.user.id, [[sr.id, 'administrate']])
) {
throw unauthorized()
}

View File

@ -1,4 +1,4 @@
export async function register ({vm}) {
export async function register ({ vm }) {
await this.getXapi(vm).registerDockerContainer(vm._xapiId)
}
register.description = 'Register the VM for Docker management'
@ -13,7 +13,7 @@ register.resolve = {
// -----------------------------------------------------------------------------
export async function deregister ({vm}) {
export async function deregister ({ vm }) {
await this.getXapi(vm).unregisterDockerContainer(vm._xapiId)
}
deregister.description = 'Deregister the VM for Docker management'
@ -28,23 +28,23 @@ deregister.resolve = {
// -----------------------------------------------------------------------------
export async function start ({vm, container}) {
export async function start ({ vm, container }) {
await this.getXapi(vm).startDockerContainer(vm._xapiId, container)
}
export async function stop ({vm, container}) {
export async function stop ({ vm, container }) {
await this.getXapi(vm).stopDockerContainer(vm._xapiId, container)
}
export async function restart ({vm, container}) {
export async function restart ({ vm, container }) {
await this.getXapi(vm).restartDockerContainer(vm._xapiId, container)
}
export async function pause ({vm, container}) {
export async function pause ({ vm, container }) {
await this.getXapi(vm).pauseDockerContainer(vm._xapiId, container)
}
export async function unpause ({vm, container}) {
export async function unpause ({ vm, container }) {
await this.getXapi(vm).unpauseDockerContainer(vm._xapiId, container)
}

View File

@ -1,27 +1,27 @@
export async function create ({name}) {
return (await this.createGroup({name})).id
export async function create ({ name }) {
return (await this.createGroup({ name })).id
}
create.description = 'creates a new group'
create.permission = 'admin'
create.params = {
name: {type: 'string'},
name: { type: 'string' },
}
// -------------------------------------------------------------------
// Deletes an existing group.
async function delete_ ({id}) {
async function delete_ ({ id }) {
await this.deleteGroup(id)
}
// delete is not a valid identifier.
export {delete_ as delete}
export { delete_ as delete }
delete_.description = 'deletes an existing group'
delete_.permission = 'admin'
delete_.params = {
id: {type: 'string'},
id: { type: 'string' },
}
// -------------------------------------------------------------------
@ -36,35 +36,35 @@ getAll.permission = 'admin'
// -------------------------------------------------------------------
// sets group.users with an array of user ids
export async function setUsers ({id, userIds}) {
export async function setUsers ({ id, userIds }) {
await this.setGroupUsers(id, userIds)
}
setUsers.description = 'sets the users belonging to a group'
setUsers.permission = 'admin'
setUsers.params = {
id: {type: 'string'},
id: { type: 'string' },
userIds: {},
}
// -------------------------------------------------------------------
// adds the user id to group.users
export async function addUser ({id, userId}) {
export async function addUser ({ id, userId }) {
await this.addUserToGroup(userId, id)
}
addUser.description = 'adds a user to a group'
addUser.permission = 'admin'
addUser.params = {
id: {type: 'string'},
userId: {type: 'string'},
id: { type: 'string' },
userId: { type: 'string' },
}
// -------------------------------------------------------------------
// remove the user id from group.users
export async function removeUser ({id, userId}) {
export async function removeUser ({ id, userId }) {
await this.removeUserFromGroup(userId, id)
}
@ -73,14 +73,14 @@ export async function removeUser ({id, userId}) {
removeUser.description = 'removes a user from a group'
removeUser.permission = 'admin'
removeUser.params = {
id: {type: 'string'},
userId: {type: 'string'},
id: { type: 'string' },
userId: { type: 'string' },
}
// -------------------------------------------------------------------
export async function set ({id, name}) {
await this.updateGroup(id, {name})
export async function set ({ id, name }) {
await this.updateGroup(id, { name })
}
set.description = 'changes the properties of an existing group'

View File

@ -1,5 +1,4 @@
import {format} from 'json-rpc-peer'
import { format } from 'json-rpc-peer'
// ===================================================================
@ -58,7 +57,7 @@ restart.resolve = {
// -------------------------------------------------------------------
export function restartAgent ({host}) {
export function restartAgent ({ host }) {
return this.getXapi(host).restartHostAgent(host._xapiId)
}
@ -77,7 +76,7 @@ export { restartAgent as restart_agent } // eslint-disable-line camelcase
// -------------------------------------------------------------------
export function start ({host}) {
export function start ({ host }) {
return this.getXapi(host).powerOnHost(host._xapiId)
}
@ -93,7 +92,7 @@ start.resolve = {
// -------------------------------------------------------------------
export function stop ({host}) {
export function stop ({ host }) {
return this.getXapi(host).shutdownHost(host._xapiId)
}
@ -109,7 +108,7 @@ stop.resolve = {
// -------------------------------------------------------------------
export function detach ({host}) {
export function detach ({ host }) {
return this.getXapi(host).ejectHostFromPool(host._xapiId)
}
@ -125,7 +124,7 @@ detach.resolve = {
// -------------------------------------------------------------------
export function enable ({host}) {
export function enable ({ host }) {
return this.getXapi(host).enableHost(host._xapiId)
}
@ -141,7 +140,7 @@ enable.resolve = {
// -------------------------------------------------------------------
export function disable ({host}) {
export function disable ({ host }) {
return this.getXapi(host).disableHost(host._xapiId)
}
@ -157,7 +156,7 @@ disable.resolve = {
// -------------------------------------------------------------------
export function forget ({host}) {
export function forget ({ host }) {
return this.getXapi(host).forgetHost(host._xapiId)
}
@ -176,11 +175,12 @@ forget.resolve = {
// Returns an array of missing new patches in the host
// Returns an empty array if up-to-date
// Throws an error if the host is not running the latest XS version
export function listMissingPatches ({host}) {
export function listMissingPatches ({ host }) {
return this.getXapi(host).listMissingPoolPatchesOnHost(host._xapiId)
}
listMissingPatches.description = 'return an array of missing new patches in the host'
listMissingPatches.description =
'return an array of missing new patches in the host'
listMissingPatches.params = {
host: { type: 'string' },
@ -192,7 +192,7 @@ listMissingPatches.resolve = {
// -------------------------------------------------------------------
export function installPatch ({host, patch: patchUuid}) {
export function installPatch ({ host, patch: patchUuid }) {
return this.getXapi(host).installPoolPatchOnHost(patchUuid, host._xapiId)
}
@ -209,7 +209,7 @@ installPatch.resolve = {
// -------------------------------------------------------------------
export function installAllPatches ({host}) {
export function installAllPatches ({ host }) {
return this.getXapi(host).installAllPoolPatchesOnHost(host._xapiId)
}
@ -225,7 +225,7 @@ installAllPatches.resolve = {
// -------------------------------------------------------------------
export function emergencyShutdownHost ({host}) {
export function emergencyShutdownHost ({ host }) {
return this.getXapi(host).emergencyShutdownHost(host._xapiId)
}
@ -241,7 +241,7 @@ emergencyShutdownHost.resolve = {
// -------------------------------------------------------------------
export function stats ({host, granularity}) {
export function stats ({ host, granularity }) {
return this.getXapiHostStats(host, granularity)
}
@ -278,9 +278,11 @@ async function handleInstallSupplementalPack (req, res, { hostId }) {
}
}
export async function installSupplementalPack ({host}) {
export async function installSupplementalPack ({ host }) {
return {
$sendTo: (await this.registerHttpRequest(handleInstallSupplementalPack, { hostId: host.id })),
$sendTo: await this.registerHttpRequest(handleInstallSupplementalPack, {
hostId: host.id,
}),
}
}

View File

@ -26,9 +26,8 @@ export function getAll (params) {
throw unauthorized()
}
return this.getAllIpPools(user.permission === 'admin'
? params && params.userId
: user.id
return this.getAllIpPools(
user.permission === 'admin' ? params && params.userId : user.id
)
}

View File

@ -14,10 +14,10 @@ export async function get (id) {
get.permission = 'admin'
get.description = 'Gets an existing job'
get.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export async function create ({job}) {
export async function create ({ job }) {
if (!job.userId) {
job.userId = this.session.get('user_id')
}
@ -31,16 +31,16 @@ create.params = {
job: {
type: 'object',
properties: {
userId: {type: 'string', optional: true},
name: {type: 'string', optional: true},
timeout: {type: 'number', optional: true},
type: {type: 'string'},
key: {type: 'string'},
method: {type: 'string'},
userId: { type: 'string', optional: true },
name: { type: 'string', optional: true },
timeout: { type: 'number', optional: true },
type: { type: 'string' },
key: { type: 'string' },
method: { type: 'string' },
paramsVector: {
type: 'object',
properties: {
type: {type: 'string'},
type: { type: 'string' },
items: {
type: 'array',
items: {
@ -54,7 +54,7 @@ create.params = {
},
}
export async function set ({job}) {
export async function set ({ job }) {
await this.updateJob(job)
}
@ -64,16 +64,16 @@ set.params = {
job: {
type: 'object',
properties: {
id: {type: 'string'},
name: {type: 'string', optional: true},
timeout: {type: ['number', 'null'], optional: true},
type: {type: 'string', optional: true},
key: {type: 'string', optional: true},
method: {type: 'string', optional: true},
id: { type: 'string' },
name: { type: 'string', optional: true },
timeout: { type: ['number', 'null'], optional: true },
type: { type: 'string', optional: true },
key: { type: 'string', optional: true },
method: { type: 'string', optional: true },
paramsVector: {
type: 'object',
properties: {
type: {type: 'string'},
type: { type: 'string' },
items: {
type: 'array',
items: {
@ -87,24 +87,24 @@ set.params = {
},
}
async function delete_ ({id}) {
async function delete_ ({ id }) {
await this.removeJob(id)
}
delete_.permission = 'admin'
delete_.description = 'Deletes an existing job'
delete_.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export {delete_ as delete}
export { delete_ as delete }
export async function runSequence ({idSequence}) {
export async function runSequence ({ idSequence }) {
await this.runJobSequence(idSequence)
}
runSequence.permission = 'admin'
runSequence.description = 'Runs jobs sequentially, in the provided order'
runSequence.params = {
idSequence: {type: 'array', items: {type: 'string'}},
idSequence: { type: 'array', items: { type: 'string' } },
}

View File

@ -1,11 +1,12 @@
export async function get ({namespace}) {
export async function get ({ namespace }) {
const logger = await this.getLogger(namespace)
return new Promise((resolve, reject) => {
const logs = {}
logger.createReadStream()
.on('data', (data) => {
logger
.createReadStream()
.on('data', data => {
logs[data.key] = data.value
})
.on('end', () => {
@ -23,16 +24,16 @@ get.permission = 'admin'
// -------------------------------------------------------------------
async function delete_ ({namespace, id}) {
async function delete_ ({ namespace, id }) {
const logger = await this.getLogger(namespace)
logger.del(id)
}
delete_.description = 'deletes one or several logs from a namespace'
delete_.params = {
id: { type: [ 'array', 'string' ] },
id: { type: ['array', 'string'] },
namespace: { type: 'string' },
}
delete_.permission = 'admin'
export {delete_ as delete}
export { delete_ as delete }

View File

@ -1,7 +1,7 @@
async function delete_ ({ message }) {
await this.getXapi(message).call('message.destroy', message._xapiRef)
}
export {delete_ as delete}
export { delete_ as delete }
delete_.params = {
id: { type: 'string' },

View File

@ -4,7 +4,14 @@ export function getBondModes () {
return ['balance-slb', 'active-backup', 'lacp']
}
export async function create ({ pool, name, description, pif, mtu = 1500, vlan = 0 }) {
export async function create ({
pool,
name,
description,
pif,
mtu = 1500,
vlan = 0,
}) {
return this.getXapi(pool).createNetwork({
name,
description,
@ -30,13 +37,19 @@ create.permission = 'admin'
// =================================================================
export async function createBonded ({ pool, name, description, pifs, mtu = 1500, mac, bondMode }) {
export async function createBonded ({
pool,
name,
description,
pifs,
mtu = 1500,
mac,
bondMode,
}) {
return this.getXapi(pool).createBondedNetwork({
name,
description,
pifIds: mapToArray(pifs, pif =>
this.getObject(pif, 'PIF')._xapiId
),
pifIds: mapToArray(pifs, pif => this.getObject(pif, 'PIF')._xapiId),
mtu: +mtu,
mac,
bondMode,
@ -56,14 +69,18 @@ createBonded.params = {
mtu: { type: ['integer', 'string'], optional: true },
mac: { type: 'string', optional: true },
// RegExp since schema-inspector does not provide a param check based on an enumeration
bondMode: { type: 'string', pattern: new RegExp(`^(${getBondModes().join('|')})$`) },
bondMode: {
type: 'string',
pattern: new RegExp(`^(${getBondModes().join('|')})$`),
},
}
createBonded.resolve = {
pool: ['pool', 'pool', 'administrate'],
}
createBonded.permission = 'admin'
createBonded.description = 'Create a bonded network. bondMode can be balance-slb, active-backup or lacp'
createBonded.description =
'Create a bonded network. bondMode can be balance-slb, active-backup or lacp'
// ===================================================================
@ -109,7 +126,7 @@ set.resolve = {
export async function delete_ ({ network }) {
return this.getXapi(network).deleteNetwork(network._xapiId)
}
export {delete_ as delete}
export { delete_ as delete }
delete_.params = {
id: { type: 'string' },

View File

@ -3,11 +3,11 @@
// ===================================================================
// Delete
async function delete_ ({PBD}) {
async function delete_ ({ PBD }) {
// TODO: check if PBD is attached before
await this.getXapi(PBD).call('PBD.destroy', PBD._xapiRef)
}
export {delete_ as delete}
export { delete_ as delete }
delete_.params = {
id: { type: 'string' },
@ -35,7 +35,7 @@ disconnect.resolve = {
// ===================================================================
// Connect
export async function connect ({PBD}) {
export async function connect ({ PBD }) {
// TODO: check if PBD is attached before
await this.getXapi(PBD).call('PBD.plug', PBD._xapiRef)
}

View File

@ -13,11 +13,11 @@ export function getIpv6ConfigurationModes () {
// ===================================================================
// Delete
async function delete_ ({pif}) {
async function delete_ ({ pif }) {
// TODO: check if PIF is attached before
await this.getXapi(pif).call('PIF.destroy', pif._xapiRef)
}
export {delete_ as delete}
export { delete_ as delete }
delete_.params = {
id: { type: 'string' },
@ -30,7 +30,7 @@ delete_.resolve = {
// ===================================================================
// Disconnect
export async function disconnect ({pif}) {
export async function disconnect ({ pif }) {
// TODO: check if PIF is attached before
await this.getXapi(pif).call('PIF.unplug', pif._xapiRef)
}
@ -45,7 +45,7 @@ disconnect.resolve = {
// ===================================================================
// Connect
export async function connect ({pif}) {
export async function connect ({ pif }) {
// TODO: check if PIF is attached before
await this.getXapi(pif).call('PIF.plug', pif._xapiRef)
}
@ -60,8 +60,23 @@ connect.resolve = {
// ===================================================================
// Reconfigure IP
export async function reconfigureIp ({ pif, mode = 'DHCP', ip = '', netmask = '', gateway = '', dns = '' }) {
await this.getXapi(pif).call('PIF.reconfigure_ip', pif._xapiRef, mode, ip, netmask, gateway, dns)
export async function reconfigureIp ({
pif,
mode = 'DHCP',
ip = '',
netmask = '',
gateway = '',
dns = '',
}) {
await this.getXapi(pif).call(
'PIF.reconfigure_ip',
pif._xapiRef,
mode,
ip,
netmask,
gateway,
dns
)
}
reconfigureIp.params = {

View File

@ -38,7 +38,7 @@ set.resolve = {
// -------------------------------------------------------------------
export async function setDefaultSr ({ sr }) {
await this.hasPermissions(this.user.id, [ [ sr.$pool, 'administrate' ] ])
await this.hasPermissions(this.user.id, [[sr.$pool, 'administrate']])
await this.getXapi(sr).setDefaultSr(sr._xapiId)
}
@ -58,7 +58,7 @@ setDefaultSr.resolve = {
// -------------------------------------------------------------------
export async function setPoolMaster ({ host }) {
await this.hasPermissions(this.user.id, [ [ host.$pool, 'administrate' ] ])
await this.hasPermissions(this.user.id, [[host.$pool, 'administrate']])
await this.getXapi(host).setPoolMaster(host._xapiId)
}
@ -75,7 +75,7 @@ setPoolMaster.resolve = {
// -------------------------------------------------------------------
export async function installPatch ({pool, patch: patchUuid}) {
export async function installPatch ({ pool, patch: patchUuid }) {
await this.getXapi(pool).installPoolPatchOnAllHosts(patchUuid)
}
@ -107,11 +107,12 @@ installAllPatches.resolve = {
pool: ['pool', 'pool', 'administrate'],
}
installAllPatches.description = 'Install automatically all patches for every hosts of a pool'
installAllPatches.description =
'Install automatically all patches for every hosts of a pool'
// -------------------------------------------------------------------
async function handlePatchUpload (req, res, {pool}) {
async function handlePatchUpload (req, res, { pool }) {
const contentLength = req.headers['content-length']
if (!contentLength) {
res.writeHead(411)
@ -122,9 +123,9 @@ async function handlePatchUpload (req, res, {pool}) {
await this.getXapi(pool).uploadPoolPatch(req, contentLength)
}
export async function uploadPatch ({pool}) {
export async function uploadPatch ({ pool }) {
return {
$sendTo: await this.registerHttpRequest(handlePatchUpload, {pool}),
$sendTo: await this.registerHttpRequest(handlePatchUpload, { pool }),
}
}
@ -139,7 +140,7 @@ uploadPatch.resolve = {
// Compatibility
//
// TODO: remove when no longer used in xo-web
export {uploadPatch as patch}
export { uploadPatch as patch }
// -------------------------------------------------------------------
@ -177,11 +178,8 @@ mergeInto.resolve = {
// -------------------------------------------------------------------
export async function getLicenseState ({pool}) {
return this.getXapi(pool).call(
'pool.get_license_state',
pool._xapiId.$ref
)
export async function getLicenseState ({ pool }) {
return this.getXapi(pool).call('pool.get_license_state', pool._xapiId.$ref)
}
getLicenseState.params = {
@ -215,11 +213,14 @@ async function handleInstallSupplementalPack (req, res, { poolId }) {
export async function installSupplementalPack ({ pool }) {
return {
$sendTo: await this.registerHttpRequest(handleInstallSupplementalPack, { poolId: pool.id }),
$sendTo: await this.registerHttpRequest(handleInstallSupplementalPack, {
poolId: pool.id,
}),
}
}
installSupplementalPack.description = 'installs supplemental pack from ISO file on all hosts'
installSupplementalPack.description =
'installs supplemental pack from ISO file on all hosts'
installSupplementalPack.params = {
pool: { type: 'string' },

View File

@ -5,68 +5,68 @@ export async function getAll () {
getAll.permission = 'admin'
getAll.description = 'Gets all existing fs remote points'
export async function get ({id}) {
export async function get ({ id }) {
return this.getRemote(id)
}
get.permission = 'admin'
get.description = 'Gets an existing fs remote point'
get.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export async function test ({id}) {
export async function test ({ id }) {
return this.testRemote(id)
}
test.permission = 'admin'
test.description = 'Performs a read/write matching test on a remote point'
test.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export async function list ({id}) {
export async function list ({ id }) {
return this.listRemoteBackups(id)
}
list.permission = 'admin'
list.description = 'Lists the files found in a remote point'
list.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export async function create ({name, url}) {
return this.createRemote({name, url})
export async function create ({ name, url }) {
return this.createRemote({ name, url })
}
create.permission = 'admin'
create.description = 'Creates a new fs remote point'
create.params = {
name: {type: 'string'},
url: {type: 'string'},
name: { type: 'string' },
url: { type: 'string' },
}
export async function set ({id, name, url, enabled}) {
await this.updateRemote(id, {name, url, enabled})
export async function set ({ id, name, url, enabled }) {
await this.updateRemote(id, { name, url, enabled })
}
set.permission = 'admin'
set.description = 'Modifies an existing fs remote point'
set.params = {
id: {type: 'string'},
name: {type: 'string', optional: true},
url: {type: 'string', optional: true},
enabled: {type: 'boolean', optional: true},
id: { type: 'string' },
name: { type: 'string', optional: true },
url: { type: 'string', optional: true },
enabled: { type: 'boolean', optional: true },
}
async function delete_ ({id}) {
async function delete_ ({ id }) {
await this.removeRemote(id)
}
delete_.permission = 'admin'
delete_.description = 'Deletes an existing fs remote point'
delete_.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export {delete_ as delete}
export { delete_ as delete }

View File

@ -1,6 +1,4 @@
import {
unauthorized,
} from 'xo-common/api-errors'
import { unauthorized } from 'xo-common/api-errors'
// ===================================================================
@ -237,4 +235,5 @@ export function recomputeAllLimits () {
}
recomputeAllLimits.permission = 'admin'
recomputeAllLimits.description = 'Recompute manually the current resource set usage'
recomputeAllLimits.description =
'Recompute manually the current resource set usage'

View File

@ -14,20 +14,26 @@ export async function get (id) {
get.permission = 'admin'
get.description = 'Gets an existing schedule'
get.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export async function create ({ jobId, cron, enabled, name, timezone }) {
return /* await */ this.createSchedule(this.session.get('user_id'), { job: jobId, cron, enabled, name, timezone })
return /* await */ this.createSchedule(this.session.get('user_id'), {
job: jobId,
cron,
enabled,
name,
timezone,
})
}
create.permission = 'admin'
create.description = 'Creates a new schedule'
create.params = {
jobId: {type: 'string'},
cron: {type: 'string'},
enabled: {type: 'boolean', optional: true},
name: {type: 'string', optional: true},
jobId: { type: 'string' },
cron: { type: 'string' },
enabled: { type: 'boolean', optional: true },
name: { type: 'string', optional: true },
}
export async function set ({ id, jobId, cron, enabled, name, timezone }) {
@ -37,21 +43,21 @@ export async function set ({ id, jobId, cron, enabled, name, timezone }) {
set.permission = 'admin'
set.description = 'Modifies an existing schedule'
set.params = {
id: {type: 'string'},
jobId: {type: 'string', optional: true},
cron: {type: 'string', optional: true},
enabled: {type: 'boolean', optional: true},
name: {type: 'string', optional: true},
id: { type: 'string' },
jobId: { type: 'string', optional: true },
cron: { type: 'string', optional: true },
enabled: { type: 'boolean', optional: true },
name: { type: 'string', optional: true },
}
async function delete_ ({id}) {
async function delete_ ({ id }) {
await this.removeSchedule(id)
}
delete_.permission = 'admin'
delete_.description = 'Deletes an existing schedule'
delete_.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export {delete_ as delete}
export { delete_ as delete }

View File

@ -1,16 +1,16 @@
export async function enable ({id}) {
export async function enable ({ id }) {
const schedule = await this.getSchedule(id)
schedule.enabled = true
await this.updateSchedule(id, schedule)
}
enable.permission = 'admin'
enable.description = 'Enables a schedule to run it\'s job as scheduled'
enable.description = "Enables a schedule to run it's job as scheduled"
enable.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export async function disable ({id}) {
export async function disable ({ id }) {
const schedule = await this.getSchedule(id)
schedule.enabled = false
await this.updateSchedule(id, schedule)
@ -19,7 +19,7 @@ export async function disable ({id}) {
disable.permission = 'admin'
disable.description = 'Disables a schedule'
disable.params = {
id: {type: 'string'},
id: { type: 'string' },
}
export function getScheduleTable () {

View File

@ -1,10 +1,10 @@
import { ignoreErrors } from 'promise-toolbox'
export async function add ({autoConnect = true, ...props}) {
export async function add ({ autoConnect = true, ...props }) {
const server = await this.registerXenServer(props)
if (autoConnect) {
this.connectXenServer(server.id)::ignoreErrors()
;this.connectXenServer(server.id)::ignoreErrors()
}
return server.id
@ -40,7 +40,7 @@ add.params = {
// -------------------------------------------------------------------
export async function remove ({id}) {
export async function remove ({ id }) {
await this.unregisterXenServer(id)
}
@ -68,7 +68,7 @@ getAll.permission = 'admin'
// -------------------------------------------------------------------
export async function set ({id, ...props}) {
export async function set ({ id, ...props }) {
await this.updateXenServer(id, props)
}
@ -104,8 +104,8 @@ set.params = {
// -------------------------------------------------------------------
export async function connect ({id}) {
this.updateXenServer(id, {enabled: true})::ignoreErrors()
export async function connect ({ id }) {
;this.updateXenServer(id, { enabled: true })::ignoreErrors()
await this.connectXenServer(id)
}
@ -121,8 +121,8 @@ connect.params = {
// -------------------------------------------------------------------
export async function disconnect ({id}) {
this.updateXenServer(id, {enabled: false})::ignoreErrors()
export async function disconnect ({ id }) {
;this.updateXenServer(id, { enabled: false })::ignoreErrors()
await this.disconnectXenServer(id)
}

View File

@ -1,7 +1,7 @@
import {deprecate} from 'util'
import { deprecate } from 'util'
import { getUserPublicProperties } from '../utils'
import {invalidCredentials} from 'xo-common/api-errors'
import { invalidCredentials } from 'xo-common/api-errors'
// ===================================================================
@ -19,7 +19,10 @@ signIn.description = 'sign in'
// -------------------------------------------------------------------
export const signInWithPassword = deprecate(signIn, 'use session.signIn() instead')
export const signInWithPassword = deprecate(
signIn,
'use session.signIn() instead'
)
signInWithPassword.params = {
email: { type: 'string' },

View File

@ -1,12 +1,7 @@
import { some } from 'lodash'
import { asInteger } from '../xapi/utils'
import {
asyncMap,
ensureArray,
forEach,
parseXml,
} from '../utils'
import { asyncMap, ensureArray, forEach, parseXml } from '../utils'
// ===================================================================
@ -50,10 +45,11 @@ scan.resolve = {
}
// -------------------------------------------------------------------
const srIsBackingHa = (sr) => sr.$pool.ha_enabled && some(sr.$pool.$ha_statefiles, f => f.$SR === sr)
const srIsBackingHa = sr =>
sr.$pool.ha_enabled && some(sr.$pool.$ha_statefiles, f => f.$SR === sr)
// TODO: find a way to call this "delete" and not destroy
export async function destroy ({sr}) {
export async function destroy ({ sr }) {
const xapi = this.getXapi(sr)
if (sr.SR_type !== 'xosan') {
await xapi.destroySr(sr._xapiId)
@ -61,7 +57,9 @@ export async function destroy ({sr}) {
}
const xapiSr = xapi.getObject(sr)
if (srIsBackingHa(xapiSr)) {
throw new Error('You tried to remove a SR the High Availability is relying on. Please disable HA first.')
throw new Error(
'You tried to remove a SR the High Availability is relying on. Please disable HA first.'
)
}
const config = xapi.xo.getData(sr, 'xosan_config')
// we simply forget because the hosted disks are being destroyed with the VMs
@ -239,12 +237,7 @@ createNfs.resolve = {
// This functions creates an HBA SR
export async function createHba ({
host,
nameLabel,
nameDescription,
scsiId,
}) {
export async function createHba ({ host, nameLabel, nameDescription, scsiId }) {
const xapi = this.getXapi(host)
const deviceConfig = {
@ -284,12 +277,7 @@ createHba.resolve = {
// This functions creates a local LVM SR
export async function createLvm ({
host,
nameLabel,
nameDescription,
device,
}) {
export async function createLvm ({ host, nameLabel, nameDescription, device }) {
const xapi = this.getXapi(host)
const deviceConfig = {
@ -328,10 +316,7 @@ createLvm.resolve = {
// This function helps to detect all NFS shares (exports) on a NFS server
// Return a table of exports with their paths and ACLs
export async function probeNfs ({
host,
server,
}) {
export async function probeNfs ({ host, server }) {
const xapi = this.getXapi(host)
const deviceConfig = {
@ -341,13 +326,7 @@ export async function probeNfs ({
let xml
try {
await xapi.call(
'SR.probe',
host._xapiRef,
deviceConfig,
'nfs',
{}
)
await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'nfs', {})
throw new Error('the call above should have thrown an error')
} catch (error) {
@ -381,20 +360,13 @@ probeNfs.resolve = {
// -------------------------------------------------------------------
// This function helps to detect all HBA devices on the host
export async function probeHba ({
host,
}) {
export async function probeHba ({ host }) {
const xapi = this.getXapi(host)
let xml
try {
await xapi.call(
'SR.probe',
host._xapiRef,
'type',
{}
)
await xapi.call('SR.probe', host._xapiRef, 'type', {})
throw new Error('the call above should have thrown an error')
} catch (error) {
@ -527,13 +499,7 @@ export async function probeIscsiIqns ({
let xml
try {
await xapi.call(
'SR.probe',
host._xapiRef,
deviceConfig,
'lvmoiscsi',
{}
)
await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'lvmoiscsi', {})
throw new Error('the call above should have thrown an error')
} catch (error) {
@ -605,13 +571,7 @@ export async function probeIscsiLuns ({
let xml
try {
await xapi.call(
'SR.probe',
host._xapiRef,
deviceConfig,
'lvmoiscsi',
{}
)
await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'lvmoiscsi', {})
throw new Error('the call above should have thrown an error')
} catch (error) {
@ -681,7 +641,9 @@ export async function probeIscsiExists ({
deviceConfig.port = asInteger(port)
}
const xml = parseXml(await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'lvmoiscsi', {}))
const xml = parseXml(
await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'lvmoiscsi', {})
)
const srs = []
forEach(ensureArray(xml['SRlist'].SR), sr => {
@ -710,11 +672,7 @@ probeIscsiExists.resolve = {
// This function helps to detect if this NFS SR already exists in XAPI
// It returns a table of SR UUID, empty if no existing connections
export async function probeNfsExists ({
host,
server,
serverPath,
}) {
export async function probeNfsExists ({ host, server, serverPath }) {
const xapi = this.getXapi(host)
const deviceConfig = {
@ -722,7 +680,9 @@ export async function probeNfsExists ({
serverpath: serverPath,
}
const xml = parseXml(await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'nfs', {}))
const xml = parseXml(
await xapi.call('SR.probe', host._xapiRef, deviceConfig, 'nfs', {})
)
const srs = []

View File

@ -20,7 +20,8 @@ export function getMethodsInfo () {
return methods
}
getMethodsInfo.description = 'returns the signatures of all available API methods'
getMethodsInfo.description =
'returns the signatures of all available API methods'
// -------------------------------------------------------------------
@ -46,7 +47,7 @@ listMethods.description = 'returns the name of all available API methods'
// -------------------------------------------------------------------
export function methodSignature ({method: name}) {
export function methodSignature ({ method: name }) {
const method = this.apiMethods[name]
if (!method) {

View File

@ -1,4 +1,4 @@
export async function add ({tag, object}) {
export async function add ({ tag, object }) {
await this.getXapi(object).addTag(object._xapiId, tag)
}
@ -15,7 +15,7 @@ add.params = {
// -------------------------------------------------------------------
export async function remove ({tag, object}) {
export async function remove ({ tag, object }) {
await this.getXapi(object).removeTag(object._xapiId, tag)
}

View File

@ -1,4 +1,4 @@
export async function cancel ({task}) {
export async function cancel ({ task }) {
await this.getXapi(task).call('task.cancel', task._xapiRef)
}
@ -12,7 +12,7 @@ cancel.resolve = {
// -------------------------------------------------------------------
export async function destroy ({task}) {
export async function destroy ({ task }) {
await this.getXapi(task).call('task.destroy', task._xapiRef)
}

View File

@ -13,9 +13,7 @@ getPermissionsForUser.params = {
// -------------------------------------------------------------------
export function hasPermission ({ userId, objectId, permission }) {
return this.hasPermissions(userId, [
[ objectId, permission ],
])
return this.hasPermissions(userId, [[objectId, permission]])
}
hasPermission.permission = 'admin'
@ -34,7 +32,7 @@ hasPermission.params = {
// -------------------------------------------------------------------
export function wait ({duration, returnValue}) {
export function wait ({ duration, returnValue }) {
return new Promise(resolve => {
setTimeout(() => {
resolve(returnValue)
@ -81,6 +79,6 @@ copyVm.params = {
}
copyVm.resolve = {
vm: [ 'vm', 'VM' ],
sr: [ 'sr', 'SR' ],
vm: ['vm', 'VM'],
sr: ['sr', 'SR'],
}

View File

@ -12,7 +12,7 @@ create.description = 'create a new authentication token'
create.params = {
expiresIn: {
optional: true,
type: [ 'number', 'string' ],
type: ['number', 'string'],
},
}
@ -21,11 +21,11 @@ create.permission = '' // sign in
// -------------------------------------------------------------------
// TODO: an user should be able to delete its own tokens.
async function delete_ ({token: id}) {
async function delete_ ({ token: id }) {
await this.deleteAuthenticationToken(id)
}
export {delete_ as delete}
export { delete_ as delete }
delete_.description = 'delete an existing authentication token'

View File

@ -1,10 +1,10 @@
import {invalidParameters} from 'xo-common/api-errors'
import { invalidParameters } from 'xo-common/api-errors'
import { getUserPublicProperties, mapToArray } from '../utils'
// ===================================================================
export async function create ({email, password, permission}) {
return (await this.createUser({email, password, permission})).id
export async function create ({ email, password, permission }) {
return (await this.createUser({ email, password, permission })).id
}
create.description = 'creates a new user'
@ -20,7 +20,7 @@ create.params = {
// -------------------------------------------------------------------
// Deletes an existing user.
async function delete_ ({id}) {
async function delete_ ({ id }) {
if (id === this.session.get('user_id')) {
throw invalidParameters('a user cannot delete itself')
}
@ -29,7 +29,7 @@ async function delete_ ({id}) {
}
// delete is not a valid identifier.
export {delete_ as delete}
export { delete_ as delete }
delete_.description = 'deletes an existing user'
@ -57,17 +57,19 @@ getAll.permission = 'admin'
// -------------------------------------------------------------------
export async function set ({id, email, password, permission, preferences}) {
export async function set ({ id, email, password, permission, preferences }) {
const isAdmin = this.user && this.user.permission === 'admin'
if (isAdmin) {
if (permission && id === this.session.get('user_id')) {
throw invalidParameters('a user cannot change its own permission')
}
} else if (email || password || permission) {
throw invalidParameters('this properties can only changed by an administrator')
throw invalidParameters(
'this properties can only changed by an administrator'
)
}
await this.updateUser(id, {email, password, permission, preferences})
await this.updateUser(id, { email, password, permission, preferences })
}
set.description = 'changes the properties of an existing user'
@ -84,16 +86,17 @@ set.params = {
// -------------------------------------------------------------------
export async function changePassword ({oldPassword, newPassword}) {
export async function changePassword ({ oldPassword, newPassword }) {
const id = this.session.get('user_id')
await this.changeUserPassword(id, oldPassword, newPassword)
}
changePassword.description = 'change password after checking old password (user function)'
changePassword.description =
'change password after checking old password (user function)'
changePassword.permission = ''
changePassword.params = {
oldPassword: {type: 'string'},
newPassword: {type: 'string'},
oldPassword: { type: 'string' },
newPassword: { type: 'string' },
}

View File

@ -1,6 +1,6 @@
// FIXME: too low level, should be removed.
async function delete_ ({vbd}) {
async function delete_ ({ vbd }) {
await this.getXapi(vbd).deleteVbd(vbd)
}
@ -16,7 +16,7 @@ export { delete_ as delete }
// -------------------------------------------------------------------
export async function disconnect ({vbd}) {
export async function disconnect ({ vbd }) {
const xapi = this.getXapi(vbd)
await xapi.disconnectVbd(vbd._xapiRef)
}
@ -31,7 +31,7 @@ disconnect.resolve = {
// -------------------------------------------------------------------
export async function connect ({vbd}) {
export async function connect ({ vbd }) {
const xapi = this.getXapi(vbd)
await xapi.connectVbd(vbd._xapiRef)
}
@ -46,7 +46,7 @@ connect.resolve = {
// -------------------------------------------------------------------
export async function set ({position, vbd}) {
export async function set ({ position, vbd }) {
if (position !== undefined) {
const xapi = this.getXapi(vbd)
await xapi.call('VBD.set_userdevice', vbd._xapiRef, String(position))
@ -66,7 +66,7 @@ set.resolve = {
// -------------------------------------------------------------------
export async function setBootable ({vbd, bootable}) {
export async function setBootable ({ vbd, bootable }) {
const xapi = this.getXapi(vbd)
await xapi.call('VBD.set_bootable', vbd._xapiRef, bootable)

View File

@ -7,10 +7,11 @@ import { parseSize } from '../utils'
// ====================================================================
export async function delete_ ({vdi}) {
export async function delete_ ({ vdi }) {
const resourceSet = reduce(
vdi.$VBDs,
(resourceSet, vbd) => resourceSet || this.getObject(this.getObject(vbd, 'VBD').VM).resourceSet,
(resourceSet, vbd) =>
resourceSet || this.getObject(this.getObject(vbd, 'VBD').VM).resourceSet,
undefined
)
@ -35,7 +36,7 @@ export { delete_ as delete }
// FIXME: human readable strings should be handled.
export async function set (params) {
const {vdi} = params
const { vdi } = params
const xapi = this.getXapi(vdi)
const ref = vdi._xapiRef
@ -52,18 +53,26 @@ export async function set (params) {
const vbds = vdi.$VBDs
if (
(vbds.length === 1) &&
((resourceSetId = xapi.xo.getData(this.getObject(vbds[0], 'VBD').VM, 'resourceSet')) !== undefined)
vbds.length === 1 &&
(resourceSetId = xapi.xo.getData(
this.getObject(vbds[0], 'VBD').VM,
'resourceSet'
)) !== undefined
) {
if (this.user.permission !== 'admin') {
await this.checkResourceSetConstraints(resourceSetId, this.user.id)
}
await this.allocateLimitsInResourceSet({ disk: size - vdi.size }, resourceSetId)
} else if (!(
(this.user.permission === 'admin') ||
(await this.hasPermissions(this.user.id, [ [ vdi.$SR, 'operate' ] ]))
)) {
await this.allocateLimitsInResourceSet(
{ disk: size - vdi.size },
resourceSetId
)
} else if (
!(
this.user.permission === 'admin' ||
(await this.hasPermissions(this.user.id, [[vdi.$SR, 'operate']]))
)
) {
throw unauthorized()
}
@ -72,14 +81,16 @@ export async function set (params) {
// Other fields.
const object = {
'name_label': 'name_label',
'name_description': 'name_description',
name_label: 'name_label',
name_description: 'name_description',
}
for (const param in object) {
const fields = object[param]
if (!(param in params)) { continue }
if (!(param in params)) {
continue
}
for (const field of (isArray(fields) ? fields : [fields])) {
for (const field of isArray(fields) ? fields : [fields]) {
await xapi.call(`VDI.set_${field}`, ref, `${params[param]}`)
}
}
@ -103,7 +114,7 @@ set.resolve = {
// -------------------------------------------------------------------
export async function migrate ({vdi, sr}) {
export async function migrate ({ vdi, sr }) {
const xapi = this.getXapi(vdi)
await xapi.moveVdi(vdi._xapiRef, sr._xapiRef)

View File

@ -5,8 +5,8 @@ import { diffItems } from '../utils'
// ===================================================================
// TODO: move into vm and rename to removeInterface
async function delete_ ({vif}) {
this.allocIpAddresses(
async function delete_ ({ vif }) {
;this.allocIpAddresses(
vif.id,
null,
vif.allowedIpv4Addresses.concat(vif.allowedIpv6Addresses)
@ -14,7 +14,7 @@ async function delete_ ({vif}) {
await this.getXapi(vif).deleteVif(vif._xapiId)
}
export {delete_ as delete}
export { delete_ as delete }
delete_.params = {
id: { type: 'string' },
@ -27,7 +27,7 @@ delete_.resolve = {
// -------------------------------------------------------------------
// TODO: move into vm and rename to disconnectInterface
export async function disconnect ({vif}) {
export async function disconnect ({ vif }) {
// TODO: check if VIF is attached before
await this.getXapi(vif).disconnectVif(vif._xapiId)
}
@ -42,7 +42,7 @@ disconnect.resolve = {
// -------------------------------------------------------------------
// TODO: move into vm and rename to connectInterface
export async function connect ({vif}) {
export async function connect ({ vif }) {
// TODO: check if VIF is attached before
await this.getXapi(vif).connectVif(vif._xapiId)
}
@ -65,7 +65,9 @@ export async function set ({
allowedIpv6Addresses,
attached,
}) {
const oldIpAddresses = vif.allowedIpv4Addresses.concat(vif.allowedIpv6Addresses)
const oldIpAddresses = vif.allowedIpv4Addresses.concat(
vif.allowedIpv6Addresses
)
const newIpAddresses = []
{
const { push } = newIpAddresses
@ -96,15 +98,11 @@ export async function set ({
return
}
const [ addAddresses, removeAddresses ] = diffItems(
const [addAddresses, removeAddresses] = diffItems(
newIpAddresses,
oldIpAddresses
)
await this.allocIpAddresses(
vif.id,
addAddresses,
removeAddresses
)
await this.allocIpAddresses(vif.id, addAddresses, removeAddresses)
return this.getXapi(vif).editVif(vif._xapiId, {
ipv4Allowed: allowedIpv4Addresses,

View File

@ -25,14 +25,13 @@ function checkPermissionOnSrs (vm, permission = 'operate') {
return permissions.push([this.getObject(vdiId, 'VDI').$SR, permission])
})
return this.hasPermissions(
this.session.get('user_id'),
permissions
).then(success => {
if (!success) {
throw unauthorized()
return this.hasPermissions(this.session.get('user_id'), permissions).then(
success => {
if (!success) {
throw unauthorized()
}
}
})
)
}
// ===================================================================
@ -359,7 +358,7 @@ async function delete_ ({
// Update resource sets
const resourceSet = xapi.xo.getData(vm._xapiId, 'resourceSet')
if (resourceSet != null) {
this.setVmResourceSet(vm._xapiId, null)::ignoreErrors()
;this.setVmResourceSet(vm._xapiId, null)::ignoreErrors()
}
return xapi.deleteVm(vm._xapiId, deleteDisks, force)
@ -1239,8 +1238,10 @@ export async function createInterface ({
}) {
const { resourceSet } = vm
if (resourceSet != null) {
await this.checkResourceSetConstraints(resourceSet, this.user.id, [ network.id ])
} else if (!(await this.hasPermissions(this.user.id, [ [ network.id, 'view' ] ]))) {
await this.checkResourceSetConstraints(resourceSet, this.user.id, [
network.id,
])
} else if (!await this.hasPermissions(this.user.id, [[network.id, 'view']])) {
throw unauthorized()
}

View File

@ -12,15 +12,17 @@ clean.permission = 'admin'
export async function exportConfig () {
return {
$getFrom: await this.registerHttpRequest((req, res) => {
res.writeHead(200, 'OK', {
'content-disposition': 'attachment',
})
$getFrom: await this.registerHttpRequest(
(req, res) => {
res.writeHead(200, 'OK', {
'content-disposition': 'attachment',
})
return this.exportConfig()
},
undefined,
{ suffix: '/config.json' }),
return this.exportConfig()
},
undefined,
{ suffix: '/config.json' }
),
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,7 @@
import Model from './model'
import {BaseError} from 'make-error'
import {EventEmitter} from 'events'
import {
isArray,
isObject,
map,
} from './utils'
import { BaseError } from 'make-error'
import { EventEmitter } from 'events'
import { isArray, isObject, map } from './utils'
// ===================================================================
@ -39,34 +35,34 @@ export default class Collection extends EventEmitter {
models = [models]
}
const {Model} = this
map(models, model => {
if (!(model instanceof Model)) {
model = new Model(model)
}
const { Model } = this
map(
models,
model => {
if (!(model instanceof Model)) {
model = new Model(model)
}
const error = model.validate()
if (error) {
// TODO: Better system inspired by Backbone.js
throw error
}
const error = model.validate()
if (error) {
// TODO: Better system inspired by Backbone.js
throw error
}
return model.properties
}, models)
return model.properties
},
models
)
models = await this._add(models, opts)
this.emit('add', models)
return array
? models
: new this.Model(models[0])
return array ? models : new this.Model(models[0])
}
async first (properties) {
if (!isObject(properties)) {
properties = (properties !== undefined)
? { id: properties }
: {}
properties = properties !== undefined ? { id: properties } : {}
}
const model = await this._first(properties)
@ -75,9 +71,7 @@ export default class Collection extends EventEmitter {
async get (properties) {
if (!isObject(properties)) {
properties = (properties !== undefined)
? { id: properties }
: {}
properties = properties !== undefined ? { id: properties } : {}
}
return /* await */ this._get(properties)
@ -100,37 +94,39 @@ export default class Collection extends EventEmitter {
models = [models]
}
const {Model} = this
map(models, model => {
if (!(model instanceof Model)) {
// TODO: Problems, we may be mixing in some default
// properties which will overwrite existing ones.
model = new Model(model)
}
const { Model } = this
map(
models,
model => {
if (!(model instanceof Model)) {
// TODO: Problems, we may be mixing in some default
// properties which will overwrite existing ones.
model = new Model(model)
}
const id = model.get('id')
const id = model.get('id')
// Missing models should be added not updated.
if (id === undefined) {
// FIXME: should not throw an exception but return a rejected promise.
throw new Error('a model without an id cannot be updated')
}
// Missing models should be added not updated.
if (id === undefined) {
// FIXME: should not throw an exception but return a rejected promise.
throw new Error('a model without an id cannot be updated')
}
const error = model.validate()
if (error !== undefined) {
// TODO: Better system inspired by Backbone.js.
throw error
}
const error = model.validate()
if (error !== undefined) {
// TODO: Better system inspired by Backbone.js.
throw error
}
return model.properties
}, models)
return model.properties
},
models
)
models = await this._update(models)
this.emit('update', models)
return array
? models
: new this.Model(models[0])
return array ? models : new this.Model(models[0])
}
// Methods to override in implementations.
@ -165,8 +161,6 @@ export default class Collection extends EventEmitter {
async _first (properties) {
const models = await this.get(properties)
return models.length
? models[0]
: null
return models.length ? models[0] : null
}
}

View File

@ -1,5 +1,12 @@
import { createClient as createRedisClient } from 'redis'
import { difference, filter, forEach, isEmpty, keys as getKeys, map } from 'lodash'
import {
difference,
filter,
forEach,
isEmpty,
keys as getKeys,
map,
} from 'lodash'
import { ignoreErrors, promisifyAll } from 'promise-toolbox'
import { v4 as generateUuid } from 'uuid'
@ -28,33 +35,33 @@ import { asyncMap } from '../utils'
const VERSION = '20170905'
export default class Redis extends Collection {
constructor ({
connection,
indexes = [],
prefix,
uri,
}) {
constructor ({ connection, indexes = [], prefix, uri }) {
super()
this.indexes = indexes
this.prefix = prefix
const redis = this.redis = promisifyAll(connection || createRedisClient(uri))
const redis = (this.redis = promisifyAll(
connection || createRedisClient(uri)
))
const key = `${prefix}:version`
redis.get(key).then(version => {
if (version === VERSION) {
return
}
redis
.get(key)
.then(version => {
if (version === VERSION) {
return
}
let p = redis.set(`${prefix}:version`, VERSION)
switch (version) {
case undefined:
// - clean indexes
// - indexes are now case insensitive
p = p.then(() => this.rebuildIndexes())
}
return p
})::ignoreErrors()
let p = redis.set(`${prefix}:version`, VERSION)
switch (version) {
case undefined:
// - clean indexes
// - indexes are now case insensitive
p = p.then(() => this.rebuildIndexes())
}
return p
})
::ignoreErrors()
}
rebuildIndexes () {
@ -66,113 +73,120 @@ export default class Redis extends Collection {
const idsIndex = `${prefix}_ids`
return asyncMap(indexes, index =>
redis.keys(`${prefix}_${index}:*`).then(keys =>
keys.length !== 0 && redis.del(keys)
redis
.keys(`${prefix}_${index}:*`)
.then(keys => keys.length !== 0 && redis.del(keys))
).then(() =>
asyncMap(redis.smembers(idsIndex), id =>
redis.hgetall(`${prefix}:${id}`).then(
values =>
values == null
? redis.srem(idsIndex, id) // entry no longer exists
: asyncMap(indexes, index => {
const value = values[index]
if (value !== undefined) {
return redis.sadd(
`${prefix}_${index}:${String(value).toLowerCase()}`,
id
)
}
})
)
)
).then(() => asyncMap(redis.smembers(idsIndex), id =>
redis.hgetall(`${prefix}:${id}`).then(values =>
values == null
? redis.srem(idsIndex, id) // entry no longer exists
: asyncMap(indexes, index => {
const value = values[index]
if (value !== undefined) {
return redis.sadd(
`${prefix}_${index}:${String(value).toLowerCase()}`,
id
)
}
})
)
))
)
}
_extract (ids) {
const prefix = this.prefix + ':'
const {redis} = this
const { redis } = this
const models = []
return Promise.all(map(ids, id => {
return redis.hgetall(prefix + id).then(model => {
// If empty, consider it a no match.
if (isEmpty(model)) {
return
}
return Promise.all(
map(ids, id => {
return redis.hgetall(prefix + id).then(model => {
// If empty, consider it a no match.
if (isEmpty(model)) {
return
}
// Mix the identifier in.
model.id = id
// Mix the identifier in.
model.id = id
models.push(model)
models.push(model)
})
})
})).then(() => models)
).then(() => models)
}
_add (models, {replace = false} = {}) {
_add (models, { replace = false } = {}) {
// TODO: remove “replace” which is a temporary measure, implement
// “set()” instead.
const {indexes, prefix, redis} = this
const { indexes, prefix, redis } = this
return Promise.all(map(models, async model => {
// Generate a new identifier if necessary.
if (model.id === undefined) {
model.id = generateUuid()
}
const { id } = model
return Promise.all(
map(models, async model => {
// Generate a new identifier if necessary.
if (model.id === undefined) {
model.id = generateUuid()
}
const { id } = model
const newEntry = await redis.sadd(prefix + '_ids', id)
const newEntry = await redis.sadd(prefix + '_ids', id)
if (!newEntry) {
if (!replace) {
throw new ModelAlreadyExists(id)
if (!newEntry) {
if (!replace) {
throw new ModelAlreadyExists(id)
}
// remove the previous values from indexes
if (indexes.length !== 0) {
const previous = await redis.hgetall(`${prefix}:${id}`)
await asyncMap(indexes, index => {
const value = previous[index]
if (value !== undefined) {
return redis.srem(
`${prefix}_${index}:${String(value).toLowerCase()}`,
id
)
}
})
}
}
// remove the previous values from indexes
if (indexes.length !== 0) {
const previous = await redis.hgetall(`${prefix}:${id}`)
await asyncMap(indexes, index => {
const value = previous[index]
if (value !== undefined) {
return redis.srem(`${prefix}_${index}:${String(value).toLowerCase()}`, id)
}
})
}
}
const params = []
forEach(model, (value, name) => {
// No need to store the identifier (already in the key).
if (name === 'id') {
return
}
const params = []
forEach(model, (value, name) => {
// No need to store the identifier (already in the key).
if (name === 'id') {
return
}
params.push(name, value)
})
params.push(name, value)
const key = `${prefix}:${id}`
const promises = [redis.del(key), redis.hmset(key, ...params)]
// Update indexes.
forEach(indexes, index => {
const value = model[index]
if (value === undefined) {
return
}
const key = prefix + '_' + index + ':' + String(value).toLowerCase()
promises.push(redis.sadd(key, id))
})
await Promise.all(promises)
return model
})
const key = `${prefix}:${id}`
const promises = [
redis.del(key),
redis.hmset(key, ...params),
]
// Update indexes.
forEach(indexes, (index) => {
const value = model[index]
if (value === undefined) {
return
}
const key = prefix + '_' + index + ':' + String(value).toLowerCase()
promises.push(redis.sadd(key, id))
})
await Promise.all(promises)
return model
}))
)
}
_get (properties) {
const {prefix, redis} = this
const { prefix, redis } = this
if (isEmpty(properties)) {
return redis.smembers(prefix + '_ids').then(ids => this._extract(ids))
@ -183,13 +197,11 @@ export default class Redis extends Collection {
if (id !== undefined) {
delete properties.id
return this._extract([id]).then(models => {
return (models.length && !isEmpty(properties))
? filter(models)
: models
return models.length && !isEmpty(properties) ? filter(models) : models
})
}
const {indexes} = this
const { indexes } = this
// Check for non indexed fields.
const unfit = difference(getKeys(properties), indexes)
@ -197,7 +209,10 @@ export default class Redis extends Collection {
throw new Error('fields not indexed: ' + unfit.join())
}
const keys = map(properties, (value, index) => `${prefix}_${index}:${String(value).toLowerCase()}`)
const keys = map(
properties,
(value, index) => `${prefix}_${index}:${String(value).toLowerCase()}`
)
return redis.sinter(...keys).then(ids => this._extract(ids))
}
@ -213,16 +228,24 @@ export default class Redis extends Collection {
// update other indexes
if (indexes.length !== 0) {
promise = Promise.all([ promise, asyncMap(ids, id =>
redis.hgetall(`${prefix}:${id}`).then(values =>
values != null && asyncMap(indexes, index => {
const value = values[index]
if (value !== undefined) {
return redis.srem(`${prefix}_${index}:${String(value).toLowerCase()}`, id)
}
})
)
) ])
promise = Promise.all([
promise,
asyncMap(ids, id =>
redis.hgetall(`${prefix}:${id}`).then(
values =>
values != null &&
asyncMap(indexes, index => {
const value = values[index]
if (value !== undefined) {
return redis.srem(
`${prefix}_${index}:${String(value).toLowerCase()}`,
id
)
}
})
)
),
])
}
return promise.then(() =>

View File

@ -1,6 +1,6 @@
import {EventEmitter} from 'events'
import { EventEmitter } from 'events'
import {createRawObject, noop} from './utils'
import { createRawObject, noop } from './utils'
// ===================================================================
@ -21,7 +21,7 @@ export default class Connection extends EventEmitter {
// Gets the value for this key.
get (key, defaultValue) {
const {_data: data} = this
const { _data: data } = this
if (key in data) {
return data[key]

View File

@ -1,16 +1,10 @@
import { getBoundPropertyDescriptor } from 'bind-property-descriptor'
import {
isArray,
isFunction,
} from './utils'
import { isArray, isFunction } from './utils'
// ===================================================================
const {
defineProperties,
getOwnPropertyDescriptor,
} = Object
const { defineProperties, getOwnPropertyDescriptor } = Object
// ===================================================================
@ -27,10 +21,12 @@ export const debounce = duration => (target, name, descriptor) => {
const s = Symbol(`debounced ${name} data`)
function debounced () {
const data = this[s] || (this[s] = {
lastCall: 0,
wrapper: null,
})
const data =
this[s] ||
(this[s] = {
lastCall: 0,
wrapper: null,
})
const now = Date.now()
if (now > data.lastCall + duration) {
@ -39,12 +35,16 @@ export const debounce = duration => (target, name, descriptor) => {
const result = fn.apply(this, arguments)
data.wrapper = () => result
} catch (error) {
data.wrapper = () => { throw error }
data.wrapper = () => {
throw error
}
}
}
return data.wrapper()
}
debounced.reset = obj => { delete obj[s] }
debounced.reset = obj => {
delete obj[s]
}
descriptor.value = debounced
return descriptor
@ -52,21 +52,12 @@ export const debounce = duration => (target, name, descriptor) => {
// -------------------------------------------------------------------
const _ownKeys = (
const _ownKeys =
(typeof Reflect !== 'undefined' && Reflect.ownKeys) ||
(({
getOwnPropertyNames: names,
getOwnPropertySymbols: symbols,
}) => symbols
? obj => names(obj).concat(symbols(obj))
: names
)(Object)
)
(({ getOwnPropertyNames: names, getOwnPropertySymbols: symbols }) =>
symbols ? obj => names(obj).concat(symbols(obj)) : names)(Object)
const _isIgnoredProperty = name => (
name[0] === '_' ||
name === 'constructor'
)
const _isIgnoredProperty = name => name[0] === '_' || name === 'constructor'
const _IGNORED_STATIC_PROPERTIES = {
__proto__: null,
@ -81,7 +72,7 @@ const _isIgnoredStaticProperty = name => _IGNORED_STATIC_PROPERTIES[name]
export const mixin = MixIns => Class => {
if (!isArray(MixIns)) {
MixIns = [ MixIns ]
MixIns = [MixIns]
}
const { name } = Class
@ -103,9 +94,10 @@ export const mixin = MixIns => Class => {
throw new Error(`${name}#${prop} is already defined`)
}
(
descriptors[prop] = getOwnPropertyDescriptor(MixIn, prop)
).enumerable = false // Object methods are enumerable but class methods are not.
;(descriptors[prop] = getOwnPropertyDescriptor(
MixIn,
prop
)).enumerable = false // Object methods are enumerable but class methods are not.
}
}
defineProperties(prototype, descriptors)
@ -143,16 +135,15 @@ export const mixin = MixIns => Class => {
const descriptors = { __proto__: null }
for (const prop of _ownKeys(Class)) {
let descriptor
if (!(
// Special properties are not defined...
_isIgnoredStaticProperty(prop) &&
// if they already exist...
(descriptor = getOwnPropertyDescriptor(Decorator, prop)) &&
// and are not configurable.
!descriptor.configurable
)) {
if (
!(
_isIgnoredStaticProperty(prop) &&
// if they already exist...
(descriptor = getOwnPropertyDescriptor(Decorator, prop)) &&
// and are not configurable.
!descriptor.configurable
)
) {
descriptors[prop] = getOwnPropertyDescriptor(Class, prop)
}
}

View File

@ -1,6 +1,6 @@
/* eslint-env jest */
import {debounce} from './decorators'
import { debounce } from './decorators'
// ===================================================================

View File

@ -27,28 +27,31 @@ export function init () {
const buf = Buffer.alloc(TEN_MIB)
// https://github.com/natevw/fatfs/blob/master/structs.js
fat16.pack({
jmpBoot: Buffer.from('eb3c90', 'hex'),
OEMName: 'mkfs.fat',
BytsPerSec: SECTOR_SIZE,
SecPerClus: 4,
ResvdSecCnt: 1,
NumFATs: 2,
RootEntCnt: 512,
TotSec16: 20480,
Media: 248,
FATSz16: 20,
SecPerTrk: 32,
NumHeads: 64,
HiddSec: 0,
TotSec32: 0,
DrvNum: 128,
Reserved1: 0,
BootSig: 41,
VolID: 895111106,
VolLab: 'NO NAME ',
FilSysType: 'FAT16 ',
}, buf)
fat16.pack(
{
jmpBoot: Buffer.from('eb3c90', 'hex'),
OEMName: 'mkfs.fat',
BytsPerSec: SECTOR_SIZE,
SecPerClus: 4,
ResvdSecCnt: 1,
NumFATs: 2,
RootEntCnt: 512,
TotSec16: 20480,
Media: 248,
FATSz16: 20,
SecPerTrk: 32,
NumHeads: 64,
HiddSec: 0,
TotSec32: 0,
DrvNum: 128,
Reserved1: 0,
BootSig: 41,
VolID: 895111106,
VolLab: 'NO NAME ',
FilSysType: 'FAT16 ',
},
buf
)
// End of sector.
buf[0x1fe] = 0x55

View File

@ -17,11 +17,7 @@ import { join as joinPath } from 'path'
import JsonRpcPeer from 'json-rpc-peer'
import { invalidCredentials } from 'xo-common/api-errors'
import {
ensureDir,
readdir,
readFile,
} from 'fs-extra'
import { ensureDir, readdir, readFile } from 'fs-extra'
import WebServer from 'http-server-plus'
import Xo from './xo'
@ -52,10 +48,7 @@ const warn = (...args) => {
// ===================================================================
const DEPRECATED_ENTRIES = [
'users',
'servers',
]
const DEPRECATED_ENTRIES = ['users', 'servers']
async function loadConfiguration () {
const config = await appConf.load('xo-server', {
@ -85,13 +78,15 @@ function createExpressApp () {
// Registers the cookie-parser and express-session middlewares,
// necessary for connect-flash.
app.use(cookieParser())
app.use(expressSession({
resave: false,
saveUninitialized: false,
app.use(
expressSession({
resave: false,
saveUninitialized: false,
// TODO: should be in the config file.
secret: 'CLWguhRZAZIXZcbrMzHCYmefxgweItKnS',
}))
// TODO: should be in the config file.
secret: 'CLWguhRZAZIXZcbrMzHCYmefxgweItKnS',
})
)
// Registers the connect-flash middleware, necessary for Passport to
// display error messages.
@ -112,7 +107,7 @@ async function setUpPassport (express, xo) {
xo.registerPassportStrategy = strategy => {
passport.use(strategy)
const {name} = strategy
const { name } = strategy
if (name !== 'local') {
strategies[name] = strategy.label || name
}
@ -123,10 +118,12 @@ async function setUpPassport (express, xo) {
await readFile(joinPath(__dirname, '..', 'signin.pug'))
)
express.get('/signin', (req, res, next) => {
res.send(signInPage({
error: req.flash('error')[0],
strategies,
}))
res.send(
signInPage({
error: req.flash('error')[0],
strategies,
})
)
})
express.get('/signout', (req, res) => {
@ -154,7 +151,7 @@ async function setUpPassport (express, xo) {
// browsers do not save cookies on redirect.
req.flash(
'token',
(await xo.createAuthenticationToken({userId: user.id})).id
(await xo.createAuthenticationToken({ userId: user.id })).id
)
// The session is only persistent for internal provider and if 'Remember me' box is checked
@ -183,7 +180,9 @@ async function setUpPassport (express, xo) {
next()
} else if (req.cookies.token) {
next()
} else if (/favicon|fontawesome|images|styles|\.(?:css|jpg|png)$/.test(url)) {
} else if (
/favicon|fontawesome|images|styles|\.(?:css|jpg|png)$/.test(url)
) {
next()
} else {
req.flash('return-url', url)
@ -192,16 +191,16 @@ async function setUpPassport (express, xo) {
})
// Install the local strategy.
xo.registerPassportStrategy(new LocalStrategy(
async (username, password, done) => {
xo.registerPassportStrategy(
new LocalStrategy(async (username, password, done) => {
try {
const user = await xo.authenticateUser({username, password})
const user = await xo.authenticateUser({ username, password })
done(null, user)
} catch (error) {
done(null, false, { message: error.message })
}
}
))
})
)
}
// ===================================================================
@ -274,40 +273,44 @@ async function registerPluginsInPath (path) {
throw error
})
await Promise.all(mapToArray(files, name => {
if (startsWith(name, PLUGIN_PREFIX)) {
return registerPluginWrapper.call(
this,
`${path}/${name}`,
name.slice(PLUGIN_PREFIX_LENGTH)
)
}
}))
await Promise.all(
mapToArray(files, name => {
if (startsWith(name, PLUGIN_PREFIX)) {
return registerPluginWrapper.call(
this,
`${path}/${name}`,
name.slice(PLUGIN_PREFIX_LENGTH)
)
}
})
)
}
async function registerPlugins (xo) {
await Promise.all(mapToArray([
`${__dirname}/../node_modules/`,
'/usr/local/lib/node_modules/',
], xo::registerPluginsInPath))
await Promise.all(
mapToArray(
[`${__dirname}/../node_modules/`, '/usr/local/lib/node_modules/'],
xo::registerPluginsInPath
)
)
}
// ===================================================================
async function makeWebServerListen (webServer, {
certificate,
async function makeWebServerListen (
webServer,
{
certificate,
// The properties was called `certificate` before.
cert = certificate,
// The properties was called `certificate` before.
cert = certificate,
key,
...opts
}) {
key,
...opts
}
) {
if (cert && key) {
[opts.cert, opts.key] = await Promise.all([
readFile(cert),
readFile(key),
])
;[opts.cert, opts.key] = await Promise.all([readFile(cert), readFile(key)])
}
try {
const niceAddress = await webServer.listen(opts)
@ -316,7 +319,7 @@ async function makeWebServerListen (webServer, {
if (error.niceAddress) {
warn(`Web server could not listen on ${error.niceAddress}`)
const {code} = error
const { code } = error
if (code === 'EACCES') {
warn(' Access denied.')
warn(' Ports < 1024 are often reserved to privileges users.')
@ -332,9 +335,11 @@ async function makeWebServerListen (webServer, {
async function createWebServer ({ listen, listenOptions }) {
const webServer = new WebServer()
await Promise.all(mapToArray(listen,
opts => makeWebServerListen(webServer, { ...listenOptions, ...opts })
))
await Promise.all(
mapToArray(listen, opts =>
makeWebServerListen(webServer, { ...listenOptions, ...opts })
)
)
return webServer
}
@ -348,7 +353,7 @@ const setUpProxies = (express, opts, xo) => {
const proxy = createProxyServer({
ignorePath: true,
}).on('error', (error) => console.error(error))
}).on('error', error => console.error(error))
// TODO: sort proxies by descending prefix length.
@ -464,7 +469,9 @@ const setUpApi = (webServer, xo, verboseLogsOnErrors) => {
}
webServer.on('upgrade', (req, socket, head) => {
if (req.url === '/api/') {
webSocketServer.handleUpgrade(req, socket, head, ws => onConnection(ws, req))
webSocketServer.handleUpgrade(req, socket, head, ws =>
onConnection(ws, req)
)
}
})
}
@ -492,7 +499,7 @@ const setUpConsoleProxy = (webServer, xo) => {
const { token } = parseCookies(req.headers.cookie)
const user = await xo.authenticateUser({ token })
if (!await xo.hasPermissions(user.id, [ [ id, 'operate' ] ])) {
if (!await xo.hasPermissions(user.id, [[id, 'operate']])) {
throw invalidCredentials()
}
@ -518,10 +525,7 @@ const setUpConsoleProxy = (webServer, xo) => {
// ===================================================================
const USAGE = (({
name,
version,
}) => `Usage: ${name} [--safe-mode]
const USAGE = (({ name, version }) => `Usage: ${name} [--safe-mode]
${name} v${version}`)(require('../package.json'))
@ -545,7 +549,7 @@ export default async function main (args) {
// Now the web server is listening, drop privileges.
try {
const {user, group} = config
const { user, group } = config
if (group) {
process.setgid(group)
debug('Group changed to', group)
@ -576,10 +580,7 @@ export default async function main (args) {
if (config.http.redirectToHttps) {
let port
forEach(config.http.listen, listen => {
if (
listen.port &&
(listen.cert || listen.certificate)
) {
if (listen.port && (listen.cert || listen.certificate)) {
port = listen.port
return false
}
@ -629,7 +630,7 @@ export default async function main (args) {
//
// TODO: implements a timeout? (or maybe it is the services launcher
// responsibility?)
forEach([ 'SIGINT', 'SIGTERM' ], signal => {
forEach(['SIGINT', 'SIGTERM'], signal => {
let alreadyCalled = false
process.on(signal, () => {

View File

@ -2,20 +2,10 @@ import Bluebird from 'bluebird'
import { BaseError } from 'make-error'
import { createPredicate } from 'value-matcher'
import { timeout } from 'promise-toolbox'
import {
assign,
filter,
find,
isEmpty,
map,
mapValues,
} from 'lodash'
import { assign, filter, find, isEmpty, map, mapValues } from 'lodash'
import { crossProduct } from './math'
import {
serializeError,
thunkToArray,
} from './utils'
import { serializeError, thunkToArray } from './utils'
export class JobExecutorError extends BaseError {}
export class UnsupportedJobType extends JobExecutorError {
@ -36,9 +26,9 @@ const paramsVectorActionsMap = {
return mapValues(mapping, key => value[key])
},
crossProduct ({ items }) {
return thunkToArray(crossProduct(
map(items, value => resolveParamsVector.call(this, value))
))
return thunkToArray(
crossProduct(map(items, value => resolveParamsVector.call(this, value)))
)
},
fetchObjects ({ pattern }) {
const objects = filter(this.xo.getObjects(), createPredicate(pattern))
@ -74,9 +64,11 @@ export default class JobExecutor {
this.xo = xo
// The logger is not available until Xo has started.
xo.on('start', () => xo.getLogger('jobs').then(logger => {
this._logger = logger
}))
xo.on('start', () =>
xo.getLogger('jobs').then(logger => {
this._logger = logger
})
)
}
async exec (job) {
@ -130,51 +122,68 @@ export default class JobExecutor {
timezone: schedule !== undefined ? schedule.timezone : undefined,
}
await Bluebird.map(paramsFlatVector, params => {
const runCallId = this._logger.notice(`Starting ${job.method} call. (${job.id})`, {
event: 'jobCall.start',
runJobId,
method: job.method,
params,
})
const call = execStatus.calls[runCallId] = {
method: job.method,
params,
start: Date.now(),
}
let promise = this.xo.callApiMethod(connection, job.method, assign({}, params))
if (job.timeout) {
promise = promise::timeout(job.timeout)
}
return promise.then(
value => {
this._logger.notice(`Call ${job.method} (${runCallId}) is a success. (${job.id})`, {
event: 'jobCall.end',
await Bluebird.map(
paramsFlatVector,
params => {
const runCallId = this._logger.notice(
`Starting ${job.method} call. (${job.id})`,
{
event: 'jobCall.start',
runJobId,
runCallId,
returnedValue: value,
})
method: job.method,
params,
}
)
call.returnedValue = value
call.end = Date.now()
},
reason => {
this._logger.notice(`Call ${job.method} (${runCallId}) has failed. (${job.id})`, {
event: 'jobCall.end',
runJobId,
runCallId,
error: serializeError(reason),
})
call.error = reason
call.end = Date.now()
const call = (execStatus.calls[runCallId] = {
method: job.method,
params,
start: Date.now(),
})
let promise = this.xo.callApiMethod(
connection,
job.method,
assign({}, params)
)
if (job.timeout) {
promise = promise::timeout(job.timeout)
}
)
}, {
concurrency: 2,
})
return promise.then(
value => {
this._logger.notice(
`Call ${job.method} (${runCallId}) is a success. (${job.id})`,
{
event: 'jobCall.end',
runJobId,
runCallId,
returnedValue: value,
}
)
call.returnedValue = value
call.end = Date.now()
},
reason => {
this._logger.notice(
`Call ${job.method} (${runCallId}) has failed. (${job.id})`,
{
event: 'jobCall.end',
runJobId,
runCallId,
error: serializeError(reason),
}
)
call.error = reason
call.end = Date.now()
}
)
},
{
concurrency: 2,
}
)
connection.close()
execStatus.end = Date.now()

View File

@ -4,97 +4,113 @@ import { forEach } from 'lodash'
import { resolveParamsVector } from './job-executor'
describe('resolveParamsVector', function () {
forEach({
'cross product with three sets': [
// Expected result.
[ { id: 3, value: 'foo', remote: 'local' },
{ id: 7, value: 'foo', remote: 'local' },
{ id: 10, value: 'foo', remote: 'local' },
{ id: 3, value: 'bar', remote: 'local' },
{ id: 7, value: 'bar', remote: 'local' },
{ id: 10, value: 'bar', remote: 'local' } ],
// Entry.
{
type: 'crossProduct',
items: [{
type: 'set',
values: [ { id: 3 }, { id: 7 }, { id: 10 } ],
}, {
type: 'set',
values: [ { value: 'foo' }, { value: 'bar' } ],
}, {
type: 'set',
values: [ { remote: 'local' } ],
}],
},
],
'cross product with `set` and `map`': [
// Expected result.
[
{ remote: 'local', id: 'vm:2' },
{ remote: 'smb', id: 'vm:2' },
forEach(
{
'cross product with three sets': [
// Expected result.
[
{ id: 3, value: 'foo', remote: 'local' },
{ id: 7, value: 'foo', remote: 'local' },
{ id: 10, value: 'foo', remote: 'local' },
{ id: 3, value: 'bar', remote: 'local' },
{ id: 7, value: 'bar', remote: 'local' },
{ id: 10, value: 'bar', remote: 'local' },
],
// Entry.
{
type: 'crossProduct',
items: [
{
type: 'set',
values: [{ id: 3 }, { id: 7 }, { id: 10 }],
},
{
type: 'set',
values: [{ value: 'foo' }, { value: 'bar' }],
},
{
type: 'set',
values: [{ remote: 'local' }],
},
],
},
],
'cross product with `set` and `map`': [
// Expected result.
[{ remote: 'local', id: 'vm:2' }, { remote: 'smb', id: 'vm:2' }],
// Entry.
{
type: 'crossProduct',
items: [{
type: 'set',
values: [ { remote: 'local' }, { remote: 'smb' } ],
}, {
type: 'map',
collection: {
type: 'fetchObjects',
pattern: {
$pool: { __or: [ 'pool:1', 'pool:8', 'pool:12' ] },
power_state: 'Running',
tags: [ 'foo' ],
type: 'VM',
// Entry.
{
type: 'crossProduct',
items: [
{
type: 'set',
values: [{ remote: 'local' }, { remote: 'smb' }],
},
{
type: 'map',
collection: {
type: 'fetchObjects',
pattern: {
$pool: { __or: ['pool:1', 'pool:8', 'pool:12'] },
power_state: 'Running',
tags: ['foo'],
type: 'VM',
},
},
iteratee: {
type: 'extractProperties',
mapping: { id: 'id' },
},
},
],
},
// Context.
{
xo: {
getObjects: function () {
return [
{
id: 'vm:1',
$pool: 'pool:1',
tags: [],
type: 'VM',
power_state: 'Halted',
},
{
id: 'vm:2',
$pool: 'pool:1',
tags: ['foo'],
type: 'VM',
power_state: 'Running',
},
{
id: 'host:1',
type: 'host',
power_state: 'Running',
},
{
id: 'vm:3',
$pool: 'pool:8',
tags: ['foo'],
type: 'VM',
power_state: 'Halted',
},
]
},
},
iteratee: {
type: 'extractProperties',
mapping: { id: 'id' },
},
}],
},
// Context.
{
xo: {
getObjects: function () {
return [{
id: 'vm:1',
$pool: 'pool:1',
tags: [],
type: 'VM',
power_state: 'Halted',
}, {
id: 'vm:2',
$pool: 'pool:1',
tags: [ 'foo' ],
type: 'VM',
power_state: 'Running',
}, {
id: 'host:1',
type: 'host',
power_state: 'Running',
}, {
id: 'vm:3',
$pool: 'pool:8',
tags: [ 'foo' ],
type: 'VM',
power_state: 'Halted',
}]
},
},
},
],
}, ([ expectedResult, entry, context ], name) => {
describe(`with ${name}`, () => {
it('Resolves params vector', () => {
expect(resolveParamsVector.call(context, entry)).toEqual(expectedResult)
],
},
([expectedResult, entry, context], name) => {
describe(`with ${name}`, () => {
it('Resolves params vector', () => {
expect(resolveParamsVector.call(context, entry)).toEqual(
expectedResult
)
})
})
})
})
}
)
})

View File

@ -8,26 +8,26 @@ import sublevel from 'level-sublevel'
import util from 'util'
import { repair as repairDb } from 'level'
import {forEach} from './utils'
import { forEach } from './utils'
import globMatcher from './glob-matcher'
// ===================================================================
async function printLogs (db, args) {
let stream = highland(db.createReadStream({reverse: true}))
let stream = highland(db.createReadStream({ reverse: true }))
if (args.since) {
stream = stream.filter(({value}) => (value.time >= args.since))
stream = stream.filter(({ value }) => value.time >= args.since)
}
if (args.until) {
stream = stream.filter(({value}) => (value.time <= args.until))
stream = stream.filter(({ value }) => value.time <= args.until)
}
const fields = Object.keys(args.matchers)
if (fields.length > 0) {
stream = stream.filter(({value}) => {
stream = stream.filter(({ value }) => {
for (const field of fields) {
const fieldValue = get(value, field)
if (fieldValue === undefined || !args.matchers[field](fieldValue)) {
@ -42,10 +42,9 @@ async function printLogs (db, args) {
stream = stream.take(args.limit)
if (args.json) {
stream = highland(stream.pipe(ndjson.serialize()))
.each(value => {
process.stdout.write(value)
})
stream = highland(stream.pipe(ndjson.serialize())).each(value => {
process.stdout.write(value)
})
} else {
stream = stream.each(value => {
console.log(util.inspect(value, { depth: null }))
@ -126,7 +125,7 @@ function getArgs () {
patterns[pattern]
? patterns[field].push(pattern)
: patterns[field] = [ pattern ]
: (patterns[field] = [pattern])
} else if (!patterns[value]) {
patterns[value] = null
}
@ -137,7 +136,7 @@ function getArgs () {
for (const field in patterns) {
const values = patterns[field]
args.matchers[field] = (values === null) ? trueFunction : globMatcher(values)
args.matchers[field] = values === null ? trueFunction : globMatcher(values)
}
// Warning: minimist makes one array of values if the same option is used many times.
@ -147,7 +146,6 @@ function getArgs () {
throw new Error(`error: too many values for ${arg} argument`)
}
})
;['since', 'until'].forEach(arg => {
if (args[arg] !== undefined) {
args[arg] = Date.parse(args[arg])
@ -158,7 +156,7 @@ function getArgs () {
}
})
if (isNaN(args.limit = +args.limit)) {
if (isNaN((args.limit = +args.limit))) {
throw new Error('error: limit is not a valid number')
}
@ -193,10 +191,9 @@ export default async function main () {
return
}
const db = sublevel(levelup(
`${config.datadir}/leveldb`,
{ valueEncoding: 'json' }
)).sublevel('logs')
const db = sublevel(
levelup(`${config.datadir}/leveldb`, { valueEncoding: 'json' })
).sublevel('logs')
return printLogs(db, args)
}

View File

@ -9,25 +9,29 @@ const parse = createParser({
keyTransform: key => key.slice(5).toLowerCase(),
})
const makeFunction = command => (fields, ...args) =>
execa.stdout(command, [
'--noheading',
'--nosuffix',
'--nameprefixes',
'--unbuffered',
'--units',
'b',
'-o',
String(fields),
...args,
]).then(stdout => map(
splitLines(stdout),
isArray(fields)
? parse
: line => {
const data = parse(line)
return data[fields]
}
))
execa
.stdout(command, [
'--noheading',
'--nosuffix',
'--nameprefixes',
'--unbuffered',
'--units',
'b',
'-o',
String(fields),
...args,
])
.then(stdout =>
map(
splitLines(stdout),
isArray(fields)
? parse
: line => {
const data = parse(line)
return data[fields]
}
)
)
export const lvs = makeFunction('lvs')
export const pvs = makeFunction('pvs')

View File

@ -11,7 +11,7 @@ const _combine = (vectors, n, cb) => {
const m = vector.length
if (n === 1) {
for (let i = 0; i < m; ++i) {
cb([ vector[i] ]) // eslint-disable-line standard/no-callback-literal
cb([vector[i]]) // eslint-disable-line standard/no-callback-literal
}
return
}
@ -19,7 +19,7 @@ const _combine = (vectors, n, cb) => {
for (let i = 0; i < m; ++i) {
const value = vector[i]
_combine(vectors, nLast, (vector) => {
_combine(vectors, nLast, vector => {
vector.push(value)
cb(vector)
})
@ -41,8 +41,7 @@ export const mergeObjects = objects => assign({}, ...objects)
//
// Ex: crossProduct([ [ { a: 2 }, { b: 3 } ], [ { c: 5 }, { d: 7 } ] ] )
// => [ { a: 2, c: 5 }, { b: 3, c: 5 }, { a: 2, d: 7 }, { b: 3, d: 7 } ]
export const crossProduct = (vectors, mergeFn = mergeObjects) => cb => (
export const crossProduct = (vectors, mergeFn = mergeObjects) => cb =>
combine(vectors)(vector => {
cb(mergeFn(vector))
})
)

Some files were not shown because too many files have changed in this diff Show More