Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"disposition": "PATCH",
"forecast_commit": "b46f7f3d59cb56c3fa0c69297b43842f3df278b7",
"reviewed_sha": "cee9cad88b7be35ebc330506254d1c0016fda56a",
"schema": "limetech.ai-review-marker.v2",
"stage": "final",
"unresolved_proportionality_findings": []
}
55 changes: 55 additions & 0 deletions api/src/core/utils/clients/emcmd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,61 @@ describe('emcmd', () => {
);
});

it.each([
[
'create-pool-storage-active',
{ cmdCreatePool: 'apply', poolName: 'cache' },
'Stop storage before creating pool "cache".',
],
[
'create-pool-storage-active',
{ cmdCreatePool: 'apply', poolName: 'cold.tier' },
'Stop storage before creating pool "cold.tier".',
],
[
'make-bootable-size',
{ cmdMakeBootable: 'apply', poolName: 'fast~cache' },
'Check the reserved boot size and available drive space for pool "fast~cache" (storage slot 30).',
],
[
'create-pool-storage-active',
{ cmdCreatePool: 'apply', poolName: 'cold.tier/private' },
'Stop storage before creating the selected pool.',
],
[
'create-pool-invalid',
{ cmdCreatePool: 'apply', poolName: 'cold.tier.' },
'Check the name and slot count for the selected pool.',
],
[
'create-pool-invalid',
{ cmdCreatePool: 'apply', poolName: 'a'.repeat(41) },
'Check the name and slot count for the selected pool.',
],
[
'make-bootable-size',
{ cmdMakeBootable: 'apply', poolName: 'cache' },
'Check the reserved boot size and available drive space for pool "cache" (storage slot 30).',
],
[
'make-bootable-size',
{ cmdCreatePool: 'apply', poolName: 'cache' },
'emcmd request failed with status 500',
],
[
'unrecognized',
{ cmdMakeBootable: 'apply', poolName: 'cache' },
'emcmd request failed with status 500',
],
] as const)('renders the matching native failure %s', async (result, command, expected) => {
vi.mocked(got.post).mockResolvedValue({
body: 'private daemon diagnostic',
statusCode: 500,
headers: { 'x-unraid-command-result': result, 'x-unraid-storage-slot': '30' },
} as unknown as Awaited<ReturnType<typeof got.post>>);
await expect(emcmd(command)).rejects.toMatchObject({ message: expected });
});

