Merge pull request #17 from giolekva/bazel

Use bazel to build rpuppy, minio, minio-importer and photos-ui apps

#7
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..e505d5c
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1 @@
+build --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64
diff --git a/apps/minio/BUILD b/apps/minio/BUILD
new file mode 100644
index 0000000..1fc1992
--- /dev/null
+++ b/apps/minio/BUILD
@@ -0,0 +1,8 @@
+load("@rules_pkg//:pkg.bzl", "pkg_tar")
+
+pkg_tar(
+	name = "chart",
+	srcs = glob(["chart/**"]),
+	extension = "tar.gz",
+	strip_prefix = "./chart",
+)
diff --git a/apps/minio/Dockerfile b/apps/minio/Dockerfile
deleted file mode 100644
index 1ee9555..0000000
--- a/apps/minio/Dockerfile
+++ /dev/null
@@ -1,17 +0,0 @@
-FROM arm64v8/alpine
-
-RUN apk add --no-cache curl
-
-RUN curl --silent --show-error --fail --location \
-    --header "Accept: application/tar+gzip, application/x-gzip, application/octet-stream" -o /usr/bin/minio \
-    "https://dl.minio.io/server/minio/release/linux-arm64/minio" \
-    && chmod 0755 /usr/bin/minio
-RUN curl --silent --show-error --fail --location \
-    --header "Accept: application/tar+gzip, application/x-gzip, application/octet-stream" -o /usr/bin/docker-entrypoint.sh \
-    "https://raw.githubusercontent.com/minio/minio/master/dockerscripts/docker-entrypoint.sh" \
-    && chmod 0755 /usr/bin/docker-entrypoint.sh
-
-EXPOSE 9000
-ENTRYPOINT ["/usr/bin/docker-entrypoint.sh"]
-VOLUME ["/data"]
-CMD ["minio"]
diff --git a/apps/minio/Dockerfile.mc b/apps/minio/Dockerfile.mc
deleted file mode 100644
index eef9ced..0000000
--- a/apps/minio/Dockerfile.mc
+++ /dev/null
@@ -1,9 +0,0 @@
-FROM arm64v8/alpine
-
-RUN apk add --no-cache curl
-
-RUN curl --silent --show-error --fail --location \
-    --header "Accept: application/tar+gzip, application/x-gzip, application/octet-stream" -o /usr/bin/mc \
-    "https://dl.minio.io/client/mc/release/linux-arm64/mc" \
-    && chmod 0755 /usr/bin/mc
-CMD ["mc"]
diff --git a/apps/minio/chart/templates/pcloud/Actions.yaml b/apps/minio/chart/templates/pcloud/Actions.yaml
index 6873de0..5333deb 100644
--- a/apps/minio/chart/templates/pcloud/Actions.yaml
+++ b/apps/minio/chart/templates/pcloud/Actions.yaml
@@ -10,7 +10,7 @@
     spec:
       containers:
       - name: create-bucket-with-webhook
-        image: giolekva/minio-mc-arm64:latest
+        image: {{ .Values.minio_mc.image.repository }}:{{ .Values.minio_mc.image.tag }}
         imagePullPolicy: Always
         env:
         - name: MINIO_ADDR
diff --git a/apps/minio/chart/values.yaml b/apps/minio/chart/values.yaml
index 79a3fca..b366190 100644
--- a/apps/minio/chart/values.yaml
+++ b/apps/minio/chart/values.yaml
@@ -5,8 +5,13 @@
   existingSecret: minio-creds
   fullnameOverride: minio
   image:
-    repository: giolekva/minio-arm
+    repository: minio/minio
     tag: latest
     pullPolicy: Always
   persistence:
     size: 1Gi
