|
| 1 | +import ArgumentParser |
| 2 | +import System |
| 3 | + |
| 4 | +extension Main |
| 5 | +{ |
| 6 | + struct Init |
| 7 | + { |
| 8 | + @Argument |
| 9 | + var location:FilePath.Directory |
| 10 | + |
| 11 | + @Option( |
| 12 | + name: [.customLong("container"), .customShort("c")], |
| 13 | + help: "Container name") |
| 14 | + var container:String = "unidoc-mongod-container" |
| 15 | + |
| 16 | + @Flag( |
| 17 | + name: [.customLong("containerized"), .customShort("m")], |
| 18 | + help: """ |
| 19 | + Use containerized setup - this prevents documentation preview servers from running \ |
| 20 | + on the host, but allows preview servers to run from inside other Docker containers |
| 21 | + """) |
| 22 | + var containerized:Bool = false |
| 23 | + } |
| 24 | +} |
| 25 | +extension Main.Init:AsyncParsableCommand |
| 26 | +{ |
| 27 | + public |
| 28 | + static let configuration:CommandConfiguration = .init(commandName: "init") |
| 29 | + |
| 30 | + func run() async throws |
| 31 | + { |
| 32 | + try self.location.create() |
| 33 | + |
| 34 | + let installation:Installation = .init( |
| 35 | + docker_compose_yml: self.location / "docker-compose.yml", |
| 36 | + unidoc_rs_init_js: self.location / "unidoc-rs-init.js", |
| 37 | + unidoc_rs_conf: self.location / "unidoc-rs.conf", |
| 38 | + container: self.container, |
| 39 | + localhost: !self.containerized) |
| 40 | + |
| 41 | + try installation.create() |
| 42 | + |
| 43 | + try SystemProcess.init(command: "docker", |
| 44 | + "compose", |
| 45 | + "--file", "\(installation.docker_compose_yml)", |
| 46 | + "up", |
| 47 | + "--detach", |
| 48 | + "--wait", |
| 49 | + echo: true)() |
| 50 | + |
| 51 | + // Even though the container is ready, the `mongod` daemon within it may not be. |
| 52 | + // We need to wait for it to be ready before we can run the `mongosh` command. |
| 53 | + // We do this by pinging the `mongod` daemon until it responds. |
| 54 | + print("Waiting for mongod to start up...") |
| 55 | + |
| 56 | + var attempts:Int = 0 |
| 57 | + waiting: do |
| 58 | + { |
| 59 | + async |
| 60 | + let interval:Void = Task.sleep(for: .seconds(1)) |
| 61 | + do |
| 62 | + { |
| 63 | + try SystemProcess.init(command: "docker", |
| 64 | + "exec", |
| 65 | + "\(installation.container)", |
| 66 | + "mongosh", |
| 67 | + "--quiet", |
| 68 | + "--eval", "exit")() |
| 69 | + } |
| 70 | + catch let error |
| 71 | + { |
| 72 | + if attempts > 10 |
| 73 | + { |
| 74 | + throw error |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + attempts += 1 |
| 79 | + } |
| 80 | + |
| 81 | + try await interval |
| 82 | + continue waiting |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + print("Initializing replica set...") |
| 87 | + |
| 88 | + try SystemProcess.init(command: "docker", |
| 89 | + "exec", |
| 90 | + "--tty", |
| 91 | + "\(installation.container)", |
| 92 | + "/bin/mongosh", "--file", "/unidoc-rs-init.js", |
| 93 | + echo: true)() |
| 94 | + |
| 95 | + print("Successfully initialized MongoDB replica set!") |
| 96 | + print(" Docker compose file: \(installation.docker_compose_yml)") |
| 97 | + print(" Docker container: \(installation.container)") |
| 98 | + } |
| 99 | +} |
0 commit comments