Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script which, creates and attaches volumes #5

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ module "vm" {
identity_name = module.identity.identity_name
key_vault_name = module.vault.key_vault_name

data_disk_performance_tier = var.data_disk_performance_tier
disk_size_gb = var.disk_size_gb

instance_type = var.instance_type
image_id = module.graphdb_image.image_id
node_count = var.node_count
Expand Down
11 changes: 11 additions & 0 deletions modules/configuration/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
data "azurerm_resource_group" "graphdb" {
name = var.resource_group_name
}

data "azurerm_user_assigned_identity" "graphdb-instances" {
name = var.identity_name
resource_group_name = var.resource_group_name
Expand Down Expand Up @@ -76,3 +80,10 @@ resource "azurerm_role_assignment" "graphdb-license-secret-reader" {
scope = data.azurerm_key_vault.graphdb.id
role_definition_name = "Key Vault Secrets User"
}

# TODO should be moved to vm module
resource "azurerm_role_assignment" "rg-contributor-role" {
principal_id = data.azurerm_user_assigned_identity.graphdb-instances.principal_id
scope = data.azurerm_resource_group.graphdb.id
role_definition_name = "Contributor"
}
2 changes: 2 additions & 0 deletions modules/vm/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ locals {
user_data_script = var.custom_user_data != null ? var.custom_user_data : templatefile("${path.module}/templates/entrypoint.sh.tpl", {
load_balancer_fqdn : var.load_balancer_fqdn
key_vault_name : var.key_vault_name
data_disk_performance_tier : var.data_disk_performance_tier
disk_size_gb : var.disk_size_gb
})
}

Expand Down
96 changes: 94 additions & 2 deletions modules/vm/templates/entrypoint.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,100 @@ done
# Login in Azure CLI with managed identity (user or system assigned)
az login --identity

# TODO: Find/create/mount volumes
# https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/tutorial-use-disks-cli
# Find/create/attach volumes
INSTANCE_HOSTNAME=\'$(hostname)\'
SUBSCRIPTION_ID=$(az account show --query "id" --output tsv)
RESOURSE_GROUP=$(az vmss list --query "[0].resourceGroup" --output tsv)
VMSS_NAME=$(az vmss list --query "[0].name" --output tsv)
INSTANCE_ID=$(az vmss list-instances --resource-group $RESOURSE_GROUP --name $VMSS_NAME --query "[?contains(osProfile.computerName, $${INSTANCE_HOSTNAME})].instanceId" --output tsv)
ZONE_ID=$(az vmss list-instances --resource-group $RESOURSE_GROUP --name $VMSS_NAME --query "[?contains(osProfile.computerName, $${INSTANCE_HOSTNAME})].zones" --output tsv)
REGION_ID=$(az vmss list-instances --resource-group $RESOURSE_GROUP --name $VMSS_NAME --query "[?contains(osProfile.computerName, $${INSTANCE_HOSTNAME})].location" --output tsv)
# Do NOT change the LUN. Based on this we find and mount the disk in the VM
LUN=2

TIER=${data_disk_performance_tier}
DISK_SIZE_GB=${disk_size_gb}

# TODO Define the disk name based on the hostname ??
diskName="Disk_$${VMSS_NAME}_$${INSTANCE_ID}"

for i in $(seq 1 6); do
# Wait for existing disks in the VMSS which are unattached
existingUnattachedDisk=$(
viktor-ribchev marked this conversation as resolved.
Show resolved Hide resolved
az disk list --resource-group $RESOURSE_GROUP \
--query "[?diskState=='Unattached' && starts_with(name, 'Disk_$${VMSS_NAME}')].{Name:name}" \
--output tsv
)

if [ -z "$${existingUnattachedDisk:-}" ]; then
echo 'Disk not yet available'
sleep 10
else
break
fi
done

if [ -z "$existingUnattachedDisk" ]; then
yaskoo marked this conversation as resolved.
Show resolved Hide resolved
echo "Creating a new managed disk"
az disk create --resource-group $RESOURSE_GROUP --name $diskName --size-gb $DISK_SIZE_GB --location $REGION_ID --sku Premium_LRS --zone $ZONE_ID --tier $TIER
fi

# Checks if a managed disk is attached to the instance
attachedDisk=$(az vmss list-instances --resource-group "$RESOURSE_GROUP" --name "$VMSS_NAME" --query "[?instanceId==\"$INSTANCE_ID\"].storageProfile.dataDisks[].name" --output tsv)

if [ -z "$attachedDisk" ]; then
echo "No data disks attached for instance ID $INSTANCE_ID in VMSS $VMSS_NAME."
# Try to attach an existing managed disk
availableDisks=$(az disk list --resource-group $RESOURSE_GROUP --query "[?diskState=='Unattached' && starts_with(name, 'Disk_$${VMSS_NAME}') && zones[0]=='$${ZONE_ID}'].{Name:name}" --output tsv)
echo "Attaching available disk $availableDisks."
# Set Internal Field Separator to newline to handle spaces in names
IFS=$'\n'
# Would iterate through all available disks and attempt to attach them
for availableDisk in $availableDisks; do
az vmss disk attach --vmss-name $VMSS_NAME --resource-group $RESOURSE_GROUP --instance-id $INSTANCE_ID --lun $LUN --disk "$availableDisk" || true
done
fi

# Gets device name based on LUN
graphdb_device=$(lsscsi --scsi --size | awk '/\[1:.*:0:2\]/ {print $7}')

# Check if the device is present after attaching the disk
if [ -b "$graphdb_device" ]; then
echo "Device $graphdb_device is available."
else
echo "Device $graphdb_device is not available. Something went wrong."
exit 1
fi

# create a file system if there isn't any
if [ "$graphdb_device: data" = "$(file -s $graphdb_device)" ]; then
mkfs -t ext4 $graphdb_device
fi

disk_mount_point="/var/opt/graphdb"
mihailradkov marked this conversation as resolved.
Show resolved Hide resolved
mkdir -p "$disk_mount_point"

# Check if the disk is already mounted
if ! mount | grep -q "$graphdb_device"; then
echo "The disk at $graphdb_device is not mounted."

# Add an entry to the fstab file to automatically mount the disk
if ! grep -q "$graphdb_device" /etc/fstab; then
echo "$graphdb_device $disk_mount_point ext4 defaults 0 2" >> /etc/fstab
fi

# Mount the disk
mount "$disk_mount_point"
echo "The disk at $graphdb_device is now mounted at $disk_mount_point."
else
echo "The disk at $graphdb_device is already mounted."
fi

mihailradkov marked this conversation as resolved.
Show resolved Hide resolved
# Recreates folders if necessary and changes owner

mkdir -p /var/opt/graphdb/node /var/opt/graphdb/cluster-proxy
# TODO research how to avoid using chown, as it would be a slow operation if data is present.
chown -R graphdb:graphdb /var/opt/graphdb
yaskoo marked this conversation as resolved.
Show resolved Hide resolved

#
# DNS hack
Expand Down
14 changes: 14 additions & 0 deletions modules/vm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ variable "custom_user_data" {
type = string
default = null
}

# Managed Data Disks

variable "disk_size_gb" {
description = "Size of the managed data disk which will be created"
type = number
default = null
}

variable "data_disk_performance_tier" {
description = "Performance tier of the managed data disk"
type = string
default = null
}
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,15 @@ variable "custom_graphdb_vm_user_data" {
type = string
default = null
}

variable "disk_size_gb" {
description = "Size of the managed data disk which will be created"
type = number
default = 500
}

variable "data_disk_performance_tier" {
description = "Performance tier of the managed data disk"
type = string
default = "P40"
}