+minio_mc:
+  image:
+    repository: minio/mc
+    tag: latest
+    pullPolicy: Always
diff --git a/apps/minio/configure-webhook.yaml b/apps/minio/configure-webhook.yaml
deleted file mode 100644
index 55f86cc..0000000
--- a/apps/minio/configure-webhook.yaml
+++ /dev/null
@@ -1,39 +0,0 @@
-apiVersion: v1
-kind: Pod
-metadata:
-  name: minio-importer-webhook
-  namespace: minio
-  labels:
-    app: minio-importer-webhook
-spec:
-  volumes:
-  - name: minio-creds
-    secret:
-      secretName: minio-creds
-  initContainers:
-  - name: setup-arn
-    image: minio/mc:RELEASE.2020-04-04T05-28-55Z
-    imagePullPolicy: IfNotPresent
-    volumeMounts:
-    - name: minio-secrets
-      readOnly: true
-      mountPath: "/etc/minio"
-    command: ["/bin/sh", "-c"]
-    args: ["mc config host add pcloud http://minio.minio.svc:9000 $(cat /etc/minio-creds/accesskey) $(cat /etc/minio-creds/secretkey) && mc mb pcloud/images && mc admin config set pcloud notify_webhook:pcloud queue_limit=\"1000\" queue_dir=\"/data/events\" endpoint=\"http://minio-importer.app-minio-importer.svc:80/new_object\" && mc admin service restart pcloud"]
-  containers:
-  - name: setup-events
-    image: minio/mc:RELEASE.2020-04-04T05-28-55Z
-    imagePullPolicy: IfNotPresent
-    volumeMounts:
-    - name: minio-secrets
-      readOnly: true
-      mountPath: "/etc/minio"
-    command: ["/bin/sh", "-c"]
-    args: ["mc config host add pcloud http://minio.minio.svc:9000 $(cat /etc/minio-creds/accesskey) $(cat /etc/minio-creds/secretkey) && mc event add pcloud/images arn:minio:sqs::pcloud:webhook --event put && mc event list pcloud/images"]
-  restartPolicy: Never
-
-
-
-# mc admin config set mio notify_webhook:pcloud-controller queue_limit=\"1000\" queue_dir=\"/data/events\" endpoint=\"http://pcloud-controller-service.pcloud:1111/minio_webhook\"
-# mc event add myminio/images arn:minio:sqs::pcloud-controller:webhook --event put
-# args: ["mc config host add mio http://minio-hl-svc:9000 $(cat /etc/minio-secrets/accesskey) $(cat /etc/minio-secrets/secretkey) && mc mb mio/images && mc admin config set mio notify_webhook:pcloud queue_limit=\"1000\" queue_dir=\"/data/events\" endpoint=\"http://pcloud-controller-service.pcloud:1111/minio_webhook\" && mc event add mio/images arn:minio:sqs::pcloud:webhook --event put && mc event list mio/images"]
diff --git a/apps/minio/importer/BUILD b/apps/minio/importer/BUILD
index c94f6ac..b931f11 100644
--- a/apps/minio/importer/BUILD
+++ b/apps/minio/importer/BUILD
@@ -40,6 +40,5 @@
 	name = "chart",
 	srcs = glob(["chart/**"]),
 	extension = "tar.gz",
-	strip_prefix = "chart",
-	package_dir = "minio-importer",
+	strip_prefix = "./chart",
 )
diff --git a/apps/minio/importer/Dockerfile b/apps/minio/importer/Dockerfile
deleted file mode 100644
index c383d8a..0000000
--- a/apps/minio/importer/Dockerfile
+++ /dev/null
@@ -1,30 +0,0 @@
-FROM golang:1-alpine AS build
-
-RUN apk update && apk upgrade && \
-    apk add --no-cache bash git openssh
-
-ENV GOOS linux
-ENV GOARCH $BUILDPLATFORM
-ENV CGO_ENABLED 0
-ENV GO111MODULE off
-
-RUN go get github.com/golang/glog
-RUN go get github.com/itaysk/regogo
-
-RUN mkdir -p $GOPATH/src/github.com/giolekva/pcloud/apps/minio/importer
-COPY . $GOPATH/src/github.com/giolekva/pcloud/apps/minio/importer
-WORKDIR $GOPATH/src/github.com/giolekva/pcloud/apps/minio/importer/cmd
-
-RUN mkdir -p /app/build
-RUN ls -la
-RUN go build -o /app/build/minio-importer -trimpath -ldflags="-s -w" main.go
-
-FROM alpine:latest
-WORKDIR /
-COPY --from=build /app/build/minio-importer /usr/bin
-RUN chmod a+x /usr/bin/minio-importer
-
-ENV PORT 80
-ENV API_ADDR ""
-EXPOSE ${PORT}
-CMD minio-importer --logtostderr --port=${PORT} --api_addr=${API_ADDR}
diff --git a/apps/minio/importer/chart/templates/deployment.yaml b/apps/minio/importer/chart/templates/deployment.yaml
index 2f1784c..bf25b3b 100644
--- a/apps/minio/importer/chart/templates/deployment.yaml
+++ b/apps/minio/importer/chart/templates/deployment.yaml
@@ -19,5 +19,5 @@
         imagePullPolicy: {{ .Values.image.pullPolicy }}
         ports:
         - containerPort: {{ .Values.containerPort }}
