Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Gitpod workspace and addition of Makefile for automation #2737

Merged
merged 69 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
4bd1283
🆕 chore(Makefile): add Makefile to project
ThomasSanson Jun 10, 2023
8153b1c
🎉 feat(config.mak): add global Makefile configuration
ThomasSanson Jun 10, 2023
14ede58
🆕 feat(help.mak): add help command to Makefile
ThomasSanson Jun 10, 2023
4f88eaa
🔍 chore(.gitignore): add .venv to ignore list
ThomasSanson Jun 10, 2023
f4f4908
🆕 chore(.gitpod.yml): add Gitpod configuration file
ThomasSanson Jun 10, 2023
943676d
🔥 chore(requirements): remove requirements.dev.txt file
ThomasSanson Jun 10, 2023
f7c6481
🆕 chore(python_version.config): add configuration file for Python ver…
ThomasSanson Jun 10, 2023
80662eb
🎉 feat(Dockerfile): add Dockerfile for Gitpod workspace
ThomasSanson Jun 10, 2023
3446d29
🆕 chore(gitpod.mak): add Gitpod make targets for building, linting, a…
ThomasSanson Jun 10, 2023
88a7179
🎉 feat(python.mak): add makefile for python virtual environment manag…
ThomasSanson Jun 10, 2023
58edff5
🆕 chore(requirements.txt): add new development dependencies
ThomasSanson Jun 10, 2023
ff807b9
🚚 chore(Dockerfile): move dev requirements file to a more appropriate…
ThomasSanson Jun 10, 2023
6198108
🔧 chore(CONTRIBUTING.md): update pip install command to use new requi…
ThomasSanson Jun 10, 2023
db1ddf0
🐛 fix(build-command.yml): update path to requirements.dev.txt to .con…
ThomasSanson Jun 10, 2023
606d18a
🆕 chore(gitpod.yml): add Gitpod workflow to run tests on push and pul…
ThomasSanson Jun 10, 2023
0ddc823
🔥 chore(.dockerignore): add .venv to ignore list
ThomasSanson Jun 10, 2023
0af9c7b
🚀 feat(Dockerfile): add markdown-table-formatter package to the Docke…
ThomasSanson Jun 10, 2023
4473558
📝 docs(CONTRIBUTING.md): update contribution guidelines
ThomasSanson Jun 10, 2023
ae22647
🐛 fix(.gitpod.yml): remove "-dev" from "bootstrap-dev" task name
ThomasSanson Jun 10, 2023
9e21dcb
🚀 feat(Makefile): add nodejs-bootstrap and nodejs-clean targets to Ma…
ThomasSanson Jun 10, 2023
4449985
🚀 chore(package.json): add markdown-table-formatter dependency
ThomasSanson Jun 10, 2023
18bc311
🔥 chore(Dockerfile): remove unnecessary whitespace
ThomasSanson Jun 10, 2023
f648149
🆕 chore(megalinter.mak): add megalinter-test target to run script bui…
ThomasSanson Jun 10, 2023
2abf6e0
🆕 feat(nodejs.mak): add make targets for cleaning and bootstrapping N…
ThomasSanson Jun 10, 2023
6a0053a
🐛 fix(CONTRIBUTING.md): update make command names to match their func…
ThomasSanson Jun 10, 2023
d6db3e8
🔧 chore(Dockerfile): add mega-linter-runner package to Dockerfile
ThomasSanson Jun 10, 2023
223d580
🔥 chore(.gitignore): add megalinter_file_names_cspell.txt to ignore list
ThomasSanson Jun 10, 2023
dfa0192
🔍 chore(.mega-linter.yml): update FILTER_REGEX_EXCLUDE to exclude new…
ThomasSanson Jun 10, 2023
6010512
🚀 feat(megalinter.mak): add new targets to run and test megalinter lo…
ThomasSanson Jun 10, 2023
6750b8a
🔧 chore(Makefile): rename megalinter-test to megalinter-tests and add…
ThomasSanson Jun 10, 2023
7b5c949
Merge branch 'gitpod'
ThomasSanson Jun 10, 2023
f9688eb
🔨 chore(Dockerfile): update NPM_VERSION to 9.7.1
ThomasSanson Jun 10, 2023
06ba0a6
🎨 style(CONTRIBUTING.md): remove unnecessary link reference
ThomasSanson Jun 10, 2023
d8f7c71
🔍 chore(.cspell.json): add "ungit" and "UNGIT" to the dictionary
ThomasSanson Jun 10, 2023
d5709ea
🔍 chore(.cspell.json): add "addsuffix" to the dictionary
ThomasSanson Jun 10, 2023
bc4c2db
🐛 fix(nodejs.mak): add sudo to nodejs-clean target to allow deleting …
ThomasSanson Jun 10, 2023
0649c1d
🐛 fix(.mega-linter.yml): fix REPOSITORY_TRIVY_ARGUMENTS to skip .auto…
ThomasSanson Jun 11, 2023
2f96852
📈 chore(requirements.txt): update pymdown-extensions to version 10.0
ThomasSanson Jun 11, 2023
1101c7b
Merge branch 'oxsecurity:main' into main
ThomasSanson Jun 11, 2023
e112cb0
🔧 chore(python_version.config): update Python version to 3.11
ThomasSanson Jun 11, 2023
e4fb205
🔀 chore(gitpod.yml): change default branch name from master to main
ThomasSanson Jun 11, 2023
3532da0
🔥 chore(.mega-linter.yml): remove unused SPELL_MISSPELL_FILTER_REGEX_…
ThomasSanson Jun 11, 2023
56a61c6
🐛 fix(gitpod.yml): change make bootstrap-dev to make bootstrap to mat…
ThomasSanson Jun 11, 2023
37d1466
🔧 chore(gitpod.yml): update Python version to 3.11
ThomasSanson Jun 11, 2023
b84a7d5
🆕 chore(.python-version): add Python version 3.11.4 to project
ThomasSanson Jun 15, 2023
4cbc9d7
🔥 chore(python_version.config): remove python version configuration file
ThomasSanson Jun 15, 2023
870b4d8
🐛 fix(Makefile): change python version detection to use .python-versi…
ThomasSanson Jun 15, 2023
c67b146
🔧 chore(Dockerfile): rename python_version.config to .python-version …
ThomasSanson Jun 15, 2023
bd2432c
Merge remote-tracking branch 'upstream/main'
ThomasSanson Jun 15, 2023
7259e96
🐛 fix(gitpod.yml): correct typo in job name
ThomasSanson Jun 18, 2023
5fce905
🔧 chore(python.mak): update python_launcher version to python3.11
ThomasSanson Jun 18, 2023
29dc807
🔧 chore(CONTRIBUTING.md): update Python version to 3.11
ThomasSanson Jun 18, 2023
f43adda
🐛 fix(CONTRIBUTING.md): update megalinter test command to match the c…
ThomasSanson Jun 18, 2023
c8e921f
🚀 chore(gitpod.yml): change Python version setup to read from .python…
ThomasSanson Jun 20, 2023
4d74fd4
🐛 fix(workflows): use python-version-file instead of python-version t…
ThomasSanson Jun 20, 2023
d100d2a
Merge branch 'oxsecurity:main' into main
ThomasSanson Jun 20, 2023
ab261a8
🔄 chore(.python-version): add newline at end of file
ThomasSanson Jun 20, 2023
60ae8d2
🐛 fix(Dockerfile): add newline at end of file
ThomasSanson Jun 20, 2023
ef92701
🔥 chore(.dockerignore): add newline at end of file
ThomasSanson Jun 20, 2023
15ade02
🔥 chore(gitpod.mak): add newline at end of file
ThomasSanson Jun 20, 2023
7d192be
🔥 chore(help.mak): add newline at end of file
ThomasSanson Jun 20, 2023
c37807c
🔥 chore(megalinter.mak): add newline at end of file
ThomasSanson Jun 20, 2023
79e8c6d
🔥 chore(nodejs.mak): add newline at end of file
ThomasSanson Jun 20, 2023
f9364e8
🔥 chore(python.mak): add newline at end of file
ThomasSanson Jun 20, 2023
7896684
🔥 chore(Makefile): add newline at end of file
ThomasSanson Jun 20, 2023
a4c64ed
🔧 chore(gitpod.mak): rename docker image tag to megalinter_gitpod:local
ThomasSanson Jun 27, 2023
6022e07
🐛 fix(gitpod.mak): reorder gitpod-tests dependencies to fix build order
ThomasSanson Jun 27, 2023
742f834
🔧 chore(.gitignore): ignore Redis dump files and server files
ThomasSanson Jul 11, 2023
2e6c5c3
Merge remote-tracking branch 'upstream/main'
ThomasSanson Jul 11, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 100 additions & 0 deletions .config/gitpod/Dockerfile
echoix marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
###################
# builder #
###################
# First stage: Install build-time dependencies
FROM ubuntu:jammy as builder

