|
13 | 13 | import org.apache.brooklyn.location.jclouds.JcloudsMachineLocation; |
14 | 14 | import org.apache.brooklyn.location.jclouds.JcloudsMachineNamer; |
15 | 15 | import org.apache.brooklyn.location.ssh.SshMachineLocation; |
| 16 | +import org.apache.brooklyn.location.winrm.WinRmMachineLocation; |
16 | 17 | import org.apache.brooklyn.util.collections.MutableMap; |
| 18 | +import org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse; |
17 | 19 | import org.jclouds.compute.domain.NodeMetadata; |
18 | 20 | import org.slf4j.Logger; |
19 | 21 | import org.slf4j.LoggerFactory; |
@@ -73,58 +75,79 @@ public MountedBlockDevice attachAndMountVolume(JcloudsMachineLocation machine, B |
73 | 75 | @Override |
74 | 76 | public void createFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptions filesystemOptions) { |
75 | 77 | 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)); |
| 78 | + if (machine instanceof SshMachineLocation) { |
| 79 | + String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix()); |
| 80 | + String filesystemType = filesystemOptions.getFilesystemType(); |
| 81 | + LOG.debug("Creating filesystem: device={}; osDeviceName={}, config={}", new Object[]{attachedDevice, osDeviceName, filesystemOptions}); |
| 82 | + |
| 83 | + // NOTE: also adds an entry to fstab so the mount remains available after a reboot. |
| 84 | + Map<String, ?> flags = MutableMap.of("allocatePTY", true); |
| 85 | + |
| 86 | + int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Creating filesystem on volume", ImmutableList.of( |
| 87 | + dontRequireTtyForSudo(), |
| 88 | + waitForFileCmd(osDeviceName, 60), |
| 89 | + installPackage(ImmutableMap.of("yum", "e4fsprogs"), null), |
| 90 | + sudo("/sbin/mkfs -F -t " + filesystemType + " " + osDeviceName))); |
| 91 | + |
| 92 | + if (exitCode != 0) { |
| 93 | + throw new RuntimeException(format("Failed to create file system. machine=%s; osDeviceName=%s; filesystemType=%s", |
| 94 | + machine, osDeviceName, filesystemType)); |
| 95 | + } |
| 96 | + } else if (machine instanceof WinRmMachineLocation) { |
| 97 | + String driveLetter = filesystemOptions.getMountPoint(); |
| 98 | + String driveLetterParam = Strings.isNullOrEmpty(driveLetter) ? "-AssignDriveLetter" : "-DriveLetter " + driveLetter; |
| 99 | + String volumeLabel = filesystemOptions.getVolumeLabel(); |
| 100 | + String volumeLabelParam = Strings.isNullOrEmpty(volumeLabel) ? "datadisk" : volumeLabel; |
| 101 | + WinRmToolResponse response = ((WinRmMachineLocation)machine).executePsScript("Get-Disk | Where partitionstyle -eq 'raw' | " + |
| 102 | + // AWS May create an instance store volume, which we need to exclude |
| 103 | + // See https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-volumes.html#instance-store-volume-map |
| 104 | + "Where {$_.SerialNumber -As [int] -NotIn 78..89 -Or $_.FriendlyName -Ne \"AWS PVDISK\"} | " + |
| 105 | + "Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition " + driveLetterParam + " -UseMaximumSize | " + |
| 106 | + "Format-Volume -FileSystem " + filesystemOptions.getFilesystemType() +" -NewFileSystemLabel \"" + volumeLabelParam + "\" -Confirm:$false"); |
| 107 | + if (response.getStatusCode() != 0) { |
| 108 | + throw new RuntimeException(format("Failed to initialize disk. machine=%s; filesystemType=%s; stdErr=%s;", |
| 109 | + machine, filesystemOptions.getFilesystemType(), response.getStdErr())); |
| 110 | + } |
| 111 | + } else { |
| 112 | + throw new IllegalStateException("Cannot create filesystem for " + machine + " of type " |
| 113 | + + machine.getClass().getName() + "; expected " + SshMachineLocation.class.getSimpleName() |
| 114 | + + " or " + WinRmMachineLocation.class.getSimpleName()); |
96 | 115 | } |
97 | 116 | } |
98 | 117 |
|
99 | 118 | @Override |
100 | 119 | public MountedBlockDevice mountFilesystem(AttachedBlockDevice attachedDevice, FilesystemOptions options) { |
101 | 120 | JcloudsMachineLocation machine = attachedDevice.getMachine(); |
102 | | - if (!(machine instanceof SshMachineLocation)) { |
103 | | - throw new IllegalStateException("Cannot mount filesystem for "+machine+" of type "+machine.getClass().getName()+"; expected "+SshMachineLocation.class.getSimpleName()); |
104 | | - } |
105 | | - |
106 | | - LOG.debug("Mounting filesystem: device={}; options={}", attachedDevice, options); |
107 | | - String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix()); |
108 | | - String mountPoint = options.getMountPoint(); |
109 | | - String filesystemType = options.getFilesystemType(); |
110 | | - |
111 | | - // NOTE: also adds an entry to fstab so the mount remains available after a reboot. |
112 | | - Map<String, ?> flags = MutableMap.of("allocatePTY", true); |
113 | | - int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Mounting EBS volume", ImmutableList.of( |
114 | | - dontRequireTtyForSudo(), |
115 | | - "echo making dir", |
116 | | - sudo("mkdir -p -m 755 " + mountPoint), |
117 | | - "echo updating fstab", |
118 | | - waitForFileCmd(osDeviceName, 60), |
119 | | - "echo \"" + osDeviceName + " " + mountPoint + " " + filesystemType + " noatime 0 0\" | " + sudo("tee -a /etc/fstab"), |
120 | | - "echo mounting device", |
121 | | - sudo("mount " + mountPoint), |
122 | | - "echo device mounted" |
123 | | - )); |
124 | | - |
125 | | - if (exitCode != 0) { |
126 | | - throw new RuntimeException(format("Failed to mount file system. machine=%s; osDeviceName=%s; mountPoint=%s; filesystemType=%s", |
127 | | - attachedDevice.getMachine(), osDeviceName, mountPoint, filesystemType)); |
| 121 | + if (machine instanceof SshMachineLocation) { |
| 122 | + LOG.debug("Mounting filesystem: device={}; options={}", attachedDevice, options); |
| 123 | + String osDeviceName = getOSDeviceName(attachedDevice.getDeviceSuffix()); |
| 124 | + String mountPoint = options.getMountPoint(); |
| 125 | + String filesystemType = options.getFilesystemType(); |
| 126 | + |
| 127 | + // NOTE: also adds an entry to fstab so the mount remains available after a reboot. |
| 128 | + Map<String, ?> flags = MutableMap.of("allocatePTY", true); |
| 129 | + int exitCode = ((SshMachineLocation)machine).execCommands(flags, "Mounting EBS volume", ImmutableList.of( |
| 130 | + dontRequireTtyForSudo(), |
| 131 | + "echo making dir", |
| 132 | + sudo("mkdir -p -m 755 " + mountPoint), |
| 133 | + "echo updating fstab", |
| 134 | + waitForFileCmd(osDeviceName, 60), |
| 135 | + "echo \"" + osDeviceName + " " + mountPoint + " " + filesystemType + " noatime 0 0\" | " + sudo("tee -a /etc/fstab"), |
| 136 | + "echo mounting device", |
| 137 | + sudo("mount " + mountPoint), |
| 138 | + "echo device mounted" |
| 139 | + )); |
| 140 | + |
| 141 | + if (exitCode != 0) { |
| 142 | + throw new RuntimeException(format("Failed to mount file system. machine=%s; osDeviceName=%s; mountPoint=%s; filesystemType=%s", |
| 143 | + attachedDevice.getMachine(), osDeviceName, mountPoint, filesystemType)); |
| 144 | + } |
| 145 | + } else if (machine instanceof WinRmMachineLocation) { |
| 146 | + LOG.debug("Ignoring mounting of filesystem on WinRmMachineLocation: device={}; options={}", attachedDevice, options); |
| 147 | + } else { |
| 148 | + throw new IllegalStateException("Cannot mount filesystem for " + machine + " of type " |
| 149 | + + machine.getClass().getName() + "; expected " + SshMachineLocation.class.getSimpleName() |
| 150 | + + " or " + WinRmMachineLocation.class.getSimpleName()); |
128 | 151 | } |
129 | 152 |
|
130 | 153 | return attachedDevice.mountedAt(options.getMountPoint()); |
|
0 commit comments