-        command: ["minio-importer"]
+        command: ["/importer"]
         args: ["--logtostderr", "--port={{ .Values.containerPort }}", "--api_addr={{ .Values.pcloudApiAddress }}"]
diff --git a/apps/minio/importer/chart/values.yaml b/apps/minio/importer/chart/values.yaml
index 987d303..7e95975 100644
--- a/apps/minio/importer/chart/values.yaml
+++ b/apps/minio/importer/chart/values.yaml
@@ -1,8 +1,8 @@
 replicas: 1
 image:
-  name: giolekva/minio-importer
+  name: localhost:30500/giolekva/minio-importer
   tag: latest
   pullPolicy: Always
 servicePort: 80
 containerPort: 1234
-pcloudApiAddress: http://api.pcloud.svc:1111/graphql
\ No newline at end of file
+pcloudApiAddress: http://api.pcloud.svc:1111/graphql
diff --git a/apps/minio/setup.sh b/apps/minio/setup.sh
deleted file mode 100644
index b4ee13b..0000000
--- a/apps/minio/setup.sh
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-kubectl create namespace minio
-helm --namespace minio install minio-initial chart/
diff --git a/apps/photos-ui/BUILD b/apps/photos-ui/BUILD
new file mode 100644
index 0000000..fa822db
--- /dev/null
+++ b/apps/photos-ui/BUILD
@@ -0,0 +1,55 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary")
+load("@io_bazel_rules_docker//container:container.bzl", "container_push", "container_image")
+load("@rules_pkg//:pkg.bzl", "pkg_tar")
+
+filegroup(
+    name = "data",
+    srcs = glob([
+	     "static/**",
+	     "*.html",
+	]),
+)
+
+go_binary(
+	name = "photos_ui",
+	srcs = ["main.go"],
+	data = [":data"],
+	deps = [
+	     "@io_bazel_rules_go//go/tools/bazel:bazel",
+	],
+)
+
+container_image(
+    name = "container",
+    base = "@alpine_base//image",
+    files = [
+    	  ":photos_ui",
+	  ":data",
+	  "photos_ui.runfiles_manifest",
+    ],
+    data_path = "/",
+    cmd = ["/photos_ui"],
+    env = {
+    	"RUNFILES_DIR": "/",
+    	"RUNFILES_MANIFEST_FILE": "/photos_ui.runfiles_manifest",
+    },
+    symlinks = {
+    	     "/photos_ui": "/apps/photos-ui/photos_ui_/photos_ui",
+    },
+)
+
+container_push(
+   name = "push_to_dev",
+   image = ":container",
+   format = "Docker",
+   registry = "localhost:30500",
+   repository = "giolekva/photos-ui",
+   tag = "latest",
+)
+
+pkg_tar(
+	name = "chart",
+	srcs = glob(["chart/**"]),
+	extension = "tar.gz",
+	strip_prefix = "./chart",
+)
diff --git a/apps/photos-ui/Dockerfile b/apps/photos-ui/Dockerfile
deleted file mode 100644
index 8e02ed9..0000000
--- a/apps/photos-ui/Dockerfile
+++ /dev/null
@@ -1,30 +0,0 @@
-FROM golang:1-alpine AS build
-
-RUN apk update && apk upgrade && \
-    apk add --no-cache bash git openssh
-
-WORKDIR /app
-COPY *.go .
-
-ENV CGO_ENABLED 0
-ENV GO111MODULE off
-RUN go build -o photos-ui -trimpath -ldflags="-s -w" main.go
-
-FROM alpine:latest
-WORKDIR /app
-COPY --from=build /app/photos-ui .
-COPY *.html .
-WORKDIR /app/static
-COPY static/photos.js .
-WORKDIR /app
-
-RUN chmod a+x /app/photos-ui
-
-ENV PATH="/app:${PATH}"
-
-ENV PORT 80
-EXPOSE ${PORT}
-ENV PCLOUD_API_ADDR ""
-CMD photos-ui --port=${PORT} --pcloud_api_addr=${PCLOUD_API_ADDR}
-
-
diff --git a/apps/photos-ui/chart/templates/install.yaml b/apps/photos-ui/chart/templates/install.yaml
index 1e88645..1978b10 100644
--- a/apps/photos-ui/chart/templates/install.yaml
+++ b/apps/photos-ui/chart/templates/install.yaml
@@ -52,4 +52,4 @@
         imagePullPolicy: {{ .Values.image.pullPolicy }}
         ports:
         - containerPort: {{ .Values.containerPort }}