# hadolint ignore=DL3002
USER root

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Copy Python version config file
COPY .python-version /tmp/

# Update system and install packages
# hadolint ignore=DL3008,DL3009,DL3013
RUN PYTHON_VERSION=$(cut -d '.' -f 1,2 /tmp/.python-version) \
&& apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends \
bash-completion \
ca-certificates \
curl \
git \
git-lfs \
gnupg \
htop \
iproute2 \
lsb-release \
make \
nano \
python3-pip \
"python${PYTHON_VERSION}" \
"python${PYTHON_VERSION}-venv" \
sudo \
tree \
vim \
wget \
&& python3 -m pip install --no-cache-dir --upgrade pip \
&& python3 -m pip install --no-cache-dir virtualenv

############################
# docker-installer #
############################
FROM builder as docker-installer

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# hadolint ignore=DL3008,DL3009
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null \
&& apt-get update \
&& apt-get install -y --no-install-recommends docker-ce docker-ce-cli containerd.io

###########################
# npm-installer #
###########################
FROM docker-installer as npm-installer

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Set a variable for packages version
ARG NPM_VERSION=9.7.1
ARG UNGIT_VERSION=1.5.23
ARG MEGA_LINTER_RUNNER_VERSION=7.0.0
ARG MARKDOWN_TABLE_FORMATTER_VERSION=1.4.0

