Skip to content

Commit ad8c5b0

Browse files
committed
Add Docker support
1 parent 096ab46 commit ad8c5b0

File tree

1 file changed

+101
-3
lines changed

1 file changed

+101
-3
lines changed

src/commands/test.js

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { Binary } = require('binary-install-raw')
22
const os = require('os')
33
const chalk = require('chalk')
44
const fetch = require('node-fetch')
5+
const { exec } = require('child_process')
6+
const fs = require('fs')
57
const semver = require('semver')
68

79
const HELP = `
@@ -13,6 +15,7 @@ ${chalk.dim('Options:')}
1315
-h, --help Show usage information
1416
-l, --logs Logs to the console information about the OS, CPU model and download url (debugging purposes)
1517
-v, --version <tag> Choose the version of the rust binary that you want to be downloaded/used
18+
-d, --docker Launches Matchstick in a docker container
1619
`
1720

1821
module.exports = {
@@ -22,7 +25,7 @@ module.exports = {
2225
let { print } = toolbox
2326

2427
// Read CLI parameters
25-
let { f, force, h, help, l, logs, v, version } = toolbox.parameters.options
28+
let { f, force, h, help, l, logs, v, version, d, docker } = toolbox.parameters.options
2629
let datasource = toolbox.parameters.first
2730

2831
// Support both long and short option variants
@@ -37,6 +40,101 @@ module.exports = {
3740
return
3841
}
3942

43+
if (docker) {
44+
let url = `https://github.com/LimeChain/matchstick/releases/download/${version || '0.2.0a'}/binary-linux-20`
45+
46+
if (version) {
47+
await fetch(url)
48+
.then(response => {
49+
if (response.status === 404) {
50+
print.info(`Error: Invalid Matchstick version '${version}'`)
51+
process.exit(1)
52+
}
53+
})
54+
}
55+
56+
// TODO: Move these in separate file (in a function maybe)
57+
let contents = `FROM ubuntu:20.04
58+
ENV ARGS=""
59+
RUN apt update
60+
RUN apt install -y nodejs
61+
RUN apt install -y npm
62+
RUN apt install -y cmake
63+
RUN apt install -y git
64+
COPY ./ ./
65+
RUN mkdir ./tests/.tools
66+
WORKDIR ./tests/.tools
67+
RUN git clone --recursive https://github.com/WebAssembly/wabt
68+
WORKDIR wabt
69+
RUN git submodule update --init
70+
RUN mkdir build
71+
WORKDIR build
72+
RUN cmake ..
73+
RUN cmake --build .
74+
WORKDIR /
75+
RUN npm run codegen
76+
RUN npm run build
77+
RUN apt install -y postgresql
78+
RUN apt install -y curl
79+
RUN curl -OL ${url}
80+
RUN mv binary-linux-20 matchstick
81+
CMD ./matchstick \${ARGS}`
82+
83+
let dir = 'tests/.docker'
84+
85+
if (!fs.existsSync(dir)) {
86+
fs.mkdirSync(dir, { recursive: true })
87+
}
88+
89+
fs.writeFileSync('tests/.docker/Dockerfile', contents, (err) => {
90+
if (err) {
91+
print.info('A problem occurred while generating the Dockerfile. Please attend to the errors below.')
92+
print.info(err)
93+
} else {
94+
print.info('Successfully generated Dockerfile.')
95+
}
96+
})
97+
98+
exec(`docker build -f tests/.docker/Dockerfile -t matchstick . `, (error, stdout, stderr) => {
99+
print.info('Building Matchstick image...')
100+
101+
if (error) {
102+
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.')
103+
print.info(`error: ${error.message}`)
104+
}
105+
if (stderr) {
106+
print.info('A problem occurred while trying to build the Matchstick Docker image. Please attend to the errors below.')
107+
print.info(`stderr: ${stderr}`)
108+
}
109+
110+
let runCommand = `docker run -e ARGS="${datasource || ''}${coverage ? '-c' : ''}" --rm matchstick`
111+
112+
console.log('runCommand output')
113+
console.log(runCommand)
114+
print.info('runCommand output')
115+
print.info(runCommand)
116+
117+
exec(runCommand, (error, stdout, stderr) => {
118+
print.info('Running Matchstick image...')
119+
120+
if (error) {
121+
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.')
122+
print.info(`error: ${error.message}`)
123+
process.exit(1)
124+
}
125+
if (stderr) {
126+
print.info('A problem occurred while trying to run the Matchstick Docker image. Please attend to the errors below.')
127+
print.info(`stderr: ${stderr}`)
128+
process.exit(1)
129+
}
130+
print.info(stdout)
131+
process.exit()
132+
})
133+
})
134+
135+
return
136+
}
137+
40138
const platform = getPlatform(logs)
41139
if (!version) {
42140
let result = await fetch('https://api.github.com/repos/LimeChain/matchstick/releases/latest')
@@ -53,7 +151,7 @@ module.exports = {
53151
let binary = new Binary(platform, url, version)
54152
await binary.install(force)
55153
datasource ? binary.run(datasource) : binary.run()
56-
}
154+
},
57155
}
58156

59157
function getPlatform(logs) {
@@ -62,7 +160,7 @@ function getPlatform(logs) {
62160
const release = os.release()
63161
const cpuCore = os.cpus()[0]
64162
const majorVersion = semver.major(release)
65-
const isM1 = cpuCore.model.includes("Apple M1")
163+
const isM1 = cpuCore.model.includes('Apple M1')
66164

67165
if (logs) {
68166
console.log(`OS type: ${type}\nOS arch: ${arch}\nOS release: ${release}\nOS major version: ${majorVersion}\nCPU model: ${cpuCore.model}`)

0 commit comments

Comments
 (0)