|
22 | 22 | import java.io.File;
|
23 | 23 | import java.io.IOException;
|
24 | 24 | import java.net.URL;
|
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
25 | 28 | import org.apache.commons.io.FileUtils;
|
26 | 29 | import org.apache.commons.lang3.SystemUtils;
|
27 | 30 | import org.slf4j.Logger;
|
|
38 | 41 | */
|
39 | 42 | public class Util {
|
40 | 43 |
|
| 44 | + public static final String homebrewInstallationPath = "/opt/homebrew/bin/brew"; |
| 45 | + |
| 46 | + private static final String mariadbNotInstalledMessage = "No such keg: /opt/homebrew/Cellar/mariadb"; |
| 47 | + |
41 | 48 | private static final Logger logger = LoggerFactory.getLogger(Util.class);
|
42 | 49 |
|
43 | 50 | private Util() {
|
@@ -154,6 +161,56 @@ public static int extractFromClasspathToFile(String packagePath, File toDir) thr
|
154 | 161 | return counter;
|
155 | 162 | }
|
156 | 163 |
|
| 164 | + /** |
| 165 | + * Method to check for the MariaDb installation on the system and then install if not installed |
| 166 | + * @return @{@link boolean} representing whether MariaDb has been installed on the system |
| 167 | + */ |
| 168 | + public static boolean installMariaDbFromHomebrew(){ |
| 169 | + boolean mariadbIsInstalled = false; |
| 170 | + |
| 171 | + ProcessBuilder pb = new ProcessBuilder(homebrewInstallationPath + " list mariadb"); |
| 172 | + pb.redirectErrorStream(true); |
| 173 | + try { |
| 174 | + Process brewCheckMariadbIsInstalled = pb.start(); |
| 175 | + String checkOutput = new String(brewCheckMariadbIsInstalled.getInputStream().readAllBytes(), StandardCharsets.UTF_8); |
| 176 | + |
| 177 | + if(checkOutput.contains(mariadbNotInstalledMessage)){ |
| 178 | + brewCheckMariadbIsInstalled.destroy(); |
| 179 | + |
| 180 | + ProcessBuilder brewInstallMariaDb = new ProcessBuilder(homebrewInstallationPath + " install mariadb"); |
| 181 | + brewInstallMariaDb.redirectErrorStream(true); |
| 182 | + |
| 183 | + Process brewInstall = brewInstallMariaDb.start(); |
| 184 | + |
| 185 | + // Wait until Homebrew installs the latest MariaDb |
| 186 | + do { |
| 187 | + brewInstall.waitFor(15, TimeUnit.SECONDS); |
| 188 | + } while (brewInstall.isAlive()); |
| 189 | + |
| 190 | + String checkInstallOutput = new String(brewInstall.getInputStream().readAllBytes(), StandardCharsets.UTF_8); |
| 191 | + |
| 192 | + if(checkInstallOutput.toLowerCase().contains("error")){ |
| 193 | + throw new IllegalStateException("Failed to install mariadb with Homebrew - see " + checkInstallOutput); |
| 194 | + } |
| 195 | + |
| 196 | + // Cause it to clean up once it has installed |
| 197 | + brewInstall.destroy(); |
| 198 | + |
| 199 | + mariadbIsInstalled = true; |
| 200 | + } else{ |
| 201 | + mariadbIsInstalled = true; |
| 202 | + } |
| 203 | + } catch (IOException ioException) { |
| 204 | + logger.error("Attempted to check the installation status of MariaDb and executing Homebrew failed", ioException); |
| 205 | + throw new RuntimeException("Attempted to check the installation status of MariaDb and executing Homebrew failed"); |
| 206 | + } catch (InterruptedException interruptedException) { |
| 207 | + logger.error("The thread process was interrupted while waiting for MariaDb to finish installation", interruptedException); |
| 208 | + throw new RuntimeException("The thread process was interrupted while waiting for MariaDb to finish installation"); |
| 209 | + } |
| 210 | + |
| 211 | + return mariadbIsInstalled; |
| 212 | + } |
| 213 | + |
157 | 214 | @SuppressWarnings("null") private static void tryN(int n, long msToWait, Procedure<IOException> procedure) throws IOException {
|
158 | 215 | IOException lastIOException = null;
|
159 | 216 | int numAttempts = 0;
|
|
0 commit comments