Skip to content

Commit 1f84ea3

Browse files
committed
Implement attached windows disk initialization in Azure ARM
1 parent a1137ec commit 1f84ea3

3 files changed

Lines changed: 106 additions & 24 deletions

File tree

blockstore/src/main/java/brooklyn/location/blockstore/AbstractVolumeManager.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,24 @@ public MountedBlockDevice attachAndMountVolume(JcloudsMachineLocation machine, B
7373
@Override
7474
public void createFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptions filesystemOptions) {
7575
JcloudsMachineLocation machine = attachedDevice.getMachine();
76-
if (!(machine instanceof SshMachineLocation)) {
77-
throw new IllegalStateException("Cannot create filesystem for "+machine+" of type "+machine.getClass().getName()+"; expected "+SshMachineLocation.class.getSimpleName());
78-
}
79-
80-
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
81-
String filesystemType = filesystemOptions.getFilesystemType();
82-
LOG.debug("Creating filesystem: device={}; osDeviceName={}, config={}", new Object[]{attachedDevice, osDeviceName, filesystemOptions});
83-
84-
// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
85-
Map<String, ?> flags = MutableMap.of("allocatePTY", true);
86-
87-
int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Creating filesystem on volume", ImmutableList.of(
88-
dontRequireTtyForSudo(),
89-
waitForFileCmd(osDeviceName, 60),
90-
installPackage(ImmutableMap.of("yum", "e4fsprogs"), null),
91-
sudo("/sbin/mkfs -F -t " + filesystemType + " " + osDeviceName)));
92-
93-
if (exitCode != 0) {
94-
throw new RuntimeException(format("Failed to create file system. machine=%s; osDeviceName=%s; filesystemType=%s",
95-
machine, osDeviceName, filesystemType));
76+
if (machine instanceof SshMachineLocation) {
77+
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
78+
String filesystemType = filesystemOptions.getFilesystemType();
79+
LOG.debug("Creating filesystem: device={}; osDeviceName={}, config={}", new Object[]{attachedDevice, osDeviceName, filesystemOptions});
80+
81+
// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
82+
Map<String, ?> flags = MutableMap.of("allocatePTY", true);
83+
84+
int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Creating filesystem on volume", ImmutableList.of(
85+
dontRequireTtyForSudo(),
86+
waitForFileCmd(osDeviceName, 60),
87+
installPackage(ImmutableMap.of("yum", "e4fsprogs"), null),
88+
sudo("/sbin/mkfs -F -t " + filesystemType + " " + osDeviceName)));
89+
90+
if (exitCode != 0) {
91+
throw new RuntimeException(format("Failed to create file system. machine=%s; osDeviceName=%s; filesystemType=%s",
92+
machine, osDeviceName, filesystemType));
93+
}
9694
}
9795
}
9896

blockstore/src/main/java/brooklyn/location/blockstore/azure/arm/AzureArmVolumeManager.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
import com.google.common.base.Optional;
1212
import com.google.common.collect.ImmutableList;
1313
import com.google.common.collect.ImmutableSet;
14+
import com.google.common.net.UrlEscapers;
1415
import org.apache.brooklyn.location.jclouds.JcloudsLocation;
1516
import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation;
17+
import org.apache.brooklyn.util.exceptions.Exceptions;
1618
import org.apache.brooklyn.util.repeat.Repeater;
1719
import org.apache.brooklyn.util.text.Identifiers;
1820
import org.apache.brooklyn.util.text.StringShortener;
1921
import org.apache.brooklyn.util.text.Strings;
2022
import org.apache.brooklyn.util.time.Duration;
2123
import org.jclouds.azurecompute.arm.AzureComputeApi;
2224
import org.jclouds.azurecompute.arm.domain.DataDisk;
25+
import org.jclouds.azurecompute.arm.domain.Deployment;
2326
import org.jclouds.azurecompute.arm.domain.Disk;
2427
import org.jclouds.azurecompute.arm.domain.ManagedDiskParameters;
2528
import org.jclouds.azurecompute.arm.domain.ResourceGroup;
@@ -28,12 +31,16 @@
2831
import org.jclouds.azurecompute.arm.domain.VirtualMachine;
2932
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties;
3033
import org.jclouds.azurecompute.arm.features.DiskApi;
34+
import org.jclouds.azurecompute.arm.features.DeploymentApi;
3135
import org.jclouds.azurecompute.arm.features.ResourceGroupApi;
3236
import org.jclouds.azurecompute.arm.features.StorageAccountApi;
3337
import org.jclouds.azurecompute.arm.features.VirtualMachineApi;
3438
import org.slf4j.Logger;
3539
import org.slf4j.LoggerFactory;
3640

