Skip to content

Commit 654064d

Browse files
Add imagemagick to db
1 parent de7226e commit 654064d

File tree

8 files changed

+265
-13
lines changed

8 files changed

+265
-13
lines changed

functions/imagemagick/Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# syntax = docker/dockerfile:1.4
3+
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder
4+
5+
WORKDIR /tmp/build
6+
RUN mkdir /tmp/nix-store-closure
7+
8+
# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
9+
# hadolint ignore=SC2046
10+
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
11+
--mount=type=cache,target=/root/.cache \
12+
--mount=type=bind,target=/tmp/build \
13+
<<EOF
14+
nix \
15+
--extra-experimental-features "nix-command flakes" \
16+
--option filter-syscalls false \
17+
--extra-trusted-substituters "https://cache.iog.io" \
18+
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
19+
--show-trace \
20+
--log-format raw \
21+
build . --out-link /tmp/output/result
22+
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
23+
EOF
24+
25+
FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7
26+
27+
WORKDIR /app
28+
29+
COPY --from=builder /tmp/nix-store-closure /nix/store
30+
COPY --from=builder /tmp/output/ /app/
31+
32+
ENTRYPOINT ["/app/result/bin/entrypoint"]
33+
CMD ["--help"]

functions/imagemagick/flake.lock

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/imagemagick/flake.nix

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
description = "imagemagick";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils, ...}@inputs:
10+
11+
flake-utils.lib.eachDefaultSystem
12+
(system:
13+
let
14+
pkgs = import nixpkgs {
15+
inherit system;
16+
};
17+
18+
in rec
19+
{
20+
packages = rec {
21+
22+
# this derivation just contains the init.clj script
23+
scripts = pkgs.stdenv.mkDerivation {
24+
name = "scripts";
25+
src = ./.;
26+
installPhase = ''
27+
cp init.clj $out
28+
'';
29+
};
30+
31+
run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
32+
export PATH=${pkgs.lib.makeBinPath [pkgs.ghostscript pkgs.imagemagick pkgs.man]}
33+
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
34+
/usr/local/bin/bb ${scripts} "$@"
35+
'';
36+
37+
default = pkgs.buildEnv {
38+
name = "imagemagick";
39+
paths = [ run-entrypoint ];
40+
};
41+
};
42+
});
43+
}
44+

functions/imagemagick/init.clj

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(ns init
2+
(:require
3+
[babashka.process]
4+
[cheshire.core]))
5+
6+
(defn output [args]
7+
(try
8+
(->
9+
(apply babashka.process/process
10+
{:out :string}
11+
args)
12+
(deref)
13+
(babashka.process/check))
14+
(catch Throwable _ nil)))
15+
16+
(defn man [t]
17+
(output ["man" t]))
18+
(defn help [t]
19+
(output [t "--help"]))
20+
(defn h [t]
21+
(output [t "-h"]))
22+
23+
(try
24+
(let [[json-string & extra-args] *command-line-args*
25+
{:keys [args]} (cheshire.core/parse-string json-string true)]
26+
(println
27+
(-> (if (= "man" (first extra-args))
28+
(let [t (second args)] (or (man t) (help t) (h t)))
29+
(-> (babashka.process/process
30+
{:out :string}
31+
(format "%s" args))
32+
(deref)
33+
(babashka.process/check)))
34+
(deref)
35+
(babashka.process/check)
36+
:out)))
37+
(catch Throwable t
38+
(binding [*out* *err*]
39+
(println (str "Error: " (.getMessage t)))
40+
(System/exit 1))))
41+
42+

functions/imagemagick/runbook.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Background
2+
3+
The `curl` function has one parameter.
4+
5+
* `args`: the args to send to the image
6+
7+
## Usage
8+
9+
This function should be given a rw bind mount for the root of a project.
10+
11+
```sh
12+
docker run --rm \
13+
--mount type=bind,source=$PWD,target=/project \
14+
--entrypoint /app/result/bin/entrypoint \
15+
--workdir /project \
16+
vonwig/imagemagick:latest '{"args": "man convert"}'
17+
```
18+
19+
## Build
20+
21+
```sh
22+
docker build -t vonwig/imagemagick:latest .
23+
```
24+
25+
```sh
26+
# docker:command=build
27+
28+
docker buildx build \
29+
--builder hydrobuild \
30+
--platform linux/amd64,linux/arm64 \
31+
--tag vonwig/imagemagick:latest \
32+
--file Dockerfile \
33+
--push .
34+
docker pull vonwig/imagemagick:latest
35+
```

