Skip to content

Instantly share code, notes, and snippets.

@hcl337
Last active September 9, 2021 20:00
Show Gist options
  • Save hcl337/12113fd6099d061e6283c2ed80a62610 to your computer and use it in GitHub Desktop.
Save hcl337/12113fd6099d061e6283c2ed80a62610 to your computer and use it in GitHub Desktop.
An example Dockerfile setup for the Freedom Robotics Agent
# Replace with valid credentials for the device you want to create
FR_ACCOUNT=A0000000000000000
FR_DEVICE=D0000000000000000
FR_TOKEN=T0000000000000000
FR_SECRET=S0000000000000000
FR_INSTALL_ELEMENTS=warn_no_fail,no_credentials,service_none
FROM ros:melodic
RUN apt-get update ; apt-get install -y curl python-pip
# Edit this list to add webrtc and others
ENV FR_INSTALL_ELEMENTS=warn_no_fail,no_credentials,service_none
RUN curl -s "https://api.freedomrobotics.ai/accounts/INSTALL_ONLY/devices/GENERIC_DEVICE/installscript?mc_token=INSTALL_ONLY_DEVICE_TOKEN&install_elements=${FR_INSTALL_ELEMENTS}" | python
RUN mkdir /freedom
COPY ./freedom_register.py /freedom/
COPY ./freedom_keep_alive.py /freedom/
COPY ./entrypoint.sh /freedom/
RUN ls /freedom
ENTRYPOINT /freedom/entrypoint.sh
#!/bin/bash
# Read the credentials for the device from environment variables
# FR_DEVICE, FR_TOKEN, FR_SECRET then register Freedom Robotics
# as a system service with a keep-alive if it exits.
python /freedom/freedom_register.py
python /freedom/freedom_keep_alive.py &
# Replace with your final code or other services to run before.
sleep infinity
import os
import time
import logging
import subprocess
def execute(cmd):
"""
Simple wrapper for executing command line
"""
try:
return subprocess.check_output(cmd, shell=True).strip()
except Exception as e:
return None
def get_freedomrobotics_python_package_path( ):
"""
Simple helper to get the freedom package base file path on the system
"""
result = execute( 'pip show freedomrobotics | grep "Location:"' ).split("\n")
for l in result:
if "Location:" in l:
return l.replace("Location: ", "")
else:
raise Exception("Could not find python package: " + str(result))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
agent_path = get_freedomrobotics_python_package_path() + "/freedomrobotics/agent.py"
num_startup_failures = 0
while True:
# If it exited immediately, then we assume something happened
# on startup (Bad credentials, deleted device, etc) and so wait
# longer inbetween.
if num_startup_failures > 5:
time.sleep(60)
start_agent_time = time.time()
execute("service freedomrobotics start")
logger.info("freedomrobotics service started")
while True:
time.sleep(10)
result = execute("service freedomrobotics status")
if result is None or "is running" not in result:
logger.error("freedomrobotics service exited")
if time.time() - start_agent_time < 30:
num_startup_failures += 1
else:
num_startup_failures = 0
break
import os
import json
import logging
import subprocess
from freedomrobotics.management import register_initd_service
from freedomrobotics.management import register_systemd_service
CREDENTIALS_PATH = os.path.expanduser("~/.config/freedomrobotics/credentials")
CREDENTIALS_DIR = os.path.dirname(CREDENTIALS_PATH)
def get_env_var(var):
return subprocess.check_output("echo $" + var, shell=True).strip()
def execute(cmd):
try:
return subprocess.check_output(cmd, shell=True).strip()
except Exception as e:
return None
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
credentials = {
"account": get_env_var("FR_ACCOUNT"),
"device": get_env_var("FR_DEVICE"),
"token": get_env_var("FR_TOKEN"),
"secret": get_env_var("FR_SECRET"),
"install_elements": get_env_var("FR_INSTALL_ELEMENTS").split(",")
}
if not os.path.exists(CREDENTIALS_DIR):
os.makedirs(CREDENTIALS_DIR)
with open(CREDENTIALS_PATH, 'w') as f:
f.write(json.dumps(credentials, indent=4))
systemd_pid = execute("pidof systemd")
logger.debug("PID of Systemd: " + str(systemd_pid))
# Try to register the service for
register_initd_service.install(logger)
@aaronwoodcox
Copy link

aaronwoodcox commented Sep 9, 2021

Note - in order to run under python3 I needed to make the following changes. Is this expected? Can/should an update be made to the example files located here?

diff --git a/freedom/freedom_keep_alive.py b/freedom/freedom_keep_alive.py
index 3a8e0de..a091dae 100644
--- a/freedom/freedom_keep_alive.py
+++ b/freedom/freedom_keep_alive.py
@@ -8,7 +8,7 @@ def execute(cmd):
     Simple wrapper for executing command line 
     """
     try:
-        return subprocess.check_output(cmd, shell=True).strip()
+        return subprocess.check_output(cmd, shell=True).decode('utf8').strip()
     except Exception as e:
         return None
 
diff --git a/freedom/freedom_register.py b/freedom/freedom_register.py
index 2bff078..89b377c 100644
--- a/freedom/freedom_register.py
+++ b/freedom/freedom_register.py
@@ -10,11 +10,11 @@ CREDENTIALS_PATH = os.path.expanduser("~/.config/freedomrobotics/credentials")
 CREDENTIALS_DIR = os.path.dirname(CREDENTIALS_PATH)
 
 def get_env_var(var):
-    return subprocess.check_output("echo $" + var, shell=True).strip()
+    return subprocess.check_output("echo $" + var, shell=True).decode('utf8').strip()
 
 def execute(cmd):
     try:
-        return subprocess.check_output(cmd, shell=True).strip()
+        return subprocess.check_output(cmd, shell=True).decode('utf8').strip()
     except Exception as e:
         return None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment