more web usage simplification and dashboard in place

This commit is contained in:
Jaromil 2018-10-13 23:30:00 +02:00
parent de76c4887d
commit 81e0954a71
7 changed files with 83 additions and 86 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -71,20 +71,19 @@
"text/html; charset=utf-8"}
:body (page/html5
(render-head)
[:body
;; navbar-guest
body
(render-footer)])})
;; navbar-guest
body
(render-footer))})
([account body]
{:headers {"Content-Type"
"text/html; charset=utf-8"}
:body (page/html5
(render-head)
[:body ;; (if (empty? account)
;; navbar-guest
;; navbar-account)
body
(render-footer)])}))
;; (if (empty? account)
;; navbar-guest
;; navbar-account)
body
(render-footer))}))
(defn notify
"render a notification message without ending the page"

View File

@ -37,7 +37,7 @@
[toaster.session :as s]
[toaster.config :as conf]
[toaster.bulma :as web]
[toaster.bulma :as web :refer [render notify login-form]]
[toaster.ring :as ring]
[toaster.views :as views]
[toaster.jobs :as job])
@ -46,18 +46,34 @@
(defonce config (conf/load-config "toaster" conf/default-settings))
(defn auth-wrap [request fun]
(f/attempt-all
[db (s/check-database)
config (s/check-config request)
account (if (conf/q config [:webserver :mock-auth])
{:email "mock@dyne.org"
:name "MockUser"
:activated true}
;; else
(s/check-account request))]
(fun request config account)
(f/when-failed [e]
(web/render [:body
(web/notify (f/message e) "is-error")
web/login-form]))))
(defn- login-page [request form]
(f/attempt-all
[acct (s/check-account request)]
(web/render acct
[:div {:class "container"}
[:body
[:h1 {:class "title"}
(str "Already logged in with account: "
(:email acct))]
[:h2 {:class "subtitle"}
[:a {:href "/logout"} "Logout"]]])
(f/when-failed [e]
(web/render form))))
(web/render [:body form]))))
(defroutes
app-routes
@ -67,49 +83,57 @@
[db (s/check-database)
conf (s/check-config request)]
(f/if-let-ok? [account (s/check-account request)]
(web/render (views/dashboard account))
(web/render [:body (views/dashboard account)])
;; else
(web/render web/login-form))
(web/render [:body web/login-form]))
(f/when-failed[e]
(web/render (web/notify (f/message e) "is-error")))))
(web/render [:body (web/notify (f/message e) "is-error")]))))
;; NEW ROUTES HERE
(POST "/dockerfile" request
(->> (fn [req conf acct]
(web/render acct
[:span (views/dockerfile-upload-post req conf acct)
(web/render acct
[:body
(views/dockerfile-upload-post req conf acct)
(views/dashboard acct)]))
(s/auth-wrap request)))
(auth-wrap request)))
;; (GET "/list" request
;; (->> (fn [req conf acct]
;; (web/render acct (views/list-jobs acct)))
;; (s/auth-wrap request)))
;; (auth-wrap request)))
(POST "/remove" request
(->> (fn [req conf acct]
(web/render acct (views/remove-job req conf acct)))
(s/auth-wrap request)))
(web/render acct [:body
(views/remove-job req conf acct)
(views/dashboard acct)]))
(auth-wrap request)))
(POST "/start" request
(->> (fn [req conf acct]
(web/render acct (views/start-job req conf acct)))
(s/auth-wrap request)))
(web/render acct [:body
(views/start-job req conf acct)
(views/dashboard acct)]))
(auth-wrap request)))
(POST "/view" request
(->> (fn [req conf acct]
(web/render acct (views/view-job req conf acct)))
(s/auth-wrap request)))
(web/render acct [:body
(views/view-job req conf acct)
(views/dashboard acct)]))
(auth-wrap request)))
(GET "/error" request
(->> (fn [req conf acct]
(web/render acct [:div
(web/notify "Generic Error Page" "is-error")]))
(s/auth-wrap request)))
(web/render acct [:body
(web/notify "Generic Error Page" "is-error")
(views/dashboard acct)]))
(auth-wrap request)))
;; JUST-AUTH ROUTES
(GET "/login" request (login-page request web/login-form))
(POST "/login" request
(f/attempt-all
[username (s/param request :username)
@ -120,19 +144,19 @@
(let [session {:session {:config config
:auth logged}}]
(conj session
(web/render logged (views/dashboard logged))))
(web/render logged [:body (views/dashboard logged)])))
;; (web/render
;; logged
;; [:div
;; [:h1 "Logged in: " username]
;; views/welcome-menu])))
(f/when-failed [e]
(web/render (web/notify
(str "Login failed: " (f/message e)) "is-error")))))
(web/render [:body (web/notify
(str "Login failed: " (f/message e)) "is-error")]))))
(GET "/logout" request
(conj {:session {:config config}}
(web/render [:div {:class "container"}
(web/render [:body
[:h1 {:class "title"} "Logged out."]])))
(GET "/signup" request (login-page request web/signup-form))
@ -154,19 +178,21 @@
password
activation
[])]
[:div
[:body
[:h2 (str "Account created: "
name " <" email ">")]
[:h3 "Account pending activation."]]
(web/notify
(str "Failure creating account: "
(f/message signup)) "is-error")))
(web/notify
"Repeat password didnt match" "is-error")))
[:body
(web/notify
(str "Failure creating account: "
(f/message signup)) "is-error")
(login-page request web/signup-form)]))
[:body (web/notify
"Repeat password didnt match" "is-error")]))
(f/when-failed [e]
(web/render
(web/notify
(str "Sign-up failure: " (f/message e)) "is-error")))))
[:body (web/notify
(str "Sign-up failure: " (f/message e)) "is-error")]))))
(GET "/activate/:email/:activation-id"
[email activation-id :as request]
@ -175,7 +201,7 @@
(get-in request [:headers "host"])
"/activate/" email "/" activation-id)]
(web/render
[:div
[:body
(f/if-let-failed?
[act (auth/activate-account
@ring/auth email
@ -195,7 +221,7 @@
(s/param request :message) "is-error")))
(route/resources "/")
(route/not-found (web/render (web/notify "Page Not Found" "is-error")))
(route/not-found (web/render [:body (web/notify "Page Not Found" "is-error")]))
) ;; end of routes

View File

@ -34,7 +34,7 @@
(str (q config [:jenkins :user]) "@" (q config [:jenkins :host])))
(defn- sync_jobs [config arg1 arg2]
(defn sync_jobs [config arg1 arg2]
(with-programs
[ssh]
(try
@ -42,7 +42,8 @@
(ssh-host config) "sync_jobs.py"
arg1 arg2)
(str/split #"\n"))
(catch Exception e (f/fail (str "ERROR in sync_jobs.py - " (.getMessage e)))))))
(catch Exception e (f/fail (str "ERROR in sync_jobs.py "
arg1 " " arg2 " - " (.getMessage e)))))))
(defn- dockerlint [path]
(with-programs [node]
@ -55,7 +56,7 @@
(defn add [path config account]
(with-programs [ssh scp node]
(let [tstamp (tc/to-long (time/now))
jobname (str (:email account) "-vm_amd64-" tstamp)
jobname (str (:email account) "-vm_amd64_ascii-" tstamp)
jobdir (str "/srv/toaster/" jobname)]
(f/attempt-all
[r_lint (dockerlint path)
@ -70,26 +71,12 @@
:account (dissoc account :password :activation-link)
:lint (if (.contains r_lint "is OK") true false)
:timestamp tstamp
:type "vm_amd64"
:type "vm_amd64_ascii"
:dockerfile (slurp path)})]
{:lint r_lint
:job r_job}
(f/when-failed [e]
(f/fail (str "Job add '" jobname "' failure: " (f/message e))))))))
(defn trash [jobid config]
(f/attempt-all [r_sync (sync_jobs config "-d" jobid)]
jobid
(f/when-failed [e]
(web/render-error
(str "Job remove failure: " (f/message e))))))
(defn start [jobid config]
(f/attempt-all [r_sync (sync_jobs config "-r" jobid)]
jobid
(f/when-failed [e]
(web/render-error
(str "Job start failure: " (f/message e))))))