-        command: ["photos-ui", "--port={{ .Values.containerPort }}", "--pcloud_api_addr={{ .Values.pcloudApiAddr }}"]
+        command: ["/photos_ui", "--port={{ .Values.containerPort }}", "--pcloud_api_addr={{ .Values.pcloudApiAddr }}"]
diff --git a/apps/photos-ui/chart/values.yaml b/apps/photos-ui/chart/values.yaml
index 74618e6..bfb5260 100644
--- a/apps/photos-ui/chart/values.yaml
+++ b/apps/photos-ui/chart/values.yaml
@@ -1,6 +1,6 @@
 replicas: 1
 image:
-  name: giolekva/photos-ui
+  name: localhost:30500/giolekva/photos-ui
   tag: latest
   pullPolicy: Always
 servicePort: 80
diff --git a/apps/photos-ui/gallery.html b/apps/photos-ui/gallery.html
index a8001d0..68e200d 100644
--- a/apps/photos-ui/gallery.html
+++ b/apps/photos-ui/gallery.html
@@ -3,7 +3,7 @@
     <head>
         <title>Photos</title>
     </head>
-    <script src="static/photos.js"></script>
+    <script src="static/photos.js?v4"></script>
     <body onload="initGallery('gallery')">
       <div id="gallery" />
     </body>
diff --git a/apps/photos-ui/main.go b/apps/photos-ui/main.go
index b85036e..e170a73 100644
--- a/apps/photos-ui/main.go
+++ b/apps/photos-ui/main.go
@@ -8,13 +8,19 @@
 	"net/http/httputil"
 	"net/url"
 	"text/template"
+
+	"github.com/bazelbuild/rules_go/go/tools/bazel"
 )
 
 var port = flag.Int("port", 3000, "Port to listen on.")
 var pcloudApiAddr = flag.String("pcloud_api_addr", "", "PCloud API Server address.")
 
 func handle_gallery(w http.ResponseWriter, r *http.Request) {
-	http.ServeFile(w, r, "./gallery.html")
+	gallery_html, err := bazel.Runfile("apps/photos-ui/gallery.html")
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+	}
+	http.ServeFile(w, r, gallery_html)
 }
 
 func handle_photo(w http.ResponseWriter, r *http.Request) {
@@ -28,7 +34,11 @@
 		http.Error(w, "Photo id must be provided", http.StatusBadRequest)
 		return
 	}
