forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs-centos7.yml
310 lines (305 loc) · 9.92 KB
/
nodejs-centos7.yml
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 在ECS(CentOS 7)上自动部署Node.js环境,含VPC、安全组配置,安装Node.js,提供示例应用。
en: Automatically deploy a Node.js environment on an ECS (CentOS 7) instance, including
configuration of VPC (Virtual Private Cloud), Security Groups, installation of
Node.js, and provision of a sample application.
Conditions:
CreateInstance:
Fn::Equals:
- Ref: SelectInstance
- false
Parameters:
SelectInstance:
Type: Boolean
Label:
en: Whether to select an existing instance
zh-cn: 是否选择已有实例
Default: true
InstanceId:
Type: String
Label:
en: ECS Instance ID
zh-cn: ECS实例ID
AssociationProperty: ALIYUN::ECS::Instance::InstanceId
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Equals:
- ${SelectInstance}
- true
Default:
ZoneId:
Type: String
Label:
en: Zone ID
zh-cn: 可用区ID
Description:
en: The available zone ID, you should confirm the zone support ECS、VPC、VSwitch
or not.
zh-cn: 可用区ID,需确认所选可用区下是否支持ECS、VPC、VSwitch等资源。
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Equals:
- ${SelectInstance}
- false
Default:
InstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 实例类型
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ZoneId
Visible:
Condition:
Fn::Equals:
- ${SelectInstance}
- false
Default:
SystemDiskCategory:
Type: String
Label:
en: System Disk Category
zh-cn: 系统盘类型
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
InstanceType: ${InstanceType}
Visible:
Condition:
Fn::Equals:
- ${SelectInstance}
- false
Default:
Password:
Type: String
Label:
en: Login Password
zh-cn: 实例密码
Description:
en: |-
The password must be 8 to 32 characters in length. <br>
It must consist three of the the following character types: uppercase letters, lowercase letters, digits, and special characters. <br>
Special characters include <span style="background:#E7E9EB;"><b>()`~!@#$%^&*_-+=|{}[]:;'<>,.?/</b></span>.<br>
zh-cn: 长度为8-30位,需包含大写字母、小写字母、特殊符号和数字中的三个,允许的特殊字符包括<span style="background:#E7E9EB;"><b>()`~!@#$%^&*_-+=|{}[]:;'<>,.?/</b></span>。
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Equals:
- ${SelectInstance}
- false
Default:
AllowedPattern: '[0-9A-Za-z\_\-&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
MinLength: 8
MaxLength: 30
NoEcho: true
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
Condition: CreateInstance
Properties:
CidrBlock: 192.168.0.0/16
SecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Condition: CreateInstance
Properties:
VpcId:
Ref: Vpc
VSwitch:
Type: ALIYUN::ECS::VSwitch
Condition: CreateInstance
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Ref: Vpc
CidrBlock: 192.168.0.0/24
InstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
Condition: CreateInstance
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
SecurityGroupId:
Ref: SecurityGroup
ImageId: centos_7_8
InstanceType:
Ref: InstanceType
SystemDiskCategory:
Ref: SystemDiskCategory
Password:
Ref: Password
IoOptimized: optimized
MaxAmount: 1
DS_Instances:
Type: DATASOURCE::ECS::Instances
Properties:
InstanceIds:
Fn::If:
- CreateInstance
- Fn::GetAtt:
- InstanceGroup
- InstanceIds
- [ Ref: InstanceId ]
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
Properties: { }
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Handle:
Ref: WaitConditionHandle
Timeout: 3720
Count: 1
SecurityGroupIngress_22:
Type: ALIYUN::ECS::SecurityGroupIngress
Properties:
SecurityGroupId:
Fn::Jq:
- First
- .[0].SecurityGroupIds[0]
- Fn::GetAtt:
- DS_Instances
- Instances
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: intranet
PortRange: 22/22
SecurityGroupIngress_3000:
Type: ALIYUN::ECS::SecurityGroupIngress
Properties:
SecurityGroupId:
Fn::Jq:
- First
- .[0].SecurityGroupIds[0]
- Fn::GetAtt:
- DS_Instances
- Instances
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: intranet
PortRange: 3000/3000
InstallNodejs:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
Fn::If:
- CreateInstance
- Fn::GetAtt:
- InstanceGroup
- InstanceIds
- [Ref: InstanceId]
Type: RunShellScript
Sync: true
Timeout: 3600
CommandContent:
Fn::Sub: |-
#!/bin/bash
if [ ! -f .ros.provision ]; then
echo "Name: 部署Node.js环境(CentOS 7)" > .ros.provision
fi
name=$(grep "^Name:" .ros.provision | awk -F':' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [[ "$name" != "部署Node.js环境(CentOS 7)" ]]; then
echo "当前实例已使用过\"$name\"教程的一键配置,不能再使用本教程的一键配置"
${WaitConditionHandle.CurlCli} --data-binary "{\"status\": \"FAILURE\", \"reason\": \"The current instance has already applied the configuration of the \\\"$name\\\" tutorial, and the configuration of this tutorial can no longer be applied.\"}"
exit 0
fi
if ! grep -q "^Step1: Install Node.js$" .ros.provision; then
echo "#########################"
echo "# Install Node.js"
echo "#########################"
yum install git -y
curl -o install.sh https://gitee.com/mirrors/nvm/raw/v0.39.3/install.sh
if [ $? -ne 0 ]; then
${WaitConditionHandle.CurlCli} --data-binary "{\"status\": \"FAILURE\", \"reason\": \"Download nvm install script failed.\"}"
fi
sed -i 's|GITHUB_REPO|GITEE_REPO|g' install.sh
sed -i 's|github.com|gitee.com|g' install.sh
sed -i 's|https://raw.githubusercontent.com/${!NVM_GITEE_REPO}/${!NVM_VERSION}|https://gitee.com/${!NVM_GITEE_REPO}/raw/${!NVM_VERSION}|g' install.sh
NVM_INSTALL_GITEE_REPO=mirrors/nvm METHOD='script' bash install.sh
if [ $? -ne 0 ]; then
${WaitConditionHandle.CurlCli} --data-binary "{\"status\": \"FAILURE\", \"reason\": \"Install nvm failed.\"}"
fi
export NVM_DIR="$([ -z "${!XDG_CONFIG_HOME-}" ] && printf %s "${!HOME}/.nvm" || printf %s "${!XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
nvm list-remote
nvm install v14.0.0
if [ $? -ne 0 ]; then
${WaitConditionHandle.CurlCli} --data-binary "{\"status\": \"FAILURE\", \"reason\": \"nvm install v14.0.0 failed.\"}"
fi
nvm install v16.0.0
if [ $? -ne 0 ]; then
${WaitConditionHandle.CurlCli} --data-binary "{\"status\": \"FAILURE\", \"reason\": \"nvm install v16.0.0 failed.\"}"
fi
nvm ls
echo "Step1: Install Node.js" >> .ros.provision
else
echo "#########################"
echo "# Node.js has been installed"
echo "#########################"
fi
if ! grep -q "^Step2: Prepare example$" .ros.provision; then
echo "#########################"
echo "# Prepare example"
echo "#########################"
cd
cat > example.js << \EOF
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${!hostname}:${!port}/`);
});
EOF
nohup node ~/example.js > example.log 2>&1 &
echo "Step2: Prepare example" >> .ros.provision
else
echo "#########################"
echo "# Example has been ready"
echo "#########################"
fi
${WaitConditionHandle.CurlCli} --data-binary '{"status": "SUCCESS"}'
DependsOn:
- SecurityGroupIngress_3000
Outputs:
ExampleUrl:
Description:
en: The page of example.js.
zh-cn: example.js页面。
Value:
Fn::Sub:
- http://${IP}:3000
- IP:
Fn::Jq:
- First
- if .[0].PublicIpAddress != [] then .[0].PublicIpAddress[0] else .[0].EipAddress.IpAddress
end
- Fn::GetAtt:
- DS_Instances
- Instances
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- SelectInstance
- InstanceId
- ZoneId
- InstanceType
- SystemDiskCategory
- Password
Label:
default: ECS
TemplateTags:
- acs:document-help:ecs:部署Node.js环境(CentOS 7)