41+
import java.io.IOException;
42+
import java.nio.file.Files;
43+
import java.nio.file.Paths;
3744
import java.util.List;
3845
import java.util.concurrent.Callable;
3946
import java.util.concurrent.TimeUnit;
@@ -154,9 +161,11 @@ private AttachedBlockDevice createAndAttachBlockDevice(JcloudsMachineLocation ma
154161
}
155162

156163
int numExistingDisks = coundDataDisks(vm);
157-
int lun = numExistingDisks; // starts from 0, so if have one disk already then next will be "1"
164+
int lun = numExistingDisks; // starts from 0, so if have one disk already then next will be "1"
158165

159-
Disk disk = addDisk(vmApi, diskApi, vm, options.getSizeInGb(), lun);
166+
DeploymentApi deploymentApi = api.getDeploymentApi(resourceGroupName.get());
167+
168+
Disk disk = addDisk(vmApi, diskApi, vm, options.getSizeInGb(), lun, deploymentApi);
160169

161170
BlockDevice blockDevice = new AzureArmBlockDevice(location, disk, resourceGroupName.get(), storageAccountName);
162171
return blockDevice.attachedTo(machine, getVolumeDeviceName(options.getDeviceSuffix()));
@@ -190,7 +199,7 @@ private void deleteStorageAccount(AzureComputeApi api, String resourceGroupName,
190199
}
191200
}
192201

193-
private Disk addDisk(VirtualMachineApi vmApi, DiskApi diskApi, VirtualMachine vm, int diskSizeGB, int lun) {
202+
private Disk addDisk(VirtualMachineApi vmApi, DiskApi diskApi, VirtualMachine vm, int diskSizeGB, int lun, DeploymentApi deploymentApi) {
194203
String vmName = vm.name();
195204
VirtualMachineProperties oldProperties = vm.properties();
196205
StorageProfile oldStorageProfile = oldProperties.storageProfile();
@@ -211,7 +220,19 @@ private Disk addDisk(VirtualMachineApi vmApi, DiskApi diskApi, VirtualMachine vm
211220
VirtualMachine newVm = vm.toBuilder().properties(newProperties).build();
212221

213222
vmApi.createOrUpdate(vmName, newVm.location(), newVm.properties(), newVm.tags(), newVm.plan());
214-
return waitDiskToAppear(diskApi, diskName, TIMEOUT);
223+
Disk result = waitDiskToAppear(diskApi, diskName, TIMEOUT);
224+
225+
if (vm.location().contains("Win")) { //TODO make better check
226+
String deploymentName = "jc" + System.currentTimeMillis();
227+
try {
228+
String deploymentTemplate = UrlEscapers.urlFormParameterEscaper().escape(new String(Files.readAllBytes(Paths.get("customscriptextension.json"))));
229+
Deployment deployment = deploymentApi.create(deploymentName, deploymentTemplate);
230+
} catch (IOException e) {
231+
Exceptions.propagate("Failed to read customscriptextension.json", e);
232+
}
233+
}
234+
235+
return result;
215236
}
216237

217238
private String getRegionName(JcloudsLocation location) {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"properties": {
3+
"template": {
4+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
5+
"contentVersion": "1.0.0.0",
6+
"parameters": {
7+
"newStorageAccountName": {
8+
"type":"string",
9+
"metadata": {
10+
"description":"Name of the Storage Account"
11+
}
12+
},
13+
"storageAccountType": {
14+
"type":"string",
15+
"defaultValue":"Standard_LRS",
16+
"allowedValues": [
17+
"Standard_LRS","Standard_GRS","Standard_ZRS"
18+
],
19+
"metadata": {
20+
"description": "Storage Account type"
21+
}
22+
},
23+
"location": {
24+
"type": "string",
25+
"allowedValues": [
26+
"East US",
27+
"West US",
28+
"West Europe",
29+
"East Asia",
30+
"Southeast Asia"
31+
],
32+
"metadata": {
33+
"description": "Location of storage accoun"
34+
}
35+
}
36+
},
37+
"variables": { },
38+
"resources": [
39+
{
40+
"type":"Microsoft.Compute/virtualMachines/extensions",
41+
"name":"[concat(variables('vmName'),'/InitialiseDisks')]",
42+
"apiVersion":"api-version=2016-04-30-preview",
43+
"location":"[resourceGroup().location]",
44+
"dependsOn":[
45+
"[concat('Microsoft.Compute/virtualMachines/', variables('vmName'))]"
46+
],
47+
"properties":{
48+
"publisher":"Microsoft.Compute",
49+
"type":"CustomScriptExtension",
50+
"typeHandlerVersion":"1.2",
51+
"settings":{
52+
"fileUris":[
53+
"https://ampdisks427.file.core.windows.net/initialize-disk/initialize-disk.ps1"
54+
],
55+
"commandToExecute": "powershell -ExecutionPolicy Unrestricted -file initialize-disk.ps1"
56+
}
57+
}
58+
}
59+
],
60+
"outputs": { }
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)