Tidy up the AWS CLI interceptor example.

This commit is contained in:
Ray Miller 2025-01-26 14:02:19 +00:00
parent 7cf4e5a4df
commit dd885ce559
Signed by: ray
GPG key ID: 043F786C4CD681B8
6 changed files with 90 additions and 45 deletions

View file

@ -8,50 +8,25 @@
(ordo interceptor create-tmp-dir)
(ordo interceptor require-commands)
(ordo interceptor user-info)
(ordo util flatten))
;; TODO: this should be in (ordo interceptor download) and it needs arg validation
(define* (download name #:key url target-dir register)
(interceptor
name
#:enter (lambda (ctx)
(let* ((url target-dir (expand-vars ctx url target-dir))
(file-name (file-name-join* target-dir (file-basename url))))
(run (context-connection ctx) "wget" "-O" file-name url #:check? #t)
(when register
(var-set! ctx register file-name))))
#:leave (lambda (ctx) (when register (var-delete! ctx register)))
#:error (lambda (ctx) (when register (var-delete! ctx register)))))
;; TODO: this should be in (ordo interceptor unzip) and it needs arg validation
(define* (unzip name #:key file-name target-dir)
(interceptor
name
#:enter (lambda (ctx)
(let ((file-name target-dir (expand-vars ctx file-name target-dir)))
(run (context-connection ctx) "unzip" file-name "-d" target-dir #:check? #t)))))
;; TODO: this should be in (ordo interceptor command)
;; Maybe it could expose more of the run functionality?
(define (command name prog . args)
(interceptor
name
#:enter (lambda (ctx)
(run (context-connection ctx)
(expand-vars ctx prog)
(map (lambda (a) (expand-vars ctx a)) (flatten args))
#:check? #t))))
(ordo interceptor download)
(ordo interceptor unzip)
(ordo interceptor command))
;; This example shows that a function can act a bit like an ansible role by
;; returning a list of interceptors to be added to the caller's interceptor
;; chain. (The list will be flattened to construct the final chain.)
(define* (install-aws-cli #:key (url "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip") update? install-dir bin-dir)
(list (require-commands "wget" "unzip")
(create-tmp-dir #:register 'aws-cli-tmp)
(download "download-aws-cli" #:url url #:target-dir (let-vars (aws-cli-tmp) aws-cli-tmp) #:register 'aws-cli-zipfile)
(unzip "extract-aws-cli" #:file-name (let-vars (aws-cli-zipfile) aws-cli-zipfile) #:target-dir (let-vars (aws-cli-tmp) aws-cli-tmp))
(download "download-aws-cli" #:url url #:target-dir (var aws-cli-tmp) #:register 'aws-cli-zipfile)
(unzip "extract-aws-cli" #:file-name (var aws-cli-zipfile) #:target-dir (var aws-cli-tmp))
(command "run-aws-cli-installer"
(let-vars (aws-cli-tmp) (file-name-join* aws-cli-tmp "aws" "install"))
(when install-dir `("-i" ,install-dir))
(when bin-dir `("-b" ,bin-dir))
(when update? "-u"))))
(list
(let-vars (aws-cli-tmp) (file-name-join* aws-cli-tmp "aws" "install"))
(when install-dir `("-i" ,install-dir))
(when bin-dir `("-b" ,bin-dir))
(when update? "-u")
#:check? #t))))
(playbook
#:name "Test Playbook"

View file

@ -7,6 +7,7 @@
(ordo interceptor create-tmp-dir)
(ordo interceptor stat-file)
(ordo interceptor user-info)
(ordo interceptor command)
(ordo interceptor debug))
(playbook
@ -22,10 +23,12 @@
(install-file
"install-hello"
#:path (let-vars (tmp-dir) (file-name-join* tmp-dir "hello.txt"))
#:content (let-vars (file-content) file-content)
#:content (var file-content)
#:register 'hello)
(stat-file
"stat-hello"
#:path (let-vars (hello) hello)
#:path (var hello)
#:register 'hello-stat)
(command "list-tmp-dir" (list "ls" "-l" (var tmp-dir) #:check? #t) #:register 'dir-list)
(command "list-root-dir" (list "ls" "-l" "/root" #:check? #f) #:register 'root-list)
(debug-vars)))))