Skip to content

Commit 92c9694

Browse files
committed
Split into deps and lein projects, Makefile -> bb.edn
1 parent ef87c71 commit 92c9694

File tree

15 files changed

+263
-63
lines changed

15 files changed

+263
-63
lines changed

dev/tramp-sample-project/Dockerfile

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Use an official Clojure image as parent
2+
FROM {{container/parent}}
3+
4+
# PATH "hardening"
5+
#
6+
# Upstream does not install the jdk via apt, but (1) copies it to /opt and
7+
# (2) adds it to PATH in an ENV directive. These are available when using docker
8+
# commands like run or exec, but will not propagate through ssh.
9+
#
10+
# Moreover, when executing a shell-command via tramp, it won't lookup the user-specific
11+
# PATH by default. Instead, it searches for the executable in conventional directories
12+
# like /usr/bin and the remotes initial `getconf PATH`.
13+
#
14+
# Lastly, in the case of running a login shell, PATH will be overwritten by /etc/profile
15+
# with a hardcoded value.
16+
#
17+
# To circumvent these and other potential issues, we ensure that java
18+
# is symlinked to the expected places.
19+
RUN ln -sv $JAVA_HOME/bin/* /usr/bin/
20+
21+
# Persisting upstreams other envars to /etc/environment ensures that they are available
22+
# from *everywhere* unless overwritten.
23+
RUN echo JAVA_HOME=$JAVA_HOME >> /etc/environment && \
24+
echo CLOJURE_VERSION=$CLOJURE_VERSION >> /etc/environment
25+
26+
# SSH server setup
27+
RUN apt-get update && apt-get install -y \
28+
openssh-server && \
29+
# Start as a service once, to init necessary runtime state
30+
service ssh start && \
31+
# ! Allow remote root access !
32+
# ! password: cider !
33+
sed -i 's/^#* *PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config && \
34+
echo 'root:cider' | chpasswd
35+
EXPOSE 22
36+
37+
# Project folder initialization
38+
COPY . /usr/src/app
39+
WORKDIR /usr/src/app
40+
RUN {{project/init-cmd}}
41+
42+
# Run the ssh server. [-D]on't be a daemon. Log to std[-e]rr.
43+
ENTRYPOINT ["/usr/sbin/sshd","-De"]

dev/tramp-sample-project/Makefile

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
build: remove-from-known-hosts
2+
docker build -t {{container/name}} .
3+
4+
run: build
5+
docker run -p {{container/ssh-port}}:22 {{container/name}}
6+
7+
remove-from-known-hosts:
8+
ssh-keygen -R '[localhost]:{{container/ssh-port}}'
9+
10+
ssh-login:
11+
echo "Password is: cider"
12+
ssh root@localhost -p {{container/ssh-port}}
13+
14+
stop:
15+
docker stop `(docker ps --quiet --filter ancestor={{container/name}})` || exit 0
16+
17+
cleanup: stop
18+
docker remove `(docker ps --all --quiet --filter ancestor={{container/name}})`

dev/tramp-sample-project/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ This way, for development purposes, we can SSH into it with TRAMP and exercise C
66

77
## Some ways to get started:
88

9-
### `cider-jack-in` from a tramp buffer
10-
* `M-:` `(async-shell-command "make run")` to run the Docker image
11-
* `M-:` `(find-file "/sshx:root@localhost#8022:/usr/src/app/src/foo.clj")`
12-
* `M-x` `cider-jack-in`
13-
* Enter password: `cider`
9+
### Using babashka
10+
11+
* Use `bb run lein` or `bb run tools-deps` to start a container
12+
* Follow the on screen instructions
13+
* Stop the container[s] with `bb stop lein`, `bb stop tools-deps`
14+
* Remove them with `bb clean lein`, `bb clean tools-deps`
15+
* See `bb.edn` for all commands and how this is wired up
16+
1417

1518
### Manually create a remote repl and connect to it
19+
* cd to `./lein`
1620
* In one terminal tab, run `make run` to run the Docker image
1721
* Once it's ready, from another tab, run `make ssh` and start a repl manually from there
1822
* The password is `cider`
@@ -24,3 +28,4 @@ Now, from emacs you can `cider-connect` to localhost.
2428
* `M-x cider-connect` (choose `localhost`, `7888`)
2529

2630
NOTE: Do not visit `foo.clj` directly - do it from dired instead.
31+

dev/tramp-sample-project/bb.edn

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
;; TODO Troubleshooting and explainers:
2+
;; - provide a better fix for the ssh-key problem
3+
;; - ! (nrepl-use-ssh-fallback-for-remote-hosts t) has to be t
4+
;; - vterm will fail with /usr/bin/bash, there's only /bin/bash
5+
;; - explain unsafe autosafe names
6+
;; - ! message how to connect
7+
;; - ! password? and fingerprint?
8+
;; - check if already running and stop it
9+
10+
{:paths ["."]
11+
:tasks
12+
{build {:depends [-dockerfile]
13+
:task (bash "docker build -t {{container/name}} ./{{project/dir}}")}
14+
start {:depends [build]
15+
:task
16+
(do (echo "\n--------------------------------------------------------------------------------\n"
17+
"Get started:\n"
18+
" (1) M-x find-file \"ssh:/root@localhost#{{container/ssh-port}}:/usr/src/app/src/foo.clj\""
19+
" (2) Accept the host key [on xfirst run only]"
20+
" (3) Enter password: \"cider\""
21+
" (4) M-x cider-jack-in-clj"
22+
"\n--------------------------------------------------------------------------------\n")
23+
(bash "docker run -p {{container/ssh-port}}:22 {{container/name}}"))}
24+
stop (bash "docker stop $(docker ps --quiet --filter ancestor={{container/name}}) || exit 0")
25+
cleanup {:depends [stop -remove-from-known-hosts]
26+
:task (bash "docker remove `(docker ps --all --quiet --filter ancestor={{container/name}})`")}
27+
list (bash "docker ps --filter ancestor={{container/name}}")
28+
29+
-remove-from-known-hosts (bash "ssh-keygen -R \"[localhost]:{{container/ssh-port}}\"")
30+
;; TODO stop if already running
31+
32+
-dockerfile (spit (str (:project/dir target) "Dockerfile") (selmer/render-file "Dockerfile.template" target))
33+
-makefile (spit (str (:project/dir target) "Makefile") (selmer/render-file "Makefile.template" target))
34+
35+
:init
36+
(do (def config
37+
{:lein {:project/dir "lein-app/"
38+
:project/init-cmd "lein deps"
39+
:container/ssh-port "8022"
40+
:container/name "cider-tramp-dev-lein"
41+
:container/parent "clojure:temurin-17-lein-bullseye"}
42+
:tools-deps {:project/dir "tools-deps-app/"
43+
:project/init-cmd "clojure -P"
44+
:container/ssh-port "9022"
45+
:container/name "cider-tramp-dev-tools-deps"
46+
:container/parent "clojure:temurin-17-tools-deps-bullseye"}})
47+
48+
(def target (config (or (keyword (first *command-line-args*)) :tools-deps)))
49+
(defn bash [s] (shell (str "bash -c '" (selmer/render s target) "'")))
50+
(defn echo [& lines] (println (selmer/render (str/join "\n" lines) target)))
51+
;; Print the shell-command that's executed. From https://clojurians.slack.com/archives/CLX41ASCS/p1693517240805539?thread_ts=1693515381.686759&cid=CLX41ASCS
52+
(alter-var-root (var babashka.process/*defaults*) assoc :pre-start-fn (fn [m] (apply println ">" (:cmd m)))))
53+
:leave (println "Finished task:" (:name (current-task)))
54+
:requires ([selmer.parser :as selmer]
55+
[clojure.string :as str])}}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Use an official Clojure image as parent
2+
FROM clojure:temurin-17-lein-bullseye
3+
4+
# PATH "hardening"
5+
#
6+
# Upstream does not install the jdk via apt, but (1) copies it to /opt and
7+
# (2) adds it to PATH in an ENV directive. These are available when using docker
8+
# commands like run or exec, but will not propagate through ssh.
9+
#
10+
# Moreover, when executing a shell-command via tramp, it won't lookup the user-specific
11+
# PATH by default. Instead, it searches for the executable in conventional directories
12+
# like /usr/bin and the remotes initial `getconf PATH`.
13+
#
14+
# Lastly, in the case of running a login shell, PATH will be overwritten by /etc/profile
15+
# with a hardcoded value.
16+
#
17+
# To circumvent these and other potential issues, we ensure that java
18+
# is symlinked to the expected places.
19+
RUN ln -sv $JAVA_HOME/bin/* /usr/bin/
20+
21+
# Persisting upstreams other envars to /etc/environment ensures that they are available
22+
# from *everywhere* unless overwritten.
23+
RUN echo JAVA_HOME=$JAVA_HOME >> /etc/environment && \
24+
echo CLOJURE_VERSION=$CLOJURE_VERSION >> /etc/environment
25+
26+
# SSH server setup
27+
RUN apt-get update && apt-get install -y \
28+
openssh-server && \
29+
# Start as a service once, to init necessary runtime state
30+
service ssh start && \
31+
# ! Allow remote root access !
32+
# ! password: cider !
33+
sed -i 's/^#* *PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config && \
34+
echo 'root:cider' | chpasswd
35+
EXPOSE 22
36+
37+
# Project folder initialization
38+
COPY . /usr/src/app
39+
WORKDIR /usr/src/app
40+
RUN lein deps
41+
42+
# Run the ssh server. [-D]on't be a daemon. Log to std[-e]rr.
43+
ENTRYPOINT ["/usr/sbin/sshd","-De"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
build: remove-from-known-hosts
2+
docker build -t cider-tramp-dev-lein .
3+
4+
run: build
5+
docker run -p 8022:22 cider-tramp-dev-lein
6+
7+
remove-from-known-hosts:
8+
ssh-keygen -R '[localhost]:8022'
9+
10+
ssh-login:
11+
echo "Password is: cider"
12+
ssh root@localhost -p 8022
13+
14+
stop:
15+
docker stop `(docker ps --quiet --filter ancestor=cider-tramp-dev-lein)` || exit 0
16+
17+
cleanup: stop
18+
docker remove `(docker ps --all --quiet --filter ancestor=cider-tramp-dev-lein)`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns foo
2+
(:require
3+
[clj-http.client :as client]))
4+
5+
(def home (client/get "http://www.clojure.org"))
6+
7+
(sort-by second > (frequencies (:body home)))

0 commit comments

Comments
 (0)