~bigdata-charmers/charms/trusty/apache-zookeeper/trunk

« back to all changes in this revision

Viewing changes to hooks/callbacks.py

  • Committer: amir sanjar
  • Date: 2015-06-09 04:26:10 UTC
  • Revision ID: amir.sanjar@canonical.com-20150609042610-3ybd104z2fx0s024
apache zookeeper first drop

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
 
 
3
import jujuresources
 
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
 
8
 
 
9
class Zookeeper(object):
 
10
 
 
11
    def __init__(self, dist_config):
 
12
        self.dist_config = dist_config
 
13
        self.resources = {
 
14
            'zookeeper': 'zookeeper-%s' % host.cpu_arch(),
 
15
        }
 
16
        self.verify_resources = utils.verify_resources(*self.resources.values())
 
17
 
 
18
    def is_installed(self):
 
19
        return unitdata.kv().get('zookeeper.installed')
 
20
 
 
21
    def install(self, force=False):
 
22
        if not force and self.is_installed():
 
23
            return
 
24
 
 
25
        jujuresources.install(self.resources['zookeeper'],
 
26
                              destination=self.dist_config.path('zookeeper'),
 
27
                              skip_top_level=True)
 
28
 
 
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)
 
36
        
 
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'))
 
41
        conf_dir.rmtree_p()
 
42
        
 
43
        
 
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')),
 
49
                })
 
50
        
 
51
    def configure_zookeeper(self):
 
52
        '''
 
53
        Configure zookeeper environment for all users
 
54
        '''
 
55
        '''
 
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
 
59
        number, in ASCII.
 
60
        '''
 
61
        myid = self.dist_config.path('zookeeper_data_dir') / 'myid'
 
62
        with open(myid, 'w+') as df:
 
63
            df.writelines(self.getid(local_unit()))
 
64
        '''
 
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
 
69
        quorum.
 
70
        '''
 
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)
 
75
            
 
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')
 
84
            
 
85
    def getid(self, unitID):
 
86
        return unitID.split("/")[1]  
 
87
            
 
88
    def start(self):
 
89
        import subprocess
 
90
        e = utils.read_etc_env()
 
91
        os.environ.update(e)
 
92
        zookeeper_home = self.dist_config.path('zookeeper')
 
93
        self.stop()
 
94
        subprocess.check_call(['{}/bin/zkServer.sh start'.format(zookeeper_home), 'start'])
 
95
        
 
96
    def stop(self):
 
97
        import subprocess
 
98
        e = utils.read_etc_env()
 
99
        os.environ.update(e)
 
100
        zookeeper_home = self.dist_config.path('zookeeper')
 
101
        subprocess.check_call(['{}/bin/zkServer.sh stop'.format(zookeeper_home), 'stop'])      
 
102
 
 
103
    def cleanup(self):
 
104
        self.dist_config.remove_dirs()
 
105
        unitdata.kv().set('zookeeper.installed', False)