Skip to content

Commit

Permalink
Merge pull request #155 from NeowayLabs/addDiskCacheOption
Browse files Browse the repository at this point in the history
Add disk cache option
  • Loading branch information
katcipis authored Sep 28, 2017
2 parents 35b097b + e016cfb commit 68e6531
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 160 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Release 0.9.0

This release breaks the API of the following functions:

* Add cache policy to klb **azure_vm_disk_attach_new**
* Add cache policy to **azure_vm_backup_recover**
* Add cache policy to **azure_vm_disk_attach_lun**
* add cache policy to **azure_vm_disk_attach**
* Attach disks on recovered backup guaranteeing same LUN

# Release 0.8.1

Fix snapshot creation to allow the storage sku
Expand Down
58 changes: 41 additions & 17 deletions azure/vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ fn azure_vm_set_storagesku(instance, storagesku) {
return $instance
}

# azure_vm_set_osdisk_caching sets the os disk caching type
# `instance` is the name of the instance.
# `caching` is the caching type, possible values: None, ReadOnly, ReadWrite
fn azure_vm_set_osdisk_caching(instance, caching) {
instance <= append($instance, "--os-disk-caching")
instance <= append($instance, $caching)
return $instance
}

# azure_vm_set_datadisk_caching sets the data disk caching type
# `instance` is the name of the instance.
# `caching` is the caching type, possible values: None, ReadOnly, ReadWrite
fn azure_vm_set_datadisk_caching(instance, caching) {
instance <= append($instance, "--data-disk-caching")
instance <= append($instance, $caching)
return $instance
}

