From 0a393dab6beaeedc1b56fd8c5e03b7844a051827 Mon Sep 17 00:00:00 2001 From: Laurentiu Badea Date: Fri, 4 Sep 2020 00:53:23 +0000 Subject: [PATCH] Add vscode remote container development files Why: The vscode Golang plugin enforces style, identifies errors and has a remote debugger. Developing in a container isolates the dev environment from the host machine. --- .devcontainer/Dockerfile | 18 +++++++++ .devcontainer/devcontainer.json | 45 ++++++++++++++++++++++ .gitignore | 2 +- README.md | 5 +++ devcontainer.md | 67 +++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 devcontainer.md diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000000..d8373fd4c57 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,18 @@ +# From https://github.com/microsoft/vscode-dev-containers/blob/master/containers/go/.devcontainer/Dockerfile +ARG VARIANT=1 +FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT} + +# [Optional] Install a version of Node.js using nvm for front end dev +ARG INSTALL_NODE="true" +ARG NODE_VERSION="lts/*" +RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi + +# [Optional] Uncomment this section to install additional OS packages. +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && apt-get -y install --no-install-recommends vim + +# [Optional] Uncomment the next line to use go get to install anything else you need +# RUN go get -x + +# [Optional] Uncomment this line to install global node packages. +# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000000..b2c53776ad4 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,45 @@ +// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.112.0/containers/go +{ + "name": "Go", + "build": { + "dockerfile": "Dockerfile", + "args": { + // Update the VARIANT arg to pick a version of Go: 1, 1.15, 1.14 + "VARIANT": "1.14", + // Options + "INSTALL_NODE": "false", + "NODE_VERSION": "lts/*", + } + }, + "containerEnv": { + "GOPRIVATE": "${localEnv:GOPRIVATE}", + }, + "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ], + + // Set *default* container specific settings.json values on container create. + "settings": { + "terminal.integrated.shell.linux": "/bin/bash", + "go.useGoProxyToCheckForToolUpdates": false, + "go.gopath": "/go", + //"go.toolsGopath": "/tmp/go", + }, + + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "golang.Go", + "ms-azuretools.vscode-docker", + "redhat.vscode-xml", + "redhat.vscode-yaml", + "eamodio.gitlens", + ], + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + "forwardPorts": [8000,8001,6060], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "mkdir ~/.ssh; ssh-keyscan github.com > ~/.ssh/known_hosts", + + // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "vscode" +} diff --git a/.gitignore b/.gitignore index 60c24e79c0d..79076f9be84 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ _obj _test .cover/ .idea/ -.vscode/ # Architecture specific extensions/prefixes *.[568vq] @@ -46,6 +45,7 @@ analytics/filesystem/testFiles/ # static/version.txt .idea/ +.vscode/ # autogenerated mac file diff --git a/README.md b/README.md index 673c2b1bdeb..5b90b0ee230 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,8 @@ Want to [add an adapter](https://docs.prebid.org/prebid-server/developers/add-ne Report bugs, request features, and suggest improvements [on Github](https://github.com/prebid/prebid-server/issues). Or better yet, [open a pull request](https://github.com/prebid/prebid-server/compare) with the changes you'd like to see. + +## IDE Setup for PBS-Go development + +The quickest way to start developing PBS-Go in a reproducible environment isolated from your host OS +is by using this [VScode Remote Container Setup](devcontainer.md) diff --git a/devcontainer.md b/devcontainer.md new file mode 100644 index 00000000000..824356d802b --- /dev/null +++ b/devcontainer.md @@ -0,0 +1,67 @@ + +### vscode in-container development + +The quickest way to get up and running with PBS-Go development in a reproducible environment isolated +from your host OS is by loading the repository in a [Docker](https://docs.docker.com/get-docker/) +container under [Visual Studio Code](https://code.visualstudio.com/). + +This covers installing Go and necessary IDE plugins for Golang editing and debugging, and is +all automated via the VSCode [.devcontainer](.devcontainer/) configuration. See +[VSCode Remote Containers](https://code.visualstudio.com/docs/remote/containers) for more +details about how to customize. + +#### Setup + +Install: + +- [Docker](https://docs.docker.com/get-docker/) +- [Visual Studio Code](https://code.visualstudio.com/) +- [VSCode Remote Development Extension Pack](https://aka.ms/vscode-remote/download/extension) + +Then: + +- start VSCode and open repository +- accept VSCode suggestion to _reopen in container_ +- VSCode will build a new container and install the IDE support and extensions in it + +Optionally, to use your github ssh key for accessing non-public GitHub repositories: +- Method 1: add your github ssh key to agent. This is needed after each OS restart. +```sh +ssh-add ~/.ssh/id_rsa # or your ssh key for github +``` +- Method 2: map your ~/.ssh (or just the key) as a docker volume in .devcontainer.json + +Feel free to customize .devcontainer.json if needed. You can add preset environment variables, +other vscode extensions to preload and additional volume mounts. + +#### Starting PBS-Go + +- `Shift`-`Cmd`-`D` or `Run` icon brings up the `Launch prebid-server` panel with interactive +debugger. Breakpoints can be set to stop execution. +- CTRL-`\`` opens the terminal to start prebid-server non-interactively +```sh +go run main.go --alsologtostderr +``` +- Create a pbs.yaml file if neccessary, with configuration overrides. + +#### Testing + +- Open any `*_test.go` file in editor +- Individual test functions can be run directly by clicking the `run test`/`debug test` annotation +above each test function. +- At the top of the file you can see `run package tests | run file tests` +- TIP: use `run package tests` at the top of the test file to quickly check code coverage: + the open editors for files in the tested package will have lines highlighted in green (covered) + and red (not covered) +- CTRL-`\`` opens the terminal to run the test suite +```sh +./validate.sh +``` + +#### Editing +- Style, imports are automatically updated on save. +- Editor can suggest correct names and + +- Remote container commands popup by clicking on _Dev Container: Go_ at bottom left +- `F1` -> type _rebuild container_ to restart with a fresh container +- `F1` -> `^`-`\`` to toggle terminal