Skip to content

Commit e37c868

Browse files
committed
user-data script
1 parent 569cf95 commit e37c868

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

lib/allora/lib/allora-stack.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ export class AlloraStack extends cdk.Stack {
3838
} = props;
3939
const { region } = env;
4040

41+
const STACK_NAME = cdk.Stack.of(this).stackName;
42+
const STACK_ID = cdk.Stack.of(this).stackId;
43+
4144

4245

4346
// Create S3 Bucket
@@ -72,17 +75,7 @@ export class AlloraStack extends cdk.Stack {
7275
});
7376
securityGroup.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(9010), 'Allow inbound TCP 9010');
7477

75-
// Read user data script and inject variables
76-
const userData = fs.readFileSync(path.join(__dirname, 'assets', 'user-data', 'node.sh')).toString();
77-
const modifiedUserData = cdk.Fn.sub(userData, {
78-
_AWS_REGION_: region,
79-
_ASSETS_S3_PATH_: `s3://${bucket.bucketName}/user-data/node.sh`,
80-
// Add other variables as needed
81-
});
82-
83-
// Create UserData for EC2 instance
84-
const ec2UserData = ec2.UserData.forLinux();
85-
ec2UserData.addCommands(modifiedUserData);
78+
8679

8780

8881
// Getting the snapshot bucket name and IAM role ARN from the common stack
@@ -113,6 +106,20 @@ export class AlloraStack extends cdk.Stack {
113106

114107
const instance = singleNode.instance;
115108

109+
// Read user data script and inject variables
110+
const userData = fs.readFileSync(path.join(__dirname, 'assets', 'user-data', 'node.sh')).toString();
111+
const modifiedUserData = cdk.Fn.sub(userData, {
112+
_AWS_REGION_: region,
113+
_ASSETS_S3_PATH_: `s3://${bucket.bucketName}/user-data/node.sh`,
114+
_NODE_CF_LOGICAL_ID_: singleNode.nodeCFLogicalId,
115+
_STACK_NAME_: STACK_NAME,
116+
_STACK_ID_: STACK_ID,
117+
});
118+
119+
// Create UserData for EC2 instance
120+
const ec2UserData = ec2.UserData.forLinux();
121+
ec2UserData.addCommands(modifiedUserData);
122+
116123
instance.addUserData(ec2UserData.render())
117124

118125
const dashboardString = cdk.Fn.sub(JSON.stringify(nodeCwDashboard.SyncNodeCWDashboardJSON()), {

lib/allora/lib/assets/node-cw-dashboard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ export const SyncNodeCWDashboardJSON = () => {
138138
"type": "metric",
139139
"properties": {
140140
"metrics": [
141-
[ "CWAgent", "theta_current_block_height", "InstanceId", "${INSTANCE_ID}", { "label": "${INSTANCE_ID}-${INSTANCE_NAME}" } ]
141+
[ "CWAgent", "allora_current_block_height", "InstanceId", "${INSTANCE_ID}", { "label": "${INSTANCE_ID}-${INSTANCE_NAME}" } ]
142142
],
143143
"sparkline": true,
144144
"view": "timeSeries",
145145
"stacked": false,
146146
"region": "${REGION}",
147147
"stat": "Maximum",
148148
"period": 60,
149-
"title": "Theta Client Block Height"
149+
"title": "Allora Client Block Height"
150150
}
151151
},
152152
{
@@ -165,7 +165,7 @@ export const SyncNodeCWDashboardJSON = () => {
165165
"metrics": [
166166
[ { "expression": "SELECT COUNT(edge_peer) FROM CWAgent GROUP BY InstanceId", "label": "${INSTANCE_ID}-${INSTANCE_NAME}" } ]
167167
],
168-
"title": "Theta Client Peer Count"
168+
"title": "Allora Client Peer Count"
169169
}
170170
},
171171
{

lib/allora/lib/assets/user-data/node.sh

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ echo "----------------------------------------------"
55

66
echo "AWS_REGION=${_AWS_REGION_}" >> /etc/environment
77
echo "ASSETS_S3_PATH=${_ASSETS_S3_PATH_}" >> /etc/environment
8+
echo "RESOURCE_ID=${_NODE_CF_LOGICAL_ID_}" >> /etc/environment
9+
echo "STACK_NAME=${_STACK_NAME_}" >> /etc/environment
10+
echo "STACK_ID=${_STACK_ID_}" >> /etc/environment
811

912
#############################
1013
# Prerequisites
14+
# Aws cli (for signalling)
1115
# Pip
1216
# Pipx
1317
# docker.io
@@ -19,15 +23,19 @@ echo "ASSETS_S3_PATH=${_ASSETS_S3_PATH_}" >> /etc/environment
1923
echo "[user-data] Update Ubuntu package list"
2024
sudo apt-get update --yes
2125

26+
2227
# Install pip
23-
echo "[user-data] Install Pip"
24-
sudo apt-get install python3-setuptools --yes
25-
sudo python3 -m easy_install install pip
26-
python3 -m pip --version
28+
echo "[user-data] Install Python, Pip, and Venv"
29+
sudo add-apt-repository ppa:deadsnakes/ppa --yes
30+
sudo apt-get update --yes
31+
sudo apt install python3.7 --yes
32+
sudo apt-get install -y python3-pip python3-venv
33+
2734

2835
# Install Pipx
2936
echo "[user-data] Install Pipx"
3037
sudo apt-get install pipx --yes
38+
pipx ensurepath
3139

3240
# Install Go
3341
echo "[user-data] Install Go"
@@ -60,6 +68,10 @@ echo \
6068
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
6169
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
6270

71+
# update again since we added the docker repo to apt sources
72+
echo "[user-data] Update again after adding docker repo to apt sources"
73+
sudo apt-get update --yes
74+
6375
# Install Docker Compose:
6476
echo "[user-data] Install docker compose"
6577
sudo apt-get install docker-compose-plugin --yes
@@ -82,6 +94,36 @@ sudo usermod -aG docker $USER
8294
echo "[user-data] Starting docker service"
8395
sudo service docker start
8496

97+
echo "[user-data] Signaling completion to CloudFormation"
98+
if [[ "$STACK_ID" != "none" ]]; then
99+
echo "Install and enable CloudFormation helper scripts"
100+
mkdir -p /opt/aws/
101+
pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
102+
sudo ln -s /usr/local/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
103+
104+
echo "Configuring CloudFormation helper scripts"
105+
mkdir -p /etc/cfn/
106+
mv /opt/cfn-hup/cfn-hup.conf /etc/cfn/cfn-hup.conf
107+
sed -i "s;__AWS_STACK_ID__;\"$STACK_ID\";g" /etc/cfn/cfn-hup.conf
108+
sed -i "s;__AWS_REGION__;\"$AWS_REGION\";g" /etc/cfn/cfn-hup.conf
109+
110+
mkdir -p /etc/cfn/hooks.d/
111+
mv /opt/cfn-hup/cfn-auto-reloader.conf /etc/cfn/hooks.d/cfn-auto-reloader.conf
112+
sed -i "s;__AWS_STACK_NAME__;\"$STACK_NAME\";g" /etc/cfn/hooks.d/cfn-auto-reloader.conf
113+
sed -i "s;__AWS_REGION__;\"$AWS_REGION\";g" /etc/cfn/hooks.d/cfn-auto-reloader.conf
114+
115+
echo "Starting CloudFormation helper scripts as a service"
116+
mv /opt/cfn-hup/cfn-hup.service /etc/systemd/system/cfn-hup.service
117+
118+
systemctl daemon-reload
119+
systemctl enable --now cfn-hup
120+
systemctl start cfn-hup.service
121+
122+
cfn-signal --stack $STACK_NAME --resource $RESOURCE_ID --region $AWS_REGION
123+
fi
124+
125+
126+
85127
echo "----------------------------------------------"
86128
echo "[user-data] Allora user-data script successful"
87129
echo "----------------------------------------------"

lib/allora/sample-configs/.env-sample-full

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ AWS_ACCOUNT_ID="xxxxxxxxxxx"
22
AWS_RESOURCE_NAME_PREFIX="AlloraWorkerx"
33
AWS_REGION="us-east-1"
44
AWS_AMI_ID="ami-04b70fa74e45c3917"
5-
AWS_INSTANCE_TYPE="t2.medium"
5+
AWS_INSTANCE_TYPE="t3.medium"
66
AWS_VPC_MAX_AZS="1"
77
AWS_VPC_NAT_GATEWAYS="0"
88
AWS_VPC_CIDR_MASK="24"

0 commit comments

Comments
 (0)