Compare commits

..

1 Commits

Author SHA1 Message Date
Julien Fontanet
48c8d25774 WiP: feat(self-signed): genSignedCert 2021-12-14 12:09:31 +01:00
550 changed files with 5278 additions and 16163 deletions

View File

@@ -1,7 +1,5 @@
'use strict'
module.exports = { module.exports = {
extends: ['plugin:eslint-comments/recommended', 'plugin:n/recommended', 'standard', 'standard-jsx', 'prettier'], extends: ['plugin:eslint-comments/recommended', 'standard', 'standard-jsx', 'prettier'],
globals: { globals: {
__DEV__: true, __DEV__: true,
$Dict: true, $Dict: true,
@@ -17,39 +15,10 @@ module.exports = {
{ {
files: ['cli.{,c,m}js', '*-cli.{,c,m}js', '**/*cli*/**/*.{,c,m}js'], files: ['cli.{,c,m}js', '*-cli.{,c,m}js', '**/*cli*/**/*.{,c,m}js'],
rules: { rules: {
'n/no-process-exit': 'off',
'no-console': 'off', 'no-console': 'off',
}, },
}, },
{
files: ['*.mjs'],
parserOptions: {
sourceType: 'module',
},
},
{
files: ['*.spec.{,c,m}js'],
rules: {
'n/no-unsupported-features/node-builtins': [
'error',
{
version: '>=16',
},
], ],
'n/no-unsupported-features/es-syntax': [
'error',
{
version: '>=16',
},
],
},
},
],
parserOptions: {
ecmaVersion: 13,
sourceType: 'script',
},
rules: { rules: {
// disabled because XAPI objects are using camel case // disabled because XAPI objects are using camel case
@@ -65,7 +34,5 @@ module.exports = {
'lines-between-class-members': 'off', 'lines-between-class-members': 'off',
'no-console': ['error', { allow: ['warn', 'error'] }], 'no-console': ['error', { allow: ['warn', 'error'] }],
strict: 'error',
}, },
} }

16
.flowconfig Normal file
View File

@@ -0,0 +1,16 @@
[ignore]
<PROJECT_ROOT>/node_modules/.*
[include]
[libs]
[lints]
[options]
esproposal.decorators=ignore
esproposal.optional_chaining=enable
include_warnings=true
module.use_strict=true
[strict]

View File

@@ -4,26 +4,14 @@ about: Create a report to help us improve
title: '' title: ''
labels: 'status: triaging :triangular_flag_on_post:, type: bug :bug:' labels: 'status: triaging :triangular_flag_on_post:, type: bug :bug:'
assignees: '' assignees: ''
--- ---
**XOA or XO from the sources?**
If XOA:
- which release channel? (`stable` vs `latest`)
- please consider creating a support ticket in [your dedicated support area](https://xen-orchestra.com/#!/member/support)
If XO from the sources:
- Don't forget to [read this first](https://xen-orchestra.com/docs/community.html)
- As well as follow [this guide](https://xen-orchestra.com/docs/community.html#report-a-bug)
**Describe the bug** **Describe the bug**
A clear and concise description of what the bug is. A clear and concise description of what the bug is.
**To Reproduce** **To Reproduce**
Steps to reproduce the behavior: Steps to reproduce the behavior:
1. Go to '...' 1. Go to '...'
2. Click on '....' 2. Click on '....'
3. Scroll down to '....' 3. Scroll down to '....'
@@ -35,8 +23,7 @@ A clear and concise description of what you expected to happen.
**Screenshots** **Screenshots**
If applicable, add screenshots to help explain your problem. If applicable, add screenshots to help explain your problem.
**Environment (please provide the following information):** **Desktop (please complete the following information):**
- Node: [e.g. 16.12.1] - Node: [e.g. 16.12.1]
- xo-server: [e.g. 5.82.3] - xo-server: [e.g. 5.82.3]
- xo-web: [e.g. 5.87.0] - xo-web: [e.g. 5.87.0]

View File

@@ -1,13 +0,0 @@
name: CI
on: [push]
jobs:
build:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: satackey/action-docker-layer-caching@v0.0.11
# Ignore the failure of a step and avoid terminating the job.
continue-on-error: true
- run: docker-compose -f docker/docker-compose.dev.yml build
- run: docker-compose -f docker/docker-compose.dev.yml up

7
.gitignore vendored
View File

@@ -1,4 +1,5 @@
/_book/ /_book/
/coverage/
/node_modules/ /node_modules/
/lerna-debug.log /lerna-debug.log
/lerna-debug.log.* /lerna-debug.log.*
@@ -10,6 +11,8 @@
/packages/*/dist/ /packages/*/dist/
/packages/*/node_modules/ /packages/*/node_modules/
/@xen-orchestra/proxy/src/app/mixins/index.mjs
/packages/vhd-cli/src/commands/index.js /packages/vhd-cli/src/commands/index.js
/packages/xen-api/examples/node_modules/ /packages/xen-api/examples/node_modules/
@@ -32,7 +35,3 @@ pnpm-debug.log.*
yarn-error.log yarn-error.log
yarn-error.log.* yarn-error.log.*
.env .env
# code coverage
.nyc_output/
coverage/

23
.travis.yml Normal file
View File

@@ -0,0 +1,23 @@
language: node_js
node_js:
- 14
# Use containers.
# http://docs.travis-ci.com/user/workers/container-based-infrastructure/
sudo: false
addons:
apt:
packages:
- qemu-utils
- blktap-utils
- vmdk-stream-converter
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH="$HOME/.yarn/bin:$PATH"
cache:
yarn: true
script:
- yarn run travis-tests

View File

@@ -1,30 +0,0 @@
Node does not cache queries to `dns.lookup`, which can lead application doing a lot of connections to have perf issues and to saturate Node threads pool.
This library attempts to mitigate these problems by providing a version of this function with a version short cache, applied on both errors and results.
> Limitation: `verbatim: false` option is not supported.
It has exactly the same API as the native method and can be used directly:
```js
import { createCachedLookup } from '@vates/cached-dns.lookup'
const lookup = createCachedLookup()
lookup('example.net', { all: true, family: 0 }, (error, result) => {
if (error != null) {
return console.warn(error)
}
console.log(result)
})
```
Or it can be used to replace the native implementation and speed up the whole app:
```js
// assign our cached implementation to dns.lookup
const restore = createCachedLookup().patchGlobal()
// to restore the previous implementation
restore()
```

View File

@@ -1 +0,0 @@
../../scripts/npmignore

View File

@@ -1,63 +0,0 @@
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
# @vates/cached-dns.lookup
[![Package Version](https://badgen.net/npm/v/@vates/cached-dns.lookup)](https://npmjs.org/package/@vates/cached-dns.lookup) ![License](https://badgen.net/npm/license/@vates/cached-dns.lookup) [![PackagePhobia](https://badgen.net/bundlephobia/minzip/@vates/cached-dns.lookup)](https://bundlephobia.com/result?p=@vates/cached-dns.lookup) [![Node compatibility](https://badgen.net/npm/node/@vates/cached-dns.lookup)](https://npmjs.org/package/@vates/cached-dns.lookup)
> Cached implementation of dns.lookup
## Install
Installation of the [npm package](https://npmjs.org/package/@vates/cached-dns.lookup):
```
> npm install --save @vates/cached-dns.lookup
```
## Usage
Node does not cache queries to `dns.lookup`, which can lead application doing a lot of connections to have perf issues and to saturate Node threads pool.
This library attempts to mitigate these problems by providing a version of this function with a version short cache, applied on both errors and results.
> Limitation: `verbatim: false` option is not supported.
It has exactly the same API as the native method and can be used directly:
```js
import { createCachedLookup } from '@vates/cached-dns.lookup'
const lookup = createCachedLookup()
lookup('example.net', { all: true, family: 0 }, (error, result) => {
if (error != null) {
return console.warn(error)
}
console.log(result)
})
```
Or it can be used to replace the native implementation and speed up the whole app:
```js
// assign our cached implementation to dns.lookup
const restore = createCachedLookup().patchGlobal()
// to restore the previous implementation
restore()
```
## Contributions
Contributions are _very_ welcomed, either on the documentation or on
the code.
You may:
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
you've encountered;
- fork and create a pull request.
## License
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)

View File

@@ -1,72 +0,0 @@
'use strict'
const assert = require('assert')
const dns = require('dns')
const LRU = require('lru-cache')
function reportResults(all, results, callback) {
if (all) {
callback(null, results)
} else {
const first = results[0]
callback(null, first.address, first.family)
}
}
exports.createCachedLookup = function createCachedLookup({ lookup = dns.lookup } = {}) {
const cache = new LRU({
max: 500,
// 1 minute: long enough to be effective, short enough so there is no need to bother with DNS TTLs
ttl: 60e3,
})
function cachedLookup(hostname, options, callback) {
let all = false
let family = 0
if (typeof options === 'function') {
callback = options
} else if (typeof options === 'number') {
family = options
} else if (options != null) {
assert.notStrictEqual(options.verbatim, false, 'not supported by this implementation')
;({ all = all, family = family } = options)
}
// cache by family option because there will be an error if there is no
// entries for the requestion family so we cannot easily cache all families
// and filter on reporting back
const key = hostname + '/' + family
const result = cache.get(key)
if (result !== undefined) {
setImmediate(reportResults, all, result, callback)
} else {
lookup(hostname, { all: true, family, verbatim: true }, function onLookup(error, results) {
// errors are not cached because this will delay recovery after DNS/network issues
//
// there are no reliable way to detect if the error is real or simply
// that there are no results for the requested hostname
//
// there should be much fewer errors than success, therefore it should
// not be a big deal to not cache them
if (error != null) {
return callback(error)
}
cache.set(key, results)
reportResults(all, results, callback)
})
}
}
cachedLookup.patchGlobal = function patchGlobal() {
const previous = dns.lookup
dns.lookup = cachedLookup
return function restoreGlobal() {
assert.strictEqual(dns.lookup, cachedLookup)
dns.lookup = previous
}
}
return cachedLookup
}

View File

@@ -1,32 +0,0 @@
{
"engines": {
"node": ">=8"
},
"dependencies": {
"lru-cache": "^7.0.4"
},
"private": false,
"name": "@vates/cached-dns.lookup",
"description": "Cached implementation of dns.lookup",
"keywords": [
"cache",
"dns",
"lookup"
],
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/cached-dns.lookup",
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
"directory": "@vates/cached-dns.lookup",
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"author": {
"name": "Vates SAS",
"url": "https://vates.fr"
},
"license": "ISC",
"version": "1.0.0",
"scripts": {
"postversion": "npm publish --access public"
}
}

View File

@@ -1,5 +1,3 @@
'use strict'
exports.coalesceCalls = function (fn) { exports.coalesceCalls = function (fn) {
let promise let promise
const clean = () => { const clean = () => {

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { coalesceCalls } = require('./') const { coalesceCalls } = require('./')

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { compose } = require('./') const { compose } = require('./')

View File

@@ -1,86 +0,0 @@
### `decorateWith(fn, ...args)`
Creates a new ([legacy](https://babeljs.io/docs/en/babel-plugin-syntax-decorators#legacy)) method decorator from a function decorator, for instance, allows using Lodash's functions as decorators:
```js
import { decorateWith } from '@vates/decorate-with'
class Foo {
@decorateWith(lodash.debounce, 150)
bar() {
// body
}
}
```
### `decorateClass(class, map)`
Decorates a number of accessors and methods directly, without using the decorator syntax:
```js
import { decorateClass } from '@vates/decorate-with'
class Foo {
get bar() {
// body
}
set bar(value) {
// body
}
baz() {
// body
}
}
decorateClass(Foo, {
// getter and/or setter
bar: {
// without arguments
get: lodash.memoize,
// with arguments
set: [lodash.debounce, 150],
},
// method (with or without arguments)
baz: lodash.curry,
})
```
The decorated class is returned, so you can export it directly.
To apply multiple transforms to an accessor/method, you can either call `decorateClass` multiple times or use [`@vates/compose`](https://www.npmjs.com/package/@vates/compose):
```js
decorateClass(Foo, {
baz: compose([
[lodash.debounce, 150]
lodash.curry,
])
})
```
### `perInstance(fn, ...args)`
Helper to decorate the method by instance instead of for the whole class.
This is often necessary for caching or deduplicating calls.
```js
import { perInstance } from '@vates/decorateWith'
class Foo {
@decorateWith(perInstance, lodash.memoize)
bar() {
// body
}
}
```
Because it's a normal function, it can also be used with `decorateClass`, with `compose` or even by itself.
### `decorateMethodsWith(class, map)`
> Deprecated alias for [`decorateClass(class, map)`](#decorateclassclass-map).

View File

@@ -31,19 +31,15 @@ class Foo {
} }
``` ```
### `decorateClass(class, map)` ### `decorateMethodsWith(class, map)`
Decorates a number of accessors and methods directly, without using the decorator syntax: Decorates a number of methods directly, without using the decorator syntax:
```js ```js
import { decorateClass } from '@vates/decorate-with' import { decorateMethodsWith } from '@vates/decorate-with'
class Foo { class Foo {
get bar() { bar() {
// body
}
set bar(value) {
// body // body
} }
@@ -52,57 +48,28 @@ class Foo {
} }
} }
decorateClass(Foo, { decorateMethodsWith(Foo, {
// getter and/or setter
bar: {
// without arguments // without arguments
get: lodash.memoize, bar: lodash.curry,
// with arguments // with arguments
set: [lodash.debounce, 150], baz: [lodash.debounce, 150],
},
// method (with or without arguments)
baz: lodash.curry,
}) })
``` ```
The decorated class is returned, so you can export it directly. The decorated class is returned, so you can export it directly.
To apply multiple transforms to an accessor/method, you can either call `decorateClass` multiple times or use [`@vates/compose`](https://www.npmjs.com/package/@vates/compose): To apply multiple transforms to a method, you can either call `decorateMethodsWith` multiple times or use [`@vates/compose`](https://www.npmjs.com/package/@vates/compose):
```js ```js
decorateClass(Foo, { decorateMethodsWith(Foo, {
baz: compose([ bar: compose([
[lodash.debounce, 150] [lodash.debounce, 150]
lodash.curry, lodash.curry,
]) ])
}) })
``` ```
### `perInstance(fn, ...args)`
Helper to decorate the method by instance instead of for the whole class.
This is often necessary for caching or deduplicating calls.
```js
import { perInstance } from '@vates/decorateWith'
class Foo {
@decorateWith(perInstance, lodash.memoize)
bar() {
// body
}
}
```
Because it's a normal function, it can also be used with `decorateClass`, with `compose` or even by itself.
### `decorateMethodsWith(class, map)`
> Deprecated alias for [`decorateClass(class, map)`](#decorateclassclass-map).
## Contributions ## Contributions
Contributions are _very_ welcomed, either on the documentation or on Contributions are _very_ welcomed, either on the documentation or on

View File

@@ -0,0 +1,53 @@
### `decorateWith(fn, ...args)`
Creates a new ([legacy](https://babeljs.io/docs/en/babel-plugin-syntax-decorators#legacy)) method decorator from a function decorator, for instance, allows using Lodash's functions as decorators:
```js
import { decorateWith } from '@vates/decorate-with'
class Foo {
@decorateWith(lodash.debounce, 150)
bar() {
// body
}
}
```
### `decorateMethodsWith(class, map)`
Decorates a number of methods directly, without using the decorator syntax:
```js
import { decorateMethodsWith } from '@vates/decorate-with'
class Foo {
bar() {
// body
}
baz() {
// body
}
}
decorateMethodsWith(Foo, {
// without arguments
bar: lodash.curry,
// with arguments
baz: [lodash.debounce, 150],
})
```
The decorated class is returned, so you can export it directly.
To apply multiple transforms to a method, you can either call `decorateMethodsWith` multiple times or use [`@vates/compose`](https://www.npmjs.com/package/@vates/compose):
```js
decorateMethodsWith(Foo, {
bar: compose([
[lodash.debounce, 150]
lodash.curry,
])
})
```

View File

@@ -1,5 +1,3 @@
'use strict'
exports.decorateWith = function decorateWith(fn, ...args) { exports.decorateWith = function decorateWith(fn, ...args) {
return (target, name, descriptor) => ({ return (target, name, descriptor) => ({
...descriptor, ...descriptor,
@@ -9,40 +7,15 @@ exports.decorateWith = function decorateWith(fn, ...args) {
const { getOwnPropertyDescriptor, defineProperty } = Object const { getOwnPropertyDescriptor, defineProperty } = Object
function applyDecorator(decorator, value) { exports.decorateMethodsWith = function decorateMethodsWith(klass, map) {
return typeof decorator === 'function' ? decorator(value) : decorator[0](value, ...decorator.slice(1))
}
exports.decorateClass = exports.decorateMethodsWith = function decorateClass(klass, map) {
const { prototype } = klass const { prototype } = klass
for (const name of Object.keys(map)) { for (const name of Object.keys(map)) {
const decorator = map[name]
const descriptor = getOwnPropertyDescriptor(prototype, name) const descriptor = getOwnPropertyDescriptor(prototype, name)
if (typeof decorator === 'function' || Array.isArray(decorator)) { const { value } = descriptor
descriptor.value = applyDecorator(decorator, descriptor.value)
} else {
const { get, set } = decorator
if (get !== undefined) {
descriptor.get = applyDecorator(get, descriptor.get)
}
if (set !== undefined) {
descriptor.set = applyDecorator(set, descriptor.set)
}
}
const decorator = map[name]
descriptor.value = typeof decorator === 'function' ? decorator(value) : decorator[0](value, ...decorator.slice(1))
defineProperty(prototype, name, descriptor) defineProperty(prototype, name, descriptor)
} }
return klass return klass
} }
exports.perInstance = function perInstance(fn, decorator, ...args) {
const map = new WeakMap()
return function () {
let decorated = map.get(this)
if (decorated === undefined) {
decorated = decorator(fn, ...args)
map.set(this, decorated)
}
return decorated.apply(this, arguments)
}
}

View File

@@ -1,152 +0,0 @@
'use strict'
const assert = require('assert')
const { describe, it } = require('tap').mocha
const { decorateClass, decorateWith, decorateMethodsWith, perInstance } = require('./')
const identity = _ => _
describe('decorateWith', () => {
it('works', () => {
const expectedArgs = [Math.random(), Math.random()]
const expectedFn = Function.prototype
const newFn = () => {}
const decorator = decorateWith(function wrapper(fn, ...args) {
assert.deepStrictEqual(fn, expectedFn)
assert.deepStrictEqual(args, expectedArgs)
return newFn
}, ...expectedArgs)
const descriptor = {
configurable: true,
enumerable: false,
value: expectedFn,
writable: true,
}
assert.deepStrictEqual(decorator({}, 'foo', descriptor), {
...descriptor,
value: newFn,
})
})
})
describe('decorateClass', () => {
it('works', () => {
class C {
foo() {}
bar() {}
get baz() {}
// eslint-disable-next-line accessor-pairs
set qux(_) {}
}
const expectedArgs = [Math.random(), Math.random()]
const P = C.prototype
const descriptors = Object.getOwnPropertyDescriptors(P)
const newFoo = () => {}
const newBar = () => {}
const newGetBaz = () => {}
const newSetQux = _ => {}
decorateClass(C, {
foo(fn) {
assert.strictEqual(arguments.length, 1)
assert.strictEqual(fn, P.foo)
return newFoo
},
bar: [
function (fn, ...args) {
assert.strictEqual(fn, P.bar)
assert.deepStrictEqual(args, expectedArgs)
return newBar
},
...expectedArgs,
],
baz: {
get(fn) {
assert.strictEqual(arguments.length, 1)
assert.strictEqual(fn, descriptors.baz.get)
return newGetBaz
},
},
qux: {
set: [
function (fn, ...args) {
assert.strictEqual(fn, descriptors.qux.set)
assert.deepStrictEqual(args, expectedArgs)
return newSetQux
},
...expectedArgs,
],
},
})
const newDescriptors = Object.getOwnPropertyDescriptors(P)
assert.deepStrictEqual(newDescriptors.foo, { ...descriptors.foo, value: newFoo })
assert.deepStrictEqual(newDescriptors.bar, { ...descriptors.bar, value: newBar })
assert.deepStrictEqual(newDescriptors.baz, { ...descriptors.baz, get: newGetBaz })
assert.deepStrictEqual(newDescriptors.qux, { ...descriptors.qux, set: newSetQux })
})
it('throws if using an accessor decorator for a method', function () {
assert.throws(() =>
decorateClass(
class {
foo() {}
},
{ foo: { get: identity, set: identity } }
)
)
})
it('throws if using a method decorator for an accessor', function () {
assert.throws(() =>
decorateClass(
class {
get foo() {}
},
{ foo: identity }
)
)
})
})
it('decorateMethodsWith is an alias of decorateClass', function () {
assert.strictEqual(decorateMethodsWith, decorateClass)
})
describe('perInstance', () => {
it('works', () => {
let calls = 0
const expectedArgs = [Math.random(), Math.random()]
const expectedFn = Function.prototype
function wrapper(fn, ...args) {
assert.strictEqual(fn, expectedFn)
assert.deepStrictEqual(args, expectedArgs)
const i = ++calls
return () => i
}
const wrapped = perInstance(expectedFn, wrapper, ...expectedArgs)
// decorator is not called before decorated called
assert.strictEqual(calls, 0)
const o1 = {}
const o2 = {}
assert.strictEqual(wrapped.call(o1), 1)
// the same decorated function is returned for the same instance
assert.strictEqual(wrapped.call(o1), 1)
// a new decorated function is returned for another instance
assert.strictEqual(wrapped.call(o2), 2)
})
})

View File

@@ -20,15 +20,11 @@
"url": "https://vates.fr" "url": "https://vates.fr"
}, },
"license": "ISC", "license": "ISC",
"version": "2.0.0", "version": "0.1.0",
"engines": { "engines": {
"node": ">=8.10" "node": ">=8.10"
}, },
"scripts": { "scripts": {
"postversion": "npm publish --access public", "postversion": "npm publish --access public"
"test": "tap"
},
"devDependencies": {
"tap": "^16.0.1"
} }
} }

View File

@@ -1,5 +1,3 @@
'use strict'
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { createLogger } = require('@xen-orchestra/log') const { createLogger } = require('@xen-orchestra/log')

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { createDebounceResource } = require('./debounceResource') const { createDebounceResource } = require('./debounceResource')

View File

@@ -1,5 +1,3 @@
'use strict'
const ensureArray = require('ensure-array') const ensureArray = require('ensure-array')
const { MultiKeyMap } = require('@vates/multi-key-map') const { MultiKeyMap } = require('@vates/multi-key-map')

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { deduped } = require('./deduped') const { deduped } = require('./deduped')

View File

@@ -1,50 +0,0 @@
> This library is compatible with Node's `EventEmitter` and web browsers' `EventTarget` APIs.
### API
```js
import { EventListenersManager } from '@vates/event-listeners-manager'
const events = new EventListenersManager(emitter)
// adding listeners
events.add('foo', onFoo).add('bar', onBar).on('baz', onBaz)
// removing a specific listener
events.remove('foo', onFoo)
// removing all listeners for a specific event
events.removeAll('foo')
// removing all listeners
events.removeAll()
```
### Typical use case
> Removing all listeners when no longer necessary.
Manually:
```js
const onFoo = () => {}
const onBar = () => {}
const onBaz = () => {}
emitter.on('foo', onFoo).on('bar', onBar).on('baz', onBaz)
// CODE LOGIC
emitter.off('foo', onFoo).off('bar', onBar).off('baz', onBaz)
```
With this library:
```js
const events = new EventListenersManager(emitter)
events.add('foo', () => {})).add('bar', () => {})).add('baz', () => {}))
// CODE LOGIC
events.removeAll()
```

View File

@@ -1 +0,0 @@
../../scripts/npmignore

View File

@@ -1,81 +0,0 @@
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
# @vates/event-listeners-manager
[![Package Version](https://badgen.net/npm/v/@vates/event-listeners-manager)](https://npmjs.org/package/@vates/event-listeners-manager) ![License](https://badgen.net/npm/license/@vates/event-listeners-manager) [![PackagePhobia](https://badgen.net/bundlephobia/minzip/@vates/event-listeners-manager)](https://bundlephobia.com/result?p=@vates/event-listeners-manager) [![Node compatibility](https://badgen.net/npm/node/@vates/event-listeners-manager)](https://npmjs.org/package/@vates/event-listeners-manager)
## Install
Installation of the [npm package](https://npmjs.org/package/@vates/event-listeners-manager):
```
> npm install --save @vates/event-listeners-manager
```
## Usage
> This library is compatible with Node's `EventEmitter` and web browsers' `EventTarget` APIs.
### API
```js
import { EventListenersManager } from '@vates/event-listeners-manager'
const events = new EventListenersManager(emitter)
// adding listeners
events.add('foo', onFoo).add('bar', onBar).on('baz', onBaz)
// removing a specific listener
events.remove('foo', onFoo)
// removing all listeners for a specific event
events.removeAll('foo')
// removing all listeners
events.removeAll()
```
### Typical use case
> Removing all listeners when no longer necessary.
Manually:
```js
const onFoo = () => {}
const onBar = () => {}
const onBaz = () => {}
emitter.on('foo', onFoo).on('bar', onBar).on('baz', onBaz)
// CODE LOGIC
emitter.off('foo', onFoo).off('bar', onBar).off('baz', onBaz)
```
With this library:
```js
const events = new EventListenersManager(emitter)
events.add('foo', () => {})).add('bar', () => {})).add('baz', () => {}))
// CODE LOGIC
events.removeAll()
```
## Contributions
Contributions are _very_ welcomed, either on the documentation or on
the code.
You may:
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
you've encountered;
- fork and create a pull request.
## License
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)

View File

@@ -1,56 +0,0 @@
'use strict'
exports.EventListenersManager = class EventListenersManager {
constructor(emitter) {
this._listeners = new Map()
this._add = (emitter.addListener || emitter.addEventListener).bind(emitter)
this._remove = (emitter.removeListener || emitter.removeEventListener).bind(emitter)
}
add(type, listener) {
let listeners = this._listeners[type]
if (listeners === undefined) {
listeners = new Set()
this._listeners.set(type, listeners)
}
// don't add the same listener multiple times (allowed on Node.js)
if (!listeners.has(listener)) {
listeners.add(listener)
this._add(type, listener)
}
return this
}
remove(type, listener) {
const allListeners = this._listeners
const listeners = allListeners.get(type)
if (listeners !== undefined && listeners.delete(listener)) {
this._remove(type, listener)
if (listeners.size === 0) {
allListeners.delete(type)
}
}
return this
}
removeAll(type) {
const allListeners = this._listeners
const remove = this._remove
const types = type !== undefined ? [type] : allListeners.keys()
for (const type of types) {
const listeners = allListeners.get(type)
if (listeners !== undefined) {
allListeners.delete(type)
for (const listener of listeners) {
remove(type, listener)
}
}
}
return this
}
}

View File

@@ -1,42 +0,0 @@
{
"engines": {
"node": ">=6"
},
"private": false,
"name": "@vates/event-listeners-manager",
"descriptions": "Easy way to clean up event listeners",
"keywords": [
"add",
"addEventListener",
"addListener",
"browser",
"clear",
"DOM",
"emitter",
"event",
"EventEmitter",
"EventTarget",
"management",
"manager",
"node",
"remove",
"removeEventListener",
"removeListener"
],
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/event-listeners-manager",
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
"directory": "@vates/event-listeners-manager",
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"author": {
"name": "Vates SAS",
"url": "https://vates.fr"
},
"license": "ISC",
"version": "1.0.0",
"scripts": {
"postversion": "npm publish --access public"
}
}

View File

@@ -1,5 +1,3 @@
'use strict'
class Node { class Node {
constructor(value) { constructor(value) {
this.children = new Map() this.children = new Map()

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { MultiKeyMap } = require('./') const { MultiKeyMap } = require('./')

View File

@@ -1,5 +1,3 @@
'use strict'
const ms = require('ms') const ms = require('ms')
exports.parseDuration = value => { exports.parseDuration = value => {

View File

@@ -1,57 +0,0 @@
`undefined` predicates are ignored and `undefined` is returned if all predicates are `undefined`, this permits the most efficient composition:
```js
const compositePredicate = every(undefined, some(predicate2, undefined))
// ends up as
const compositePredicate = predicate2
```
Predicates can also be passed wrapped in an array:
```js
const compositePredicate = every([predicate1, some([predicate2, predicate3])])
```
`this` and all arguments are passed to the nested predicates.
### `every(predicates)`
> Returns a predicate that returns `true` iff every predicate returns `true`.
```js
const isBetween3And7 = every(
n => n >= 3,
n => n <= 7
)
isBetween3And10(0)
// → false
isBetween3And10(5)
// → true
isBetween3And10(10)
// → false
```
### `some(predicates)`
> Returns a predicate that returns `true` iff some predicate returns `true`.
```js
const isAliceOrBob = some(
name => name === 'Alice',
name => name === 'Bob'
)
isAliceOrBob('Alice')
// → true
isAliceOrBob('Bob')
// → true
isAliceOrBob('Oscar')
// → false
```

View File

@@ -1 +0,0 @@
../../scripts/npmignore

View File

@@ -1,90 +0,0 @@
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
# @vates/predicates
[![Package Version](https://badgen.net/npm/v/@vates/predicates)](https://npmjs.org/package/@vates/predicates) ![License](https://badgen.net/npm/license/@vates/predicates) [![PackagePhobia](https://badgen.net/bundlephobia/minzip/@vates/predicates)](https://bundlephobia.com/result?p=@vates/predicates) [![Node compatibility](https://badgen.net/npm/node/@vates/predicates)](https://npmjs.org/package/@vates/predicates)
> Utilities to compose predicates
## Install
Installation of the [npm package](https://npmjs.org/package/@vates/predicates):
```
> npm install --save @vates/predicates
```
## Usage
`undefined` predicates are ignored and `undefined` is returned if all predicates are `undefined`, this permits the most efficient composition:
```js
const compositePredicate = every(undefined, some(predicate2, undefined))
// ends up as
const compositePredicate = predicate2
```
Predicates can also be passed wrapped in an array:
```js
const compositePredicate = every([predicate1, some([predicate2, predicate3])])
```
`this` and all arguments are passed to the nested predicates.
### `every(predicates)`
> Returns a predicate that returns `true` iff every predicate returns `true`.
```js
const isBetween3And7 = every(
n => n >= 3,
n => n <= 7
)
isBetween3And10(0)
// → false
isBetween3And10(5)
// → true
isBetween3And10(10)
// → false
```
### `some(predicates)`
> Returns a predicate that returns `true` iff some predicate returns `true`.
```js
const isAliceOrBob = some(
name => name === 'Alice',
name => name === 'Bob'
)
isAliceOrBob('Alice')
// → true
isAliceOrBob('Bob')
// → true
isAliceOrBob('Oscar')
// → false
```
## Contributions
Contributions are _very_ welcomed, either on the documentation or on
the code.
You may:
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
you've encountered;
- fork and create a pull request.
## License
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)

View File

@@ -1,71 +0,0 @@
'use strict'
const {
isArray,
prototype: { filter },
} = Array
class InvalidPredicate extends TypeError {
constructor(value) {
super('not a valid predicate')
this.value = value
}
}
function isDefinedPredicate(value) {
if (value === undefined) {
return false
}
if (typeof value !== 'function') {
throw new InvalidPredicate(value)
}
return true
}
function handleArgs() {
let predicates
if (!(arguments.length === 1 && isArray((predicates = arguments[0])))) {
predicates = arguments
}
return filter.call(predicates, isDefinedPredicate)
}
exports.every = function every() {
const predicates = handleArgs.apply(this, arguments)
const n = predicates.length
if (n === 0) {
return
}
if (n === 1) {
return predicates[0]
}
return function everyPredicate() {
for (let i = 0; i < n; ++i) {
if (!predicates[i].apply(this, arguments)) {
return false
}
}
return true
}
}
exports.some = function some() {
const predicates = handleArgs.apply(this, arguments)
const n = predicates.length
if (n === 0) {
return
}
if (n === 1) {
return predicates[0]
}
return function somePredicate() {
for (let i = 0; i < n; ++i) {
if (predicates[i].apply(this, arguments)) {
return true
}
}
return false
}
}

View File

@@ -1,65 +0,0 @@
'use strict'
const assert = require('assert/strict')
const { describe, it } = require('tap').mocha
const { every, some } = require('./')
const T = () => true
const F = () => false
const testArgsHandling = fn => {
it('returns undefined if all predicates are undefined', () => {
assert.equal(fn(undefined), undefined)
assert.equal(fn([undefined]), undefined)
})
it('returns the predicate if only a single one is passed', () => {
assert.equal(fn(undefined, T), T)
assert.equal(fn([undefined, T]), T)
})
it('throws if it receives a non-predicate', () => {
const error = new TypeError('not a valid predicate')
error.value = 3
assert.throws(() => fn(3), error)
})
it('forwards this and arguments to predicates', () => {
const thisArg = 'qux'
const args = ['foo', 'bar', 'baz']
const predicate = function () {
assert.equal(this, thisArg)
assert.deepEqual(Array.from(arguments), args)
}
fn(predicate, predicate).apply(thisArg, args)
})
}
const runTests = (fn, truthTable) =>
it('works', () => {
truthTable.forEach(([result, ...predicates]) => {
assert.equal(fn(...predicates)(), result)
assert.equal(fn(predicates)(), result)
})
})
describe('every', () => {
testArgsHandling(every)
runTests(every, [
[true, T, T],
[false, T, F],
[false, F, T],
[false, F, F],
])
})
describe('some', () => {
testArgsHandling(some)
runTests(some, [
[true, T, T],
[true, T, F],
[true, F, T],
[false, F, F],
])
})

View File

@@ -1,40 +0,0 @@
{
"private": false,
"name": "@vates/predicates",
"description": "Utilities to compose predicates",
"keywords": [
"and",
"combine",
"compose",
"every",
"function",
"functions",
"or",
"predicate",
"predicates",
"some"
],
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/predicates",
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
"directory": "@vates/predicates",
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"author": {
"name": "Vates SAS",
"url": "https://vates.fr"
},
"license": "ISC",
"version": "1.0.0",
"engines": {
"node": ">=6"
},
"scripts": {
"postversion": "npm publish --access public",
"test": "tap"
},
"devDependencies": {
"tap": "^16.0.1"
}
}

View File

@@ -1,5 +1,3 @@
'use strict'
const readChunk = (stream, size) => const readChunk = (stream, size) =>
size === 0 size === 0
? Promise.resolve(Buffer.alloc(0)) ? Promise.resolve(Buffer.alloc(0))

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { Readable } = require('stream') const { Readable } = require('stream')

View File

@@ -1,7 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'
const fs = require('fs') const fs = require('fs')
const mapKeys = (object, iteratee) => { const mapKeys = (object, iteratee) => {

View File

@@ -1,5 +1,3 @@
'use strict'
const wrapCall = (fn, arg, thisArg) => { const wrapCall = (fn, arg, thisArg) => {
try { try {
return Promise.resolve(fn.call(thisArg, arg)) return Promise.resolve(fn.call(thisArg, arg))

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const { asyncMapSettled } = require('./') const { asyncMapSettled } = require('./')

View File

@@ -1,5 +1,3 @@
'use strict'
// type MaybePromise<T> = Promise<T> | T // type MaybePromise<T> = Promise<T> | T
// //
// declare export function asyncMap<T1, T2>( // declare export function asyncMap<T1, T2>(

View File

@@ -0,0 +1 @@
module.exports = require('../../@xen-orchestra/babel-config')(require('./package.json'))

View File

@@ -9,14 +9,28 @@
}, },
"version": "0.2.0", "version": "0.2.0",
"engines": { "engines": {
"node": ">=14" "node": ">=10"
}, },
"main": "dist/",
"scripts": { "scripts": {
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
"postversion": "npm publish --access public", "postversion": "npm publish --access public",
"test": "tap --lines 67 --functions 92 --branches 52 --statements 67" "prebuild": "rimraf dist/",
"predev": "yarn run prebuild",
"prepublishOnly": "yarn run build"
},
"devDependencies": {
"@babel/cli": "^7.7.4",
"@babel/core": "^7.7.4",
"@babel/plugin-proposal-decorators": "^7.8.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.0",
"@babel/preset-env": "^7.7.4",
"cross-env": "^7.0.2",
"rimraf": "^3.0.0"
}, },
"dependencies": { "dependencies": {
"@vates/decorate-with": "^2.0.0", "@vates/decorate-with": "^0.1.0",
"@xen-orchestra/log": "^0.3.0", "@xen-orchestra/log": "^0.3.0",
"golike-defer": "^0.5.1", "golike-defer": "^0.5.1",
"object-hash": "^2.0.1" "object-hash": "^2.0.1"
@@ -26,8 +40,5 @@
"author": { "author": {
"name": "Vates SAS", "name": "Vates SAS",
"url": "https://vates.fr" "url": "https://vates.fr"
},
"devDependencies": {
"tap": "^16.0.1"
} }
} }

View File

@@ -1,14 +1,12 @@
'use strict' import assert from 'assert'
import hash from 'object-hash'
const assert = require('assert') import { createLogger } from '@xen-orchestra/log'
const hash = require('object-hash') import { decorateWith } from '@vates/decorate-with'
const { createLogger } = require('@xen-orchestra/log') import { defer } from 'golike-defer'
const { decorateClass } = require('@vates/decorate-with')
const { defer } = require('golike-defer')
const log = createLogger('xo:audit-core') const log = createLogger('xo:audit-core')
exports.Storage = class Storage { export class Storage {
constructor() { constructor() {
this._lock = Promise.resolve() this._lock = Promise.resolve()
} }
@@ -31,7 +29,7 @@ const ID_TO_ALGORITHM = {
5: 'sha256', 5: 'sha256',
} }
class AlteredRecordError extends Error { export class AlteredRecordError extends Error {
constructor(id, nValid, record) { constructor(id, nValid, record) {
super('altered record') super('altered record')
@@ -40,9 +38,8 @@ class AlteredRecordError extends Error {
this.record = record this.record = record
} }
} }
exports.AlteredRecordError = AlteredRecordError
class MissingRecordError extends Error { export class MissingRecordError extends Error {
constructor(id, nValid) { constructor(id, nValid) {
super('missing record') super('missing record')
@@ -50,10 +47,8 @@ class MissingRecordError extends Error {
this.nValid = nValid this.nValid = nValid
} }
} }
exports.MissingRecordError = MissingRecordError
const NULL_ID = 'nullId' export const NULL_ID = 'nullId'
exports.NULL_ID = NULL_ID
const HASH_ALGORITHM_ID = '5' const HASH_ALGORITHM_ID = '5'
const createHash = (data, algorithmId = HASH_ALGORITHM_ID) => const createHash = (data, algorithmId = HASH_ALGORITHM_ID) =>
@@ -62,12 +57,13 @@ const createHash = (data, algorithmId = HASH_ALGORITHM_ID) =>
excludeKeys: key => key === 'id', excludeKeys: key => key === 'id',
})}` })}`
class AuditCore { export class AuditCore {
constructor(storage) { constructor(storage) {
assert.notStrictEqual(storage, undefined) assert.notStrictEqual(storage, undefined)
this._storage = storage this._storage = storage
} }
@decorateWith(defer)
async add($defer, subject, event, data) { async add($defer, subject, event, data) {
const time = Date.now() const time = Date.now()
$defer(await this._storage.acquireLock()) $defer(await this._storage.acquireLock())
@@ -152,6 +148,7 @@ class AuditCore {
} }
} }
@decorateWith(defer)
async deleteRangeAndRewrite($defer, newest, oldest) { async deleteRangeAndRewrite($defer, newest, oldest) {
assert.notStrictEqual(newest, undefined) assert.notStrictEqual(newest, undefined)
assert.notStrictEqual(oldest, undefined) assert.notStrictEqual(oldest, undefined)
@@ -192,9 +189,3 @@ class AuditCore {
} }
} }
} }
exports.AuditCore = AuditCore
decorateClass(AuditCore, {
add: defer,
deleteRangeAndRewrite: defer,
})

View File

@@ -1,9 +1,6 @@
'use strict' /* eslint-env jest */
const assert = require('assert/strict') import { AlteredRecordError, AuditCore, MissingRecordError, NULL_ID, Storage } from '.'
const { afterEach, describe, it } = require('tap').mocha
const { AlteredRecordError, AuditCore, MissingRecordError, NULL_ID, Storage } = require('.')
const asyncIteratorToArray = async asyncIterator => { const asyncIteratorToArray = async asyncIterator => {
const array = [] const array = []
@@ -75,7 +72,7 @@ const auditCore = new AuditCore(db)
const storeAuditRecords = async () => { const storeAuditRecords = async () => {
await Promise.all(DATA.map(data => auditCore.add(...data))) await Promise.all(DATA.map(data => auditCore.add(...data)))
const records = await asyncIteratorToArray(auditCore.getFrom()) const records = await asyncIteratorToArray(auditCore.getFrom())
assert.equal(records.length, DATA.length) expect(records.length).toBe(DATA.length)
return records return records
} }
@@ -86,11 +83,10 @@ describe('auditCore', () => {
const [newestRecord, deletedRecord] = await storeAuditRecords() const [newestRecord, deletedRecord] = await storeAuditRecords()
const nValidRecords = await auditCore.checkIntegrity(NULL_ID, newestRecord.id) const nValidRecords = await auditCore.checkIntegrity(NULL_ID, newestRecord.id)
assert.equal(nValidRecords, DATA.length) expect(nValidRecords).toBe(DATA.length)
await db.del(deletedRecord.id) await db.del(deletedRecord.id)
await assert.rejects( await expect(auditCore.checkIntegrity(NULL_ID, newestRecord.id)).rejects.toEqual(
auditCore.checkIntegrity(NULL_ID, newestRecord.id),
new MissingRecordError(deletedRecord.id, 1) new MissingRecordError(deletedRecord.id, 1)
) )
}) })
@@ -101,8 +97,7 @@ describe('auditCore', () => {
alteredRecord.event = '' alteredRecord.event = ''
await db.put(alteredRecord) await db.put(alteredRecord)
await assert.rejects( await expect(auditCore.checkIntegrity(NULL_ID, newestRecord.id)).rejects.toEqual(
auditCore.checkIntegrity(NULL_ID, newestRecord.id),
new AlteredRecordError(alteredRecord.id, 1, alteredRecord) new AlteredRecordError(alteredRecord.id, 1, alteredRecord)
) )
}) })
@@ -112,8 +107,8 @@ describe('auditCore', () => {
await auditCore.deleteFrom(secondRecord.id) await auditCore.deleteFrom(secondRecord.id)
assert.equal(await db.get(firstRecord.id), undefined) expect(await db.get(firstRecord.id)).toBe(undefined)
assert.equal(await db.get(secondRecord.id), undefined) expect(await db.get(secondRecord.id)).toBe(undefined)
await auditCore.checkIntegrity(secondRecord.id, thirdRecord.id) await auditCore.checkIntegrity(secondRecord.id, thirdRecord.id)
}) })

View File

@@ -10,7 +10,7 @@
"url": "https://github.com/vatesfr/xen-orchestra.git" "url": "https://github.com/vatesfr/xen-orchestra.git"
}, },
"engines": { "engines": {
"node": ">=8.3" "node": ">=6"
}, },
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"author": { "author": {

View File

@@ -1,5 +1,3 @@
'use strict'
const getopts = require('getopts') const getopts = require('getopts')
const { version } = require('./package.json') const { version } = require('./package.json')

View File

@@ -1,5 +1,3 @@
'use strict'
const { dirname } = require('path') const { dirname } = require('path')
const fs = require('promise-toolbox/promisifyAll')(require('fs')) const fs = require('promise-toolbox/promisifyAll')(require('fs'))

View File

@@ -1,4 +1,4 @@
'use strict' #!/usr/bin/env node
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@@ -1,5 +1,3 @@
'use strict'
const filenamify = require('filenamify') const filenamify = require('filenamify')
const get = require('lodash/get') const get = require('lodash/get')
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')

View File

@@ -1,5 +1,3 @@
'use strict'
const groupBy = require('lodash/groupBy') const groupBy = require('lodash/groupBy')
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { createHash } = require('crypto') const { createHash } = require('crypto')

View File

@@ -1,7 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'
require('./_composeCommands')({ require('./_composeCommands')({
'clean-vms': { 'clean-vms': {
get main() { get main() {

View File

@@ -7,12 +7,12 @@
"bugs": "https://github.com/vatesfr/xen-orchestra/issues", "bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"dependencies": { "dependencies": {
"@xen-orchestra/async-map": "^0.1.2", "@xen-orchestra/async-map": "^0.1.2",
"@xen-orchestra/backups": "^0.22.0", "@xen-orchestra/backups": "^0.16.2",
"@xen-orchestra/fs": "^1.0.1", "@xen-orchestra/fs": "^0.19.2",
"filenamify": "^4.1.0", "filenamify": "^4.1.0",
"getopts": "^2.2.5", "getopts": "^2.2.5",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"promise-toolbox": "^0.21.0" "promise-toolbox": "^0.20.0"
}, },
"engines": { "engines": {
"node": ">=7.10.1" "node": ">=7.10.1"
@@ -27,7 +27,7 @@
"scripts": { "scripts": {
"postversion": "npm publish --access public" "postversion": "npm publish --access public"
}, },
"version": "0.7.1", "version": "0.6.1",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"author": { "author": {
"name": "Vates SAS", "name": "Vates SAS",

View File

@@ -1,8 +1,6 @@
'use strict'
const { asyncMap, asyncMapSettled } = require('@xen-orchestra/async-map') const { asyncMap, asyncMapSettled } = require('@xen-orchestra/async-map')
const Disposable = require('promise-toolbox/Disposable') const Disposable = require('promise-toolbox/Disposable.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors') const ignoreErrors = require('promise-toolbox/ignoreErrors.js')
const { compileTemplate } = require('@xen-orchestra/template') const { compileTemplate } = require('@xen-orchestra/template')
const { limitConcurrency } = require('limit-concurrency-decorator') const { limitConcurrency } = require('limit-concurrency-decorator')

View File

@@ -1,5 +1,3 @@
'use strict'
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
exports.DurablePartition = class DurablePartition { exports.DurablePartition = class DurablePartition {

View File

@@ -1,5 +1,3 @@
'use strict'
const assert = require('assert') const assert = require('assert')
const { formatFilenameDate } = require('./_filenameDate.js') const { formatFilenameDate } = require('./_filenameDate.js')
@@ -8,9 +6,9 @@ const { Task } = require('./Task.js')
const { watchStreamSize } = require('./_watchStreamSize.js') const { watchStreamSize } = require('./_watchStreamSize.js')
exports.ImportVmBackup = class ImportVmBackup { exports.ImportVmBackup = class ImportVmBackup {
constructor({ adapter, metadata, srUuid, xapi, settings: { newMacAddresses, mapVdisSrs = {} } = {} }) { constructor({ adapter, metadata, srUuid, xapi, settings: { newMacAddresses } = {} }) {
this._adapter = adapter this._adapter = adapter
this._importDeltaVmSettings = { newMacAddresses, mapVdisSrs } this._importDeltaVmSettings = { newMacAddresses }
this._metadata = metadata this._metadata = metadata
this._srUuid = srUuid this._srUuid = srUuid
this._xapi = xapi this._xapi = xapi
@@ -30,12 +28,7 @@ exports.ImportVmBackup = class ImportVmBackup {
} else { } else {
assert.strictEqual(metadata.mode, 'delta') assert.strictEqual(metadata.mode, 'delta')
const ignoredVdis = new Set( backup = await adapter.readDeltaVmBackup(metadata)
Object.entries(this._importDeltaVmSettings.mapVdisSrs)
.filter(([_, srUuid]) => srUuid === null)
.map(([vdiUuid]) => vdiUuid)
)
backup = await adapter.readDeltaVmBackup(metadata, ignoredVdis)
Object.values(backup.streams).forEach(stream => watchStreamSize(stream, sizeContainer)) Object.values(backup.streams).forEach(stream => watchStreamSize(stream, sizeContainer))
} }

View File

@@ -1,18 +1,12 @@
'use strict'
const { asyncMap, asyncMapSettled } = require('@xen-orchestra/async-map') const { asyncMap, asyncMapSettled } = require('@xen-orchestra/async-map')
const Disposable = require('promise-toolbox/Disposable') const Disposable = require('promise-toolbox/Disposable.js')
const fromCallback = require('promise-toolbox/fromCallback') const fromCallback = require('promise-toolbox/fromCallback.js')
const fromEvent = require('promise-toolbox/fromEvent') const fromEvent = require('promise-toolbox/fromEvent.js')
const pDefer = require('promise-toolbox/defer') const pDefer = require('promise-toolbox/defer.js')
const groupBy = require('lodash/groupBy.js')
const pickBy = require('lodash/pickBy.js')
const { dirname, join, normalize, resolve } = require('path') const { dirname, join, normalize, resolve } = require('path')
const { createLogger } = require('@xen-orchestra/log') const { createLogger } = require('@xen-orchestra/log')
const { Constants, createVhdDirectoryFromStream, openVhd, VhdAbstract, VhdDirectory, VhdSynthetic } = require('vhd-lib') const { Constants, createVhdDirectoryFromStream, openVhd, VhdAbstract, VhdSynthetic } = require('vhd-lib')
const { deduped } = require('@vates/disposable/deduped.js') const { deduped } = require('@vates/disposable/deduped.js')
const { decorateMethodsWith } = require('@vates/decorate-with')
const { compose } = require('@vates/compose')
const { execFile } = require('child_process') const { execFile } = require('child_process')
const { readdir, stat } = require('fs-extra') const { readdir, stat } = require('fs-extra')
const { v4: uuidv4 } = require('uuid') const { v4: uuidv4 } = require('uuid')
@@ -32,7 +26,7 @@ exports.DIR_XO_CONFIG_BACKUPS = DIR_XO_CONFIG_BACKUPS
const DIR_XO_POOL_METADATA_BACKUPS = 'xo-pool-metadata-backups' const DIR_XO_POOL_METADATA_BACKUPS = 'xo-pool-metadata-backups'
exports.DIR_XO_POOL_METADATA_BACKUPS = DIR_XO_POOL_METADATA_BACKUPS exports.DIR_XO_POOL_METADATA_BACKUPS = DIR_XO_POOL_METADATA_BACKUPS
const { debug, warn } = createLogger('xo:backups:RemoteAdapter') const { warn } = createLogger('xo:backups:RemoteAdapter')
const compareTimestamp = (a, b) => a.timestamp - b.timestamp const compareTimestamp = (a, b) => a.timestamp - b.timestamp
@@ -73,11 +67,10 @@ const debounceResourceFactory = factory =>
} }
class RemoteAdapter { class RemoteAdapter {
constructor(handler, { debounceResource = res => res, dirMode, vhdDirectoryCompression } = {}) { constructor(handler, { debounceResource = res => res, dirMode } = {}) {
this._debounceResource = debounceResource this._debounceResource = debounceResource
this._dirMode = dirMode this._dirMode = dirMode
this._handler = handler this._handler = handler
this._vhdDirectoryCompression = vhdDirectoryCompression
} }
get handler() { get handler() {
@@ -93,6 +86,9 @@ class RemoteAdapter {
return partition return partition
} }
_getLvmLogicalVolumes = Disposable.factory(this._getLvmLogicalVolumes)
_getLvmLogicalVolumes = deduped(this._getLvmLogicalVolumes, (devicePath, pvId, vgName) => [devicePath, pvId, vgName])
_getLvmLogicalVolumes = debounceResourceFactory(this._getLvmLogicalVolumes)
async *_getLvmLogicalVolumes(devicePath, pvId, vgName) { async *_getLvmLogicalVolumes(devicePath, pvId, vgName) {
yield this._getLvmPhysicalVolume(devicePath, pvId && (await this._findPartition(devicePath, pvId))) yield this._getLvmPhysicalVolume(devicePath, pvId && (await this._findPartition(devicePath, pvId)))
@@ -104,6 +100,9 @@ class RemoteAdapter {
} }
} }
_getLvmPhysicalVolume = Disposable.factory(this._getLvmPhysicalVolume)
_getLvmPhysicalVolume = deduped(this._getLvmPhysicalVolume, (devicePath, partition) => [devicePath, partition?.id])
_getLvmPhysicalVolume = debounceResourceFactory(this._getLvmPhysicalVolume)
async *_getLvmPhysicalVolume(devicePath, partition) { async *_getLvmPhysicalVolume(devicePath, partition) {
const args = [] const args = []
if (partition !== undefined) { if (partition !== undefined) {
@@ -124,6 +123,9 @@ class RemoteAdapter {
} }
} }
_getPartition = Disposable.factory(this._getPartition)
_getPartition = deduped(this._getPartition, (devicePath, partition) => [devicePath, partition?.id])
_getPartition = debounceResourceFactory(this._getPartition)
async *_getPartition(devicePath, partition) { async *_getPartition(devicePath, partition) {
const options = ['loop', 'ro'] const options = ['loop', 'ro']
@@ -176,6 +178,7 @@ class RemoteAdapter {
}) })
} }
_usePartitionFiles = Disposable.factory(this._usePartitionFiles)
async *_usePartitionFiles(diskId, partitionId, paths) { async *_usePartitionFiles(diskId, partitionId, paths) {
const path = yield this.getPartition(diskId, partitionId) const path = yield this.getPartition(diskId, partitionId)
@@ -187,22 +190,6 @@ class RemoteAdapter {
return files return files
} }
// check if we will be allowed to merge a a vhd created in this adapter
// with the vhd at path `path`
async isMergeableParent(packedParentUid, path) {
return await Disposable.use(openVhd(this.handler, path), vhd => {
// this baseUuid is not linked with this vhd
if (!vhd.footer.uuid.equals(packedParentUid)) {
return false
}
const isVhdDirectory = vhd instanceof VhdDirectory
return isVhdDirectory
? this.#useVhdDirectory() && this.#getCompressionType() === vhd.compressionType
: !this.#useVhdDirectory()
})
}
fetchPartitionFiles(diskId, partitionId, paths) { fetchPartitionFiles(diskId, partitionId, paths) {
const { promise, reject, resolve } = pDefer() const { promise, reject, resolve } = pDefer()
Disposable.use( Disposable.use(
@@ -224,9 +211,9 @@ class RemoteAdapter {
async deleteDeltaVmBackups(backups) { async deleteDeltaVmBackups(backups) {
const handler = this._handler const handler = this._handler
debug(`deleteDeltaVmBackups will delete ${backups.length} delta backups`, { backups })
// this will delete the json, unused VHDs will be detected by `cleanVm` // unused VHDs will be detected by `cleanVm`
await asyncMapSettled(backups, ({ _filename }) => handler.unlink(_filename)) await asyncMapSettled(backups, ({ _filename }) => VhdAbstract.unlink(handler, _filename))
} }
async deleteMetadataBackup(backupId) { async deleteMetadataBackup(backupId) {
@@ -256,42 +243,22 @@ class RemoteAdapter {
) )
} }
deleteVmBackup(file) { async deleteVmBackup(filename) {
return this.deleteVmBackups([file]) const metadata = JSON.parse(String(await this._handler.readFile(filename)))
} metadata._filename = filename
async deleteVmBackups(files) { if (metadata.mode === 'delta') {
const { delta, full, ...others } = groupBy(await asyncMap(files, file => this.readVmBackupMetadata(file)), 'mode') await this.deleteDeltaVmBackups([metadata])
} else if (metadata.mode === 'full') {
const unsupportedModes = Object.keys(others) await this.deleteFullVmBackups([metadata])
if (unsupportedModes.length !== 0) { } else {
throw new Error('no deleter for backup modes: ' + unsupportedModes.join(', ')) throw new Error(`no deleter for backup mode ${metadata.mode}`)
}
await Promise.all([
delta !== undefined && this.deleteDeltaVmBackups(delta),
full !== undefined && this.deleteFullVmBackups(full),
])
const dirs = new Set(files.map(file => dirname(file)))
for (const dir of dirs) {
// don't merge in main process, unused VHDs will be merged in the next backup run
await this.cleanVm(dir, { remove: true, onLog: warn })
} }
} }
#getCompressionType() { getDisk = Disposable.factory(this.getDisk)
return this._vhdDirectoryCompression getDisk = deduped(this.getDisk, diskId => [diskId])
} getDisk = debounceResourceFactory(this.getDisk)
#useVhdDirectory() {
return this.handler.type === 's3'
}
#useAlias() {
return this.#useVhdDirectory()
}
async *getDisk(diskId) { async *getDisk(diskId) {
const handler = this._handler const handler = this._handler
@@ -328,6 +295,7 @@ class RemoteAdapter {
// - `<partitionId>`: partitioned disk // - `<partitionId>`: partitioned disk
// - `<pvId>/<vgName>/<lvName>`: LVM on a partitioned disk // - `<pvId>/<vgName>/<lvName>`: LVM on a partitioned disk
// - `/<vgName>/lvName>`: LVM on a raw disk // - `/<vgName>/lvName>`: LVM on a raw disk
getPartition = Disposable.factory(this.getPartition)
async *getPartition(diskId, partitionId) { async *getPartition(diskId, partitionId) {
const devicePath = yield this.getDisk(diskId) const devicePath = yield this.getDisk(diskId)
if (partitionId === undefined) { if (partitionId === undefined) {
@@ -344,10 +312,13 @@ class RemoteAdapter {
return yield this._getPartition(devicePath, await this._findPartition(devicePath, partitionId)) return yield this._getPartition(devicePath, await this._findPartition(devicePath, partitionId))
} }
// if we use alias on this remote, we have to name the file alias.vhd // this function will be the one where we plug the logic of the storage format by fs type/user settings
// if the file is named .vhd => vhd
// if the file is named alias.vhd => alias to a vhd
getVhdFileName(baseName) { getVhdFileName(baseName) {
if (this.#useAlias()) { if (this._handler.type === 's3') {
return `${baseName}.alias.vhd` return `${baseName}.alias.vhd` // we want an alias to a vhddirectory
} }
return `${baseName}.vhd` return `${baseName}.vhd`
} }
@@ -356,14 +327,9 @@ class RemoteAdapter {
const handler = this._handler const handler = this._handler
const backups = { __proto__: null } const backups = { __proto__: null }
await asyncMap(await handler.list(BACKUP_DIR), async entry => { await asyncMap(await handler.list(BACKUP_DIR), async vmUuid => {
// ignore hidden and lock files const vmBackups = await this.listVmBackups(vmUuid)
if (entry[0] !== '.' && !entry.endsWith('.lock')) { backups[vmUuid] = vmBackups
const vmBackups = await this.listVmBackups(entry)
if (vmBackups.length !== 0) {
backups[entry] = vmBackups
}
}
}) })
return backups return backups
@@ -504,11 +470,10 @@ class RemoteAdapter {
async writeVhd(path, input, { checksum = true, validator = noop } = {}) { async writeVhd(path, input, { checksum = true, validator = noop } = {}) {
const handler = this._handler const handler = this._handler
if (this.#useVhdDirectory()) { if (path.endsWith('.alias.vhd')) {
const dataPath = `${dirname(path)}/data/${uuidv4()}.vhd` const dataPath = `${dirname(path)}/data/${uuidv4()}.vhd`
await createVhdDirectoryFromStream(handler, dataPath, input, { await createVhdDirectoryFromStream(handler, dataPath, input, {
concurrency: 16, concurrency: 16,
compression: this.#getCompressionType(),
async validator() { async validator() {
await input.task await input.task
return validator.apply(this, arguments) return validator.apply(this, arguments)
@@ -536,8 +501,8 @@ class RemoteAdapter {
// if it's a path : open all hierarchy of parent // if it's a path : open all hierarchy of parent
if (typeof paths === 'string') { if (typeof paths === 'string') {
let vhd let vhd,
let vhdPath = paths vhdPath = paths
do { do {
const disposable = await openVhd(handler, vhdPath) const disposable = await openVhd(handler, vhdPath)
vhd = disposable.value vhd = disposable.value
@@ -577,15 +542,14 @@ class RemoteAdapter {
return stream return stream
} }
async readDeltaVmBackup(metadata, ignoredVdis) { async readDeltaVmBackup(metadata) {
const handler = this._handler const handler = this._handler
const { vbds, vhds, vifs, vm } = metadata const { vbds, vdis, vhds, vifs, vm } = metadata
const dir = dirname(metadata._filename) const dir = dirname(metadata._filename)
const vdis = ignoredVdis === undefined ? metadata.vdis : pickBy(metadata.vdis, vdi => !ignoredVdis.has(vdi.uuid))
const streams = {} const streams = {}
await asyncMapSettled(Object.keys(vdis), async ref => { await asyncMapSettled(Object.keys(vdis), async id => {
streams[`${ref}.vhd`] = await this._createSyntheticStream(handler, join(dir, vhds[ref])) streams[`${id}.vhd`] = await this._createSyntheticStream(handler, join(dir, vhds[id]))
}) })
return { return {
@@ -618,30 +582,4 @@ Object.assign(RemoteAdapter.prototype, {
isValidXva, isValidXva,
}) })
decorateMethodsWith(RemoteAdapter, {
_getLvmLogicalVolumes: compose([
Disposable.factory,
[deduped, (devicePath, pvId, vgName) => [devicePath, pvId, vgName]],
debounceResourceFactory,
]),
_getLvmPhysicalVolume: compose([
Disposable.factory,
[deduped, (devicePath, partition) => [devicePath, partition?.id]],
debounceResourceFactory,
]),
_getPartition: compose([
Disposable.factory,
[deduped, (devicePath, partition) => [devicePath, partition?.id]],
debounceResourceFactory,
]),
_usePartitionFiles: Disposable.factory,
getDisk: compose([Disposable.factory, [deduped, diskId => [diskId]], debounceResourceFactory]),
getPartition: Disposable.factory,
})
exports.RemoteAdapter = RemoteAdapter exports.RemoteAdapter = RemoteAdapter

View File

@@ -1,5 +1,3 @@
'use strict'
const { DIR_XO_POOL_METADATA_BACKUPS } = require('./RemoteAdapter.js') const { DIR_XO_POOL_METADATA_BACKUPS } = require('./RemoteAdapter.js')
const { PATH_DB_DUMP } = require('./_PoolMetadataBackup.js') const { PATH_DB_DUMP } = require('./_PoolMetadataBackup.js')

View File

@@ -1,6 +1,4 @@
'use strict' const CancelToken = require('promise-toolbox/CancelToken.js')
const CancelToken = require('promise-toolbox/CancelToken')
const Zone = require('node-zone') const Zone = require('node-zone')
const logAfterEnd = () => { const logAfterEnd = () => {
@@ -9,8 +7,6 @@ const logAfterEnd = () => {
const noop = Function.prototype const noop = Function.prototype
const serializeErrors = errors => (Array.isArray(errors) ? errors.map(serializeError) : errors)
// Create a serializable object from an error. // Create a serializable object from an error.
// //
// Otherwise some fields might be non-enumerable and missing from logs. // Otherwise some fields might be non-enumerable and missing from logs.
@@ -19,7 +15,6 @@ const serializeError = error =>
? { ? {
...error, // Copy enumerable properties. ...error, // Copy enumerable properties.
code: error.code, code: error.code,
errors: serializeErrors(error.errors), // supports AggregateError
message: error.message, message: error.message,
name: error.name, name: error.name,
stack: error.stack, stack: error.stack,

View File

@@ -1,5 +1,3 @@
'use strict'
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { DIR_XO_POOL_METADATA_BACKUPS } = require('./RemoteAdapter.js') const { DIR_XO_POOL_METADATA_BACKUPS } = require('./RemoteAdapter.js')

View File

@@ -1,14 +1,11 @@
'use strict'
const assert = require('assert') const assert = require('assert')
const findLast = require('lodash/findLast.js') const findLast = require('lodash/findLast.js')
const groupBy = require('lodash/groupBy.js') const groupBy = require('lodash/groupBy.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors') const ignoreErrors = require('promise-toolbox/ignoreErrors.js')
const keyBy = require('lodash/keyBy.js') const keyBy = require('lodash/keyBy.js')
const mapValues = require('lodash/mapValues.js') const mapValues = require('lodash/mapValues.js')
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { createLogger } = require('@xen-orchestra/log') const { createLogger } = require('@xen-orchestra/log')
const { decorateMethodsWith } = require('@vates/decorate-with')
const { defer } = require('golike-defer') const { defer } = require('golike-defer')
const { formatDateTime } = require('@xen-orchestra/xapi') const { formatDateTime } = require('@xen-orchestra/xapi')
@@ -24,13 +21,6 @@ const { watchStreamSize } = require('./_watchStreamSize.js')
const { debug, warn } = createLogger('xo:backups:VmBackup') const { debug, warn } = createLogger('xo:backups:VmBackup')
class AggregateError extends Error {
constructor(errors, message) {
super(message)
this.errors = errors
}
}
const asyncEach = async (iterable, fn, thisArg = iterable) => { const asyncEach = async (iterable, fn, thisArg = iterable) => {
for (const item of iterable) { for (const item of iterable) {
await fn.call(thisArg, item) await fn.call(thisArg, item)
@@ -44,11 +34,10 @@ const forkDeltaExport = deltaExport =>
}, },
}) })
class VmBackup { exports.VmBackup = class VmBackup {
constructor({ config, getSnapshotNameLabel, job, remoteAdapters, remotes, schedule, settings, srs, vm }) { constructor({ config, getSnapshotNameLabel, job, remoteAdapters, remotes, schedule, settings, srs, vm }) {
if (vm.other_config['xo:backup:job'] === job.id && 'start' in vm.blocked_operations) { if (vm.other_config['xo:backup:job'] === job.id) {
// don't match replicated VMs created by this very job otherwise they // otherwise replicated VMs would be matched and replicated again and again
// will be replicated again and again
throw new Error('cannot backup a VM created by this very job') throw new Error('cannot backup a VM created by this very job')
} }
@@ -135,18 +124,16 @@ class VmBackup {
return return
} }
const errors = []
await (parallel ? asyncMap : asyncEach)(writers, async function (writer) { await (parallel ? asyncMap : asyncEach)(writers, async function (writer) {
try { try {
await fn(writer) await fn(writer)
} catch (error) { } catch (error) {
errors.push(error)
this.delete(writer) this.delete(writer)
warn(warnMessage, { error, writer: writer.constructor.name }) warn(warnMessage, { error, writer: writer.constructor.name })
} }
}) })
if (writers.size === 0) { if (writers.size === 0) {
throw new AggregateError(errors, 'all targets have failed, step: ' + warnMessage) throw new Error('all targets have failed, step: ' + warnMessage)
} }
} }
@@ -181,7 +168,6 @@ class VmBackup {
} }
const snapshotRef = await vm[settings.checkpointSnapshot ? '$checkpoint' : '$snapshot']({ const snapshotRef = await vm[settings.checkpointSnapshot ? '$checkpoint' : '$snapshot']({
ignoreNobakVdis: true,
name_label: this._getSnapshotNameLabel(vm), name_label: this._getSnapshotNameLabel(vm),
}) })
this.timestamp = Date.now() this.timestamp = Date.now()
@@ -398,6 +384,7 @@ class VmBackup {
this._fullVdisRequired = fullVdisRequired this._fullVdisRequired = fullVdisRequired
} }
run = defer(this.run)
async run($defer) { async run($defer) {
const settings = this._settings const settings = this._settings
assert( assert(
@@ -445,8 +432,3 @@ class VmBackup {
} }
} }
} }
exports.VmBackup = VmBackup
decorateMethodsWith(VmBackup, {
run: defer,
})

View File

@@ -1,5 +1,3 @@
'use strict'
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { DIR_XO_CONFIG_BACKUPS } = require('./RemoteAdapter.js') const { DIR_XO_CONFIG_BACKUPS } = require('./RemoteAdapter.js')

View File

@@ -1,6 +1,4 @@
'use strict'
exports.isMetadataFile = filename => filename.endsWith('.json') exports.isMetadataFile = filename => filename.endsWith('.json')
exports.isVhdFile = filename => filename.endsWith('.vhd') exports.isVhdFile = filename => filename.endsWith('.vhd')
exports.isXvaFile = filename => filename.endsWith('.xva') exports.isXvaFile = filename => filename.endsWith('.xva')
exports.isXvaSumFile = filename => filename.endsWith('.xva.checksum') exports.isXvaSumFile = filename => filename.endsWith('.xva.cheksum')

View File

@@ -1,16 +1,11 @@
'use strict'
require('@xen-orchestra/log/configure.js').catchGlobalErrors( require('@xen-orchestra/log/configure.js').catchGlobalErrors(
require('@xen-orchestra/log').createLogger('xo:backups:worker') require('@xen-orchestra/log').createLogger('xo:backups:worker')
) )
require('@vates/cached-dns.lookup').createCachedLookup().patchGlobal() const Disposable = require('promise-toolbox/Disposable.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors.js')
const Disposable = require('promise-toolbox/Disposable')
const ignoreErrors = require('promise-toolbox/ignoreErrors')
const { compose } = require('@vates/compose') const { compose } = require('@vates/compose')
const { createDebounceResource } = require('@vates/disposable/debounceResource.js') const { createDebounceResource } = require('@vates/disposable/debounceResource.js')
const { decorateMethodsWith } = require('@vates/decorate-with')
const { deduped } = require('@vates/disposable/deduped.js') const { deduped } = require('@vates/disposable/deduped.js')
const { getHandler } = require('@xen-orchestra/fs') const { getHandler } = require('@xen-orchestra/fs')
const { parseDuration } = require('@vates/parse-duration') const { parseDuration } = require('@vates/parse-duration')
@@ -63,6 +58,11 @@ class BackupWorker {
}).run() }).run()
} }
getAdapter = Disposable.factory(this.getAdapter)
getAdapter = deduped(this.getAdapter, remote => [remote.url])
getAdapter = compose(this.getAdapter, function (resource) {
return this.debounceResource(resource)
})
async *getAdapter(remote) { async *getAdapter(remote) {
const handler = getHandler(remote, this.#remoteOptions) const handler = getHandler(remote, this.#remoteOptions)
await handler.sync() await handler.sync()
@@ -70,13 +70,17 @@ class BackupWorker {
yield new RemoteAdapter(handler, { yield new RemoteAdapter(handler, {
debounceResource: this.debounceResource, debounceResource: this.debounceResource,
dirMode: this.#config.dirMode, dirMode: this.#config.dirMode,
vhdDirectoryCompression: this.#config.vhdDirectoryCompression,
}) })
} finally { } finally {
await handler.forget() await handler.forget()
} }
} }
getXapi = Disposable.factory(this.getXapi)
getXapi = deduped(this.getXapi, ({ url }) => [url])
getXapi = compose(this.getXapi, function (resource) {
return this.debounceResource(resource)
})
async *getXapi({ credentials: { username: user, password }, ...opts }) { async *getXapi({ credentials: { username: user, password }, ...opts }) {
const xapi = new Xapi({ const xapi = new Xapi({
...this.#xapiOptions, ...this.#xapiOptions,
@@ -98,30 +102,6 @@ class BackupWorker {
} }
} }
decorateMethodsWith(BackupWorker, {
getAdapter: compose([
Disposable.factory,
[deduped, remote => [remote.url]],
[
compose,
function (resource) {
return this.debounceResource(resource)
},
],
]),
getXapi: compose([
Disposable.factory,
[deduped, xapi => [xapi.url]],
[
compose,
function (resource) {
return this.debounceResource(resource)
},
],
]),
})
// Received message: // Received message:
// //
// Message { // Message {

View File

@@ -1,7 +1,5 @@
'use strict' const cancelable = require('promise-toolbox/cancelable.js')
const CancelToken = require('promise-toolbox/CancelToken.js')
const cancelable = require('promise-toolbox/cancelable')
const CancelToken = require('promise-toolbox/CancelToken')
// Similar to `Promise.all` + `map` but pass a cancel token to the callback // Similar to `Promise.all` + `map` but pass a cancel token to the callback
// //

View File

@@ -1,5 +1,3 @@
'use strict'
/* eslint-env jest */ /* eslint-env jest */
const rimraf = require('rimraf') const rimraf = require('rimraf')
@@ -11,8 +9,6 @@ const crypto = require('crypto')
const { RemoteAdapter } = require('./RemoteAdapter') const { RemoteAdapter } = require('./RemoteAdapter')
const { VHDFOOTER, VHDHEADER } = require('./tests.fixtures.js') const { VHDFOOTER, VHDHEADER } = require('./tests.fixtures.js')
const { VhdFile, Constants, VhdDirectory, VhdAbstract } = require('vhd-lib') const { VhdFile, Constants, VhdDirectory, VhdAbstract } = require('vhd-lib')
const { checkAliases } = require('./_cleanVm')
const { dirname, basename } = require('path')
let tempDir, adapter, handler, jobId, vdiId, basePath let tempDir, adapter, handler, jobId, vdiId, basePath
@@ -39,11 +35,7 @@ const uniqueId = () => crypto.randomBytes(16).toString('hex')
async function generateVhd(path, opts = {}) { async function generateVhd(path, opts = {}) {
let vhd let vhd
let dataPath = path const dataPath = opts.useAlias ? path + '.data' : path
if (opts.useAlias) {
await handler.mkdir(dirname(path) + '/data/')
dataPath = dirname(path) + '/data/' + basename(path)
}
if (opts.mode === 'directory') { if (opts.mode === 'directory') {
await handler.mkdir(dataPath) await handler.mkdir(dataPath)
vhd = new VhdDirectory(handler, dataPath) vhd = new VhdDirectory(handler, dataPath)
@@ -170,7 +162,7 @@ test('it remove backup meta data referencing a missing vhd in delta backup', asy
`${basePath}/deleted.vhd`, // in metadata but not in vhds `${basePath}/deleted.vhd`, // in metadata but not in vhds
`${basePath}/orphan.vhd`, `${basePath}/orphan.vhd`,
`${basePath}/child.vhd`, `${basePath}/child.vhd`,
// abandonned.vhd is not here anymore // abandonned.json is not here
], ],
}), }),
{ flags: 'w' } { flags: 'w' }
@@ -186,7 +178,7 @@ test('it merges delta of non destroyed chain', async () => {
`metadata.json`, `metadata.json`,
JSON.stringify({ JSON.stringify({
mode: 'delta', mode: 'delta',
size: 12000, // a size too small size: 209920,
vhds: [ vhds: [
`${basePath}/grandchild.vhd`, // grand child should not be merged `${basePath}/grandchild.vhd`, // grand child should not be merged
`${basePath}/child.vhd`, `${basePath}/child.vhd`,
@@ -212,25 +204,20 @@ test('it merges delta of non destroyed chain', async () => {
}, },
}) })
let loggued = [] let loggued = ''
const onLog = message => { const onLog = message => {
loggued.push(message) loggued += message + '\n'
} }
await adapter.cleanVm('/', { remove: true, onLog }) await adapter.cleanVm('/', { remove: true, onLog })
expect(loggued[0]).toEqual(`the parent /${basePath}/orphan.vhd of the child /${basePath}/child.vhd is unused`) expect(loggued).toEqual(`the parent /${basePath}/orphan.vhd of the child /${basePath}/child.vhd is unused\n`)
expect(loggued[1]).toEqual(`incorrect size in metadata: 12000 instead of 209920`) loggued = ''
loggued = []
await adapter.cleanVm('/', { remove: true, merge: true, onLog }) await adapter.cleanVm('/', { remove: true, merge: true, onLog })
const [unused, merging] = loggued const [unused, merging] = loggued.split('\n')
expect(unused).toEqual(`the parent /${basePath}/orphan.vhd of the child /${basePath}/child.vhd is unused`) expect(unused).toEqual(`the parent /${basePath}/orphan.vhd of the child /${basePath}/child.vhd is unused`)
expect(merging).toEqual(`merging /${basePath}/child.vhd into /${basePath}/orphan.vhd`) expect(merging).toEqual(`merging /${basePath}/child.vhd into /${basePath}/orphan.vhd`)
const metadata = JSON.parse(await handler.readFile(`metadata.json`))
// size should be the size of children + grand children after the merge
expect(metadata.size).toEqual(209920)
// merging is already tested in vhd-lib, don't retest it here (and theses vhd are as empty as my stomach at 12h12) // merging is already tested in vhd-lib, don't retest it here (and theses vhd are as empty as my stomach at 12h12)
// only check deletion // only check deletion
const remainingVhds = await handler.list(basePath) const remainingVhds = await handler.list(basePath)
expect(remainingVhds.length).toEqual(2) expect(remainingVhds.length).toEqual(2)
@@ -244,7 +231,11 @@ test('it finish unterminated merge ', async () => {
JSON.stringify({ JSON.stringify({
mode: 'delta', mode: 'delta',
size: 209920, size: 209920,
vhds: [`${basePath}/orphan.vhd`, `${basePath}/child.vhd`], vhds: [
`${basePath}/orphan.vhd`, // grand child should not be merged
`${basePath}/child.vhd`,
// orphan is not here, he should be merged in child
],
}) })
) )
@@ -270,6 +261,7 @@ test('it finish unterminated merge ', async () => {
}) })
) )
// a unfinished merging
await adapter.cleanVm('/', { remove: true, merge: true }) await adapter.cleanVm('/', { remove: true, merge: true })
// merging is already tested in vhd-lib, don't retest it here (and theses vhd are as empty as my stomach at 12h12) // merging is already tested in vhd-lib, don't retest it here (and theses vhd are as empty as my stomach at 12h12)
@@ -282,17 +274,12 @@ test('it finish unterminated merge ', async () => {
// each of the vhd can be a file, a directory, an alias to a file or an alias to a directory // each of the vhd can be a file, a directory, an alias to a file or an alias to a directory
// the message an resulting files should be identical to the output with vhd files which is tested independantly // the message an resulting files should be identical to the output with vhd files which is tested independantly
describe('tests multiple combination ', () => { describe('tests mulitple combination ', () => {
for (const useAlias of [true, false]) { for (const useAlias of [true, false]) {
for (const vhdMode of ['file', 'directory']) { for (const vhdMode of ['file', 'directory']) {
test(`alias : ${useAlias}, mode: ${vhdMode}`, async () => { test(`alias : ${useAlias}, mode: ${vhdMode}`, async () => {
// a broken VHD // a broken VHD
if (useAlias) { const brokenVhdDataPath = basePath + useAlias ? 'broken.data' : 'broken.vhd'
await handler.mkdir(basePath + '/data')
}
const brokenVhdDataPath = basePath + (useAlias ? '/data/broken.vhd' : '/broken.vhd')
if (vhdMode === 'directory') { if (vhdMode === 'directory') {
await handler.mkdir(brokenVhdDataPath) await handler.mkdir(brokenVhdDataPath)
} else { } else {
@@ -313,7 +300,6 @@ describe('tests multiple combination ', () => {
parentUid: crypto.randomBytes(16), parentUid: crypto.randomBytes(16),
}, },
}) })
// an ancestor of a vhd present in metadata // an ancestor of a vhd present in metadata
const ancestor = await generateVhd(`${basePath}/ancestor.vhd`, { const ancestor = await generateVhd(`${basePath}/ancestor.vhd`, {
useAlias, useAlias,
@@ -376,29 +362,22 @@ describe('tests multiple combination ', () => {
], ],
}) })
) )
await adapter.cleanVm('/', { remove: true, merge: true }) await adapter.cleanVm('/', { remove: true, merge: true })
const metadata = JSON.parse(await handler.readFile(`metadata.json`))
// size should be the size of children + grand children + clean after the merge
expect(metadata.size).toEqual(vhdMode === 'file' ? 314880 : undefined)
// broken vhd, non referenced, abandonned should be deleted ( alias and data) // broken vhd, non referenced, abandonned should be deleted ( alias and data)
// ancestor and child should be merged // ancestor and child should be merged
// grand child and clean vhd should not have changed // grand child and clean vhd should not have changed
const survivors = await handler.list(basePath) const survivors = await handler.list(basePath)
// console.log(survivors) // console.log(survivors)
if (useAlias) { if (useAlias) {
const dataSurvivors = await handler.list(basePath + '/data')
// the goal of the alias : do not move a full folder // the goal of the alias : do not move a full folder
expect(dataSurvivors).toContain('ancestor.vhd') expect(survivors).toContain('ancestor.vhd.data')
expect(dataSurvivors).toContain('grandchild.vhd') expect(survivors).toContain('grandchild.vhd.data')
expect(dataSurvivors).toContain('cleanAncestor.vhd') expect(survivors).toContain('cleanAncestor.vhd.data')
expect(survivors).toContain('clean.vhd.alias.vhd') expect(survivors).toContain('clean.vhd.alias.vhd')
expect(survivors).toContain('child.vhd.alias.vhd') expect(survivors).toContain('child.vhd.alias.vhd')
expect(survivors).toContain('grandchild.vhd.alias.vhd') expect(survivors).toContain('grandchild.vhd.alias.vhd')
expect(survivors.length).toEqual(4) // the 3 ok + data expect(survivors.length).toEqual(6)
expect(dataSurvivors.length).toEqual(3) // the 3 ok + data
} else { } else {
expect(survivors).toContain('clean.vhd') expect(survivors).toContain('clean.vhd')
expect(survivors).toContain('child.vhd') expect(survivors).toContain('child.vhd')
@@ -409,31 +388,3 @@ describe('tests multiple combination ', () => {
} }
} }
}) })
test('it cleans orphan merge states ', async () => {
await handler.writeFile(`${basePath}/.orphan.vhd.merge.json`, '')
await adapter.cleanVm('/', { remove: true })
expect(await handler.list(basePath)).toEqual([])
})
test('check Aliases should work alone', async () => {
await handler.mkdir('vhds')
await handler.mkdir('vhds/data')
await generateVhd(`vhds/data/ok.vhd`)
await VhdAbstract.createAlias(handler, 'vhds/ok.alias.vhd', 'vhds/data/ok.vhd')
await VhdAbstract.createAlias(handler, 'vhds/missingData.alias.vhd', 'vhds/data/nonexistent.vhd')
await generateVhd(`vhds/data/missingalias.vhd`)
await checkAliases(['vhds/missingData.alias.vhd', 'vhds/ok.alias.vhd'], 'vhds/data', { remove: true, handler })
// only ok have suvived
const alias = (await handler.list('vhds')).filter(f => f.endsWith('.vhd'))
expect(alias.length).toEqual(1)
const data = await handler.list('vhds/data')
expect(data.length).toEqual(1)
})

View File

@@ -1,11 +1,8 @@
'use strict'
const assert = require('assert') const assert = require('assert')
const sum = require('lodash/sum') const sum = require('lodash/sum')
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { Constants, mergeVhd, openVhd, VhdAbstract, VhdFile } = require('vhd-lib') const { Constants, mergeVhd, openVhd, VhdAbstract, VhdFile } = require('vhd-lib')
const { isVhdAlias, resolveVhdAlias } = require('vhd-lib/aliases') const { dirname, resolve } = require('path')
const { dirname, resolve, basename } = require('path')
const { DISK_TYPES } = Constants const { DISK_TYPES } = Constants
const { isMetadataFile, isVhdFile, isXvaFile, isXvaSumFile } = require('./_backupType.js') const { isMetadataFile, isVhdFile, isXvaFile, isXvaSumFile } = require('./_backupType.js')
const { limitConcurrency } = require('limit-concurrency-decorator') const { limitConcurrency } = require('limit-concurrency-decorator')
@@ -13,24 +10,6 @@ const { limitConcurrency } = require('limit-concurrency-decorator')
const { Task } = require('./Task.js') const { Task } = require('./Task.js')
const { Disposable } = require('promise-toolbox') const { Disposable } = require('promise-toolbox')
// checking the size of a vhd directory is costly
// 1 Http Query per 1000 blocks
// we only check size of all the vhd are VhdFiles
function shouldComputeVhdsSize(vhds) {
return vhds.every(vhd => vhd instanceof VhdFile)
}
const computeVhdsSize = (handler, vhdPaths) =>
Disposable.use(
vhdPaths.map(vhdPath => openVhd(handler, vhdPath)),
async vhds => {
if (shouldComputeVhdsSize(vhds)) {
const sizes = await asyncMap(vhds, vhd => vhd.getSize())
return sum(sizes)
}
}
)
// chain is an array of VHDs from child to parent // chain is an array of VHDs from child to parent
// //
// the whole chain will be merged into parent, parent will be renamed to child // the whole chain will be merged into parent, parent will be renamed to child
@@ -85,12 +64,13 @@ async function mergeVhdChain(chain, { handler, onLog, remove, merge }) {
) )
clearInterval(handle) clearInterval(handle)
await Promise.all([ await Promise.all([
VhdAbstract.rename(handler, parent, child), VhdAbstract.rename(handler, parent, child),
asyncMap(children.slice(0, -1), child => { asyncMap(children.slice(0, -1), child => {
onLog(`the VHD ${child} is unused`) onLog(`the VHD ${child} is unused`)
if (remove) { if (remove) {
onLog(`mergeVhdChain: deleting unused VHD ${child}`) onLog(`deleting unused VHD ${child}`)
return VhdAbstract.unlink(handler, child) return VhdAbstract.unlink(handler, child)
} }
}), }),
@@ -102,11 +82,10 @@ async function mergeVhdChain(chain, { handler, onLog, remove, merge }) {
const noop = Function.prototype const noop = Function.prototype
const INTERRUPTED_VHDS_REG = /^\.(.+)\.merge.json$/ const INTERRUPTED_VHDS_REG = /^(?:(.+)\/)?\.(.+)\.merge.json$/
const listVhds = async (handler, vmDir) => { const listVhds = async (handler, vmDir) => {
const vhds = new Set() const vhds = []
const aliases = {} const interruptedVhds = new Set()
const interruptedVhds = new Map()
await asyncMap( await asyncMap(
await handler.list(`${vmDir}/vdis`, { await handler.list(`${vmDir}/vdis`, {
@@ -121,76 +100,24 @@ const listVhds = async (handler, vmDir) => {
async vdiDir => { async vdiDir => {
const list = await handler.list(vdiDir, { const list = await handler.list(vdiDir, {
filter: file => isVhdFile(file) || INTERRUPTED_VHDS_REG.test(file), filter: file => isVhdFile(file) || INTERRUPTED_VHDS_REG.test(file),
})
aliases[vdiDir] = list.filter(vhd => isVhdAlias(vhd)).map(file => `${vdiDir}/${file}`)
list.forEach(file => {
const res = INTERRUPTED_VHDS_REG.exec(file)
if (res === null) {
vhds.add(`${vdiDir}/${file}`)
} else {
interruptedVhds.set(`${vdiDir}/${res[1]}`, `${vdiDir}/${file}`)
}
})
}
)
)
return { vhds, interruptedVhds, aliases }
}
async function checkAliases(aliasPaths, targetDataRepository, { handler, onLog = noop, remove = false }) {
const aliasFound = []
for (const path of aliasPaths) {
const target = await resolveVhdAlias(handler, path)
if (!isVhdFile(target)) {
onLog(`Alias ${path} references a non vhd target: ${target}`)
if (remove) {
await handler.unlink(target)
await handler.unlink(path)
}
continue
}
try {
const { dispose } = await openVhd(handler, target)
try {
await dispose()
} catch (e) {
// error during dispose should not trigger a deletion
}
} catch (error) {
onLog(`target ${target} of alias ${path} is missing or broken`, { error })
if (remove) {
try {
await VhdAbstract.unlink(handler, path)
} catch (e) {
if (e.code !== 'ENOENT') {
onLog(`Error while deleting target ${target} of alias ${path}`, { error: e })
}
}
}
continue
}
aliasFound.push(resolve('/', target))
}
const entries = await handler.list(targetDataRepository, {
ignoreMissing: true,
prependDir: true, prependDir: true,
}) })
entries.forEach(async entry => { list.forEach(file => {
if (!aliasFound.includes(entry)) { const res = INTERRUPTED_VHDS_REG.exec(file)
onLog(`the Vhd ${entry} is not referenced by a an alias`) if (res === null) {
if (remove) { vhds.push(file)
await VhdAbstract.unlink(handler, entry) } else {
} const [, dir, file] = res
interruptedVhds.add(`${dir}/${file}`)
} }
}) })
} }
exports.checkAliases = checkAliases )
)
return { vhds, interruptedVhds }
}
const defaultMergeLimiter = limitConcurrency(1) const defaultMergeLimiter = limitConcurrency(1)
@@ -202,16 +129,17 @@ exports.cleanVm = async function cleanVm(
const handler = this._handler const handler = this._handler
const vhdsToJSons = new Set() const vhds = new Set()
const vhdParents = { __proto__: null } const vhdParents = { __proto__: null }
const vhdChildren = { __proto__: null } const vhdChildren = { __proto__: null }
const { vhds, interruptedVhds, aliases } = await listVhds(handler, vmDir) const vhdsList = await listVhds(handler, vmDir)
// remove broken VHDs // remove broken VHDs
await asyncMap(vhds, async path => { await asyncMap(vhdsList.vhds, async path => {
try { try {
await Disposable.use(openVhd(handler, path, { checkSecondFooter: !interruptedVhds.has(path) }), vhd => { await Disposable.use(openVhd(handler, path, { checkSecondFooter: !vhdsList.interruptedVhds.has(path) }), vhd => {
vhds.add(path)
if (vhd.footer.diskType === DISK_TYPES.DIFFERENCING) { if (vhd.footer.diskType === DISK_TYPES.DIFFERENCING) {
const parent = resolve('/', dirname(path), vhd.header.parentUnicodeName) const parent = resolve('/', dirname(path), vhd.header.parentUnicodeName)
vhdParents[path] = parent vhdParents[path] = parent
@@ -226,7 +154,6 @@ exports.cleanVm = async function cleanVm(
} }
}) })
} catch (error) { } catch (error) {
vhds.delete(path)
onLog(`error while checking the VHD with path ${path}`, { error }) onLog(`error while checking the VHD with path ${path}`, { error })
if (error?.code === 'ERR_ASSERTION' && remove) { if (error?.code === 'ERR_ASSERTION' && remove) {
onLog(`deleting broken ${path}`) onLog(`deleting broken ${path}`)
@@ -235,28 +162,7 @@ exports.cleanVm = async function cleanVm(
} }
}) })
// remove interrupted merge states for missing VHDs // @todo : add check for data folder of alias not referenced in a valid alias
for (const interruptedVhd of interruptedVhds.keys()) {
if (!vhds.has(interruptedVhd)) {
const statePath = interruptedVhds.get(interruptedVhd)
interruptedVhds.delete(interruptedVhd)
onLog('orphan merge state', {
mergeStatePath: statePath,
missingVhdPath: interruptedVhd,
})
if (remove) {
onLog(`deleting orphan merge state ${statePath}`)
await handler.unlink(statePath)
}
}
}
// check if alias are correct
// check if all vhd in data subfolder have a corresponding alias
await asyncMap(Object.keys(aliases), async dir => {
await checkAliases(aliases[dir], `${dir}/data`, { handler, onLog, remove })
})
// remove VHDs with missing ancestors // remove VHDs with missing ancestors
{ {
@@ -296,7 +202,7 @@ exports.cleanVm = async function cleanVm(
await Promise.all(deletions) await Promise.all(deletions)
} }
const jsons = new Set() const jsons = []
const xvas = new Set() const xvas = new Set()
const xvaSums = [] const xvaSums = []
const entries = await handler.list(vmDir, { const entries = await handler.list(vmDir, {
@@ -304,7 +210,7 @@ exports.cleanVm = async function cleanVm(
}) })
entries.forEach(path => { entries.forEach(path => {
if (isMetadataFile(path)) { if (isMetadataFile(path)) {
jsons.add(path) jsons.push(path)
} else if (isXvaFile(path)) { } else if (isXvaFile(path)) {
xvas.add(path) xvas.add(path)
} else if (isXvaSumFile(path)) { } else if (isXvaSumFile(path)) {
@@ -326,25 +232,22 @@ exports.cleanVm = async function cleanVm(
// compile the list of unused XVAs and VHDs, and remove backup metadata which // compile the list of unused XVAs and VHDs, and remove backup metadata which
// reference a missing XVA/VHD // reference a missing XVA/VHD
await asyncMap(jsons, async json => { await asyncMap(jsons, async json => {
let metadata const metadata = JSON.parse(await handler.readFile(json))
try {
metadata = JSON.parse(await handler.readFile(json))
} catch (error) {
onLog(`failed to read metadata file ${json}`, { error })
jsons.delete(json)
return
}
const { mode } = metadata const { mode } = metadata
let size
if (mode === 'full') { if (mode === 'full') {
const linkedXva = resolve('/', vmDir, metadata.xva) const linkedXva = resolve('/', vmDir, metadata.xva)
if (xvas.has(linkedXva)) { if (xvas.has(linkedXva)) {
unusedXvas.delete(linkedXva) unusedXvas.delete(linkedXva)
size = await handler.getSize(linkedXva).catch(error => {
onLog(`failed to get size of ${json}`, { error })
})
} else { } else {
onLog(`the XVA linked to the metadata ${json} is missing`) onLog(`the XVA linked to the metadata ${json} is missing`)
if (remove) { if (remove) {
onLog(`deleting incomplete backup ${json}`) onLog(`deleting incomplete backup ${json}`)
jsons.delete(json)
await handler.unlink(json) await handler.unlink(json)
} }
} }
@@ -360,18 +263,46 @@ exports.cleanVm = async function cleanVm(
// possible (existing disks) even if one disk is missing // possible (existing disks) even if one disk is missing
if (missingVhds.length === 0) { if (missingVhds.length === 0) {
linkedVhds.forEach(_ => unusedVhds.delete(_)) linkedVhds.forEach(_ => unusedVhds.delete(_))
linkedVhds.forEach(path => {
vhdsToJSons[path] = json // checking the size of a vhd directory is costly
// 1 Http Query per 1000 blocks
// we only check size of all the vhd are VhdFiles
const shouldComputeSize = linkedVhds.every(vhd => vhd instanceof VhdFile)
if (shouldComputeSize) {
try {
await Disposable.use(Disposable.all(linkedVhds.map(vhdPath => openVhd(handler, vhdPath))), async vhds => {
const sizes = await asyncMap(vhds, vhd => vhd.getSize())
size = sum(sizes)
}) })
} catch (error) {
onLog(`failed to get size of ${json}`, { error })
}
}
} else { } else {
onLog(`Some VHDs linked to the metadata ${json} are missing`, { missingVhds }) onLog(`Some VHDs linked to the metadata ${json} are missing`, { missingVhds })
if (remove) { if (remove) {
onLog(`deleting incomplete backup ${json}`) onLog(`deleting incomplete backup ${json}`)
jsons.delete(json)
await handler.unlink(json) await handler.unlink(json)
} }
} }
} }
const metadataSize = metadata.size
if (size !== undefined && metadataSize !== size) {
onLog(`incorrect size in metadata: ${metadataSize ?? 'none'} instead of ${size}`)
// don't update if the the stored size is greater than found files,
// it can indicates a problem
if (fixMetadata && (metadataSize === undefined || metadataSize < size)) {
try {
metadata.size = size
await handler.writeFile(json, JSON.stringify(metadata), { flags: 'w' })
} catch (error) {
onLog(`failed to update size in backup metadata ${json}`, { error })
}
}
}
}) })
// TODO: parallelize by vm/job/vdi // TODO: parallelize by vm/job/vdi
@@ -383,7 +314,7 @@ exports.cleanVm = async function cleanVm(
const vhdChainsToMerge = { __proto__: null } const vhdChainsToMerge = { __proto__: null }
const toCheck = new Set(unusedVhds) const toCheck = new Set(unusedVhds)
let shouldDelete = false
const getUsedChildChainOrDelete = vhd => { const getUsedChildChainOrDelete = vhd => {
if (vhd in vhdChainsToMerge) { if (vhd in vhdChainsToMerge) {
const chain = vhdChainsToMerge[vhd] const chain = vhdChainsToMerge[vhd]
@@ -409,64 +340,8 @@ exports.cleanVm = async function cleanVm(
onLog(`the VHD ${vhd} is unused`) onLog(`the VHD ${vhd} is unused`)
if (remove) { if (remove) {
onLog(`getUsedChildChainOrDelete: deleting unused VHD`, { onLog(`deleting unused VHD ${vhd}`)
vhdChildren, unusedVhdsDeletion.push(VhdAbstract.unlink(handler, vhd))
vhd,
})
// temporarly disabled
shouldDelete = true
// unusedVhdsDeletion.push(VhdAbstract.unlink(handler, vhd))
}
}
{
// eslint-disable-next-line no-console
const debug = console.debug
if (shouldDelete) {
const chains = { __proto__: null }
const queue = new Set(vhds)
function addChildren(parent, chain) {
queue.delete(parent)
const child = vhdChildren[parent]
if (child !== undefined) {
const childChain = chains[child]
if (childChain !== undefined) {
// if a chain already exists, use it
delete chains[child]
chain.push(...childChain)
} else {
chain.push(child)
addChildren(child, chain)
}
}
}
for (const vhd of queue) {
const chain = []
addChildren(vhd, chain)
chains[vhd] = chain
}
const entries = Object.entries(chains)
debug(`${vhds.size} VHDs (${unusedVhds.size} unused) found among ${entries.length} chains [`)
const decorateVhd = vhd => {
const shortPath = basename(vhd)
return unusedVhds.has(vhd) ? `${shortPath} [unused]` : shortPath
}
for (let i = 0, n = entries.length; i < n; ++i) {
debug(`in ${dirname(entries[i][0])}`)
debug(' [')
const [parent, children] = entries[i]
debug(' ' + decorateVhd(parent))
for (const child of children) {
debug(' ' + decorateVhd(child))
}
debug(' ]')
}
debug(']')
} }
} }
@@ -475,9 +350,9 @@ exports.cleanVm = async function cleanVm(
}) })
// merge interrupted VHDs // merge interrupted VHDs
for (const parent of interruptedVhds.keys()) { vhdsList.interruptedVhds.forEach(parent => {
vhdChainsToMerge[parent] = [vhdChildren[parent], parent] vhdChainsToMerge[parent] = [vhdChildren[parent], parent]
} })
Object.values(vhdChainsToMerge).forEach(chain => { Object.values(vhdChainsToMerge).forEach(chain => {
if (chain !== undefined) { if (chain !== undefined) {
@@ -486,15 +361,9 @@ exports.cleanVm = async function cleanVm(
}) })
} }
const metadataWithMergedVhd = {} const doMerge = () => {
const doMerge = async () => { const promise = asyncMap(toMerge, async chain => limitedMergeVhdChain(chain, { handler, onLog, remove, merge }))
await asyncMap(toMerge, async chain => { return merge ? promise.then(sizes => ({ size: sum(sizes) })) : promise
const merged = await limitedMergeVhdChain(chain, { handler, onLog, remove, merge })
if (merged !== undefined) {
const metadataPath = vhdsToJSons[chain[0]] // all the chain should have the same metada file
metadataWithMergedVhd[metadataPath] = true
}
})
} }
await Promise.all([ await Promise.all([
@@ -519,52 +388,6 @@ exports.cleanVm = async function cleanVm(
}), }),
]) ])
// update size for delta metadata with merged VHD
// check for the other that the size is the same as the real file size
await asyncMap(jsons, async metadataPath => {
const metadata = JSON.parse(await handler.readFile(metadataPath))
let fileSystemSize
const merged = metadataWithMergedVhd[metadataPath] !== undefined
const { mode, size, vhds, xva } = metadata
try {
if (mode === 'full') {
// a full backup : check size
const linkedXva = resolve('/', vmDir, xva)
fileSystemSize = await handler.getSize(linkedXva)
} else if (mode === 'delta') {
const linkedVhds = Object.keys(vhds).map(key => resolve('/', vmDir, vhds[key]))
fileSystemSize = await computeVhdsSize(handler, linkedVhds)
// the size is not computed in some cases (e.g. VhdDirectory)
if (fileSystemSize === undefined) {
return
}
// don't warn if the size has changed after a merge
if (!merged && fileSystemSize !== size) {
onLog(`incorrect size in metadata: ${size ?? 'none'} instead of ${fileSystemSize}`)
}
}
} catch (error) {
onLog(`failed to get size of ${metadataPath}`, { error })
return
}
// systematically update size after a merge
if ((merged || fixMetadata) && size !== fileSystemSize) {
metadata.size = fileSystemSize
try {
await handler.writeFile(metadataPath, JSON.stringify(metadata), { flags: 'w' })
} catch (error) {
onLog(`failed to update size in backup metadata ${metadataPath} after merge`, { error })
}
}
})
return { return {
// boolean whether some VHDs were merged (or should be merged) // boolean whether some VHDs were merged (or should be merged)
merge: toMerge.length !== 0, merge: toMerge.length !== 0,

View File

@@ -1,9 +1,7 @@
'use strict'
const compareVersions = require('compare-versions') const compareVersions = require('compare-versions')
const find = require('lodash/find.js') const find = require('lodash/find.js')
const groupBy = require('lodash/groupBy.js') const groupBy = require('lodash/groupBy.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors') const ignoreErrors = require('promise-toolbox/ignoreErrors.js')
const omit = require('lodash/omit.js') const omit = require('lodash/omit.js')
const { asyncMap } = require('@xen-orchestra/async-map') const { asyncMap } = require('@xen-orchestra/async-map')
const { CancelToken } = require('promise-toolbox') const { CancelToken } = require('promise-toolbox')
@@ -11,8 +9,6 @@ const { createVhdStreamWithLength } = require('vhd-lib')
const { defer } = require('golike-defer') const { defer } = require('golike-defer')
const { cancelableMap } = require('./_cancelableMap.js') const { cancelableMap } = require('./_cancelableMap.js')
const { Task } = require('./Task.js')
const { pick } = require('lodash')
const TAG_BASE_DELTA = 'xo:base_delta' const TAG_BASE_DELTA = 'xo:base_delta'
exports.TAG_BASE_DELTA = TAG_BASE_DELTA exports.TAG_BASE_DELTA = TAG_BASE_DELTA
@@ -21,17 +17,6 @@ const TAG_COPY_SRC = 'xo:copy_of'
exports.TAG_COPY_SRC = TAG_COPY_SRC exports.TAG_COPY_SRC = TAG_COPY_SRC
const ensureArray = value => (value === undefined ? [] : Array.isArray(value) ? value : [value]) const ensureArray = value => (value === undefined ? [] : Array.isArray(value) ? value : [value])
const resolveUuid = async (xapi, cache, uuid, type) => {
if (uuid == null) {
return uuid
}
let ref = cache.get(uuid)
if (ref === undefined) {
ref = await xapi.call(`${type}.get_by_uuid`, uuid)
cache.set(uuid, ref)
}
return ref
}
exports.exportDeltaVm = async function exportDeltaVm( exports.exportDeltaVm = async function exportDeltaVm(
vm, vm,
@@ -65,6 +50,17 @@ exports.exportDeltaVm = async function exportDeltaVm(
return return
} }
// If the VDI name start with `[NOBAK]`, do not export it.
if (vdi.name_label.startsWith('[NOBAK]')) {
// FIXME: find a way to not create the VDI snapshot in the
// first time.
//
// The snapshot must not exist otherwise it could break the
// next export.
ignoreErrors.call(vdi.$destroy())
return
}
vbds[vbd.$ref] = vbd vbds[vbd.$ref] = vbd
const vdiRef = vdi.$ref const vdiRef = vdi.$ref
@@ -169,12 +165,6 @@ exports.importDeltaVm = defer(async function importDeltaVm(
} }
} }
const cache = new Map()
const mapVdisSrRefs = {}
for (const [vdiUuid, srUuid] of Object.entries(mapVdisSrs)) {
mapVdisSrRefs[vdiUuid] = await resolveUuid(xapi, cache, srUuid, 'SR')
}
const baseVdis = {} const baseVdis = {}
baseVm && baseVm &&
baseVm.$VBDs.forEach(vbd => { baseVm.$VBDs.forEach(vbd => {
@@ -189,11 +179,6 @@ exports.importDeltaVm = defer(async function importDeltaVm(
let suspendVdi let suspendVdi
if (vmRecord.power_state === 'Suspended') { if (vmRecord.power_state === 'Suspended') {
const vdi = vdiRecords[vmRecord.suspend_VDI] const vdi = vdiRecords[vmRecord.suspend_VDI]
if (vdi === undefined) {
Task.warning('Suspend VDI not available for this suspended VM', {
vm: pick(vmRecord, 'uuid', 'name_label'),
})
} else {
suspendVdi = await xapi.getRecord( suspendVdi = await xapi.getRecord(
'VDI', 'VDI',
await xapi.VDI_create({ await xapi.VDI_create({
@@ -203,12 +188,11 @@ exports.importDeltaVm = defer(async function importDeltaVm(
[TAG_BASE_DELTA]: undefined, [TAG_BASE_DELTA]: undefined,
[TAG_COPY_SRC]: vdi.uuid, [TAG_COPY_SRC]: vdi.uuid,
}, },
sr: mapVdisSrRefs[vdi.uuid] ?? sr.$ref, sr: mapVdisSrs[vdi.uuid] ?? sr.$ref,
}) })
) )
$defer.onFailure(() => suspendVdi.$destroy()) $defer.onFailure(() => suspendVdi.$destroy())
} }
}
// 1. Create the VM. // 1. Create the VM.
const vmRef = await xapi.VM_create( const vmRef = await xapi.VM_create(
@@ -271,7 +255,7 @@ exports.importDeltaVm = defer(async function importDeltaVm(
[TAG_BASE_DELTA]: undefined, [TAG_BASE_DELTA]: undefined,
[TAG_COPY_SRC]: vdi.uuid, [TAG_COPY_SRC]: vdi.uuid,
}, },
SR: mapVdisSrRefs[vdi.uuid] ?? sr.$ref, SR: mapVdisSrs[vdi.uuid] ?? sr.$ref,
}) })
) )
$defer.onFailure(() => newVdi.$destroy()) $defer.onFailure(() => newVdi.$destroy())

View File

@@ -1,5 +1,3 @@
'use strict'
exports.extractIdsFromSimplePattern = function extractIdsFromSimplePattern(pattern) { exports.extractIdsFromSimplePattern = function extractIdsFromSimplePattern(pattern) {
if (pattern === undefined) { if (pattern === undefined) {
return [] return []

View File

@@ -1,5 +1,3 @@
'use strict'
const { utcFormat, utcParse } = require('d3-time-format') const { utcFormat, utcParse } = require('d3-time-format')
// Format a date in ISO 8601 in a safe way to be used in filenames // Format a date in ISO 8601 in a safe way to be used in filenames

View File

@@ -1,5 +1,3 @@
'use strict'
const eos = require('end-of-stream') const eos = require('end-of-stream')
const { PassThrough } = require('stream') const { PassThrough } = require('stream')

View File

@@ -1,5 +1,3 @@
'use strict'
// returns all entries but the last retention-th // returns all entries but the last retention-th
exports.getOldEntries = function getOldEntries(retention, entries) { exports.getOldEntries = function getOldEntries(retention, entries) {
return entries === undefined ? [] : retention > 0 ? entries.slice(0, -retention) : entries return entries === undefined ? [] : retention > 0 ? entries.slice(0, -retention) : entries

View File

@@ -1,6 +1,4 @@
'use strict' const Disposable = require('promise-toolbox/Disposable.js')
const Disposable = require('promise-toolbox/Disposable')
const { join } = require('path') const { join } = require('path')
const { mkdir, rmdir } = require('fs-extra') const { mkdir, rmdir } = require('fs-extra')
const { tmpdir } = require('os') const { tmpdir } = require('os')

View File

@@ -1,5 +1,3 @@
'use strict'
const BACKUP_DIR = 'xo-vm-backups' const BACKUP_DIR = 'xo-vm-backups'
exports.BACKUP_DIR = BACKUP_DIR exports.BACKUP_DIR = BACKUP_DIR

View File

@@ -1,26 +1,11 @@
'use strict'
const assert = require('assert') const assert = require('assert')
const COMPRESSED_MAGIC_NUMBERS = [ const isGzipFile = async (handler, fd) => {
// https://tools.ietf.org/html/rfc1952.html#page-5 // https://tools.ietf.org/html/rfc1952.html#page-5
Buffer.from('1F8B', 'hex'), const magicNumber = Buffer.allocUnsafe(2)
// https://github.com/facebook/zstd/blob/dev/doc/zstd_compression_format.md#zstandard-frames assert.strictEqual((await handler.read(fd, magicNumber, 0)).bytesRead, magicNumber.length)
Buffer.from('28B52FFD', 'hex'), return magicNumber[0] === 31 && magicNumber[1] === 139
]
const MAGIC_NUMBER_MAX_LENGTH = Math.max(...COMPRESSED_MAGIC_NUMBERS.map(_ => _.length))
const isCompressedFile = async (handler, fd) => {
const header = Buffer.allocUnsafe(MAGIC_NUMBER_MAX_LENGTH)
assert.strictEqual((await handler.read(fd, header, 0)).bytesRead, header.length)
for (const magicNumber of COMPRESSED_MAGIC_NUMBERS) {
if (magicNumber.compare(header, 0, magicNumber.length) === 0) {
return true
}
}
return false
} }
// TODO: better check? // TODO: better check?
@@ -58,8 +43,8 @@ async function isValidXva(path) {
return false return false
} }
return (await isCompressedFile(handler, fd)) return (await isGzipFile(handler, fd))
? true // compressed files cannot be validated at this time ? true // gzip files cannot be validated at this time
: await isValidTar(handler, size, fd) : await isValidTar(handler, size, fd)
} finally { } finally {
handler.closeFile(fd).catch(noop) handler.closeFile(fd).catch(noop)

View File

@@ -1,6 +1,4 @@
'use strict' const fromCallback = require('promise-toolbox/fromCallback.js')
const fromCallback = require('promise-toolbox/fromCallback')
const { createLogger } = require('@xen-orchestra/log') const { createLogger } = require('@xen-orchestra/log')
const { createParser } = require('parse-pairs') const { createParser } = require('parse-pairs')
const { execFile } = require('child_process') const { execFile } = require('child_process')

View File

@@ -1,6 +1,4 @@
'use strict' const fromCallback = require('promise-toolbox/fromCallback.js')
const fromCallback = require('promise-toolbox/fromCallback')
const { createParser } = require('parse-pairs') const { createParser } = require('parse-pairs')
const { execFile } = require('child_process') const { execFile } = require('child_process')

View File

@@ -1,5 +1,3 @@
'use strict'
exports.watchStreamSize = function watchStreamSize(stream, container = { size: 0 }) { exports.watchStreamSize = function watchStreamSize(stream, container = { size: 0 }) {
stream.on('data', data => { stream.on('data', data => {
container.size += data.length container.size += data.length

View File

@@ -1,52 +0,0 @@
- [File structure on remote](#file-structure-on-remote)
- [Structure of `metadata.json`](#structure-of-metadatajson)
- [Task logs](#task-logs)
- [During backup](#during-backup)
## File structure on remote
```
<remote>
├─ xo-config-backups
│ └─ <schedule ID>
│ └─ <YYYYMMDD>T<HHmmss>
│ ├─ metadata.json
│ └─ data.json
└─ xo-pool-metadata-backups
└─ <schedule ID>
└─ <pool UUID>
└─ <YYYYMMDD>T<HHmmss>
├─ metadata.json
└─ data
```
## Structure of `metadata.json`
```ts
interface Metadata {
jobId: String
jobName: String
scheduleId: String
scheduleName: String
timestamp: number
pool?: Pool
poolMaster?: Host
}
```
## Task logs
### During backup
```
job.start(data: { reportWhen: ReportWhen })
├─ task.start(data: { type: 'pool', id: string, pool?: Pool, poolMaster?: Host })
│ ├─ task.start(data: { type: 'remote', id: string })
│ │ └─ task.end
│ └─ task.end
├─ task.start(data: { type: 'xo' })
│ ├─ task.start(data: { type: 'remote', id: string })
│ │ └─ task.end
│ └─ task.end
└─ job.end
```

View File

@@ -1,97 +0,0 @@
- [File structure on remote](#file-structure-on-remote)
- [Attributes](#attributes)
- [Of created snapshots](#of-created-snapshots)
- [Of created VMs and snapshots](#of-created-vms-and-snapshots)
- [Of created VMs](#of-created-vms)
- [Task logs](#task-logs)
- [During backup](#during-backup)
- [During restoration](#during-restoration)
## File structure on remote
```
<remote>
└─ xo-vm-backups
├─ index.json // TODO
└─ <VM UUID>
├─ index.json // TODO
├─ vdis
│ └─ <job UUID>
│ └─ <VDI UUID>
│ ├─ index.json // TODO
│ └─ <YYYYMMDD>T<HHmmss>.vhd
├─ <YYYYMMDD>T<HHmmss>.json // backup metadata
├─ <YYYYMMDD>T<HHmmss>.xva
└─ <YYYYMMDD>T<HHmmss>.xva.checksum
```
## Attributes
### Of created snapshots
- `other_config`:
- `xo:backup:deltaChainLength` = n (number of delta copies/replicated since a full)
- `xo:backup:exported` = 'true' (added at the end of the backup)
### Of created VMs and snapshots
- `other_config`:
- `xo:backup:datetime`: format is UTC %Y%m%dT%H:%M:%SZ
- from snapshots: snapshot.snapshot_time
- with offline backup: formatDateTime(Date.now())
- `xo:backup:job` = job.id
- `xo:backup:schedule` = schedule.id
- `xo:backup:vm` = vm.uuid
### Of created VMs
- `name_label`: `${original name} - ${job name} - (${safeDateFormat(backup timestamp)})`
- tag:
- copy in delta mode: `Continuous Replication`
- copy in full mode: `Disaster Recovery`
- imported from backup: `restored from backup`
- `blocked_operations.start`: message
- for copies/replications only, added after complete transfer
- `other_config[xo:backup:sr]` = sr.uuid
## Task logs
### During backup
```
job.start(data: { mode: Mode, reportWhen: ReportWhen })
├─ task.info(message: 'vms', data: { vms: string[] })
├─ task.warning(message: string)
├─ task.start(data: { type: 'VM', id: string })
│ ├─ task.warning(message: string)
│ ├─ task.start(message: 'snapshot')
│ │ └─ task.end
│ ├─ task.start(message: 'export', data: { type: 'SR' | 'remote', id: string })
│ │ ├─ task.warning(message: string)
│ │ ├─ task.start(message: 'transfer')
│ │ │ ├─ task.warning(message: string)
│ │ │ └─ task.end(result: { size: number })
│ │ │
│ │ │ // in case of full backup, DR and CR
│ │ ├─ task.start(message: 'clean')
│ │ │ ├─ task.warning(message: string)
│ │ │ └─ task.end
│ │ │
│ │ │ // in case of delta backup
│ │ ├─ task.start(message: 'merge')
│ │ │ ├─ task.warning(message: string)
│ │ │ └─ task.end(result: { size: number })
│ │ │
│ │ └─ task.end
│ └─ task.end
└─ job.end
```
### During restoration
```
task.start(message: 'restore', data: { jobId: string, srId: string, time: number })
├─ task.start(message: 'transfer')
│ └─ task.end(result: { id: string, size: number })
└─ task.end
```

View File

@@ -1,5 +1,3 @@
'use strict'
const mapValues = require('lodash/mapValues.js') const mapValues = require('lodash/mapValues.js')
const { dirname } = require('path') const { dirname } = require('path')

View File

@@ -1,7 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'
const { catchGlobalErrors } = require('@xen-orchestra/log/configure.js') const { catchGlobalErrors } = require('@xen-orchestra/log/configure.js')
const { createLogger } = require('@xen-orchestra/log') const { createLogger } = require('@xen-orchestra/log')
const { getSyncedHandler } = require('@xen-orchestra/fs') const { getSyncedHandler } = require('@xen-orchestra/fs')
@@ -43,32 +41,13 @@ const main = Disposable.wrap(async function* main(args) {
let taskFiles let taskFiles
while ((taskFiles = await listRetry()) !== undefined) { while ((taskFiles = await listRetry()) !== undefined) {
const taskFileBasename = min(taskFiles) const taskFileBasename = min(taskFiles)
const previousTaskFile = join(CLEAN_VM_QUEUE, taskFileBasename)
const taskFile = join(CLEAN_VM_QUEUE, '_' + taskFileBasename) const taskFile = join(CLEAN_VM_QUEUE, '_' + taskFileBasename)
// move this task to the end // move this task to the end
try { await handler.rename(join(CLEAN_VM_QUEUE, taskFileBasename), taskFile)
await handler.rename(previousTaskFile, taskFile)
} catch (error) {
// this error occurs if the task failed too many times (i.e. too many `_` prefixes)
// there is nothing more that can be done
if (error.code === 'ENAMETOOLONG') {
await handler.unlink(previousTaskFile)
}
throw error
}
try { try {
const vmDir = getVmBackupDir(String(await handler.readFile(taskFile))) const vmDir = getVmBackupDir(String(await handler.readFile(taskFile)))
try {
await adapter.cleanVm(vmDir, { merge: true, onLog: info, remove: true }) await adapter.cleanVm(vmDir, { merge: true, onLog: info, remove: true })
} catch (error) {
// consider the clean successful if the VM dir is missing
if (error.code !== 'ENOENT') {
throw error
}
}
handler.unlink(taskFile).catch(error => warn('deleting task failure', { error })) handler.unlink(taskFile).catch(error => warn('deleting task failure', { error }))
} catch (error) { } catch (error) {

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