View File

@ -46,20 +46,3 @@
(if-let [db @ring/db]
db
(f/fail "No connection to database.")))
(defn auth-wrap [request fun]
(f/attempt-all
[db (check-database)
config (check-config request)
account (if (conf/q config [:webserver :mock-auth])
{:email "mock@dyne.org"
:name "MockUser"
:activated true}
;; else
(check-account request))]
(fun request config account)
(f/when-failed [e]
(web/render [:span
(web/notify (f/message e) "is-error")
web/login-form
]))))

View File

@ -22,7 +22,7 @@
[clojure.string :as str]
[clojure.contrib.humanize :as humanize :refer [datetime]]
;; [clojure.data.json :as json :refer [read-str]]
[toaster.bulma :as web]
[toaster.bulma :as web :refer [button notify render-yaml]]
[toaster.session :as s]
[toaster.ring :as ring]
[toaster.jobs :as job]
@ -38,7 +38,7 @@
[hiccup.form :as hf]))
(defn- box-list [account joblist]
[:div {:class "box column"}
[:div {:class "box"}
[:h1 {:class "title"} (str "List all toaster jobs for " (:name account))]
[:table {:class "table is-fullwidth is-hoverable"}
[:thead nil
@ -62,7 +62,7 @@
))]]])
(defn- box-add []
[:div {:class "box column"}
[:div {:class "box"}
[:h1 {:class "title"} "Upload a Dockerfile to toast"]
[:p " Choose the file in your computer and click 'Submit' to
proceed to validation."]
@ -117,8 +117,10 @@
[joblist (db/query @ring/jobs {:email (:email account)})]
[:div {:class "container has-text-centered"}
[:div {:class "columns"}
(if (> 0 (count joblist)) (box-list account joblist))
[:span
;(if (> 0 (count joblist))
(box-list account joblist)
;)
(box-add) ]]
(f/when-failed [e]
(web/notify
@ -129,7 +131,7 @@
[jobid (s/param request :jobid)
jobfound (db/query @ring/jobs {:jobid jobid})
r_rmjob (db/delete! @ring/jobs jobid)
r_sync (job/trash jobid config)]
r_sync (job/sync_jobs config "-d" jobid)]
(web/notify (str "Job removed: " jobid) "is-primary")
(f/when-failed [e]
(web/notify (str "Failure removing job: " (f/message e)) "is-error"))))
@ -138,7 +140,7 @@
(f/attempt-all
[jobid (s/param request :jobid)
jobfound (db/query @ring/jobs {:jobid jobid})
r_sync (job/start jobid config)]
r_sync (job/sync_jobs config "-r" jobid)]
(web/notify (str "Job started: " jobid) "is-success")
(f/when-failed [e]
(web/notify (str "Failure starting job: " (f/message e)) "is-error"))))
@ -148,7 +150,7 @@
[jobid (s/param request :jobid)
jobfound (db/fetch @ring/jobs jobid)
dockerfile (-> jobfound :dockerfile)]
[:div {:class "container"}
[:div {:class "box"}
[:h1 {:class "title"} (str "Viewing job: " jobid)]
[:pre dockerfile]]
(f/when-failed [e]