-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compute,api,update-server): Move system configs out of Dockerfile (
#2073) Overhaul the container configuration and initialization to remove as much as possible from the Dockerfile and the volatile storage, preferring instead to live in files that are included in the api server wheel and unpacked to /data during initialization. Most configuration and setup scripts are no longer COPYd into /usr/local/bin. Instead, they are packed into the api server’s wheel (opentrons) in a /resources subdirectory. This resources subdirectory is recursive-included into the manifest, so putting a file into the directory will include it in the wheel. Because wheels do not run arbitrary code when they are installed, there is some external tooling to get the files out of wheel/resources and into their eventual home in /data/system. * Provisioning The compute/find_module_path.py is an executable python script that uses importlib to find the location of the opentrons module as Python would do (so it always gets the right one) without importing opentrons, which has significant side effects. The opentrons/api/opentrons/resources/scripts/provision-api-resources script uses find_module_path to find the current opentrons module and copy everything in /resources in the package to /data/system (the OT_CONFIG_DIR). This includes all the system configurations and scripts that were previously in compute/. The provision script is designed to be invoked during updates. This happens during the first boot of the container (more on this later) and during a software update via the update server. In the case of the update-server driven update, because we assume a user is never updating to 3.3 from 3.0 on a 3.0 Dockerfile, we can assume the provision script is in /data/system/scripts and is therefore in the path. The update server therefore invokes the provision script after installing a new api server, the provision script finds the right (newly-installed) opentrons module, and updates /data/resources with the new scripts. In the case of the first container boot, we rely on the one piece of initialization left in compute/. compute/container_setup.sh does the first-boot check that setup.sh used to do, and in addition to removing old api server installations and updating the cached container ID it runs the provision script - this time from the module installed in /usr/local/lib. * Container Initialization The Dockerfile CMD has been slightly simplified by symlinking the environment file into /etc/profile.d. This requires the use of bash’s -l flag to get a login shell in the docker container. Because the symlinks to the environment must be present when the shell starts, the environment script (opentrons/api/opentrons/resources/ot-environ.sh) is linked into /etc/profile.d by the Dockerfile (the rest of the configuration file symlinks are in opentrons/api/opentrons/resources/scripts/setup.sh). It is also linked twice - once from /data/system, which will be empty when the container boots for the first time; and once from the system installation of the api server in /usr/local/lib, as a fallback. The one-time environment variables ensure it is only executed once. Then, the container setup script runs. In most cases it doesn’t do much, but see above for the behavior during the first container boot. Since /data/system/scripts is in the path, we still call setup.sh and start.sh. In addition to what it used to do, setup.sh now makes symlinks for most of the configuration files we had been COPYing into Docker. These are now in /data/system. start.sh is pretty similar to before. * Container Building The Dockerfile is now parameterized to make building a local container slightly easier. Invoking docker build with no arguments builds a container for the pi (it has to, since this is what Resin does). Invoking docker build with the arguments to change the base image and clear RUNNING_ON_PI will build a container for local machines. To make this easier, there is a new top-level Makefile target, api-local-container, that invokes docker build with the correct arguments. Note that the local container still doesn’t 100% work; it needs more love to get over things like not having a dbus socket on all hosts. * Misc - Sweet new motd - Removed some of the nmcli commands used for janitoring the static ipv6 routes - Removed the nginx root server block and deleted the static files it was - serving - hardcoded some ports to get away from infinite env vars we can’t trust - Fixed an issue in the update server where it was sending the repr() of tracebacks in 500 messages for /server/update. Now it sends the result of traceback.format_tb() Closes #1114
- Loading branch information
Showing
30 changed files
with
257 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
0
compute/conf/inetd.conf → api/opentrons/resources/inetd.conf
100755 → 100644
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Set up the environment for the OT2. | ||
# This is sourced by the system login shell profile by symlinks placed in | ||
# /etc/profile.d by the Dockerfile. | ||
|
||
if [ -z $OT_ENVIRON_SET_UP ]; then | ||
echo "[ $0 ] Configuring environment" | ||
|
||
# Make sure pip installs things into /data | ||
export PIP_ROOT=/data/packages | ||
|
||
export OT_CONFIG_PATH=/data/system | ||
|
||
# Required for proper pipenv operation | ||
export PIPENV_VENV_IN_PROJECT=true | ||
# This is used by D-Bus clients such as Network Manager cli, announce_mdns | ||
# connecting to Host OS services | ||
export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket | ||
export PYTHONPATH=$PYTHONPATH:/data/packages/usr/local/lib/python3.6/site-packages | ||
export PATH=$PATH:/data/packages/usr/local/bin:$OT_CONFIG_PATH/scripts | ||
|
||
# TODO(seth, 8/15/2018): These are almost certainly unused and should be hardcoded | ||
# if they are in fact still used | ||
export OT_SETTINGS_DIR="" | ||
export OT_SERVER_UNIX_SOCKET_PATH=/tmp/aiohttp.sock | ||
export LABWARE_DEF=/etc/labware | ||
export AUDIO_FILES=/etc/audio | ||
export USER_DEFN_ROOT=/data/user_storage/opentrons_data/labware | ||
export OT_SMOOTHIE_ID=AMA | ||
export OT_ENVIRON_SET_UP=1 | ||
echo "[ $0 ] Environment configuration done" | ||
else | ||
echo "[ $0 ] Environment already configured" | ||
fi |
File renamed without changes.
File renamed without changes.
File renamed without changes.
0
compute/scripts/miniterm → api/opentrons/resources/scripts/miniterm
100644 → 100755
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python | ||
""" Copy everything here (except this script) into /data/system. | ||
This should be run | ||
- On the first boot of a new container (handled by `container_setup.sh`) | ||
- When a new version of the API server is installed by runapp (handled by `setup.py`) in the API server wheel | ||
""" | ||
|
||
import os | ||
import shutil | ||
import sys | ||
|
||
sys.path.append('/usr/local/bin') | ||
import find_python_module_path | ||
|
||
def provision(): | ||
""" Should be called the first time a given version of the server is run in a container. | ||
Should not be called if the server is not running in a container. | ||
""" | ||
provision_from_module = find_python_module_path.find_module('opentrons') | ||
provision_from_resources = os.path.join(provision_from_module, 'resources') | ||
print("Provisioning config and initialization from {}" | ||
.format(provision_from_resources)) | ||
config_dir = os.environ.get('OT_CONFIG_PATH', '/data/system') | ||
if os.path.exists(config_dir): | ||
shutil.rmtree(config_dir) | ||
shutil.copytree(provision_from_resources, config_dir) | ||
|
||
if __name__ == '__main__': | ||
provision() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env bash | ||
|
||
echo "[ $0 ] API server setup beginning" | ||
|
||
if [ ! -z $RUNNING_ON_PI ] ; then | ||
echo "[ $0 ] Container running on raspi detected, running system setup" | ||
mount_usb.py | ||
setup_gpio.py | ||
|
||
# Cleanup any connections. This will leave only wlan0 | ||
nmcli --terse --fields uuid,device connection show | sed -rn "s/(.*):(--)/\1/p" | xargs nmcli connection del || true | ||
nmcli --terse --fields uuid,device connection show | sed -rn "s/(.*):(eth0)/\1/p" | xargs nmcli connection del || true | ||
|
||
|
||
# nmcli makes an async call which might not finish before next network-related | ||
# operation starts. There is no graceful way to await for D-BUS event in shell | ||
# hence sleep is added to avoid race condition | ||
sleep 1 | ||
nmcli con add con-name "static-eth0" ifname eth0 type ethernet ipv4.method link-local | ||
else | ||
echo "[ $0 ] Container running locally" | ||
fi | ||
|
||
echo "[$0 ] Creating config file links (OT_CONFIG_PATH=$OT_CONFIG_PATH )..." | ||
|
||
ln -sf $OT_CONFIG_PATH/jupyter /root/.jupyter | ||
ln -sf $OT_CONFIG_PATH/audio /etc/audio | ||
rm /etc/nginx/nginx.conf | ||
ln -sf $OT_CONFIG_PATH/nginx.conf /etc/nginx/nginx.conf | ||
ln -sf $OT_CONFIG_PATH/inetd.conf /etc/inetd.conf | ||
mkdir -p /run/nginx | ||
|
||
echo "[ $0 ] API server setup done" |
1 change: 1 addition & 0 deletions
1
compute/scripts/setup_gpio.py → ...opentrons/resources/scripts/setup_gpio.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#!/usr/bin/env python | ||
from time import sleep | ||
|
||
from opentrons.drivers.rpi_drivers import gpio | ||
|
24 changes: 15 additions & 9 deletions
24
compute/scripts/start.sh → api/opentrons/resources/scripts/start.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,42 @@ | ||
#!/usr/bin/env bash | ||
|
||
echo "[ $0 ] API server starting" | ||
# mdns announcement | ||
announce_mdns.py & | ||
if [ ! -z $RUNNING_ON_PI ]; then | ||
echo "[ $0 ] MDNS beginning" | ||
announce_mdns.py & | ||
fi | ||
|
||
echo "[ $0 ] Starting nginx" | ||
# serve static pages and proxy HTTP services | ||
nginx | ||
|
||
echo "[ $0 ] Starting inetd" | ||
# enable SSH over ethernet | ||
inetd -e /etc/inetd.conf | ||
|
||
echo "[ $0 ] Running user boot scripts" | ||
# If user boot script exists, run it | ||
mkdir -p /data/boot.d | ||
run-parts /data/boot.d | ||
|
||
echo "Starting Opentrons update server" | ||
python -m otupdate --debug --port $OT_UPDATE_PORT & | ||
echo "[ $0 ] Starting Opentrons update server" | ||
python -m otupdate --debug --port 34000 & | ||
|
||
echo "Starting Jupyter Notebook server" | ||
echo "[ $0 ] Starting Jupyter Notebook server" | ||
mkdir -p /data/user_storage/opentrons_data/jupyter | ||
jupyter notebook --allow-root & | ||
|
||
# Check if config exists, and alert if not found | ||
echo "Checking for deck calibration data..." | ||
echo "[ $0 ] Checking for deck calibration data..." | ||
config_path=`python -c "from opentrons import config; print(config.get_config_index().get('deckCalibrationFile'))"` | ||
|
||
if [ ! -e "$config_path" ]; then | ||
echo $config_path | ||
echo "Config file not found. Please perform factory calibration and then restart robot" | ||
echo "[ $0 ] Config file not found. Please perform factory calibration and then restart robot" | ||
fi | ||
|
||
export ENABLE_NETWORKING_ENDPOINTS=true | ||
echo "Starting Opentrons API server" | ||
echo "[ $0 ] Starting Opentrons API server" | ||
python -m opentrons.server.main -U $OT_SERVER_UNIX_SOCKET_PATH opentrons.server.main:init | ||
echo "Server exited unexpectedly. Please power-cycle the machine, and contact Opentrons support." | ||
echo "[ $0 ] Server exited unexpectedly. Please power-cycle the machine, and contact Opentrons support." | ||
while true; do sleep 1; done |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.