Skip to content

Commit f2c1644

Browse files
author
Venkat Yalla
authored
Create iotopsQuickstart.sh
1 parent d200317 commit f2c1644

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

scripts/iotopsQuickstart.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#!/bin/bash
2+
3+
# Function to print in green
4+
print_green() {
5+
echo -e "\033[32m$1\033[0m"
6+
}
7+
8+
# Check current Azure subscription
9+
echo "Checking current Azure subscription..."
10+
CURRENT_SUBSCRIPTION=$(az account show --query "name" -o tsv 2>/dev/null)
11+
if [ -z "$CURRENT_SUBSCRIPTION" ]; then
12+
echo "Error: No active Azure subscription found. Please run 'az login' to set up an account."
13+
exit 1
14+
fi
15+
16+
echo "Current subscription: $(print_green "$CURRENT_SUBSCRIPTION")"
17+
read -p "Is this the correct subscription? (y/n): " RESPONSE
18+
19+
if [[ "$RESPONSE" != "y" ]]; then
20+
echo "Please use 'az account set' to select the correct subscription and rerun the script."
21+
exit 1
22+
fi
23+
24+
# Default values
25+
DEFAULT_LOCATION="westus2"
26+
DEFAULT_RESOURCE_GROUP="${CODESPACE_NAME:-default-rg}"
27+
CLUSTER_NAME="iotops-quickstart-cluster"
28+
SUPPORTED_LOCATIONS=("eastus" "eastus2" "westus2" "westus" "westeurope" "northeurope")
29+
30+
# Prompt for custom values
31+
read -p "Provide custom location and resource group? (y/n): " CUSTOM_VALUES
32+
33+
if [[ "$CUSTOM_VALUES" == "y" ]]; then
34+
# User provides custom values
35+
while true; do
36+
read -p "Enter resource group name: " RESOURCE_GROUP
37+
38+
# Check if the resource group exists
39+
echo "Checking if resource group '$RESOURCE_GROUP' exists..."
40+
az group show --name "$RESOURCE_GROUP" &> /dev/null
41+
if [ $? -eq 0 ]; then
42+
# Resource group exists, check location
43+
RG_LOCATION=$(az group show --name "$RESOURCE_GROUP" --query "location" -o tsv)
44+
if [[ " ${SUPPORTED_LOCATIONS[*]} " == *" $RG_LOCATION "* ]]; then
45+
LOCATION="$RG_LOCATION"
46+
break
47+
else
48+
echo "Error: Resource group is in unsupported location '$RG_LOCATION'."
49+
echo "Supported locations: ${SUPPORTED_LOCATIONS[*]}."
50+
read -p "Try a different resource group? (y/n): " TRY_AGAIN
51+
[[ "$TRY_AGAIN" != "y" ]] && exit 1
52+
fi
53+
else
54+
# Resource group does not exist
55+
read -p "Enter location for new resource group (Supported: ${SUPPORTED_LOCATIONS[*]}): " LOCATION
56+
if [[ " ${SUPPORTED_LOCATIONS[*]} " == *" $LOCATION "* ]]; then
57+
echo "Creating resource group '$RESOURCE_GROUP' in '$LOCATION'..."
58+
az group create --name "$RESOURCE_GROUP" --location "$LOCATION" --output none
59+
[ $? -ne 0 ] && echo "Error: Failed to create resource group." && exit 1
60+
break
61+
else
62+
echo "Error: Unsupported location '$LOCATION'."
63+
fi
64+
fi
65+
done
66+
else
67+
# Use default values
68+
RESOURCE_GROUP="$DEFAULT_RESOURCE_GROUP"
69+
LOCATION="$DEFAULT_LOCATION"
70+
71+
# Check if default resource group exists, create if not
72+
echo "Checking if resource group '$RESOURCE_GROUP' exists..."
73+
az group show --name "$RESOURCE_GROUP" &> /dev/null
74+
if [ $? -ne 0 ]; then
75+
echo "Creating resource group '$RESOURCE_GROUP' in '$LOCATION'..."
76+
az group create --name "$RESOURCE_GROUP" --location "$LOCATION" --output none
77+
[ $? -ne 0 ] && echo "Error: Failed to create resource group." && exit 1
78+
fi
79+
fi
80+
81+
# Ensure connectedk8s extension is installed
82+
echo "Ensuring 'connectedk8s' extension is installed..."
83+
az extension add --name connectedk8s --only-show-errors &> /dev/null
84+
85+
# Check if cluster is already connected
86+
echo "Checking if cluster '$CLUSTER_NAME' is connected to Azure Arc..."
87+
az connectedk8s show --name "$CLUSTER_NAME" --resource-group "$RESOURCE_GROUP" &> /dev/null
88+
if [ $? -eq 0 ]; then
89+
echo "Cluster '$CLUSTER_NAME' is already connected."
90+
else
91+
echo "Connecting cluster '$CLUSTER_NAME' to Azure Arc..."
92+
az connectedk8s connect --name "$CLUSTER_NAME" --resource-group "$RESOURCE_GROUP"
93+
[ $? -ne 0 ] && echo "Error: Failed to connect cluster to Azure Arc." && exit 1
94+
fi
95+
96+
# Save environment variables
97+
SCRIPT_DIR="$(dirname "$0")"
98+
ENV_VARS_FILE="$SCRIPT_DIR/env_vars.txt"
99+
100+
echo "Saving environment variables to '$ENV_VARS_FILE'..."
101+
cat <<EOL > "$ENV_VARS_FILE"
102+
export CURRENT_SUBSCRIPTION="$CURRENT_SUBSCRIPTION"
103+
export RESOURCE_GROUP="$RESOURCE_GROUP"
104+
export LOCATION="$LOCATION"
105+
export CLUSTER_NAME="$CLUSTER_NAME"
106+
EOL
107+
108+
# Derive unique names based on Codespace
109+
echo "Deriving unique resource names..."
110+
if [ -z "$CODESPACE_NAME" ]; then
111+
echo "Error: 'CODESPACE_NAME' environment variable is not set."
112+
exit 1
113+
fi
114+
115+
UNIQUE_SUFFIX=$(echo "$CODESPACE_NAME" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]')
116+
UNIQUE_SUFFIX=${UNIQUE_SUFFIX:0:18}
117+
118+
STORAGE_ACCOUNT="st${UNIQUE_SUFFIX}"
119+
STORAGE_ACCOUNT=${STORAGE_ACCOUNT:0:24}
120+
121+
SCHEMA_REGISTRY="sr${UNIQUE_SUFFIX}"
122+
SCHEMA_REGISTRY_NAMESPACE="srn${UNIQUE_SUFFIX}"
123+
124+
echo "Storage Account: $(print_green "$STORAGE_ACCOUNT")"
125+
echo "Schema Registry: $(print_green "$SCHEMA_REGISTRY")"
126+
echo "Schema Registry Namespace: $(print_green "$SCHEMA_REGISTRY_NAMESPACE")"
127+
128+
# Append to env_vars.txt
129+
cat <<EOL >> "$ENV_VARS_FILE"
130+
export UNIQUE_SUFFIX="$UNIQUE_SUFFIX"
131+
export STORAGE_ACCOUNT="$STORAGE_ACCOUNT"
132+
export SCHEMA_REGISTRY="$SCHEMA_REGISTRY"
133+
export SCHEMA_REGISTRY_NAMESPACE="$SCHEMA_REGISTRY_NAMESPACE"
134+
EOL
135+
136+
# Create storage account if it doesn't exist
137+
echo "Checking if storage account '$STORAGE_ACCOUNT' exists..."
138+
az storage account show --name "$STORAGE_ACCOUNT" --resource-group "$RESOURCE_GROUP" &> /dev/null
139+
if [ $? -ne 0 ]; then
140+
echo "Creating storage account '$STORAGE_ACCOUNT'..."
141+
az storage account create \
142+
--name "$STORAGE_ACCOUNT" \
143+
--location "$LOCATION" \
144+
--resource-group "$RESOURCE_GROUP" \
145+
--enable-hierarchical-namespace true \
146+
--sku Standard_RAGRS \
147+
--kind StorageV2 \
148+
--output none
149+
[ $? -ne 0 ] && echo "Error: Failed to create storage account." && exit 1
150+
else
151+
echo "Storage account '$STORAGE_ACCOUNT' already exists."
152+
fi
153+
154+
# Get Storage Account Resource ID
155+
SA_RESOURCE_ID=$(az storage account show --name "$STORAGE_ACCOUNT" --resource-group "$RESOURCE_GROUP" --query id -o tsv)
156+
echo "export SA_RESOURCE_ID=\"$SA_RESOURCE_ID\"" >> "$ENV_VARS_FILE"
157+
158+
# Create schema registry if it doesn't exist
159+
echo "Checking if schema registry '$SCHEMA_REGISTRY' exists..."
160+
az iot ops schema registry show --name "$SCHEMA_REGISTRY" --resource-group "$RESOURCE_GROUP" &> /dev/null
161+
if [ $? -ne 0 ]; then
162+
echo "Creating schema registry '$SCHEMA_REGISTRY'..."
163+
az iot ops schema registry create \
164+
--name "$SCHEMA_REGISTRY" \
165+
--resource-group "$RESOURCE_GROUP" \
166+
--registry-namespace "$SCHEMA_REGISTRY_NAMESPACE" \
167+
--sa-resource-id "$SA_RESOURCE_ID" \
168+
--output none
169+
[ $? -ne 0 ] && echo "Error: Failed to create schema registry." && exit 1
170+
else
171+
echo "Schema registry '$SCHEMA_REGISTRY' already exists."
172+
fi
173+
174+
# Initialize IoT Ops if not already initialized
175+
echo "Initializing IoT Operations on cluster..."
176+
az iot ops show --cluster "$CLUSTER_NAME" --resource-group "$RESOURCE_GROUP" &> /dev/null
177+
if [ $? -ne 0 ]; then
178+
az iot ops init --cluster "$CLUSTER_NAME" --resource-group "$RESOURCE_GROUP"
179+
[ $? -ne 0 ] && echo "Error: Failed to initialize IoT Operations." && exit 1
180+
else
181+
echo "IoT Operations already initialized on cluster."
182+
fi
183+
184+
# Get Schema Registry Resource ID
185+
SR_RESOURCE_ID=$(az iot ops schema registry show --name "$SCHEMA_REGISTRY" --resource-group "$RESOURCE_GROUP" --query id -o tsv)
186+
echo "export SR_RESOURCE_ID=\"$SR_RESOURCE_ID\"" >> "$ENV_VARS_FILE"
187+
188+
# Create IoT Ops instance if it doesn't exist
189+
INSTANCE_NAME="${CLUSTER_NAME}-instance"
190+
echo "Checking if IoT Operations instance '$INSTANCE_NAME' exists..."
191+
az iot ops show --cluster "$CLUSTER_NAME" --resource-group "$RESOURCE_GROUP" --name "$INSTANCE_NAME" &> /dev/null
192+
if [ $? -ne 0 ]; then
193+
echo "Creating IoT Operations instance '$INSTANCE_NAME'..."
194+
az iot ops create \
195+
--cluster "$CLUSTER_NAME" \
196+
--resource-group "$RESOURCE_GROUP" \
197+
--name "$INSTANCE_NAME" \
198+
--sr-resource-id "$SR_RESOURCE_ID" \
199+
--broker-frontend-replicas 1 \
200+
--broker-frontend-workers 1 \
201+
--broker-backend-part 1 \
202+
--broker-backend-workers 1 \
203+
--broker-backend-rf 2 \
204+
--broker-mem-profile Low \
205+
--output none
206+
[ $? -ne 0 ] && echo "Error: Failed to create IoT Operations instance." && exit 1
207+
else
208+
echo "IoT Operations instance '$INSTANCE_NAME' already exists."
209+
fi
210+
211+
echo "export INSTANCE_NAME=\"$INSTANCE_NAME\"" >> "$ENV_VARS_FILE"
212+
213+
# Completion message
214+
echo -e "\n$(print_green 'Setup completed successfully!')"
215+
echo "Environment variables saved to '$(print_green "$ENV_VARS_FILE")'."
216+
echo "To load the variables, run: $(print_green "source $ENV_VARS_FILE")"

0 commit comments

Comments
 (0)