-
Notifications
You must be signed in to change notification settings - Fork 189
/
start-hbase.py
executable file
·130 lines (104 loc) · 3.58 KB
/
start-hbase.py
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
#!/usr/bin/env python3
#
# Script to start hbase-docker and update the /etc/hosts file to
# point to the hbase-docker container
#
# hbase thrift and master server logs are written to the
# startup-relative 'data/logs' directory
#
import json
import logging
import os
import os.path
from shutil import (rmtree)
from subprocess import (check_output, run)
# Image
IMAGE_NAME = 'dajobe/hbase'
# Docker container name to use
CONTAINER_NAME = 'hbase-docker'
# Maps to $PWD/data
DATA_DIR_IN_CONTAINER = '/data'
# List of Tuples of (Label, Port number, use: api or web)
CONFIG = [
('REST API', 8080, 'api'),
('REST UI', 8085, 'web'),
('Thrift API', 9090, 'api'),
('Thrift UI', 9095, 'web'),
('Zookeeper API', 2181, 'api'),
('Master UI', 16010, 'web'),
]
def main():
''' Start HBase in docker '''
logging.basicConfig()
#logging.basicConfig(level=logging.DEBUG)
cwd = os.getcwd()
data_dir = os.path.join(cwd, 'data')
# Set up data directory
if os.path.exists(data_dir):
rmtree(data_dir, ignore_errors=False)
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# force kill any existing container
cmd = ['docker', 'rm', '-f', CONTAINER_NAME]
logging.debug(cmd)
# Do not care about output (or exit code)
run(cmd, check=False)
print('Starting HBase container')
cmd = ['docker', 'run',
f'--name={CONTAINER_NAME}', '-h', CONTAINER_NAME,
'-d', '-P', '-v', f'{data_dir}:{DATA_DIR_IN_CONTAINER}', IMAGE_NAME]
logging.debug(cmd)
container_id = check_output(cmd, encoding='utf-8').strip()
print(f'Container has ID {container_id}')
# Get the container configuration
cmd = ['docker', 'inspect', container_id]
logging.debug(cmd)
config_json = check_output(cmd, encoding='utf-8')
logging.debug(config_json)
config = json.loads(config_json)
logging.debug(json.dumps(config))
docker_hostname = config[0]['Config']['Hostname']
docker_ip = config[0]['NetworkSettings']['IPAddress']
hosts_hbase_docker_ip = ''
with open('/etc/hosts') as hosts_file:
for line in hosts_file.readlines():
fields = line.split()
if len(fields) > 2 and fields[1] == docker_hostname:
hosts_hbase_docker_ip = fields[0]
break
if hosts_hbase_docker_ip == docker_ip:
print(f'/etc/hosts already contains {docker_hostname} hostname and IP')
else:
print(f'Updating /etc/hosts to make {docker_hostname} point to {docker_ip} ({docker_hostname})')
print('Running sudo - expect to type your password')
if hosts_hbase_docker_ip == '':
cmd_input = f'docker_ip {CONTAINER_NAME} {docker_hostname}'
cmd = ['sudo', 'tee', '-a', '/etc/hosts']
run(cmd, input=cmd_input, check=True)
else:
sed_script = \
f's/^.*{CONTAINER_NAME}.*$/{docker_ip} {CONTAINER_NAME} {docker_hostname}/'
cmd = ['sudo', 'sed', '-i.bak', sed_script, '/etc/hosts']
run(cmd, check=True)
hostname = 'localhost'
print(f'\nConnect to HBase at {hostname} on these endpoints')
for cfg in CONFIG:
(label, port, typ) = cfg
mapped_port = config[0]['NetworkSettings']['Ports'][f'{port}/tcp'][0]['HostPort']
key = f'{hostname}:{mapped_port}'
if typ == 'web':
key = f'http://{key}/'
print(f' {label:<15} {key}')
hostname = docker_hostname
print(f'\nOR Connect to HBase on container {hostname} at these endpoints')
for cfg in CONFIG:
(label, port, typ) = cfg
key = f'{hostname}:{port}'
if typ == 'web':
key = f'http://{key}/'
print(' {0:<15s} {1:s}'.format(label, key))
print('\nFor docker status:')
print(f'$ id={container_id}')
print('$ docker inspect $id')
if __name__ == '__main__':
main()