-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtomcat.yml
293 lines (256 loc) · 10.1 KB
/
tomcat.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
AWSTemplateFormatVersion: '2010-09-09'
Description: Despliegue app Java sobre instancia EC2 con ubuntu 20.04
Parameters:
EC2AMI:
Description: Imagen del Sistema Operativo
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id'
Default: '/aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id'
KeyName:
Description: Par clave valor para acceso SSH
Type: AWS::EC2::KeyPair::KeyName
Default: vockey
InstanceType:
Description: Tamaño instancia EC2
Type: String
Default: t2.micro
AllowedValues:
- t2.nano
- t2.micro
- t2.small
- t2.medium
ConstraintDescription: Tipos de instancia validas
SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Resources:
EC2Instance:
Type: AWS::EC2::Instance
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Count: '1'
Metadata:
AWS::CloudFormation::Init:
configSets:
full_install:
- install_and_enable_cfn_hup
install_and_enable_cfn_hup:
files:
"/etc/cfn/cfn-hup.conf":
content:
Fn::Join:
- ''
- - "[main]\n"
- stack=
- Ref: AWS::StackId
- "\n"
- region=
- Ref: AWS::Region
- "\n"
mode: '000400'
owner: root
group: root
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content:
Fn::Join:
- ''
- - "[cfn-auto-reloader-hook]\n"
- "triggers=post.update\n"
- "path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init\n"
- "action=/opt/aws/bin/cfn-init -v"
- "--stack "
- Ref: AWS::StackName
- " --resource EC2Instance"
- " --configsets full_install"
- " --region "
- Ref: AWS::Region
- "\n"
- "runas=root"
"/lib/systemd/system/cfn-hup.service":
content:
Fn::Join:
- ''
- - "[Unit]\n"
- "Description=cfn-hup daemon\n\n"
- "[Service]\n"
- "Type=simple\n"
- "ExecStart=/opt/aws/bin/cfn-hup\n"
- "Restart=always\n\n"
- "[Install]\n"
- "WantedBy=multi-user.target"
commands:
01enable_cfn_hup:
command: systemctl enable cfn-hup.service
02start_cfn_hup:
command: systemctl start cfn-hup.service
Properties:
InstanceType:
Ref: InstanceType
SecurityGroups:
- Ref: SecurityGroup
KeyName:
Ref: KeyName
IamInstanceProfile:
"LabInstanceProfile"
Monitoring: true
ImageId:
Ref: EC2AMI
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
exec > /tmp/userdata.log 2>&1
# Actualizar todas las apps
apt update -y
# Instalar unzip
apt install unzip -y
# Instalación CodeDeploy Agent
apt install ruby-full -y
apt install wget -y
cd /home/ubuntu
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
chmod +x ./install
./install auto > /tmp/logfile
service codedeploy-agent start
# Instalar AWS helper scripts de CloudFormation
mkdir -p /opt/aws/bin
wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --configsets full_install --region ${AWS::Region}
sleep 1
# Este script instala y configura Tomcat 10 en Ubuntu 20.04
# Se debe ejecutar con permisos de superusuario
# Salir en caso de error
#set -e
# Comprobar que se ejecuta como superusuario
echo "Actualizando sistema..."
apt update
apt upgrade -y
# Instalar Java 17 Development Kit y Runtime Environment
echo "Instalando Java..."
apt install openjdk-17-jdk -y
apt install openjdk-17-jre -y
java -version
# Descargar Tomcat
echo "Descargando Tomcat 10..."
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.18/bin/apache-tomcat-10.1.18.tar.gz -P /tmp
# Instalar tar
apt install tar -y
# Descomprimir Tomcat
echo "Descomprimiendo Tomcat..."
mkdir -p /opt/tomcat
tar xf /tmp/apache-tomcat-10*.tar.gz -C /opt/tomcat --strip-components=1
# Crear grupo tomcat
echo "Creando grupo tomcat..."
if ! getent group tomcat > /dev/null 2>&1; then
groupadd tomcat
fi
# Crear usuario tomcat
echo "Creando usuario tomcat..."
if ! id -u tomcat > /dev/null 2>&1; then
useradd -m -s /bin/false -g tomcat -d /opt/tomcat tomcat
fi
# Configurar permisos
echo "Configurando permisos..."
cd /opt/tomcat
# Cambiar el propietario y grupo de todos los archivos y subdirectorios a tomcat
chown -R tomcat:tomcat /opt/tomcat/
# Permisos de lectura y ejecución para el grupo tomcat
chmod -R g+r conf
chmod g+x conf
chmod -R u+x /opt/tomcat/bin
echo "Configurando usuarios administrativos..."
# Buscar "</tomcat-users>" y añadir los usuarios administrativos. (el \ es para escapar el /)
# El comando sed -i hace los cambios directamente en el archivo y no muestra la salida por pantalla
sed -i 's/<\/tomcat-users>/ <role rolename="manager-gui" \/>\n <user username="manager" password="manager_password" roles="manager-gui" \/>\n <role rolename="admin-gui" \/>\n <user username="admin" password="admin_password" roles="manager-gui,admin-gui" \/>\n<\/tomcat-users>/' /opt/tomcat/conf/tomcat-users.xml
echo "Configurando acceso a la página del Manager..."
sed -i '/<Valve/ s/^/<!-- /' /opt/tomcat/webapps/manager/META-INF/context.xml
sed -i '/:1|0:0:0:0:0:0:0:1" \/>/ s/$/ -->/' /opt/tomcat/webapps/manager/META-INF/context.xml
echo "Configurando acceso a la página del Host Manager..."
sed -i '/<Valve/ s/^/<!-- /' /opt/tomcat/webapps/host-manager/META-INF/context.xml
sed -i '/:1|0:0:0:0:0:0:0:1" \/>/ s/$/ -->/' /opt/tomcat/webapps/host-manager/META-INF/context.xml
echo "Creando servicio..."
# Obtener la ruta de la instalación de Java
RUTA=$(sudo update-java-alternatives -l | grep '1.17.0' | awk '{print $3}')
cat > /etc/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=$RUTA"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
EOF
echo "Iniciando Tomcat..."
systemctl daemon-reload
systemctl start tomcat
systemctl --no-pager status tomcat
systemctl enable tomcat
echo "Configurando firewall..."
ufw allow 8080
echo "Tomcat instalado correctamente"
echo "Accede a la página del Manager a través del puerto 8080"
echo "La contraseña para el usuario manager es manager_password por default y para el usuario admin es admin_password por default"
# Instalar git
apt install git -y
# Crear directorio temporal
mkdir /home/tmp
cd /home/tmp
# Clonar repositorio
git clone https://github.com/Alexiiius/Stack-AWS-Tomcat.git
# Movernos al directorio de la app
cd Stack-AWS-Tomcat/app
# Compilar la app y moverla a la carpeta de Tomcat
chmod +x gradlew
./gradlew war
mv build/libs/*.war /opt/tomcat/webapps/hola.war
echo "Aplicación desplegada correctamente"
# Manda la señal de que la instancia está lista
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}
# Reiniciar la instancia ya que se queda "pillada" tras compilar la app por alguna razon.
echo "Reiniciando la instancia..."
shutdown -r now
Tags:
- Key: Name
Value: !Ref AWS::StackName
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Acesso SSH y web en 8080
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp:
Ref: SSHLocation
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp:
Ref: SSHLocation
Tags:
- Key: Name
Value: !Ref AWS::StackName
Outputs:
PublicIP:
Description: The Public IP of the EC2 instance
Value: !GetAtt EC2Instance.PublicIp
Export:
Name: PublicIP