forked from webpod/webpod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
204 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {task} from "../../task.js" | ||
|
||
task('deploy:lock', async ({$, host}) => { | ||
const locked = await $`[ -f ${await host.deployPath}/.webpod/deploy.lock ] && printf +locked || echo ${await host.userStartedDeploy} > ${await host.deployPath}/.webpod/deploy.lock` | ||
if (locked.stdout == '+locked') { | ||
const lockedUser = await $`cat ${await host.deployPath}/.webpod/deploy.lock` | ||
throw new Error( | ||
`Deploy locked by ${lockedUser}. Execute "deploy:unlock" task to unlock.` | ||
) | ||
} | ||
}) | ||
|
||
task('deploy:unlock', async ({$, host}) => { | ||
await $`rm -f ${await host.deployPath}/.webpod/deploy.lock` | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import {define} from "../../host.js" | ||
import * as path from "path" | ||
import {task} from "../../task.js" | ||
import exec from "@webpod/exec" | ||
import {commandSupportsOption} from "../../utils.js" | ||
|
||
type Release = { | ||
createdAt: string | ||
releaseName: string | ||
user: string | ||
rev: string | ||
} | ||
|
||
define('releaseName', async ({$, host}) => { | ||
const latest = await $`cd ${host.deployPath} cat .webpod/latest_release || echo 0` | ||
return (parseInt(latest.stdout) + 1).toString() | ||
}) | ||
|
||
define('releasesList', async ({$, host}) => { | ||
$.cd(await host.deployPath) | ||
|
||
// If there is no releases return an empty list. | ||
if (!await $.test`[ -d releases ] && [ "$(ls -A releases)" ]`) { | ||
return [] | ||
} | ||
|
||
// Will list only dirs in releases. | ||
const ll = (await $`cd releases && ls -t -1 -d */`) | ||
.split('\n') | ||
.map(x => x.trim().replace(/\/$/, '')) | ||
.map(x => path.basename(x)) | ||
|
||
// Return releases from newest to oldest. | ||
if (!await $.test`[ -f .webpod/releases_log ]`) { | ||
return [] | ||
} | ||
|
||
const releaseLogs: Release[] = (await $`tail -n 300 .webpod/releases_log`) | ||
.split('\n') | ||
.map(x => JSON.parse(x)) | ||
.filter(x => x) | ||
|
||
const releases = [] | ||
for (const {releaseName} of releaseLogs) { | ||
if (ll.includes(releaseName)) { | ||
releases.push(releaseName) | ||
} | ||
} | ||
|
||
return releases | ||
}) | ||
|
||
// Return release path. | ||
define('releasesPath', async ({$, host}) => { | ||
const releaseExists = await $.test`[ -h ${host.deployPath}/release ]` | ||
if (releaseExists) { | ||
const link = await $`readlink ${host.deployPath}/release` | ||
return link[0] === '/' ? link.toString() : `${host.deployPath}/${link}` | ||
} else { | ||
throw new Error(`The "release_path" (${host.deployPath}/release) does not exist.`) | ||
} | ||
}) | ||
|
||
// Current release revision. Usually a git hash. | ||
define('releaseRevision', async ({$, host}) => { | ||
return (await $`cat ${host.releasesPath}/REVISION`).toString(); | ||
}); | ||
|
||
define('useRelativeSymlink', async ({$}) => { | ||
return commandSupportsOption($, 'ln', '--relative') | ||
}) | ||
|
||
define('binSymlink', async ({host}) => { | ||
return host.useRelativeSymlink ? ['ln', '-nfs', '--relative'] : ['ln', '-nfs'] | ||
}) | ||
|
||
// Return the release path during a deployment | ||
// but fallback to the current path otherwise. | ||
define('releaseOrCurrentPath', async ({$, host}) => { | ||
const releaseExists = await $.test`[ -h ${host.deployPath}/release ]` | ||
return releaseExists ? await host.releasesPath: await host.currentPath; | ||
}); | ||
|
||
// Clean up unfinished releases and prepare next release | ||
task('deploy:release', async ({host, $}) => { | ||
$.cd(await host.deployPath) | ||
|
||
// Clean up if there is unfinished release. | ||
if (await $.test`[ -h release ]`) { | ||
await $`rm release` | ||
} | ||
|
||
// We need to get releasesList at same point as releaseName, | ||
// as standard releaseName's implementation depends on it and, | ||
// if user overrides it, we need to get releasesList manually. | ||
const releasesList = await host.releasesList | ||
const releaseName = await host.releaseName | ||
const releasePath = 'releases/' + releaseName; | ||
|
||
// Check what there is no such release path. | ||
if (await $.test`[ -d ${releasePath} ]`) { | ||
throw new Error(`Release name "${releaseName}" already exists.`) | ||
} | ||
|
||
// Save release_name if it is a number. | ||
if (releaseName.match(/^\d+$/)) { | ||
await $`echo ${releaseName} > .webpod/latest_release` | ||
} | ||
|
||
// Save release info. | ||
const metainfo: Release = { | ||
createdAt: new Date().toISOString(), | ||
releaseName: releaseName, | ||
user: await host.userStartedDeploy, | ||
rev: exec.git('rev-parse', 'HEAD').trim(), | ||
} | ||
await $`echo ${JSON.stringify(metainfo)} >> .webpod/releases_log`; | ||
|
||
// Make new release. | ||
await $`mkdir -p ${releasePath}` | ||
await $`${host.binSymlink} ${releasePath} ${host.deployPath}/release` | ||
|
||
// Add to releases list. | ||
releasesList.unshift(releaseName) | ||
host.releasesList = releasesList | ||
|
||
// Set previous_release. | ||
if (releasesList[1]) { | ||
host.previousReleasePath = `${await host.deployPath}/releases/${releasesList[1]}` | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {task} from '../../task.js' | ||
|
||
task('deploy:setup', async ({host, $}) => { | ||
await $` | ||
[ -d ${await host.deployPath} ] || mkdir -p ${await host.deployPath}; | ||
cd ${await host.deployPath}; | ||
[ -d .webpod ] || mkdir .webpod; | ||
[ -d releases ] || mkdir releases; | ||
[ -d shared ] || mkdir shared; | ||
` | ||
// If current_path points to something like "/var/www/html", make sure it is | ||
// a symlink and not a directory. | ||
if (await $.test`[ ! -L ${await host.currentPath} ] && [ -d ${await host.currentPath} ]`) { | ||
throw new Error( | ||
`There is a directory (not symlink) at {{current_path}}.\n` + | ||
`Remove this directory so it can be replaced with a symlink for atomic deployments.` | ||
); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters