28
from charmhelpers.fetch import apt_install, apt_update
28
from charmhelpers.contrib.hahelpers.cluster import (
33
from charmhelpers.fetch import apt_install, apt_update, add_source
29
34
from charmhelpers.contrib.network import ufw
30
36
import memcached_utils
39
__author__ = 'Felipe Reyes <felipe.reyes@canonical.com>'
33
41
DOT = os.path.dirname(os.path.abspath(__file__))
34
42
ETC_DEFAULT_MEMCACHED = '/etc/default/memcached'
48
57
@hooks.hook('install')
59
enable_repcached = config('repcached')
62
add_source(config('repcached_origin'))
50
64
apt_update(fatal=True)
51
65
apt_install(["memcached", "python-cheetah", "python-memcache"], fatal=True)
67
85
service_stop('memcached')
88
@hooks.hook('cluster-relation-joined')
89
def cluster_relation_joined():
91
if not config('repcached'):
92
return config_changed()
95
if peers and not oldest_peer(peers):
96
log("Delaying operation to oldest peer", level='INFO')
99
replica = replication.get_repcached_replica()
101
replica = replication.set_repcached_replica()
103
master, replica = replica
104
log("Setting replication with peer unit: %s" % replica, level="INFO")
105
config_changed(replica=replica)
108
@hooks.hook('cluster-relation-changed')
109
def cluster_relation_changed():
111
if not config('repcached'):
112
return config_changed()
115
if peers and oldest_peer(peers):
116
log("Delaying operation to secondary peer", level='INFO')
121
log('Waiting for master memcached node to become available',
123
secondary = relation_get('replica')
125
if secondary == unit_get('private-address'):
126
replication.store_replica(relation_get('master'), secondary)
127
return config_changed(replica=relation_get('master'))
128
elif secondary is None:
129
raise AttributeError()
131
msg = """This unit will be marked as failed because
132
Repcached replication only can take place between two units.
133
If you want to disable this set 'juju set repcached=false'"""
134
log(msg, level='WARN')
136
except AttributeError:
137
log("Master not yet available, waiting for %d secs" %
138
replication.REPCACHED_MASTER_WAIT)
139
time.sleep(replication.REPCACHED_MASTER_WAIT)
70
143
@hooks.hook('cache-relation-joined')
71
144
def cache_relation_joined():
100
173
mem_size = int(re.findall(r'\d+', output.split('\n')[2])[1])
101
174
mem_size = int(mem_size * 0.9)
176
if config('repcached'):
177
# If repcached was enabled after install, we need
178
# to make sure to install the memcached package with replication
180
if not memcached_utils.dpkg_info_contains('memcached',
183
add_source(config('repcached_origin'))
184
apt_update(fatal=True)
185
apt_install(["memcached"], fatal=True)
188
replica = replication.get_current_replica()
191
config('repcached') and
192
os.path.exists(replication.REPCACHED_REPLICA_FILE)
194
log("Replication has been disabled, removing from this unit",
196
ufw.service(config('repcached_port'), 'close')
197
os.remove(replication.REPCACHED_REPLICA_FILE)
103
200
configs = {'mem_size': mem_size,
104
202
'large_pages_enabled': False}
205
configs['repcached_port'] = config('repcached_port')
206
ufw.service(config('repcached_port'), 'open')
106
208
for key in ['request-limit', 'min-item-size', 'slab-page-size', 'threads',
107
209
'disable-auto-cleanup', 'disable-cas', 'factor',
108
210
'connection-limit', 'tcp-port', 'udp-port']: