Skip to content

Commit

Permalink
breaking(baremetal): Throw an error if there is not enough available …
Browse files Browse the repository at this point in the history
…space
  • Loading branch information
Tobbe committed Sep 25, 2024
1 parent b05a9a5 commit fc6cefd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
26 changes: 13 additions & 13 deletions packages/cli/src/commands/deploy/__tests__/baremetal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ describe('serverConfigWithDefaults', () => {
expect(config.branch).toEqual('moon')
})

it('does not provide default freeSpaceRequired', () => {
it('provides default freeSpaceRequired', () => {
const config = baremetal.serverConfigWithDefaults({}, {})
expect(config.freeSpaceRequired).toBeUndefined()
expect(config.freeSpaceRequired).toEqual(2048)
})
})

Expand Down Expand Up @@ -688,9 +688,9 @@ describe('deployTasks', () => {
expect(tasks[0].skip()).toBeTruthy()
})

it('warns if there is not enough available space on the server and freeSpaceRequired is not configured', async () => {
it('throws an error if there is not enough available space on the server and freeSpaceRequired is not configured', () => {
const ssh = {
exec: () => ({ stdout: 'df:1875' }),
exec: () => ({ stdout: 'df:1875893' }),
}

const { freeSpaceRequired: _, ...serverConfig } = defaultServerConfig
Expand All @@ -702,29 +702,29 @@ describe('deployTasks', () => {
{}, // lifecycle
)

await tasks[0].task({}, mockTask)

expect(mockTask.skip).toHaveBeenCalledWith(
expect.stringContaining(
'Warning: Your server is running low on disk space.',
),
expect(() => tasks[0].task({}, {})).rejects.toThrowError(
/Not enough disk space\. You need at least 2048MB free space to continue\. \(Currently 1832MB available\)/,
)
})

it('throws an error if there is less available space on the server than freeSpaceRequired', () => {
const ssh = {
exec: () => ({ stdout: 'df:1875' }),
exec: () => ({ stdout: 'df:3875893' }),
}

const tasks = baremetal.deployTasks(
defaultYargs,
ssh,
{ ...defaultServerConfig, sides: ['api', 'web'] },
{
...defaultServerConfig,
sides: ['api', 'web'],
freeSpaceRequired: 4096,
},
{}, // lifecycle
)

expect(() => tasks[0].task({}, {})).rejects.toThrowError(
/Not enough disk space/,
/Not enough disk space\. You need at least 4096MB free space to continue/,
)
})

Expand Down
15 changes: 4 additions & 11 deletions packages/cli/src/commands/deploy/baremetal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const DEFAULT_SERVER_CONFIG = {
monitorCommand: 'pm2',
sides: ['api', 'web'],
keepReleases: 5,
freeSpaceRequired: 2048,
}

export const command = 'baremetal [environment]'
Expand Down Expand Up @@ -172,7 +173,7 @@ export const verifyServerConfig = (config) => {
throwMissingConfig('repo')
}

if (config.freeSpaceRequired && !/^\d+$/.test(config.freeSpaceRequired)) {
if (!/^\d+$/.test(config.freeSpaceRequired)) {
throw new Error('"freeSpaceRequired" must be an integer >= 0')
}

Expand Down Expand Up @@ -415,18 +416,10 @@ export const deployTasks = (yargs, ssh, serverConfig, serverLifecycle) => {
)

if (dfMb < freeSpaceRequired) {
if (typeof serverConfig.freeSpaceRequired === 'undefined') {
return task.skip(
c.warning(
'Warning: Your server is running low on disk space. (' +
`${Math.round(dfMb)}MB available)`,
),
)
}

throw new Error(
`Not enough disk space. You need at least ${freeSpaceRequired}` +
'MB free space to continue.',
`MB free space to continue. (Currently ${Math.round(dfMb)}MB ` +
'available)',
)
}
},
Expand Down

0 comments on commit fc6cefd

Please sign in to comment.