Skip to content

Commit

Permalink
ready for arm image
Browse files Browse the repository at this point in the history
  • Loading branch information
smashah committed May 26, 2022
1 parent ad53df1 commit 94e4e9b
Show file tree
Hide file tree
Showing 6 changed files with 502 additions and 35 deletions.
95 changes: 61 additions & 34 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,56 +1,83 @@
# syntax = docker/dockerfile:1.3-labs
FROM node:current-stretch-slim
COPY . /usr/src/app
FROM node:current-bullseye-slim
ENV APP_DIR=/usr/src/app


ENV SKIP_GITIGNORE_CHECK true
ENV NODE_ENV production
ENV PORT 8080

# Add your custom ENV vars here:
ENV WA_POPUP true
ENV IS_DOCKER=true
ENV WA_DISABLE_SPINS true
ENV WA_PORT $PORT
ENV WA_EXECUTABLE_PATH=/usr/bin/google-chrome
ENV CHROME_PATH=${WA_EXECUTABLE_PATH}
ENV WA_USE_CHROME=true

ENV PUPPETEER_CHROMIUM_REVISION=${PUPPETEER_CHROMIUM_REVISION}
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PLAYWRIGHT_BROWSERS_PATH=${APP_DIR}

COPY . $APP_DIR

WORKDIR $APP_DIR

RUN <<eot bash
mkdir -p /usr/src/app
mkdir -p /usr/src/app/node_modules
mkdir -p /sessions
apt update
apt install nano wget --no-install-recommends -y
apt upgrade -y
cd /tmp
wget -q --no-check-certificate https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
apt install ./google-chrome-stable_current_amd64.deb -y
rm google-chrome-stable_current_amd64.deb
apt install nano -y
dpkg --print-architecture
if [ $(dpkg --print-architecture) == "arm64" ];
then
echo "Installing arm64 dependencies"
cd $APP_DIR
apt -y -qq install libgtk2.0-0 libsm6 libatk-bridge2.0-0 libc6-dev libdrm2 libice6 libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm-dev libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
PUPPETEER_SKIP_DOWNLOAD=true npm i puppeteer playwright-core os util extract-zip fs-extra lodash node-fetch rimraf
dpigs
npm run postinstall
npm uninstall playwright-core os util extract-zip fs-extra lodash node-fetch rimraf
else
echo "Installing dependencies"
apt install nano wget --no-install-recommends -y
apt upgrade -y
cd /tmp
wget -q --no-check-certificate https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
apt install ./google-chrome-stable_current_amd64.deb -y
rm google-chrome-stable_current_amd64.deb
export PUPPETEER_SKIP_DOWNLOAD=true
ln -s /usr/bin/google-chrome-stable /usr/bin/google-chrome
cd /opt/google/chrome
rm -rf WidevineCdm/
cd locales
ls | grep -v file.txt | xargs rm
fi
apt autoremove -y
rm -rf /var/lib/apt/lists/*
rm -rf /usr/share/doc/*
rm -rf /usr/share/icons/*
cd /opt/google/chrome
rm -rf WidevineCdm/
cd locales
ls | grep -v file.txt | xargs rm
groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser
mkdir -p /home/pptruser/Downloads
chown -R pptruser:pptruser /home/pptruser
chown -R pptruser:pptruser /sessions
chown -R pptruser:pptruser /usr/src/app/node_modules
groupadd -r owauser && useradd -r -g owauser -G audio,video owauser
mkdir -p /home/owauser/Downloads
chown -R owauser:owauser /home/owauser
chown -R owauser:owauser /sessions
chown -R owauser:owauser /usr/src/app/node_modules
chown -R owauser:owauser ${WA_EXECUTABLE_PATH}
chown -R owauser:owauser /usr/bin/google-chrome
chown -R owauser:owauser /usr/bin/google-chrome
cd /usr/src/app
chown -R owauser:owauser /usr/src/app
npm i @open-wa/wa-automate@latest --ignore-scripts
chown -R pptruser:pptruser /usr/src/app
npm cache clean --force
eot

WORKDIR /usr/src/app


# skip the puppeteer browser download
ENV PUPPETEER_SKIP_DOWNLOAD true
ENV NODE_ENV production
ENV PORT 8080

# Add your custom ENV vars here:
ENV WA_USE_CHROME true
ENV WA_POPUP true
ENV WA_DISABLE_SPINS true
ENV WA_PORT $PORT
ENV WA_EXECUTABLE_PATH /usr/bin/google-chrome-stable

RUN npm prune --production && chown -R owauser:owauser $APP_DIR
EXPOSE $PORT

# test with root later
USER pptruser
USER owauser


ENTRYPOINT ["./start.sh", "--in-docker", "--qr-timeout", "0", "--popup", "--debug"]
52 changes: 52 additions & 0 deletions Dockerfile.browserless
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
FROM browserless/chrome

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

#install chrome
# RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list

# RUN apt update

# RUN apt install google-chrome-stable fonts-freefont-ttf libxss1 --no-install-recommends -y \
# && rm -rf /var/lib/apt/lists/*

# RUN apt upgrade -y

# skip the puppeteer browser download
ENV PUPPETEER_SKIP_DOWNLOAD true

RUN npm i @open-wa/wa-automate@latest
# Add user so we don't need --no-sandbox.
# same layer as npm install to keep re-chowned files from using up several hundred MBs more space
# RUN groupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \
# && mkdir -p /home/pptruser/Downloads \
# && chown -R pptruser:pptruser /home/pptruser \
# && chown -R pptruser:pptruser /usr/src/app \
# && chown -R pptruser:pptruser /sessions \
# && chown -R pptruser:pptruser /usr/src/app/node_modules

# RUN mkdir -p /sessions

# USER bl

COPY . /usr/src/app

ENV NODE_ENV production

ENV IN_DOCKER true

# Add your custom ENV vars here:
ENV WA_USE_CHROME true
ENV WA_POPUP true
ENV WA_DISABLE_SPINS true

#PORT will most likely be set by your cloud provider. If not, uncomment the next line and set it here
# ENV PORT 8080

ENV PORT=8080
ENV WA_PORT=$PORT
EXPOSE $PORT

ENTRYPOINT [ "node", "./node_modules/@open-wa/wa-automate/bin/server.js", "--use-chrome", " --in-docker", "--qr-timeout", "0", "--popup"]
129 changes: 129 additions & 0 deletions env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* eslint-disable no-undef */
/* eslint-disable no-useless-escape */
/**
* Taken from https://github.com/browserless/chrome/blob/master/env.js
*
* By @joelgriffith
*/
import os from 'os'
import playwright from 'playwright-core'
import puppeteer from 'puppeteer'
const IS_DOCKER = process.env.IS_DOCKER === 'true';

