|
56 | 56 | }; |
57 | 57 | configFile = format.generate "syncstorage.toml" (lib.recursiveUpdate settings cfg.settings); |
58 | 58 |
|
59 | | - mysqlSetupScript = pkgs.writeShellScript "firefox-syncserver-setup" '' |
60 | | - set -euo pipefail |
61 | | - shopt -s inherit_errexit |
62 | | -
|
63 | | - schema_configured() { |
64 | | - mysql ${cfg.database.name} -Ne 'SHOW TABLES' | grep -q services |
65 | | - } |
66 | | -
|
67 | | - update_config() { |
68 | | - mysql ${cfg.database.name} <<"EOF" |
69 | | - BEGIN; |
70 | | -
|
71 | | - INSERT INTO `services` (`id`, `service`, `pattern`) |
72 | | - VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
73 | | - ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}'; |
74 | | - INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, |
75 | | - `capacity`, `downed`, `backoff`) |
76 | | - VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
77 | | - 0, ${toString cfg.singleNode.capacity}, 0, 0) |
78 | | - ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity}; |
79 | | -
|
80 | | - COMMIT; |
81 | | - EOF |
82 | | - } |
83 | | -
|
84 | | -
|
85 | | - for (( try = 0; try < 60; try++ )); do |
86 | | - if ! schema_configured; then |
87 | | - sleep 2 |
88 | | - else |
89 | | - update_config |
90 | | - exit 0 |
91 | | - fi |
92 | | - done |
93 | | -
|
94 | | - echo "Single-node setup failed" |
95 | | - exit 1 |
96 | | - ''; |
97 | | - |
98 | | - postgresqlSetupScript = pkgs.writeShellScript "firefox-syncserver-setup" '' |
99 | | - set -euo pipefail |
100 | | - shopt -s inherit_errexit |
101 | | -
|
102 | | - schema_configured() { |
103 | | - psql -d ${cfg.database.name} -tAc "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'services')" | grep -q t |
104 | | - } |
105 | | -
|
106 | | - update_config() { |
107 | | - psql -d ${cfg.database.name} <<'EOF' |
108 | | - BEGIN; |
109 | | -
|
110 | | - INSERT INTO services (id, service, pattern) |
111 | | - VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
112 | | - ON CONFLICT (id) DO UPDATE SET service = 'sync-1.5', pattern = '{node}/1.5/{uid}'; |
113 | | - INSERT INTO nodes (id, service, node, available, current_load, |
114 | | - capacity, downed, backoff) |
115 | | - VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
116 | | - 0, ${toString cfg.singleNode.capacity}, 0, 0) |
117 | | - ON CONFLICT (id) DO UPDATE SET node = '${cfg.singleNode.url}', capacity = ${toString cfg.singleNode.capacity}; |
118 | | -
|
119 | | - COMMIT; |
120 | | - EOF |
121 | | - } |
122 | | -
|
| 59 | + setupScript = |
| 60 | + let |
| 61 | + dbSpecific = |
| 62 | + if dbIsMySQL then |
| 63 | + { |
| 64 | + listTables = "mysql ${cfg.database.name} -Ne 'SHOW TABLES'"; |
| 65 | + execSql = "mysql ${cfg.database.name}"; |
| 66 | + upsertSql = '' |
| 67 | + BEGIN; |
| 68 | +
|
| 69 | + INSERT INTO `services` (`id`, `service`, `pattern`) |
| 70 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 71 | + ON DUPLICATE KEY UPDATE service='sync-1.5', pattern='{node}/1.5/{uid}'; |
| 72 | + INSERT INTO `nodes` (`id`, `service`, `node`, `available`, `current_load`, |
| 73 | + `capacity`, `downed`, `backoff`) |
| 74 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 75 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 76 | + ON DUPLICATE KEY UPDATE node = '${cfg.singleNode.url}', capacity=${toString cfg.singleNode.capacity}; |
| 77 | +
|
| 78 | + COMMIT; |
| 79 | + ''; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + listTables = "psql -d ${cfg.database.name} -tAc \"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'services')\""; |
| 84 | + execSql = "psql -d ${cfg.database.name}"; |
| 85 | + upsertSql = '' |
| 86 | + BEGIN; |
| 87 | +
|
| 88 | + INSERT INTO services (id, service, pattern) |
| 89 | + VALUES (1, 'sync-1.5', '{node}/1.5/{uid}') |
| 90 | + ON CONFLICT (id) DO UPDATE SET service = 'sync-1.5', pattern = '{node}/1.5/{uid}'; |
| 91 | + INSERT INTO nodes (id, service, node, available, current_load, |
| 92 | + capacity, downed, backoff) |
| 93 | + VALUES (1, 1, '${cfg.singleNode.url}', ${toString cfg.singleNode.capacity}, |
| 94 | + 0, ${toString cfg.singleNode.capacity}, 0, 0) |
| 95 | + ON CONFLICT (id) DO UPDATE SET node = '${cfg.singleNode.url}', capacity = ${toString cfg.singleNode.capacity}; |
| 96 | +
|
| 97 | + COMMIT; |
| 98 | + ''; |
| 99 | + }; |
| 100 | + in |
| 101 | + pkgs.writeShellScript "firefox-syncserver-setup" '' |
| 102 | + set -euo pipefail |
| 103 | + shopt -s inherit_errexit |
123 | 104 |
|
124 | | - for (( try = 0; try < 60; try++ )); do |
125 | | - if ! schema_configured; then |
126 | | - sleep 2 |
127 | | - else |
128 | | - update_config |
129 | | - exit 0 |
130 | | - fi |
131 | | - done |
| 105 | + schema_configured() { |
| 106 | + ${dbSpecific.listTables} | grep -q services |
| 107 | + } |
132 | 108 |
|
133 | | - echo "Single-node setup failed" |
134 | | - exit 1 |
135 | | - ''; |
| 109 | + update_config() { |
| 110 | + ${dbSpecific.execSql} <<'EOF' |
| 111 | + ${dbSpecific.upsertSql} |
| 112 | + EOF |
| 113 | + } |
136 | 114 |
|
137 | | - setupScript = if dbIsMySQL then mysqlSetupScript else postgresqlSetupScript; |
| 115 | + for (( try = 0; try < 60; try++ )); do |
| 116 | + if ! schema_configured; then |
| 117 | + sleep 2 |
| 118 | + else |
| 119 | + update_config |
| 120 | + exit 0 |
| 121 | + fi |
| 122 | + done |
| 123 | +
|
| 124 | + echo "Single-node setup failed" |
| 125 | + exit 1 |
| 126 | + ''; |
138 | 127 | in |
139 | 128 |
|
140 | 129 | { |
|
0 commit comments