Skip to content

Commit 326f78d

Browse files
committed
Initialize windows disk in Azure ARM - simplify code to not use the ARM API
1 parent 1f84ea3 commit 326f78d

3 files changed

Lines changed: 37 additions & 112 deletions

File tree

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

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation;
1414
import org.apache.brooklyn.location.jclouds.JcloudsMachineNamer;
1515
import org.apache.brooklyn.location.ssh.SshMachineLocation;
16+
import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
1617
import org.apache.brooklyn.util.collections.MutableMap;
18+
import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse;
1719
import org.jclouds.compute.domain.NodeMetadata;
1820
import org.slf4j.Logger;
1921
import org.slf4j.LoggerFactory;
@@ -91,38 +93,45 @@ public void createFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptio
9193
throw new RuntimeException(format("Failed to create file system. machine=%s; osDeviceName=%s; filesystemType=%s",
9294
machine, osDeviceName, filesystemType));
9395
}
96+
} else if (machine instanceof WinRmMachineLocation) {
97+
WinRmToolResponse response = ((WinRmMachineLocation)machine).executePsScript("Get-Disk | Where partitionstyle -eq 'raw' | " +
98+
"Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | " +
99+
"Format-Volume -FileSystem NTFS -NewFileSystemLabel \"datadisk\" -Confirm:$false");
100+
if (response.getStatusCode() != 0) {
101+
throw new RuntimeException(format("Failed to initialize disk. machine=%s; filesystemType=%s",
102+
machine, filesystemOptions.getFilesystemType()));
103+
}
94104
}
95105
}
96106

97107
@Override
98108
public MountedBlockDevice mountFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptions options) {
99109
JcloudsMachineLocation machine = attachedDevice.getMachine();
100-
if (!(machine instanceof SshMachineLocation)) {
101-
throw new IllegalStateException("Cannot mount filesystem for "+machine+" of type "+machine.getClass().getName()+"; expected "+SshMachineLocation.class.getSimpleName());
102-
}
103-
104-
LOG.debug("Mounting filesystem: device={}; options={}", attachedDevice, options);
105-
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
106-
String mountPoint = options.getMountPoint();
107-
String filesystemType = options.getFilesystemType();
110+
if (machine instanceof SshMachineLocation) {
108111

109-
// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
110-
Map<String, ?> flags = MutableMap.of("allocatePTY", true);
111-
int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Mounting EBS volume", ImmutableList.of(
112-
dontRequireTtyForSudo(),
113-
"echo making dir",
114-
sudo("mkdir -p -m 755 " + mountPoint),
115-
"echo updating fstab",
116-
waitForFileCmd(osDeviceName, 60),
117-
"echo \"" + osDeviceName + " " + mountPoint + " " + filesystemType + " noatime 0 0\" | " + sudo("tee -a /etc/fstab"),
118-
"echo mounting device",
119-
sudo("mount " + mountPoint),
120-
"echo device mounted"
121-
));
112+
LOG.debug("Mounting filesystem: device={}; options={}", attachedDevice, options);
113+
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
114+
String mountPoint = options.getMountPoint();
115+
String filesystemType = options.getFilesystemType();
116+
117+
// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
118+
Map<String, ?> flags = MutableMap.of("allocatePTY", true);
119+
int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Mounting EBS volume", ImmutableList.of(
120+
dontRequireTtyForSudo(),
121+
"echo making dir",
122+
sudo("mkdir -p -m 755 " + mountPoint),
123+
"echo updating fstab",
124+
waitForFileCmd(osDeviceName, 60),
125+
"echo \"" + osDeviceName + " " + mountPoint + " " + filesystemType + " noatime 0 0\" | " + sudo("tee -a /etc/fstab"),
126+
"echo mounting device",
127+
sudo("mount " + mountPoint),
128+
"echo device mounted"
129+
));
122130

123-
if (exitCode != 0) {
124-
throw new RuntimeException(format("Failed to mount file system. machine=%s; osDeviceName=%s; mountPoint=%s; filesystemType=%s",
125-
attachedDevice.getMachine(), osDeviceName, mountPoint, filesystemType));
131+
if (exitCode != 0) {
132+
throw new RuntimeException(format("Failed to mount file system. machine=%s; osDeviceName=%s; mountPoint=%s; filesystemType=%s",
133+
attachedDevice.getMachine(), osDeviceName, mountPoint, filesystemType));
134+
}
126135
}
127136

128137
return attachedDevice.mountedAt(options.getMountPoint());

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
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;
1514
import org.apache.brooklyn.location.jclouds.JcloudsLocation;
1615
import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation;
17-
import org.apache.brooklyn.util.exceptions.Exceptions;
1816
import org.apache.brooklyn.util.repeat.Repeater;
1917
import org.apache.brooklyn.util.text.Identifiers;
2018
import org.apache.brooklyn.util.text.StringShortener;
2119
import org.apache.brooklyn.util.text.Strings;
2220
import org.apache.brooklyn.util.time.Duration;
2321
import org.jclouds.azurecompute.arm.AzureComputeApi;
2422
import org.jclouds.azurecompute.arm.domain.DataDisk;
25-
import org.jclouds.azurecompute.arm.domain.Deployment;
2623
import org.jclouds.azurecompute.arm.domain.Disk;
2724
import org.jclouds.azurecompute.arm.domain.ManagedDiskParameters;
2825
import org.jclouds.azurecompute.arm.domain.ResourceGroup;
@@ -31,16 +28,12 @@
3128
import org.jclouds.azurecompute.arm.domain.VirtualMachine;
3229
import org.jclouds.azurecompute.arm.domain.VirtualMachineProperties;
3330
import org.jclouds.azurecompute.arm.features.DiskApi;
34-
import org.jclouds.azurecompute.arm.features.DeploymentApi;
3531
import org.jclouds.azurecompute.arm.features.ResourceGroupApi;
3632
import org.jclouds.azurecompute.arm.features.StorageAccountApi;
3733
import org.jclouds.azurecompute.arm.features.VirtualMachineApi;
3834
import org.slf4j.Logger;
3935
import org.slf4j.LoggerFactory;
4036

41-
import java.io.IOException;
42-
import java.nio.file.Files;
43-
import java.nio.file.Paths;
4437
import java.util.List;
4538
import java.util.concurrent.Callable;
4639
import java.util.concurrent.TimeUnit;
@@ -161,11 +154,9 @@ private AttachedBlockDevice createAndAttachBlockDevice(JcloudsMachineLocation ma
161154
}
162155

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

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

170161
BlockDevice blockDevice = new AzureArmBlockDevice(location, disk, resourceGroupName.get(), storageAccountName);
171162
return blockDevice.attachedTo(machine, getVolumeDeviceName(options.getDeviceSuffix()));
@@ -199,7 +190,7 @@ private void deleteStorageAccount(AzureComputeApi api, String resourceGroupName,
199190
}
200191
}
201192

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

222213
vmApi.createOrUpdate(vmName, newVm.location(), newVm.properties(), newVm.tags(), newVm.plan());
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;
214+
return waitDiskToAppear(diskApi, diskName, TIMEOUT);
236215
}
237216

238217
private String getRegionName(JcloudsLocation location) {

blockstore/src/test/resources/customscriptextension.json

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)