Skip to content

Commit

Permalink
Add imagemagick to db
Browse files Browse the repository at this point in the history
  • Loading branch information
slimslenderslacks committed Oct 11, 2024
1 parent de7226e commit 654064d
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 13 deletions.
33 changes: 33 additions & 0 deletions functions/imagemagick/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

# syntax = docker/dockerfile:1.4
FROM nixos/nix:2.21.1@sha256:3f6c77ee4d2c82e472e64e6cd7087241dc391421a0b42c22e6849c586d5398d9 AS builder

WORKDIR /tmp/build
RUN mkdir /tmp/nix-store-closure

# ignore SC2046 because the output of nix-store -qR will never have spaces - this is safe here
# hadolint ignore=SC2046
RUN --mount=type=cache,target=/nix,from=nixos/nix:2.21.1,source=/nix \
--mount=type=cache,target=/root/.cache \
--mount=type=bind,target=/tmp/build \
<<EOF
nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
--extra-trusted-substituters "https://cache.iog.io" \
--extra-trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" \
--show-trace \
--log-format raw \
build . --out-link /tmp/output/result
cp -R $(nix-store -qR /tmp/output/result) /tmp/nix-store-closure
EOF

FROM babashka/babashka:latest@sha256:9e0381fc4c78ee6ff12fd8836352cf343afba289aceb77e36129d92f30a92cc7

WORKDIR /app

COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/output/ /app/

ENTRYPOINT ["/app/result/bin/entrypoint"]
CMD ["--help"]
61 changes: 61 additions & 0 deletions functions/imagemagick/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions functions/imagemagick/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
description = "imagemagick";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, ...}@inputs:

flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
};

in rec
{
packages = rec {

# this derivation just contains the init.clj script
scripts = pkgs.stdenv.mkDerivation {
name = "scripts";
src = ./.;
installPhase = ''
cp init.clj $out
'';
};

run-entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
export PATH=${pkgs.lib.makeBinPath [pkgs.ghostscript pkgs.imagemagick pkgs.man]}
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
/usr/local/bin/bb ${scripts} "$@"
'';

default = pkgs.buildEnv {
name = "imagemagick";
paths = [ run-entrypoint ];
};
};
});
}

42 changes: 42 additions & 0 deletions functions/imagemagick/init.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(ns init
(:require
[babashka.process]
[cheshire.core]))

(defn output [args]
(try
(->
(apply babashka.process/process
{:out :string}
args)
(deref)
(babashka.process/check))
(catch Throwable _ nil)))

(defn man [t]
(output ["man" t]))
(defn help [t]
(output [t "--help"]))
(defn h [t]
(output [t "-h"]))

(try
(let [[json-string & extra-args] *command-line-args*
{:keys [args]} (cheshire.core/parse-string json-string true)]
(println
(-> (if (= "man" (first extra-args))
(let [t (second args)] (or (man t) (help t) (h t)))
(-> (babashka.process/process
{:out :string}
(format "%s" args))
(deref)
(babashka.process/check)))
(deref)
(babashka.process/check)
:out)))
(catch Throwable t
(binding [*out* *err*]
(println (str "Error: " (.getMessage t)))
(System/exit 1))))


35 changes: 35 additions & 0 deletions functions/imagemagick/runbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Background

The `curl` function has one parameter.

* `args`: the args to send to the image

## Usage

This function should be given a rw bind mount for the root of a project.

```sh
docker run --rm \
--mount type=bind,source=$PWD,target=/project \
--entrypoint /app/result/bin/entrypoint \
--workdir /project \
vonwig/imagemagick:latest '{"args": "man convert"}'
```

## Build

```sh
docker build -t vonwig/imagemagick:latest .
```

```sh
# docker:command=build

docker buildx build \
--builder hydrobuild \
--platform linux/amd64,linux/arm64 \
--tag vonwig/imagemagick:latest \
--file Dockerfile \
--push .
docker pull vonwig/imagemagick:latest
```
8 changes: 8 additions & 0 deletions prompts/examples/imagemagick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
tools:
- name: imagemagick
---

# prompt user

Use Imagemagick to convert the family.pdf file into a bunch of jpg images.
51 changes: 40 additions & 11 deletions src/docker.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@

(defn backend-login-info [_]
(curl/get
"http://localhost/registry/info"
{:raw-args ["--unix-socket" (get-backend-socket)]
:throw false}))
"http://localhost/registry/info"
{:raw-args ["--unix-socket" (get-backend-socket)]
:throw false}))

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

(spec/def ::pty-output string?)
(spec/def ::exit-code integer?)
(spec/def ::info any?)
(spec/def ::done #{:timeout :exited})
(spec/def ::timeout integer?)
(spec/def ::kill-container any?)

(spec/def ::container-response (spec/keys :req-un [::pty-output ::exit-code ::info ::done]
:opt-un [::timeout ::kill-container]))

(defn- -pull [m]
(pull (merge m
{:serveraddress "https://index.docker.io/v1/"}
Expand All @@ -236,7 +246,9 @@
{:creds {:username (:user m)
:password (or (:jwt m) (:pat m))}}))))

(defn run-function [{:keys [timeout] :or {timeout 600000} :as m}]
(defn run-function
"run container function with no stdin"
[{:keys [timeout] :or {timeout 600000} :as m}]
(-pull m)
(let [x (create m)
finished-channel (async/promise-chan)]
Expand Down Expand Up @@ -340,21 +352,38 @@
{:pty-output s
:exit-code (-> info :State :ExitCode)
:info info}))
(catch Throwable t
(catch Throwable _
(delete x)
{}))))

(defn run-with-stdin-content
"run container with stdin read from a file"
[m]
(let [x (docker/function-call-with-stdin
(assoc m :content (slurp (-> m :stdin :file))))]
(async/<!! (async/thread
(Thread/sleep 10)
(docker/finish-call x)))))

(defn run-container
" params ::container-definition
returns ::container-response"
[m]
(if (-> m :stdin :file)
(run-with-stdin-content m)
(run-function m)))

(defn get-login-info-from-desktop-backend
"returns token or nil if not logged in or backend.sock is not available"
[]
(try
(when (is-logged-in? {})
(merge
{:is-logged-in? true}
(try
(get-login-info {})
(catch Throwable ex
(logging/warn "Unable to extract login info: {{ex}}" {:ex ex})))))
(merge
{:is-logged-in? true}
(try
(get-login-info {})
(catch Throwable ex
(logging/warn "Unable to extract login info: {{ex}}" {:ex ex})))))
(catch Throwable _)))

(comment
Expand Down
4 changes: 2 additions & 2 deletions src/prompts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
[])))

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

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

0 comments on commit 654064d

Please sign in to comment.