Skip to content

Commit 0e0147b

Browse files
committed
ESPHome: random autogenerated password
Also adds a generic mechanism for a service to have autogenerated variables without needing any build.py. TODO: automatically generate submenu to allow for default or custom passwords.
1 parent 5722927 commit 0e0147b

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

.templates/esphome/service.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ esphome:
33
image: esphome/esphome:latest
44
restart: unless-stopped
55
user: 0:1000
6+
environment:
7+
- USERNAME=admin
8+
- PASSWORD=%randomPassword%
69
volumes:
710
- ./volumes/esphome:/config
811
- /etc/localtime:/etc/localtime:ro

docs/BuildStack-RandomPassword.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
This page explains how to have a service generate a random password during build time. This will require that your service have a working options menu.
44

5+
Alternatively, a service may just use `%randomPassword%` in a value without any
6+
options menu. This will generate a new random password the first time this
7+
service is added. The generated password may be found in the
8+
`docker-compose.yml`.
9+
510
Keep in mind that updating strings in a service's yaml config isn't limited to passwords.
611

712
## A word of caution

docs/Containers/ESPHome.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# ESPHome
2+
3+
[ESPHome](https://esphome.io/) is a system to control your ESP8266/ESP32 by
4+
simple yet powerful configuration files and control them remotely through Home
5+
Automation systems.
6+
7+
Web UI is at: `http://raspberrypi.local:6052/`
8+
Login username is `admin` and the password is automatically generated and can be
9+
found and changed in your `docker-compose.yml`.
10+
11+
## USB serial programming from a docker-container
12+
13+
To support flashing an ESP device directly from your RPi, `/dev/ttyUSB0` is made
14+
available to the container. This file usually auto-created when an
15+
USB-to-serial-converter is plugged in. Docker needs the file to exist when the
16+
container is started. This is usually handled by install.sh and menu.sh. But
17+
just in case this somehow fails (e.g. RPi already connected when said scripts
18+
are executed), just run:
19+
20+
```
21+
sudo mknod -m660 /dev/ttyUSB0 c 188 0
22+
sudo chgrp dialout /dev/ttyUSB0
23+
```

docs/Default-Configs.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Do note that the ports listed are not all of the ports containers use. They are
1616
| deconz | *none* | IOtSt4ckDec0nZ | 8090 | No |
1717
| diyhue | *none* | *none* | 8070 | No |
1818
| dozzle | *none* | *none* | 8080 | No |
19+
| esphome | admin | *random* | 6052 | No |
1920
| espruinohub | *none* | *none* | *none* | No |
2021
| gitea | *none* | *none* | 7920 | No |
2122
| grafana | *none* | *none* | 3000 | No |
@@ -42,3 +43,8 @@ Do note that the ports listed are not all of the ports containers use. They are
4243
| webthingsio_gateway | *none* | *none* | 4060 | No |
4344
| zigbee2mqtt | *none* | *none* | *none* | No |
4445
| zigbee2mqtt_assistant | *none* | *none* | *none* | No |
46+
47+
Note:
48+
49+
*random* - a random password is autogenerated. Check docker-compose.yml for what
50+
it actually is.

scripts/buildstack_menu.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ def main():
1111
import math
1212
import sys
1313
import subprocess
14+
import re
1415
from deps.chars import specialChars, commonTopBorder, commonBottomBorder, commonEmptyLine, padText
1516
from deps.consts import servicesDirectory, templatesDirectory, volumesDirectory, buildCache, envFile, dockerPathOutput, servicesFileName, composeOverrideFile
1617
from deps.yaml_merge import mergeYaml
18+
from deps.common_functions import generateRandomString
1719
from blessed import Terminal
1820
global signal
1921
global renderMode
@@ -52,6 +54,7 @@ def buildServices(): # TODO: Move this into a dependency so that it can be execu
5254
global dockerComposeServicesYaml
5355
try:
5456
runPrebuildHook()
57+
replaceVariables()
5558
dockerFileYaml = {}
5659
menuStateFileYaml = {}
5760
dockerFileYaml["version"] = "3.6"
@@ -473,6 +476,30 @@ def runPrebuildHook():
473476
except:
474477
pass
475478

479+
def replaceVariables():
480+
global dockerComposeServicesYaml
481+
482+
def replace(stringValue):
483+
if "%randomPassword%" in stringValue:
484+
pw = generateRandomString()
485+
return re.sub(re.escape("%randomPassword%"), pw, stringValue)
486+
return stringValue
487+
488+
def iterate(root):
489+
if isinstance(root, list):
490+
for key, value in enumerate(root):
491+
if isinstance(value, str):
492+
root[key] = replace(value)
493+
else:
494+
iterate(value)
495+
if isinstance(root, dict):
496+
for key, value in root.items():
497+
if isinstance(value, str):
498+
root[key] = replace(value)
499+
else:
500+
iterate(value)
501+
iterate(dockerComposeServicesYaml)
502+
476503
def runPostBuildHook():
477504
for (index, checkedMenuItem) in enumerate(checkedMenuItems):
478505
buildScriptPath = templatesDirectory + '/' + checkedMenuItem + '/' + buildScriptFile

0 commit comments

Comments
 (0)