/
classes.py
1087 lines (909 loc) · 38.6 KB
/
classes.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
import base64
import contextlib
import os
import random
import re
import shutil
import string
import subprocess
import charmhelpers.contrib.hahelpers.cluster as ch_cluster
import charmhelpers.contrib.network.ip as ch_ip
import charmhelpers.contrib.openstack.utils as os_utils
import charmhelpers.contrib.openstack.ha as os_ha
import charmhelpers.contrib.openstack.ha.utils as os_ha_utils
import charmhelpers.contrib.openstack.cert_utils as cert_utils
import charmhelpers.core.hookenv as hookenv
import charmhelpers.core.host as ch_host
import charmhelpers.fetch as fetch
import charms.reactive as reactive
from charms_openstack.charm.core import (
BaseOpenStackCharm,
BaseOpenStackCharmActions,
BaseOpenStackCharmAssessStatus,
get_snap_version,
)
from charms_openstack.charm.utils import (
is_data_changed,
)
import charms_openstack.adapters as os_adapters
import charms_openstack.ip as os_ip
VIP_KEY = "vip"
CIDR_KEY = "vip_cidr"
IFACE_KEY = "vip_iface"
DNSHA_KEY = "dns-ha"
APACHE_SSL_VHOST = '/etc/apache2/sites-available/openstack_https_frontend.conf'
SYSTEM_CA_CERTS = '/etc/ssl/certs/ca-certificates.crt'
SNAP_PATH_PREFIX_FORMAT = '/var/snap/{}/common'
SNAP_CA_CERTS = SNAP_PATH_PREFIX_FORMAT + '/etc/ssl/certs/ca-certificates.crt'
class OpenStackCharm(BaseOpenStackCharm,
BaseOpenStackCharmActions,
BaseOpenStackCharmAssessStatus):
"""
Base class for all OpenStack Charm classes;
encapulates general OpenStack charm payload operations
Theory:
Derive form this class, set the name, first_release and releases class
variables so that get_charm_instance() will create an instance of this
charm.
See the other class variables for details on what they are for and do.
"""
abstract_class = True
# first_release = this is the first release in which this charm works
release = 'icehouse'
# package type - package type (deb or snap) in which this charm works
package_type = 'deb'
# The name of the charm (for printing, etc.)
name = 'charmname'
# List of packages to install
packages = []
# List of packages to purge
purge_packages = []
# Python version used to execute installed workload
python_version = 2
# List of snaps to install
snaps = []
# Mode to install snaps in (jailmode/devmode/classic)
snap_mode = 'jailmode'
# Package to determine application version from
# defaults to first in packages if not provided
version_package = release_pkg = None
# Snap to determine application version from;
# defaults to first in snaps if not provided
version_snap = release_snap = None
# Keystone endpoint type
service_type = None
# Default service for the charm
default_service = None
# A dictionary of:
# {
# 'config.file': ['list', 'of', 'services', 'to', 'restart'],
# 'config2.file': ['more', 'services'],
# }
restart_map = {}
# A dictionary of:
# {
# '/etc/init.d/executable': 0o755,
# '/var/lib/super-secret-file': 0o600,
# }
permission_override_map = {}
# The list of required services that are checked for assess_status
# e.g. required_relations = ['identity-service', 'shared-db']
required_relations = []
# The command used to sync the database
sync_cmd = []
# The list of services that this charm manages
services = []
# package_codenames = {}
# The name of the repository source configuration option.
# Useful for charms managing software from UCA and consuming the
# `openstack` layer directly for re-use of common code, but not being a
# OpenStack component.
#
# Subordinate charms should set this to an empty string ('') to activate
# special handling of release selection.
source_config_key = 'openstack-origin'
@property
def resource_install_map(self):
"""Return map of resource names to installation methods
:returns Map of Juju resource names to installation methods
:rtype: {'resource_name': f}
"""
install_map = {
'driver-deb': self.install_deb
}
return install_map
def install_deb(self, deb):
"""Install the given deb.
:param deb: Path to deb
:type: str
"""
# No attempt is made to deal with dependancies. These should be
# handled by the charms 'packages' list.
subprocess.check_call(['dpkg', '-i', deb],
env=fetch.get_apt_dpkg_env())
def install_resources(self):
"""Install Juju application resources
"""
for resource_name, install_func in self.resource_install_map.items():
resource = hookenv.resource_get(resource_name)
if resource:
install_func(resource)
@property
def region(self):
"""Return the OpenStack Region as contained in the config item 'region'
"""
return self.config['region']
@property
def public_url(self):
"""Return the public endpoint URL for the default service as specified
in the self.default_service attribute
"""
return "{}:{}".format(os_ip.canonical_url(os_ip.PUBLIC),
self.api_port(self.default_service,
os_ip.PUBLIC))
@property
def admin_url(self):
"""Return the admin endpoint URL for the default service as specificed
in the self.default_service attribute
"""
return "{}:{}".format(os_ip.canonical_url(os_ip.ADMIN),
self.api_port(self.default_service,
os_ip.ADMIN))
@property
def internal_url(self):
"""Return the internal internal endpoint URL for the default service as
specificated in the self.default_service attribtue
"""
return "{}:{}".format(os_ip.canonical_url(os_ip.INTERNAL),
self.api_port(self.default_service,
os_ip.INTERNAL))
@property
def application_version(self):
"""Return the current version of the application being deployed by
the charm, as indicated by the version_package or version_snap
attribute
"""
if os_utils.snap_install_requested():
if not self.version_snap:
self.version_snap = self.snaps[0]
version = get_snap_version(self.version_snap,
fatal=False)
if not version:
version = os_utils.get_os_codename_install_source(
self.config[self.source_config_key]
)
else:
if not self.version_package:
self.version_package = self.packages[0]
version = fetch.get_upstream_version(
self.version_package
)
if not version:
version = os_utils.os_release(self.version_package)
return version
def run_pause_or_resume(self, action):
"""Helper to enable pause/resume action to be processed."""
actions = {
'pause': os_utils.pause_unit,
'resume': os_utils.resume_unit}
_services, _ = ch_cluster.get_managed_services_and_ports(
self.full_service_list,
[])
actions[action](self.assess_status, services=_services)
def pause(self):
"""Pause the charms services."""
reactive.set_flag("charm.paused")
self.run_pause_or_resume('pause')
def resume(self):
"""Resume the charms services."""
reactive.clear_flag("charm.paused")
self.run_pause_or_resume('resume')
def series_upgrade_prepare(self):
"""Prepare to upgrade series"""
reactive.set_flag("charm.series-upgrading")
reactive.set_flag("charm.paused")
os_utils.set_unit_upgrading()
self.run_pause_or_resume('pause')
def series_upgrade_complete(self):
"""Prepare to upgrade series"""
reactive.clear_flag("charm.series-upgrading")
reactive.clear_flag("charm.paused")
os_utils.clear_unit_paused()
os_utils.clear_unit_upgrading()
self.run_pause_or_resume('resume')
def enable_services(self):
"""Enable services
This method is for charm managed enabling of previously disabled
services where the end user is not involved nor informed about the
activity.
Use the pause and resume methods for end user facing activities.
"""
os_utils.manage_payload_services('resume', self.full_service_list)
def disable_services(self):
"""Disable services
This method is for charm managed disabling of services where the end
user is not involved nor informed about the activity.
Use the pause and resume methods for end user facing activities.
"""
os_utils.manage_payload_services('pause', self.full_service_list)
def restart_services(self):
"""Restart services"""
os_utils.manage_payload_services('stop', self.full_service_list)
os_utils.manage_payload_services('start', self.full_service_list)
def get_certificate_requests(self):
"""Return a dict of certificate requests"""
return cert_utils.get_certificate_request(
json_encode=False).get('cert_requests', {})
@property
def rabbit_client_cert_dir(self):
return '/var/lib/charm/{}'.format(self.service_name)
@property
def rabbit_cert_file(self):
return '{}/rabbit-client-ca.pem'.format(self.rabbit_client_cert_dir)
def get_default_cn(self):
"""Return the default Canonical Name to be used for TLS setup
:returns: 'canonical_name'
:rtype: str
"""
return os_ip.resolve_address(endpoint_type=os_ip.INTERNAL)
def configure_cert(self, path, cert, key, cn=None):
"""Write out TLS certificate and key to disk.
:param path: Directory to place files in
:type path: str
:param cert: TLS Certificate
:type cert: str
:param key: TLS Key
:type key: str
:param cn: Canonical name for service
:type cn: Option[None, str]
"""
if not cn:
cn = self.get_default_cn()
ch_host.mkdir(path=path)
if cn:
cert_filename = 'cert_{}'.format(cn)
key_filename = 'key_{}'.format(cn)
else:
cert_filename = 'cert'
key_filename = 'key'
_cert_path = os.path.join(path, cert_filename)
_key_path = os.path.join(path, key_filename)
if os.path.islink(_cert_path) or os.path.islink(_key_path):
hookenv.log(
"Certificate or key is a symlink, the following operation "
"will overwrite the link target file. This is unlikley to be "
"the desired effect. cert: {}, key: {}"
.format(_cert_path, _key_path), level=hookenv.WARNING)
ch_host.write_file(path=os.path.join(path, cert_filename),
content=cert.encode('utf-8'), group=self.group,
perms=0o640)
ch_host.write_file(path=os.path.join(path, key_filename),
content=key.encode('utf-8'), group=self.group,
perms=0o640)
def get_local_addresses(self):
"""Return list of local addresses on each configured network
For each network return an address the local unit has on that network
if one exists.
:returns: [private_addr, admin_addr, public_addr, ...]
:rtype: List[str]
"""
addresses = [
os_utils.get_host_ip(hookenv.unit_get('private-address'))]
for addr_type in os_ip.ADDRESS_MAP.keys():
laddr = os_ip.resolve_address(endpoint_type=addr_type)
if laddr:
addresses.append(laddr)
return sorted(list(set(addresses)))
def get_certs_and_keys(self, keystone_interface=None,
certificates_interface=None):
"""Collect TLS config for local endpoints
TLS keys and certs may come from user specified configuration for this
charm or they may come directly from the ``certificates`` relation.
If collecting from ``certificates`` relation there may be a certificate
and key per endpoint (public, admin etc).
:param keystone_interface: DEPRECATED Functionality removed.
:type keystone_interace: Option[None, KeystoneRequires(RelationBase)]
:param certificates_interface: Certificates interface object
:type certificates_interface: TlsRequires(Endpoint)
:returns: [
{'key': 'key1', 'cert': 'cert1', 'ca': 'ca1', 'cn': 'cn1'}
{'key': 'key2', 'cert': 'cert2', 'ca': 'ca2', 'cn': 'cn2'}
...
]
:rtype: List[Dict[str,str]]
"""
if self.config_defined_ssl_key and self.config_defined_ssl_cert:
ssl_artifacts = []
for ep_type in [os_ip.INTERNAL, os_ip.ADMIN, os_ip.PUBLIC]:
ssl_artifacts.append({
'key': self.config_defined_ssl_key.decode('utf-8'),
'cert': self.config_defined_ssl_cert.decode('utf-8'),
'ca': (self.config_defined_ssl_ca.decode('utf-8')
if self.config_defined_ssl_ca else None),
'cn': os_ip.resolve_address(endpoint_type=ep_type)})
return ssl_artifacts
elif certificates_interface:
keys_and_certs = []
reqs = certificates_interface.get_batch_requests()
ca = certificates_interface.get_ca()
chain = certificates_interface.get_chain()
for cn, data in sorted(reqs.items()):
cert = data['cert']
if chain:
cert = cert + os.linesep + chain
keys_and_certs.append({
'key': data['key'],
'cert': cert,
'ca': ca,
'chain': chain,
'cn': cn})
return keys_and_certs
else:
return []
def _get_b64decode_for(self, param):
config_value = self.config.get(param)
if config_value:
return base64.b64decode(config_value)
return None
@property
@hookenv.cached
def config_defined_ssl_key(self):
return self._get_b64decode_for('ssl_key')
@property
@hookenv.cached
def config_defined_ssl_cert(self):
return self._get_b64decode_for('ssl_cert')
@property
@hookenv.cached
def config_defined_ssl_ca(self):
return self._get_b64decode_for('ssl_ca')
def config_changed(self):
tls = reactive.endpoint_from_flag('certificates.available')
self.configure_tls(certificates_interface=tls)
def configure_ssl(self, keystone_interface=None):
"""DEPRECATED Configure SSL certificates and keys.
Please use configure_tls insteaad.
"""
hookenv.log('configure_ssl method is DEPRECATED, please use '
'configure_tls instead.', level=hookenv.WARNING)
self.configure_tls(
certificates_interface=reactive.endpoint_from_flag(
'certificates.available'))
def configure_tls(self, certificates_interface=None):
"""Write out TLS certificate data.
The reactive handler counterpart in ``layer-openstack`` will make
sure this helper is called when certificate data is available or
changed.
Note that if your charm uses the OpenStackCharm base class directly
and want to write out client/server certificate and key data you will
need to override this method and call configure_cert() with a path
argument appropriate for the service you are implementing a charm
for.
:param certificates_interface: A certificates relation
:type certificates_interface: Option[None, TlsRequires(Endpoint)]
:returns: List of certificate data as returned by get_certs_and_keys()
:rtype: List[Dict[str,str]]
"""
tls_objects = self.get_certs_and_keys(
certificates_interface=certificates_interface)
if tls_objects:
# NOTE(fnordahl): regardless of changes to data we may
# have other changes we want to apply to the files.
# (e.g. ownership, permissions)
#
# Also note that update_central_cacerts() used in configure_ca()
# has it's own logic to detect data changes.
#
# LP: #1821314
for tls_object in tls_objects:
self.configure_ca(tls_object['ca'])
if 'chain' in tls_object:
self.configure_ca(tls_object['chain'], postfix='chain')
# NOTE(fnordahl): Retaining for in-transition compability with current
# usage. The RabbitMQ TLS configuration should be initiated by the
# layer code. Given we have non-API services consuming RabbitMQ we
# should probably move the RabbitMQ reactive handling code down to the
# ``openstack`` layer too.
#
# Will address this in separate review. LP: #1841912
amqp_ssl = reactive.endpoint_from_flag('amqp.available.ssl')
if amqp_ssl:
self.configure_rabbit_cert(amqp_ssl)
return tls_objects
def configure_rabbit_cert(self, rabbit_interface):
if not os.path.exists(self.rabbit_client_cert_dir):
os.makedirs(self.rabbit_client_cert_dir)
with open(self.rabbit_cert_file, 'w') as crt:
crt.write(rabbit_interface.get_ssl_cert())
@contextlib.contextmanager
def update_central_cacerts(self, cert_files, update_certs=True):
"""Update Central certs info if one of cert_files changes"""
checksums = {path: ch_host.path_hash(path)
for path in cert_files}
yield
new_checksums = {path: ch_host.path_hash(path)
for path in cert_files}
if checksums != new_checksums and update_certs:
self.run_update_certs()
self.install_snap_certs()
def configure_ca(self, ca_cert, update_certs=True, postfix=''):
"""Write Certificate Authority certificate"""
# TODO(jamespage): work this out for snap based installations
name = self.service_name
if postfix:
name = '-'.join((name, postfix))
cert_file = (
'/usr/local/share/ca-certificates/{}.crt'
.format(name))
if ca_cert:
with self.update_central_cacerts([cert_file], update_certs):
with open(cert_file, 'w') as crt:
crt.write(ca_cert)
def run_update_certs(self):
"""Update certifiacte
Run update-ca-certificates to update the directory /etc/ssl/certs to
hold SSL certificates and generates ca-certificates.crt, a concatenated
single-file list of certificates
"""
subprocess.check_call(['update-ca-certificates', '--fresh'])
def install_snap_certs(self):
"""Install systems CA certificates for a snap
Installs the aggregated host system ca-certificates.crt into
$SNAP_COMMON/etc/ssl/certs for services running within a sandboxed
snap to consume.
Snaps should set the REQUESTS_CA_BUNDLE environment variable to
ensure requests based API calls use the updated system certs.
"""
if (os_utils.snap_install_requested() and
os.path.exists(SYSTEM_CA_CERTS)):
ca_certs = SNAP_CA_CERTS.format(self.primary_snap)
ch_host.mkdir(os.path.dirname(ca_certs))
shutil.copyfile(SYSTEM_CA_CERTS, ca_certs)
@property
def service_name(self):
return hookenv.service_name()
@property
def full_service_list(self):
"""Copy of full list of services managed
Including those automatically added by framework that charm author may
have no knowledge about.
:returns: Full list of services managed by charm
:rtype: List[str]
"""
return self.services[:]
class OpenStackAPICharm(OpenStackCharm):
"""The base class for API OS charms -- this just bakes in the default
configuration and adapter classes.
"""
abstract_class = True
MEMCACHE_CONF = '/etc/memcached.conf'
# The adapters class that this charm uses to adapt interfaces.
# If None, then it defaults to OpenstackRelationAdapters
adapters_class = os_adapters.OpenStackAPIRelationAdapters
# The configuration base class to use for the charm
# If None, then the default ConfigurationAdapter is used.
configuration_class = os_adapters.APIConfigurationAdapter
# These can be overridden in the derived charm class to allow specialism of
# config files. These values are read in the APIConfigurationAdapter and
# used to furnish the dictionary provided from the property
# 'wsgi_worker_context'. e.g. config.wsgi_worker_context.processes would
# be the number of processes for the main API wsgi worker.
wsgi_script = None
wsgi_admin_script = None
wsgi_public_script = None
wsgi_process_weight = None # use the default from charm-helpers
wsgi_admin_process_weight = None # use the default from charm-helpers
wsgi_public_process_weight = None # use the default from charm-helpers
def upgrade_charm(self):
"""Setup token cache in case previous charm version did not."""
self.setup_token_cache()
super().upgrade_charm()
def install(self):
"""Install packages related to this charm based on
contents of self.packages attribute.
"""
self.configure_source()
super().install()
def setup_token_cache(self):
"""Check if a token cache package is needed and install it if it is"""
if fetch.filter_installed_packages(self.token_cache_pkgs()):
self.install()
def enable_memcache(self, release=None):
"""Determine if memcache should be enabled on the local unit
@param release: release of OpenStack currently deployed
@returns boolean Whether memcache should be enabled
"""
if not release:
release = os_utils.get_os_codename_install_source(
self.config[self.source_config_key])
if release not in os_utils.OPENSTACK_RELEASES:
return ValueError("Unkown release {}".format(release))
return (os_utils.OPENSTACK_RELEASES.index(release) >=
os_utils.OPENSTACK_RELEASES.index('mitaka'))
def token_cache_pkgs(self, release=None):
"""Determine additional packages needed for token caching
@param release: release of OpenStack currently deployed
@returns List of package to enable token caching
"""
packages = []
if self.enable_memcache(release=release):
if self.python_version == 2:
packages.extend(['memcached', 'python-memcache'])
else:
packages.extend(['memcached', 'python3-memcache'])
return packages
def get_amqp_credentials(self):
"""Provide the default amqp username and vhost as a tuple.
This needs to be overridden in a derived class to provide the username
and vhost to the amqp interface IF the default amqp handlers are being
used.
:returns (username, host): two strings to send to the amqp provider.
"""
raise RuntimeError(
"get_amqp_credentials() needs to be overridden in the derived "
"class")
def get_database_setup(self):
"""Provide the default database credentials as a list of 3-tuples
This is used when using the default handlers for the shared-db service
and provides the (db, db_user, ip) for each database as a list.
returns a structure of:
[
{'database': <database>,
'username': <username>,
'hostname': <hostname of this unit>
'prefix': <the optional prefix for the database>, },
]
This allows multiple databases to be setup.
If more complex database setup is required, then the default
setup_database() will need to be ignored, and a custom function
written.
:returns [{'database': ...}, ...]: credentials for multiple databases
"""
raise RuntimeError(
"get_database_setup() needs to be overridden in the derived "
"class")
@property
def all_packages(self):
"""List of packages to be installed
@return ['pkg1', 'pkg2', ...]
"""
return (super(OpenStackAPICharm, self).all_packages +
self.token_cache_pkgs())
@property
def all_snaps(self):
"""List of snaps to be installed
@return ['snap1', 'snap2', ...]
"""
return super().all_snaps
@property
def full_restart_map(self):
"""Map of services to be restarted if a file changes
@return {
'file1': ['svc1', 'svc3'],
'file2': ['svc2', 'svc3'],
...
}
"""
_restart_map = super(OpenStackAPICharm, self).full_restart_map.copy()
if self.enable_memcache():
_restart_map[self.MEMCACHE_CONF] = ['memcached']
return _restart_map
@property
def full_service_list(self):
"""Copy of full list of services managed
Including those automatically added by framework that charm author may
have no knowledge about.
:returns: Full list of services managed by charm
:rtype: List[str]
"""
services = super().full_service_list
if self.enable_memcache():
services.append('memcached')
return services
class HAOpenStackCharm(OpenStackAPICharm):
abstract_class = True
HAPROXY_CONF = '/etc/haproxy/haproxy.cfg'
ha_resources = []
def __init__(self, **kwargs):
super(HAOpenStackCharm, self).__init__(**kwargs)
self.set_haproxy_stat_password()
@property
def apache_ssl_vhost_file(self):
"""Apache vhost for SSL termination
:returns: string
"""
return APACHE_SSL_VHOST
def enable_apache_ssl_vhost(self):
"""Enable Apache vhost for SSL termination
Enable Apache vhost for SSL termination if vhost exists and it is not
curently enabled
"""
if not os.path.exists(self.apache_ssl_vhost_file):
open(self.apache_ssl_vhost_file, 'a').close()
check_enabled = subprocess.call(
['a2query', '-s', 'openstack_https_frontend'])
if check_enabled:
subprocess.check_call(['a2ensite', 'openstack_https_frontend'])
self.service_reload('apache2', restart_on_failure=True)
def configure_apache(self):
if self.apache_enabled():
self.install()
self.enable_apache_modules()
self.enable_apache_ssl_vhost()
@property
def all_packages(self):
"""List of packages to be installed
@return ['pkg1', 'pkg2', ...]
"""
_packages = super(HAOpenStackCharm, self).all_packages
if self.haproxy_enabled():
_packages.append('haproxy')
if not os_utils.snap_install_requested():
if self.apache_enabled():
_packages.append('apache2')
return _packages
@property
def all_snaps(self):
"""List of snaps to be installed
@return ['snap1', 'snap2', ...]
"""
_snaps = super().all_snaps
return _snaps
@property
def full_restart_map(self):
"""Map of services to be restarted if a file changes
@return {
'file1': ['svc1', 'svc3'],
'file2': ['svc2', 'svc3'],
...
}
"""
_restart_map = super(HAOpenStackCharm, self).full_restart_map
if self.haproxy_enabled():
_restart_map[self.HAPROXY_CONF] = ['haproxy']
if os_utils.snap_install_requested():
# TODO(coreycb): add nginx config/service for ssl vhost
pass
else:
if self.apache_enabled():
_restart_map[self.apache_ssl_vhost_file] = ['apache2']
return _restart_map
def apache_enabled(self):
"""Determine if apache is being used
@return True if apache is being used"""
if os_utils.snap_install_requested():
return False
else:
return (self.get_state('ssl.enabled') or
self.get_state('ssl.requested'))
def nginx_ssl_enabled(self):
"""Determine if nginx is being used
@return True if nginx is being used"""
if os_utils.snap_install_requested():
return (self.get_state('ssl.enabled') or
self.get_state('ssl.requested'))
else:
return False
def haproxy_enabled(self):
"""Determine if haproxy is fronting the services
@return True if haproxy is fronting the service"""
return 'haproxy' in self.ha_resources
def configure_ha_resources(self, hacluster):
"""Inform the ha subordinate about each service it should manage. The
child class specifies the services via self.ha_resources
@param hacluster instance of interface class HAClusterRequires
"""
RESOURCE_TYPES = {
'vips': self._add_ha_vips_config,
'haproxy': self._add_ha_haproxy_config,
'dnsha': self._add_dnsha_config,
}
if self.ha_resources:
for res_type in self.ha_resources:
RESOURCE_TYPES[res_type](hacluster)
hacluster.bind_resources(iface=self.config[IFACE_KEY])
def _add_ha_vips_config(self, hacluster):
"""Add a VirtualIP object for each user specified vip to self.resources
@param hacluster instance of interface class HAClusterRequires
"""
if not self.config.get(VIP_KEY):
return
for vip in self.config[VIP_KEY].split():
iface, netmask, fallback = os_ha_utils.get_vip_settings(vip)
if fallback:
hacluster.add_vip(
self.name,
vip,
iface,
netmask)
else:
hacluster.add_vip(self.name, vip)
if iface:
# Remove vip resource using old raw nic name.
old_vip_key = 'res_{}_{}_vip'.format(self.name, iface)
hacluster.delete_resource(old_vip_key)
def _add_ha_haproxy_config(self, hacluster):
"""Add a InitService object for haproxy to self.resources
@param hacluster instance of interface class HAClusterRequires
"""
hacluster.add_init_service(self.name, 'haproxy')
def _add_dnsha_config(self, hacluster):
"""Add a DNSHA object to self.resources
@param hacluster instance of interface class HAClusterRequires
"""
if not self.config.get(DNSHA_KEY):
return
settings = ['os-admin-hostname', 'os-internal-hostname',
'os-public-hostname', 'os-access-hostname']
for setting in settings:
hostname = self.config.get(setting)
if hostname is None:
hookenv.log(
'DNS HA: Hostname setting {} is None. Ignoring.'.format(
setting),
hookenv.DEBUG)
continue
m = re.search('os-(.+?)-hostname', setting)
if m:
endpoint_type = m.group(1)
# resolve_address's ADDRESS_MAP uses 'int' not 'internal'
if endpoint_type == 'internal':
endpoint_type = 'int'
else:
msg = (
'Unexpected DNS hostname setting: {}. Cannot determine '
'endpoint_type name'.format(setting))
hookenv.status_set('blocked', msg)
raise os_ha.DNSHAException(msg)
ip = os_ip.resolve_address(
endpoint_type=endpoint_type,
override=False)
hacluster.add_dnsha(self.name, ip, hostname, endpoint_type)
def set_haproxy_stat_password(self):
"""Set a stats password for accessing haproxy statistics"""
if not self.get_state('haproxy.stat.password'):
password = ''.join([
random.choice(string.ascii_letters + string.digits)
for n in range(32)])
self.set_state('haproxy.stat.password', password)
def enable_apache_modules(self):
"""Enable Apache modules needed for SSL termination"""
if os_utils.snap_install_requested():
return
restart = False
for module in ['ssl', 'proxy', 'proxy_http', 'headers']:
check_enabled = subprocess.call(['a2query', '-m', module])
if check_enabled != 0:
subprocess.check_call(['a2enmod', module])
restart = True
if restart:
self.service_restart('apache2')
def configure_tls(self, certificates_interface=None):
"""Configure TLS certificates and keys
NOTE(AJK): This function tries to minimise the work it does,
particularly with writing files and restarting apache.
:param certificates_interface: certificates relation endpoint
:type certificates_interface: TlsRequires(Endpoint) object
"""
# this takes care of writing out the CA certificate
tls_objects = super().configure_tls(
certificates_interface=certificates_interface)
with is_data_changed(
'configure_ssl.ssl_objects', tls_objects) as changed:
if tls_objects:
# NOTE(fnordahl): regardless of changes to data we may
# have other changes we want to apply to the files.
# (e.g. ownership, permissions)
#
# Also note that c-h.host.write_file used in configure_cert()
# has it's own logic to detect data changes.
#
# LP: #1821314
for tls_object in tls_objects:
self.set_state('ssl.requested', True)
if os_utils.snap_install_requested():
path = ('/var/snap/{snap_name}/common/etc/nginx/ssl'
.format(snap_name=self.primary_snap))
else:
path = os.path.join('/etc/apache2/ssl/', self.name)
self.configure_cert(
path,
tls_object['cert'],
tls_object['key'],
cn=tls_object['cn'])
cert_utils.create_ip_cert_links(
os.path.join('/etc/apache2/ssl/', self.name))
if not os_utils.snap_install_requested() and changed:
self.configure_apache()
self.service_reload('apache2')
self.remove_state('ssl.requested')
self.set_state('ssl.enabled', True)
else:
self.set_state('ssl.enabled', False)
def update_peers(self, cluster):
"""Update peers in the cluster about the addresses that this unit
holds.
NOTE(AJK): This uses the helper is_data_changed() to track whether this
has already been done, and doesn't re-advertise the changes if nothing
has changed.
@param cluster: the interface object for the cluster relation
"""
laddrs = []
for addr_type in sorted(os_ip.ADDRESS_MAP.keys()):
cidr = self.config.get(os_ip.ADDRESS_MAP[addr_type]['config'])
laddr = ch_ip.get_relation_ip(
os_ip.ADDRESS_MAP[addr_type]['binding'],
cidr)
laddrs.append((addr_type, laddr))
with is_data_changed('update_peers.laddrs', laddrs) as changed: