4
from charmhelpers.core.hookenv import local_unit, unit_private_ip
5
from charmhelpers.core import unitdata
6
from charmhelpers.core import host
7
from jujubigdata import utils
9
class Zookeeper(object):
11
def __init__(self, dist_config):
12
self.dist_config = dist_config
14
'zookeeper': 'zookeeper-%s' % host.cpu_arch(),
16
self.verify_resources = utils.verify_resources(*self.resources.values())
18
def is_installed(self):
19
return unitdata.kv().get('zookeeper.installed')
21
def install(self, force=False):
22
if not force and self.is_installed():
25
jujuresources.install(self.resources['zookeeper'],
26
destination=self.dist_config.path('zookeeper'),
29
self.dist_config.add_users()
30
utils.disable_firewall()
31
self.dist_config.add_dirs()
32
self.dist_config.add_packages()
33
self.setup_zookeeper_config()
34
self.configure_zookeeper()
35
unitdata.kv().set('zookeeper.installed', True)
37
def setup_zookeeper_config(self):
38
conf_dir = self.dist_config.path('zookeeper') / 'conf'
39
self.dist_config.path('zookeeper_conf').rmtree_p()
40
conf_dir.copytree(self.dist_config.path('zookeeper_conf'))
44
zookeeper_cfg = self.dist_config.path('zookeeper_conf') / 'zoo.cfg'
45
if not zookeeper_cfg.exists():
46
(self.dist_config.path('zookeeper_conf') / 'zoo_sample.cfg').copy(zookeeper_cfg)
47
utils.re_edit_in_place(zookeeper_cfg, {
48
r'.*dataDir*.*': 'dataDir={}'.format(self.dist_config.path('zookeeper_data_dir')),
51
def configure_zookeeper(self):
53
Configure zookeeper environment for all users
56
The entries of the form server.X list the servers that make up the ZooKeeper
57
service. When the server starts up, it knows which server it is by looking for
58
the file myid in the data directory. That file has the contains the server
61
myid = self.dist_config.path('zookeeper_data_dir') / 'myid'
62
with open(myid, 'w+') as df:
63
df.writelines(self.getid(local_unit()))
65
Running ZooKeeper in standalone mode is convenient for evaluation,
66
some development, and testing. But in production, you should run ZooKeeper
67
in replicated mode. A replicated group of servers in the same application
68
is called a quorum, list all unique servers (server.xxx=xxx:2888:3888) in the
71
zookeeper_cfg = self.dist_config.path('zookeeper_conf') / 'zoo.cfg'
72
with open(zookeeper_cfg, 'w+') as zf:
73
quorumServer = "server.{}={}:2888:3888".format(self.getid(local_unit()), unit_private_ip())
74
zf.writelines(quorumServer)
76
zookeeper_bin = self.dist_config.path('zookeeper') / 'bin'
77
with utils.environment_edit_in_place('/etc/environment') as env:
78
if zookeeper_bin not in env['PATH']:
79
env['PATH'] = ':'.join([env['PATH'], zookeeper_bin])
80
env['ZOOKEEPER_CONF_DIR'] = self.dist_config.path('zookeeper_conf')
81
env['ZOOKEEPER_DATA_DIR'] = self.dist_config.path('zookeeper_data_dir')
82
env['ZOOKEEPER_LOG_DIR'] = self.dist_config.path('zookeeper_log_dir')
83
env['ZOOKEEPER_PID_DIR'] = self.dist_config.path('zookeeper_pid_dir')
85
def getid(self, unitID):
86
return unitID.split("/")[1]
90
e = utils.read_etc_env()
92
zookeeper_home = self.dist_config.path('zookeeper')
94
subprocess.check_call(['{}/bin/zkServer.sh start'.format(zookeeper_home), 'start'])
98
e = utils.read_etc_env()
100
zookeeper_home = self.dist_config.path('zookeeper')
101
subprocess.check_call(['{}/bin/zkServer.sh stop'.format(zookeeper_home), 'stop'])
104
self.dist_config.remove_dirs()
105
unitdata.kv().set('zookeeper.installed', False)