-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
setup.clj
263 lines (238 loc) · 11.8 KB
/
setup.clj
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
(ns metabase.api.setup
(:require [compojure.core :refer [GET POST]]
[metabase.api.common :as api]
[metabase.api.database :as database-api :refer [DBEngineString]]
[metabase.driver :as driver]
[metabase.email :as email]
[metabase.events :as events]
[metabase.integrations.slack :as slack]
[metabase.models.card :refer [Card]]
[metabase.models.collection :refer [Collection]]
[metabase.models.dashboard :refer [Dashboard]]
[metabase.models.database :refer [Database]]
[metabase.models.metric :refer [Metric]]
[metabase.models.pulse :refer [Pulse]]
[metabase.models.segment :refer [Segment]]
[metabase.models.session :refer [Session]]
[metabase.models.setting.cache :as setting.cache]
[metabase.models.table :refer [Table]]
[metabase.models.user :as user :refer [User]]
[metabase.public-settings :as public-settings]
[metabase.server.middleware.session :as mw.session]
[metabase.setup :as setup]
[metabase.sync.schedules :as sync.schedules]
[metabase.util :as u]
[metabase.util.i18n :as i18n :refer [tru]]
[metabase.util.schema :as su]
[schema.core :as s]
[toucan.db :as db]
[toucan.models :as t.models])
(:import java.util.UUID))
(def ^:private SetupToken
"Schema for a string that matches the instance setup token."
(su/with-api-error-message (s/constrained su/NonBlankString setup/token-match?)
"Token does not match the setup token."))
(defn- setup-create-user! [{:keys [email first-name last-name password]}]
(let [session-id (str (UUID/randomUUID))
new-user (db/insert! User
:email email
:first_name first-name
:last_name last-name
:password (str (UUID/randomUUID))
:is_superuser true)
user-id (u/the-id new-user)]
;; this results in a second db call, but it avoids redundant password code so figure it's worth it
(user/set-password! user-id password)
;; then we create a session right away because we want our new user logged in to continue the setup process
(let [session (or (db/insert! Session
:id session-id
:user_id user-id)
;; HACK -- Toucan doesn't seem to work correctly with models with string IDs
(t.models/post-insert (Session (str session-id))))]
;; return user ID, session ID, and the Session object itself
{:session-id session-id, :user-id user-id, :session session})))
(defn- setup-create-database!
"Create a new Database. Returns newly created Database."
[{:keys [name driver details schedules database]}]
(when driver
(when-not (some-> (u/ignore-exceptions (driver/the-driver driver)) driver/available?)
(let [msg (tru "Cannot create Database: cannot find driver {0}." driver)]
(throw (ex-info msg {:errors {:database {:engine msg}}, :status-code 400}))))
(db/insert! Database
(merge
{:name name, :engine driver, :details details}
(u/select-non-nil-keys database #{:is_on_demand :is_full_sync :auto_run_queries})
(when schedules
(sync.schedules/schedule-map->cron-strings schedules))))))
(defn- setup-set-settings! [request {:keys [email site-name site-locale allow-tracking?]}]
;; set a couple preferences
(public-settings/site-name site-name)
(public-settings/admin-email email)
(when site-locale
(public-settings/site-locale site-locale))
;; default to `true` if allow_tracking isn't specified. The setting will set itself correctly whether a boolean or
;; boolean string is specified
(public-settings/anon-tracking-enabled (or (nil? allow-tracking?)
allow-tracking?))
;; set `source-address-header` to "X-Forwarded-For" if such a header is present
(when (get-in request [:headers "x-forwarded-for"])
(public-settings/source-address-header "X-Forwarded-For")))
(api/defendpoint POST "/"
"Special endpoint for creating the first user during setup. This endpoint both creates the user AND logs them in and
returns a session ID."
[:as {{:keys [token]
{:keys [name engine details is_full_sync
is_on_demand schedules
auto_run_queries]
:as database} :database
{:keys [first_name last_name email password]} :user
{:keys [allow_tracking site_name site_locale]} :prefs} :body, :as request}]
{token SetupToken
site_name su/NonBlankString
site_locale (s/maybe su/ValidLocale)
first_name su/NonBlankString
last_name su/NonBlankString
email su/Email
password su/ComplexPassword
allow_tracking (s/maybe (s/cond-pre s/Bool su/BooleanString))
schedules (s/maybe sync.schedules/ExpandedSchedulesMap)
auto_run_queries (s/maybe s/Bool)}
(letfn [(create! []
(try
(db/transaction
(let [user-info (setup-create-user!
{:email email, :first-name first_name, :last-name last_name, :password password})
db (setup-create-database!
{:name name, :driver engine, :details details, :schedules schedules, :database database})]
(setup-set-settings!
request
{:email email, :site-name site_name, :site-locale site_locale, :allow-tracking? allow_tracking})
;; clear the setup token now, it's no longer needed
(setup/clear-token!)
(assoc user-info :database db)))
(catch Throwable e
;; if the transaction fails, restore the Settings cache from the DB again so any changes made in this
;; endpoint (such as clearing the setup token) are reverted. We can't use `dosync` here to accomplish
;; this because there is `io!` in this block
(setting.cache/restore-cache!)
(throw e))))]
(let [{:keys [user-id session-id database session]} (create!)]
(events/publish-event! :database-create database)
(events/publish-event! :user-login {:user_id user-id, :session_id session-id, :first_login true})
;; return response with session ID and set the cookie as well
(mw.session/set-session-cookie request {:id session-id} session))))
(api/defendpoint POST "/validate"
"Validate that we can connect to a database given a set of details."
[:as {{{:keys [engine details]} :details, token :token} :body}]
{token SetupToken
engine DBEngineString}
(let [engine (keyword engine)
invalid-response (fn [field m] {:status 400, :body (if (#{:dbname :port :host} field)
{:errors {field m}}
{:message m})})]
(database-api/test-database-connection engine details :invalid-response-handler invalid-response)))
;;; Admin Checklist
(defmulti ^:private admin-checklist-entry
{:arglists '([entry-name])}
identity)
(defmethod admin-checklist-entry :add-a-database
[_]
{:title (tru "Add a database")
:group (tru "Get connected")
:description (tru "Connect to your data so your whole team can start to explore.")
:link "/admin/databases/create"
:completed (db/exists? Database, :is_sample false)
:triggered :always})
(defmethod admin-checklist-entry :set-up-email
[_]
{:title (tru "Set up email")
:group (tru "Get connected")
:description (tru "Add email credentials so you can more easily invite team members and get updates via Pulses.")
:link "/admin/settings/email"
:completed (email/email-configured?)
:triggered :always})
(defmethod admin-checklist-entry :set-slack-credentials
[_]
{:title (tru "Set Slack credentials")
:group (tru "Get connected")
:description (tru "Does your team use Slack? If so, you can send automated updates via pulses and ask questions with MetaBot.")
:link "/admin/settings/slack"
:completed (slack/slack-configured?)
:triggered :always})
(defmethod admin-checklist-entry :invite-team-members
[_]
{:title (tru "Invite team members")
:group (tru "Get connected")
:description (tru "Share answers and data with the rest of your team.")
:link "/admin/people/"
:completed (> (db/count User) 1)
:triggered (or (db/exists? Dashboard)
(db/exists? Pulse)
(>= (db/count Card) 5))})
(defmethod admin-checklist-entry :hide-irrelevant-tables
[_]
{:title (tru "Hide irrelevant tables")
:group (tru "Curate your data")
:description (tru "If your data contains technical or irrelevant info you can hide it.")
:link "/admin/datamodel/database"
:completed (db/exists? Table, :visibility_type [:not= nil])
:triggered (>= (db/count Table) 20)})
(defmethod admin-checklist-entry :organize-questions
[_]
{:title (tru "Organize questions")
:group (tru "Curate your data")
:description (tru "Have a lot of saved questions in {0}? Create collections to help manage them and add context." (tru "Metabase"))
:link "/collection/root"
:completed (db/exists? Collection)
:triggered (>= (db/count Card) 30)})
(defmethod admin-checklist-entry :create-metrics
[_]
{:title (tru "Create metrics")
:group (tru "Curate your data")
:description (tru "Define canonical metrics to make it easier for the rest of your team to get the right answers.")
:link "/admin/datamodel/database"
:completed (db/exists? Metric)
:triggered (>= (db/count Card) 30)})
(defmethod admin-checklist-entry :create-segments
[_]
{:title (tru "Create segments")
:group (tru "Curate your data")
:description (tru "Keep everyone on the same page by creating canonical sets of filters anyone can use while asking questions.")
:link "/admin/datamodel/database"
:completed (db/exists? Segment)
:triggered (>= (db/count Card) 30)})
(defn- admin-checklist-values []
(map
admin-checklist-entry
[:add-a-database :set-up-email :set-slack-credentials :invite-team-members :hide-irrelevant-tables
:organize-questions :create-metrics :create-segments]))
(defn- add-next-step-info
"Add `is_next_step` key to all the `steps` from `admin-checklist`.
The next step is the *first* step where `:triggered` is `true` and `:completed` is `false`."
[steps]
(first
(reduce
(fn [[acc already-found-next-step?] {:keys [triggered completed], :as step}]
(let [is-next-step? (and (not already-found-next-step?)
triggered
(not completed))
step (-> (assoc step :is_next_step (boolean is-next-step?))
(update :triggered boolean))]
[(conj (vec acc) step)
(or is-next-step? already-found-next-step?)]))
[[] false]
steps)))
(defn- partition-steps-into-groups
"Partition the admin checklist steps into a sequence of groups."
[steps]
(for [[{group-name :group}, :as tasks] (partition-by :group steps)]
{:name group-name
:tasks tasks}))
(defn- admin-checklist []
(partition-steps-into-groups (add-next-step-info (admin-checklist-values))))
(api/defendpoint GET "/admin_checklist"
"Return various \"admin checklist\" steps and whether they've been completed. You must be a superuser to see this!"
[]
(api/check-superuser)
(admin-checklist))
(api/define-routes)