Skip to content

Commit a004cd5

Browse files
abrousseau001vorburger
authored andcommitted
More work done, should be able to install MariaDb from Homebrew now
1 parent daf12f3 commit a004cd5

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

mariaDB4j-core/src/main/java/ch/vorburger/mariadb4j/DB.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@
5151
* @author Michael Vorburger
5252
* @author Michael Seaton
5353
* @author Gordon Little
54+
* @author Adam Brousseau
5455
*/
5556
public class DB {
5657

5758
private static final Logger logger = LoggerFactory.getLogger(DB.class);
5859

59-
private static final String homebrewInstallationPath = "/opt/homebrew/bin/brew";
60-
6160
protected final DBConfiguration configuration;
6261

6362
private File baseDir;
@@ -412,7 +411,7 @@ protected void unpackEmbeddedDb() {
412411
// Check for Homebrew, before doing anything else
413412
// If HomeBrew is not installed, then throw a runtime error, similar to how the below extraction method
414413
// does below for other operating systems
415-
if(configuration.isMacOs() && !Util.doesExecutableExistAndIsExecutable(new File(homebrewInstallationPath)))
414+
if(configuration.isMacOs() && !Util.doesExecutableExistAndIsExecutable(new File(Util.homebrewInstallationPath)))
416415
{
417416
throw new RuntimeException("Homebrew must be installed on the system before using this library");
418417
}
@@ -425,7 +424,7 @@ protected void unpackEmbeddedDb() {
425424
}
426425

427426
// Windows, Linux, and any other supported OS can have their binaries extracted as normal
428-
else if(!configuration.isMacOs()) {
427+
if(!configuration.isMacOs()) {
429428
try {
430429
Util.extractFromClasspathToFile(configuration.getBinariesClassPathLocation(), baseDir);
431430
if (!configuration.isWindows()) {
@@ -439,6 +438,10 @@ else if(!configuration.isMacOs()) {
439438
throw new RuntimeException("Error unpacking embedded DB", e);
440439
}
441440
}
441+
else{
442+
if(!Util.installMariaDbFromHomebrew())
443+
throw new RuntimeException("Error installing MariaDB from Homebrew");
444+
}
442445
}
443446

444447
/**

mariaDB4j-core/src/main/java/ch/vorburger/mariadb4j/Util.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.io.File;
2323
import java.io.IOException;
2424
import java.net.URL;
25+
import java.nio.charset.StandardCharsets;
26+
import java.util.concurrent.TimeUnit;
27+
2528
import org.apache.commons.io.FileUtils;
2629
import org.apache.commons.lang3.SystemUtils;
2730
import org.slf4j.Logger;
@@ -38,6 +41,10 @@
3841
*/
3942
public class Util {
4043

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+
4148
private static final Logger logger = LoggerFactory.getLogger(Util.class);
4249

4350
private Util() {
@@ -154,6 +161,56 @@ public static int extractFromClasspathToFile(String packagePath, File toDir) thr
154161
return counter;
155162
}
156163

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+
157214
@SuppressWarnings("null") private static void tryN(int n, long msToWait, Procedure<IOException> procedure) throws IOException {
158215
IOException lastIOException = null;
159216
int numAttempts = 0;

0 commit comments

Comments
 (0)