Skip to content

Commit

Permalink
Added script which, creates and attaches volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-ribchev committed Nov 10, 2023
1 parent 9e19115 commit 116558f
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
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.

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 @@ -32,3 +36,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"
}
95 changes: 94 additions & 1 deletion modules/vm/templates/entrypoint.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,102 @@ done
# Login in Azure CLI with managed identity (user or system assigned)
az login --identity

# Get the license
az keyvault secret download --vault-name ${key_vault_name} --name graphdb-license --file /etc/graphdb/graphdb.license --encoding base64

# TODO: Find/create/mount volumes
# https://learn.microsoft.com/en-us/azure/virtual-machine-scale-sets/tutorial-use-disks-cli

# Find/create/attach volumes

instanceHostname=\'$(hostname)\'
subscriptionID=$(az account show --query "id" --output tsv)
resourceGroup=$(az vmss list --query "[0].resourceGroup" --output tsv)
vmssName=$(az vmss list --query "[0].name" --output tsv)
instanceID=$(az vmss list-instances --resource-group $resourceGroup --name $vmssName --query "[?contains(osProfile.computerName, $${instanceHostname})].instanceId" --output tsv)
zoneID=$(az vmss list-instances --resource-group $resourceGroup --name $vmssName --query "[?contains(osProfile.computerName, $${instanceHostname})].zones" --output tsv)
regionID=$(az vmss list-instances --resource-group $resourceGroup --name $vmssName --query "[?contains(osProfile.computerName, $${instanceHostname})].location" --output tsv)
# TODO replace with external variables
tier="P40"
lun=2 # Based on this we find and mount the disk in the VM

diskSizeGB=128

# TODO Define the disk name based on the hostname ??
diskName="Disk_$${vmssName}_$${instanceID}"
# Finds disks in the VMSS which are unattached
existingUnattachedDisk=$(az disk list --resource-group $resourceGroup --query "[?diskState=='Unattached' && starts_with(name, 'Disk_$${vmssName}')].{Name:name}" --output tsv)

if [ -z "$existingUnattachedDisk" ]; then
echo "Creating a new managed disk"
az disk create --resource-group $resourceGroup --name $diskName --size-gb $diskSizeGB --location $regionID --sku Premium_LRS --zone $zoneID --tier $tier
fi

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

if [ -z "$attachedDisk" ]; then
echo "No data disks attached for instance ID $instanceID in VMSS $vmssName."
# Try to attach an existing managed disk
availableDisks=$(az disk list --resource-group $resourceGroup --query "[?diskState=='Unattached' && starts_with(name, 'Disk_$${vmssName}') && zones[0]=='$${zoneID}'].{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 $vmssName --resource-group $resourceGroup --instance-id $instanceID --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"

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

# Create the mount point if it doesn't exist
if [ ! -d "$disk_mount_point" ]; then
mkdir -p "$disk_mount_point"
fi

# 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

# Recreates folders if necessary and changes owner

if [ ! -d /var/opt/graphdb/node ]; then
mkdir -p /var/opt/graphdb/node
fi

if [ ! -d /var/opt/graphdb/cluster-proxy ]; then
mkdir -p /var/opt/graphdb/cluster-proxy
fi

chown -R graphdb:graphdb /var/opt/graphdb
#
# DNS hack
#
Expand Down

0 comments on commit 116558f

Please sign in to comment.