-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathic_os_rollout.py
357 lines (321 loc) · 11.4 KB
/
ic_os_rollout.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
IC-OS rollout operators.
"""
import itertools
from typing import Any, Sequence
import dfinity.dre as dre
import dfinity.ic_types as ic_types
import dfinity.prom_api as prom
import yaml
from dfinity.ic_os_rollout import (
DR_DRE_SLACK_ID,
SLACK_CHANNEL,
SLACK_CONNECTION_ID,
RolloutPlanWithRevision,
SubnetIdWithRevision,
assign_default_revision,
check_plan,
rollout_planner,
subnet_id_and_git_revision_from_args,
)
import airflow.providers.slack.operators.slack as slack
from airflow.decorators import task
from airflow.exceptions import AirflowException
from airflow.hooks.subprocess import SubprocessHook
from airflow.models.baseoperator import BaseOperator
from airflow.template.templater import Templater
from airflow.utils.context import Context
class RolloutParams(Templater):
template_fields: Sequence[str] = ("subnet_id", "git_revision")
subnet_id: str | SubnetIdWithRevision
git_revision: str
network: ic_types.ICNetwork
def __init__(
self,
*,
subnet_id: str | SubnetIdWithRevision,
git_revision: str,
network: ic_types.ICNetwork,
) -> None:
self.subnet_id = subnet_id
self.git_revision = git_revision
self.network = network
class ICRolloutBaseOperator(RolloutParams, BaseOperator):
def __init__(
self,
*,
task_id: str,
subnet_id: str | SubnetIdWithRevision,
git_revision: str,
network: ic_types.ICNetwork,
**kwargs: Any,
):
RolloutParams.__init__(
self,
subnet_id=subnet_id,
git_revision=git_revision,
network=network,
)
BaseOperator.__init__(self, task_id=task_id, **kwargs)
class CreateProposalIdempotently(ICRolloutBaseOperator):
template_fields = tuple(
itertools.chain.from_iterable(
(ICRolloutBaseOperator.template_fields, ("simulate_proposal",))
)
)
simulate_proposal: bool
def __init__(
self,
*,
task_id: str,
subnet_id: str | SubnetIdWithRevision,
git_revision: str,
simulate_proposal: bool,
network: ic_types.ICNetwork,
**kwargs: Any,
):
ICRolloutBaseOperator.__init__(
self,
task_id=task_id,
subnet_id=subnet_id,
git_revision=git_revision,
network=network,
**kwargs,
)
self.simulate_proposal = simulate_proposal
def execute(self, context: Context) -> dict[str, int | str | bool]:
subnet_id, git_revision = subnet_id_and_git_revision_from_args(
self.subnet_id, self.git_revision
)
runner = dre.DRE(network=self.network, subprocess_hook=SubprocessHook())
# Get proposals sorted by proposal number.
props = sorted(
runner.get_ic_os_version_deployment_proposals_for_subnet(
subnet_id=subnet_id,
),
key=lambda prop: -prop["proposal_id"],
)
props_for_git_revision = [
p for p in props if p["payload"]["replica_version_id"] == git_revision
]
if self.simulate_proposal:
self.log.info(f"simulate_proposal={self.simulate_proposal}")
try:
res = int(
prom.query_prometheus_servers(
self.network.prometheus_urls,
"sum(ic_replica_info{"
f'ic_subnet="{subnet_id}"'
"}) by (ic_subnet)",
)[0]["value"]
)
self.log.info("Remembering current replica count (%s)...", res)
self.xcom_push(
context=context,
key="replica_count",
value=res,
)
except IndexError:
raise RuntimeError(f"No replicas have been found with subnet {subnet_id}")
if not props:
self.log.info("No proposals for subnet. Will create.")
elif not props_for_git_revision:
self.log.info(
"No proposals with revision %s for subnet. Will create.", git_revision
)
elif props_for_git_revision[0]["proposal_id"] < props[0]["proposal_id"]:
self.log.info(
"Proposal %s with git revision %s for subnet "
"is not the last (%s). Will create.",
props_for_git_revision[0]["proposal_id"],
git_revision,
props[0]["proposal_id"],
)
elif props_for_git_revision[0]["status"] not in (
ic_types.ProposalStatus.PROPOSAL_STATUS_OPEN,
ic_types.ProposalStatus.PROPOSAL_STATUS_ADOPTED,
ic_types.ProposalStatus.PROPOSAL_STATUS_EXECUTED,
):
self.log.info(
"Proposal %s with git revision %s for subnet "
"is in state %s and must be created again. Will create.",
props_for_git_revision[0]["proposal_id"],
git_revision,
props_for_git_revision[0]["status"].name,
)
else:
prop = props_for_git_revision[0]
self.log.info(
"Proposal %s with git revision %s for subnet "
"is in state %s and does not need to be created.",
prop["proposal_id"],
git_revision,
prop["status"].name,
)
url = f"{self.network.proposal_display_url}/{prop['proposal_id']}"
self.log.info(
"Proposal " + url + f" titled {prop['title']}"
f" has executed. No need to do anything."
)
return {
"proposal_id": int(prop["proposal_id"]),
"proposal_url": url,
"needs_vote": prop["status"]
== ic_types.ProposalStatus.PROPOSAL_STATUS_OPEN,
}
self.log.info(
f"Creating proposal for subnet ID {subnet_id} to "
+ f"adopt revision {git_revision} (simulate {self.simulate_proposal})."
)
proposal_number = (
runner.authenticated().propose_to_update_subnet_replica_version(
subnet_id, git_revision, dry_run=self.simulate_proposal
)
)
url = f"{self.network.proposal_display_url}/{proposal_number}"
return {
"proposal_id": proposal_number,
"proposal_url": url,
"needs_vote": True,
}
class RequestProposalVote(slack.SlackAPIPostOperator):
def __init__(
self,
source_task_id: str,
_ignored: Any = None,
**kwargs: Any,
) -> None:
self.source_task_id = source_task_id
dr_dre_slack_id = DR_DRE_SLACK_ID
text = (
"""Proposal <{{
task_instance.xcom_pull(
task_ids='%(source_task_id)s',
map_indexes=task_instance.map_index,
).proposal_url
}}|{{
task_instance.xcom_pull(
task_ids='%(source_task_id)s',
map_indexes=task_instance.map_index,
).proposal_id
}}> is now up for voting. <!subteam^%(dr_dre_slack_id)s>"""
""" please vote for the proposal using your HSM."""
) % locals()
slack.SlackAPIPostOperator.__init__(
self,
channel=SLACK_CHANNEL,
username="Airflow",
text=text,
slack_conn_id=SLACK_CONNECTION_ID,
**kwargs,
)
def execute(self, context: Context) -> None:
proposal_creation_result = context["task_instance"].xcom_pull(
task_ids=self.source_task_id,
map_indexes=context["task_instance"].map_index,
)
if proposal_creation_result["proposal_id"] == dre.FAKE_PROPOSAL_NUMBER:
self.log.info("Fake proposal. Not requesting vote.")
elif not proposal_creation_result["needs_vote"]:
self.log.info("Proposal does not need vote. Not requesting vote.")
else:
self.log.info("Requesting vote on proposal with text: %s", self.text)
slack.SlackAPIPostOperator.execute(self, context=context)
class NotifyAboutStalledSubnet(slack.SlackAPIPostOperator):
def __init__(
self,
subnet_id: str,
_ignored: Any = None,
**kwargs: Any,
) -> None:
dr_dre_slack_id = DR_DRE_SLACK_ID
text = (
f"""Subnet `{subnet_id}` has not finished upgrading in over an hour."""
f""" <!subteam^{dr_dre_slack_id}>"""
""" please investigate *as soon as possible*."""
)
slack.SlackAPIPostOperator.__init__(
self,
channel=SLACK_CHANNEL,
username="Airflow",
text=text,
slack_conn_id=SLACK_CONNECTION_ID,
**kwargs,
)
class UpgradeUnassignedNodes(BaseOperator):
template_fields = ("simulate",)
network: ic_types.ICNetwork
simulate: bool
def __init__(
self,
*,
task_id: str,
network: ic_types.ICNetwork,
simulate: bool,
**kwargs: Any,
):
self.simulate = simulate
self.network = network
BaseOperator.__init__(self, task_id=task_id, **kwargs)
def execute(self, context: Context) -> None:
if self.simulate:
self.log.info(f"simulate={self.simulate}")
p = (
dre.DRE(network=self.network, subprocess_hook=SubprocessHook())
.authenticated()
.upgrade_unassigned_nodes(dry_run=self.simulate)
)
if p.exit_code != 0:
raise AirflowException("dre exited with status code %d", p.exit_code)
@task
def schedule(
network: ic_types.ICNetwork, **context: dict[str, Any]
) -> RolloutPlanWithRevision:
plan_data_structure = yaml.safe_load(
context["task"].render_template( # type: ignore
"{{ params.plan }}",
context,
)
)
default_git_revision = "{:040}".format(
context["task"].render_template( # type: ignore
"{{ params.git_revision }}",
context,
)
)
subnet_list_source = dre.DRE(
network=network,
subprocess_hook=SubprocessHook(),
).get_subnet_list
plan = assign_default_revision(
rollout_planner(
plan_data_structure,
subnet_list_source=subnet_list_source,
),
default_git_revision,
)
for nstr, (_, members) in plan.items():
print(f"Batch {int(nstr)+1}:")
for item in members:
print(
f" Subnet {item.subnet_id} ({item.subnet_num}) will start"
f" to be rolled out at {item.start_at} to git"
f" revision {item.git_revision}."
)
try:
check_plan(plan)
except Exception as e:
print("Cannot proceed with rollout plan as planned: %s" % e)
raise AirflowException("Unsafe rollout plan")
return plan
if __name__ == "__main__":
network = ic_types.ICNetwork(
"https://ic0.app/",
"https://dashboard.internetcomputer.org/proposal",
"https://dashboard.internetcomputer.org/release",
["https://victoria.mainnet.dfinity.network/select/0/prometheus/api/v1/query"],
80,
"dfinity.ic_admin.mainnet.proposer_key_file",
)
x = UpgradeUnassignedNodes(task_id="upgrade", simulate=True, network=network)
x.execute({})