This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.cljs
84 lines (74 loc) · 2.16 KB
/
user.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(ns re-conf.resources.user
"User managment"
(:refer-clojure :exclude [name])
(:require-macros
[clojure.core.strint :refer (<<)])
(:require
[re-conf.spec.user :refer (user-exists? group-exists?)]
[re-conf.resources.log :refer (info debug error channel?)]
[re-conf.resources.common :refer (run)]
[cljs.core.async :refer [<! go take!]]
[re-conf.resources.shell :refer (sh)]))
(defn append-options
[args opts]
(cond-> args
(not (opts :home)) (into ["--no-create-home"])
(opts :gid) (into ["--gid" (str (opts :gid))])
(opts :system) (into ["--system"])
(opts :uid) (into ["--uid" (str (opts :uid))])
(opts :home) (into ["--home" (<< "/home/~(second args)")])))
(defn addgroup
"Add a group"
[name options]
(go
(let [{:keys [ok]} (<! (group-exists? name))]
(if-not ok
(<! (apply sh (append-options ["/usr/sbin/addgroup" name] options)))
{:ok "group already exists skipping creation"}))))
(defn rmgroup
"Remove a group"
[name & _]
(go
(<! (sh "/usr/sbin/delgroup" name))))
(defn adduser
"Add a user"
[name options]
(let [props "'First Last,RoomNumber,WorkPhone,HomePhone'"]
(go
(let [{:keys [ok]} (<! (user-exists? name))]
(if-not ok
(<! (apply sh (append-options ["/usr/sbin/adduser" name "--gecos" props "--disabled-password"] options)))
{:ok "user already exists skipping creation"})))))
(defn rmuser
"Remove a user"
[name & _]
(go
(<! (sh "/usr/sbin/deluser" "--remove-home" name))))
(def user-states {:present adduser
:absent rmuser})
(defn user
"User resource"
([name]
(user nil name {} :present))
([name state]
(user nil name state {}))
([a b c]
(if (channel? a)
(user a b c {})
((user-states b) a c)))
([c name state options]
(run c user [name state options])))
(def group-states {:present addgroup
:absent rmgroup})
(defn group
"Group resource"
([name]
(group nil name :present {}))
([name state]
(group nil name state {}))
([a b c]
(if (channel? a)
(group a b c {})
((group-states b) a c)))
([c name state options]
(run c group [name state options])))