This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
/
cluster.py
1370 lines (1150 loc) · 48.9 KB
/
cluster.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
# -*- coding: utf-8 -*-
# Copyright 2013 Mirantis, Inc.
#
# 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.
"""
Cluster-related objects and collections
"""
import copy
from distutils.version import StrictVersion
import six
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql as psql
from sqlalchemy.orm.exc import MultipleResultsFound
from sqlalchemy.orm.exc import NoResultFound
from nailgun import consts
from nailgun.db import db
from nailgun.db.sqlalchemy import models
from nailgun.errors import errors
from nailgun.extensions import fire_callback_on_cluster_delete
from nailgun.extensions import fire_callback_on_node_collection_delete
from nailgun.logger import logger
from nailgun.objects import NailgunCollection
from nailgun.objects import NailgunObject
from nailgun.objects.plugin import ClusterPlugins
from nailgun.objects import Release
from nailgun.objects.serializers.cluster import ClusterSerializer
from nailgun.plugins.manager import PluginManager
from nailgun.policy.merge import NetworkRoleMergePolicy
from nailgun.settings import settings
from nailgun.utils import AttributesGenerator
from nailgun.utils import dict_merge
from nailgun.utils import traverse
class Attributes(NailgunObject):
"""Cluster attributes object."""
#: SQLAlchemy model for Cluster attributes
model = models.Attributes
@classmethod
def generate_fields(cls, instance):
"""Generate field values for Cluster attributes using generators.
:param instance: Attributes instance
:returns: None
"""
instance.generated = traverse(
instance.generated or {},
AttributesGenerator,
{
'cluster': instance.cluster,
'settings': settings,
}
)
# TODO(ikalnitsky):
#
# Think about traversing "editable" attributes. It might be very
# useful to generate default values for editable attribute at
# cluster creation time.
@classmethod
def merged_attrs(cls, instance):
"""Generates merged dict of attributes
Result includes generated Cluster attributes recursively updated
by new values from editable attributes
:param instance: Attributes instance
:returns: dict of merged attributes
"""
return dict_merge(
instance.generated,
instance.editable
)
@classmethod
def merged_attrs_values(cls, instance):
"""Transforms raw dict of attributes into dict of facts
Raw dict is taken from :func:`merged_attrs`
The result of this function is a dict of facts that wil be sent to
orchestrator
:param instance: Attributes instance
:returns: dict of merged attributes
"""
attrs = cls.merged_attrs(instance)
for group_attrs in attrs.itervalues():
for attr, value in group_attrs.iteritems():
if isinstance(value, dict) and 'value' in value:
group_attrs[attr] = value['value']
if 'common' in attrs:
attrs.update(attrs.pop('common'))
if 'additional_components' in attrs:
for comp, enabled in attrs['additional_components'].iteritems():
if isinstance(enabled, bool):
attrs.setdefault(comp, {}).update({
"enabled": enabled
})
attrs.pop('additional_components')
return attrs
class Cluster(NailgunObject):
"""Cluster object."""
#: SQLAlchemy model for Cluster
model = models.Cluster
#: Serializer for Cluster
serializer = ClusterSerializer
@classmethod
def create(cls, data):
"""Create Cluster instance with specified parameters in DB.
This includes:
* creating Cluster attributes and generating default values \
(see :func:`create_attributes`)
* creating NetworkGroups for Cluster
* adding default pending changes (see :func:`add_pending_changes`)
* if "nodes" are specified in data then they are added to Cluster \
(see :func:`update_nodes`)
:param data: dictionary of key-value pairs as object fields
:returns: Cluster instance
"""
# TODO(enchantner): fix this temporary hack in clients
if "release_id" not in data:
release_id = data.pop("release", None)
data["release_id"] = release_id
# remove read-only attribute
data.pop("is_locked", None)
assign_nodes = data.pop("nodes", [])
enabled_editable_attributes = None
if 'components' in data:
enabled_core_attributes = cls.get_cluster_attributes_by_components(
data['components'], data["release_id"])
data = dict_merge(data, enabled_core_attributes['cluster'])
enabled_editable_attributes = enabled_core_attributes['editable']
data["fuel_version"] = settings.VERSION["release"]
cluster = super(Cluster, cls).create(data)
cls.create_default_group(cluster)
cls.create_attributes(cluster, enabled_editable_attributes)
cls.create_vmware_attributes(cluster)
cls.create_default_extensions(cluster)
try:
cls.get_network_manager(cluster).\
create_network_groups_and_config(cluster, data)
cls.add_pending_changes(
cluster, consts.CLUSTER_CHANGES.attributes)
cls.add_pending_changes(
cluster, consts.CLUSTER_CHANGES.networks)
cls.add_pending_changes(
cluster, consts.CLUSTER_CHANGES.vmware_attributes)
if assign_nodes:
cls.update_nodes(cluster, assign_nodes)
except (
errors.OutOfVLANs,
errors.OutOfIPs,
errors.NoSuitableCIDR
) as exc:
raise errors.CannotCreate(exc.message)
db().flush()
ClusterPlugins.add_compatible_plugins(cluster)
PluginManager.enable_plugins_by_components(cluster)
return cluster
@classmethod
def get_cluster_attributes_by_components(cls, components, release_id):
"""Enable cluster attributes by given components
:param components: list of component names
:type components: list of strings
:param release_id: Release model id
:type release_id: str
:returns: dict -- objects with enabled attributes for cluster
"""
def _update_attributes_dict_by_binds_exp(bind_exp, value):
"""Update cluster and attributes data with bound values
:param bind_exp: path to specific attribute for model in format
model:some.attribute.value. Model can be
settings|cluster
:type bind_exp: str
:param value: value for specific attribute
:type value: bool|str|int
:returns: None
"""
model, attr_expr = bind_exp.split(':')
if model not in ('settings', 'cluster'):
return
path_items = attr_expr.split('.')
path_items.insert(0, model)
attributes = cluster_attributes
for i in six.moves.range(0, len(path_items) - 1):
attributes = attributes.setdefault(path_items[i], {})
attributes[path_items[-1]] = value
release = Release.get_by_uid(release_id)
cluster_attributes = {}
for component in Release.get_all_components(release):
if component['name'] in components:
for bind_item in component.get('bind', []):
if isinstance(bind_item, six.string_types):
_update_attributes_dict_by_binds_exp(bind_item, True)
elif isinstance(bind_item, list):
_update_attributes_dict_by_binds_exp(bind_item[0],
bind_item[1])
return {
'editable': cluster_attributes.get('settings', {}),
'cluster': cluster_attributes.get('cluster', {})
}
@classmethod
def delete(cls, instance):
node_ids = [
_id for (_id,) in
db().query(models.Node.id).
filter_by(cluster_id=instance.id).
order_by(models.Node.id)]
fire_callback_on_node_collection_delete(node_ids)
fire_callback_on_cluster_delete(instance)
super(Cluster, cls).delete(instance)
@classmethod
def get_default_kernel_params(cls, instance):
kernel_params = instance.attributes.editable.get("kernel_params", {})
return kernel_params.get("kernel", {}).get("value")
@classmethod
def create_attributes(cls, instance, editable_attributes=None):
"""Create attributes for Cluster instance, generate their values
(see :func:`Attributes.generate_fields`)
:param instance: Cluster instance
:param editable_attributes: key-value dictionary represents editable
attributes that will be merged with default editable attributes
:returns: None
"""
merged_editable_attributes = \
cls.get_default_editable_attributes(instance)
if editable_attributes:
merged_editable_attributes = dict_merge(
merged_editable_attributes, editable_attributes)
attributes = Attributes.create(
{
"editable": merged_editable_attributes,
"generated": instance.release.attributes_metadata.get(
"generated"
),
"cluster_id": instance.id
}
)
Attributes.generate_fields(attributes)
db().flush()
return attributes
@classmethod
def create_default_extensions(cls, instance):
"""Sets default extensions list from release model
:param instance: Cluster instance
:returns: None
"""
instance.extensions = instance.release.extensions
db().flush()
@classmethod
def get_default_editable_attributes(cls, instance):
"""Get editable attributes from release metadata
:param instance: Cluster instance
:returns: Dict object
"""
editable = instance.release.attributes_metadata.get("editable")
# Add default attributes of connected plugins
plugin_attrs = PluginManager.get_plugins_attributes(
instance, all_versions=True, default=True)
editable = dict(plugin_attrs, **editable)
editable = traverse(editable, AttributesGenerator, {
'cluster': instance,
'settings': settings,
})
return editable
@classmethod
def get_attributes(cls, instance, all_plugins_versions=False):
"""Get attributes for current Cluster instance.
:param instance: Cluster instance
:param all_plugins_versions: Get attributes of all versions of plugins
:returns: dict
"""
try:
attrs = db().query(models.Attributes).filter(
models.Attributes.cluster_id == instance.id
).one()
except MultipleResultsFound:
raise errors.InvalidData(
u"Multiple rows with attributes were found for cluster '{0}'"
.format(instance.name)
)
except NoResultFound:
raise errors.InvalidData(
u"No attributes were found for cluster '{0}'"
.format(instance.name)
)
attrs = dict(attrs)
# Merge plugins attributes into editable ones
plugin_attrs = PluginManager.get_plugins_attributes(
instance, all_versions=all_plugins_versions)
plugin_attrs = traverse(plugin_attrs, AttributesGenerator, {
'cluster': instance,
'settings': settings,
})
attrs['editable'].update(plugin_attrs)
return attrs
@classmethod
def get_editable_attributes(cls, instance, all_plugins_versions=False):
"""Get editable attributes for current Cluster instance.
:param instance: Cluster instance
:param all_plugins_versions: Get attributes of all versions of plugins
:return: dict
"""
return cls.get_attributes(instance, all_plugins_versions)['editable']
@classmethod
def update_attributes(cls, instance, data):
PluginManager.process_cluster_attributes(instance, data['editable'])
for key, value in data.iteritems():
setattr(instance.attributes, key, value)
cls.add_pending_changes(instance, "attributes")
db().flush()
@classmethod
def patch_attributes(cls, instance, data):
PluginManager.process_cluster_attributes(instance, data['editable'])
instance.attributes.editable = dict_merge(
instance.attributes.editable, data['editable'])
cls.add_pending_changes(instance, "attributes")
cls.get_network_manager(instance).update_restricted_networks(instance)
db().flush()
@classmethod
def get_updated_editable_attributes(cls, instance, data):
"""Same as get_editable_attributes but also merges given data.
:param instance: Cluster object
:param data: dict
:returns: dict
"""
return {'editable': dict_merge(
cls.get_editable_attributes(instance),
data.get('editable', {})
)}
@classmethod
def get_network_manager(cls, instance=None):
"""Get network manager for Cluster instance.
If instance is None then the default NetworkManager is returned
:param instance: Cluster instance
:returns: NetworkManager/NovaNetworkManager/NeutronManager
"""
if not instance:
from nailgun.network.manager import NetworkManager
return NetworkManager
ver = instance.release.environment_version
net_provider = instance.net_provider
if net_provider == consts.CLUSTER_NET_PROVIDERS.neutron:
from nailgun.network import neutron
if StrictVersion(ver) < StrictVersion('6.1'):
return neutron.NeutronManagerLegacy
if StrictVersion(ver) == StrictVersion('6.1'):
return neutron.NeutronManager61
if StrictVersion(ver) == StrictVersion('7.0'):
return neutron.NeutronManager70
if StrictVersion(ver) >= StrictVersion('8.0'):
return neutron.NeutronManager80
return neutron.NeutronManager
elif net_provider == consts.CLUSTER_NET_PROVIDERS.nova_network:
from nailgun.network import nova_network
if StrictVersion(ver) < StrictVersion('6.1'):
return nova_network.NovaNetworkManagerLegacy
if StrictVersion(ver) == StrictVersion('6.1'):
return nova_network.NovaNetworkManager61
if StrictVersion(ver) == StrictVersion('7.0'):
return nova_network.NovaNetworkManager70
if StrictVersion(ver) >= StrictVersion('8.0'):
raise errors.NovaNetworkNotSupported()
return nova_network.NovaNetworkManager
raise ValueError(
'The network provider "{0}" is not supported.'
.format(net_provider)
)
@classmethod
def add_pending_changes(cls, instance, changes_type, node_id=None):
"""Add pending changes for current Cluster.
If node_id is specified then links created changes with node.
:param instance: Cluster instance
:param changes_type: name of changes to add
:param node_id: node id for changes
:returns: None
"""
logger.debug(
u"New pending changes in environment {0}: {1}{2}".format(
instance.id,
changes_type,
u" node_id={0}".format(node_id) if node_id else u""
)
)
# TODO(enchantner): check if node belongs to cluster
ex_chs = db().query(models.ClusterChanges).filter_by(
cluster=instance,
name=changes_type
)
if not node_id:
ex_chs = ex_chs.first()
else:
ex_chs = ex_chs.filter_by(node_id=node_id).first()
# do nothing if changes with the same name already pending
if ex_chs:
return
ch = models.ClusterChanges(
cluster_id=instance.id,
name=changes_type
)
if node_id:
ch.node_id = node_id
db().add(ch)
db().flush()
@classmethod
def get_nodes_not_for_deletion(cls, cluster):
"""All clusters nodes except nodes for deletion."""
return db().query(models.Node).filter_by(
cluster=cluster, pending_deletion=False).order_by(models.Node.id)
@classmethod
def clear_pending_changes(cls, instance, node_id=None):
"""Clear pending changes for current Cluster.
If node_id is specified then only clears changes connected
to this node.
:param instance: Cluster instance
:param node_id: node id for changes
:returns: None
"""
logger.debug(
u"Removing pending changes in environment {0}{1}".format(
instance.id,
u" where node_id={0}".format(node_id) if node_id else u""
)
)
chs = db().query(models.ClusterChanges).filter_by(
cluster_id=instance.id
)
if node_id:
chs = chs.filter_by(node_id=node_id)
map(db().delete, chs.all())
db().flush()
@classmethod
def update(cls, instance, data):
"""Update Cluster object instance with specified parameters in DB
If "nodes" are specified in data then they will replace existing ones
(see :func:`update_nodes`)
:param instance: Cluster instance
:param data: dictionary of key-value pairs as object fields
:returns: Cluster instance
"""
# remove read-only attributes
data.pop("fuel_version", None)
data.pop("is_locked", None)
nodes = data.pop("nodes", None)
changes = data.pop("changes", None)
super(Cluster, cls).update(instance, data)
if nodes is not None:
cls.update_nodes(instance, nodes)
if changes is not None:
cls.update_changes(instance, changes)
return instance
@classmethod
def update_nodes(cls, instance, nodes_ids):
"""Update Cluster nodes by specified node IDs
Nodes with specified IDs will replace existing ones in Cluster
:param instance: Cluster instance
:param nodes_ids: list of nodes ids
:returns: None
"""
# TODO(NAME): sepatate nodes
# for deletion and addition by set().
new_nodes = []
if nodes_ids:
new_nodes = db().query(models.Node).filter(
models.Node.id.in_(nodes_ids)
)
nodes_to_remove = [n for n in instance.nodes
if n not in new_nodes]
nodes_to_add = [n for n in new_nodes
if n not in instance.nodes]
for node in nodes_to_add:
if not node.online:
raise errors.NodeOffline(
u"Cannot add offline node "
u"'{0}' to environment".format(node.id)
)
# we should reset hostname to default value to guarantee
# hostnames uniqueness for nodes outside clusters
from nailgun.objects import Node
for node in nodes_to_remove:
node.hostname = Node.default_slave_name(node)
map(instance.nodes.remove, nodes_to_remove)
map(instance.nodes.append, nodes_to_add)
net_manager = cls.get_network_manager(instance)
map(
net_manager.clear_assigned_networks,
nodes_to_remove
)
map(
net_manager.clear_bond_configuration,
nodes_to_remove
)
cls.replace_provisioning_info_on_nodes(instance, [], nodes_to_remove)
cls.replace_deployment_info_on_nodes(instance, [], nodes_to_remove)
from nailgun.objects import NodeCollection
NodeCollection.reset_network_template(nodes_to_remove)
from nailgun.objects import OpenstackConfig
OpenstackConfig.disable_by_nodes(nodes_to_remove)
map(
net_manager.assign_networks_by_default,
nodes_to_add
)
cls.update_nodes_network_template(instance, nodes_to_add)
db().flush()
@classmethod
def update_changes(cls, instance, changes):
instance.changes_list = [
models.ClusterChanges(**change) for change in changes
]
db().flush()
@classmethod
def get_ifaces_for_network_in_cluster(cls, instance, net):
"""Method for receiving node_id:iface pairs for all nodes in cluster
:param instance: Cluster instance
:param net: Nailgun specific network name
:type net: str
:returns: List of node_id, iface pairs for all nodes in cluster.
"""
nics_db = db().query(
models.NodeNICInterface.node_id,
models.NodeNICInterface.name
).filter(
models.NodeNICInterface.node.has(cluster_id=instance.id),
models.NodeNICInterface.assigned_networks_list.any(name=net)
)
bonds_db = db().query(
models.NodeBondInterface.node_id,
models.NodeBondInterface.name
).filter(
models.NodeBondInterface.node.has(cluster_id=instance.id),
models.NodeBondInterface.assigned_networks_list.any(name=net)
)
return nics_db.union(bonds_db)
@classmethod
def replace_provisioning_info_on_nodes(cls, instance, data, nodes):
for node in nodes:
node_data = next((n for n in data if node.uid == n.get('uid')), {})
node.replaced_provisioning_info = node_data
@classmethod
def replace_deployment_info_on_nodes(cls, instance, data, nodes):
for node in instance.nodes:
node_data = [n for n in data if node.uid == n.get('uid')]
node.replaced_deployment_info = node_data
@classmethod
def replace_provisioning_info(cls, instance, data):
received_nodes = data.pop('nodes', [])
instance.is_customized = True
instance.replaced_provisioning_info = data
cls.replace_provisioning_info_on_nodes(
instance, received_nodes, instance.nodes)
return cls.get_provisioning_info(instance)
@classmethod
def replace_deployment_info(cls, instance, data):
instance.is_customized = True
cls.replace_deployment_info_on_nodes(instance, data, instance.nodes)
return cls.get_deployment_info(instance)
@classmethod
def get_provisioning_info(cls, instance):
data = {}
if instance.replaced_provisioning_info:
data.update(instance.replaced_provisioning_info)
nodes = []
for node in instance.nodes:
if node.replaced_provisioning_info:
nodes.append(node.replaced_provisioning_info)
if data:
data['nodes'] = nodes
return data
@classmethod
def get_deployment_info(cls, instance):
data = []
for node in instance.nodes:
if node.replaced_deployment_info:
data.extend(node.replaced_deployment_info)
return data
@classmethod
def get_creds(cls, instance):
return instance.attributes.editable['access']
@classmethod
def should_assign_public_to_all_nodes(cls, instance):
"""Check if Public network is to be assigned to all nodes in cluster
:param instance: cluster instance
:returns: True when Public network is to be assigned to all nodes
"""
if instance.net_provider == \
consts.CLUSTER_NET_PROVIDERS.nova_network:
return True
assignment = instance.attributes.editable.get(
'public_network_assignment')
if not assignment or assignment['assign_to_all_nodes']['value']:
return True
return False
@classmethod
def neutron_dvr_enabled(cls, instance):
neutron_attrs = instance.attributes.editable.get(
'neutron_advanced_configuration')
if neutron_attrs:
return neutron_attrs['neutron_dvr']['value']
else:
return False
@classmethod
def get_roles(cls, instance):
"""Returns a dictionary of node roles available for deployment.
:param instance: cluster instance
:returns: a dictionary of roles metadata
"""
available_roles = copy.deepcopy(instance.release.roles_metadata)
available_roles.update(
PluginManager.get_plugins_node_roles(instance))
return available_roles
@classmethod
def set_primary_role(cls, instance, nodes, role_name):
"""Method for assigning primary attribute for specific role.
- verify that there is no primary attribute of specific role
assigned to cluster nodes with this role in role list
or pending role list, and this node is not marked for deletion
- if there is no primary role assigned, filter nodes which have current
role in roles or pending_roles
- if there is nodes with ready state - they should have higher priority
- if role was in primary_role_list - change primary attribute
for that association, same for role_list, this is required
because deployment_serializer used by cli to generate deployment info
:param instance: Cluster db objects
:param nodes: list of Node db objects
:param role_name: string with known role name
"""
if role_name not in cls.get_roles(instance):
logger.warning(
'Trying to assign primary for non-existing role %s', role_name)
return
node = cls.get_primary_node(instance, role_name)
if not node:
# get nodes with a given role name which are not going to be
# removed
filtered_nodes = []
for node in nodes:
if (not node.pending_deletion and (
role_name in set(node.roles + node.pending_roles))):
filtered_nodes.append(node)
filtered_nodes = sorted(filtered_nodes, key=lambda node: node.id)
if filtered_nodes:
primary_node = next((
node for node in filtered_nodes
if node.status == consts.NODE_STATUSES.ready),
filtered_nodes[0])
primary_node.primary_roles = list(primary_node.primary_roles)
primary_node.primary_roles.append(role_name)
db().flush()
@classmethod
def set_primary_roles(cls, instance, nodes):
"""Assignment of all primary attribute for all roles that requires it.
This method is idempotent
To mark role as primary add has_primary: true attribute to release
:param instance: Cluster db object
:param nodes: list of Node db objects
"""
if not instance.is_ha_mode:
return
roles_metadata = cls.get_roles(instance)
for role, meta in six.iteritems(roles_metadata):
if meta.get('has_primary'):
cls.set_primary_role(instance, nodes, role)
@classmethod
def get_nodes_by_role(cls, instance, role_name):
"""Get nodes related to some specific role
:param instance: cluster db object
:type: python object
:param role_name: node role name
:type: string
"""
if role_name not in cls.get_roles(instance):
logger.warning("%s role doesn't exist", role_name)
return []
nodes = db().query(models.Node).filter_by(
cluster_id=instance.id
).filter(sa.or_(
models.Node.roles.any(role_name),
models.Node.pending_roles.any(role_name)
)).all()
return nodes
@classmethod
def get_nodes_by_status(cls, instance, status):
"""Get cluster nodes with particular status
:param instance: cluster instance
:param status: node status
:return: filtered query on nodes
"""
return db().query(models.Node).filter_by(
cluster_id=instance.id,
status=status
)
@classmethod
def get_primary_node(cls, instance, role_name):
"""Get primary node for role_name
If primary node is not found None will be returned
Pending roles and roles are used in search
:param instance: cluster db object
:type: python object
:param role_name: node role name
:type: string
:returns: node db object or None
"""
logger.debug("Getting primary node for role: %s", role_name)
if role_name not in cls.get_roles(instance):
logger.debug("Role not found: %s", role_name)
return None
primary_node = db().query(models.Node).filter_by(
pending_deletion=False,
cluster_id=instance.id
).filter(
models.Node.primary_roles.any(role_name)
).first()
if primary_node is None:
logger.debug("Not found primary node for role: %s", role_name)
else:
logger.debug("Found primary node: %s for role: %s",
primary_node.id, role_name)
return primary_node
@classmethod
def get_controllers_group_id(cls, instance):
return cls.get_controllers_node_group(instance).id
@classmethod
def get_controllers_node_group(cls, instance):
return cls.get_common_node_group(instance, ['controller'])
@classmethod
def get_common_node_group(cls, instance, noderoles):
"""Returns a common node group for a given node roles.
If a given node roles have different node groups, the error
will be raised, so it's mandatory to have them the same
node group.
:param instance: a Cluster instance
:param noderoles: a list of node roles
:returns: a common NodeGroup instance
"""
nodegroups = cls.get_node_groups(instance, noderoles).all()
# NOTE(ikalnitsky):
# The 'nodegroups' may be an empty list only in case when there's
# no nodes with a given 'noderoles' in the cluster. I think we
# should reconsider this behaviour, and propogate some error up
# above, because returning default node group doesn't look like
# a right choice here.
if not nodegroups:
return cls.get_default_group(instance)
if len(nodegroups) > 1:
raise errors.CanNotFindCommonNodeGroup(
'Node roles [{0}] has more than one common node group'.format(
', '.join(noderoles)))
return nodegroups[0]
@classmethod
def get_node_groups(cls, instance, noderoles):
"""Returns node groups for given node roles.
:param instance: a Cluster instance
:param noderoles: a list of node roles
:returns: a query for list of NodeGroup instances
"""
psql_noderoles = sa.cast(
psql.array(noderoles),
psql.ARRAY(sa.String(consts.ROLE_NAME_MAX_SIZE)))
nodegroups = db().query(models.NodeGroup).join(models.Node).filter(
models.Node.cluster_id == instance.id,
models.Node.pending_deletion.is_(False)
).filter(sa.or_(
models.Node.roles.overlap(psql_noderoles),
models.Node.pending_roles.overlap(psql_noderoles)
))
return nodegroups
@classmethod
def get_bond_interfaces_for_all_nodes(cls, instance, networks=None):
bond_interfaces_query = db().query(models.NodeBondInterface).\
join(models.Node).filter(models.Node.cluster_id == instance.id)
if networks:
bond_interfaces_query = bond_interfaces_query.join(
models.NodeBondInterface.assigned_networks_list,
aliased=True).filter(models.NetworkGroup.id.in_(networks))
return bond_interfaces_query.all()
@classmethod
def get_nic_interfaces_for_all_nodes(cls, instance, networks=None):
nic_interfaces_query = db().query(models.NodeNICInterface).\
join(models.Node).filter(models.Node.cluster_id == instance.id)
if networks:
nic_interfaces_query = nic_interfaces_query.join(
models.NodeNICInterface.assigned_networks_list, aliased=True).\
filter(models.NetworkGroup.id.in_(networks))
return nic_interfaces_query.all()
@classmethod
def get_default_group(cls, instance):
return next(g for g in instance.node_groups if g.is_default)
@classmethod
def create_default_group(cls, instance):
node_group = models.NodeGroup(name=consts.NODE_GROUPS.default,
is_default=True)
instance.node_groups.append(node_group)
db.add(node_group)
db().flush()
return node_group
@classmethod
def get_deployment_tasks(cls, instance):
"""Return deployment graph for cluster based on cluster attributes
- if there is deployment_graph defined by user - use it instead of
defined
- if instance assigned for patching - return custom patching graph
- else return default for release and enabled plugins
deployment graph
"""
if instance.deployment_tasks:
return instance.deployment_tasks
else:
release_deployment_tasks = \
Release.get_deployment_tasks(instance.release)
plugin_deployment_tasks = \
PluginManager.get_plugins_deployment_tasks(instance)
return release_deployment_tasks + plugin_deployment_tasks
@classmethod
def get_refreshable_tasks(cls, instance, filter_by_configs=None):
"""Return list of refreshable tasks
If 'filter_by_configs' specified then only tasks needed to update
these config resources will be returned as a result, otherwise
all refreshable tasks will be returned
:param instance: a Cluster instance
:param filter_by_configs: a list with configs resources
:return: list of tasks
"""
if filter_by_configs:
filter_by_configs = set(filter_by_configs)
tasks = []
for task in cls.get_deployment_tasks(instance):
refresh_on = task.get(consts.TASK_REFRESH_FIELD)
if (refresh_on
and (filter_by_configs is None
or filter_by_configs.intersection(set(refresh_on)))):
tasks.append(task)
return tasks
@classmethod
def get_volumes_metadata(cls, instance):
"""Return proper volumes metadata for cluster
Metadata consists of general volumes metadata from release
and volumes metadata from plugins which are releated to this cluster