/
deploy.ts
235 lines (204 loc) · 6.62 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright (C) 2018-2020 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import Bluebird from "bluebird"
import chalk from "chalk"
import { includes } from "lodash"
import { LogEntry } from "../logger/log-entry"
import { BaseTask, TaskType, getServiceStatuses, getRunTaskResults } from "./base"
import { Service, ServiceStatus, getLinkUrl } from "../types/service"
import { Garden } from "../garden"
import { TaskTask, getTaskVersion } from "./task"
import { BuildTask } from "./build"
import { ConfigGraph } from "../config-graph"
import { startPortProxies } from "../proxy"
import { TaskResults } from "../task-graph"
import { prepareRuntimeContext } from "../runtime-context"
import { GetServiceStatusTask } from "./get-service-status"
import { GetTaskResultTask } from "./get-task-result"
export interface DeployTaskParams {
garden: Garden
graph: ConfigGraph
service: Service
force: boolean
forceBuild: boolean
fromWatch?: boolean
log: LogEntry
hotReloadServiceNames?: string[]
}
export class DeployTask extends BaseTask {
type: TaskType = "deploy"
private graph: ConfigGraph
private service: Service
private forceBuild: boolean
private fromWatch: boolean
private hotReloadServiceNames: string[]
constructor({
garden,
graph,
log,
service,
force,
forceBuild,
fromWatch = false,
hotReloadServiceNames = [],
}: DeployTaskParams) {
super({ garden, log, force, version: service.module.version })
this.graph = graph
this.service = service
this.forceBuild = forceBuild
this.fromWatch = fromWatch
this.hotReloadServiceNames = hotReloadServiceNames
}
async getDependencies() {
const dg = this.graph
// We filter out service dependencies on services configured for hot reloading (if any)
const deps = await dg.getDependencies({
nodeType: "deploy",
name: this.getName(),
recursive: false,
filterFn: (depNode) => !(depNode.type === "deploy" && includes(this.hotReloadServiceNames, depNode.name)),
})
const statusTask = new GetServiceStatusTask({
garden: this.garden,
graph: this.graph,
log: this.log,
service: this.service,
force: false,
hotReloadServiceNames: this.hotReloadServiceNames,
})
if (this.fromWatch && includes(this.hotReloadServiceNames, this.service.name)) {
// Only need to get existing statuses and results when hot-reloading
const dependencyStatusTasks = deps.deploy.map((service) => {
return new GetServiceStatusTask({
garden: this.garden,
graph: this.graph,
log: this.log,
service,
force: false,
hotReloadServiceNames: this.hotReloadServiceNames,
})
})
const taskResultTasks = await Bluebird.map(deps.run, async (task) => {
return new GetTaskResultTask({
garden: this.garden,
log: this.log,
task,
force: false,
version: await getTaskVersion(this.garden, this.graph, task),
})
})
return [statusTask, ...dependencyStatusTasks, ...taskResultTasks]
} else {
const deployTasks = deps.deploy.map((service) => {
return new DeployTask({
garden: this.garden,
graph: this.graph,
log: this.log,
service,
force: false,
forceBuild: this.forceBuild,
fromWatch: this.fromWatch,
hotReloadServiceNames: this.hotReloadServiceNames,
})
})
const taskTasks = await Bluebird.map(deps.run, (task) => {
return TaskTask.factory({
task,
garden: this.garden,
log: this.log,
graph: this.graph,
force: false,
forceBuild: this.forceBuild,
})
})
const buildTasks = await BuildTask.factory({
garden: this.garden,
log: this.log,
module: this.service.module,
force: this.forceBuild,
})
return [statusTask, ...deployTasks, ...taskTasks, ...buildTasks]
}
}
getName() {
return this.service.name
}
getDescription() {
return `deploying service '${this.service.name}' (from module '${this.service.module.name}')`
}
async process(dependencyResults: TaskResults): Promise<ServiceStatus> {
let version = this.version
const hotReload = includes(this.hotReloadServiceNames, this.service.name)
const dependencies = await this.graph.getDependencies({
nodeType: "deploy",
name: this.getName(),
recursive: false,
})
const serviceStatuses = getServiceStatuses(dependencyResults)
const taskResults = getRunTaskResults(dependencyResults)
// TODO: attach runtimeContext to GetServiceTask output
const runtimeContext = await prepareRuntimeContext({
garden: this.garden,
graph: this.graph,
dependencies,
version: this.version,
serviceStatuses,
taskResults,
})
const actions = await this.garden.getActionRouter()
let status = serviceStatuses[this.service.name]
const { versionString } = version
const log = this.log.info({
status: "active",
section: this.service.name,
msg: `Deploying version ${versionString}...`,
})
if (!this.force && versionString === status.version && status.state === "ready") {
// already deployed and ready
log.setSuccess({
msg: chalk.green("Already deployed"),
append: true,
})
} else {
try {
status = await actions.deployService({
service: this.service,
runtimeContext,
log,
force: this.force,
hotReload,
})
} catch (err) {
log.setError()
throw err
}
log.setSuccess({
msg: chalk.green(`Done (took ${log.getDuration(1)} sec)`),
append: true,
})
}
for (const ingress of status.ingresses || []) {
log.info(chalk.gray("→ Ingress: ") + chalk.underline.gray(getLinkUrl(ingress)))
}
if (this.garden.persistent) {
const proxies = await startPortProxies(this.garden, log, this.service, status)
for (const proxy of proxies) {
const targetHost = proxy.spec.targetHostname || this.service.name
log.info(
chalk.gray(
`→ Forward: ` +
chalk.underline(proxy.localUrl) +
` → ${targetHost}:${proxy.spec.targetPort}` +
(proxy.spec.name ? ` (${proxy.spec.name})` : "")
)
)
}
}
return status
}
}