Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Mage python runner (#67265)

Nicola Mometto1e3a87911608
diff --git a/bb.edn b/bb.edn
index a8cc34f3dc9..309ea33824c 100644
--- a/bb.edn
+++ b/bb.edn
@@ -107,6 +107,12 @@
:requires [[mage.start-maildev :as start-maildev]]
:task (task! (start-maildev/start-maildev!))}
+ start-python-runner
+ {:doc "Start python-runner container with internal Moto S3 server for Python transforms"
+ :examples [["./bin/mage start-python-runner" "start python-runner with internal S3"]]
+ :requires [[mage.start-python-runner :as start-python-runner]]
+ :task (task! (start-python-runner/start-python-runner!))}
+
start-db
{:doc "Start a db on a default port in docker"
:examples [["./bin/mage start-db postgres latest" "start the latest postgres db we support"]
diff --git a/mage/src/mage/start_python_runner.clj b/mage/src/mage/start_python_runner.clj
new file mode 100644
index 00000000000..ed0b4592d5d
--- /dev/null
+++ b/mage/src/mage/start_python_runner.clj
@@ -0,0 +1,70 @@
+(ns mage.start-python-runner
+ (:require
+ [babashka.http-client :as http]
+ [clojure.string :as str]
+ [mage.be-dev :as be-dev]
+ [mage.color :as c]
+ [mage.shell :as shell]))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private container-name "mb-python-runner")
+(def ^:private image-name "metabase/python-runner:latest")
+
+(defn- kill-existing! []
+ (println (c/red "Killing existing container:") container-name "...")
+ (shell/sh* {:quiet? true} "docker" "kill" container-name)
+ (shell/sh* {:quiet? true} "docker" "rm" container-name))
+
+(defn- wait-for-server!
+ "Wait for the python-runner server to be ready."
+ []
+ (print "Waiting for python-runner server")
+ (flush)
+ (loop [attempts 60]
+ (if (zero? attempts)
+ (do
+ (println)
+ (println (c/red " ✗ Server failed to start after 30 seconds"))
+ false)
+ (let [ready? (try
+ (let [resp (http/get "http://localhost:5001/v1/status"
+ {:throw false :timeout 1000})]
+ (= 200 (:status resp)))
+ (catch Exception _ false))]
+ (if ready?
+ (do
+ (println)
+ (println (c/green " ✓ Server is ready"))
+ true)
+ (do
+ (print ".")
+ (flush)
+ (Thread/sleep 500)
+ (recur (dec attempts))))))))
+
+(defn start-python-runner!
+ "Starts the python-runner container with internal Moto S3 server."
+ []
+ (kill-existing!)
+ (println (c/green "Starting python-runner container..."))
+ (let [cmd ["docker" "run"
+ "-d"
+ "-p" "5001:5000"
+ "-p" "4566:4566"
+ "-e" "AUTH_TOKEN=dev-token-12345"
+ "-e" "ENABLE_INTERNAL_S3=true"
+ "--name" container-name
+ image-name]]
+ (println "Running:" (c/magenta (str/join " " cmd)))
+ (apply shell/sh cmd)
+ (println)
+ (when (wait-for-server!)
+ (println (c/cyan "Started python-runner on port 5001 with internal S3 on port 4566"))
+ (println)
+ (when (be-dev/nrepl-open?)
+ (println "Configuring Metabase via nREPL...")
+ (be-dev/nrepl-eval
+ "metabase-enterprise.transforms-python.settings"
+ "(python-storage-s-3-container-endpoint! \"http://localhost:4566\")")
+ (println)))))