|
| 1 | +import static java.lang.Integer.parseInt; |
| 2 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 3 | +import static java.nio.file.Files.readString; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.net.Socket; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.Comparator; |
| 11 | + |
| 12 | +public class SetupMinio { |
| 13 | + |
| 14 | + static final String MINIO_EXE_PATH = fromEnv("MINIO_EXE", "../minio/minio"); |
| 15 | + static final String PID_PATH = fromEnv("MINIO_PID", "../minio/minio.pid"); |
| 16 | + static final String MC_EXE_PATH = fromEnv("MC_EXE", "../minio/mc"); |
| 17 | + static final String DATA_PATH = fromEnv("MINIO_DATA", "./build/data1"); |
| 18 | + static final String MINIO_HOST = fromEnv("MINIO_HOST", "127.0.0.1"); |
| 19 | + static final String MINIO_PORT = fromEnv("MINIO_PORT", "9000"); |
| 20 | + |
| 21 | + public static void main(String[] args) throws Exception { |
| 22 | + killMinioServer(); |
| 23 | + deleteMinioData(); |
| 24 | + setupMinio(); |
| 25 | + } |
| 26 | + |
| 27 | + static void killMinioServer() throws Exception { |
| 28 | + Path pidPath = Paths.get(PID_PATH); |
| 29 | + if (!Files.exists(pidPath)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + long pid = Long.parseLong(readString(pidPath, UTF_8)); |
| 33 | + System.out.println("Killing Minio server process, pid: " + pid + " ..."); |
| 34 | + new ProcessBuilder("/usr/bin/kill", String.valueOf(pid)).inheritIO().start().waitFor(); |
| 35 | + Files.delete(pidPath); |
| 36 | + } |
| 37 | + |
| 38 | + static void deleteMinioData() throws Exception { |
| 39 | + Path minioDataPath = Paths.get(DATA_PATH); |
| 40 | + if (!Files.exists(minioDataPath)) { |
| 41 | + return; |
| 42 | + } |
| 43 | + System.out.println("Deleting Minio data: " + minioDataPath + " ..."); |
| 44 | + Files.walk(minioDataPath).sorted(Comparator.reverseOrder()).forEach(p -> { |
| 45 | + try { |
| 46 | + Files.delete(p); |
| 47 | + } catch (IOException e) { |
| 48 | + throw new RuntimeException("Failed to delete " + p, e); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + static void setupMinio() throws Exception { |
| 54 | + System.out.println("Starting Minio server ..."); |
| 55 | + Process minioServerProcess = |
| 56 | + new ProcessBuilder(MINIO_EXE_PATH, "server", "--address", MINIO_HOST + ":" + MINIO_PORT, DATA_PATH) |
| 57 | + .inheritIO() |
| 58 | + .start(); |
| 59 | + Files.write(Paths.get(PID_PATH), String.valueOf(minioServerProcess.pid()).getBytes(UTF_8)); |
| 60 | + boolean minioServerStarted = false; |
| 61 | + for (int i = 0; i < 16; i++) { |
| 62 | + try (Socket sock = new Socket(MINIO_HOST, parseInt(MINIO_PORT))) { |
| 63 | + minioServerStarted = true; |
| 64 | + break; |
| 65 | + } catch (IOException e) { |
| 66 | + Thread.sleep(1000); |
| 67 | + } |
| 68 | + } |
| 69 | + if (!minioServerStarted) { |
| 70 | + throw new RuntimeException("Cannot start Minio"); |
| 71 | + } |
| 72 | + Thread.sleep(2000); // improve log output |
| 73 | + System.out.println("Minio server started, pid: " + minioServerProcess.pid() + ", creating bucket ..."); |
| 74 | + int mcAliasStatus = new ProcessBuilder(MC_EXE_PATH, "alias", "set", "local", |
| 75 | + "http://" + MINIO_HOST + ":" + MINIO_PORT, "minioadmin", "minioadmin") |
| 76 | + .inheritIO() |
| 77 | + .start() |
| 78 | + .waitFor(); |
| 79 | + if (mcAliasStatus != 0) { |
| 80 | + killMinioServer(); |
| 81 | + throw new RuntimeException("Minio mc alias set error, status: " + mcAliasStatus); |
| 82 | + } |
| 83 | + int mcMbStatus = new ProcessBuilder(MC_EXE_PATH, "mb", "local/bucket1").inheritIO().start().waitFor(); |
| 84 | + if (mcMbStatus != 0) { |
| 85 | + killMinioServer(); |
| 86 | + throw new RuntimeException("Minio mc mb error, status: " + mcAliasStatus); |
| 87 | + } |
| 88 | + System.out.println("Minio server set up successfully"); |
| 89 | + } |
| 90 | + |
| 91 | + static String fromEnv(String envVarName, String defaultValue) { |
| 92 | + String env = System.getenv(envVarName); |
| 93 | + if (null != env) { |
| 94 | + return env; |
| 95 | + } |
| 96 | + return defaultValue; |
| 97 | + } |
| 98 | +} |
0 commit comments