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

Initialize attached windows disks in Azure ARM #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation;
import org.apache.brooklyn.location.jclouds.JcloudsMachineNamer;
import org.apache.brooklyn.location.ssh.SshMachineLocation;
import org.apache.brooklyn.location.winrm.WinRmMachineLocation;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse;
import org.jclouds.compute.domain.NodeMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -73,58 +75,63 @@ public MountedBlockDevice attachAndMountVolume(JcloudsMachineLocation machine, B
@Override
public void createFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptions filesystemOptions) {
JcloudsMachineLocation machine = attachedDevice.getMachine();
if (!(machine instanceof SshMachineLocation)) {
throw new IllegalStateException("Cannot create filesystem for "+machine+" of type "+machine.getClass().getName()+"; expected "+SshMachineLocation.class.getSimpleName());
}

String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
String filesystemType = filesystemOptions.getFilesystemType();
LOG.debug("Creating filesystem: device={}; osDeviceName={}, config={}", new Object[]{attachedDevice, osDeviceName, filesystemOptions});

// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
Map<String, ?> flags = MutableMap.of("allocatePTY", true);

int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Creating filesystem on volume", ImmutableList.of(
dontRequireTtyForSudo(),
waitForFileCmd(osDeviceName, 60),
installPackage(ImmutableMap.of("yum", "e4fsprogs"), null),
sudo("/sbin/mkfs -F -t " + filesystemType + " " + osDeviceName)));

if (exitCode != 0) {
throw new RuntimeException(format("Failed to create file system. machine=%s; osDeviceName=%s; filesystemType=%s",
machine, osDeviceName, filesystemType));
if (machine instanceof SshMachineLocation) {
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
String filesystemType = filesystemOptions.getFilesystemType();
LOG.debug("Creating filesystem: device={}; osDeviceName={}, config={}", new Object[]{attachedDevice, osDeviceName, filesystemOptions});

// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
Map<String, ?> flags = MutableMap.of("allocatePTY", true);

int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Creating filesystem on volume", ImmutableList.of(
dontRequireTtyForSudo(),
waitForFileCmd(osDeviceName, 60),
installPackage(ImmutableMap.of("yum", "e4fsprogs"), null),
sudo("/sbin/mkfs -F -t " + filesystemType + " " + osDeviceName)));

if (exitCode != 0) {
throw new RuntimeException(format("Failed to create file system. machine=%s; osDeviceName=%s; filesystemType=%s",
machine, osDeviceName, filesystemType));
}
} else if (machine instanceof WinRmMachineLocation) {
WinRmToolResponse response = ((WinRmMachineLocation)machine).executePsScript("Get-Disk | Where partitionstyle -eq 'raw' | " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could format a disk which you should not format this way.
I prefer passing explicit disk letter.

"Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | " +
"Format-Volume -FileSystem NTFS -NewFileSystemLabel \"datadisk\" -Confirm:$false");
if (response.getStatusCode() != 0) {
throw new RuntimeException(format("Failed to initialize disk. machine=%s; filesystemType=%s",
machine, filesystemOptions.getFilesystemType()));
}
}
}

@Override
public MountedBlockDevice mountFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptions options) {
JcloudsMachineLocation machine = attachedDevice.getMachine();
if (!(machine instanceof SshMachineLocation)) {
throw new IllegalStateException("Cannot mount filesystem for "+machine+" of type "+machine.getClass().getName()+"; expected "+SshMachineLocation.class.getSimpleName());
}

LOG.debug("Mounting filesystem: device={}; options={}", attachedDevice, options);
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
String mountPoint = options.getMountPoint();
String filesystemType = options.getFilesystemType();

// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
Map<String, ?> flags = MutableMap.of("allocatePTY", true);
int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Mounting EBS volume", ImmutableList.of(
dontRequireTtyForSudo(),
"echo making dir",
sudo("mkdir -p -m 755 " + mountPoint),
"echo updating fstab",
waitForFileCmd(osDeviceName, 60),
"echo \"" + osDeviceName + " " + mountPoint + " " + filesystemType + " noatime 0 0\" | " + sudo("tee -a /etc/fstab"),
"echo mounting device",
sudo("mount " + mountPoint),
"echo device mounted"
));

if (exitCode != 0) {
throw new RuntimeException(format("Failed to mount file system. machine=%s; osDeviceName=%s; mountPoint=%s; filesystemType=%s",
attachedDevice.getMachine(), osDeviceName, mountPoint, filesystemType));
if (machine instanceof SshMachineLocation) {

LOG.debug("Mounting filesystem: device={}; options={}", attachedDevice, options);
String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix());
String mountPoint = options.getMountPoint();
String filesystemType = options.getFilesystemType();

// NOTE: also adds an entry to fstab so the mount remains available after a reboot.
Map<String, ?> flags = MutableMap.of("allocatePTY", true);
int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Mounting EBS volume", ImmutableList.of(
dontRequireTtyForSudo(),
"echo making dir",
sudo("mkdir -p -m 755 " + mountPoint),
"echo updating fstab",
waitForFileCmd(osDeviceName, 60),
"echo \"" + osDeviceName + " " + mountPoint + " " + filesystemType + " noatime 0 0\" | " + sudo("tee -a /etc/fstab"),
"echo mounting device",
sudo("mount " + mountPoint),
"echo device mounted"
));

if (exitCode != 0) {
throw new RuntimeException(format("Failed to mount file system. machine=%s; osDeviceName=%s; mountPoint=%s; filesystemType=%s",
attachedDevice.getMachine(), osDeviceName, mountPoint, filesystemType));
}
}

return attachedDevice.mountedAt(options.getMountPoint());
Expand Down