Compare commits

...

147 Commits

Author SHA1 Message Date
Julien Fontanet
4c991c1a57 WiP 2022-07-04 17:41:46 +02:00
Julien Fontanet
dfce56cee8 feat(async-each): add basic JsDoc typing 2022-07-04 17:37:52 +02:00
Julien Fontanet
a6fee2946a feat(async-each): concurrency 0 means no limit
It's identical to `Infinity` but has broader support (e.g. in JSON).
2022-07-04 17:22:47 +02:00
Julien Fontanet
34c849ee89 fix(vhd-lib/VhdAbstract#readBlock): return type 2022-07-04 10:57:44 +02:00
Mathieu
c7192ed3bf feat(xo-web): display maintenance mode badge next to the SR name (#6313) 2022-07-01 16:22:45 +02:00
Julien Fontanet
4d3dc0c5f7 feat: release 5.72.0 2022-06-30 16:47:32 +02:00
Julien Fontanet
9ba4afa073 chore(CHANGELOG): integrate released changes 2022-06-30 15:49:25 +02:00
Julien Fontanet
3ea4422d13 feat(xo-web): 5.99.0 2022-06-30 15:47:22 +02:00
Julien Fontanet
de2e314f7d feat(xo-server): 5.98.0 2022-06-30 15:46:58 +02:00
Julien Fontanet
2380fb42fe feat(@xen-orchestra/proxy): 0.23.4 2022-06-30 15:46:14 +02:00
Julien Fontanet
95b76076a3 feat(xo-remote-parser): 0.9.1 2022-06-30 15:45:29 +02:00
Julien Fontanet
b415d4c34c feat(vhd-lib): 3.3.1 2022-06-30 15:44:21 +02:00
Julien Fontanet
2d82b6dd6e feat(@xen-orchestra/xapi): 1.4.0 2022-06-30 15:38:10 +02:00
Mathieu
16b1935f12 feat(xo-server,xo-web/SR): display maintenance mode button (#6308)
Fixes #6215
2022-06-30 15:31:28 +02:00
Florent BEAUCHAMP
50ec614b2a feat(xo-web/remotes): ability to set useVhdDirectory in remote params (#6273) 2022-06-30 15:28:42 +02:00
rajaa-b
9e11a0af6e feat(xapi/VM_import): translate checksum error (#6304) 2022-06-30 12:08:36 +02:00
Florent BEAUCHAMP
0c3e42e0b9 fix(vhd-lib): fix VhdDirectory merge on non-S3 remote (#6310) 2022-06-30 11:40:21 +02:00
Julien Fontanet
36b31bb0b3 chore(vhd-lib/merge): minor comment improvement 2022-06-29 15:29:20 +02:00
Mathieu
c03c41450b feat: technical release (#6311) 2022-06-29 15:27:14 +02:00
Florent BEAUCHAMP
dfc2b5d88b feat(Backup): use vhd directory setting of remote (#6303) 2022-06-29 10:51:13 +02:00
Florent BEAUCHAMP
87e3e3ffe3 fix(xo-remote-parser): properly handle undefined options (#6309) 2022-06-29 10:26:50 +02:00
Rajaa.BARHTAOUI
dae37c6a50 feat(xo-web/tasks): show tasks for Self Service users (#6217)
See zammad#5436
2022-06-28 18:35:58 +02:00
Mathieu
c7df11cc6f feat(xo-web/user): user tokens management through XO interface (#6276) 2022-06-28 17:57:59 +02:00
Julien Fontanet
87f1f208c3 feat(vhd-cli): 0.8.0 2022-06-28 16:52:27 +02:00
Julien Fontanet
ba8c5d740e feat(vhd-cli info): list method with multiple VHDs 2022-06-27 16:24:43 +02:00
Julien Fontanet
c275d5d999 chore(vhd-cli): remove build step 2022-06-27 16:24:43 +02:00
Mathieu
cfc53c9c94 feat(xo-web/proxies): copy proxy URL (#6287) 2022-06-27 15:41:32 +02:00
Julien Fontanet
87df917157 feat(vhd-lib/merge): human readable UUID check
Introduced by a1bcd35e2
2022-06-27 14:10:15 +02:00
Julien Fontanet
395d87d290 chore(xo-common): remove build step 2022-06-23 17:24:54 +02:00
Julien Fontanet
aff8ec08ad chore(template): remove build step 2022-06-23 17:24:54 +02:00
Julien Fontanet
4d40b56d85 fix(xo-server/file restore): ignore non-regular files/dirs (#6305)
Fixes zammad#7648

This also ignore (broken and valid) symlinks.
2022-06-23 16:37:56 +02:00
Julien Fontanet
667d0724c3 docs(configuration/custom ca): fix systemd path
Introduced by 03a66e469
2022-06-22 11:32:24 +02:00
Julien Fontanet
a49395553a docs(configuration/custom ca): fix systemd path
Introduced by 03a66e469
2022-06-22 11:30:09 +02:00
Julien Fontanet
cce09bd9cc docs(configuration/custom ca): add note regarding XO Proxy 2022-06-22 10:44:25 +02:00
Julien Fontanet
03a66e4690 docs(configuration/custom ca): use separate systemd file
This is better as it avoids conflicts with existing config and is compatible with the way XO Proxy service is handled.
2022-06-22 10:44:25 +02:00
Florent BEAUCHAMP
fd752fee80 feat(backups,vhd-lib): implement copyless merge (#6271) 2022-06-22 10:36:57 +02:00
Julien Fontanet
8a71f84733 chore(xo-server): remove Model wrapping 2022-06-22 10:10:39 +02:00
Julien Fontanet
9ef2c7da4c chore(complex-matcher): remove build step 2022-06-22 09:55:59 +02:00
Julien Fontanet
8975073416 fix(xapi): add missing file
Introduced by b12c17947

Thanks @Danp2.
2022-06-22 00:07:32 +02:00
Julien Fontanet
d1c1378c9d feat(xo-server-db): minimal CLI to browser the DB 2022-06-21 18:11:44 +02:00
Julien Fontanet
7941284a1d feat(xo-server/collection/Redis): set of all indexes 2022-06-21 17:47:56 +02:00
Julien Fontanet
af2d17b7a5 feat(xo-server/collection/Redis): set of all namespaces 2022-06-21 17:29:19 +02:00
Julien Fontanet
3ca2b01d9a feat(xo-server/collection/Redis): assert namespace doesnt contain _ or : 2022-06-21 17:24:10 +02:00
Julien Fontanet
67193a2ab7 chore(xo-server/collection/Redis): replace prefix by namespace 2022-06-21 17:23:25 +02:00
Julien Fontanet
9757aa36de chore(xo-server/collection/Redis): _id field was never used 2022-06-21 17:23:18 +02:00
Julien Fontanet
29854a9f87 feat(xo-server): new sr.{enable,disable}MaintenanceMode methods 2022-06-21 15:07:09 +02:00
Julien Fontanet
b12c179470 feat(xapi): new SR_{enable,disable}MaintenanceMode methods 2022-06-21 15:07:09 +02:00
Julien Fontanet
bbef15e4e4 feat(xo-server/proxy.get{,All}); return associated URL(s) (#6291) 2022-06-21 11:33:25 +02:00
Florent BEAUCHAMP
c483929a0d fix(ova import): drain disk entry completly (#6284) 2022-06-20 16:09:20 +02:00
Julien Fontanet
1741f395dd chore(xo-server/deleteAuthenticationTokens): optimization
Don't use xo-server/deleteAuthenticationToken to avoid fetching the records twice.
2022-06-19 11:37:42 +02:00
Julien Fontanet
0f29262797 chore(value-matcher): remove build step 2022-06-19 11:28:11 +02:00
Julien Fontanet
31ed477b96 feat(xo-server/token.delete): available for non-admins 2022-06-17 11:59:29 +02:00
Julien Fontanet
9e5de5413d feat(xo-server/Collection#remove): accept a pattern 2022-06-17 11:59:29 +02:00
Florent BEAUCHAMP
0f297a81a4 feat(xo-remote-parser): additional parameters in URL (#6270) 2022-06-16 23:14:34 +02:00
Mathieu
89313def99 fix(xapi/vm): throw forbiddenOperation on blockedOperation (#6290) 2022-06-16 14:39:20 +02:00
Julien Fontanet
8e0be4edaf feat(xo-server/vm.set): blockedOperations now accepts string reasons and null
Related to #6290
2022-06-16 10:16:43 +02:00
Julien Fontanet
a8dfdfb922 fix(event-listeners-manager/add): _listeners is a Map 2022-06-15 14:37:38 +02:00
Julien Fontanet
f096024248 chore(event-listeners-manager): add tests 2022-06-15 14:37:31 +02:00
Julien Fontanet
4f50f90213 feat(xo-server/token.create): minimum duration is now one minute
This change also handles negative or zero invalid durations.
2022-06-15 11:26:32 +02:00
Julien Fontanet
4501902331 feat(xo-server): XO Proxy channel based on current channel (#6277) 2022-06-15 10:42:57 +02:00
Julien Fontanet
df19679dba fix(xo-cli): close connection when finished 2022-06-15 10:25:06 +02:00
Julien Fontanet
9f5a2f67f9 fix(xo-cli): xdg-basedir import
Introduced by 2d5c40632
2022-06-15 10:22:39 +02:00
Julien Fontanet
2d5c406325 chore: update dev deps 2022-06-13 19:33:09 +02:00
Julien Fontanet
151b8a8940 feat(read-chunk): add readChunkStrict 2022-06-13 12:01:02 +02:00
Julien Fontanet
cda027b94a docs(read-chunk): behavior when stream has ended 2022-06-13 11:22:42 +02:00
Julien Fontanet
ee2117abf6 chore(CHANGELOG.unreleased): pkgs list should be ordered
See https://team.vates.fr/vates/pl/1q6or14b9jffjfxk9qyebfg6sh
2022-06-13 11:22:08 +02:00
Thierry Goettelmann
6e7294d49f feat: release 5.71.1 (#6285) 2022-06-13 11:06:36 +02:00
Manon Mercier
062e45f697 docs(backup/troubleshooting): add no XAPI associated error (#6279)
Co-authored-by: Jon Sands <fohdeesha@gmail.com>
2022-06-13 10:07:20 +02:00
Julien Fontanet
d18b39990d feat(xo-server/api): introduce a global async API context (#6274)
This allows access to contextual data deep inside the call stack.

Example use cases:
- current user
- specific permission (e.g. read only token)
- current authentication token
2022-06-13 09:43:39 +02:00
Julien Fontanet
7387ac2411 fix(xo-server/disk.import): fix xapi._getOrWaitObject call
Maybe related to #6282

Introduced by 5063a6982
2022-06-10 17:34:33 +02:00
Thierry Goettelmann
4186592f9f feat: technical release (#6281) 2022-06-10 17:05:04 +02:00
Thierry Goettelmann
6c9d5a72a6 feat(xo-web/backup): show cleanVm logs only in case of warnings (#6280) 2022-06-09 22:07:29 +02:00
Julien Fontanet
83690a4dd4 fix(xo-server/_importOvaVm): fix VM creation
Fixes https://xcp-ng.org/forum/post/49920

Introduced by 2af5328a0f
2022-06-09 18:51:35 +02:00
Florent BEAUCHAMP
c11e03ab26 fix(xo-vmdk-to-vhd/generateVmdkData): don't use VM name as OVF filename
It might break the OVA depending on present characters.
2022-06-09 17:18:30 +02:00
Florent BEAUCHAMP
c7d8709267 fix(xo-vmdk-to-vhd/generateVmdkData): reduce compression level
The max value (9) is very slow and should be avoided.
2022-06-09 17:18:30 +02:00
Florent BEAUCHAMP
6579deffad fix(xo-server): don't create zombie task on OVA export
Introduced by 4b9db257f
2022-06-09 17:18:30 +02:00
Julien Fontanet
e2739e7a4b fix(xo-server): make auth tokens created_at/expiration numbers 2022-06-09 16:15:14 +02:00
Florent BEAUCHAMP
c0d587f541 fix(backups): task warning if beforeBackup or checkBaseVdis steps fail (#6266) 2022-06-09 14:39:25 +02:00
Florent BEAUCHAMP
05a96ffc14 fix(xo-web): handle missing result of broken merge tasks in backup logs (#6275) 2022-06-09 14:14:26 +02:00
Julien Fontanet
32a47444d7 feat(proxy-cli): new --url flag
Which can be used instead of `--host` and `--token`.
2022-06-09 13:38:06 +02:00
Julien Fontanet
9ff5de5f33 feat(xo-server): expose _xapiRef to the API
Fixes zammad#7439

This makes objects searchable by their opaque ref in the UI.
2022-06-09 09:52:17 +02:00
Julien Fontanet
09badf33d0 feat(docs/configuration): use NODE_EXTRA_CA_CERTS instead of --use-openssl-ca (#6226)
Fixes zammad#6310

Easier to use and compatible with more distributions.
2022-06-09 09:08:16 +02:00
Julien Fontanet
1643d3637f chore(xo-server/api): remove unused api from context 2022-06-08 22:52:24 +02:00
Julien Fontanet
b962e9ebe8 fix(xo-server/system.methodSignature): declare expected params 2022-06-08 22:52:03 +02:00
Julien Fontanet
66f3528e10 fix(xapi/VM_snapshot): handle undefined VM.VUSBs
Fixes zammad#7401
2022-06-08 16:29:27 +02:00
Julien Fontanet
a5e9f051a2 docs(REST API): content-type is no longer necessary with -T
Because it is no longer set by default to `application/x-www-form-urlencoded` like it was with `--data-binary`.
2022-06-07 23:46:14 +02:00
Julien Fontanet
63bfb76516 docs(REST API): use -T instead of --data-binary for cURL
Because `--data-binary` loads the whole data in memory which isn't compatible with big data like a VHD file, whereas `-T` streams the data to the server.
2022-06-07 23:38:05 +02:00
tkrafael
f88f7d41aa fix(xen-api/putResource): use agent for both requests (#6261)
Fixes #6260
2022-06-07 19:33:33 +02:00
Julien Fontanet
877383ac85 fix(xo-server/sr.createExt): fix SR_create call
Introduced by 052126613
2022-06-07 18:59:30 +02:00
Julien Fontanet
dd5e11e835 feat(xo-server/api): don't filters error sent to admin users (#6262)
Previous behavior was hiding all errors not explicitly dedicated to be sent to API users and replacing them with an *unknown error from the peer*.

This was done to avoid leaking sensitive information, but it often hides important info.

Administrators can already see the raw errors in Settings/Logs, therefore it makes sense to not hide them for these users.
2022-06-07 13:34:34 +02:00
Julien Fontanet
3d43550ffe feat(xo-cli): provide authentication token description 2022-06-07 10:57:28 +02:00
Julien Fontanet
115bc8fa0a feat(xo-server): authentication tokens can have a description 2022-06-07 10:57:26 +02:00
Julien Fontanet
15c46e324c feat(xo-server/api): new user.getAuthenticationTokens 2022-06-07 10:04:45 +02:00
Julien Fontanet
df38366066 fix(xo-server/collection/redis#get): correctly filter on properties when id is provided 2022-06-07 10:04:14 +02:00
Julien Fontanet
28b13ccfff fix(xo-server/collection/redis#get): don't mutate properties param 2022-06-07 09:57:25 +02:00
Julien Fontanet
26a433ebbe feat(xo-server/createAuthenticationToken): add created_at field 2022-06-07 09:20:34 +02:00
Julien Fontanet
1902595190 feat(xo-server/getAuthenticationTokensForUser): filter and remove expired tokens 2022-06-07 09:15:30 +02:00
Julien Fontanet
80146cfb58 feat(xo-server/proxies): expose auth tokens
First step to show expose them in the UI, to make XO Proxies easier to use as HTTP proxies.
2022-06-07 09:02:46 +02:00
Yannick Achy
03d2d6fc94 docs(backups): explain HTTP timeout error and auto power on behavior (#6263)
Co-authored-by: Jon Sands <fohdeesha@gmail.com>
2022-06-05 12:21:39 +02:00
Julien Fontanet
379e4d7596 chore(xo-server): use @xen-orchestra/xapi/VBD_unplug 2022-06-02 17:08:22 +02:00
Julien Fontanet
9860bd770b chore(xo-server): use @xen-orchestra/xapi/VBD_destroy 2022-06-02 17:07:18 +02:00
Julien Fontanet
2af5328a0f chore(xo-server): use @xen-orchestra/xapi/VM_create 2022-06-02 17:02:10 +02:00
Julien Fontanet
4084a44f83 chore(xo-server): use @xen-orchestra/xapi/VDI_exportContent 2022-06-02 16:57:21 +02:00
Julien Fontanet
ba7c7ddb23 chore(xo-server): use @xen-orchestra/xapi/VDI_importContent 2022-06-02 16:54:23 +02:00
Julien Fontanet
2351e7b98c chore(xo-server): use @xen-orchestra/xapi/VBD_create 2022-06-02 16:37:49 +02:00
Julien Fontanet
d353dc622c fix(xapi/VBD_create): don't fail if the VBD could not be plugged
Otherwise, the creation method would have failed but the VBD would still exist, violating the principle of least surprise.
2022-06-02 16:26:29 +02:00
Julien Fontanet
3ef6adfd02 feat(xapi/VBD_create): returns the new VBD's ref 2022-06-02 16:25:19 +02:00
Julien Fontanet
5063a6982a chore(xo-server): use @xen-orchestra/xapi/VDI_create 2022-06-02 16:10:16 +02:00
Julien Fontanet
0008f2845c feat(xapi/VDI_create): move sm_config in second param
Similarly to other creation methods, properties that must be explicited are passed in second param.
2022-06-02 14:45:57 +02:00
Julien Fontanet
a0994bc428 fix(scripts/gen-deps-list.js): add missing await
Introduced by a0836ebdd
2022-06-01 16:51:31 +02:00
Julien Fontanet
8fe0d97aec fix(scripts/gen-deps-list.js): fix packages order (#6259)
`deptree` nodes should be added only once with the full list of their dependencies.

For better display, packages are sorted by names before resolving the graph for nicer display.
2022-06-01 16:07:36 +02:00
Julien Fontanet
a8b3c02780 chore(CHANGELOG): integrate released changes 2022-06-01 15:56:01 +02:00
Julien Fontanet
f3489fb57c feat(xo-web): 5.97.1 2022-06-01 15:51:16 +02:00
Julien Fontanet
434b5b375d feat(xo-server): 5.95.0 2022-06-01 15:51:16 +02:00
Julien Fontanet
445120f9f5 feat(@xen-orchestra/proxy): 0.23.1 2022-06-01 15:51:16 +02:00
Julien Fontanet
71b11f0d9c feat(@xen-orchestra/xapi): 1.1.0 2022-06-01 15:51:16 +02:00
Julien Fontanet
8297a9e0e7 feat(@xen-orchestra/fs): 1.0.3 2022-06-01 15:51:16 +02:00
Florent BEAUCHAMP
4999672f2d fix(xo-web/backups): scheduled health check is available to enterprise (#6257)
Introduced by cae3555ca
2022-06-01 15:36:36 +02:00
Thierry Goettelmann
70608ed7e9 fix(scripts/gen-deps-lists.js): various fixes 2022-06-01 14:04:41 +02:00
Julien Fontanet
a0836ebdd7 feat(scripts/gen-deps-list.js): test mode (#6258) 2022-06-01 13:53:56 +02:00
Florent BEAUCHAMP
2b1edd1d4c feat: always log and display full remote errors (#6216)
Co-authored-by: Julien Fontanet <julien.fontanet@isonoe.net>
2022-05-31 17:30:27 +02:00
Thierry Goettelmann
42bb7cc973 feat: release 5.71.0 (#6256) 2022-05-31 16:20:41 +02:00
Julien Fontanet
8299c37bb7 fix(xo-server/pool.rollingUpdate): duplicate poolId declaration
Introduced by 7a2005c20
2022-05-31 14:32:13 +02:00
Mathieu
7a2005c20c feat(xo-server/pool): disable scheduled job when starting RPU (#6244)
See zammad#5377, zammad#5333
2022-05-31 11:59:52 +02:00
Pierre Donias
ae0eb9e66e fix(xo-web/health): make "Too many snapshots" table sortable by number of snaphots (#6255)
See zammad#6439
2022-05-31 11:45:11 +02:00
Julien Fontanet
052126613a feat(xapi,xo-server): create SRs with other_config.auto-scan=true (#6246)
Fixes https://team.vates.fr/vates/pl/nf18hnr51f8f3f3brcbra57uar
2022-05-31 11:24:15 +02:00
l-austenfeld
7959657bd6 fix(xo-server/xapi): missing shutdownHost default parameter (#6253)
Add a default empty object parameter to enable calls to shutdownHost with only one parameter.
This implicitly fixes the density load-balancer, since it calls shutdownHost with only one parameter.
2022-05-31 10:01:47 +02:00
Thierry Goettelmann
9f8bb376ea feat: technical release (#6254) 2022-05-30 17:45:59 +02:00
Julien Fontanet
ee8e2fa906 docs(REST API): use | cat trick in VDI import example 2022-05-30 16:51:35 +02:00
Julien Fontanet
33a380b173 docs(REST API): add name_label param in VDI import example 2022-05-30 16:50:36 +02:00
Julien Fontanet
6e5b6996fa docs(REST API): add required content-type in VM import 2022-05-30 16:48:01 +02:00
Julien Fontanet
6409dc276c docs(REST API): don't use --progress-bar in VDI import example
This is not necessary and more in line with other examples.
2022-05-30 16:46:54 +02:00
Julien Fontanet
98f7ce43e3 feat(xo-server/RestApi): VDI import now returns the new VDI's UUID 2022-05-30 16:45:41 +02:00
Julien Fontanet
aa076e1d2d chore(xo-server/rest-api): use xen-api shorthand syntax 2022-05-30 16:23:39 +02:00
Julien Fontanet
7a096d1b5c chore(xo-server/rest-api): remove unnecessary awaits 2022-05-30 16:00:43 +02:00
Julien Fontanet
93b17ccddd chore(xo-server/api/vm): format with Prettier
Introduced by d7d81431e
2022-05-30 16:00:43 +02:00
Julien Fontanet
68c118c3e5 fix(xo-server/api/vm): add missing quote
Introduced by d7d81431e
2022-05-30 16:00:43 +02:00
Thierry Goettelmann
c0b0ba433f feat(backups,xo-web): add cleanVm warnings to task (#6225) 2022-05-30 15:39:54 +02:00
Thierry Goettelmann
d7d81431ef feat(xo-server/vm.migrate): call VM.assert_can_migrate before (#6245)
Fixes #5301
2022-05-30 15:04:12 +02:00
Pierre Donias
7451f45885 fix(xo-web/home): don't make VM's resource set name clickable for non-admins (#6252)
See https://xcp-ng.org/forum/topic/5902/permissions-for-users-to-be-able-to-snapshot/5?_=1653902135402

Non-admin users aren't allowed to view the Self Service page so it doesn't make
sense to have a link to that page
2022-05-30 15:02:03 +02:00
Florent BEAUCHAMP
c9882001a9 fix(xo-web,xo-server): store health check settings in job instead of schedule (#6251)
Introduced by cae3555ca7
2022-05-30 14:56:28 +02:00
Mathieu
837b06ef2b feat(xo-server/xo-web/pool): avoid RPU/ host reboot, shutdown / host agent reboot during backup (#6232)
See zammad#5377
2022-05-30 11:13:13 +02:00
Julien Fontanet
0e49150b8e feat(xo-server/RestApi): add VDI import
Related to zammad#7036
2022-05-29 20:48:59 +02:00
Julien Fontanet
0ec5f4bf68 chore(proxy,xo-server): update to http-server-plus@0.11.1
This new version fixes, among others, the support of the `Expect: 100-Continue` HTTP header, which is notably used by cURL during `POST`.
2022-05-29 20:44:00 +02:00
Julien Fontanet
601730d737 feat(xapi): new SR_importVdi()
Creates a new VDI on an SR from a VHD.
2022-05-29 20:44:00 +02:00
Julien Fontanet
28eb4b21bd fix(xo-server/disk.import): VHD import
Introduced by 0706e6f4ff
2022-05-29 14:09:08 +02:00
Julien Fontanet
a5afe0bca1 feat(vhd-lib/peekFooterFromStream): check checksum and content 2022-05-29 14:07:48 +02:00
211 changed files with 5060 additions and 3584 deletions

2
.gitignore vendored
View File

@@ -10,8 +10,6 @@
/packages/*/dist/
/packages/*/node_modules/
/packages/vhd-cli/src/commands/index.js
/packages/xen-api/examples/node_modules/
/packages/xen-api/plot.dat

View File

@@ -1,4 +1,4 @@
### `asyncEach(iterable, iteratee, [opts])`
### `pEach(iterable, iteratee, [opts])`
Executes `iteratee` in order for each value yielded by `iterable`.
@@ -6,7 +6,7 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
`iterable` must be an iterable or async iterable.
`iteratee` is called with the same `this` value as `asyncEach`, and with the following arguments:
`iteratee` is called with the same `this` value as `pEach`, and with the following arguments:
- `value`: the value yielded by `iterable`
- `index`: the 0-based index for this value
@@ -14,15 +14,15 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
`opts` is an object that can contains the following options:
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`. The value `0` means no concurrency limit.
- `signal`: an abort signal to stop the iteration
- `stopOnError`: wether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`
```js
import { asyncEach } from '@vates/async-each'
import { pEach } from '@vates/async-each'
const contents = []
await asyncEach(
await pEach(
['foo.txt', 'bar.txt', 'baz.txt'],
async function (filename, i) {
contents[i] = await readFile(filename)

View File

@@ -16,7 +16,7 @@ Installation of the [npm package](https://npmjs.org/package/@vates/async-each):
## Usage
### `asyncEach(iterable, iteratee, [opts])`
### `pEach(iterable, iteratee, [opts])`
Executes `iteratee` in order for each value yielded by `iterable`.
@@ -24,7 +24,7 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
`iterable` must be an iterable or async iterable.
`iteratee` is called with the same `this` value as `asyncEach`, and with the following arguments:
`iteratee` is called with the same `this` value as `pEach`, and with the following arguments:
- `value`: the value yielded by `iterable`
- `index`: the 0-based index for this value
@@ -32,15 +32,15 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
`opts` is an object that can contains the following options:
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`. The value `0` means no concurrency limit.
- `signal`: an abort signal to stop the iteration
- `stopOnError`: wether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`
```js
import { asyncEach } from '@vates/async-each'
import { pEach } from '@vates/async-each'
const contents = []
await asyncEach(
await pEach(
['foo.txt', 'bar.txt', 'baz.txt'],
async function (filename, i) {
contents[i] = await readFile(filename)

View File

@@ -9,7 +9,16 @@ class AggregateError extends Error {
}
}
exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 1, signal, stopOnError = true } = {}) {
/**
* @template Item
* @param {Iterable<Item>} iterable
* @param {(item: Item, index: number, iterable: Iterable<Item>) => Promise<void>} iteratee
* @returns {Promise<void>}
*/
exports.pEach = function pEach(iterable, iteratee, { concurrency = 10, signal, stopOnError = true } = {}) {
if (concurrency === 0) {
concurrency = Infinity
}
return new Promise((resolve, reject) => {
const it = (iterable[Symbol.iterator] || iterable[Symbol.asyncIterator]).call(iterable)
const errors = []
@@ -19,7 +28,7 @@ exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 1, si
let onAbort
if (signal !== undefined) {
onAbort = () => {
onRejectedWrapper(new Error('asyncEach aborted'))
onRejectedWrapper(new Error('pEach aborted'))
}
signal.addEventListener('abort', onAbort)
}
@@ -42,11 +51,10 @@ exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 1, si
clean()
})(reject)
let onFulfilled = value => {
let onFulfilled = () => {
--running
next()
}
const onFulfilledWrapper = value => onFulfilled(value)
let onRejected = stopOnError
? reject
@@ -57,6 +65,14 @@ exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 1, si
}
const onRejectedWrapper = reason => onRejected(reason)
const run = async (value, index) => {
try {
onFulfilled(await iteratee.call(this, value, index++))
} catch (error) {
onRejected(error)
}
}
let nextIsRunning = false
let next = async () => {
if (nextIsRunning) {
@@ -77,17 +93,7 @@ exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 1, si
}
} else {
++running
try {
const result = iteratee.call(this, cursor.value, index++, iterable)
let then
if (result != null && typeof result === 'object' && typeof (then = result.then) === 'function') {
then.call(result, onFulfilledWrapper, onRejectedWrapper)
} else {
onFulfilled(result)
}
} catch (error) {
onRejected(error)
}
run(cursor.value, index++, iterable)
}
nextIsRunning = false
return next()

View File

@@ -2,7 +2,7 @@
/* eslint-env jest */
const { asyncEach } = require('./')
const { pEach } = require('./')
const randomDelay = (max = 10) =>
new Promise(resolve => {
@@ -14,7 +14,7 @@ const rejectionOf = p =>
p.then(reject, resolve)
})
describe('asyncEach', () => {
describe('pEach', () => {
const thisArg = 'qux'
const values = ['foo', 'bar', 'baz']
@@ -36,7 +36,7 @@ describe('asyncEach', () => {
it('works', async () => {
const iteratee = jest.fn(async () => {})
await asyncEach.call(thisArg, iterable, iteratee)
await pEach.call(thisArg, iterable, iteratee)
expect(iteratee.mock.instances).toEqual(Array.from(values, () => thisArg))
expect(iteratee.mock.calls).toEqual(Array.from(values, (value, index) => [value, index, iterable]))
@@ -45,7 +45,7 @@ describe('asyncEach', () => {
it('respects a concurrency of ' + concurrency, async () => {
let running = 0
await asyncEach(
await pEach(
values,
async () => {
++running
@@ -66,7 +66,7 @@ describe('asyncEach', () => {
}
})
expect(await rejectionOf(asyncEach(iterable, iteratee, { stopOnError: true }))).toBe(error)
expect(await rejectionOf(pEach(iterable, iteratee, { stopOnError: true }))).toBe(error)
expect(iteratee).toHaveBeenCalledTimes(2)
})
@@ -78,7 +78,7 @@ describe('asyncEach', () => {
throw error
})
const error = await rejectionOf(asyncEach(iterable, iteratee, { stopOnError: false }))
const error = await rejectionOf(pEach(iterable, iteratee, { stopOnError: false }))
expect(error.errors).toEqual(errors)
expect(iteratee.mock.calls).toEqual(Array.from(values, (value, index) => [value, index, iterable]))
})
@@ -91,7 +91,7 @@ describe('asyncEach', () => {
}
})
await expect(asyncEach(iterable, iteratee, { signal: ac.signal })).rejects.toThrow('asyncEach aborted')
await expect(pEach(iterable, iteratee, { signal: ac.signal })).rejects.toThrow('pEach aborted')
expect(iteratee).toHaveBeenCalledTimes(2)
})
})

View File

@@ -9,7 +9,7 @@ exports.EventListenersManager = class EventListenersManager {
}
add(type, listener) {
let listeners = this._listeners[type]
let listeners = this._listeners.get(type)
if (listeners === undefined) {
listeners = new Set()
this._listeners.set(type, listeners)

View File

@@ -0,0 +1,67 @@
'use strict'
const t = require('tap')
const { EventEmitter } = require('events')
const { EventListenersManager } = require('./')
const noop = Function.prototype
// function spy (impl = Function.prototype) {
// function spy() {
// spy.calls.push([Array.from(arguments), this])
// }
// spy.calls = []
// return spy
// }
function assertListeners(t, event, listeners) {
t.strictSame(t.context.ee.listeners(event), listeners)
}
t.beforeEach(function (t) {
t.context.ee = new EventEmitter()
t.context.em = new EventListenersManager(t.context.ee)
})
t.test('.add adds a listener', function (t) {
t.context.em.add('foo', noop)
assertListeners(t, 'foo', [noop])
t.end()
})
t.test('.add does not add a duplicate listener', function (t) {
t.context.em.add('foo', noop).add('foo', noop)
assertListeners(t, 'foo', [noop])
t.end()
})
t.test('.remove removes a listener', function (t) {
t.context.em.add('foo', noop).remove('foo', noop)
assertListeners(t, 'foo', [])
t.end()
})
t.test('.removeAll removes all listeners of a given type', function (t) {
t.context.em.add('foo', noop).add('bar', noop).removeAll('foo')
assertListeners(t, 'foo', [])
assertListeners(t, 'bar', [noop])
t.end()
})
t.test('.removeAll removes all listeners', function (t) {
t.context.em.add('foo', noop).add('bar', noop).removeAll()
assertListeners(t, 'foo', [])
assertListeners(t, 'bar', [])
t.end()
})

View File

@@ -35,8 +35,12 @@
"url": "https://vates.fr"
},
"license": "ISC",
"version": "1.0.0",
"version": "1.0.1",
"scripts": {
"postversion": "npm publish --access public"
"postversion": "npm publish --access public",
"test": "tap --branches=72"
},
"devDependencies": {
"tap": "^16.2.0"
}
}

View File

@@ -1,6 +1,9 @@
### `readChunk(stream, [size])`
- returns the next available chunk of data
- like `stream.read()`, a number of bytes can be specified
- returns `null` if the stream has ended
- returns with less data than expected if stream has ended
- returns `null` if the stream has ended and no data has been read
```js
import { readChunk } from '@vates/read-chunk'
@@ -11,3 +14,13 @@ import { readChunk } from '@vates/read-chunk'
}
})()
```
### `readChunkStrict(stream, [size])`
Similar behavior to `readChunk` but throws if the stream ended before the requested data could be read.
```js
import { readChunkStrict } from '@vates/read-chunk'
const chunk = await readChunkStrict(stream, 1024)
```

View File

@@ -16,9 +16,12 @@ Installation of the [npm package](https://npmjs.org/package/@vates/read-chunk):
## Usage
### `readChunk(stream, [size])`
- returns the next available chunk of data
- like `stream.read()`, a number of bytes can be specified
- returns `null` if the stream has ended
- returns with less data than expected if stream has ended
- returns `null` if the stream has ended and no data has been read
```js
import { readChunk } from '@vates/read-chunk'
@@ -30,6 +33,16 @@ import { readChunk } from '@vates/read-chunk'
})()
```
### `readChunkStrict(stream, [size])`
Similar behavior to `readChunk` but throws if the stream ended before the requested data could be read.
```js
import { readChunkStrict } from '@vates/read-chunk'
const chunk = await readChunkStrict(stream, 1024)
```
## Contributions
Contributions are _very_ welcomed, either on the documentation or on

View File

@@ -30,3 +30,22 @@ const readChunk = (stream, size) =>
onReadable()
})
exports.readChunk = readChunk
exports.readChunkStrict = async function readChunkStrict(stream, size) {
const chunk = await readChunk(stream, size)
if (chunk === null) {
throw new Error('stream has ended without data')
}
if (size !== undefined && chunk.length !== size) {
const error = new Error('stream has ended with not enough data')
Object.defineProperties(error, {
chunk: {
value: chunk,
},
})
throw error
}
return chunk
}

View File

@@ -4,7 +4,7 @@
const { Readable } = require('stream')
const { readChunk } = require('./')
const { readChunk, readChunkStrict } = require('./')
const makeStream = it => Readable.from(it, { objectMode: false })
makeStream.obj = Readable.from
@@ -43,3 +43,27 @@ describe('readChunk', () => {
})
})
})
const rejectionOf = promise =>
promise.then(
value => {
throw value
},
error => error
)
describe('readChunkStrict', function () {
it('throws if stream is empty', async () => {
const error = await rejectionOf(readChunkStrict(makeStream([])))
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('stream has ended without data')
expect(error.chunk).toEqual(undefined)
})
it('throws if stream ends with not enough data', async () => {
const error = await rejectionOf(readChunkStrict(makeStream(['foo', 'bar']), 10))
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('stream has ended with not enough data')
expect(error.chunk).toEqual(Buffer.from('foobar'))
})
})

View File

@@ -19,7 +19,7 @@
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"version": "0.1.2",
"version": "1.0.0",
"engines": {
"node": ">=8.10"
},

View File

@@ -23,7 +23,7 @@ module.exports = async function main(args) {
},
})
await asyncMap(_, async vmDir => {
await pEach(_, async vmDir => {
vmDir = resolve(vmDir)
try {
await adapter.cleanVm(vmDir, { fixMetadata: fix, remove, merge, onLog: (...args) => console.warn(...args) })

View File

@@ -11,8 +11,8 @@ module.exports = async function createSymlinkIndex([backupDir, fieldPath]) {
const indexDir = join(backupDir, 'indexes', filenamify(fieldPath))
await mktree(indexDir)
await asyncMap(await readdir2(backupDir), async vmDir =>
asyncMap(
await pEach(await readdir2(backupDir), async vmDir =>
pEach(
(await readdir2(vmDir)).filter(_ => _.endsWith('.json')),
async json => {
const metadata = JSON.parse(await readFile(json))

View File

@@ -7,8 +7,8 @@
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"dependencies": {
"@xen-orchestra/async-map": "^0.1.2",
"@xen-orchestra/backups": "^0.23.0",
"@xen-orchestra/fs": "^1.0.1",
"@xen-orchestra/backups": "^0.26.0",
"@xen-orchestra/fs": "^1.1.0",
"filenamify": "^4.1.0",
"getopts": "^2.2.5",
"lodash": "^4.17.15",
@@ -27,7 +27,7 @@
"scripts": {
"postversion": "npm publish --access public"
},
"version": "0.7.1",
"version": "0.7.4",
"license": "AGPL-3.0-or-later",
"author": {
"name": "Vates SAS",

View File

@@ -6,7 +6,7 @@ const ignoreErrors = require('promise-toolbox/ignoreErrors')
const { compileTemplate } = require('@xen-orchestra/template')
const { limitConcurrency } = require('limit-concurrency-decorator')
const { extractIdsFromSimplePattern } = require('./_extractIdsFromSimplePattern.js')
const { extractIdsFromSimplePattern } = require('./extractIdsFromSimplePattern.js')
const { PoolMetadataBackup } = require('./_PoolMetadataBackup.js')
const { Task } = require('./Task.js')
const { VmBackup } = require('./_VmBackup.js')
@@ -159,7 +159,7 @@ exports.Backup = class Backup {
const promises = []
if (pools.length !== 0 && settings.retentionPoolMetadata !== 0) {
promises.push(
asyncMap(pools, async pool =>
pEach(pools, async pool =>
runTask(
{
name: `Starting metadata backup for the pool (${pool.$id}). (${job.id})`,
@@ -245,7 +245,7 @@ exports.Backup = class Backup {
})
)
),
() => settings.healthCheckSr !== undefined ? this._getRecord('SR', settings.healthCheckSr) : undefined,
() => (settings.healthCheckSr !== undefined ? this._getRecord('SR', settings.healthCheckSr) : undefined),
async (srs, remoteAdapters, healthCheckSr) => {
// remove adapters that failed (already handled)
remoteAdapters = remoteAdapters.filter(_ => _ !== undefined)
@@ -284,7 +284,10 @@ exports.Backup = class Backup {
)
)
const { concurrency } = settings
await asyncMapSettled(vmIds, concurrency === 0 ? handleVm : limitConcurrency(concurrency)(handleVm))
await pEach(vmIds, handleVm, {
concurrency,
stopOnError: false,
})
}
)
}

View File

@@ -1,6 +1,8 @@
'use strict'
const { asyncMap } = require('@xen-orchestra/async-map')
const { pEach } = require('@xen-orchestra/async-map')
const noop = Function.protoype
exports.DurablePartition = class DurablePartition {
// private resource API is used exceptionally to be able to separate resource creation and release
@@ -8,10 +10,10 @@ exports.DurablePartition = class DurablePartition {
flushAll() {
const partitionDisposers = this.#partitionDisposers
return asyncMap(Object.keys(partitionDisposers), path => {
return pEach(Object.keys(partitionDisposers), path => {
const disposers = partitionDisposers[path]
delete partitionDisposers[path]
return asyncMap(disposers, d => d(path).catch(noop => {}))
return pEach(disposers, d => d(path).catch(noop))
})
}

View File

@@ -15,7 +15,7 @@ const { deduped } = require('@vates/disposable/deduped.js')
const { decorateMethodsWith } = require('@vates/decorate-with')
const { compose } = require('@vates/compose')
const { execFile } = require('child_process')
const { readdir, stat } = require('fs-extra')
const { readdir, lstat } = require('fs-extra')
const { v4: uuidv4 } = require('uuid')
const { ZipFile } = require('yazl')
const zlib = require('zlib')
@@ -47,13 +47,10 @@ const resolveSubpath = (root, path) => resolve(root, `.${resolve('/', path)}`)
const RE_VHDI = /^vhdi(\d+)$/
async function addDirectory(files, realPath, metadataPath) {
try {
const subFiles = await readdir(realPath)
await asyncMap(subFiles, file => addDirectory(files, realPath + '/' + file, metadataPath + '/' + file))
} catch (error) {
if (error == null || error.code !== 'ENOTDIR') {
throw error
}
const stats = await lstat(realPath)
if (stats.isDirectory()) {
await pEach(await readdir(realPath), file => addDirectory(files, realPath + '/' + file, metadataPath + '/' + file))
} else if (stats.isFile()) {
files.push({
realPath,
metadataPath,
@@ -183,7 +180,7 @@ class RemoteAdapter {
const path = yield this.getPartition(diskId, partitionId)
const files = []
await asyncMap(paths, file =>
await pEach(paths, file =>
addDirectory(files, resolveSubpath(path, file), normalize('./' + file).replace(/\/+$/, ''))
)
@@ -229,7 +226,9 @@ class RemoteAdapter {
const handler = this._handler
// this will delete the json, unused VHDs will be detected by `cleanVm`
await asyncMapSettled(backups, ({ _filename }) => handler.unlink(_filename))
await pEach(backups, ({ _filename }) => handler.unlink(_filename), {
stopOnError: false,
})
}
async deleteMetadataBackup(backupId) {
@@ -249,13 +248,16 @@ class RemoteAdapter {
let list = await handler.list(dir)
list.sort()
list = list.filter(timestamp => /^\d{8}T\d{6}Z$/.test(timestamp)).slice(0, -retention)
await asyncMapSettled(list, timestamp => handler.rmtree(`${dir}/${timestamp}`))
await pEach(list, timestamp => handler.rmtree(`${dir}/${timestamp}`), { stopOnError: false })
}
async deleteFullVmBackups(backups) {
const handler = this._handler
await asyncMapSettled(backups, ({ _filename, xva }) =>
Promise.all([handler.unlink(_filename), handler.unlink(resolveRelativeFromFile(_filename, xva))])
await pEach(
backups,
({ _filename, xva }) =>
Promise.all([handler.unlink(_filename), handler.unlink(resolveRelativeFromFile(_filename, xva))]),
{ stopOnError: false }
)
}
@@ -264,7 +266,7 @@ class RemoteAdapter {
}
async deleteVmBackups(files) {
const metadatas = await asyncMap(files, file => this.readVmBackupMetadata(file))
const metadatas = await pEach(files, file => this.readVmBackupMetadata(file))
const { delta, full, ...others } = groupBy(metadatas, 'mode')
const unsupportedModes = Object.keys(others)
@@ -284,7 +286,7 @@ class RemoteAdapter {
}
const dedupedVmUuid = new Set(metadatas.map(_ => _.vm.uuid))
await asyncMap(dedupedVmUuid, vmUuid => this.invalidateVmBackupListCache(vmUuid))
await pEach(dedupedVmUuid, vmUuid => this.invalidateVmBackupListCache(vmUuid))
}
#getCompressionType() {
@@ -292,7 +294,7 @@ class RemoteAdapter {
}
#useVhdDirectory() {
return this.handler.type === 's3'
return this.handler.useVhdDirectory()
}
#useAlias() {
@@ -363,7 +365,7 @@ class RemoteAdapter {
const handler = this._handler
const backups = { __proto__: null }
await asyncMap(await handler.list(BACKUP_DIR), async entry => {
await pEach(await handler.list(BACKUP_DIR), async entry => {
// ignore hidden and lock files
if (entry[0] !== '.' && !entry.endsWith('.lock')) {
const vmBackups = await this.listVmBackups(entry)
@@ -381,10 +383,14 @@ class RemoteAdapter {
path = resolveSubpath(rootPath, path)
const entriesMap = {}
await asyncMap(await readdir(path), async name => {
await pEach(await readdir(path), async name => {
try {
const stats = await stat(`${path}/${name}`)
entriesMap[stats.isDirectory() ? `${name}/` : name] = {}
const stats = await lstat(`${path}/${name}`)
if (stats.isDirectory()) {
entriesMap[name + '/'] = {}
} else if (stats.isFile()) {
entriesMap[name] = {}
}
} catch (error) {
if (error == null || error.code !== 'ENOENT') {
throw error
@@ -410,10 +416,13 @@ class RemoteAdapter {
}
const results = []
await asyncMapSettled(partitions, partition =>
partition.type === LVM_PARTITION_TYPE
? this._listLvmLogicalVolumes(devicePath, partition, results)
: results.push(partition)
await pEach(
partitions,
partition =>
partition.type === LVM_PARTITION_TYPE
? this._listLvmLogicalVolumes(devicePath, partition, results)
: results.push(partition),
{ stopOnError: false }
)
return results
})
@@ -424,10 +433,10 @@ class RemoteAdapter {
const safeReaddir = createSafeReaddir(handler, 'listPoolMetadataBackups')
const backupsByPool = {}
await asyncMap(await safeReaddir(DIR_XO_POOL_METADATA_BACKUPS, { prependDir: true }), async scheduleDir =>
asyncMap(await safeReaddir(scheduleDir), async poolId => {
await pEach(await safeReaddir(DIR_XO_POOL_METADATA_BACKUPS, { prependDir: true }), async scheduleDir =>
pEach(await safeReaddir(scheduleDir), async poolId => {
const backups = backupsByPool[poolId] ?? (backupsByPool[poolId] = [])
return asyncMap(await safeReaddir(`${scheduleDir}/${poolId}`, { prependDir: true }), async backupDir => {
return pEach(await safeReaddir(`${scheduleDir}/${poolId}`, { prependDir: true }), async backupDir => {
try {
backups.push({
id: backupDir,
@@ -468,7 +477,7 @@ class RemoteAdapter {
filter: isMetadataFile,
prependDir: true,
})
await asyncMap(files, async file => {
await pEach(files, async file => {
try {
const metadata = await this.readVmBackupMetadata(file)
// inject an id usable by importVmBackupNg()
@@ -552,8 +561,8 @@ class RemoteAdapter {
const safeReaddir = createSafeReaddir(handler, 'listXoMetadataBackups')
const backups = []
await asyncMap(await safeReaddir(DIR_XO_CONFIG_BACKUPS, { prependDir: true }), async scheduleDir =>
asyncMap(await safeReaddir(scheduleDir, { prependDir: true }), async backupDir => {
await pEach(await safeReaddir(DIR_XO_CONFIG_BACKUPS, { prependDir: true }), async scheduleDir =>
pEach(await safeReaddir(scheduleDir, { prependDir: true }), async backupDir => {
try {
backups.push({
id: backupDir,
@@ -632,9 +641,13 @@ class RemoteAdapter {
const vdis = ignoredVdis === undefined ? metadata.vdis : pickBy(metadata.vdis, vdi => !ignoredVdis.has(vdi.uuid))
const streams = {}
await asyncMapSettled(Object.keys(vdis), async ref => {
streams[`${ref}.vhd`] = await this._createSyntheticStream(handler, join(dir, vhds[ref]))
})
await pEach(
Object.keys(vdis),
async ref => {
streams[`${ref}.vhd`] = await this._createSyntheticStream(handler, join(dir, vhds[ref]))
},
{ stopOnError: false }
)
return {
streams,

View File

@@ -1,6 +1,6 @@
'use strict'
const { asyncMap } = require('@xen-orchestra/async-map')
const { pEach } = require('@vates/async-each')
const { DIR_XO_POOL_METADATA_BACKUPS } = require('./RemoteAdapter.js')
const { forkStreamUnpipe } = require('./_forkStreamUnpipe.js')
@@ -52,7 +52,7 @@ exports.PoolMetadataBackup = class PoolMetadataBackup {
)
const metaDataFileName = `${dir}/metadata.json`
await asyncMap(
await pEach(
Object.entries(this._remoteAdapters),
([remoteId, adapter]) =>
Task.run(

View File

@@ -146,13 +146,20 @@ class VmBackup {
}
const errors = []
await (parallel ? asyncMap : asyncEach)(writers, async function (writer) {
await (parallel ? pEach : asyncEach)(writers, async function (writer) {
try {
await fn(writer)
} catch (error) {
errors.push(error)
this.delete(writer)
warn(warnMessage, { error, writer: writer.constructor.name })
// these two steps are the only one that are not already in their own sub tasks
if (warnMessage === 'writer.checkBaseVdis()' || warnMessage === 'writer.beforeBackup()') {
Task.warning(
`the writer ${writer.constructor.name} has failed the step ${warnMessage} with error ${error.message}. It won't be used anymore in this job execution.`
)
}
}
})
if (writers.size === 0) {
@@ -324,13 +331,13 @@ class VmBackup {
const snapshotsPerSchedule = groupBy(this._jobSnapshots, _ => _.other_config['xo:backup:schedule'])
const xapi = this._xapi
await asyncMap(Object.entries(snapshotsPerSchedule), ([scheduleId, snapshots]) => {
await pEach(Object.entries(snapshotsPerSchedule), ([scheduleId, snapshots]) => {
const settings = {
...baseSettings,
...allSettings[scheduleId],
...allSettings[this.vm.uuid],
}
return asyncMap(getOldEntries(settings.snapshotRetention, snapshots), ({ $ref }) => {
return pEach(getOldEntries(settings.snapshotRetention, snapshots), ({ $ref }) => {
if ($ref !== baseVmRef) {
return xapi.VM_destroy($ref)
}
@@ -360,7 +367,7 @@ class VmBackup {
baseVm = await xapi.getRecord('VM', baseVm.$ref)
const baseUuidToSrcVdi = new Map()
await asyncMap(await baseVm.$getDisks(), async baseRef => {
await pEach(await baseVm.$getDisks(), async baseRef => {
const [baseUuid, snapshotOf] = await Promise.all([
xapi.getField('VDI', baseRef, 'uuid'),
xapi.getField('VDI', baseRef, 'snapshot_of'),

View File

@@ -38,7 +38,7 @@ exports.XoMetadataBackup = class XoMetadataBackup {
)
const metaDataFileName = `${dir}/metadata.json`
await asyncMap(
await pEach(
Object.entries(this._remoteAdapters),
([remoteId, adapter]) =>
Task.run(

View File

@@ -4,11 +4,12 @@ const assert = require('assert')
const sum = require('lodash/sum')
const { asyncMap } = require('@xen-orchestra/async-map')
const { Constants, mergeVhd, openVhd, VhdAbstract, VhdFile } = require('vhd-lib')
const { isVhdAlias, resolveVhdAlias } = require('vhd-lib/aliases')
const { dirname, resolve } = require('path')
const { DISK_TYPES } = Constants
const { isMetadataFile, isVhdFile, isXvaFile, isXvaSumFile } = require('./_backupType.js')
const { isVhdAlias, resolveVhdAlias } = require('vhd-lib/aliases')
const { limitConcurrency } = require('limit-concurrency-decorator')
const { pEach } = require('@vates/async-each')
const { Task } = require('./Task.js')
const { Disposable } = require('promise-toolbox')
@@ -47,42 +48,32 @@ const computeVhdsSize = (handler, vhdPaths) =>
// | |
// \___________rename_____________/
async function mergeVhdChain(chain, { handler, onLog, remove, merge }) {
async function mergeVhdChain(chain, { handler, logInfo, remove, merge }) {
assert(chain.length >= 2)
const chainCopy = [...chain]
const parent = chainCopy.pop()
const children = chainCopy
if (merge) {
onLog(`merging ${children.length} children into ${parent}`)
logInfo(`merging children into parent`, { childrenCount: children.length, parent })
let done, total
const handle = setInterval(() => {
if (done !== undefined) {
onLog(`merging ${children.join(',')} into ${parent}: ${done}/${total}`)
logInfo(`merging children in progress`, { children, parent, doneCount: done, totalCount: total })
}
}, 10e3)
const mergedSize = await mergeVhd(handler, parent, handler, children, {
logInfo,
onProgress({ done: d, total: t }) {
done = d
total = t
},
remove,
})
clearInterval(handle)
const mergeTargetChild = children.shift()
await Promise.all([
VhdAbstract.rename(handler, parent, mergeTargetChild),
asyncMap(children, child => {
onLog(`the VHD ${child} is already merged`)
if (remove) {
onLog(`deleting merged VHD ${child}`)
return VhdAbstract.unlink(handler, child)
}
}),
])
return mergedSize
}
}
@@ -95,13 +86,13 @@ const listVhds = async (handler, vmDir) => {
const aliases = {}
const interruptedVhds = new Map()
await asyncMap(
await pEach(
await handler.list(`${vmDir}/vdis`, {
ignoreMissing: true,
prependDir: true,
}),
async jobDir =>
asyncMap(
pEach(
await handler.list(jobDir, {
prependDir: true,
}),
@@ -125,14 +116,19 @@ const listVhds = async (handler, vmDir) => {
return { vhds, interruptedVhds, aliases }
}
async function checkAliases(aliasPaths, targetDataRepository, { handler, onLog = noop, remove = false }) {
async function checkAliases(
aliasPaths,
targetDataRepository,
{ handler, logInfo = noop, logWarn = console.warn, 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}`)
logWarn('alias references non VHD target', { path, target })
if (remove) {
logInfo('removing alias and non VHD target', { path, target })
await handler.unlink(target)
await handler.unlink(path)
}
@@ -147,13 +143,13 @@ async function checkAliases(aliasPaths, targetDataRepository, { handler, onLog =
// error during dispose should not trigger a deletion
}
} catch (error) {
onLog(`target ${target} of alias ${path} is missing or broken`, { error })
logWarn('missing or broken alias target', { target, path, 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 })
} catch (error) {
if (error.code !== 'ENOENT') {
logWarn('error deleting alias target', { target, path, error })
}
}
}
@@ -170,20 +166,22 @@ async function checkAliases(aliasPaths, targetDataRepository, { handler, onLog =
entries.forEach(async entry => {
if (!aliasFound.includes(entry)) {
onLog(`the Vhd ${entry} is not referenced by a an alias`)
logWarn('no alias references VHD', { entry })
if (remove) {
logInfo('deleting unaliased VHD')
await VhdAbstract.unlink(handler, entry)
}
}
})
}
exports.checkAliases = checkAliases
const defaultMergeLimiter = limitConcurrency(1)
exports.cleanVm = async function cleanVm(
vmDir,
{ fixMetadata, remove, merge, mergeLimiter = defaultMergeLimiter, onLog = noop }
{ fixMetadata, remove, merge, mergeLimiter = defaultMergeLimiter, logInfo = noop, logWarn = console.warn }
) {
const limitedMergeVhdChain = mergeLimiter(mergeVhdChain)
@@ -196,7 +194,7 @@ exports.cleanVm = async function cleanVm(
const { vhds, interruptedVhds, aliases } = await listVhds(handler, vmDir)
// remove broken VHDs
await asyncMap(vhds, async path => {
await pEach(vhds, async path => {
try {
await Disposable.use(openVhd(handler, path, { checkSecondFooter: !interruptedVhds.has(path) }), vhd => {
if (vhd.footer.diskType === DISK_TYPES.DIFFERENCING) {
@@ -214,9 +212,9 @@ exports.cleanVm = async function cleanVm(
})
} catch (error) {
vhds.delete(path)
onLog(`error while checking the VHD with path ${path}`, { error })
logWarn('VHD check error', { path, error })
if (error?.code === 'ERR_ASSERTION' && remove) {
onLog(`deleting broken ${path}`)
logInfo('deleting broken path', { path })
return VhdAbstract.unlink(handler, path)
}
}
@@ -228,12 +226,12 @@ exports.cleanVm = async function cleanVm(
const statePath = interruptedVhds.get(interruptedVhd)
interruptedVhds.delete(interruptedVhd)
onLog('orphan merge state', {
logWarn('orphan merge state', {
mergeStatePath: statePath,
missingVhdPath: interruptedVhd,
})
if (remove) {
onLog(`deleting orphan merge state ${statePath}`)
logInfo('deleting orphan merge state', { statePath })
await handler.unlink(statePath)
}
}
@@ -241,8 +239,8 @@ exports.cleanVm = async function cleanVm(
// 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 })
await pEach(Object.keys(aliases), async dir => {
await checkAliases(aliases[dir], `${dir}/data`, { handler, logInfo, logWarn, remove })
})
// remove VHDs with missing ancestors
@@ -264,9 +262,9 @@ exports.cleanVm = async function cleanVm(
if (!vhds.has(parent)) {
vhds.delete(vhdPath)
onLog(`the parent ${parent} of the VHD ${vhdPath} is missing`)
logWarn('parent VHD is missing', { parent, vhdPath })
if (remove) {
onLog(`deleting orphan VHD ${vhdPath}`)
logInfo('deleting orphan VHD', { vhdPath })
deletions.push(VhdAbstract.unlink(handler, vhdPath))
}
}
@@ -299,11 +297,11 @@ exports.cleanVm = async function cleanVm(
}
})
await asyncMap(xvas, async path => {
await pEach(xvas, async path => {
// check is not good enough to delete the file, the best we can do is report
// it
if (!(await this.isValidXva(path))) {
onLog(`the XVA with path ${path} is potentially broken`)
logWarn('XVA might be broken', { path })
}
})
@@ -312,12 +310,12 @@ exports.cleanVm = async function cleanVm(
// compile the list of unused XVAs and VHDs, and remove backup metadata which
// reference a missing XVA/VHD
await asyncMap(jsons, async json => {
await pEach(jsons, async json => {
let metadata
try {
metadata = JSON.parse(await handler.readFile(json))
} catch (error) {
onLog(`failed to read metadata file ${json}`, { error })
logWarn('failed to read metadata file', { json, error })
jsons.delete(json)
return
}
@@ -328,9 +326,9 @@ exports.cleanVm = async function cleanVm(
if (xvas.has(linkedXva)) {
unusedXvas.delete(linkedXva)
} else {
onLog(`the XVA linked to the metadata ${json} is missing`)
logWarn('metadata XVA is missing', { json })
if (remove) {
onLog(`deleting incomplete backup ${json}`)
logInfo('deleting incomplete backup', { json })
jsons.delete(json)
await handler.unlink(json)
}
@@ -351,9 +349,9 @@ exports.cleanVm = async function cleanVm(
vhdsToJSons[path] = json
})
} else {
onLog(`Some VHDs linked to the metadata ${json} are missing`, { missingVhds })
logWarn('some metadata VHDs are missing', { json, missingVhds })
if (remove) {
onLog(`deleting incomplete backup ${json}`)
logInfo('deleting incomplete backup', { json })
jsons.delete(json)
await handler.unlink(json)
}
@@ -394,9 +392,9 @@ exports.cleanVm = async function cleanVm(
}
}
onLog(`the VHD ${vhd} is unused`)
logWarn('unused VHD', { vhd })
if (remove) {
onLog(`deleting unused VHD ${vhd}`)
logInfo('deleting unused VHD', { vhd })
unusedVhdsDeletion.push(VhdAbstract.unlink(handler, vhd))
}
}
@@ -419,8 +417,8 @@ exports.cleanVm = async function cleanVm(
const metadataWithMergedVhd = {}
const doMerge = async () => {
await asyncMap(toMerge, async chain => {
const merged = await limitedMergeVhdChain(chain, { handler, onLog, remove, merge })
await pEach(toMerge, async chain => {
const merged = await limitedMergeVhdChain(chain, { handler, logInfo, logWarn, remove, merge })
if (merged !== undefined) {
const metadataPath = vhdsToJSons[chain[0]] // all the chain should have the same metada file
metadataWithMergedVhd[metadataPath] = true
@@ -431,19 +429,19 @@ exports.cleanVm = async function cleanVm(
await Promise.all([
...unusedVhdsDeletion,
toMerge.length !== 0 && (merge ? Task.run({ name: 'merge' }, doMerge) : doMerge()),
asyncMap(unusedXvas, path => {
onLog(`the XVA ${path} is unused`)
pEach(unusedXvas, path => {
logWarn('unused XVA', { path })
if (remove) {
onLog(`deleting unused XVA ${path}`)
logInfo('deleting unused XVA', { path })
return handler.unlink(path)
}
}),
asyncMap(xvaSums, path => {
pEach(xvaSums, path => {
// no need to handle checksums for XVAs deleted by the script, they will be handled by `unlink()`
if (!xvas.has(path.slice(0, -'.checksum'.length))) {
onLog(`the XVA checksum ${path} is unused`)
logInfo('unused XVA checksum', { path })
if (remove) {
onLog(`deleting unused XVA checksum ${path}`)
logInfo('deleting unused XVA checksum', { path })
return handler.unlink(path)
}
}
@@ -453,7 +451,7 @@ 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 => {
await pEach(jsons, async metadataPath => {
const metadata = JSON.parse(await handler.readFile(metadataPath))
let fileSystemSize
@@ -477,11 +475,11 @@ exports.cleanVm = async function cleanVm(
// 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}`)
logWarn('incorrect size in metadata', { size: size ?? 'none', fileSystemSize })
}
}
} catch (error) {
onLog(`failed to get size of ${metadataPath}`, { error })
logWarn('failed to get metadata size', { metadataPath, error })
return
}
@@ -491,7 +489,7 @@ exports.cleanVm = async function cleanVm(
try {
await handler.writeFile(metadataPath, JSON.stringify(metadata), { flags: 'w' })
} catch (error) {
onLog(`failed to update size in backup metadata ${metadataPath} after merge`, { error })
logWarn('metadata size update failed', { metadataPath, error })
}
}
})

View File

@@ -5,10 +5,10 @@ const find = require('lodash/find.js')
const groupBy = require('lodash/groupBy.js')
const ignoreErrors = require('promise-toolbox/ignoreErrors')
const omit = require('lodash/omit.js')
const { asyncMap } = require('@xen-orchestra/async-map')
const { CancelToken } = require('promise-toolbox')
const { createVhdStreamWithLength } = require('vhd-lib')
const { defer } = require('golike-defer')
const { pEach } = require('@vates/async-each')
const { cancelableMap } = require('./_cancelableMap.js')
const { Task } = require('./Task.js')
@@ -237,13 +237,13 @@ exports.importDeltaVm = defer(async function importDeltaVm(
$defer.onFailure.call(xapi, 'VM_destroy', vmRef)
// 2. Delete all VBDs which may have been created by the import.
await asyncMap(await xapi.getField('VM', vmRef, 'VBDs'), ref => ignoreErrors.call(xapi.call('VBD.destroy', ref)))
await pEach(await xapi.getField('VM', vmRef, 'VBDs'), ref => ignoreErrors.call(xapi.call('VBD.destroy', ref)))
// 3. Create VDIs & VBDs.
const vbdRecords = deltaVm.vbds
const vbds = groupBy(vbdRecords, 'VDI')
const newVdis = {}
await asyncMap(Object.keys(vdiRecords), async vdiRef => {
await pEach(Object.keys(vdiRecords), async vdiRef => {
const vdi = vdiRecords[vdiRef]
let newVdi
@@ -279,7 +279,7 @@ exports.importDeltaVm = defer(async function importDeltaVm(
const vdiVbds = vbds[vdiRef]
if (vdiVbds !== undefined) {
await asyncMap(Object.values(vdiVbds), vbd =>
await pEach(Object.values(vdiVbds), vbd =>
xapi.VBD_create({
...vbd,
VDI: newVdi.$ref,
@@ -323,7 +323,7 @@ exports.importDeltaVm = defer(async function importDeltaVm(
}),
// Create VIFs.
asyncMap(Object.values(deltaVm.vifs), vif => {
pEach(Object.values(deltaVm.vifs), vif => {
let network = vif.$network$uuid && xapi.getObjectByUuid(vif.$network$uuid, undefined)
if (network === undefined) {

View File

@@ -69,6 +69,8 @@ job.start(data: { mode: Mode, reportWhen: ReportWhen })
├─ task.warning(message: string)
├─ task.start(data: { type: 'VM', id: string })
│ ├─ task.warning(message: string)
| ├─ task.start(message: 'clean-vm')
│ │ └─ task.end
│ ├─ task.start(message: 'snapshot')
│ │ └─ task.end
│ ├─ task.start(message: 'export', data: { type: 'SR' | 'remote', id: string, isFull: boolean })
@@ -89,12 +91,8 @@ job.start(data: { mode: Mode, reportWhen: ReportWhen })
│ │ ├─ 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.start(message: 'clean-vm')
│ │ └─ task.end
│ └─ task.end
└─ job.end

View File

@@ -8,7 +8,7 @@
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"version": "0.23.0",
"version": "0.26.0",
"engines": {
"node": ">=14.6"
},
@@ -22,7 +22,7 @@
"@vates/disposable": "^0.1.1",
"@vates/parse-duration": "^0.1.1",
"@xen-orchestra/async-map": "^0.1.2",
"@xen-orchestra/fs": "^1.0.1",
"@xen-orchestra/fs": "^1.1.0",
"@xen-orchestra/log": "^0.3.0",
"@xen-orchestra/template": "^0.1.0",
"compare-versions": "^4.0.1",
@@ -38,7 +38,7 @@
"promise-toolbox": "^0.21.0",
"proper-lockfile": "^4.1.2",
"uuid": "^8.3.2",
"vhd-lib": "^3.1.0",
"vhd-lib": "^3.3.1",
"yazl": "^2.5.1"
},
"devDependencies": {
@@ -46,7 +46,7 @@
"tmp": "^0.2.1"
},
"peerDependencies": {
"@xen-orchestra/xapi": "^1.0.0"
"@xen-orchestra/xapi": "^1.4.0"
},
"license": "AGPL-3.0-or-later",
"author": {

View File

@@ -33,7 +33,7 @@ exports.DeltaBackupWriter = class DeltaBackupWriter extends MixinBackupWriter(Ab
const backupDir = getVmBackupDir(backup.vm.uuid)
const vdisDir = `${backupDir}/vdis/${backup.job.id}`
await asyncMap(baseUuidToSrcVdi, async ([baseUuid, srcVdi]) => {
await pEach(baseUuidToSrcVdi, async ([baseUuid, srcVdi]) => {
let found = false
try {
const vhds = await handler.list(`${vdisDir}/${srcVdi.uuid}`, {
@@ -41,7 +41,7 @@ exports.DeltaBackupWriter = class DeltaBackupWriter extends MixinBackupWriter(Ab
prependDir: true,
})
const packedBaseUuid = packUuid(baseUuid)
await asyncMap(vhds, async path => {
await pEach(vhds, async path => {
try {
await checkVhdChain(handler, path)
// Warning, this should not be written as found = found || await adapter.isMergeableParent(packedBaseUuid, path)

View File

@@ -25,7 +25,7 @@ exports.DeltaReplicationWriter = class DeltaReplicationWriter extends MixinRepli
const xapi = replicatedVm.$xapi
const replicatedVdis = new Set(
await asyncMap(await replicatedVm.$getDisks(), async vdiRef => {
await pEach(await replicatedVm.$getDisks(), async vdiRef => {
const otherConfig = await xapi.getField('VDI', vdiRef, 'other_config')
return otherConfig[TAG_COPY_SRC]
})
@@ -60,7 +60,10 @@ exports.DeltaReplicationWriter = class DeltaReplicationWriter extends MixinRepli
const { scheduleId, vm } = this._backup
// delete previous interrupted copies
ignoreErrors.call(asyncMapSettled(listReplicatedVms(xapi, scheduleId, undefined, vm.uuid), vm => vm.$destroy))
ignoreErrors.call(
pEach(listReplicatedVms(xapi, scheduleId, undefined, vm.uuid), vm => vm.$destroy),
{ stopOnError: false }
)
this._oldEntries = getOldEntries(settings.copyRetention - 1, listReplicatedVms(xapi, scheduleId, srUuid, vm.uuid))
@@ -76,7 +79,7 @@ exports.DeltaReplicationWriter = class DeltaReplicationWriter extends MixinRepli
}
async _deleteOldEntries() {
return asyncMapSettled(this._oldEntries, vm => vm.$destroy())
return pEach(this._oldEntries, vm => vm.$destroy(), { stopOnError: false })
}
async _transfer({ timestamp, deltaExport, sizeContainers }) {
@@ -108,7 +111,7 @@ exports.DeltaReplicationWriter = class DeltaReplicationWriter extends MixinRepli
targetVm.ha_restart_priority !== '' &&
Promise.all([targetVm.set_ha_restart_priority(''), targetVm.add_tags('HA disabled')]),
targetVm.set_name_label(`${vm.name_label} - ${job.name} - (${formatFilenameDate(timestamp)})`),
asyncMap(['start', 'start_on'], op =>
pEach(['start', 'start_on'], op =>
targetVm.update_blocked_operations(
op,
'Start operation for this vm is blocked, clone it if you want to use it.'

View File

@@ -40,12 +40,14 @@ exports.FullReplicationWriter = class FullReplicationWriter extends MixinReplica
// delete previous interrupted copies
ignoreErrors.call(
asyncMapSettled(listReplicatedVms(xapi, scheduleId, undefined, vm.uuid), vm => xapi.VM_destroy(vm.$ref))
pEach(listReplicatedVms(xapi, scheduleId, undefined, vm.uuid), vm => xapi.VM_destroy(vm.$ref), {
stopOnError: false,
})
)
const oldVms = getOldEntries(settings.copyRetention - 1, listReplicatedVms(xapi, scheduleId, srUuid, vm.uuid))
const deleteOldBackups = () => asyncMapSettled(oldVms, vm => xapi.VM_destroy(vm.$ref))
const deleteOldBackups = () => pEach(oldVms, vm => xapi.VM_destroy(vm.$ref), { stopOnError: false })
const { deleteFirst } = settings
if (deleteFirst) {
await deleteOldBackups()
@@ -66,7 +68,7 @@ exports.FullReplicationWriter = class FullReplicationWriter extends MixinReplica
const targetVm = await xapi.getRecord('VM', targetVmRef)
await Promise.all([
asyncMap(['start', 'start_on'], op =>
pEach(['start', 'start_on'], op =>
targetVm.update_blocked_operations(
op,
'Start operation for this vm is blocked, clone it if you want to use it.'

View File

@@ -6,8 +6,9 @@ const { join } = require('path')
const { getVmBackupDir } = require('../_getVmBackupDir.js')
const MergeWorker = require('../merge-worker/index.js')
const { formatFilenameDate } = require('../_filenameDate.js')
const { Task } = require('../Task.js')
const { warn } = createLogger('xo:backups:MixinBackupWriter')
const { info, warn } = createLogger('xo:backups:MixinBackupWriter')
exports.MixinBackupWriter = (BaseClass = Object) =>
class MixinBackupWriter extends BaseClass {
@@ -25,11 +26,17 @@ exports.MixinBackupWriter = (BaseClass = Object) =>
async _cleanVm(options) {
try {
return await this._adapter.cleanVm(this.#vmBackupDir, {
...options,
fixMetadata: true,
onLog: warn,
lock: false,
return await Task.run({ name: 'clean-vm' }, () => {
return this._adapter.cleanVm(this.#vmBackupDir, {
...options,
fixMetadata: true,
logInfo: info,
logWarn: (message, data) => {
warn(message, data)
Task.warning(message, data)
},
lock: false,
})
})
} catch (error) {
warn(error)

View File

@@ -18,7 +18,7 @@
"preferGlobal": true,
"dependencies": {
"golike-defer": "^0.5.1",
"xen-api": "^1.2.0"
"xen-api": "^1.2.1"
},
"scripts": {
"postversion": "npm publish"

View File

@@ -1,7 +1,7 @@
{
"private": false,
"name": "@xen-orchestra/emit-async",
"version": "0.1.0",
"version": "1.0.0",
"license": "ISC",
"description": "Emit an event for async listeners to settle",
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/emit-async",

View File

@@ -1,7 +1,7 @@
{
"private": false,
"name": "@xen-orchestra/fs",
"version": "1.0.1",
"version": "1.1.0",
"license": "AGPL-3.0-or-later",
"description": "The File System for Xen Orchestra backups.",
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/fs",
@@ -42,7 +42,7 @@
"proper-lockfile": "^4.1.2",
"readable-stream": "^3.0.6",
"through2": "^4.0.2",
"xo-remote-parser": "^0.8.0"
"xo-remote-parser": "^0.9.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0",

View File

@@ -1,6 +1,7 @@
import asyncMapSettled from '@xen-orchestra/async-map/legacy'
import getStream from 'get-stream'
import { coalesceCalls } from '@vates/coalesce-calls'
import { createLogger } from '@xen-orchestra/log'
import { fromCallback, fromEvent, ignoreErrors, timeout } from 'promise-toolbox'
import { limitConcurrency } from 'limit-concurrency-decorator'
import { parse } from 'xo-remote-parser'
@@ -11,6 +12,8 @@ import { synchronized } from 'decorator-synchronized'
import { basename, dirname, normalize as normalizePath } from './_path'
import { createChecksumStream, validChecksumOfReadStream } from './checksum'
const { warn } = createLogger('@xen-orchestra:fs')
const checksumFile = file => file + '.checksum'
const computeRate = (hrtime, size) => {
const seconds = hrtime[0] + hrtime[1] / 1e9
@@ -357,11 +360,12 @@ export default class RemoteHandlerAbstract {
readRate: computeRate(readDuration, SIZE),
}
} catch (error) {
warn(`error while testing the remote at step ${step}`, { error })
return {
success: false,
step,
file: testFileName,
error: error.message || String(error),
error,
}
} finally {
ignoreErrors.call(this._unlink(testFileName))
@@ -420,6 +424,10 @@ export default class RemoteHandlerAbstract {
// Methods that can be implemented by inheriting classes
useVhdDirectory() {
return this._remote.useVhdDirectory ?? false
}
async _closeFile(fd) {
throw new Error('Not implemented')
}
@@ -546,15 +554,18 @@ export default class RemoteHandlerAbstract {
}
const files = await this._list(dir)
await asyncMapSettled(files, file =>
this._unlink(`${dir}/${file}`).catch(error => {
// Unlink dir behavior is not consistent across platforms
// https://github.com/nodejs/node-v0.x-archive/issues/5791
if (error.code === 'EISDIR' || error.code === 'EPERM') {
return this._rmtree(`${dir}/${file}`)
}
throw error
})
await pEach(files, file =>
this._unlink(`${dir}/${file}`).catch(
error => {
// Unlink dir behavior is not consistent across platforms
// https://github.com/nodejs/node-v0.x-archive/issues/5791
if (error.code === 'EISDIR' || error.code === 'EPERM') {
return this._rmtree(`${dir}/${file}`)
}
throw error
},
{ stopOnError: false }
)
)
return this._rmtree(dir)
}

View File

@@ -28,7 +28,7 @@ import createBufferFromStream from './_createBufferFromStream.js'
import guessAwsRegion from './_guessAwsRegion.js'
import RemoteHandlerAbstract from './abstract'
import { basename, join, split } from './_path'
import { asyncEach } from '@vates/async-each'
import { pEach } from '@vates/async-each'
// endpoints https://docs.aws.amazon.com/general/latest/gr/s3.html
@@ -369,7 +369,7 @@ export default class S3Handler extends RemoteHandlerAbstract {
)
NextContinuationToken = result.IsTruncated ? result.NextContinuationToken : undefined
await asyncEach(
await pEach(
result.Contents ?? [],
async ({ Key }) => {
// _unlink will add the prefix, but Key contains everything
@@ -525,4 +525,8 @@ export default class S3Handler extends RemoteHandlerAbstract {
}
async _closeFile(fd) {}
useVhdDirectory() {
return true
}
}

View File

@@ -14,14 +14,14 @@
"url": "https://vates.fr"
},
"license": "AGPL-3.0-or-later",
"version": "0.4.0",
"version": "0.5.0",
"engines": {
"node": ">=12"
},
"dependencies": {
"@vates/event-listeners-manager": "^1.0.0",
"@vates/event-listeners-manager": "^1.0.1",
"@vates/parse-duration": "^0.1.1",
"@xen-orchestra/emit-async": "^0.1.0",
"@xen-orchestra/emit-async": "^1.0.0",
"@xen-orchestra/log": "^0.3.0",
"app-conf": "^2.1.0",
"lodash": "^4.17.21",

View File

@@ -9,7 +9,7 @@
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"version": "0.1.1",
"version": "0.1.2",
"engines": {
"node": ">=8.10"
},
@@ -30,7 +30,7 @@
"rimraf": "^3.0.0"
},
"dependencies": {
"@vates/read-chunk": "^0.1.2"
"@vates/read-chunk": "^1.0.0"
},
"author": {
"name": "Vates SAS",

View File

@@ -33,26 +33,19 @@ async function main(argv) {
ignoreUnknownFormats: true,
})
const { hostname = 'localhost', port } = config?.http?.listen?.https ?? {}
const {
_: args,
file,
help,
host,
raw,
token,
} = getopts(argv, {
const opts = getopts(argv, {
alias: { file: 'f', help: 'h' },
boolean: ['help', 'raw'],
default: {
token: config.authenticationToken,
},
stopEarly: true,
string: ['file', 'host', 'token'],
string: ['file', 'host', 'token', 'url'],
})
if (help || (file === '' && args.length === 0)) {
const { _: args, file } = opts
if (opts.help || (file === '' && args.length === 0)) {
return console.log(
'%s',
`Usage:
@@ -77,18 +70,29 @@ ${pkg.name} v${pkg.version}`
const baseRequest = {
headers: {
'content-type': 'application/json',
cookie: `authenticationToken=${token}`,
},
pathname: '/api/v1',
protocol: 'https:',
rejectUnauthorized: false,
}
if (host !== '') {
baseRequest.host = host
let { token } = opts
if (opts.url !== '') {
const { protocol, host, username } = new URL(opts.url)
Object.assign(baseRequest, { protocol, host })
if (username !== '') {
token = username
}
} else {
baseRequest.hostname = hostname
baseRequest.port = port
baseRequest.protocol = 'https:'
if (opts.host !== '') {
baseRequest.host = opts.host
} else {
const { hostname = 'localhost', port } = config?.http?.listen?.https ?? {}
baseRequest.hostname = hostname
baseRequest.port = port
}
}
baseRequest.headers.cookie = `authenticationToken=${token}`
const call = async ({ method, params }) => {
if (callPath.length !== 0) {
process.stderr.write(`\n${colors.bold(`--- call #${callPath.join('.')}`)} ---\n\n`)
@@ -127,7 +131,7 @@ ${pkg.name} v${pkg.version}`
stdout.write(inspect(JSON.parse(line), { colors: true, depth: null }))
stdout.write('\n')
}
} else if (raw && typeof result === 'string') {
} else if (opts.raw && typeof result === 'string') {
stdout.write(result)
} else {
stdout.write(inspect(result, { colors: true, depth: null }))

View File

@@ -1,7 +1,7 @@
{
"private": false,
"name": "@xen-orchestra/proxy-cli",
"version": "0.2.0",
"version": "0.3.1",
"license": "AGPL-3.0-or-later",
"description": "CLI for @xen-orchestra/proxy",
"keywords": [
@@ -26,7 +26,7 @@
},
"dependencies": {
"@iarna/toml": "^2.2.0",
"@vates/read-chunk": "^0.1.2",
"@vates/read-chunk": "^1.0.0",
"ansi-colors": "^4.1.1",
"app-conf": "^2.1.0",
"content-type": "^1.0.4",

View File

@@ -250,7 +250,7 @@ export default class Backups {
listPoolMetadataBackups: [
async ({ remotes }) => {
const backupsByRemote = {}
await asyncMap(Object.entries(remotes), async ([remoteId, remote]) => {
await pEach(Object.entries(remotes), async ([remoteId, remote]) => {
try {
await Disposable.use(this.getAdapter(remote), async adapter => {
backupsByRemote[remoteId] = await adapter.listPoolMetadataBackups()
@@ -274,7 +274,7 @@ export default class Backups {
listVmBackups: [
async ({ remotes }) => {
const backups = {}
await asyncMap(Object.keys(remotes), async remoteId => {
await pEach(Object.keys(remotes), async remoteId => {
try {
await Disposable.use(this.getAdapter(remotes[remoteId]), async adapter => {
backups[remoteId] = formatVmBackups(await adapter.listAllVmBackups())
@@ -304,7 +304,7 @@ export default class Backups {
listXoMetadataBackups: [
async ({ remotes }) => {
const backupsByRemote = {}
await asyncMap(Object.entries(remotes), async ([remoteId, remote]) => {
await pEach(Object.entries(remotes), async ([remoteId, remote]) => {
try {
await Disposable.use(this.getAdapter(remote), async adapter => {
backupsByRemote[remoteId] = await adapter.listXoMetadataBackups()

View File

@@ -1,7 +1,7 @@
{
"private": true,
"name": "@xen-orchestra/proxy",
"version": "0.22.1",
"version": "0.23.4",
"license": "AGPL-3.0-or-later",
"description": "XO Proxy used to remotely execute backup jobs",
"keywords": [
@@ -32,13 +32,13 @@
"@vates/decorate-with": "^2.0.0",
"@vates/disposable": "^0.1.1",
"@xen-orchestra/async-map": "^0.1.2",
"@xen-orchestra/backups": "^0.23.0",
"@xen-orchestra/fs": "^1.0.1",
"@xen-orchestra/backups": "^0.26.0",
"@xen-orchestra/fs": "^1.1.0",
"@xen-orchestra/log": "^0.3.0",
"@xen-orchestra/mixin": "^0.1.0",
"@xen-orchestra/mixins": "^0.4.0",
"@xen-orchestra/self-signed": "^0.1.0",
"@xen-orchestra/xapi": "^1.0.0",
"@xen-orchestra/mixins": "^0.5.0",
"@xen-orchestra/self-signed": "^0.1.3",
"@xen-orchestra/xapi": "^1.4.0",
"ajv": "^8.0.3",
"app-conf": "^2.1.0",
"async-iterator-to-stream": "^1.1.0",
@@ -46,7 +46,7 @@
"get-stream": "^6.0.0",
"getopts": "^2.2.3",
"golike-defer": "^0.5.1",
"http-server-plus": "^0.11.0",
"http-server-plus": "^0.11.1",
"http2-proxy": "^5.0.53",
"json-rpc-protocol": "^0.13.1",
"jsonrpc-websocket-client": "^0.7.2",
@@ -60,7 +60,7 @@
"source-map-support": "^0.5.16",
"stoppable": "^1.0.6",
"xdg-basedir": "^5.1.0",
"xen-api": "^1.2.0",
"xen-api": "^1.2.1",
"xo-common": "^0.8.0"
},
"devDependencies": {

View File

@@ -9,7 +9,7 @@
"type": "git",
"url": "https://github.com/vatesfr/xen-orchestra.git"
},
"version": "0.1.0",
"version": "0.1.3",
"engines": {
"node": ">=8.10"
},

View File

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

View File

@@ -1 +0,0 @@
../../scripts/babel-eslintrc.js

View File

@@ -1,8 +1,10 @@
import escapeRegExp from 'lodash/escapeRegExp'
'use strict'
const escapeRegExp = require('lodash/escapeRegExp')
const compareLengthDesc = (a, b) => b.length - a.length
export function compileTemplate(pattern, rules) {
exports.compileTemplate = function compileTemplate(pattern, rules) {
const matches = Object.keys(rules).sort(compareLengthDesc).map(escapeRegExp).join('|')
const regExp = new RegExp(`\\\\(?:\\\\|${matches})|${matches}`, 'g')
return (...params) =>

View File

@@ -1,5 +1,8 @@
/* eslint-env jest */
import { compileTemplate } from '.'
'use strict'
const { compileTemplate } = require('.')
it("correctly replaces the template's variables", () => {
const replacer = compileTemplate('{property}_\\{property}_\\\\{property}_{constant}_%_FOO', {

View File

@@ -14,31 +14,13 @@
"name": "Vates SAS",
"url": "https://vates.fr"
},
"preferGlobal": false,
"main": "dist/",
"browserslist": [
">2%"
],
"engines": {
"node": ">=6"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"cross-env": "^7.0.2",
"rimraf": "^3.0.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
"clean": "rimraf dist/",
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
"prebuild": "yarn run clean",
"predev": "yarn run prebuild",
"prepublishOnly": "yarn run build",
"postversion": "npm publish --access public"
},
"dependencies": {
"lodash": "^4.17.15"
},
"scripts": {
"postversion": "npm publish --access public"
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "@xen-orchestra/upload-ova",
"version": "0.1.4",
"version": "0.1.5",
"license": "AGPL-3.0-or-later",
"description": "Basic CLI to upload ova files to Xen-Orchestra",
"keywords": [
@@ -43,7 +43,7 @@
"pw": "^0.0.4",
"xdg-basedir": "^4.0.0",
"xo-lib": "^0.11.1",
"xo-vmdk-to-vhd": "^2.3.0"
"xo-vmdk-to-vhd": "^2.4.2"
},
"devDependencies": {
"@babel/cli": "^7.0.0",

View File

@@ -0,0 +1,9 @@
'use strict'
// TODO: remove when Node >=15.0
module.exports = class AggregateError extends Error {
constructor(errors, message) {
super(message)
this.errors = errors
}
}

View File

@@ -230,8 +230,9 @@ function mixin(mixins) {
defineProperties(xapiProto, descriptors)
}
mixin({
task: require('./task.js'),
host: require('./host.js'),
SR: require('./sr.js'),
task: require('./task.js'),
VBD: require('./vbd.js'),
VDI: require('./vdi.js'),
VIF: require('./vif.js'),

View File

@@ -1,6 +1,6 @@
{
"name": "@xen-orchestra/xapi",
"version": "1.0.0",
"version": "1.4.0",
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@xen-orchestra/xapi",
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
"repository": {
@@ -15,7 +15,7 @@
"node": ">=14"
},
"peerDependencies": {
"xen-api": "^1.2.0"
"xen-api": "^1.2.1"
},
"scripts": {
"postversion": "npm publish --access public"
@@ -26,8 +26,10 @@
"@xen-orchestra/log": "^0.3.0",
"d3-time-format": "^3.0.0",
"golike-defer": "^0.5.1",
"json-rpc-protocol": "^0.13.2",
"lodash": "^4.17.15",
"promise-toolbox": "^0.21.0",
"vhd-lib": "^3.3.1",
"xo-common": "^0.8.0"
},
"private": false,

172
@xen-orchestra/xapi/sr.js Normal file
View File

@@ -0,0 +1,172 @@
'use strict'
const { asyncMap, asyncMapSettled } = require('@xen-orchestra/async-map')
const { decorateClass } = require('@vates/decorate-with')
const { defer } = require('golike-defer')
const { incorrectState } = require('xo-common/api-errors')
const { VDI_FORMAT_RAW } = require('./index.js')
const peekFooterFromStream = require('vhd-lib/peekFooterFromVhdStream')
const AggregateError = require('./_AggregateError.js')
const { warn } = require('@xen-orchestra/log').createLogger('xo:xapi:sr')
const OC_MAINTENANCE = 'xo:maintenanceState'
class Sr {
async create({
content_type = 'user', // recommended by Citrix
device_config,
host,
name_description = '',
name_label,
physical_size = 0,
shared,
sm_config = {},
type,
}) {
const ref = await this.call(
'SR.create',
host,
device_config,
physical_size,
name_label,
name_description,
type,
content_type,
shared,
sm_config
)
// https://developer-docs.citrix.com/projects/citrix-hypervisor-sdk/en/latest/xc-api-extensions/#sr
this.setFieldEntry('SR', ref, 'other_config', 'auto-scan', 'true').catch(warn)
return ref
}
// Switch the SR to maintenance mode:
// - shutdown all running VMs with a VDI on this SR
// - their UUID is saved into SR.other_config[OC_MAINTENANCE].shutdownVms
// - clean shutdown is attempted, and falls back to a hard shutdown
// - unplug all connected hosts from this SR
async enableMaintenanceMode($defer, ref, { vmsToShutdown = [] } = {}) {
const state = { timestamp: Date.now() }
// will throw if already in maintenance mode
await this.call('SR.add_to_other_config', ref, OC_MAINTENANCE, JSON.stringify(state))
await $defer.onFailure.call(this, 'call', 'SR.remove_from_other_config', ref, OC_MAINTENANCE)
const runningVms = new Map()
const handleVbd = async ref => {
const vmRef = await this.getField('VBD', ref, 'VM')
if (!runningVms.has(vmRef)) {
const power_state = await this.getField('VM', vmRef, 'power_state')
const isPaused = power_state === 'Paused'
if (isPaused || power_state === 'Running') {
runningVms.set(vmRef, isPaused)
}
}
}
await pEach(await this.getField('SR', ref, 'VDIs'), async ref => {
await pEach(await this.getField('VDI', ref, 'VBDs'), handleVbd)
})
{
const runningVmUuids = await pEach(runningVms.keys(), ref => this.getField('VM', ref, 'uuid'))
const set = new Set(vmsToShutdown)
for (const vmUuid of runningVmUuids) {
if (!set.has(vmUuid)) {
throw incorrectState({
actual: vmsToShutdown,
expected: runningVmUuids,
property: 'vmsToShutdown',
})
}
}
}
state.shutdownVms = {}
await pEach(
runningVms,
async ([ref, isPaused]) => {
state.shutdownVms[await this.getField('VM', ref, 'uuid')] = isPaused
try {
await this.callAsync('VM.clean_shutdown', ref)
} catch (error) {
warn('SR_enableMaintenanceMode, VM clean shutdown', { error })
await this.callAsync('VM.hard_shutdown', ref)
}
$defer.onFailure.call(this, 'callAsync', 'VM.start', ref, isPaused, true)
},
{ stopOnError: false }
)
state.unpluggedPbds = []
await pEach(
await this.getField('SR', ref, 'PBDs'),
async ref => {
if (await this.getField('PBD', ref, 'currently_attached')) {
state.unpluggedPbds.push(await this.getField('PBD', ref, 'uuid'))
await this.callAsync('PBD.unplug', ref)
$defer.onFailure.call(this, 'callAsync', 'PBD.plug', ref)
}
},
{ stopOnError: false }
)
await this.setFieldEntry('SR', ref, 'other_config', OC_MAINTENANCE, JSON.stringify(state))
}
// this method is best effort and will not stop on first error
async disableMaintenanceMode(ref) {
const state = JSON.parse((await this.getField('SR', ref, 'other_config'))[OC_MAINTENANCE])
// will throw if not in maintenance mode
await this.call('SR.remove_from_other_config', ref, OC_MAINTENANCE)
const errors = []
await pEach(state.unpluggedPbds, async uuid => {
try {
await this.callAsync('PBD.plug', await this.call('PBD.get_by_uuid', uuid))
} catch (error) {
errors.push(error)
}
})
await pEach(Object.entries(state.shutdownVms), async ([uuid, isPaused]) => {
try {
await this.callAsync('VM.start', await this.call('VM.get_by_uuid', uuid), isPaused, true)
} catch (error) {
errors.push(error)
}
})
if (errors.length !== 0) {
throw new AggregateError(errors)
}
}
async importVdi(
$defer,
ref,
stream,
{ name_label = '[XO] Imported disk - ' + new Date().toISOString(), ...vdiCreateOpts } = {}
) {
const footer = await peekFooterFromStream(stream)
const vdiRef = await this.VDI_create({ ...vdiCreateOpts, name_label, SR: ref, virtual_size: footer.currentSize })
$defer.onFailure.call(this, 'callAsync', 'VDI.destroy', vdiRef)
await this.VDI_importContent(vdiRef, stream, { format: VDI_FORMAT_RAW })
return vdiRef
}
}
module.exports = Sr
decorateClass(Sr, { enableMaintenanceMode: defer, importVdi: defer })

View File

@@ -6,6 +6,8 @@ const { Ref } = require('xen-api')
const isVmRunning = require('./_isVmRunning.js')
const { warn } = require('@xen-orchestra/log').createLogger('xo:xapi:vbd')
const noop = Function.prototype
module.exports = class Vbd {
@@ -66,8 +68,10 @@ module.exports = class Vbd {
})
if (isVmRunning(powerState)) {
await this.callAsync('VBD.plug', vbdRef)
this.callAsync('VBD.plug', vbdRef).catch(warn)
}
return vbdRef
}
async unplug(ref) {

View File

@@ -30,8 +30,7 @@ class Vdi {
other_config = {},
read_only = false,
sharable = false,
sm_config,
SR,
SR = this.pool.default_SR,
tags,
type = 'user',
virtual_size,
@@ -39,10 +38,10 @@ class Vdi {
},
{
// blindly copying `sm_config` from another VDI can create problems,
// therefore it is ignored by default by this method
// therefore it should be passed explicitly
//
// see https://github.com/vatesfr/xen-orchestra/issues/4482
setSmConfig = false,
sm_config,
} = {}
) {
return this.call('VDI.create', {
@@ -51,7 +50,7 @@ class Vdi {
other_config,
read_only,
sharable,
sm_config: setSmConfig ? sm_config : undefined,
sm_config,
SR,
tags,
type,

View File

@@ -11,7 +11,8 @@ const { asyncMap } = require('@xen-orchestra/async-map')
const { createLogger } = require('@xen-orchestra/log')
const { decorateClass } = require('@vates/decorate-with')
const { defer } = require('golike-defer')
const { incorrectState } = require('xo-common/api-errors.js')
const { incorrectState, forbiddenOperation } = require('xo-common/api-errors.js')
const { JsonRpcError } = require('json-rpc-protocol')
const { Ref } = require('xen-api')
const extractOpaqueRef = require('./_extractOpaqueRef.js')
@@ -47,7 +48,7 @@ const cleanBiosStrings = biosStrings => {
async function listNobakVbds(xapi, vbdRefs) {
const vbds = []
await asyncMap(vbdRefs, async vbdRef => {
await pEach(vbdRefs, async vbdRef => {
const vbd = await xapi.getRecord('VBD', vbdRef)
if (
vbd.type === 'Disk' &&
@@ -151,7 +152,7 @@ class Vm {
if (ignoreNobakVdis) {
if (vm.power_state === 'Halted') {
await asyncMap(await listNobakVbds(this, vm.VBDs), async vbd => {
await pEach(await listNobakVbds(this, vm.VBDs), async vbd => {
await this.VBD_destroy(vbd.$ref)
$defer.call(this, 'VBD_create', vbd)
})
@@ -180,9 +181,7 @@ class Vm {
)
if (destroyNobakVdis) {
await asyncMap(await listNobakVbds(this, await this.getField('VM', ref, 'VBDs')), vbd =>
this.VDI_destroy(vbd.VDI)
)
await pEach(await listNobakVbds(this, await this.getField('VM', ref, 'VBDs')), vbd => this.VDI_destroy(vbd.VDI))
}
return ref
@@ -343,7 +342,13 @@ class Vm {
const vm = await this.getRecord('VM', vmRef)
if (!bypassBlockedOperation && 'destroy' in vm.blocked_operations) {
throw new Error('destroy is blocked')
throw forbiddenOperation(
`destroy is blocked: ${
vm.blocked_operations.destroy === 'true'
? 'protected from accidental deletion'
: vm.blocked_operations.destroy
}`
)
}
if (!forceDeleteDefaultTemplate && isDefaultTemplate(vm)) {
@@ -378,7 +383,7 @@ class Vm {
await this.call('VM.destroy', vmRef)
await Promise.all([
asyncMap(vm.snapshots, snapshotRef =>
pEach(vm.snapshots, snapshotRef =>
this.VM_destroy(snapshotRef).catch(error => {
warn('VM_destroy: failed to destroy snapshot', {
error,
@@ -388,7 +393,7 @@ class Vm {
})
),
deleteDisks &&
asyncMap(disks, async vdiRef => {
pEach(disks, async vdiRef => {
try {
// Dont destroy if attached to other (non control domain) VMs
for (const vbdRef of await this.getField('VDI', vdiRef, 'VBDs')) {
@@ -503,6 +508,22 @@ class Vm {
}
return ref
} catch (error) {
if (
// xxhash is the new form consistency hashing in CH 8.1 which uses a faster,
// more efficient hashing algorithm to generate the consistency checks
// in order to support larger files without the consistency checking process taking an incredibly long time
error.code === 'IMPORT_ERROR' &&
error.params?.some(
param =>
param.includes('INTERNAL_ERROR') &&
param.includes('Expected to find an inline checksum') &&
param.includes('.xxhash')
)
) {
warn('import', { error })
throw new JsonRpcError('Importing this VM requires XCP-ng or Citrix Hypervisor >=8.1')
}
// augment the error with as much relevant info as possible
const [poolMaster, sr] = await Promise.all([
safeGetRecord(this, 'host', this.pool.master),
@@ -525,17 +546,21 @@ class Vm {
// requires the VM to be halted because it's not possible to re-plug VUSB on a live VM
if (unplugVusbs && isHalted) {
await asyncMap(vm.VUSBs, async ref => {
const vusb = await this.getRecord('VUSB', ref)
await vusb.$call('destroy')
$defer.call(this, 'call', 'VUSB.create', vusb.VM, vusb.USB_group, vusb.other_config)
})
// vm.VUSBs can be undefined (e.g. on XS 7.0.0)
const vusbs = vm.VUSBs
if (vusbs !== undefined) {
await pEach(vusbs, async ref => {
const vusb = await this.getRecord('VUSB', ref)
await vusb.$call('destroy')
$defer.call(this, 'call', 'VUSB.create', vusb.VM, vusb.USB_group, vusb.other_config)
})
}
}
let destroyNobakVdis = false
if (ignoreNobakVdis) {
if (isHalted) {
await asyncMap(await listNobakVbds(this, vm.VBDs), async vbd => {
await pEach(await listNobakVbds(this, vm.VBDs), async vbd => {
await this.VBD_destroy(vbd.$ref)
$defer.call(this, 'VBD_create', vbd)
})
@@ -632,9 +657,7 @@ class Vm {
)
if (destroyNobakVdis) {
await asyncMap(await listNobakVbds(this, await this.getField('VM', ref, 'VBDs')), vbd =>
this.VDI_destroy(vbd.VDI)
)
await pEach(await listNobakVbds(this, await this.getField('VM', ref, 'VBDs')), vbd => this.VDI_destroy(vbd.VDI))
}
return ref

View File

@@ -1,5 +1,130 @@
# ChangeLog
## **5.72.0** (2022-06-30)
<img id="latest" src="https://badgen.net/badge/channel/latest/yellow" alt="Channel: latest" />
### Highlights
- [Backup] Merge delta backups without copying data when using VHD directories on NFS/SMB/local remote(https://github.com/vatesfr/xen-orchestra/pull/6271))
- [Proxies] Ability to copy the proxy access URL (PR [#6287](https://github.com/vatesfr/xen-orchestra/pull/6287))
- [SR/Advanced] Ability to enable/disable _Maintenance Mode_ [#6215](https://github.com/vatesfr/xen-orchestra/issues/6215) (PRs [#6308](https://github.com/vatesfr/xen-orchestra/pull/6308), [#6297](https://github.com/vatesfr/xen-orchestra/pull/6297))
- [User] User tokens management through XO interface (PR [#6276](https://github.com/vatesfr/xen-orchestra/pull/6276))
- [Tasks, VM/General] Self Service users: show tasks related to their pools, hosts, SRs, networks and VMs (PR [#6217](https://github.com/vatesfr/xen-orchestra/pull/6217))
### Enhancements
> Users must be able to say: “Nice enhancement, I'm eager to test it”
- [Backup/Restore] Clearer error message when importing a VM backup requires XCP-n/CH >= 8.1 (PR [#6304](https://github.com/vatesfr/xen-orchestra/pull/6304))
- [Backup] Users can use VHD directory on any remote type (PR [#6273](https://github.com/vatesfr/xen-orchestra/pull/6273))
### Bug fixes
> Users must be able to say: “I had this issue, happy to know it's fixed”
- [VDI Import] Fix `this._getOrWaitObject is not a function`
- [VM] Attempting to delete a protected VM should display a modal with the error and the ability to bypass it (PR [#6290](https://github.com/vatesfr/xen-orchestra/pull/6290))
- [OVA Import] Fix import stuck after first disk
- [File restore] Ignore symbolic links
### Released packages
- @vates/event-listeners-manager 1.0.1
- @vates/read-chunk 1.0.0
- @xen-orchestra/backups 0.26.0
- @xen-orchestra/backups-cli 0.7.4
- xo-remote-parser 0.9.1
- @xen-orchestra/fs 1.1.0
- @xen-orchestra/openflow 0.1.2
- @xen-orchestra/xapi 1.4.0
- @xen-orchestra/proxy 0.23.4
- @xen-orchestra/proxy-cli 0.3.1
- vhd-lib 3.3.1
- vhd-cli 0.8.0
- xo-vmdk-to-vhd 2.4.2
- xo-server 5.98.0
- xo-web 5.99.0
## **5.71.1 (2022-06-13)**
<img id="stable" src="https://badgen.net/badge/channel/stable/green" alt="Channel: stable" />
### Enhancements
- Show raw errors to administrators instead of _unknown error from the peer_ (PR [#6260](https://github.com/vatesfr/xen-orchestra/pull/6260))
### Bug fixes
- [New SR] Fix `method.startsWith is not a function` when creating an _ext_ SR
- Import VDI content now works when there is a HTTP proxy between XO and the host (PR [#6261](https://github.com/vatesfr/xen-orchestra/pull/6261))
- [Backup] Fix `undefined is not iterable (cannot read property Symbol(Symbol.iterator))` on XS 7.0.0
- [Backup] Ensure a warning is shown if a target preparation step fails (PR [#6266](https://github.com/vatesfr/xen-orchestra/pull/6266))
- [OVA Export] Avoid creating a zombie task (PR [#6267](https://github.com/vatesfr/xen-orchestra/pull/6267))
- [OVA Export] Increase speed by lowering compression to acceptable level (PR [#6267](https://github.com/vatesfr/xen-orchestra/pull/6267))
- [OVA Export] Fix broken OVAs due to special characters in VM name (PR [#6267](https://github.com/vatesfr/xen-orchestra/pull/6267))
### Released packages
- @xen-orchestra/backups 0.25.0
- @xen-orchestra/backups-cli 0.7.3
- xen-api 1.2.1
- @xen-orchestra/xapi 1.2.0
- @xen-orchestra/proxy 0.23.2
- @xen-orchestra/proxy-cli 0.3.0
- xo-cli 0.14.0
- xo-vmdk-to-vhd 2.4.1
- xo-server 5.96.0
- xo-web 5.97.2
## **5.71.0 (2022-05-31)**
### Highlights
- [Backup] _Restore Health Check_ can now be configured to be run automatically during a backup schedule (PRs [#6227](https://github.com/vatesfr/xen-orchestra/pull/6227), [#6228](https://github.com/vatesfr/xen-orchestra/pull/6228), [#6238](https://github.com/vatesfr/xen-orchestra/pull/6238) & [#6242](https://github.com/vatesfr/xen-orchestra/pull/6242))
- [Backup] VMs with USB Pass-through devices are now supported! The advanced _Offline Snapshot Mode_ setting must be enabled. For Full Backup or Disaster Recovery jobs, Rolling Snapshot needs to be anabled as well. (PR [#6239](https://github.com/vatesfr/xen-orchestra/pull/6239))
- [Backup] Implement file cache for listing the backups of a VM (PR [#6220](https://github.com/vatesfr/xen-orchestra/pull/6220))
- [RPU/Host] If some backup jobs are running on the pool, ask for confirmation before starting an RPU, shutdown/rebooting a host or restarting a host's toolstack (PR [6232](https://github.com/vatesfr/xen-orchestra/pull/6232))
- [XO Web] Add ability to configure a default filter for Storage [#6236](https://github.com/vatesfr/xen-orchestra/issues/6236) (PR [#6237](https://github.com/vatesfr/xen-orchestra/pull/6237))
- [REST API] Support VDI creation via VHD import
### Enhancements
- [Backup] Merge multiple VHDs at once which will speed up the merging phase after reducing the retention of a backup job(PR [#6184](https://github.com/vatesfr/xen-orchestra/pull/6184))
- [Backup] Add setting `backups.metadata.defaultSettings.unconditionalSnapshot` in `xo-server`'s configuration file to force a snapshot even when not required by the backup, this is useful to avoid locking the VM halted during the backup (PR [#6221](https://github.com/vatesfr/xen-orchestra/pull/6221))
- [VM migration] Ensure the VM can be migrated before performing the migration to avoid issues [#5301](https://github.com/vatesfr/xen-orchestra/issues/5301) (PR [#6245](https://github.com/vatesfr/xen-orchestra/pull/6245))
- [Backup] Show any detected errors on existing backups instead of fixing them silently (PR [#6207](https://github.com/vatesfr/xen-orchestra/pull/6225))
- Created SRs will now have auto-scan enabled similarly to what XenCenter does (PR [#6246](https://github.com/vatesfr/xen-orchestra/pull/6246))
- [RPU] Disable scheduled backup jobs during RPU (PR [#6244](https://github.com/vatesfr/xen-orchestra/pull/6244))
### Bug fixes
- [S3] Fix S3 remote with empty directory not showing anything to restore (PR [#6218](https://github.com/vatesfr/xen-orchestra/pull/6218))
- [S3] remote fom did not save the `https` and `allow unatuhorized`during remote creation (PR [#6219](https://github.com/vatesfr/xen-orchestra/pull/6219))
- [VM/advanced] Fix various errors when adding ACLs [#6213](https://github.com/vatesfr/xen-orchestra/issues/6213) (PR [#6230](https://github.com/vatesfr/xen-orchestra/pull/6230))
- [Home/Self] Don't make VM's resource set name clickable for non admin users as they aren't allowed to view the Self Service page (PR [#6252](https://github.com/vatesfr/xen-orchestra/pull/6252))
- [load-balancer] Fix density mode failing to shutdown hosts (PR [#6253](https://github.com/vatesfr/xen-orchestra/pull/6253))
- [Health] Make "Too many snapshots" table sortable by number of snapshots (PR [#6255](https://github.com/vatesfr/xen-orchestra/pull/6255))
- [Remote] Show complete errors instead of only a potentially missing message (PR [#6216](https://github.com/vatesfr/xen-orchestra/pull/6216))
### Released packages
- @xen-orchestra/self-signed 0.1.3
- vhd-lib 3.2.0
- @xen-orchestra/fs 1.0.3
- vhd-cli 0.7.2
- xo-vmdk-to-vhd 2.4.0
- @xen-orchestra/upload-ova 0.1.5
- @xen-orchestra/xapi 1.1.0
- @xen-orchestra/backups 0.24.0
- @xen-orchestra/backups-cli 0.7.2
- @xen-orchestra/emit-async 1.0.0
- @xen-orchestra/mixins 0.5.0
- @xen-orchestra/proxy 0.23.1
- xo-server 5.95.0
- xo-web 5.97.1
- xo-server-backup-reports 0.17.0
## 5.70.2 (2022-05-16)
### Bug fixes
@@ -35,8 +160,6 @@
## 5.70.0 (2022-04-29)
<img id="latest" src="https://badgen.net/badge/channel/latest/yellow" alt="Channel: latest" />
### Highlights
- [VM export] Feat export to `ova` format (PR [#6006](https://github.com/vatesfr/xen-orchestra/pull/6006))
@@ -73,8 +196,6 @@
## **5.69.2** (2022-04-13)
<img id="stable" src="https://badgen.net/badge/channel/stable/green" alt="Channel: stable" />
### Enhancements
- [Rolling Pool Update] New algorithm for XCP-ng updates (PR [#6188](https://github.com/vatesfr/xen-orchestra/pull/6188))

View File

@@ -7,20 +7,12 @@
> Users must be able to say: “Nice enhancement, I'm eager to test it”
- [Backup] Merge multiple VHDs at once which will speed up the merging ĥase after reducing the retention of a backup job(PR [#6184](https://github.com/vatesfr/xen-orchestra/pull/6184))
- [Backup] Implement file cache for listing the backups of a VM (PR [#6220](https://github.com/vatesfr/xen-orchestra/pull/6220))
- [Backup] Add setting `backups.metadata.defaultSettings.unconditionalSnapshot` in `xo-server`'s configuration file to force a snapshot even when not required by the backup, this is useful to avoid locking the VM halted during the backup (PR [#6221](https://github.com/vatesfr/xen-orchestra/pull/6221))
- [XO Web] Add ability to configure a default filter for Storage [#6236](https://github.com/vatesfr/xen-orchestra/issues/6236) (PR [#6237](https://github.com/vatesfr/xen-orchestra/pull/6237))
- [Backup] VMs with USB Pass-through devices are now supported! The advanced _Offline Snapshot Mode_ setting must be enabled. For Full Backup or Disaster Recovery jobs, Rolling Snapshot needs to be anabled as well. (PR [#6239](https://github.com/vatesfr/xen-orchestra/pull/6239))
- [SR] When SR is in maintenance, add "Maintenance mode" badge next to its name (PR [#6313](https://github.com/vatesfr/xen-orchestra/pull/6313))
### Bug fixes
> Users must be able to say: “I had this issue, happy to know it's fixed”
- [S3] Fix S3 remote with empty directory not showing anything to restore (PR [#6218](https://github.com/vatesfr/xen-orchestra/pull/6218))
- [S3] remote fom did not save the `https` and `allow unatuhorized`during remote creation (PR [#6219](https://github.com/vatesfr/xen-orchestra/pull/6219))
- [VM/advanced] Fix various errors when adding ACLs [#6213](https://github.com/vatesfr/xen-orchestra/issues/6213) (PR [#6230](https://github.com/vatesfr/xen-orchestra/pull/6230))
### Packages to release
> When modifying a package, add it here with its release type.
@@ -32,22 +24,12 @@
> - patch: if the change is a bug fix or a simple code improvement
> - minor: if the change is a new feature
> - major: if the change breaks compatibility
>
> Keep this list alphabetically ordered to avoid merge conflicts
<!--packages-start-->
- @xen-orchestra/self-signed patch
- vhd-lib patch
- @xen-orchestra/fs patch
- vhd-cli patch
- xo-vmdk-to-vhd minor
- @xen-orchestra/upload-ova patch
- @xen-orchestra/backups minor
- @xen-orchestra/backups-cli patch
- @xen-orchestra/emit-async major
- @xen-orchestra/mixins minor
- @xen-orchestra/proxy minor
- xo-server minor
- @vates/async-each minor
- xo-web minor
- xo-server-backup-reports minor
<!--packages-end-->

View File

@@ -99,3 +99,38 @@ To solve this issue, we recommend that you:
- wait until the other backup job is completed/the merge process is done
- make sure your remote storage is not being overworked
## Error: HTTP connection has timed out
This error occurs when XO tries to fetch data from a host, via the HTTP GET method. This error essentially means that the host (dom0 specifically) isn't responding anymore, after we asked it to expose the disk to be exported. This could be a symptom of having an overloaded dom0 that couldn't respond fast enough. It can also be caused by dom0 having trouble attaching the disk in question to expose it for fetching via HTTP, or just not having enough resources to answer our GET request.
::: warning
As a temporary workaround you can increase the timeout higher than the default value, to allow the host more time to respond. But you will need to eventually diagnose the root cause of the slow host response or else you risk the issue returning.
:::
Create the following file:
```
/etc/xo-server/config.httpInactivityTimeout.toml
```
Add the following lines:
```
# XOA Support - Work-around HTTP timeout issue during backups
[xapiOptions]
httpInactivityTimeout = 1800000 # 30 mins
```
## Error: Expected values to be strictly equal
This error occurs at the end of the transfer. XO checks the exported VM disk integrity, to ensure it's a valid VHD file (we check the VHD header as well as the footer of the received file). This error means the header and footage did not match, so the file is incomplete (likely the export from dom0 failed at some point and we only received a partial HD/VM disk).
## Error: the job is already running
This means the same job is still running, typically from the last scheduled run. This happens when you have a backup job scheduled too often. It can also occur if you have a long timeout configured for the job, and a slow VM export or slow transfer to your remote. In either case, you need to adjust your backup schedule to allow time for the job to finish or timeout before the next scheduled run. We consider this an error to ensure you'll be notified that the planned schedule won't run this time because the previous one isn't finished.
## Error: VDI_IO_ERROR
This error comes directly from your host/dom0, and not XO. Essentially, XO asked the host to expose a VM disk to export via HTTP (as usual), XO managed to make the HTTP GET connection, and even start the transfer. But then at some point the host couldn't read the VM disk any further, causing this error on the host side. This might happen if the VDI is corrupted on the storage, or if there's a race condition during snapshots. More rarely, this can also occur if your SR is just too slow to keep up with the export as well as live VM traffic.
## Error: no XAPI associated to <UUID>
This message means that XO had a UUID of a VM to backup, but when the job ran it couldn't find any object matching it. This could be caused by the pool where this VM lived no longer being connected to XO. Double-check that the pool hosting the VM is currently connected under Settings > Servers. You can also search for the VM UUID in the Home > VMs search bar. If you can see it, run the backup job again and it will work. If you cannot, either the VM was removed or the pool is not connected.

View File

@@ -66,12 +66,13 @@ You shouldn't have to change this. It's the path where `xo-web` files are served
## Custom certificate authority
If you use certificates signed by an in-house CA for your XenServer hosts, and want to have Xen Orchestra connect to them without rejection, you need to add the `--use-openssl-ca` option in Node, but also add the CA to your trust store (`/etc/ssl/certs` via `update-ca-certificates` in your XOA).
If you use certificates signed by an in-house CA for your XCP-ng or XenServer hosts, and want to have Xen Orchestra connect to them without rejection, you can use the [`NODE_EXTRA_CA_CERTS`](https://nodejs.org/api/cli.html#cli_node_extra_ca_certs_file) environment variable.
To enable this option in your XOA, edit the `/etc/systemd/system/xo-server.service` file and add this:
To enable this option in your XOA, create `/etc/systemd/system/xo-server.service.d/ca.conf` with the following content:
```
Environment=NODE_OPTIONS=--use-openssl-ca
[Service]
Environment=NODE_EXTRA_CA_CERTS=/usr/local/share/ca-certificates/my-cert.crt
```
Don't forget to reload `systemd` conf and restart `xo-server`:
@@ -81,9 +82,7 @@ Don't forget to reload `systemd` conf and restart `xo-server`:
# systemctl restart xo-server.service
```
:::tip
The `--use-openssl-ca` option is ignored by Node if Xen-Orchestra is run with Linux capabilities. Capabilities are commonly used to bind applications to privileged ports (<1024) (i.e. `CAP_NET_BIND_SERVICE`). Local NAT rules (`iptables`) or a reverse proxy would be required to use privileged ports and a custom certficate authority.
:::
> For XO Proxy, the process is almost the same except the file to create is `/etc/systemd/system/xo-proxy.service.d/ca.conf` and the service to restart is `xo-proxy.service`.
## Redis server

View File

@@ -18,6 +18,8 @@ If you lose your main pool, you can start the copy on the other side, with very
:::warning
It is normal that you can't boot the copied VM directly: we protect it. The normal workflow is to make a clone and then work on it.
This also affects VMs with "Auto Power On" enabled, because of our protections you can ensure these won't start on your CR destination if you happen to reboot it.
:::
## Configure it

View File

@@ -35,3 +35,7 @@ A higher retention number will lead to huge space occupation on your SR.
If you boot a copy of your production VM, be careful: if they share the same static IP, you'll have troubles.
A good way to avoid this kind of problem is to remove the network interface on the DR VM and check if the export is correctly done.
:::warning
For each DR replicated VM, we add "start" as a blocked operation, meaning even VMs with "Auto power on" enabled will not be started on your DR destination if it reboots.
:::

View File

@@ -141,6 +141,28 @@ curl \
> myDisk.vhd
```
## VDI Import
A VHD can be imported on an SR to create a VDI at `/rest/v0/srs/<sr uuid>/vdis`.
```bash
curl \
-X POST \
-b authenticationToken=KQxQdm2vMiv7jBIK0hgkmgxKzemd8wSJ7ugFGKFkTbs \
-T myDisk.vhd \
'https://xo.example.org/rest/v0/srs/357bd56c-71f9-4b2a-83b8-3451dec04b8f/vdis?name_label=my_imported_VDI' \
| cat
```
> Note: the final `| cat` ensures cURL's standard output is not a TTY, which is necessary for upload stats to be dislayed.
This request returns the UUID of the created VDI.
The following query parameters are supported to customize the created VDI:
- `name_label`
- `name_description`
## The future
We are adding features and improving the REST API step by step. If you have interesting use cases or feedback, please ask directly at <https://xcp-ng.org/forum/category/12/xen-orchestra>

View File

@@ -60,6 +60,7 @@
"testEnvironment": "node",
"testPathIgnorePatterns": [
"/@vates/decorate-with/",
"/@vates/event-listeners-manager/",
"/@vates/predicates/",
"/@xen-orchestra/audit-core/",
"/dist/",

View File

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

View File

@@ -1 +0,0 @@
../../scripts/babel-eslintrc.js

View File

@@ -0,0 +1,14 @@
'use strict'
const { parse } = require('./')
const { ast, pattern } = require('./index.fixtures')
module.exports = ({ benchmark }) => {
benchmark('parse', () => {
parse(pattern)
})
benchmark('toString', () => {
ast.toString()
})
}

View File

@@ -1,8 +1,10 @@
import * as CM from './'
'use strict'
export const pattern = 'foo !"\\\\ \\"" name:|(wonderwoman batman) hasCape? age:32 chi*go /^foo\\/bar\\./i'
const CM = require('./')
export const ast = new CM.And([
exports.pattern = 'foo !"\\\\ \\"" name:|(wonderwoman batman) hasCape? age:32 chi*go /^foo\\/bar\\./i'
exports.ast = new CM.And([
new CM.String('foo'),
new CM.Not(new CM.String('\\ "')),
new CM.Property('name', new CM.Or([new CM.String('wonderwoman'), new CM.String('batman')])),

View File

@@ -1,4 +1,6 @@
import { escapeRegExp, isPlainObject, some } from 'lodash'
'use strict'
const { escapeRegExp, isPlainObject, some } = require('lodash')
// ===================================================================
@@ -23,7 +25,7 @@ class Node {
}
}
export class Null extends Node {
class Null extends Node {
match() {
return true
}
@@ -32,10 +34,11 @@ export class Null extends Node {
return ''
}
}
exports.Null = Null
const formatTerms = terms => terms.map(term => term.toString(true)).join(' ')
export class And extends Node {
class And extends Node {
constructor(children) {
super()
@@ -54,8 +57,9 @@ export class And extends Node {
return isNested ? `(${terms})` : terms
}
}
exports.And = And
export class Comparison extends Node {
class Comparison extends Node {
constructor(operator, value) {
super()
this._comparator = Comparison.comparators[operator]
@@ -71,6 +75,7 @@ export class Comparison extends Node {
return this._operator + String(this._value)
}
}
exports.Comparison = Comparison
Comparison.comparators = {
'>': (a, b) => a > b,
'>=': (a, b) => a >= b,
@@ -78,7 +83,7 @@ Comparison.comparators = {
'<=': (a, b) => a <= b,
}
export class Or extends Node {
class Or extends Node {
constructor(children) {
super()
@@ -96,8 +101,9 @@ export class Or extends Node {
return `|(${formatTerms(this.children)})`
}
}
exports.Or = Or
export class Not extends Node {
class Not extends Node {
constructor(child) {
super()
@@ -112,8 +118,9 @@ export class Not extends Node {
return '!' + this.child.toString(true)
}
}
exports.Not = Not
export class NumberNode extends Node {
exports.Number = exports.NumberNode = class NumberNode extends Node {
constructor(value) {
super()
@@ -133,9 +140,8 @@ export class NumberNode extends Node {
return String(this.value)
}
}
export { NumberNode as Number }
export class NumberOrStringNode extends Node {
class NumberOrStringNode extends Node {
constructor(value) {
super()
@@ -160,9 +166,9 @@ export class NumberOrStringNode extends Node {
return this.value
}
}
export { NumberOrStringNode as NumberOrString }
exports.NumberOrString = exports.NumberOrStringNode = NumberOrStringNode
export class Property extends Node {
class Property extends Node {
constructor(name, child) {
super()
@@ -178,12 +184,13 @@ export class Property extends Node {
return `${formatString(this.name)}:${this.child.toString(true)}`
}
}
exports.Property = Property
const escapeChar = char => '\\' + char
const formatString = value =>
Number.isNaN(+value) ? (isRawString(value) ? value : `"${value.replace(/\\|"/g, escapeChar)}"`) : `"${value}"`
export class GlobPattern extends Node {
class GlobPattern extends Node {
constructor(value) {
// fallback to string node if no wildcard
if (value.indexOf('*') === -1) {
@@ -216,8 +223,9 @@ export class GlobPattern extends Node {
return this.value
}
}
exports.GlobPattern = GlobPattern
export class RegExpNode extends Node {
class RegExpNode extends Node {
constructor(pattern, flags) {
super()
@@ -245,9 +253,9 @@ export class RegExpNode extends Node {
return this.re.toString()
}
}
export { RegExpNode as RegExp }
exports.RegExp = RegExpNode
export class StringNode extends Node {
class StringNode extends Node {
constructor(value) {
super()
@@ -275,9 +283,9 @@ export class StringNode extends Node {
return formatString(this.value)
}
}
export { StringNode as String }
exports.String = exports.StringNode = StringNode
export class TruthyProperty extends Node {
class TruthyProperty extends Node {
constructor(name) {
super()
@@ -292,6 +300,7 @@ export class TruthyProperty extends Node {
return formatString(this.name) + '?'
}
}
exports.TruthyProperty = TruthyProperty
// -------------------------------------------------------------------
@@ -531,7 +540,7 @@ const parser = P.grammar({
),
ws: P.regex(/\s*/),
}).default
export const parse = parser.parse.bind(parser)
exports.parse = parser.parse.bind(parser)
// -------------------------------------------------------------------
@@ -573,7 +582,7 @@ const _getPropertyClauseStrings = ({ child }) => {
}
// Find possible values for property clauses in a and clause.
export const getPropertyClausesStrings = node => {
exports.getPropertyClausesStrings = function getPropertyClausesStrings(node) {
if (!node) {
return {}
}
@@ -605,7 +614,7 @@ export const getPropertyClausesStrings = node => {
// -------------------------------------------------------------------
export const setPropertyClause = (node, name, child) => {
exports.setPropertyClause = function setPropertyClause(node, name, child) {
const property = child && new Property(name, typeof child === 'string' ? new StringNode(child) : child)
if (node === undefined) {

View File

@@ -1,7 +1,9 @@
/* eslint-env jest */
import { ast, pattern } from './index.fixtures'
import {
'use strict'
const { ast, pattern } = require('./index.fixtures')
const {
getPropertyClausesStrings,
GlobPattern,
Null,
@@ -11,7 +13,7 @@ import {
Property,
setPropertyClause,
StringNode,
} from './'
} = require('./')
it('getPropertyClausesStrings', () => {
const tmp = getPropertyClausesStrings(parse('foo bar:baz baz:|(foo bar /^boo$/ /^far$/) foo:/^bar$/'))

View File

@@ -16,7 +16,6 @@
"url": "https://vates.fr"
},
"preferGlobal": false,
"main": "dist/",
"browserslist": [
">2%"
],
@@ -26,21 +25,7 @@
"dependencies": {
"lodash": "^4.17.4"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-plugin-lodash": "^3.3.2",
"cross-env": "^7.0.2",
"rimraf": "^3.0.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
"clean": "rimraf dist/",
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
"prebuild": "yarn run clean",
"predev": "yarn run prebuild",
"prepublishOnly": "yarn run build",
"postversion": "npm publish"
}
}

View File

@@ -1,12 +0,0 @@
import { parse } from './'
import { ast, pattern } from './index.fixtures'
export default ({ benchmark }) => {
benchmark('parse', () => {
parse(pattern)
})
benchmark('toString', () => {
ast.toString()
})
}

View File

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

View File

@@ -1 +0,0 @@
../../scripts/babel-eslintrc.js

View File

@@ -1,3 +1,5 @@
'use strict'
const match = (pattern, value) => {
if (Array.isArray(pattern)) {
return (
@@ -43,4 +45,6 @@ const match = (pattern, value) => {
return pattern === value
}
export const createPredicate = pattern => value => match(pattern, value)
exports.createPredicate = function createPredicate(pattern) {
return value => match(pattern, value)
}

View File

@@ -16,27 +16,13 @@
"url": "https://vates.fr"
},
"preferGlobal": false,
"main": "dist/",
"browserslist": [
">2%"
],
"engines": {
"node": ">=6"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"cross-env": "^7.0.2",
"rimraf": "^3.0.0"
},
"scripts": {
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
"clean": "rimraf dist/",
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
"prebuild": "yarn run clean",
"predev": "yarn run prebuild",
"prepublishOnly": "yarn run build",
"postversion": "npm publish"
}
}

View File

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

View File

@@ -1 +0,0 @@
../../scripts/babel-eslintrc.js

View File

@@ -1,3 +1,5 @@
'use strict'
const { createWriteStream } = require('fs')
const { PassThrough } = require('stream')
@@ -12,7 +14,7 @@ const createOutputStream = path => {
return stream
}
export const writeStream = (input, path) => {
exports.writeStream = function writeStream(input, path) {
const output = createOutputStream(path)
return new Promise((resolve, reject) =>

View File

@@ -1,11 +1,13 @@
import { VhdFile, checkVhdChain } from 'vhd-lib'
import getopts from 'getopts'
import { getHandler } from '@xen-orchestra/fs'
import { resolve } from 'path'
'use strict'
const { VhdFile, checkVhdChain } = require('vhd-lib')
const getopts = require('getopts')
const { getHandler } = require('@xen-orchestra/fs')
const { resolve } = require('path')
const checkVhd = (handler, path) => new VhdFile(handler, path).readHeaderAndFooter()
export default async rawArgs => {
module.exports = async function check(rawArgs) {
const { chain, _: args } = getopts(rawArgs, {
boolean: ['chain'],
default: {

View File

@@ -1,9 +1,11 @@
import { getSyncedHandler } from '@xen-orchestra/fs'
import { openVhd, Constants } from 'vhd-lib'
import Disposable from 'promise-toolbox/Disposable'
import omit from 'lodash/omit'
'use strict'
const deepCompareObjects = function (src, dest, path) {
const { getSyncedHandler } = require('@xen-orchestra/fs')
const { openVhd, Constants } = require('vhd-lib')
const Disposable = require('promise-toolbox/Disposable')
const omit = require('lodash/omit')
function deepCompareObjects(src, dest, path) {
for (const key of Object.keys(src)) {
const srcValue = src[key]
const destValue = dest[key]
@@ -29,7 +31,7 @@ const deepCompareObjects = function (src, dest, path) {
}
}
export default async args => {
module.exports = async function compare(args) {
if (args.length < 4 || args.some(_ => _ === '-h' || _ === '--help')) {
return `Usage: compare <sourceRemoteUrl> <source VHD> <destionationRemoteUrl> <destination> `
}

View File

@@ -1,9 +1,11 @@
import { getSyncedHandler } from '@xen-orchestra/fs'
import { openVhd, VhdFile, VhdDirectory } from 'vhd-lib'
import Disposable from 'promise-toolbox/Disposable'
import getopts from 'getopts'
'use strict'
export default async rawArgs => {
const { getSyncedHandler } = require('@xen-orchestra/fs')
const { openVhd, VhdFile, VhdDirectory } = require('vhd-lib')
const Disposable = require('promise-toolbox/Disposable')
const getopts = require('getopts')
module.exports = async function copy(rawArgs) {
const {
directory,
help,

View File

@@ -0,0 +1,43 @@
//
// This file has been generated by [index-modules](https://npmjs.com/index-modules)
//
var d = Object.defineProperty
function de(o, n, v) {
d(o, n, { enumerable: true, value: v })
return v
}
function dl(o, n, g, a) {
d(o, n, {
configurable: true,
enumerable: true,
get: function () {
return de(o, n, g(a))
},
})
}
function r(p) {
var v = require(p)
return v && v.__esModule
? v
: typeof v === 'object' || typeof v === 'function'
? Object.create(v, { default: { enumerable: true, value: v } })
: { default: v }
}
function e(p, i) {
dl(defaults, i, function () {
return exports[i].default
})
dl(exports, i, r, p)
}
d(exports, '__esModule', { value: true })
var defaults = de(exports, 'default', {})
e('./check.js', 'check')
e('./compare.js', 'compare')
e('./copy.js', 'copy')
e('./info.js', 'info')
e('./merge.js', 'merge')
e('./raw.js', 'raw')
e('./repl.js', 'repl')
e('./synthetize.js', 'synthetize')

View File

@@ -1,9 +1,13 @@
import { Constants, VhdFile } from 'vhd-lib'
import { getHandler } from '@xen-orchestra/fs'
import { resolve } from 'path'
import * as UUID from 'uuid'
import humanFormat from 'human-format'
import invert from 'lodash/invert.js'
'use strict'
const { Constants, VhdFile } = require('vhd-lib')
const { getHandler } = require('@xen-orchestra/fs')
const { openVhd } = require('vhd-lib/openVhd')
const { resolve } = require('path')
const Disposable = require('promise-toolbox/Disposable')
const humanFormat = require('human-format')
const invert = require('lodash/invert.js')
const UUID = require('uuid')
const { PLATFORMS } = Constants
@@ -32,8 +36,8 @@ function mapProperties(object, mapping) {
return result
}
export default async args => {
const vhd = new VhdFile(getHandler({ url: 'file:///' }), resolve(args[0]))
async function showDetails(handler, path) {
const vhd = new VhdFile(handler, resolve(path))
try {
await vhd.readHeaderAndFooter()
@@ -67,3 +71,29 @@ export default async args => {
})
)
}
async function showList(handler, paths) {
let previousUuid
for (const path of paths) {
await Disposable.use(openVhd(handler, resolve(path)), async vhd => {
const uuid = MAPPERS.uuid(vhd.footer.uuid)
const fields = [path, MAPPERS.bytes(vhd.footer.currentSize), uuid, MAPPERS.diskType(vhd.footer.diskType)]
if (vhd.footer.diskType === Constants.DISK_TYPES.DIFFERENCING) {
const parentUuid = MAPPERS.uuid(vhd.header.parentUuid)
fields.push(parentUuid === previousUuid ? '<above VHD>' : parentUuid)
}
previousUuid = uuid
console.log(fields.join(' | '))
})
}
}
module.exports = async function info(args) {
const handler = getHandler({ url: 'file:///' })
if (args.length === 1) {
return showDetails(handler, args[0])
}
return showList(handler, args)
}

View File

@@ -1,9 +1,11 @@
import { Bar } from 'cli-progress'
import { mergeVhd } from 'vhd-lib'
import { getHandler } from '@xen-orchestra/fs'
import { resolve } from 'path'
'use strict'
export default async function main(args) {
const { Bar } = require('cli-progress')
const { mergeVhd } = require('vhd-lib')
const { getHandler } = require('@xen-orchestra/fs')
const { resolve } = require('path')
module.exports = async function merge(args) {
if (args.length < 2 || args.some(_ => _ === '-h' || _ === '--help')) {
return `Usage: ${this.command} <child VHD> <parent VHD>`
}

View File

@@ -1,11 +1,13 @@
import { openVhd } from 'vhd-lib'
import { getSyncedHandler } from '@xen-orchestra/fs'
import { resolve } from 'path'
'use strict'
import { writeStream } from '../_utils'
import { Disposable } from 'promise-toolbox'
const { openVhd } = require('vhd-lib')
const { getSyncedHandler } = require('@xen-orchestra/fs')
const { resolve } = require('path')
export default async args => {
const { writeStream } = require('../_utils')
const { Disposable } = require('promise-toolbox')
module.exports = async function raw(args) {
if (args.length < 2 || args.some(_ => _ === '-h' || _ === '--help')) {
return `Usage: ${this.command} <input VHD> [<output raw>]`
}

View File

@@ -1,10 +1,12 @@
import { asCallback, fromCallback, fromEvent } from 'promise-toolbox'
import { getHandler } from '@xen-orchestra/fs'
import { relative } from 'path'
import { start as createRepl } from 'repl'
import * as vhdLib from 'vhd-lib'
'use strict'
export default async args => {
const { asCallback, fromCallback, fromEvent } = require('promise-toolbox')
const { getHandler } = require('@xen-orchestra/fs')
const { relative } = require('path')
const { start: createRepl } = require('repl')
const vhdLib = require('vhd-lib')
module.exports = async function repl(args) {
const cwd = process.cwd()
const handler = getHandler({ url: 'file://' + cwd })
await handler.sync()

View File

@@ -1,9 +1,11 @@
import path from 'path'
import { createSyntheticStream } from 'vhd-lib'
import { createWriteStream } from 'fs'
import { getHandler } from '@xen-orchestra/fs'
'use strict'
export default async function main(args) {
const path = require('path')
const { createSyntheticStream } = require('vhd-lib')
const { createWriteStream } = require('fs')
const { getHandler } = require('@xen-orchestra/fs')
module.exports = async function synthetize(args) {
if (args.length < 2 || args.some(_ => _ === '-h' || _ === '--help')) {
return `Usage: ${this.command} <input VHD> <output VHD>`
}

View File

@@ -1,10 +1,11 @@
#!/usr/bin/env node
import execPromise from 'exec-promise'
'use strict'
import pkg from '../package.json'
const execPromise = require('exec-promise')
import commands from './commands'
const pkg = require('./package.json')
const commands = require('./commands').default
function runCommand(commands, [command, ...args]) {
if (command === undefined || command === '-h' || command === '--help') {

View File

@@ -1,7 +1,7 @@
{
"private": false,
"name": "vhd-cli",
"version": "0.7.1",
"version": "0.8.0",
"license": "ISC",
"description": "Tools to read/create and merge VHD files",
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/packages/vhd-cli",
@@ -16,40 +16,24 @@
"url": "https://vates.fr"
},
"preferGlobal": true,
"main": "dist/",
"bin": {
"vhd-cli": "dist/index.js"
"vhd-cli": "./index.js"
},
"engines": {
"node": ">=8.10"
"node": ">=10"
},
"dependencies": {
"@xen-orchestra/fs": "^1.0.1",
"@xen-orchestra/fs": "^1.1.0",
"cli-progress": "^3.1.0",
"exec-promise": "^0.7.0",
"getopts": "^2.2.3",
"human-format": "^1.0.0",
"lodash": "^4.17.21",
"uuid": "^8.3.2",
"vhd-lib": "^3.1.0"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"cross-env": "^7.0.2",
"execa": "^5.0.0",
"index-modules": "^0.4.3",
"promise-toolbox": "^0.21.0",
"rimraf": "^3.0.0",
"tmp": "^0.2.1"
"uuid": "^8.3.2",
"vhd-lib": "^3.3.1"
},
"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/",
"prebuild": "rimraf dist/ && index-modules --cjs-lazy src/commands",
"predev": "yarn run prebuild",
"prepublishOnly": "yarn run build",
"postversion": "npm publish"
}
}

View File

@@ -86,10 +86,19 @@ exports.VhdAbstract = class VhdAbstract {
}
/**
* @typedef {Object} BitmapBlock
* @property {number} id
* @property {Buffer} bitmap
*
* @typedef {Object} FullBlock
* @property {number} id
* @property {Buffer} bitmap
* @property {Buffer} data
* @property {Buffer} buffer - bitmap + data
*
* @param {number} blockId
* @param {boolean} onlyBitmap
* @returns {Buffer}
* @returns {Promise<BitmapBlock | FullBlock>}
*/
readBlock(blockId, onlyBitmap = false) {
throw new Error(`reading ${onlyBitmap ? 'bitmap of block' : 'block'} ${blockId} is not implemented`)
@@ -104,7 +113,7 @@ exports.VhdAbstract = class VhdAbstract {
*
* @returns {number} the merged data size
*/
async coalesceBlock(child, blockId) {
async mergeBlock(child, blockId) {
const block = await child.readBlock(blockId)
await this.writeEntireBlock(block)
return block.data.length

View File

@@ -53,19 +53,25 @@ test('Can coalesce block', async () => {
const childDirectoryVhd = yield openVhd(handler, childDirectoryName)
await childDirectoryVhd.readBlockAllocationTable()
await parentVhd.coalesceBlock(childFileVhd, 0)
let childBlockData = (await childDirectoryVhd.readBlock(0)).data
await parentVhd.mergeBlock(childDirectoryVhd, 0)
await parentVhd.writeFooter()
await parentVhd.writeBlockAllocationTable()
let parentBlockData = (await parentVhd.readBlock(0)).data
let childBlockData = (await childFileVhd.readBlock(0)).data
// block should be present in parent
expect(parentBlockData.equals(childBlockData)).toEqual(true)
// block should not be in child since it's a rename for vhd directory
await expect(childDirectoryVhd.readBlock(0)).rejects.toThrowError()
await parentVhd.coalesceBlock(childDirectoryVhd, 0)
childBlockData = (await childFileVhd.readBlock(1)).data
await parentVhd.mergeBlock(childFileVhd, 1)
await parentVhd.writeFooter()
await parentVhd.writeBlockAllocationTable()
parentBlockData = (await parentVhd.readBlock(0)).data
childBlockData = (await childDirectoryVhd.readBlock(0)).data
expect(parentBlockData).toEqual(childBlockData)
parentBlockData = (await parentVhd.readBlock(1)).data
// block should be present in parent in case of mixed vhdfile/vhddirectory
expect(parentBlockData.equals(childBlockData)).toEqual(true)
// block should still be child
await childFileVhd.readBlock(1)
})
})

View File

@@ -117,7 +117,7 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
}
static async create(handler, path, { flags = 'wx+', compression } = {}) {
await handler.mkdir(path)
await handler.mktree(path)
const vhd = new VhdDirectory(handler, path, { flags, compression })
return {
dispose: () => {},
@@ -142,13 +142,13 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
return test(this.#blockTable, blockId)
}
_getChunkPath(partName) {
#getChunkPath(partName) {
return this._path + '/' + partName
}
async _readChunk(partName) {
// here we can implement compression and / or crypto
const buffer = await this._handler.readFile(this._getChunkPath(partName))
const buffer = await this._handler.readFile(this.#getChunkPath(partName))
const uncompressed = await this.#compressor.decompress(buffer)
return {
@@ -163,17 +163,23 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
`Can't write a chunk ${partName} in ${this._path} with read permission`
)
// in case of VhdDirectory, we want to create the file if it does not exists
const flags = this._opts?.flags === 'r+' ? 'w' : this._opts?.flags
const compressed = await this.#compressor.compress(buffer)
return this._handler.outputFile(this._getChunkPath(partName), compressed, this._opts)
return this._handler.outputFile(this.#getChunkPath(partName), compressed, { flags })
}
// put block in subdirectories to limit impact when doing directory listing
_getBlockPath(blockId) {
#getBlockPath(blockId) {
const blockPrefix = Math.floor(blockId / 1e3)
const blockSuffix = blockId - blockPrefix * 1e3
return `blocks/${blockPrefix}/${blockSuffix}`
}
_getFullBlockPath(blockId) {
return this.#getChunkPath(this.#getBlockPath(blockId))
}
async readHeaderAndFooter() {
await this.#readChunkFilters()
@@ -200,7 +206,7 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
if (onlyBitmap) {
throw new Error(`reading 'bitmap of block' ${blockId} in a VhdDirectory is not implemented`)
}
const { buffer } = await this._readChunk(this._getBlockPath(blockId))
const { buffer } = await this._readChunk(this.#getBlockPath(blockId))
return {
id: blockId,
bitmap: buffer.slice(0, this.bitmapSize),
@@ -240,25 +246,39 @@ exports.VhdDirectory = class VhdDirectory extends VhdAbstract {
}
// only works if data are in the same handler
// and if the full block is modified in child ( which is the case whit xcp)
// and if the full block is modified in child ( which is the case with xcp)
// and if the compression type is same on both sides
async coalesceBlock(child, blockId) {
async mergeBlock(child, blockId, isResumingMerge = false) {
const childBlockPath = child._getFullBlockPath?.(blockId)
if (
!(child instanceof VhdDirectory) ||
childBlockPath !== undefined ||
this._handler !== child._handler ||
child.compressionType !== this.compressionType
child.compressionType !== this.compressionType ||
child.compressionType === 'MIXED'
) {
return super.coalesceBlock(child, blockId)
return super.mergeBlock(child, blockId)
}
await this._handler.copy(
child._getChunkPath(child._getBlockPath(blockId)),
this._getChunkPath(this._getBlockPath(blockId))
)
try {
await this._handler.rename(childBlockPath, this._getFullBlockPath(blockId))
} catch (error) {
if (error.code === 'ENOENT' && isResumingMerge === true) {
// when resuming, the blocks moved since the last merge state write are
// not in the child anymore but it should be ok
// it will throw an error if block is missing in parent
// won't detect if the block was already in parent and is broken/missing in child
const { data } = await this.readBlock(blockId)
assert.strictEqual(data.length, this.header.blockSize)
} else {
throw error
}
}
setBitmap(this.#blockTable, blockId)
return sectorsToBytes(this.sectorsPerBlock)
}
async writeEntireBlock(block) {
await this._writeChunk(this._getBlockPath(block.id), block.buffer)
await this._writeChunk(this.#getBlockPath(block.id), block.buffer)
setBitmap(this.#blockTable, block.id)
}

View File

@@ -222,14 +222,14 @@ test('Can coalesce block', async () => {
const childDirectoryVhd = yield openVhd(handler, childDirectoryName)
await childDirectoryVhd.readBlockAllocationTable()
await parentVhd.coalesceBlock(childFileVhd, 0)
await parentVhd.mergeBlock(childFileVhd, 0)
await parentVhd.writeFooter()
await parentVhd.writeBlockAllocationTable()
let parentBlockData = (await parentVhd.readBlock(0)).data
let childBlockData = (await childFileVhd.readBlock(0)).data
expect(parentBlockData).toEqual(childBlockData)
await parentVhd.coalesceBlock(childDirectoryVhd, 0)
await parentVhd.mergeBlock(childDirectoryVhd, 0)
await parentVhd.writeFooter()
await parentVhd.writeBlockAllocationTable()
parentBlockData = (await parentVhd.readBlock(0)).data

View File

@@ -43,6 +43,16 @@ const VhdSynthetic = class VhdSynthetic extends VhdAbstract {
}
}
get compressionType() {
const compressionType = this.vhds[0].compressionType
for (let i = 0; i < this.vhds.length; i++) {
if (compressionType !== this.vhds[i].compressionType) {
return 'MIXED'
}
}
return compressionType
}
/**
* @param {Array<VhdAbstract>} vhds the chain of Vhds used to compute this Vhd, from the deepest child (in position 0), to the root (in the last position)
* only the last one can have any type. Other must have type DISK_TYPES.DIFFERENCING (delta)
@@ -54,7 +64,7 @@ const VhdSynthetic = class VhdSynthetic extends VhdAbstract {
}
async readBlockAllocationTable() {
await asyncMap(this.#vhds, vhd => vhd.readBlockAllocationTable())
await pEach(this.#vhds, vhd => vhd.readBlockAllocationTable())
}
containsBlock(blockId) {
@@ -64,7 +74,7 @@ const VhdSynthetic = class VhdSynthetic extends VhdAbstract {
async readHeaderAndFooter() {
const vhds = this.#vhds
await asyncMap(vhds, vhd => vhd.readHeaderAndFooter())
await pEach(vhds, vhd => vhd.readHeaderAndFooter())
for (let i = 0, n = vhds.length - 1; i < n; ++i) {
const child = vhds[i]
@@ -74,17 +84,33 @@ const VhdSynthetic = class VhdSynthetic extends VhdAbstract {
}
}
async readBlock(blockId, onlyBitmap = false) {
#getVhdWithBlock(blockId) {
const index = this.#vhds.findIndex(vhd => vhd.containsBlock(blockId))
assert(index !== -1, `no such block ${blockId}`)
return this.#vhds[index]
}
async readBlock(blockId, onlyBitmap = false) {
// only read the content of the first vhd containing this block
return await this.#vhds[index].readBlock(blockId, onlyBitmap)
return await this.#getVhdWithBlock(blockId).readBlock(blockId, onlyBitmap)
}
async mergeBlock(child, blockId) {
throw new Error(`can't coalesce block into a vhd synthetic`)
}
_readParentLocatorData(id) {
return this.#vhds[this.#vhds.length - 1]._readParentLocatorData(id)
}
_getFullBlockPath(blockId) {
const vhd = this.#getVhdWithBlock(blockId)
return vhd?._getFullBlockPath(blockId)
}
// return true if all the vhds ar an instance of cls
checkVhdsClass(cls) {
return this.#vhds.every(vhd => vhd instanceof cls)
}
}
// add decorated static method

View File

@@ -8,7 +8,7 @@ const { Disposable } = require('promise-toolbox')
module.exports = async function chain(parentHandler, parentPath, childHandler, childPath, force = false) {
await Disposable.use(
[openVhd(parentHandler, parentPath), openVhd(childHandler, childPath)],
[openVhd(parentHandler, parentPath), openVhd(childHandler, childPath, { flags: 'r+' })],
async ([parentVhd, childVhd]) => {
await childVhd.readHeaderAndFooter()
const { header, footer } = childVhd

View File

@@ -1,13 +1,16 @@
'use strict'
const { createLogger } = require('@xen-orchestra/log')
const { parseVhdStream } = require('./parseVhdStream.js')
const { VhdDirectory } = require('./Vhd/VhdDirectory.js')
const { Disposable } = require('promise-toolbox')
const { asyncEach } = require('@vates/async-each')
const { pEach } = require('@vates/async-each')
const { warn } = createLogger('vhd-lib:createVhdDirectoryFromStream')
const buildVhd = Disposable.wrap(async function* (handler, path, inputStream, { concurrency, compression }) {
const vhd = yield VhdDirectory.create(handler, path, { compression })
await asyncEach(
await pEach(
parseVhdStream(inputStream),
async function (item) {
switch (item.type) {
@@ -50,7 +53,7 @@ exports.createVhdDirectoryFromStream = async function createVhdDirectoryFromStre
}
} catch (error) {
// cleanup on error
await handler.rmtree(path)
await handler.rmtree(path).catch(warn)
throw error
}
}

View File

@@ -6,7 +6,8 @@ exports.checkVhdChain = require('./checkChain')
exports.createReadableSparseStream = require('./createReadableSparseStream')
exports.createVhdStreamWithLength = require('./createVhdStreamWithLength')
exports.createVhdDirectoryFromStream = require('./createVhdDirectoryFromStream').createVhdDirectoryFromStream
exports.mergeVhd = require('./merge')
const { mergeVhd } = require('./merge')
exports.mergeVhd = mergeVhd
exports.peekFooterFromVhdStream = require('./peekFooterFromVhdStream')
exports.openVhd = require('./openVhd').openVhd
exports.VhdAbstract = require('./Vhd/VhdAbstract').VhdAbstract

View File

@@ -9,6 +9,7 @@ const { getHandler } = require('@xen-orchestra/fs')
const { pFromCallback } = require('promise-toolbox')
const { VhdFile, chainVhd, mergeVhd } = require('./index')
const { _cleanupVhds: cleanupVhds } = require('./merge')
const { checkFile, createRandomFile, convertFromRawToVhd } = require('./tests/utils')
@@ -38,14 +39,15 @@ test('merge works in normal cases', async () => {
await createRandomFile(`${tempDir}/${childRandomFileName}`, mbOfChildren)
await convertFromRawToVhd(`${tempDir}/${childRandomFileName}`, `${tempDir}/${child1FileName}`)
await chainVhd(handler, parentFileName, handler, child1FileName, true)
await checkFile(`${tempDir}/${parentFileName}`)
// merge
await mergeVhd(handler, parentFileName, handler, child1FileName)
// check that vhd is still valid
await checkFile(`${tempDir}/${parentFileName}`)
// check that the merged vhd is still valid
await checkFile(`${tempDir}/${child1FileName}`)
const parentVhd = new VhdFile(handler, parentFileName)
const parentVhd = new VhdFile(handler, child1FileName)
await parentVhd.readHeaderAndFooter()
await parentVhd.readBlockAllocationTable()
@@ -138,11 +140,11 @@ test('it can resume a merge ', async () => {
await mergeVhd(handler, 'parent.vhd', handler, 'child1.vhd')
// reload header footer and block allocation table , they should succed
await parentVhd.readHeaderAndFooter()
await parentVhd.readBlockAllocationTable()
await childVhd.readHeaderAndFooter()
await childVhd.readBlockAllocationTable()
let offset = 0
// check that the data are the same as source
for await (const block of parentVhd.blocks()) {
for await (const block of childVhd.blocks()) {
const blockContent = block.data
// first block is marked as already merged, should not be modified
// second block should come from children
@@ -153,7 +155,7 @@ test('it can resume a merge ', async () => {
await fs.read(fd, buffer, 0, buffer.length, offset)
expect(buffer.equals(blockContent)).toEqual(true)
offset += parentVhd.header.blockSize
offset += childVhd.header.blockSize
}
})
@@ -183,9 +185,9 @@ test('it merge multiple child in one pass ', async () => {
await mergeVhd(handler, parentFileName, handler, [grandChildFileName, childFileName])
// check that vhd is still valid
await checkFile(parentFileName)
await checkFile(grandChildFileName)
const parentVhd = new VhdFile(handler, parentFileName)
const parentVhd = new VhdFile(handler, grandChildFileName)
await parentVhd.readHeaderAndFooter()
await parentVhd.readBlockAllocationTable()
@@ -206,3 +208,21 @@ test('it merge multiple child in one pass ', async () => {
offset += parentVhd.header.blockSize
}
})
test('it cleans vhd mergedfiles', async () => {
const handler = getHandler({ url: `file://${tempDir}` })
await handler.writeFile('parent', 'parentData')
await handler.writeFile('child1', 'child1Data')
await handler.writeFile('child2', 'child2Data')
await handler.writeFile('child3', 'child3Data')
// childPath is from the grand children to the children
await cleanupVhds(handler, 'parent', ['child3', 'child2', 'child1'], { remove: true })
// only child3 should stay, with the data of parent
const [child3, ...other] = await handler.list('.')
expect(other.length).toEqual(0)
expect(child3).toEqual('child3')
expect((await handler.readFile('child3')).toString('utf8')).toEqual('parentData')
})

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