Skip to content

Commit

Permalink
Add devcontainer description to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Mar 22, 2021
1 parent 0776957 commit 7ff245d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
62 changes: 62 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
There are two ways to use devcontainer: the original one with Visual Studio created by ludeeus, and the alternative one without any editor I created.

## Developing with Visual Studio Code + devcontainer

The easiest way to get started with custom integration development is to use Visual Studio Code with devcontainers. This approach will create a preconfigured development environment with all the tools you need.
Expand Down Expand Up @@ -58,3 +60,63 @@ Then launch the task `Run Home Assistant on port 9123`, and launch the debbuger
with the existing debugging configuration `Python: Attach Local`.

For more information, look at [the Remote Python Debugger integration documentation](https://www.home-assistant.io/integrations/debugpy/).

## Developing with any editors + devcontainer

This repository inherits the integration_blueprint repository. Since I do not use Visual Studio, I left all its settings, but made my own mechanism for quick and convenient code testing, which works with absolutely any editor (I use PyCharm). And it can even be run on a remote machine. The only limitation of this method is that it needs Linux to work. Perhaps, it might work on other systems as well, but I haven't tested it, sorry.

Similar first way in the container you will have a dedicated Home Assistant core instance running with your custom component code. You can configure this instance by updating the `./devcontainer/configuration.yaml` file.

**Prerequisites**

- [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- Docker
- For Linux, macOS, or Windows 10 Pro/Enterprise/Education use the [current release version of Docker](https://docs.docker.com/install/)
- Windows 10 Home requires [WSL 2](https://docs.microsoft.com/windows/wsl/wsl2-install) and the current Edge version of Docker Desktop (see instructions [here](https://docs.docker.com/docker-for-windows/wsl-tech-preview/)). This can also be used for Windows Pro/Enterprise/Education.
- Bash environment

**Getting started:**

1. Fork the repository.
2. Clone the repository to your computer.
3. Run devcontainer from command line: \
`./bin/devcontainer start`

Note: By default, the container is created in local access only mode.
If you want to have access from the outside (for example, when starting on a remote machine), add parameter `--public` at the first start (after creating the container, this setting is remembered by the system): \
`./bin/devcontainer --public start`

While container is run Home Assistant will be available at http://localhost:9123/

Note: The container can be stopped by pressing the good old Ctrl-C keys.

### Useful devcontainer commands

- Start container: `./bin/devcontainer start`
- Destroy container: `./bin/devcontainer down`
- Upgrade Home Assistant to latest dev: `./bin/devcontainer upgrade`
- Install a specific version of Home Assistant: `./bin/devcontainer set-version`
- Run command line into container: `./bin/devcontainer bash`

### Bootstrap script

Sometimes you need to perform additional steps at the stage of container initialization. To do this, you can use script `.devcontainer/bootstrap.sh`. By default, it is configured to automatically install the libraries required for your component from private repositories. But you are free to change it however you like.
The main thing to remember is that the script will be executed INSIDE the container.

The script is automatically executed immediately after initializing the container and immediately after upgrading the HA version. It can also be executed at any time with command `./bin/devcontainer bootstrap`.

When the script is run, it receives only one parameter — the current container command: `install`, `upgrade` or `bootstrap`.
The current directory when running the script is the root directory of the repository inside the container. Home Assistant settings are located in directory `/config`.

To automatically install libraries from private repositories, you need to register each such library in the `requirements.txt` file as a link of the form
```requirements.txt
git+https://{GITHUB_TOKEN}@github.com/<USER>/<REPOSITORY>.git@<BRANCH>#egg=<LIBRARY>
```
Note: `{GITHUB_TOKEN}` is text as is, NOT actual token!

After that, in the root of the repository, create a file `secrets.yaml` and write the contents to it:
```yaml
github_token: <YOUR_TOKEN>
```
When you run the bootstrap script, it will automatically extract the required lines from requirements.txt, replace line `{GITHUB_TOKEN}` in them with the real token, and install the libraries. At the same time, file `secrets.yaml` is prohibited via `.gitignore` from being sent to the repository. Therefore, you can not be afraid that the token will be compromised.
2 changes: 1 addition & 1 deletion bin/devcontainer
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ case "${cmd}" in
fi
log.info "Send command '${cmd}' to container..."
if [[ "${cmd}" == "start" ]]; then
log.info "After Home Assistant initialization you can access to system on http://localhost:9123/"
log.info "Note: After Home Assistant initialization you can access to system on http://localhost:9123/"
fi
${docker} exec -it -w "${workdir}" "${container}" container "${cmd}"

Expand Down

0 comments on commit 7ff245d

Please sign in to comment.