# hadolint ignore=DL3008
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y --no-install-recommends nodejs \
&& npm install -g "npm@${NPM_VERSION}" \
&& npm install -g \
"ungit@${UNGIT_VERSION}" \
"mega-linter-runner@${MEGA_LINTER_RUNNER_VERSION}" \
"markdown-table-formatter@${MARKDOWN_TABLE_FORMATTER_VERSION}"

###########################
# cleanup #
###########################
FROM npm-installer as cleanup

RUN apt-get clean && rm -rf /var/lib/apt/lists/* tmp/*

Comment on lines +78 to +84
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a conceptual error. (We kinda became Dockerfile specialists with time).
Even though we might think that since it a separate stage, and that we often use that pattern to build optimized images, it doesn't do exactly what you might think.

Since it is a "FROM", then this stage will create a new image by using cleanup as a base image, like if it was a published image, but in this case, it is defined in the Dockerfile. Docker images work essentially by applying layers on top of each other, each layer being immutable. Just like if each layer was a tar file, and extracted in the same folder. (It is really like that it works).

So, just before the RUN apt-get clean from line 83, the layers of the image will be all the layers that cleanup consists of, and at line 83, we are adding a new layer, that will, once applied, delete files in the filesystem of the image.

That means that what will be downloaded and extracted will contain the files, and later on when finishing pulling, we will remove them. So there are no space gains at all, since they had to be included in the layer like at lines 17-20, 53, and 71, but don't appear in the final state. It is "wasted space". The solution for this is the (ugly) pattern that you often see in Dockerfiles, that you chain the clean commands directly in the same "RUN" as you created them. Each instruction in a Dockerfile makes a layer, that won't change.

Edit: I read a little too fast. Finally you used a COPY --from at the last stage that was based off the original one. So it doesn't really matter inside the final image, since all the stages will be discarded. But I'm not sure that the user added applied on line 91 will really work well if you copy and replace the whole filesystem. If you are to base your image on ubuntu:jammy, and then completely replace the whole filesystem with your contents, why need to have the final image download two times the contents of ubuntu:jammy? Once with the FROM (the 6 layers of the base image), and another one with the COPY --from, that includes the changes.

If the current final image works as expected, (replacing the whole root filesystem), then changing the base image for the final stage to scratch, ie: FROM scratch, that will work the same.
FROM scratch means to base the image on nothing, your image won't have layers before it.

###########################
# gitpod-user #
###########################
FROM cleanup as gitpod-user

# UID must be 33333.
RUN useradd -l -u 33333 -G sudo -md /home/gitpod -s /bin/bash -p gitpod gitpod

#########################
# final #
#########################
FROM ubuntu:jammy

COPY --from=gitpod-user / /

USER gitpod
34 changes: 34 additions & 0 deletions .config/make/00_config.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Makefile global config
.DEFAULT_GOAL:=help
.EXPORT_ALL_VARIABLES:
.ONESHELL:
.SILENT:
MAKEFLAGS += "-j$(NUM_CORES) -l$(NUM_CORES)"
MAKEFLAGS += --silent
SHELL:=/bin/bash
.SHELLFLAGS = -eu -o pipefail -c

# Makefile colors config
bold=$(shell tput bold)
normal=$(shell tput sgr0)
errorTitle=$(shell tput setab 1 && tput bold && echo '\n')
recommendation=$(shell tput setab 4)
underline=$(shell tput smul)
reset=$(shell tput -Txterm sgr0)
black=$(shell tput setaf 0)
red=$(shell tput setaf 1)
green=$(shell tput setaf 2)
yellow=$(shell tput setaf 3)
blue=$(shell tput setaf 4)
magenta=$(shell tput setaf 5)
cyan=$(shell tput setaf 6)
white=$(shell tput setaf 7)

define HEADER
How to use me:
make help
make bootstrap-dev
make ${cyan}<target>${reset}

endef
export HEADER
13 changes: 13 additions & 0 deletions .config/make/gitpod.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## —— Gitpod —————————————————————————————————————————————————————————————————————————————————————
.PHONY: gitpod-build
gitpod-build: ## Run docker build image in local
docker build --tag megalinter_gitpod:local --file .config/gitpod/Dockerfile .

.PHONY: gitpod-lint
gitpod-lint: ## Run hadolint command to lint Dokerfile
docker run --rm -i hadolint/hadolint < .config/gitpod/Dockerfile

.PHONY: gitpod-tests
gitpod-tests: ## Run tests for gitpod
$(MAKE) gitpod-lint
$(MAKE) gitpod-build
5 changes: 5 additions & 0 deletions .config/make/help.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## —— Help ———————————————————————————————————————————————————————————————————————————————————————
.PHONY: help
help: ## Help command
echo -e "$$HEADER"
grep -E '(^[a-zA-Z0-9_-]+:.*?## .*$$)|(^## )' $(MAKEFILE_LIST) | sed 's/^[^:]*://g' | awk 'BEGIN {FS = ":.*?## | #"} ; {printf "${cyan}%-30s${reset} ${white}%s${reset} ${green}%s${reset}\n", $$1, $$2, $$3}' | sed -e 's/\[36m##/\n[32m##/'
20 changes: 20 additions & 0 deletions .config/make/megalinter.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## —— Megalinter —————————————————————————————————————————————————————————————————————————————————————
.PHONY: megalinter-build
megalinter-build: ## Run script build.sh
source .venv/bin/activate
bash build.sh

.PHONY: megalinter-run
megalinter-run: ## Run megalinter locally
npx mega-linter-runner --flavor python --release beta

.PHONY: megalinter-tests
megalinter-tests: ## Run all megalinter tests
$(MAKE) megalinter-run
$(MAKE) megalinter-build

.PHONY: megalinter-clean
megalinter-clean: ## Clean megalinter locally
sudo rm -rf megalinter-reports
sudo rm -rf site
sudo rm -f *megalinter_file_names_cspell.txt
8 changes: 8 additions & 0 deletions .config/make/nodejs.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## —— Node.js —————————————————————————————————————————————————————————————————————————————————————
.PHONY: nodejs-clean
nodejs-clean: ## Clean nodejs files
sudo rm -rf node_modules

.PHONY: nodejs-bootstrap
nodejs-bootstrap: ## Bootstrap nodejs
npm install
75 changes: 75 additions & 0 deletions .config/make/python.mak
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Python default launcher
python_launcher ?= python3.11
python_requirements_file ?= .config/python/dev/requirements.txt
python_requirements_dev_file ?= .config/python/dev/requirements.txt

## —— Python —————————————————————————————————————————————————————————————————————————————————————
.PHONY: python-bootstrap
python-bootstrap: ## Bootstrap python
$(MAKE) python-venv-init
$(MAKE) python-venv-upgrade
$(MAKE) python-venv-requirements

.PHONY: python-bootstrap-dev
python-bootstrap-dev: ## Bootstrap python for dev env
$(MAKE) python-venv-requirements-dev
$(MAKE) python-venv-linters-install

# ===============================================================================================
# .venv
# ===============================================================================================
.PHONY: python-venv-init
python-venv-init: ## Create venv ".venv/" if not exist
if [ ! -d .venv ] ; then
$(python_launcher) -m venv .venv
fi

.PHONY: python-venv-upgrade
python-venv-upgrade: ## Upgrade venv with pip, setuptools and wheel
source .venv/bin/activate
pip install --upgrade pip setuptools wheel

.PHONY: python-venv-requirements
python-venv-requirements: ## Install or upgrade from $(python_requirements_file)
source .venv/bin/activate
pip install --upgrade --requirement $(python_requirements_file)

.PHONY: python-venv-requirements-dev
python-venv-requirements-dev: ## Install or upgrade from $(python_requirements_dev_file)
source .venv/bin/activate
pip install --upgrade --requirement $(python_requirements_dev_file)

.PHONY: python-venv-linters-install
python-venv-linters-install: ## Install or upgrade linters
source .venv/bin/activate
pip install --upgrade flake8

.PHONY: python-venv-purge
python-venv-purge: ## Remove venv ".venv/" folder
rm -rf .venv

# ===============================================================================================
# Utils
# ===============================================================================================
.PHONY: python-purge-cache
python-purge-cache: ## Purge cache to avoid used cached files
if [ -d .venv ] ; then
source .venv/bin/activate
pip cache purge
fi

.PHONY: python-version
python-version: ## Displays the python version used for the .venv
source .venv/bin/activate
$(python_launcher) --version

.PHONY: python-flake8
python-flake8: ## Run flake8 linter for python
source .venv/bin/activate
flake8 --config .config/.flake8

.PHONY: python-pytest
python-pytest: ## Run pytest to test python scripts
source .venv/bin/activate
cd scripts/
$(python_launcher) -m pytest
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ webpreview
yq
importlib-metadata>=3.10
mkdocs-material
pymdown-extensions==9.11
pymdown-extensions==10.0
mkdocs-glightbox==0.3.2
mdx_truly_sane_lists
beautifulsoup4
Expand Down
4 changes: 4 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@
"Trivy",
"Twilio",
"Uncomparables",
"ungit",
"UNGIT",
"VARNAME",
"VBDOTNET",
"Vuillamy",
Expand All @@ -463,6 +465,7 @@
"addgroup",
"additional",
"addlinespace",
"addsuffix",
"adrienverge",
"agentkeepalive",
"agentlib",
Expand Down Expand Up @@ -621,6 +624,7 @@
"dartanalyzer",
"dateformat",
"davidjeddy",
"dearmor",
"debugent",
"debuglog",
"decamelize",
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ARG NODE_VERSION="16"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
COPY requirements.dev.txt /tmp/pip-tmp/
COPY .config/python/dev/requirements.txt /tmp/pip-tmp/
RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.dev.txt \
&& rm -rf /tmp/pip-tmp && pip3 install --no-cache-dir mkdocs-material pymdown-extensions==9.11 mkdocs-glightbox==0.3.2 pymdown-extensions==9.11

Expand Down
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pids
*.pid
*.seed
*.pid.lock
*.log
*.log
.venv