it('throws on http failures reported by got', async () => {
vi.mocked(got.post).mockResolvedValue({
body: '',
Expand Down
9 changes: 9 additions & 0 deletions api/src/core/utils/clients/emcmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import retry from 'p-retry';
import { AppError } from '@app/core/errors/app-error.js';
import { appLogger } from '@app/core/log.js';
import { LooseObject } from '@app/core/types/global.js';
import { StorageCommandError } from '@app/core/utils/clients/storage-command-error.js';
import { store } from '@app/store/index.js';
import { loadSingleStateFile } from '@app/store/modules/emhttp.js';
import { StateFileKey } from '@app/store/types.js';
Expand Down Expand Up @@ -121,6 +122,14 @@ export const emcmd = async (
throwHttpErrors: false,
});

if (response.statusCode === 500) {
const failure = StorageCommandError.fromReply(
commands,
response.headers?.['x-unraid-command-result'],
response.headers?.['x-unraid-storage-slot']
);
if (failure) throw failure;
}
if (response.statusCode >= 400) {
throw new Error(`emcmd request failed with status ${response.statusCode}`);
}
Expand Down
75 changes: 75 additions & 0 deletions api/src/core/utils/clients/storage-command-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/** Only a known native result for the command submitted may enter onboarding output. */
export class StorageCommandError extends Error {
static fromReply(
commands: Record<string, unknown>,
result: unknown,
slot: unknown
): StorageCommandError | undefined {
const pool = commands.poolName;
const target =
typeof pool === 'string' && /^[a-z](?:[a-z0-9~._-]{0,38}[a-z_-])?$/.test(pool)
? `pool "${pool}"`
: 'the selected pool';

if (commands.cmdCreatePool === 'apply') {
switch (result) {
case 'create-pool-storage-active':
return new StorageCommandError(`Stop storage before creating ${target}.`);
case 'create-pool-invalid':
return new StorageCommandError(`Check the name and slot count for ${target}.`);
case 'create-pool-failed':
return new StorageCommandError(
`Could not create ${target}. Check the boot device and pool configuration.`
);
case 'create-pool-refresh-failed':
return new StorageCommandError(
`Storage could not refresh after creating ${target}. Reload Storage and check the pool before retrying.`
);
}
}

if (commands.cmdMakeBootable === 'apply') {
const device =
typeof slot === 'string' && /^(0|[1-9]\d{0,3})$/.test(slot) && Number(slot) <= 4094
? `${target} (storage slot ${slot})`
: target;
switch (result) {
case 'make-bootable-storage-active':
return new StorageCommandError(`Stop storage before making ${device} bootable.`);
case 'make-bootable-invalid':
return new StorageCommandError(
`Check the boot pool and reserved size for ${device}.`
);
case 'make-bootable-ineligible':
return new StorageCommandError(
`Check boot eligibility and the assigned drives for ${device}.`
);
case 'make-bootable-size':
return new StorageCommandError(
`Check the reserved boot size and available drive space for ${device}.`
);
case 'make-bootable-target-missing':
return new StorageCommandError(
`The selected drive for ${device} disappeared before boot setup began. Check its connection and assignment.`
);
case 'make-bootable-partial':
return new StorageCommandError(
`Boot setup may have changed a drive in ${device}. Check the drive partitions, boot pool and pending transfer before retrying.`
);
case 'make-bootable-transfer-pending':
return new StorageCommandError(
`A boot transfer is already pending. Check the existing boot setup before changing ${device}.`
);
case 'make-bootable-refresh-failed':
return new StorageCommandError(
`Storage could not refresh after boot setup for ${device}. Reload Storage and check the boot configuration before retrying.`
);
case 'make-bootable-failed':
return new StorageCommandError(
`Boot setup did not finish for ${device}. Check the drives and boot configuration before retrying.`
);
}
}
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { execa } from 'execa';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { emcmd } from '@app/core/utils/clients/emcmd.js';
import { StorageCommandError } from '@app/core/utils/clients/storage-command-error.js';
import { getShares } from '@app/core/utils/shares/get-shares.js';
import { getters } from '@app/store/index.js';
import { loadStateFileSync } from '@app/store/services/state-file-loader.js';
Expand Down Expand Up @@ -471,4 +472,23 @@ describe('OnboardingInternalBootService', () => {
expect(result.output).toContain('socket failure');
expect(internalBootStateService.invalidateCachedInternalBootDeviceState).not.toHaveBeenCalled();
});
it('returns only the actionable native reason after a boot setup failure', async () => {
const service = createService();
vi.mocked(emcmd).mockResolvedValueOnce({} as Awaited<ReturnType<typeof emcmd>>);
vi.mocked(emcmd).mockRejectedValueOnce(
new StorageCommandError('Stop storage before creating pool "cache".')
);
const result = await service.createInternalBootPool({
poolName: 'cache',
devices: ['disk-1'],
bootSizeMiB: 8192,
updateBios: false,
});
expect(result).toEqual({
ok: false,
code: 1,
output: 'Stop storage before creating pool "cache".',
});
expect(vi.mocked(emcmd)).toHaveBeenCalledTimes(2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
OnboardingInternalBootResult,
} from '@app/unraid-api/graph/resolvers/onboarding/onboarding.model.js';
import { emcmd } from '@app/core/utils/clients/emcmd.js';
import { StorageCommandError } from '@app/core/utils/clients/storage-command-error.js';
import { withTimeout } from '@app/core/utils/misc/with-timeout.js';
import { getShares } from '@app/core/utils/shares/get-shares.js';
import { getters } from '@app/store/index.js';
Expand Down Expand Up @@ -605,6 +606,9 @@ export class OnboardingInternalBootService {
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
this.logger.warn(`createInternalBootPool failed with error='${message}'`);
if (error instanceof StorageCommandError) {
return { ok: false, code: 1, output: error.message };
}
output.push('mkbootpool: command failed or timed out');
output.push(message);
return {
Expand Down
Loading