prompts/examples/imagemagick.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
tools:
3+
- name: imagemagick
4+
---
5+
6+
# prompt user
7+
8+
Use Imagemagick to convert the family.pdf file into a bunch of jpg images.

src/docker.clj

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161

6262
(defn backend-login-info [_]
6363
(curl/get
64-
"http://localhost/registry/info"
65-
{:raw-args ["--unix-socket" (get-backend-socket)]
66-
:throw false}))
64+
"http://localhost/registry/info"
65+
{:raw-args ["--unix-socket" (get-backend-socket)]
66+
:throw false}))
6767

6868
(defn backend-get-token [_]
6969
(curl/get
@@ -228,6 +228,16 @@
228228
(spec/def ::container-definition (spec/keys :opt-un [::host-dir ::entrypoint ::command ::user ::jwt]
229229
:req-un [::image]))
230230

231+
(spec/def ::pty-output string?)
232+
(spec/def ::exit-code integer?)
233+
(spec/def ::info any?)
234+
(spec/def ::done #{:timeout :exited})
235+
(spec/def ::timeout integer?)
236+
(spec/def ::kill-container any?)
237+
238+
(spec/def ::container-response (spec/keys :req-un [::pty-output ::exit-code ::info ::done]
239+
:opt-un [::timeout ::kill-container]))
240+
231241
(defn- -pull [m]
232242
(pull (merge m
233243
{:serveraddress "https://index.docker.io/v1/"}
@@ -236,7 +246,9 @@
236246
{:creds {:username (:user m)
237247
:password (or (:jwt m) (:pat m))}}))))
238248

239-
(defn run-function [{:keys [timeout] :or {timeout 600000} :as m}]
249+
(defn run-function
250+
"run container function with no stdin"
251+
[{:keys [timeout] :or {timeout 600000} :as m}]
240252
(-pull m)
241253
(let [x (create m)
242254
finished-channel (async/promise-chan)]
@@ -340,21 +352,38 @@
340352
{:pty-output s
341353
:exit-code (-> info :State :ExitCode)
342354
:info info}))
343-
(catch Throwable t
355+
(catch Throwable _
344356
(delete x)
345357
{}))))
346358

359+
(defn run-with-stdin-content
360+
"run container with stdin read from a file"
361+
[m]
362+
(let [x (docker/function-call-with-stdin
363+
(assoc m :content (slurp (-> m :stdin :file))))]
364+
(async/<!! (async/thread
365+
(Thread/sleep 10)
366+
(docker/finish-call x)))))
367+
368+
(defn run-container
369+
" params ::container-definition
370+
returns ::container-response"
371+
[m]
372+
(if (-> m :stdin :file)
373+
(run-with-stdin-content m)
374+
(run-function m)))
375+
347376
(defn get-login-info-from-desktop-backend
348377
"returns token or nil if not logged in or backend.sock is not available"
349378
[]
350379
(try
351380
(when (is-logged-in? {})
352-
(merge
353-
{:is-logged-in? true}
354-
(try
355-
(get-login-info {})
356-
(catch Throwable ex
357-
(logging/warn "Unable to extract login info: {{ex}}" {:ex ex})))))
381+
(merge
382+
{:is-logged-in? true}
383+
(try
384+
(get-login-info {})
385+
(catch Throwable ex
386+
(logging/warn "Unable to extract login info: {{ex}}" {:ex ex})))))
358387
(catch Throwable _)))
359388

360389
(comment

src/prompts.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
[])))
9999

100100
(def hub-images
101-
#{"curl" "qrencode" "toilet" "figlet" "gh" "typos" "fzf" "jq" "fmpeg" "pylint"})
101+
#{"curl" "qrencode" "toilet" "figlet" "gh" "typos" "fzf" "jq" "fmpeg" "pylint" "imagemagick"})
102102

103103
(defn collect-functions
104104
"get either :functions or :tools collection
@@ -252,7 +252,7 @@
252252
(when user {:user user})
253253
(when jwt {:jwt jwt})
254254
(when timeout {:timeout timeout}))
255-
{:keys [pty-output exit-code done] :as result} (docker/run-function function-call)
255+
{:keys [pty-output exit-code done] :as result} (docker/run-container function-call)
256256
exit-code-fail? (if (false? (:check-exit-code definition))
257257
false
258258
(not= 0 exit-code))]

0 commit comments

Comments
 (0)