docker-compose.ts 1.36 KB
Newer Older
1 2 3 4
import * as compose from 'docker-compose'
import * as shell from 'shelljs'
import * as path from 'path'

5 6 7 8 9 10
type ServiceNames =
  | 'batch_submitter'
  | 'dtl'
  | 'l2geth'
  | 'relayer'
  | 'verifier'
11
  | 'replica'
12 13

const OPS_DIRECTORY = path.join(process.cwd(), '../ops')
14 15 16 17 18 19
const DEFAULT_SERVICES: ServiceNames[] = [
  'batch_submitter',
  'dtl',
  'l2geth',
  'relayer',
]
20 21

export class DockerComposeNetwork {
22
  constructor(private readonly services: ServiceNames[] = DEFAULT_SERVICES) {}
23

24 25 26 27 28
  async up(options?: compose.IDockerComposeOptions) {
    const out = await compose.upMany(this.services, {
      cwd: OPS_DIRECTORY,
      ...options,
    })
29

30
    const { err, exitCode } = out
31

32 33 34 35 36 37
    if (!err || exitCode) {
      console.error(err)
      throw new Error(
        'Unexpected error when starting docker-compose network, dumping output'
      )
    }
38

39 40 41 42 43 44 45
    if (err.includes('Creating')) {
      console.info(
        '🐳 Tests required starting containers. Waiting for sequencer to ready.'
      )
      shell.exec(`${OPS_DIRECTORY}/scripts/wait-for-sequencer.sh`, {
        cwd: OPS_DIRECTORY,
      })
46
    }
47 48 49 50 51 52 53 54 55 56 57 58 59 60

    return out
  }

  async logs() {
    return compose.logs(this.services, { cwd: OPS_DIRECTORY })
  }

  async stop(service: ServiceNames) {
    return compose.stopOne(service, { cwd: OPS_DIRECTORY })
  }

  async rm() {
    return compose.rm({ cwd: OPS_DIRECTORY })
61
  }
62
}