@naturalcycles/backend-lib 9.78.0 → 9.80.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/deploy/deployHealthCheck.d.ts +1 -5
- package/dist/deploy/deployHealthCheck.js +3 -34
- package/dist/deploy/index.d.ts +2 -9
- package/dist/deploy/index.js +1 -6
- package/dist/express/createDefaultApp.js +2 -2
- package/dist/server/compressionMiddleware.d.ts +14 -1
- package/dist/server/compressionMiddleware.js +81 -27
- package/dist/server/serverStatusMiddleware.d.ts +2 -2
- package/dist/server/serverStatusMiddleware.js +5 -14
- package/dist/util.d.ts +0 -1
- package/dist/util.js +1 -4
- package/package.json +3 -13
- package/src/deploy/deployHealthCheck.ts +2 -48
- package/src/deploy/index.ts +2 -17
- package/src/express/createDefaultApp.ts +2 -2
- package/src/server/compressionMiddleware.ts +107 -28
- package/src/server/serverStatusMiddleware.ts +4 -20
- package/src/util.ts +1 -5
- package/bin/deploy-gae.js +0 -2
- package/bin/deploy-prepare.js +0 -2
- package/bin/undeploy-gae.js +0 -2
- package/dist/bin/deploy-gae.d.ts +0 -1
- package/dist/bin/deploy-gae.js +0 -12
- package/dist/bin/deploy-prepare.d.ts +0 -1
- package/dist/bin/deploy-prepare.js +0 -19
- package/dist/bin/undeploy-gae.d.ts +0 -1
- package/dist/bin/undeploy-gae.js +0 -13
- package/dist/deploy/backend.cfg.util.d.ts +0 -30
- package/dist/deploy/backend.cfg.util.js +0 -23
- package/dist/deploy/deploy.model.d.ts +0 -29
- package/dist/deploy/deploy.model.js +0 -1
- package/dist/deploy/deploy.util.d.ts +0 -7
- package/dist/deploy/deploy.util.js +0 -151
- package/dist/deploy/deployGae.d.ts +0 -10
- package/dist/deploy/deployGae.js +0 -90
- package/dist/deploy/deployInfo.util.d.ts +0 -1
- package/dist/deploy/deployInfo.util.js +0 -25
- package/dist/deploy/deployPrepare.d.ts +0 -13
- package/dist/deploy/deployPrepare.js +0 -85
- package/src/bin/deploy-gae.ts +0 -14
- package/src/bin/deploy-prepare.ts +0 -24
- package/src/bin/undeploy-gae.ts +0 -15
- package/src/deploy/backend.cfg.util.ts +0 -63
- package/src/deploy/deploy.model.ts +0 -32
- package/src/deploy/deploy.util.ts +0 -214
- package/src/deploy/deployGae.ts +0 -127
- package/src/deploy/deployInfo.util.ts +0 -27
- package/src/deploy/deployPrepare.ts +0 -113
- package/src/deploy/files-default/.gcloudignore +0 -7
package/src/deploy/deployGae.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { _anyToError } from '@naturalcycles/js-lib/error'
|
|
2
|
-
import { pRetry } from '@naturalcycles/js-lib/promise/pRetry.js'
|
|
3
|
-
import { _objectAssign } from '@naturalcycles/js-lib/types'
|
|
4
|
-
import { appendToGithubSummary } from '@naturalcycles/nodejs-lib/env'
|
|
5
|
-
import { exec2 } from '@naturalcycles/nodejs-lib/exec2'
|
|
6
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
7
|
-
import { getBackendCfg } from './backend.cfg.util.js'
|
|
8
|
-
import { createDeployInfo } from './deploy.util.js'
|
|
9
|
-
import type { DeployHealthCheckOptions } from './deployHealthCheck.js'
|
|
10
|
-
import { deployHealthCheck } from './deployHealthCheck.js'
|
|
11
|
-
import type { DeployPrepareOptions } from './deployPrepare.js'
|
|
12
|
-
import { deployPrepare } from './deployPrepare.js'
|
|
13
|
-
|
|
14
|
-
export interface DeployGaeOptions extends DeployPrepareOptions, DeployHealthCheckOptions {}
|
|
15
|
-
|
|
16
|
-
export async function deployGae(opt: DeployGaeOptions = {}): Promise<void> {
|
|
17
|
-
const { logOnFailure, logOnSuccess } = opt
|
|
18
|
-
|
|
19
|
-
// 1. build
|
|
20
|
-
|
|
21
|
-
const packageManager = detectPackageManager()
|
|
22
|
-
|
|
23
|
-
exec2.spawn(`${packageManager} build`)
|
|
24
|
-
|
|
25
|
-
// 2. deploy-prepare
|
|
26
|
-
|
|
27
|
-
const deployInfo = await deployPrepare()
|
|
28
|
-
|
|
29
|
-
const targetDir = './tmp/deploy'
|
|
30
|
-
const appYamlPath = `${targetDir}/app.yaml`
|
|
31
|
-
|
|
32
|
-
const { gaeProject, gaeService, gaeVersion, versionUrl, serviceUrl } = deployInfo
|
|
33
|
-
_objectAssign(opt, {
|
|
34
|
-
gaeProject,
|
|
35
|
-
gaeService,
|
|
36
|
-
gaeVersion,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
await pRetry(
|
|
40
|
-
async () => {
|
|
41
|
-
try {
|
|
42
|
-
exec2.spawn(
|
|
43
|
-
`gcloud app deploy ${appYamlPath} --project ${gaeProject} --version ${gaeVersion} --quiet --no-promote`,
|
|
44
|
-
)
|
|
45
|
-
} catch (err) {
|
|
46
|
-
if (logOnFailure) {
|
|
47
|
-
logs(gaeProject, gaeService, gaeVersion)
|
|
48
|
-
}
|
|
49
|
-
throw err
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
{
|
|
53
|
-
name: 'deploy',
|
|
54
|
-
maxAttempts: 2,
|
|
55
|
-
delay: 30_000,
|
|
56
|
-
// todo: this doesn't work, as the error is different from what is logged.
|
|
57
|
-
// We shoud somehow capture the logged text
|
|
58
|
-
predicate: err => _anyToError(err).message.includes('operation is already in progress'),
|
|
59
|
-
},
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
// Health check (versionUrl)
|
|
63
|
-
// yarn deploy-health-check --url $deployInfo_versionUrl --repeat 3 --timeoutSec 180 --intervalSec 2
|
|
64
|
-
await deployHealthCheck(versionUrl, opt)
|
|
65
|
-
|
|
66
|
-
// Only if "timestamped version" is used ('1' is default)
|
|
67
|
-
if (gaeVersion !== '1') {
|
|
68
|
-
// Rollout (promote versionUrl to serviceUrl)
|
|
69
|
-
// gcloud app services set-traffic $deployInfo_gaeService --project $deployInfo_gaeProject --splits $deployInfo_gaeVersion=1 --quiet
|
|
70
|
-
exec2.spawn(
|
|
71
|
-
`gcloud app services set-traffic ${gaeService} --project ${gaeProject} --splits ${gaeVersion}=1 --quiet`,
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
// Health check (serviceUrl)
|
|
75
|
-
// yarn deploy-health-check --url $deployInfo_serviceUrl --repeat 3 --timeoutSec 60 --intervalSec 2
|
|
76
|
-
await deployHealthCheck(serviceUrl, opt)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
appendToGithubSummary(`Deployed ${serviceUrl}`)
|
|
80
|
-
|
|
81
|
-
// Logs
|
|
82
|
-
if (logOnSuccess) {
|
|
83
|
-
logs(gaeProject, gaeService, gaeVersion)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Undeploys/removes the GAE service/version, using the same rules as deployGae.
|
|
89
|
-
* Detects the service/version by the same criteria: branch name, backend.cfg.yaml, etc.
|
|
90
|
-
*/
|
|
91
|
-
export async function undeployGae(branch: string): Promise<void> {
|
|
92
|
-
const projectDir = '.'
|
|
93
|
-
const backendCfg = getBackendCfg(projectDir)
|
|
94
|
-
|
|
95
|
-
const { gaeProject, gaeService, gaeVersion } = await createDeployInfo(backendCfg, branch)
|
|
96
|
-
|
|
97
|
-
// if (prod) {
|
|
98
|
-
// console.log(`undeployGae (branch: ${branch}): not removing prod version (safety check)`)
|
|
99
|
-
// return
|
|
100
|
-
// }
|
|
101
|
-
|
|
102
|
-
console.log(
|
|
103
|
-
`undeployGae (branch: ${branch}): going to remove ${gaeProject}/${gaeService}/${gaeVersion}`,
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
exec2.spawn(
|
|
107
|
-
`gcloud app versions delete --project ${gaeProject} --service ${gaeService} ${gaeVersion} --quiet`,
|
|
108
|
-
)
|
|
109
|
-
|
|
110
|
-
appendToGithubSummary(`removed ${gaeProject}/${gaeService}/${gaeVersion}`)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function logs(gaeProject: string, gaeService: string, gaeVersion: string): void {
|
|
114
|
-
try {
|
|
115
|
-
exec2.spawn(
|
|
116
|
-
`gcloud app logs read --project ${gaeProject} --service ${gaeService} --version ${gaeVersion}`,
|
|
117
|
-
)
|
|
118
|
-
} catch {}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function detectPackageManager(): string {
|
|
122
|
-
const pj = fs2.readText('package.json')
|
|
123
|
-
if (pj.includes('pnpm')) {
|
|
124
|
-
return 'pnpm'
|
|
125
|
-
}
|
|
126
|
-
return 'yarn' // yarn is still the default
|
|
127
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
|
|
2
|
-
import { _memoized } from '@naturalcycles/js-lib/decorators'
|
|
3
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
4
|
-
import type { DeployInfo } from './deploy.model.js'
|
|
5
|
-
|
|
6
|
-
export const getDeployInfo = _memoized((projectDir: string): DeployInfo => {
|
|
7
|
-
const deployInfoPath = `${projectDir}/deployInfo.json`
|
|
8
|
-
try {
|
|
9
|
-
return fs2.readJson(deployInfoPath)
|
|
10
|
-
} catch {
|
|
11
|
-
// console.error(`cannot read ${deployInfoPath}, returning empty version`)
|
|
12
|
-
return getDeployInfoStub()
|
|
13
|
-
}
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
function getDeployInfoStub(stub = ''): DeployInfo {
|
|
17
|
-
return {
|
|
18
|
-
gaeProject: stub,
|
|
19
|
-
gaeService: stub,
|
|
20
|
-
gaeVersion: stub,
|
|
21
|
-
serviceUrl: stub,
|
|
22
|
-
versionUrl: stub,
|
|
23
|
-
gitBranch: stub,
|
|
24
|
-
gitRev: stub,
|
|
25
|
-
ts: localTime.nowUnix(),
|
|
26
|
-
}
|
|
27
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { loadEnvFileIfExists } from '@naturalcycles/nodejs-lib'
|
|
2
|
-
import type { CliOptions } from '@naturalcycles/nodejs-lib/args'
|
|
3
|
-
import { dimGrey } from '@naturalcycles/nodejs-lib/colors'
|
|
4
|
-
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
5
|
-
import { kpySync } from '@naturalcycles/nodejs-lib/kpy'
|
|
6
|
-
import { srcDir } from '../paths.cnst.js'
|
|
7
|
-
import { getBackendCfg } from './backend.cfg.util.js'
|
|
8
|
-
import type { DeployInfo } from './deploy.model.js'
|
|
9
|
-
import { createAndSaveAppYaml, createAndSaveDeployInfo } from './deploy.util.js'
|
|
10
|
-
|
|
11
|
-
export interface DeployPrepareOptions {
|
|
12
|
-
projectDir?: string
|
|
13
|
-
targetDir?: string
|
|
14
|
-
createNpmrc?: boolean
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Comma-separated list of env variables that will be passed to app.yaml from process.env.
|
|
18
|
-
* Use it to pass secrets.
|
|
19
|
-
*/
|
|
20
|
-
appYamlPassEnv?: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const deployPrepareCliOptions = {
|
|
24
|
-
projectDir: {
|
|
25
|
-
type: 'string',
|
|
26
|
-
default: '.',
|
|
27
|
-
},
|
|
28
|
-
targetDir: {
|
|
29
|
-
type: 'string',
|
|
30
|
-
default: `./tmp/deploy`,
|
|
31
|
-
},
|
|
32
|
-
createNpmrc: {
|
|
33
|
-
type: 'boolean',
|
|
34
|
-
desc: 'Create .npmrc in targetDir if NPM_TOKEN env var is set',
|
|
35
|
-
default: true,
|
|
36
|
-
},
|
|
37
|
-
appYamlPassEnv: {
|
|
38
|
-
type: 'string',
|
|
39
|
-
desc: 'Comma-separated list of env variables that will be passed to app.yaml from process.env',
|
|
40
|
-
},
|
|
41
|
-
} as const satisfies CliOptions
|
|
42
|
-
|
|
43
|
-
const DEFAULT_FILES = [
|
|
44
|
-
'dist',
|
|
45
|
-
// '!dist/test',
|
|
46
|
-
// '!dist/**/*.test.*',
|
|
47
|
-
// '!dist/**/*.mock.*',
|
|
48
|
-
'!dist/**/*.script.*',
|
|
49
|
-
'src', // For Sentry
|
|
50
|
-
'!src/test',
|
|
51
|
-
'!src/**/*.test.*',
|
|
52
|
-
'!src/**/*.mock.*',
|
|
53
|
-
'!src/**/*.script.*',
|
|
54
|
-
'!**/__snapshots__',
|
|
55
|
-
'!**/__exclude',
|
|
56
|
-
'static',
|
|
57
|
-
'secret/**/*.enc', // encrypted secrets
|
|
58
|
-
'package.json',
|
|
59
|
-
'yarn.lock',
|
|
60
|
-
'.yarnrc',
|
|
61
|
-
'pnpm-lock.yaml',
|
|
62
|
-
'pnpm-workspace.yaml',
|
|
63
|
-
// 'tsconfig.json', // for path-mapping to work!
|
|
64
|
-
// 'tsconfig.dist.json',
|
|
65
|
-
'.gcloudignore',
|
|
66
|
-
'app.yaml',
|
|
67
|
-
'patches', // to allow patch-package
|
|
68
|
-
'resources',
|
|
69
|
-
]
|
|
70
|
-
|
|
71
|
-
const defaultFilesDir = `${srcDir}/deploy/files-default`
|
|
72
|
-
|
|
73
|
-
export async function deployPrepare(opt: DeployPrepareOptions = {}): Promise<DeployInfo> {
|
|
74
|
-
loadEnvFileIfExists()
|
|
75
|
-
|
|
76
|
-
const { projectDir = '.', targetDir = './tmp/deploy', createNpmrc = true } = opt
|
|
77
|
-
|
|
78
|
-
const backendCfg = getBackendCfg(projectDir)
|
|
79
|
-
const inputPatterns = backendCfg.files || DEFAULT_FILES
|
|
80
|
-
const appYamlPassEnv = opt.appYamlPassEnv || backendCfg.appYamlPassEnv
|
|
81
|
-
|
|
82
|
-
console.log(`1. Copy files to ${dimGrey(targetDir)}`)
|
|
83
|
-
|
|
84
|
-
// Clean targetDir
|
|
85
|
-
fs2.emptyDir(targetDir)
|
|
86
|
-
|
|
87
|
-
kpySync({
|
|
88
|
-
baseDir: defaultFilesDir,
|
|
89
|
-
outputDir: targetDir,
|
|
90
|
-
dotfiles: true,
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
kpySync({
|
|
94
|
-
baseDir: projectDir,
|
|
95
|
-
inputPatterns,
|
|
96
|
-
outputDir: targetDir,
|
|
97
|
-
dotfiles: true,
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
const { NPM_TOKEN } = process.env
|
|
101
|
-
if (NPM_TOKEN && createNpmrc) {
|
|
102
|
-
const npmrcPath = `${targetDir}/.npmrc`
|
|
103
|
-
const npmrc = `//registry.npmjs.org/:_authToken=${NPM_TOKEN}`
|
|
104
|
-
fs2.writeFile(npmrcPath, npmrc)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
console.log(`2. Generate ${dimGrey('deployInfo.json')} and ${dimGrey('app.yaml')} in targetDir`)
|
|
108
|
-
|
|
109
|
-
const deployInfo = await createAndSaveDeployInfo(backendCfg, targetDir)
|
|
110
|
-
createAndSaveAppYaml(backendCfg, deployInfo, projectDir, targetDir, appYamlPassEnv)
|
|
111
|
-
|
|
112
|
-
return deployInfo
|
|
113
|
-
}
|