/
database_tasks.py
3121 lines (2426 loc) · 114 KB
/
database_tasks.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
from cryptography import fernet
from octavia_lib.common import constants as lib_consts
from oslo_config import cfg
from oslo_db import exception as odb_exceptions
from oslo_log import log as logging
from oslo_utils import excutils
from oslo_utils import uuidutils
import sqlalchemy
from sqlalchemy.orm import exc
from taskflow import task
from taskflow.types import failure
from octavia.api.drivers import utils as provider_utils
from octavia.common import constants
from octavia.common import data_models
from octavia.common import exceptions
from octavia.common.tls_utils import cert_parser
from octavia.common import utils
from octavia.controller.worker import task_utils as task_utilities
from octavia.db import api as db_apis
from octavia.db import repositories as repo
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
class BaseDatabaseTask(task.Task):
"""Base task to load drivers common to the tasks."""
def __init__(self, **kwargs):
self.repos = repo.Repositories()
self.amphora_repo = repo.AmphoraRepository()
self.health_mon_repo = repo.HealthMonitorRepository()
self.listener_repo = repo.ListenerRepository()
self.loadbalancer_repo = repo.LoadBalancerRepository()
self.vip_repo = repo.VipRepository()
self.member_repo = repo.MemberRepository()
self.pool_repo = repo.PoolRepository()
self.amp_health_repo = repo.AmphoraHealthRepository()
self.l7policy_repo = repo.L7PolicyRepository()
self.l7rule_repo = repo.L7RuleRepository()
self.task_utils = task_utilities.TaskUtils()
super().__init__(**kwargs)
def _delete_from_amp_health(self, session, amphora_id):
"""Delete the amphora_health record for an amphora.
:param amphora_id: The amphora id to delete
"""
LOG.debug('Disabling health monitoring on amphora: %s', amphora_id)
try:
self.amp_health_repo.delete(session,
amphora_id=amphora_id)
except (sqlalchemy.orm.exc.NoResultFound,
sqlalchemy.orm.exc.UnmappedInstanceError):
LOG.debug('No existing amphora health record to delete '
'for amphora: %s, skipping.', amphora_id)
def _mark_amp_health_busy(self, session, amphora_id):
"""Mark the amphora_health record busy for an amphora.
:param amphora_id: The amphora id to mark busy
"""
LOG.debug('Marking health monitoring busy on amphora: %s', amphora_id)
try:
self.amp_health_repo.update(session,
amphora_id=amphora_id,
busy=True)
except (sqlalchemy.orm.exc.NoResultFound,
sqlalchemy.orm.exc.UnmappedInstanceError):
LOG.debug('No existing amphora health record to mark busy '
'for amphora: %s, skipping.', amphora_id)
class CreateAmphoraInDB(BaseDatabaseTask):
"""Task to create an initial amphora in the Database."""
def execute(self, *args, loadbalancer_id=None, **kwargs):
"""Creates an pending create amphora record in the database.
:returns: The created amphora object
"""
with db_apis.session().begin() as session:
amphora = self.amphora_repo.create(
session,
id=uuidutils.generate_uuid(),
load_balancer_id=loadbalancer_id,
status=constants.PENDING_CREATE,
cert_busy=False)
if loadbalancer_id:
LOG.info("Created Amphora %s in DB for load balancer %s",
amphora.id, loadbalancer_id)
else:
LOG.info("Created Amphora %s in DB", amphora.id)
return amphora.id
def revert(self, result, *args, **kwargs):
"""Revert by storing the amphora in error state in the DB
In a future version we might change the status to DELETED
if deleting the amphora was successful
:param result: Id of created amphora.
:returns: None
"""
if isinstance(result, failure.Failure):
# This task's execute failed, so nothing needed to be done to
# revert
return
# At this point the revert is being called because another task
# executed after this failed so we will need to do something and
# result is the amphora's id
LOG.warning("Reverting create amphora in DB for amp id %s ", result)
# Delete the amphora for now. May want to just update status later
try:
with db_apis.session().begin() as session:
self.amphora_repo.delete(session, id=result)
except Exception as e:
LOG.error("Failed to delete amphora %(amp)s "
"in the database due to: "
"%(except)s", {'amp': result, 'except': str(e)})
class MarkLBAmphoraeDeletedInDB(BaseDatabaseTask):
"""Task to mark a list of amphora deleted in the Database."""
def execute(self, loadbalancer):
"""Update load balancer's amphorae statuses to DELETED in the database.
:param loadbalancer: The load balancer which amphorae should be
marked DELETED.
:returns: None
"""
with db_apis.session().begin() as session:
db_lb = self.repos.load_balancer.get(
session, id=loadbalancer[constants.LOADBALANCER_ID])
for amp in db_lb.amphorae:
LOG.debug("Marking amphora %s DELETED ", amp.id)
self.amphora_repo.update(session,
id=amp.id, status=constants.DELETED)
class DeleteHealthMonitorInDB(BaseDatabaseTask):
"""Delete the health monitor in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, health_mon):
"""Delete the health monitor in DB
:param health_mon: The health monitor which should be deleted
:returns: None
"""
LOG.debug("DB delete health monitor: %s ",
health_mon[constants.HEALTHMONITOR_ID])
try:
with db_apis.session().begin() as session:
self.health_mon_repo.delete(
session,
id=health_mon[constants.HEALTHMONITOR_ID])
except exc.NoResultFound:
# ignore if the HealthMonitor was not found
pass
def revert(self, health_mon, *args, **kwargs):
"""Mark the health monitor ERROR since the mark active couldn't happen
:param health_mon: The health monitor which couldn't be deleted
:returns: None
"""
LOG.warning("Reverting mark health monitor delete in DB "
"for health monitor with id %s",
health_mon[constants.HEALTHMONITOR_ID])
with db_apis.session().begin() as session:
self.health_mon_repo.update(
session,
id=health_mon[constants.HEALTHMONITOR_ID],
provisioning_status=constants.ERROR)
class DeleteHealthMonitorInDBByPool(DeleteHealthMonitorInDB):
"""Delete the health monitor in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, pool_id):
"""Delete the health monitor in the DB.
:param pool_id: ID of pool which health monitor should be deleted.
:returns: None
"""
with db_apis.session().begin() as session:
db_pool = self.pool_repo.get(session,
id=pool_id)
provider_hm = provider_utils.db_HM_to_provider_HM(
db_pool.health_monitor).to_dict()
super().execute(
provider_hm)
def revert(self, pool_id, *args, **kwargs):
"""Mark the health monitor ERROR since the mark active couldn't happen
:param pool_id: ID of pool which health monitor couldn't be deleted
:returns: None
"""
with db_apis.session().begin() as session:
db_pool = self.pool_repo.get(session,
id=pool_id)
provider_hm = provider_utils.db_HM_to_provider_HM(
db_pool.health_monitor).to_dict()
super().revert(
provider_hm, *args, **kwargs)
class DeleteMemberInDB(BaseDatabaseTask):
"""Delete the member in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, member):
"""Delete the member in the DB
:param member: The member to be deleted
:returns: None
"""
LOG.debug("DB delete member for id: %s ", member[constants.MEMBER_ID])
with db_apis.session().begin() as session:
self.member_repo.delete(session,
id=member[constants.MEMBER_ID])
def revert(self, member, *args, **kwargs):
"""Mark the member ERROR since the delete couldn't happen
:param member: Member that failed to get deleted
:returns: None
"""
LOG.warning("Reverting delete in DB for member id %s",
member[constants.MEMBER_ID])
try:
with db_apis.session().begin() as session:
self.member_repo.update(session,
member[constants.MEMBER_ID],
provisioning_status=constants.ERROR)
except Exception as e:
LOG.error("Failed to update member %(mem)s "
"provisioning_status to ERROR due to: %(except)s",
{'mem': member[constants.MEMBER_ID], 'except': str(e)})
class DeleteListenerInDB(BaseDatabaseTask):
"""Delete the listener in the DB."""
def execute(self, listener):
"""Delete the listener in DB
:param listener: The listener to delete
:returns: None
"""
LOG.debug("Delete in DB for listener id: %s",
listener[constants.LISTENER_ID])
with db_apis.session().begin() as session:
self.listener_repo.delete(session,
id=listener[constants.LISTENER_ID])
def revert(self, listener, *args, **kwargs):
"""Mark the listener ERROR since the listener didn't delete
:param listener: Listener that failed to get deleted
:returns: None
"""
# TODO(johnsom) Fix this, it doesn't revert anything
LOG.warning("Reverting mark listener delete in DB for listener id %s",
listener[constants.LISTENER_ID])
class DeletePoolInDB(BaseDatabaseTask):
"""Delete the pool in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, pool_id):
"""Delete the pool in DB
:param pool_id: The pool_id to be deleted
:returns: None
"""
LOG.debug("Delete in DB for pool id: %s ", pool_id)
with db_apis.session().begin() as session:
self.pool_repo.delete(session, id=pool_id)
def revert(self, pool_id, *args, **kwargs):
"""Mark the pool ERROR since the delete couldn't happen
:param pool_id: pool_id that failed to get deleted
:returns: None
"""
LOG.warning("Reverting delete in DB for pool id %s", pool_id)
try:
with db_apis.session().begin() as session:
self.pool_repo.update(session, pool_id,
provisioning_status=constants.ERROR)
except Exception as e:
LOG.error("Failed to update pool %(pool)s "
"provisioning_status to ERROR due to: %(except)s",
{'pool': pool_id, 'except': str(e)})
class DeleteL7PolicyInDB(BaseDatabaseTask):
"""Delete the L7 policy in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, l7policy):
"""Delete the l7policy in DB
:param l7policy: The l7policy to be deleted
:returns: None
"""
LOG.debug("Delete in DB for l7policy id: %s ",
l7policy[constants.L7POLICY_ID])
with db_apis.session().begin() as session:
self.l7policy_repo.delete(session,
id=l7policy[constants.L7POLICY_ID])
def revert(self, l7policy, *args, **kwargs):
"""Mark the l7policy ERROR since the delete couldn't happen
:param l7policy: L7 policy that failed to get deleted
:returns: None
"""
LOG.warning("Reverting delete in DB for l7policy id %s",
l7policy[constants.L7POLICY_ID])
try:
with db_apis.session().begin() as session:
self.l7policy_repo.update(session,
l7policy[constants.L7POLICY_ID],
provisioning_status=constants.ERROR)
except Exception as e:
LOG.error("Failed to update l7policy %(l7policy)s "
"provisioning_status to ERROR due to: %(except)s",
{'l7policy': l7policy[constants.L7POLICY_ID],
'except': str(e)})
class DeleteL7RuleInDB(BaseDatabaseTask):
"""Delete the L7 rule in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, l7rule):
"""Delete the l7rule in DB
:param l7rule: The l7rule to be deleted
:returns: None
"""
LOG.debug("Delete in DB for l7rule id: %s",
l7rule[constants.L7RULE_ID])
with db_apis.session().begin() as session:
self.l7rule_repo.delete(session,
id=l7rule[constants.L7RULE_ID])
def revert(self, l7rule, *args, **kwargs):
"""Mark the l7rule ERROR since the delete couldn't happen
:param l7rule: L7 rule that failed to get deleted
:returns: None
"""
LOG.warning("Reverting delete in DB for l7rule id %s",
l7rule[constants.L7RULE_ID])
try:
with db_apis.session().begin() as session:
self.l7rule_repo.update(session,
l7rule[constants.L7RULE_ID],
provisioning_status=constants.ERROR)
except Exception as e:
LOG.error("Failed to update l7rule %(l7rule)s "
"provisioning_status to ERROR due to: %(except)s",
{'l7rule': l7rule[constants.L7RULE_ID],
'except': str(e)})
class ReloadAmphora(BaseDatabaseTask):
"""Get an amphora object from the database."""
def execute(self, amphora):
"""Get an amphora object from the database.
:param amphora_id: The amphora ID to lookup
:returns: The amphora object
"""
LOG.debug("Get amphora from DB for amphora id: %s ",
amphora[constants.ID])
with db_apis.session().begin() as session:
return self.amphora_repo.get(
session, id=amphora[constants.ID]).to_dict()
class ReloadLoadBalancer(BaseDatabaseTask):
"""Get an load balancer object from the database."""
def execute(self, loadbalancer_id, *args, **kwargs):
"""Get an load balancer object from the database.
:param loadbalancer_id: The load balancer ID to lookup
:returns: The load balancer object
"""
LOG.debug("Get load balancer from DB for load balancer id: %s ",
loadbalancer_id)
with db_apis.session().begin() as session:
db_lb = self.loadbalancer_repo.get(session,
id=loadbalancer_id)
lb_dict = provider_utils.db_loadbalancer_to_provider_loadbalancer(
db_lb)
return lb_dict.to_dict()
class UpdateVIPAfterAllocation(BaseDatabaseTask):
"""Update a VIP associated with a given load balancer."""
def execute(self, loadbalancer_id, vip):
"""Update a VIP associated with a given load balancer.
:param loadbalancer_id: Id of a load balancer which VIP should be
updated.
:param vip: data_models.Vip object with update data.
:returns: The load balancer object.
"""
with db_apis.session().begin() as session:
self.repos.vip.update(session, loadbalancer_id,
port_id=vip[constants.PORT_ID],
subnet_id=vip[constants.SUBNET_ID],
ip_address=vip[constants.IP_ADDRESS])
db_lb = self.repos.load_balancer.get(session,
id=loadbalancer_id)
prov_lb = provider_utils.db_loadbalancer_to_provider_loadbalancer(
db_lb)
LOG.info("Updated vip with port id %s, subnet id %s, ip address %s "
"for load balancer %s",
vip[constants.PORT_ID],
vip[constants.SUBNET_ID],
vip[constants.IP_ADDRESS],
loadbalancer_id)
return prov_lb.to_dict()
class UpdateAdditionalVIPsAfterAllocation(BaseDatabaseTask):
"""Update a VIP associated with a given load balancer."""
def execute(self, loadbalancer_id, additional_vips):
"""Update additional VIPs associated with a given load balancer.
:param loadbalancer_id: Id of a load balancer which VIP should be
updated.
:param additional_vips: data_models.AdditionalVip object with update
data.
:returns: The load balancer object.
"""
with db_apis.session().begin() as session:
for vip in additional_vips:
LOG.info("Updating additional VIP with subnet_id %s, "
"ip_address %s for load balancer %s",
vip[constants.SUBNET_ID], vip[constants.IP_ADDRESS],
loadbalancer_id)
self.repos.additional_vip.update(
session, loadbalancer_id,
vip[constants.SUBNET_ID],
ip_address=vip[constants.IP_ADDRESS],
port_id=vip[constants.PORT_ID])
db_lb = self.repos.load_balancer.get(session,
id=loadbalancer_id)
return provider_utils.db_loadbalancer_to_provider_loadbalancer(
db_lb).to_dict()
class UpdateAmphoraeVIPData(BaseDatabaseTask):
"""Update amphorae VIP data."""
def execute(self, amps_data):
"""Update amphorae VIP data.
:param amps_data: Amphorae update dicts.
:returns: None
"""
with db_apis.session().begin() as session:
for amp_data in amps_data:
self.repos.amphora.update(
session,
amp_data.get(constants.ID),
vrrp_ip=amp_data[constants.VRRP_IP],
ha_ip=amp_data[constants.HA_IP],
vrrp_port_id=amp_data[constants.VRRP_PORT_ID],
ha_port_id=amp_data[constants.HA_PORT_ID],
vrrp_id=1)
class UpdateAmphoraVIPData(BaseDatabaseTask):
"""Update amphorae VIP data."""
def execute(self, amp_data):
"""Update amphorae VIP data.
:param amps_data: Amphorae update dicts.
:returns: None
"""
with db_apis.session().begin() as session:
self.repos.amphora.update(
session,
amp_data.get(constants.ID),
vrrp_ip=amp_data[constants.VRRP_IP],
ha_ip=amp_data[constants.HA_IP],
vrrp_port_id=amp_data[constants.VRRP_PORT_ID],
ha_port_id=amp_data[constants.HA_PORT_ID],
vrrp_id=1)
class UpdateAmpFailoverDetails(BaseDatabaseTask):
"""Update amphora failover details in the database."""
def execute(self, amphora, vip, base_port):
"""Update amphora failover details in the database.
:param amphora: The amphora to update
:param vip: The VIP object associated with this amphora.
:param base_port: The base port object associated with the amphora.
:returns: None
"""
# role and vrrp_priority will be updated later.
with db_apis.session().begin() as session:
self.repos.amphora.update(
session,
amphora.get(constants.ID),
# TODO(johnsom) We should do a better job getting the fixed_ip
# as this could be a problem with dual stack.
# Fix this during the multi-vip patch.
vrrp_ip=(
base_port[constants.FIXED_IPS][0][constants.IP_ADDRESS]),
ha_ip=vip[constants.IP_ADDRESS],
vrrp_port_id=base_port[constants.ID],
ha_port_id=vip[constants.PORT_ID],
vrrp_id=1)
class AssociateFailoverAmphoraWithLBID(BaseDatabaseTask):
"""Associate failover amphora with loadbalancer in the database."""
def execute(self, amphora_id, loadbalancer_id):
"""Associate failover amphora with loadbalancer in the database.
:param amphora_id: Id of an amphora to update
:param loadbalancer_id: Id of a load balancer to be associated with
a given amphora.
:returns: None
"""
with db_apis.session().begin() as session:
self.repos.amphora.associate(session,
load_balancer_id=loadbalancer_id,
amphora_id=amphora_id)
def revert(self, amphora_id, *args, **kwargs):
"""Remove amphora-load balancer association.
:param amphora_id: Id of an amphora that couldn't be associated
with a load balancer.
:returns: None
"""
try:
with db_apis.session().begin() as session:
self.repos.amphora.update(session, amphora_id,
loadbalancer_id=None)
except Exception as e:
LOG.error("Failed to update amphora %(amp)s "
"load balancer id to None due to: "
"%(except)s", {'amp': amphora_id, 'except': str(e)})
class _MarkAmphoraRoleAndPriorityInDB(BaseDatabaseTask):
"""Alter the amphora role and priority in DB."""
def _execute(self, amphora_id, amp_role, vrrp_priority):
"""Alter the amphora role and priority in DB.
:param amphora_id: Amphora ID to update.
:param amp_role: Amphora role to be set.
:param vrrp_priority: VRRP priority to set.
:returns: None
"""
LOG.debug("Mark %(role)s in DB for amphora: %(amp)s",
{constants.ROLE: amp_role, 'amp': amphora_id})
with db_apis.session().begin() as session:
self.amphora_repo.update(session, amphora_id, role=amp_role,
vrrp_priority=vrrp_priority)
def _revert(self, result, amphora_id, *args, **kwargs):
"""Removes role and vrrp_priority association.
:param result: Result of the association.
:param amphora_id: Amphora ID which role/vrrp_priority association
failed.
:returns: None
"""
if isinstance(result, failure.Failure):
return
LOG.warning("Reverting amphora role in DB for amp id %(amp)s",
{'amp': amphora_id})
try:
with db_apis.session().begin() as session:
self.amphora_repo.update(session, amphora_id,
role=None, vrrp_priority=None)
except Exception as e:
LOG.error("Failed to update amphora %(amp)s "
"role and vrrp_priority to None due to: "
"%(except)s", {'amp': amphora_id, 'except': str(e)})
class MarkAmphoraMasterInDB(_MarkAmphoraRoleAndPriorityInDB):
"""Alter the amphora role to: MASTER."""
def execute(self, amphora):
"""Mark amphora as MASTER in db.
:param amphora: Amphora to update role.
:returns: None
"""
amp_role = constants.ROLE_MASTER
self._execute(amphora[constants.ID], amp_role,
constants.ROLE_MASTER_PRIORITY)
def revert(self, result, amphora, *args, **kwargs):
"""Removes amphora role association.
:param amphora: Amphora to update role.
:returns: None
"""
self._revert(result, amphora[constants.ID], *args, **kwargs)
class MarkAmphoraBackupInDB(_MarkAmphoraRoleAndPriorityInDB):
"""Alter the amphora role to: Backup."""
def execute(self, amphora):
"""Mark amphora as BACKUP in db.
:param amphora: Amphora to update role.
:returns: None
"""
amp_role = constants.ROLE_BACKUP
self._execute(amphora[constants.ID], amp_role,
constants.ROLE_BACKUP_PRIORITY)
def revert(self, result, amphora, *args, **kwargs):
"""Removes amphora role association.
:param amphora: Amphora to update role.
:returns: None
"""
self._revert(result, amphora[constants.ID], *args, **kwargs)
class MarkAmphoraStandAloneInDB(_MarkAmphoraRoleAndPriorityInDB):
"""Alter the amphora role to: Standalone."""
def execute(self, amphora):
"""Mark amphora as STANDALONE in db.
:param amphora: Amphora to update role.
:returns: None
"""
amp_role = constants.ROLE_STANDALONE
self._execute(amphora[constants.ID], amp_role, None)
def revert(self, result, amphora, *args, **kwargs):
"""Removes amphora role association.
:param amphora: Amphora to update role.
:returns: None
"""
self._revert(result, amphora[constants.ID], *args, **kwargs)
class MarkAmphoraAllocatedInDB(BaseDatabaseTask):
"""Will mark an amphora as allocated to a load balancer in the database.
Assume sqlalchemy made sure the DB got
retried sufficiently - so just abort
"""
def execute(self, amphora, loadbalancer_id):
"""Mark amphora as allocated to a load balancer in DB.
:param amphora: Amphora to be updated.
:param loadbalancer_id: Id of a load balancer to which an amphora
should be allocated.
:returns: None
"""
LOG.info('Mark ALLOCATED in DB for amphora: %(amp)s with '
'compute id: %(comp)s for load balancer: %(lb)s',
{
'amp': amphora.get(constants.ID),
'comp': amphora[constants.COMPUTE_ID],
'lb': loadbalancer_id
})
with db_apis.session().begin() as session:
self.amphora_repo.update(
session,
amphora.get(constants.ID),
status=constants.AMPHORA_ALLOCATED,
compute_id=amphora[constants.COMPUTE_ID],
lb_network_ip=amphora[constants.LB_NETWORK_IP],
load_balancer_id=loadbalancer_id)
def revert(self, result, amphora, loadbalancer_id, *args, **kwargs):
"""Mark the amphora as broken and ready to be cleaned up.
:param result: Execute task result
:param amphora: Amphora that was updated.
:param loadbalancer_id: Id of a load balancer to which an amphora
failed to be allocated.
:returns: None
"""
if isinstance(result, failure.Failure):
return
LOG.warning("Reverting mark amphora ready in DB for amp "
"id %(amp)s and compute id %(comp)s",
{'amp': amphora.get(constants.ID),
'comp': amphora[constants.COMPUTE_ID]})
self.task_utils.mark_amphora_status_error(
amphora.get(constants.ID))
class MarkAmphoraBootingInDB(BaseDatabaseTask):
"""Mark the amphora as booting in the database."""
def execute(self, amphora_id, compute_id):
"""Mark amphora booting in DB.
:param amphora_id: Id of the amphora to update
:param compute_id: Id of a compute on which an amphora resides
:returns: None
"""
LOG.debug("Mark BOOTING in DB for amphora: %(amp)s with "
"compute id %(id)s", {'amp': amphora_id,
constants.ID: compute_id})
with db_apis.session().begin() as session:
self.amphora_repo.update(session, amphora_id,
status=constants.AMPHORA_BOOTING,
compute_id=compute_id)
def revert(self, result, amphora_id, compute_id, *args, **kwargs):
"""Mark the amphora as broken and ready to be cleaned up.
:param result: Execute task result
:param amphora_id: Id of the amphora that failed to update
:param compute_id: Id of a compute on which an amphora resides
:returns: None
"""
if isinstance(result, failure.Failure):
return
LOG.warning("Reverting mark amphora booting in DB for amp "
"id %(amp)s and compute id %(comp)s",
{'amp': amphora_id, 'comp': compute_id})
try:
with db_apis.session().begin() as session:
self.amphora_repo.update(session, amphora_id,
status=constants.ERROR,
compute_id=compute_id)
except Exception as e:
LOG.error("Failed to update amphora %(amp)s "
"status to ERROR due to: "
"%(except)s", {'amp': amphora_id, 'except': str(e)})
class MarkAmphoraDeletedInDB(BaseDatabaseTask):
"""Mark the amphora deleted in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, amphora):
"""Mark the amphora as deleted in DB.
:param amphora: Amphora to be updated.
:returns: None
"""
LOG.debug("Mark DELETED in DB for amphora: %(amp)s with "
"compute id %(comp)s",
{'amp': amphora[constants.ID],
'comp': amphora[constants.COMPUTE_ID]})
with db_apis.session().begin() as session:
self.amphora_repo.update(session,
amphora[constants.ID],
status=constants.DELETED)
def revert(self, amphora, *args, **kwargs):
"""Mark the amphora as broken and ready to be cleaned up.
:param amphora: Amphora that was updated.
:returns: None
"""
LOG.warning("Reverting mark amphora deleted in DB "
"for amp id %(amp)s and compute id %(comp)s",
{'amp': amphora[constants.ID],
'comp': amphora[constants.COMPUTE_ID]})
self.task_utils.mark_amphora_status_error(amphora[constants.ID])
class MarkAmphoraPendingDeleteInDB(BaseDatabaseTask):
"""Mark the amphora pending delete in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, amphora):
"""Mark the amphora as pending delete in DB.
:param amphora: Amphora to be updated.
:returns: None
"""
LOG.debug("Mark PENDING DELETE in DB for amphora: %(amp)s "
"with compute id %(id)s",
{'amp': amphora[constants.ID],
'id': amphora[constants.COMPUTE_ID]})
with db_apis.session().begin() as session:
self.amphora_repo.update(session,
amphora[constants.ID],
status=constants.PENDING_DELETE)
def revert(self, amphora, *args, **kwargs):
"""Mark the amphora as broken and ready to be cleaned up.
:param amphora: Amphora that was updated.
:returns: None
"""
LOG.warning("Reverting mark amphora pending delete in DB "
"for amp id %(amp)s and compute id %(comp)s",
{'amp': amphora[constants.ID],
'comp': amphora[constants.COMPUTE_ID]})
self.task_utils.mark_amphora_status_error(amphora[constants.ID])
class MarkAmphoraPendingUpdateInDB(BaseDatabaseTask):
"""Mark the amphora pending update in the DB.
Since sqlalchemy will likely retry by itself always revert if it fails
"""
def execute(self, amphora):
"""Mark the amphora as pending update in DB.
:param amphora: Amphora to be updated.
:returns: None
"""
LOG.debug("Mark PENDING UPDATE in DB for amphora: %(amp)s "
"with compute id %(id)s",
{'amp': amphora.get(constants.ID),
'id': amphora[constants.COMPUTE_ID]})
with db_apis.session().begin() as session:
self.amphora_repo.update(session,
amphora.get(constants.ID),
status=constants.PENDING_UPDATE)
def revert(self, amphora, *args, **kwargs):
"""Mark the amphora as broken and ready to be cleaned up.
:param amphora: Amphora that was updated.
:returns: None
"""
LOG.warning("Reverting mark amphora pending update in DB "
"for amp id %(amp)s and compute id %(comp)s",
{'amp': amphora.get(constants.ID),
'comp': amphora[constants.COMPUTE_ID]})
self.task_utils.mark_amphora_status_error(amphora.get(constants.ID))
class MarkAmphoraReadyInDB(BaseDatabaseTask):
"""This task will mark an amphora as ready in the database.
Assume sqlalchemy made sure the DB got
retried sufficiently - so just abort
"""
def execute(self, amphora):
"""Mark amphora as ready in DB.
:param amphora: Amphora to be updated.
:returns: None
"""
LOG.info("Mark READY in DB for amphora: %(amp)s with compute "
"id %(comp)s",
{"amp": amphora.get(constants.ID),
"comp": amphora[constants.COMPUTE_ID]})
with db_apis.session().begin() as session:
self.amphora_repo.update(
session,
amphora.get(constants.ID),
status=constants.AMPHORA_READY,
compute_id=amphora[constants.COMPUTE_ID],
lb_network_ip=amphora[constants.LB_NETWORK_IP])
def revert(self, amphora, *args, **kwargs):
"""Mark the amphora as broken and ready to be cleaned up.
:param amphora: Amphora that was updated.
:returns: None
"""
LOG.warning("Reverting mark amphora ready in DB for amp "
"id %(amp)s and compute id %(comp)s",
{'amp': amphora.get(constants.ID),
'comp': amphora[constants.COMPUTE_ID]})
try:
with db_apis.session().begin() as session:
self.amphora_repo.update(
session,
amphora.get(constants.ID),
status=constants.ERROR,
compute_id=amphora[constants.COMPUTE_ID],
lb_network_ip=amphora[constants.LB_NETWORK_IP])
except Exception as e:
LOG.error("Failed to update amphora %(amp)s "
"status to ERROR due to: "
"%(except)s", {'amp': amphora.get(constants.ID),
'except': str(e)})
class UpdateAmphoraComputeId(BaseDatabaseTask):
"""Associate amphora with a compute in DB."""
def execute(self, amphora_id, compute_id):
"""Associate amphora with a compute in DB.
:param amphora_id: Id of the amphora to update
:param compute_id: Id of a compute on which an amphora resides
:returns: None
"""
with db_apis.session().begin() as session:
self.amphora_repo.update(session, amphora_id,
compute_id=compute_id)
class UpdateAmphoraInfo(BaseDatabaseTask):
"""Update amphora with compute instance details."""
def execute(self, amphora_id, compute_obj):
"""Update amphora with compute instance details.