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

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

from charms.reactive import hook, when, when_all, when_any, when_not, set_state 

from charmhelpers.core import hookenv, host 

from charmhelpers import fetch 

 

# import subprocess 

import fileinput 

import os 

import errno 

 

from libhaproxy import ProxyHelper 

 

ph = ProxyHelper() 

 

 

@when_not('haproxy.installed') 

def install_haproxy(): 

hookenv.status_set('maintenance', 'Installing HAProxy') 

fetch.add_source(ph.ppa) 

fetch.apt_update() 

fetch.install('haproxy') 

set_state('haproxy.installed') 

 

 

@when('haproxy.installed') 

@when_not('haproxy.configured') 

def configure_haproxy(): 

hookenv.status_set('maintenance', 'Configuring HAProxy') 

try: 

os.makedirs(ph.ssl_path) 

except OSError as exception: 

if exception.errno != errno.EEXIST: 

raise 

# Enable udp for rsyslog 

for line in fileinput.input('/etc/rsyslog.conf', inplace=True): 

line = line.replace('#module(load="imudp")', 'module(load="imudp")') 

line = line.replace( 

'#input(type="imudp" port="514")', 

'input(type="imudp" port="514")') 

print(line, end='') 

# end statement above avoids inserting new lines at EOL 

host.service_restart('rsyslog.service') 

if ph.charm_config['enable-stats']: 

ph.enable_stats() 

if ph.charm_config['enable-letsencrypt']: 

ph.enable_letsencrypt() 

if ph.charm_config['enable-upnp']: 

ph.add_upnp_cron() 

if ph.charm_config['enable-https-redirect']: 

ph.enable_redirect() 

ph.add_timeout_tunnel() 

hookenv.status_set('active', '') 

set_state('haproxy.configured') 

 

 

@when_all('reverseproxy.triggered', 'reverseproxy.changed') 

def configure_relation(reverseproxy, *args): 

hookenv.status_set('maintenance', 'Setting up relation') 

hookenv.log("Received config: {}".format(reverseproxy.config), "Info") 

# Process either dict or list of dicts to support legacy relations 

configs = [] 

if isinstance(reverseproxy.config, dict): 

configs.append(reverseproxy.config) 

else: 

configs = reverseproxy.config 

status = ph.process_configs(configs) 

reverseproxy.set_cfg_status(**status) 

hookenv.status_set('active', '') 

 

 

@when_all('reverseproxy.triggered', 'reverseproxy.departed') 

def remove_relation(reverseproxy, *args): 

hookenv.log("Removing config for: {}".format(hookenv.remote_unit())) 

# Process either dict or list of dicts to support legacy relations 

# TODO: This doesn't seem to clean up frontends and close ports 

configs = [] 

if isinstance(reverseproxy.config, dict): 

configs.append(reverseproxy.config) 

else: 

configs = reverseproxy.config 

 

for names in ph.get_config_names(configs): 

unit_name = names[0] 

backend_name = names[1] 

hookenv.log("Cleaning on depart for {}, {}".format( 

unit_name, 

backend_name), 'DEBUG') 

ph.clean_config(unit=unit_name, backend_name=backend_name) 

 

 

@when('config.changed.version') 

def version_changed(): 

if hookenv.hook_name() == "install": 

return 

hookenv.log('Version change will not affect running units', 'WARNING') 

hookenv.status_set('active', "version change to {} not applied, redeploy" 

"unit for version change".format( 

ph.charm_config['version'])) 

 

 

@when_any('config.changed.enable-stats', 

'config.changed.stats-user', 

'config.changed.stats-passwd', 

'config.changed.stats-url', 

'config.changed.stats-port', 

'config.changed.stats-local') 

def stats_changed(): 

if hookenv.hook_name() == "install": 

return 

if ph.charm_config['enable-stats']: 

hookenv.log('Enabling stats for config change') 

ph.enable_stats() 

else: 

hookenv.log('Disabling stats for config change') 

ph.disable_stats() 

 

 

@when_any('config.changed.enable-upnp') 

def upnp_changed(): 

if hookenv.hook_name() == "install": 

return 

if ph.charm_config['enable-upnp']: 

ph.add_upnp_cron() 

ph.renew_upnp() 

else: 

ph.remove_upnp_cron() 

# Turning on upnp to close ports so a release is issued 

ph.charm_config['enable-upnp'] = True 

ph.release_upnp() 

# Turning upnp back off and opening ports w/o upnp 

ph.charm_config['enable-upnp'] = False 

ph.update_ports() 

 

 

@when('config.changed.upnp-renew-interval') 

def upnp_interval_changed(): 

if hookenv.hook_name() == "install": 

return 

ph.remove_upnp_cron() 

if ph.charm_config['enable-upnp']: 

ph.add_upnp_cron() 

 

 

@when_any('config.changed.enable-letsencrypt', 

'config.changed.letsencrypt-domains', 

'config.changed.letsencrypt-email') 

def letsencrypt_config_changed(): 

if hookenv.hook_name() == "install": 

return 

ph.disable_letsencrypt() 

if ph.charm_config['enable-letsencrypt']: 

ph.enable_letsencrypt() 

 

 

@when('config.changed.cert-renew-interval') 

def cert_interval_changed(): 

if hookenv.hook_name() == "install": 

return 

ph.remove_cert_cron() 

if ph.charm_config['enable-letsencrypt']: 

ph.add_cert_cron() 

 

 

@when('config.changed.enable-https-redirect') 

def redirect_changed(): 

if hookenv.hook_name() == "install": 

return 

if ph.charm_config['enable-https-redirect']: 

ph.enable_redirect() 

else: 

ph.disable_redirect() 

 

 

@hook('stop') 

def stop_haproxy(): 

if ph.charm_config['enable-stats']: 

hookenv.log("Disabling status to free any opened ports", "INFO") 

ph.disable_stats() 

hookenv.log("Disabling letsencrypt to free any opened ports", "INFO") 

ph.disable_letsencrypt()