Skip to content

Commit d79e0bc

Browse files
Merge pull request circleci#3909 from circleci/fix-script-unicode-error
adding gist link for org rename script
2 parents a1bc203 + 5bf186f commit d79e0bc

File tree

1 file changed

+1
-126
lines changed

1 file changed

+1
-126
lines changed

jekyll/_cci2/updating-server.adoc

+1-126
Original file line numberDiff line numberDiff line change
@@ -16,132 +16,7 @@ or you suspect that an **organization rename might have happened at any point**.
1616

1717
. SSH into your Services machine
1818
. REPL into `workflows-conductor` by running the following: `sudo docker exec -it workflows-conductor lein repl :connect 6005`
19-
. Copy/paste this script into the REPL session. It will run migration and output current progress.
20-
+
21-
```
22-
23-
(def domain-client @workflows-conductor.domain.contexts/*domain-connection*)
24-
(def permissions-client @workflows-conductor.domain.contexts/*permissions-connection*)
25-
26-
;; Syncing orgs
27-
(require '[circleci.domain.client.orgs :as domain-orgs])
28-
29-
(defn get-domain-org
30-
"Pass-through fn that actually makes API call to domain-service"
31-
[org-id]
32-
(domain-orgs/org-by-id domain-client org-id))
33-
34-
(defn new-domain-org
35-
"Pass-through fn that actually makes API call to domain-service"
36-
[org-data]
37-
(domain-orgs/new-org domain-client org-data))
38-
39-
(defn sync-org
40-
[org]
41-
(when org
42-
(let [domain-org (try
43-
(get-domain-org (:analytics-id org))
44-
(catch Exception e
45-
(println "ERROR: Exception while fetching org from domain-service" (:analytics-id org))
46-
(throw e)))
47-
mongo-ids {:id (:analytics-id org)
48-
:external-id (str (or (:github-id org) (:bitbucket-id org)))}
49-
domain-ids (select-keys domain-org [:id :external-id])]
50-
(when-not (= mongo-ids domain-ids)
51-
(let [new-domain-org (try
52-
(new-domain-org {:id (:analytics-id org)
53-
:name (:name org)
54-
:provider-id (:id (circle.services.domain/provider-by-type (:vcs-type org)))
55-
:external-id (some-> (or (:github-id org) (:bitbucket-id org))
56-
str)})
57-
(catch Exception e
58-
(println "ERROR: Exception while creating an org" (:analytics-id org))
59-
(throw e)))
60-
new-domain-ids (select-keys new-domain-org [:id :external-id])]
61-
(when-not (= mongo-ids new-domain-ids)
62-
(println "ERROR: Orgs doesn't match after sync.\nMongo org:" mongo-ids "\ndomain-service org:" domain-ids)))))))
63-
64-
(defn sync-all-orgs
65-
[last-id]
66-
(loop [prev-id last-id]
67-
(let [org (first (circle.model.organization/find-active :where (when prev-id {:_id {:$gt prev-id}})
68-
:only [:github-id :bitbucket-id :analytics-id :name :vcs-type]
69-
:limit 1
70-
:sort {:_id 1}))]
71-
(when org
72-
(println "Syncing org" (select-keys org [:_id :name]))
73-
(try
74-
(sync-org org)
75-
(catch Exception e
76-
(println e)))
77-
(recur (:_id org))))))
78-
79-
;; Syncing contexts
80-
(require '[circleci.permissions.client.actors :as actors])
81-
(require '[circleci.permissions.client.permissions :as permissions])
82-
(require '[circleci.permissions.shared.permissions :as permissions-names])
83-
84-
(defn context-to-permission [{:contexts-service-client.context.response/keys [id name organization-ref] :as context}]
85-
(println (format "\tCreating permissions for context '%s' (%s)" name id))
86-
(try
87-
(if-not (circleci.domain.client.contexts/context-by-id domain-client id)
88-
(println (format "ERROR: context '%s' (%s) doesn't exist in domain-service") name id)
89-
(when (empty? (actors/actors-allowed-to-run-contexts permissions-client [id]))
90-
(permissions/new-permission permissions-client organization-ref permissions-names/run-context id)
91-
(permissions/new-permission permissions-client organization-ref permissions-names/execute-context id)
92-
(permissions/new-permission permissions-client organization-ref permissions-names/view-context id)
93-
(permissions/new-permission permissions-client organization-ref permissions-names/edit-context-vars id)))
94-
95-
(catch Exception e
96-
(println (format "\tERROR: Unexpected error while creating permissions: '%s'" (.getMessage e))))))
97-
98-
(defn context-to-domain
99-
[{:contexts-service-client.context.response/keys [id name organization-ref] :as context}]
100-
(try
101-
(println (format "\tMigrating context '%s' (%s)" name id))
102-
(circleci.domain.client.contexts/new-context domain-client id name organization-ref nil)
103-
104-
(catch clojure.lang.ExceptionInfo e
105-
(let [{:keys [status body]} (ex-data e)
106-
{:keys [errors]} (cheshire.core/parse-string body true)]
107-
(cond
108-
;; context already migrated
109-
(and (= status 400)
110-
(= 1 (count errors))
111-
(= "key_duplication" (some-> errors first :type)))
112-
nil
113-
114-
;; org not found
115-
(and (= status 400)
116-
(= 1 (count errors))
117-
(= "bad_argument" (some-> errors first :type))
118-
(= "org_id" (some-> errors first :value)))
119-
(println "\tERROR: domain-service doesn't know org with id" organization-ref)
120-
121-
:else
122-
(println "\tERROR: Failed to create a context in domain-service:" (ex-data e)))))
123-
124-
(catch Exception e
125-
(println (format "\tERROR: Unexpected error while creating context in domain-service: '%s'" (.getMessage e))))))
126-
127-
(defn migrate-contexts
128-
[]
129-
(doseq [org (circle.model.organization/find-active :where {} :only [:analytics-id :name])]
130-
(println (format "Migrating contexts for org '%s' (%s)" (:name org) (:analytics-id org)))
131-
(doseq [context (contexts-service-client.core/list-contexts @workflows-conductor.domain.contexts/*contexts-connection* (:analytics-id org))]
132-
(context-to-domain context)
133-
(context-to-permission context))))
134-
135-
(defn migrate
136-
[]
137-
(println "===== Syncing orgs =====\n")
138-
(sync-all-orgs nil)
139-
(println "\n==== Syncing contexts =====\n")
140-
(migrate-contexts))
141-
142-
(migrate)
143-
```
144-
19+
. Go to this link for the https://gist.githubusercontent.com/BoVice/49a5a98e93508e7913b7d62d6e5de68b/raw/e354eb42a97ca509809eaafe7b28052481702b9e/org-rename.cjl[org rename script]. Copy/paste this script into the REPL session. It will run migration and output current progress.
14520
. If any `ERROR` messages are present in the output please report back to your CSM or reach out to support.
14621

14722
== Upgrade Steps Overview

0 commit comments

Comments
 (0)