# azure_vm_create creates a "Virtual Machine".
# `instance` is the name of the instance.
fn azure_vm_create(instance) {
Expand Down Expand Up @@ -315,19 +333,19 @@ fn azure_vm_availset_delete(name, group) {
}

# azure_vm_disk_attach attaches an existing disk to the VM.
fn azure_vm_disk_attach(name, resgroup, diskID) {
az vm disk attach -g $resgroup --vm-name $name --disk $diskID
fn azure_vm_disk_attach(name, resgroup, diskID, caching) {
az vm disk attach -g $resgroup --vm-name $name --disk $diskID --caching $caching
}

# azure_vm_disk_attach_lun does the same as azure_vm_disk_attach
# azure_vm_disk_attach_with_lun does the same as azure_vm_disk_attach
# but with the specificied LUN.
fn azure_vm_disk_attach_lun(name, resgroup, diskID, lun) {
az vm disk attach -g $resgroup --vm-name $name --disk $diskID --lun $lun
fn azure_vm_disk_attach_with_lun(name, resgroup, diskID, caching, lun) {
az vm disk attach -g $resgroup --vm-name $name --disk $diskID --lun $lun --caching $caching
}

# azure_vm_disk_attach_new creats a new disk and attaches to the VM.
fn azure_vm_disk_attach_new(name, resgroup, diskname, size, sku) {
az vm disk attach -g $resgroup --vm-name $name --disk $diskname --new --size-gb $size --sku $sku
# azure_vm_disk_attach_new creates a new disk and attaches to the VM.
fn azure_vm_disk_attach_new(name, resgroup, diskname, size, sku, caching) {
az vm disk attach -g $resgroup --vm-name $name --disk $diskname --new --size-gb $size --sku $sku --caching $caching
}

# azure_vm_get_datadisks_ids will returns a list with the
Expand Down Expand Up @@ -691,6 +709,10 @@ fn azure_vm_backup_exists(backup_resgroup) {
# no storage-sku should be set on the vm instance, since it
# will be defined on the disks created from the backup.
#
# The caching option defines the caching type of the
# disks that will be attached to the recovered VM.
# All disks recovered from the backup will have the same caching type.
#
# The backup_resgroup is the name of the resource group
# where the snapshots are stored just as it is returned by
# azure_vm_backup_create.
Expand All @@ -700,7 +722,7 @@ fn azure_vm_backup_exists(backup_resgroup) {
#
# This function returns an empty string on success or a
# non empty error message if it fails.
fn azure_vm_backup_recover(instance, storagesku, backup_resgroup) {
fn azure_vm_backup_recover(instance, storagesku, caching, backup_resgroup) {
fn log(msg) {
echo "vm.backup.recover: "+$msg
}
Expand Down Expand Up @@ -734,15 +756,13 @@ fn azure_vm_backup_recover(instance, storagesku, backup_resgroup) {

if $osdiskname != "" {
msg <= format("found os disk name %q on vm instance: ", $osdiskname)

return $msg+"should not call azure_vm_set_osdiskname on a vm that is being recovered from backup"
}

sku <= _azure_vm_get($instance, "storage-sku")

if $sku != "" {
msg <= format("found storage-sku %q on vm instance: ", $sku)

return $msg+"should not call azure_vm_set_storagesku on a vm that is being recovered from backup"
}

Expand Down Expand Up @@ -771,9 +791,7 @@ fn azure_vm_backup_recover(instance, storagesku, backup_resgroup) {
osdiskid = $id
} else {
lun <= _azure_vm_backup_datadisk_lun($name)

idlun = ($id $lun)

datadisks <= append($datadisks, $idlun)
}
}
Expand All @@ -796,6 +814,12 @@ fn azure_vm_backup_recover(instance, storagesku, backup_resgroup) {
log("created os disk: "+$osdisk)

instance <= azure_vm_set_osdisk_id($instance, $osdisk)
instance <= azure_vm_set_datadisk_caching($instance, $caching)
# if $caching != "None" {
# OS disk do not support None caching
# Right now setting os disk caching on attached os disk do not work
# instance <= azure_vm_set_osdisk_caching($instance, $caching)
# }

log("creating VM")
azure_vm_create($instance)
Expand All @@ -814,14 +838,14 @@ fn azure_vm_backup_recover(instance, storagesku, backup_resgroup) {

diskname = $vmname+"-disk-"+$lun

d <= azure_disk_new($diskname, $resgroup, $location)
d <= azure_disk_set_source($d, $id)
d <= azure_disk_set_sku($d, $storagesku)
d <= azure_disk_new($diskname, $resgroup, $location)
d <= azure_disk_set_source($d, $id)
d <= azure_disk_set_sku($d, $storagesku)
diskid <= azure_disk_create($d)

log("created disk id: "+$diskid)
log("attaching on VM")
azure_vm_disk_attach($vmname, $resgroup, $diskid)
azure_vm_disk_attach_with_lun($vmname, $resgroup, $diskid, $caching, $lun)
log("attached")
}

Expand Down
4 changes: 2 additions & 2 deletions examples/azure/backup/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ range <= split($sequence, "\n")
print("creating %q disks with size %q\n", $vm_disks_count, $vm_disks_size)

for i in $range {
azure_vm_disk_attach_new($vm_name, $group, "disk" + $i, $vm_disks_size, "Premium_LRS")
azure_vm_disk_attach_new($vm_name, $group, "disk" + $i, $vm_disks_size, "Premium_LRS", "None")
}

echo "stopping VM"
Expand Down Expand Up @@ -151,6 +151,6 @@ backupvm <= new_vm_nodisk($backup_vm_name, $subnet_name)
backupvm <= azure_vm_set_ostype($backupvm, "linux")
echo "restoring backup"

azure_vm_backup_recover($backupvm, "Premium_LRS", $backups[0])
azure_vm_backup_recover($backupvm, "Premium_LRS", "None", $backups[0])
echo "finished with success"

10 changes: 6 additions & 4 deletions examples/azure/snapshots/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ echo "creating virtual machine"

create_vm($vm_name, $subnet_name, "Premium_LRS")

azure_vm_disk_attach_new($vm_name, $group, "premiumDisk", "100", "Premium_LRS")
azure_vm_disk_attach_new($vm_name, $group, "standardDisk", "200", "Standard_LRS")
azure_vm_disk_attach_new($vm_name, $group, "bigPremiumDisk", "1023", "Premium_LRS")
azure_vm_disk_attach_new($vm_name, $group, "premiumDisk", "100", "Premium_LRS", "None")
azure_vm_disk_attach_new($vm_name, $group, "standardDisk", "200", "Standard_LRS", "None")
azure_vm_disk_attach_new($vm_name, $group, "bigPremiumDisk", "1023", "Premium_LRS", "None")

echo "created main VM"
echo "creating backup VM"
Expand All @@ -113,6 +113,8 @@ fn log(msg) {
echo $ts + ":" + $msg
}

diskcaching = "None"

for id in $ids {
snapshot_name <= addsuffix("snapshot")

Expand All @@ -133,7 +135,7 @@ for id in $ids {

echo "created disk with success, attaching it to backup vm"

azure_vm_disk_attach($vm_backup_name, $group, $disk_name)
azure_vm_disk_attach($vm_backup_name, $group, $disk_name, $diskcaching)

echo "attached disk with success"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/azure/vm/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ range <= split($sequence, "\n")
print("creating %q disks with size %q\n", $vm_disks_count, $vm_disks_size)

for i in $range {
azure_vm_disk_attach_new($vm_name, $group, "disk"+$i, $vm_disks_size, "Premium_LRS")
azure_vm_disk_attach_new($vm_name, $group, "disk"+$i, $vm_disks_size, "Premium_LRS", "None")
}

echo "finished with success"
Expand Down
10 changes: 6 additions & 4 deletions tests/azure/testdata/attach_new_disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ vmname = $ARGS[2]
diskname = $ARGS[3]
size = $ARGS[4]
sku = $ARGS[5]
caching = $ARGS[6]

azure_login()

echo "creating new disk and attaching it"
echo "diskname: "+$diskname
echo "disksize: "+$size
echo "disksku: "+$sku
echo "name: "+$diskname
echo "size gb: "+$size
echo "sku: "+$sku
echo "caching: "+$caching

azure_vm_disk_attach_new($vmname, $resgroup, $diskname, $size, $sku)
azure_vm_disk_attach_new($vmname, $resgroup, $diskname, $size, $sku, $caching)

echo "done"
15 changes: 8 additions & 7 deletions tests/azure/testdata/attach_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import klb/azure/login
import klb/azure/vm
import klb/azure/disk

resgroup = $ARGS[1]
location = $ARGS[2]
vmname = $ARGS[3]
diskname = $ARGS[4]
disksku = $ARGS[5]
snapshotid = $ARGS[6]
resgroup = $ARGS[1]
location = $ARGS[2]
vmname = $ARGS[3]
diskname = $ARGS[4]
disksku = $ARGS[5]
snapshotid = $ARGS[6]
diskcaching = "None"

azure_login()

Expand All @@ -18,4 +19,4 @@ disk <= azure_disk_set_source($disk, $snapshotid)
disk <= azure_disk_set_sku($disk, $disksku)

azure_disk_create($disk)
azure_vm_disk_attach($vmname, $resgroup, $diskname)
azure_vm_disk_attach($vmname, $resgroup, $diskname, $diskcaching)
6 changes: 6 additions & 0 deletions tests/azure/testdata/create_vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ osdisk = $ARGS[8]
imageurn = $ARGS[9]
keyfile = $ARGS[10]
sku = $ARGS[11]
caching = $ARGS[12]

azure_login()

Expand All @@ -26,5 +27,10 @@ vm <= azure_vm_set_osdiskname($vm, $osdisk)
vm <= azure_vm_set_imageurn($vm, $imageurn)
vm <= azure_vm_set_publickeyfile($vm, $keyfile)
vm <= azure_vm_set_storagesku($vm, $sku)
if $caching != "None" {
# OS disk do not support no caching =/
vm <= azure_vm_set_osdisk_caching($vm, $caching)
}
vm <= azure_vm_set_datadisk_caching($vm, $caching)

azure_vm_create($vm)
5 changes: 3 additions & 2 deletions tests/azure/testdata/recover_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ subnet = $ARGS[6]
pubkey = $ARGS[7]
ostype = $ARGS[8]
storagesku = $ARGS[9]
bkpresgroup = $ARGS[10]
caching = $ARGS[10]
bkpresgroup = $ARGS[11]

azure_login()

Expand Down Expand Up @@ -47,7 +48,7 @@ vm <= azure_vm_set_ostype($vm, $ostype)

echo "creating vm from backup: "+$bkpresgroup

res <= azure_vm_backup_recover($vm, $storagesku, $bkpresgroup)
res <= azure_vm_backup_recover($vm, $storagesku, $caching, $bkpresgroup)

if $res != "" {
echo "error: "+$res
Expand Down
Loading

0 comments on commit 68e6531

Please sign in to comment.