-
Notifications
You must be signed in to change notification settings - Fork 6
/
userdata.sh
205 lines (187 loc) · 5.81 KB
/
userdata.sh
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
#!/bin/bash
set -x
apt-get update
apt-get install -y \
curl \
docker-compose \
kitty-terminfo \
nfs-common \
python3-pip \
ruby-full
pip install awscli
# Install more current version Docker Compose
curl -SL https://github.com/docker/compose/releases/download/v2.29.1/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# Format and attach EBS
mkfs -t ext4 -L ar-io-data /dev/sda1
echo "/dev/sda1 /data ext4 defaults 0 2" >> /etc/fstab
mkdir /data
mount /data
mkdir /data/sqlite
mkdir /data/tmp
# Mount the EFS cache filesystem
echo "${fs_id}.efs.${region}.amazonaws.com:/ /efs nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,_netdev 0 0" >> /etc/fstab
mkdir /efs
mount /efs
mkdir -p /efs/data/chunks
mkdir -p /efs/data/contiguous
mkdir -p /efs/data/headers
mkdir -p /efs/wallets
# Copy in the latest DB snapshot
backup_dir="/efs/backups"
newest_backup=$(find $backup_dir -name "*.db" -type f | xargs ls -t | head -n 1)
instance_id=$(echo $newest_backup | awk -F '/' '{print $(NF-3)}')
copy_from_dir="$backup_dir/$instance_id/sqlite/before-install"
cp $copy_from_dir/*.db /data/sqlite/
# Download and install the CodeDeploy agent
curl -O https://aws-codedeploy-${region}.s3.amazonaws.com/latest/install
chmod +x ./install
./install auto
# Download and install the CloudWatch agent for Ubuntu
curl -O https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
dpkg -i -E ./amazon-cloudwatch-agent.deb
# Configure the CloudWatch agent
cat <<EOF > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
{
"agent": {
"metrics_collection_interval": 60,
"run_as_root": true,
"logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
},
"metrics": {
"metrics_collected": {
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"swap": {
"measurement": [
"swap_used_percent"
],
"metrics_collection_interval": 60
}
},
"append_dimensions": {
"ImageId": "\$${aws:ImageId}",
"InstanceId": "\$${aws:InstanceId}",
"InstanceType": "\$${aws:InstanceType}"
},
"aggregation_dimensions": [
[
"InstanceId",
"InstanceType"
]
]
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/syslog",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/syslog"
},
{
"file_path": "/var/log/auth.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/auth.log"
},
{
"file_path": "/var/log/cloud-init-output.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/cloud-init-output.log"
},
{
"file_path": "/var/log/cloud-init.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/cloud-init.log"
},
{
"file_path": "/var/log/dmesg",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/dmesg"
},
{
"file_path": "/var/log/kernel.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/kernel.log"
},
{
"file_path": "/var/log/aws/code-deploy-agent/codedeploy-agent.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}/codedeploy-agent.log"
}
]
}
}
}
}
EOF
systemctl restart amazon-cloudwatch-agent
# Get the region from the metadata service
export AWS_DEFAULT_REGION=$(curl http://169.254.169.254/latest/meta-data/placement/region)
# Download Release 15
mkdir -p /opt/ar-io-node
curl -o /opt/ar-io-node/docker-compose.yaml \
-L https://gist.githubusercontent.com/kay-is/4282773a7ee0bf16cdf34794829daa7f/raw/a5cd4b33c07efa4ff048e6c81f0bd2ae99633b46/docker-compose.yaml
# Download the .env file from SSM
aws ssm get-parameter \
--name /ar-io-nodes/env \
--with-decryption \
--query 'Parameter.Value' \
--output text > /opt/ar-io-node/.env
# Download the observer wallet key from SSM
export observer_wallet=$(cat /opt/ar-io-node/.env | grep OBSERVER_WALLET | cut -d '=' -f2)
aws ssm get-parameter \
--name /ar-io-nodes/observer-key \
--with-decryption \
--query 'Parameter.Value' \
--output text > "/efs/wallets/$${observer_wallet}.json"
# Add the EC2 instance ID to the .env file
export INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
echo "INSTANCE_ID=$INSTANCE_ID" >> /opt/ar-io-node/.env
# User the AWS logs driver for Docker
cat <<EOF > /etc/docker/daemon.json
{
"log-driver": "awslogs",
"log-opts": {
"awslogs-region": "${region}",
"awslogs-group": "${log_group_name}",
"tag": "{{ with split .ImageName \":\" }}{{join . \"_\"}}{{end}}-{{.ID}}"
}
}
EOF
systemctl restart docker
# Write systemd service file
cat <<EOF > /etc/systemd/system/ar-io-node.service
[Unit]
Description=ar-io-node
After=docker.service
Requires=docker.service
[Service]
WorkingDirectory=/opt/ar-io-node
Restart=always
RestartSec=10s
ExecStart=/usr/local/bin/docker-compose -f /opt/ar-io-node/docker-compose.yaml up
ExecStop=/usr/local/bin/docker-compose -f /opt/ar-io-node/docker-compose.yaml down
TimeoutSec=60
[Install]
WantedBy=multi-user.target
EOF
# Enable and start gateway service
systemctl daemon-reload
systemctl enable ar-io-node
systemctl start ar-io-node