const USE_CHROME_STABLE = process.env.USE_CHROME_STABLE && process.env.USE_CHROME_STABLE === 'true';

const MAC = 'MAC';
const WINDOWS = 'WINDOWS';
const LINUX = 'LINUX';
const LINUX_ARM64 = 'LINUX_ARM64';

const CHROME_BINARY_PATHS = {
LINUX: '/usr/bin/google-chrome',
MAC: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome',
WIN: 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe',
};

const PLATFORM = os.platform() === 'win32' ?
WINDOWS :
os.platform() === 'darwin' ?
MAC :
os.arch() === 'arm64' ?
LINUX_ARM64 :
LINUX;

/*
* Assess which chromium revision to install.
* Note that in docker we do our own install, and
* ignore puppeteer's install.js file.
*/
const PUPPETEER_CHROMIUM_REVISION = (() => {
return puppeteer._preferredRevision;
})();

/*
* Sometimes we don't use puppeteer's built-in chromium
* for compatibility reasons
*/
const PUPPETEER_BINARY_LOCATION = (() => {
if (PLATFORM === LINUX_ARM64) {
return playwright.chromium.executablePath();
}

const browserFetcher = puppeteer.createBrowserFetcher();
return browserFetcher.revisionInfo(PUPPETEER_CHROMIUM_REVISION).executablePath;
})();

/*
* Tells puppeteer, in its install script, what revision to download.
* This is set in our deploy.js file in our docker build. If
* PUPPETEER_SKIP_CHROMIUM_DOWNLOAD is true, then this is ignored
*/
const CHROME_BINARY_LOCATION = (() => {
if (process.env.CHROME_BINARY_LOCATION) {
return process.env.CHROME_BINARY_LOCATION;
}

// In docker we symlink any chrome installs to the default install location
// so that chromedriver can do its thing
if (IS_DOCKER) {
return CHROME_BINARY_PATHS.LINUX;
}

// If using chrome-stable, default to it's natural habitat
if (USE_CHROME_STABLE) {
return CHROME_BINARY_PATHS[PLATFORM];
}

// All else uses pptr's bin
return PUPPETEER_BINARY_LOCATION;
})();

/*
* Tells the chromedriver library to download the appropriate chromedriver binary.
* The only time this should be false is when building chrome stable in docker.
*/
const CHROMEDRIVER_SKIP_DOWNLOAD = (() => {
if (process.env.CHROMEDRIVER_SKIP_DOWNLOAD) {
return process.env.CHROMEDRIVER_SKIP_DOWNLOAD;
}

if (IS_DOCKER) {
return !USE_CHROME_STABLE;
}

return true;
})();

/*
* Tells puppeteer to skip downloading the appropriate chrome revision. This is generally
* not the case, however when installing chrome-stable in docker we want to skip it as
* we'll download google-chrome-stable from a deb package instead.
*/
const PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = (() => {
if (process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD) {
return process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD;
}

if (IS_DOCKER) {
return USE_CHROME_STABLE;
}

return false;
})();

const e = {
IS_DOCKER,
USE_CHROME_STABLE,
PUPPETEER_CHROMIUM_REVISION,
CHROME_BINARY_LOCATION,
CHROMEDRIVER_SKIP_DOWNLOAD,
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD,
PUPPETEER_BINARY_LOCATION,
PLATFORM,
WINDOWS,
MAC,
LINUX,
LINUX_ARM64,
};

export default e
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "wa-automate-docker",
"version": "1.0.0",
"description": "This is a simple app that will expose an API that you can use to control your whatsapp instance.",
"type": "module",
"scripts": {
"postinstall": "node ./scripts/postinstall.js",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm update && npx @open-wa/wa-automate --in-docker -p 8080 --npm-options=--ignore-scripts",
"sessiondata" : "npx @open-wa/wa-automate --disable-spins --session-data-only"
Expand Down
Loading

0 comments on commit 94e4e9b

Please sign in to comment.