Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright 2019 Canonical Ltd. 

2# 

3# This file is part of the CPUConfig Charm for Juju. 

4# 

5# This program is free software: you can redistribute it and/or modify 

6# it under the terms of the GNU General Public License version 3, as 

7# published by the Free Software Foundation. 

8# 

9# This program is distributed in the hope that it will be useful, but 

10# WITHOUT ANY WARRANTY; without even the implied warranties of 

11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 

12# PURPOSE. See the GNU General Public License for more details. 

13# 

14# You should have received a copy of the GNU General Public License 

15# along with this program. If not, see <http://www.gnu.org/licenses/>. 

16 

17"""Reactive hooks for sysconfig charm.""" 

18 

19from charmhelpers.core import hookenv, host 

20 

21from charms.reactive import ( 

22 clear_flag, 

23 helpers, 

24 hook, 

25 set_flag, 

26 when, 

27 when_none, 

28 when_not, 

29) 

30 

31from lib_sysconfig import CPUFREQUTILS, GRUB_CONF, KERNEL, SYSTEMD_SYSTEM, SysConfigHelper 

32 

33 

34@when_none('sysconfig.installed', 'sysconfig.unsupported') 

35@when('juju-info.connected') 

36def install_sysconfig(): 

37 """Install the charm if it is not running on a container. 

38 

39 Deploy can be forced using enable-container option only for testing. (Default: false). 

40 """ 

41 syshelper = SysConfigHelper() 

42 

43 # container not supported unless enable-container=true for testing purpose 

44 if host.is_container() and not syshelper.enable_container: 

45 hookenv.status_set('blocked', 'containers are not supported') 

46 set_flag('sysconfig.unsupported') 

47 return 

48 

49 if not syshelper.is_config_valid(): 

50 hookenv.status_set('blocked', 'configuration parameters not valid.') 

51 return 

52 

53 syshelper.install_configured_kernel() 

54 syshelper.update_cpufreq() 

55 syshelper.update_grub_file() 

56 syshelper.update_systemd_system_file() 

57 set_flag('sysconfig.installed') 

58 update_status() 

59 

60 

61@when('sysconfig.installed') 

62@when_not('sysconfig.unsupported') 

63@when('config.changed') 

64def config_changed(): 

65 """Apply configuration updates if the charm is installed.""" 

66 syshelper = SysConfigHelper() 

67 hookenv.status_set('maintenance', 'applying changes') 

68 

69 if not syshelper.is_config_valid(): 

70 hookenv.status_set('blocked', 'configuration parameters not valid.') 

71 return 

72 

73 # Kernel 

74 if syshelper.charm_config.changed('kernel-version'): 

75 syshelper.install_configured_kernel() 

76 

77 # cpufreq 

78 if syshelper.charm_config.changed('governor') or helpers.any_file_changed([CPUFREQUTILS]): 

79 syshelper.update_cpufreq() 

80 

81 # GRUB 

82 if any(syshelper.charm_config.changed(flag) for flag in ( 

83 'reservation', 

84 'hugepages', 

85 'hugepagesz', 

86 'raid-autodetection', 

87 'enable-pti', 

88 'enable-iommu', 

89 'grub-config-flags', 

90 'kernel-version', 

91 'update-grub', 

92 'config-flags', 

93 'cpu-range', 

94 )) or helpers.any_file_changed([GRUB_CONF]): 

95 syshelper.update_grub_file() 

96 

97 # systemd 

98 if any(syshelper.charm_config.changed(flag) for flag in ( 

99 'reservation', 

100 'systemd-config-flags', 

101 'cpu-range', 

102 'config-flags', 

103 )) or helpers.any_file_changed([SYSTEMD_SYSTEM]): 

104 syshelper.update_systemd_system_file() 

105 

106 update_status() 

107 

108 

109@hook('update-status') 

110@when_not('sysconfig.unsupported') 

111def update_status(): 

112 """Update the workload message checking if reboot is needed. 

113 

114 Note: After the reboot use clear-notification action to clear the 

115 'reboot required' message. 

116 """ 

117 resources = [KERNEL, SYSTEMD_SYSTEM, GRUB_CONF] 

118 boot_changes = SysConfigHelper.boot_resources.resources_changed_since_boot(resources) 

119 

120 if boot_changes: 

121 hookenv.status_set('active', 'reboot required. Changes in: {}'.format(', '.join(boot_changes))) 

122 else: 

123 hookenv.status_set('active', 'ready') 

124 

125 

126@when('config.changed.enable-container') 

127@when_not('sysconfig.installed') 

128def enable_container_changed(): 

129 """Trigger installation if enable-container option changed.""" 

130 clear_flag('sysconfig.unsupported') 

131 

132 

133@when('sysconfig.installed') 

134@when_not('juju-info.available') 

135def remove_configuration(): 

136 """Remove configuration applied by the charm if the juju-info relation is departed. 

137 

138 For safety, kernels installed by the charm won't be removed. 

139 """ 

140 hookenv.status_set('maintenance', 'removing sysconfig configurations') 

141 syshelper = SysConfigHelper() 

142 syshelper.remove_cpufreq_configuration() 

143 syshelper.remove_grub_configuration() 

144 syshelper.remove_systemd_configuration() 

145 clear_flag('sysconfig.installed') 

146 clear_flag('sysconfig.unsupported')