-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
deploy.js
187 lines (169 loc) · 6.43 KB
/
deploy.js
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
'use strict';
const Serverless = require('../../Serverless');
const chalk = require('chalk');
const { confirm, doesServiceInstanceHaveLinkedProvider } = require('./utils');
const _ = require('lodash');
const overrideStdoutWrite = require('process-utils/override-stdout-write');
const { getDashboardInteractUrl } = require('@serverless/dashboard-plugin/lib/dashboard');
const AWS = require('aws-sdk');
const isAuthenticated = require('@serverless/dashboard-plugin/lib/isAuthenticated');
const printMessage = ({
serviceName,
hasBeenDeployed,
dashboardPlugin,
isConfiguredWithDashboard,
}) => {
if (isConfiguredWithDashboard) {
if (hasBeenDeployed) {
process.stdout.write(
[
`\n${chalk.green('Your project is live and available in ')}${chalk.white.bold(
`./${serviceName}`
)}`,
`\n Run ${chalk.bold('serverless info')} in the project directory`,
' View your endpoints and services',
`\n Open ${chalk.bold(getDashboardInteractUrl(dashboardPlugin))}`,
' Invoke your functions and view logs in the dashboard',
`\n Run ${chalk.bold('serverless deploy')} in the project directory`,
" Redeploy your service after you've updated your service code or configuration\n\n",
].join('\n')
);
return;
}
process.stdout.write(
[
`\n${chalk.green(
'Your project is ready for deployment and available in '
)}${chalk.white.bold(`./${serviceName}`)}`,
`\n Run ${chalk.bold('serverless deploy')} in the project directory`,
' Deploy your newly created service',
`\n Run ${chalk.bold('serverless info')} in the project directory after deployment`,
' View your endpoints and services',
'\n Open Serverless Dashboard after deployment',
' Invoke your functions and view logs in the dashboard\n\n',
].join('\n')
);
return;
}
if (hasBeenDeployed) {
process.stdout.write(
[
`\n${chalk.green('Your project is live and available in ')}${chalk.white.bold(
`./${serviceName}`
)}`,
`\n Run ${chalk.bold('serverless info')} in the project directory`,
' View your endpoints and services',
`\n Run ${chalk.bold('serverless deploy')} in the directory`,
" Redeploy your service after you've updated your service code or configuration",
`\n Run ${chalk.bold('serverless invoke')} and ${chalk.bold(
'serverless logs'
)} in the project directory`,
' Invoke your functions directly and view the logs',
`\n Run ${chalk.bold('serverless')} in the project directory`,
' Add metrics, alerts, and a log explorer, by enabling the dashboard functionality\n\n',
].join('\n')
);
return;
}
process.stdout.write(
[
`\n${chalk.green('Your project is ready for deployment and available in ')}${chalk.white.bold(
`./${serviceName}`
)}`,
`\n Run ${chalk.bold('serverless deploy')} in the project directory`,
' Deploy your newly created service',
`\n Run ${chalk.bold('serverless info')} in the project directory after deployment`,
' View your endpoints and services',
`\n Run ${chalk.bold('serverless invoke')} and ${chalk.bold(
'serverless logs'
)} in the project directory after deployment`,
' Invoke your functions directly and view the logs',
`\n Run ${chalk.bold('serverless')} in the project directory`,
' Add metrics, alerts, and a log explorer, by enabling the dashboard functionality\n\n',
].join('\n')
);
};
const configurePlugin = (serverless, originalStdWrite) => {
serverless.pluginManager.addPlugin(require('./deploy-progress-plugin'));
const interactivePlugin = serverless.pluginManager.plugins.find(
(plugin) => plugin.constructor.name === 'InteractiveDeployProgress'
);
interactivePlugin.progress._writeOriginalStdout = (data) => originalStdWrite(data);
return interactivePlugin;
};
module.exports = {
async isApplicable(context) {
const { configuration, serviceDir, history, options } = context;
if (!serviceDir) {
context.inapplicabilityReasonCode = 'NOT_IN_SERVICE_DIRECTORY';
return false;
}
if (
_.get(configuration, 'provider') !== 'aws' &&
_.get(configuration, 'provider.name') !== 'aws'
) {
context.inapplicabilityReasonCode = 'NON_AWS_PROVIDER';
return false;
}
// If `awsCredentials` step was not executed, we should proceed as it means that user has available credentials
if (!history.has('awsCredentials')) return true;
// We want to proceed if the service instance has a linked provider
if (
configuration.org &&
isAuthenticated() &&
(await doesServiceInstanceHaveLinkedProvider({ configuration, options }))
) {
return true;
}
// We want to proceed if local credentials are available
if (new AWS.Config().credentials) return true;
context.inapplicabilityReasonCode = 'NO_CREDENTIALS_CONFIGURED';
return false;
},
async run({ configuration, configurationFilename, serviceDir, stepHistory }) {
const serviceName = configuration.service;
const shouldDeploy = await confirm('Do you want to deploy your project?', {
name: 'shouldDeploy',
});
stepHistory.set('shouldDeploy', shouldDeploy);
if (!shouldDeploy) {
printMessage({
serviceName,
hasBeenDeployed: false,
isConfiguredWithDashboard: Boolean(configuration.org),
});
return;
}
const serverless = new Serverless({
configuration,
serviceDir,
configurationFilename,
isConfigurationResolved: true,
hasResolvedCommandsExternally: true,
isTelemetryReportedExternally: true,
commands: ['deploy'],
options: {},
});
let interactiveOutputPlugin;
try {
await overrideStdoutWrite(
() => {},
async (originalStdWrite) => {
await serverless.init();
interactiveOutputPlugin = configurePlugin(serverless, originalStdWrite);
await serverless.run();
}
);
} catch (err) {
interactiveOutputPlugin.handleError();
throw err;
}
printMessage({
serviceName,
hasBeenDeployed: true,
isConfiguredWithDashboard: Boolean(configuration.org),
dashboardPlugin: serverless.pluginManager.dashboardPlugin,
});
},
configuredQuestions: ['shouldDeploy'],
};