This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
171 lines (159 loc) · 5.16 KB
/
index.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
'use strict';
const {spawnSync} = require('child_process');
const {Promise} = require('bluebird');
const _ = require('lodash');
/**
* Plugin for Serverless 1.x that drops you to a shell with your env vars!
*/
class ServerlessLocalShell {
/**
* The plugin constructor
* @param {Object} serverless
* @param {Object} options
* makes
* @return {undefined}
*/
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider(
this.serverless.service.provider.name);
this.commands = {
'shell': {
usage: 'Drop to a REPL with your environment variables properly set',
lifecycleEvents: [
'shell',
],
options: {
function: {
usage:
'Specify the function whose environment you want (e.g. ' +
'"--function myFunction")',
shortcut: 'f',
required: false,
type: 'string',
},
shell: {
usage: 'Specify a different shell (e.g. "--shell bash")',
shortcut: 'S',
required: false,
type: 'string',
},
quiet: {
usage:
`Don't output anay log messages besides the subcommand's output`,
shortcut: 'q',
required: false,
default: false,
type: 'boolean',
},
},
},
};
this.hooks = {
'shell:shell': () => Promise.bind(this)
.then(this.loadEnvVars)
.then(this.shell),
};
}
/**
* cribbed from serverless's invoke local (except get rid of lodash)
*/
loadEnvVars() {
try {
// from invoke local's extendedValidate
this.options.functionObj = this.serverless.service.getFunction(
this.options.function);
} catch (e) {
// skip if no function defined
this.options.functionObj = {};
}
const lambdaName = this.options.functionObj.name;
const memorySize = Number(this.options.functionObj.memorySize) ||
Number(this.serverless.service.provider.memorySize) ||
1024;
const lambdaDefaultEnvVars = {
// PATH: '/usr/local/lib64/node-v4.3.x/bin:/usr/local/bin:/usr/bin/:/bin',
LANG: 'en_US.UTF-8',
LD_LIBRARY_PATH: '/usr/local/lib64/node-v4.3.x/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib', // eslint-disable-line max-len
LAMBDA_TASK_ROOT: '/var/task',
LAMBDA_RUNTIME_DIR: '/var/runtime',
AWS_REGION:
this.options.region ||
_.get(this.serverless, 'service.provider.region'),
AWS_DEFAULT_REGION:
this.options.region ||
_.get(this.serverless, 'service.provider.region'),
AWS_LAMBDA_LOG_GROUP_NAME:
this.provider.naming.getLogGroupName(lambdaName),
AWS_LAMBDA_LOG_STREAM_NAME:
'2016/12/02/[$LATEST]f77ff5e4026c45bda9a9ebcec6bc9cad',
AWS_LAMBDA_FUNCTION_NAME: lambdaName,
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: memorySize,
AWS_LAMBDA_FUNCTION_VERSION: '$LATEST',
NODE_PATH: '/var/runtime:/var/task:/var/runtime/node_modules',
};
const providerEnvVars = this.serverless.service.provider.environment || {};
const functionEnvVars = this.options.functionObj.environment || {};
const enterpriseCredsEnvVars = {};
if (this.serverless.service.provider.name === 'aws') {
const {cachedCredentials} = this.serverless.getProvider('aws');
if (cachedCredentials) {
enterpriseCredsEnvVars.AWS_ACCESS_KEY_ID =
cachedCredentials.accessKeyId;
enterpriseCredsEnvVars.AWS_SECRET_ACCESS_KEY =
cachedCredentials.secretAccessKey;
enterpriseCredsEnvVars.AWS_SESSION_TOKEN =
cachedCredentials.sessionToken;
enterpriseCredsEnvVars.AWS_REGION =
cachedCredentials.region;
}
}
Object.assign(
process.env,
lambdaDefaultEnvVars,
providerEnvVars,
functionEnvVars,
enterpriseCredsEnvVars
);
}
/**
* load the right environment variables and start shell
*/
shell() {
let shellBinary;
if (this.options.shell) {
shellBinary = this.options.shell;
} else if (_.has(this.serverless, 'service.custom.shellBinary')) {
shellBinary = this.serverless.service.custom.shellBinary;
} else if (this.serverless.service.provider.runtime.startsWith('nodejs')) {
shellBinary = 'node';
} else {
shellBinary = this.serverless.service.provider.runtime;
}
if (!this.options.quiet) {
this.serverless.cli.log(`Spawning ${shellBinary}...`);
}
if (process.env.SLS_DEBUG) {
// eslint-disable-next-line no-console
console.log(`Setting env: ${JSON.stringify(process.env, null, 2)}`);
}
spawnSync(shellBinary, [], {
env: process.env,
stdio: 'inherit',
shell: true,
});
};
/**
* get the custom.pythonRequirements contents, with defaults set
* @return {Object}
*/
custom() {
return Object.assign({
zip: false,
cleanupZipHelper: true,
}, this.serverless.service.custom &&
this.serverless.service.custom.pythonRequirements || {});
}
}
module.exports = ServerlessLocalShell;