From f54bfc3312f2394b306f8995ea539c5cdafa0805 Mon Sep 17 00:00:00 2001 From: David Gannon <19214156+dgannon991@users.noreply.github.com> Date: Fri, 12 Jul 2024 19:16:44 +0100 Subject: [PATCH] Build out own images and use them in our examples Signed-off-by: David Gannon <19214156+dgannon991@users.noreply.github.com> --- airgap/charts/whalegap/values.yaml | 4 +- airgap/porter.yaml | 6 +- docker/porter.yaml | 12 +- images/README.md | 3 + images/whalesay/Dockerfile | 5 + images/whalesay/LICENSE | 36 ++++++ images/whalesay/Makefile | 8 ++ images/whalesay/cowsay | 187 +++++++++++++++++++++++++++++ images/whalesay/docker.cow | 19 +++ images/whalesayd/.gitignore | 1 + images/whalesayd/Dockerfile | 7 ++ images/whalesayd/Makefile | 13 ++ images/whalesayd/README.md | 36 ++++++ images/whalesayd/go.mod | 3 + images/whalesayd/main.go | 49 ++++++++ 15 files changed, 378 insertions(+), 11 deletions(-) create mode 100644 images/README.md create mode 100644 images/whalesay/Dockerfile create mode 100644 images/whalesay/LICENSE create mode 100644 images/whalesay/Makefile create mode 100755 images/whalesay/cowsay create mode 100644 images/whalesay/docker.cow create mode 100644 images/whalesayd/.gitignore create mode 100644 images/whalesayd/Dockerfile create mode 100644 images/whalesayd/Makefile create mode 100644 images/whalesayd/README.md create mode 100644 images/whalesayd/go.mod create mode 100644 images/whalesayd/main.go diff --git a/airgap/charts/whalegap/values.yaml b/airgap/charts/whalegap/values.yaml index 9faf3f7..c5b7486 100644 --- a/airgap/charts/whalegap/values.yaml +++ b/airgap/charts/whalegap/values.yaml @@ -8,8 +8,8 @@ msg: "Whale aren't you special?" replicaCount: 1 image: - repository: "carolynvs/whalesayd" - digest: "sha256:8b92b7269f59e3ed824e811a1ff1ee64f0d44c0218efefada57a4bebc2d7ef6f" + repository: "ghcr.io/getporter/examples/images/whalesayd" + digest: "sha256:18c099989986f61c0678b8040cc8d2e80ba20ecd096da51f8da3a31c5d1138b8" pullPolicy: IfNotPresent imagePullSecrets: [] diff --git a/airgap/porter.yaml b/airgap/porter.yaml index fa9058b..8c59aab 100644 --- a/airgap/porter.yaml +++ b/airgap/porter.yaml @@ -1,6 +1,6 @@ schemaVersion: 1.0.0-alpha.1 name: examples/whalegap -version: 0.2.0 +version: 0.2.1 description: "An example bundle that demonstrates how to sneak a whale-sized bundle through an airgap" registry: ghcr.io/getporter @@ -22,8 +22,8 @@ images: whalesayd: description: "Whalesay as a service" imageType: "docker" - repository: "carolynvs/whalesayd" - digest: "sha256:8b92b7269f59e3ed824e811a1ff1ee64f0d44c0218efefada57a4bebc2d7ef6f" + repository: "ghcr.io/getporter/examples/images/whalesayd" + digest: "sha256:18c099989986f61c0678b8040cc8d2e80ba20ecd096da51f8da3a31c5d1138b8" mixins: - helm3 diff --git a/docker/porter.yaml b/docker/porter.yaml index 318c6bb..270bd4a 100644 --- a/docker/porter.yaml +++ b/docker/porter.yaml @@ -1,6 +1,6 @@ schemaVersion: 1.0.0-alpha.1 name: examples/whalesay -version: 0.2.0 +version: 0.2.1 description: "An example bundle that uses docker through the magic of whalespeak" registry: ghcr.io/getporter @@ -21,16 +21,16 @@ mixins: install: - docker: run: - image: "docker/whalesay:latest" + image: "ghcr.io/getporter/examples/images/whalesay:latest" rm: true arguments: - cowsay - Hello World - + upgrade: - docker: run: - image: "docker/whalesay:latest" + image: "ghcr.io/getporter/examples/images/whalesay:latest" rm: true arguments: - cowsay @@ -39,7 +39,7 @@ upgrade: say: - docker: run: - image: "docker/whalesay:latest" + image: "ghcr.io/getporter/examples/images/whalesay:latest" rm: true arguments: - cowsay @@ -48,7 +48,7 @@ say: uninstall: - docker: run: - image: "docker/whalesay:latest" + image: "ghcr.io/getporter/examples/images/whalesay:latest" rm: true arguments: - cowsay diff --git a/images/README.md b/images/README.md new file mode 100644 index 0000000..9cec5f8 --- /dev/null +++ b/images/README.md @@ -0,0 +1,3 @@ +# Porter Example Images + +A collection of images used within the porter examples, and published to the getporter/examples/images container registry diff --git a/images/whalesay/Dockerfile b/images/whalesay/Dockerfile new file mode 100644 index 0000000..a627975 --- /dev/null +++ b/images/whalesay/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine +RUN apk add --no-cache perl +COPY cowsay /usr/local/bin/cowsay +COPY docker.cow /usr/local/share/cows/default.cow +ENTRYPOINT ["/usr/local/bin/cowsay"] diff --git a/images/whalesay/LICENSE b/images/whalesay/LICENSE new file mode 100644 index 0000000..9447704 --- /dev/null +++ b/images/whalesay/LICENSE @@ -0,0 +1,36 @@ +============== +cowsay License +============== + +cowsay is distributed under the same licensing terms as Perl: the +Artistic License or the GNU General Public License. If you don't +want to track down these licenses and read them for yourself, use +the parts that I'd prefer: + +(0) I wrote it and you didn't. + +(1) Give credit where credit is due if you borrow the code for some +other purpose. + +(2) If you have any bugfixes or suggestions, please notify me so +that I may incorporate them. + +(3) If you try to make money off of cowsay, you suck. + +=============== +cowsay Legalese +=============== + +(0) Copyright (c) 1999 Tony Monroe. All rights reserved. All +lefts may or may not be reversed at my discretion. + +(1) This software package can be freely redistributed or modified +under the terms described above in the "cowsay License" section +of this file. + +(2) cowsay is provided "as is," with no warranties whatsoever, +expressed or implied. If you want some implied warranty about +merchantability and/or fitness for a particular purpose, you will +not find it here, because there is no such thing here. + +(3) I hate legalese. diff --git a/images/whalesay/Makefile b/images/whalesay/Makefile new file mode 100644 index 0000000..66d49fb --- /dev/null +++ b/images/whalesay/Makefile @@ -0,0 +1,8 @@ +REGISTRY?=ghcr.io/getporter/examples/images/whalesay +LATEST=$(REGISTRY):latest + +build: + docker build -t $(LATEST) . + +publish: build + docker push $(LATEST) diff --git a/images/whalesay/cowsay b/images/whalesay/cowsay new file mode 100755 index 0000000..8ef8908 --- /dev/null +++ b/images/whalesay/cowsay @@ -0,0 +1,187 @@ +#!/usr/bin/perl + +## +## Cowsay 3.03 +## +## This file is part of cowsay. (c) 1999-2000 Tony Monroe. +## + +use Text::Tabs qw(expand); +use Text::Wrap qw(wrap fill $columns); +use File::Basename; +use Getopt::Std; +use Cwd; + +$version = "3.03"; +$progname = basename($0); +$eyes = "oo"; +$tongue = " "; +$cowpath = $ENV{'COWPATH'} || '/usr/local/share/cows'; +@message = (); +$thoughts = ""; + +## Yeah, this is rude, I know. But hopefully it gets around a nasty +## little version dependency. + +$Text::Wrap::initial_tab = 8; +$Text::Wrap::subsequent_tab = 8; +$Text::Wrap::tabstop = 8; + +## One of these days, we'll get it ported to Windows. Yeah, right. + +if (($^O eq "MSWin32") or ($^O eq "Windows_NT")) { ## Many perls, eek! + $pathsep = ';'; +} else { + $pathsep = ':'; +} + +%opts = ( + 'e' => 'oo', + 'f' => 'default.cow', + 'n' => 0, + 'T' => ' ', + 'W' => 40, +); + +getopts('bde:f:ghlLnNpstT:wW:y', \%opts); + +&display_usage if $opts{'h'}; +&list_cowfiles if $opts{'l'}; + +$borg = $opts{'b'}; +$dead = $opts{'d'}; +$greedy = $opts{'g'}; +$paranoid = $opts{'p'}; +$stoned = $opts{'s'}; +$tired = $opts{'t'}; +$wired = $opts{'w'}; +$young = $opts{'y'}; +$eyes = substr($opts{'e'}, 0, 2); +$tongue = substr($opts{'T'}, 0, 2); +$the_cow = ""; + +&slurp_input; +$Text::Wrap::columns = $opts{'W'}; +@message = ($opts{'n'} ? expand(@message) : + split("\n", fill("", "", @message))); +&construct_balloon; +&construct_face; +&get_cow; +print @balloon_lines; +print $the_cow; + +sub list_cowfiles { + my $basedir; + my @dirfiles; + chop($basedir = cwd); + for my $d (split(/$pathsep/, $cowpath)) { + print "Cow files in $d:\n"; + opendir(COWDIR, $d) || die "$0: Cannot open $d\n"; + for my $file (readdir COWDIR) { + if ($file =~ s/\.cow$//) { + push(@dirfiles, $file); + } + } + closedir(COWDIR); + print wrap("", "", sort @dirfiles), "\n"; + @dirfiles = (); + chdir($basedir); + } + exit(0); +} + +sub slurp_input { + unless ($ARGV[0]) { + chomp(@message = ); + } else { + &display_usage if $opts{'n'}; + @message = join(' ', @ARGV); + } +} + +sub maxlength { + my ($l, $m); + $m = -1; + for my $i (@_) { + $l = length $i; + $m = $l if ($l > $m); + } + return $m; +} + +sub construct_balloon { + my $max = &maxlength(@message); + my $max2 = $max + 2; ## border space fudge. + my $format = "%s %-${max}s %s\n"; + my @border; ## up-left, up-right, down-left, down-right, left, right + if ($0 =~ /think/i) { + $thoughts = 'o'; + @border = qw[ ( ) ( ) ( ) ]; + } elsif (@message < 2) { + $thoughts = '\\'; + @border = qw[ < > ]; + } else { + $thoughts = '\\'; + if ($V and $V gt v5.6.0) { # Thanks, perldelta. + @border = qw[ / \\ \\ / | | ]; + } else { + @border = qw[ / \ \ / | | ]; + } + } + push(@balloon_lines, + " " . ("_" x $max2) . " \n" , + sprintf($format, $border[0], $message[0], $border[1]), + (@message < 2 ? "" : + map { sprintf($format, $border[4], $_, $border[5]) } + @message[1 .. $#message - 1]), + (@message < 2 ? "" : + sprintf($format, $border[2], $message[$#message], $border[3])), + " " . ("-" x $max2) . " \n" + ); +} + +sub construct_face { + if ($borg) { $eyes = "=="; } + if ($dead) { $eyes = "xx"; $tongue = "U "; } + if ($greedy) { $eyes = "\$\$"; } + if ($paranoid) { $eyes = "@@"; } + if ($stoned) { $eyes = "**"; $tongue = "U "; } + if ($tired) { $eyes = "--"; } + if ($wired) { $eyes = "OO"; } + if ($young) { $eyes = ".."; } +} + +sub get_cow { +## +## Get a cow from the specified cowfile; otherwise use the default cow +## which was defined above in $the_cow. +## + my $f = $opts{'f'}; + my $full = ""; + if ($opts{'f'} =~ m,/,) { + $full = $opts{'f'}; + } else { + for my $d (split(/:/, $cowpath)) { + if (-f "$d/$f") { + $full = "$d/$f"; + last; + } elsif (-f "$d/$f.cow") { + $full = "$d/$f.cow"; + last; + } + } + if ($full eq "") { + die "$progname: Could not find $f cowfile!\n"; + } + } + do $full; + die "$progname: $@\n" if $@; +} + +sub display_usage { + die < + +## Try it out + +```bash +docker run -d -e DEFAULT_MESSAGE="whale aren't you just precious?" -p 8080:8080 ghcr.io/getporter/examples/images/whalesayd +``` + +```bash +$ curl http://localhost:8080?msg=time+for+a+nap! + _________________ +< time for a nap! > + ----------------- + \ + \ + \ + ## . + ## ## ## == + ## ## ## ## === + /""""""""""""""""___/ === + ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ + \______ o __/ + \ \ __/ + \____\______/ +``` + +[whalesay]: https://github.com/orgs/getporter/packages/container/package/examples%2Fimages%2Fwhalesay diff --git a/images/whalesayd/go.mod b/images/whalesayd/go.mod new file mode 100644 index 0000000..6de838d --- /dev/null +++ b/images/whalesayd/go.mod @@ -0,0 +1,3 @@ +module whalesayd + +go 1.22.2 diff --git a/images/whalesayd/main.go b/images/whalesayd/main.go new file mode 100644 index 0000000..f87d775 --- /dev/null +++ b/images/whalesayd/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "bytes" + "log" + "net/http" + "os" + "os/exec" +) + +func main() { + http.HandleFunc("/", Say) + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +func Say(w http.ResponseWriter, r *http.Request) { + var msg = "Whale Hello There!" + if defaultMsg, ok := os.LookupEnv("DEFAULT_MESSAGE"); ok { + msg = defaultMsg + } + userMsg := r.FormValue("msg") + if userMsg != "" { + msg = userMsg + } + cowsay := exec.Command("cowsay", msg) + + buf := bytes.Buffer{} + cowsay.Stdout = &buf + err := cowsay.Start() + if err != nil { + w.WriteHeader(500) + w.Write([]byte("Oops, we couldn't get the whale started, sorry!")) + return + } + + err = cowsay.Wait() + if err != nil { + w.WriteHeader(500) + w.Write([]byte("Oops, the whale ran outta steam, sorry!")) + return + } + + _, err = w.Write(buf.Bytes()) + if err != nil { + w.WriteHeader(500) + w.Write([]byte("Oops, the whale is a bit tongue tied, sorry!")) + return + } +}