-	t, err := template.ParseFiles("photo.html")
+	photo_html, err := bazel.Runfile("apps/photos-ui/photo.html")
+	if err != nil {
+		http.Error(w, err.Error(), http.StatusInternalServerError)
+	}
+	t, err := template.ParseFiles(photo_html)
 	if err != nil {
 		log.Print(err)
 		http.Error(w, "Could not process page", http.StatusInternalServerError)
@@ -52,7 +62,11 @@
 
 func main() {
 	flag.Parse()
-	fs := http.FileServer(http.Dir("./static"))
+	static_dir, err := bazel.Runfile("apps/photos-ui/static")
+	if err != nil {
+		log.Fatal(err)
+	}
+	fs := http.FileServer(http.Dir(static_dir))
 	http.Handle("/static/", http.StripPrefix("/static/", fs))
 	http.Handle("/graphql", newGqlProxy(*pcloudApiAddr))
 	http.HandleFunc("/photo", handle_photo)
diff --git a/apps/photos-ui/photo.html b/apps/photos-ui/photo.html
index d41bb29..d2dd612 100644
--- a/apps/photos-ui/photo.html
+++ b/apps/photos-ui/photo.html
@@ -3,7 +3,7 @@
     <head>
         <title>Photos</title>
     </head>
-    <script src="static/photos.js"></script>
+    <script src="static/photos.js?v4"></script>
     <body onload="initImg('photo', '{{ .Id }}')">
       <img id="photo" onload="drawFaces('photo', 'faces', '{{ .Id }}')" src=""></img>
       <canvas id="faces"></canvas>
diff --git a/apps/photos-ui/photos_ui.runfiles_manifest b/apps/photos-ui/photos_ui.runfiles_manifest
new file mode 100644
index 0000000..b604c19
--- /dev/null
+++ b/apps/photos-ui/photos_ui.runfiles_manifest
@@ -0,0 +1,3 @@
+/apps/photos-ui/gallery.html /gallery.html
+/apps/photos-ui/photo.html /photo.html
+/apps/photos-ui/static/photos.js /static/photos.js
diff --git a/apps/photos-ui/static/photos.js b/apps/photos-ui/static/photos.js
index ed33e0d..d098829 100644
--- a/apps/photos-ui/static/photos.js
+++ b/apps/photos-ui/static/photos.js
@@ -35,7 +35,7 @@
     imgs = await fetchAllPhotos();
     img_list = "<ul>";
     for (img of imgs) {
-	img_list += "<li><a href='/photo?id=" + img.id + "'><img style='max-width: 300px' src='http://minio/" + img.objectPath + "' /></a></li>";
+	img_list += "<li><a href='/photo?id=" + img.id + "'><img style='max-width: 300px' src='http://minio:8080/" + img.objectPath + "' /></a></li>";
     }
     img_list += "</ul>";
     document.getElementById(gallery_elem_id).innerHTML = img_list;
@@ -43,7 +43,7 @@
 
 async function initImg(img_elem_id, id) {
     img = await fetchImage(id);
-    document.getElementById(img_elem_id).setAttribute("src", "http://minio/" + img.objectPath);
+    document.getElementById(img_elem_id).setAttribute("src", "http://minio:8080/" + img.objectPath);
 }
 
 async function drawFaces(photo_elem_id, faces_canvas_elem_id, id){
diff --git a/apps/rpuppy/BUILD b/apps/rpuppy/BUILD
index 8e8ee9d..4d92193 100644
--- a/apps/rpuppy/BUILD
+++ b/apps/rpuppy/BUILD
@@ -23,7 +23,7 @@
    image = ":container",
    format = "Docker",
    registry = "localhost:30500",
-   repository = "giolekva/rpuppy-arm",
+   repository = "giolekva/rpuppy",
    tag = "latest",
 )
 
@@ -31,6 +31,5 @@
 	name = "chart",
 	srcs = glob(["chart/**"]),
 	extension = "tar.gz",
-	strip_prefix = "chart",
-	package_dir = "rpuppy",
+	strip_prefix = "./chart",
 )
diff --git a/apps/rpuppy/chart/templates/install.yaml b/apps/rpuppy/chart/templates/install.yaml
index db5245b..9d65755 100644
--- a/apps/rpuppy/chart/templates/install.yaml
+++ b/apps/rpuppy/chart/templates/install.yaml
@@ -52,4 +52,4 @@
         imagePullPolicy: {{ .Values.image.pullPolicy }}
         ports:
         - containerPort: {{ .Values.containerPort }}
-        command: ["rpuppy", "--port={{ .Values.containerPort }}"]
+        command: ["/rpuppy", "--port={{ .Values.containerPort }}"]
diff --git a/apps/rpuppy/chart/values.yaml b/apps/rpuppy/chart/values.yaml
index 5da7fac..37ddb9e 100644
--- a/apps/rpuppy/chart/values.yaml
+++ b/apps/rpuppy/chart/values.yaml
@@ -1,6 +1,6 @@
 replicas: 1
 image:
-  name: giolekva/rpuppy-arm
+  name: localhost:30500/giolekva/rpuppy
   tag: latest
   pullPolicy: Always
 servicePort: 80