volumes: wait for mount during add/update
this is a better feedback mechanism for the user
This commit is contained in:
+38
-27
@@ -69,32 +69,6 @@ function validateHostPath(hostPath, mountType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
async function update(volume, mountType, mountOptions) {
|
||||
assert.strictEqual(typeof volume, 'object');
|
||||
assert.strictEqual(typeof mountType, 'string');
|
||||
assert.strictEqual(typeof mountOptions, 'object');
|
||||
|
||||
let error = mounts.validateMountOptions(mountType, mountOptions);
|
||||
if (error) throw error;
|
||||
|
||||
if (mountType === 'noop') {
|
||||
await mounts.removeMountFile(volume.hostPath);
|
||||
} else {
|
||||
await mounts.writeMountFile(Object.assign({}, volume, { mountType, mountOptions }));
|
||||
}
|
||||
|
||||
let result;
|
||||
[error, result] = await safe(database.query('UPDATE volumes SET mountType=?, mountOptionsJson=? WHERE id=?', [ mountType, JSON.stringify(mountOptions), volume.id ]));
|
||||
if (error) throw error;
|
||||
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Volume not found');
|
||||
}
|
||||
|
||||
async function getStatus(volume) {
|
||||
assert.strictEqual(typeof volume, 'object');
|
||||
|
||||
return await mounts.getStatus(volume.mountType, volume.hostPath); // { state, message }
|
||||
}
|
||||
|
||||
async function add(volume, auditSource) {
|
||||
assert.strictEqual(typeof volume, 'object');
|
||||
assert.strictEqual(typeof auditSource, 'object');
|
||||
@@ -112,8 +86,15 @@ async function add(volume, auditSource) {
|
||||
|
||||
const id = uuid.v4();
|
||||
|
||||
// try mounting and unmount on fail
|
||||
await mounts.writeMountFile(volume);
|
||||
[error] = await safe(mounts.waitForMount(mountType, hostPath, { times: 20, interval: 500 })); // 10 seconds
|
||||
if (error) {
|
||||
await safe(mounts.removeMountFile(hostPath));
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
if (mountType !== 'noop') await mounts.writeMountFile(volume);
|
||||
await database.query('INSERT INTO volumes (id, name, hostPath, mountType, mountOptionsJson) VALUES (?, ?, ?, ?, ?)', [ id, name, hostPath, mountType, JSON.stringify(mountOptions) ]);
|
||||
} catch (error) {
|
||||
if (error.code === 'ER_DUP_ENTRY' && error.sqlMessage.indexOf('name') !== -1) throw new BoxError(BoxError.ALREADY_EXISTS, 'name already exists');
|
||||
@@ -131,6 +112,36 @@ async function add(volume, auditSource) {
|
||||
return id;
|
||||
}
|
||||
|
||||
async function update(volume, mountType, mountOptions) {
|
||||
assert.strictEqual(typeof volume, 'object');
|
||||
assert.strictEqual(typeof mountType, 'string');
|
||||
assert.strictEqual(typeof mountOptions, 'object');
|
||||
|
||||
let error = mounts.validateMountOptions(mountType, mountOptions);
|
||||
if (error) throw error;
|
||||
|
||||
if (mountType === 'noop') {
|
||||
await mounts.removeMountFile(volume.hostPath);
|
||||
} else {
|
||||
await mounts.writeMountFile(Object.assign({}, volume, { mountType, mountOptions }));
|
||||
|
||||
[error] = await safe(mounts.waitForMount(mountType, volume.hostPath, { times: 20, interval: 500 })); // 10 seconds
|
||||
if (error) {
|
||||
await safe(mounts.removeMountFile(volume.hostPath));
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const [, result] = await database.query('UPDATE volumes SET mountType=?, mountOptionsJson=? WHERE id=?', [ mountType, JSON.stringify(mountOptions), volume.id ]);
|
||||
if (result.affectedRows !== 1) throw new BoxError(BoxError.NOT_FOUND, 'Volume not found');
|
||||
}
|
||||
|
||||
async function getStatus(volume) {
|
||||
assert.strictEqual(typeof volume, 'object');
|
||||
|
||||
return await mounts.getStatus(volume.mountType, volume.hostPath); // { state, message }
|
||||
}
|
||||
|
||||
async function get(id) {
|
||||
assert.strictEqual(typeof id, 'string');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user