-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathconf-select
More file actions
224 lines (195 loc) · 7.63 KB
/
Copy pathconf-select
File metadata and controls
224 lines (195 loc) · 7.63 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import optparse
import copy
import os
import re
import sys
import errno
from params import stack_root
# The global prefix and current directory
root = stack_root
current = root + "/current"
vconfroot = 'etc'
lib_root = "usr/lib"
vconf_name = "conf.dist"
packages = ("hadoop", "hbase", "hive", "hive-hcatalog",
"kafka", "oozie", "spark", "tez",
"zookeeper", "zeppelin", "flink", "solr", "ranger-admin", "ranger-usersync", "ranger-tagsync")
''' conf link
/usr/bigtop/3.2.0/usr/lib/${pname}/conf -> /etc/${pname}/conf -> /etc/alternatives/${pname}-conf -> /usr/bigtop/3.2.0/etc/${pname}/conf.dist.0
'''
def printHelp():
print("""
usage: conf-select [-h] [<command>] --package --stack-version --conf-version
arguments:
<command> One of create-conf-dir, set-conf-dir
<--package> package name to set
<--stack-version> stack version number
<--conf-version> conf version to set
optional arguments:
-h, --help show this help message and exit
Commands:
set-conf-dir : set the conf to a specified version
create-conf-dir : create the specified conf directory
dry-run-set : dry run for set conf
dry-run-create : dry run for create conf
""")
def chkPkg(sver, pname):
'''
checks package name against the package tuple, check if the package directory exists and returns True
e.g. /usr/bigtop/3.2.0/usr/lib/zookeeper
'''
pkgdir = os.path.join(root, sver, lib_root, pname)
if not os.path.isdir(pkgdir) or pname not in packages:
print(pname + " not installed or incorrect package name")
sys.exit(1)
return True
def chksVer(sver):
'''
returns True if the stack version number exists
e.g. /usr/bigtop/3.2.0/usr/lib
'''
if not os.path.isdir(os.path.join(root, sver, lib_root)):
print(sver + " Incorrect stack version")
sys.exit(1)
return True
def chkPath(pname, sver, cver, method):
'''
e.g. /usr/bigtop/3.2.0/etc/zookeeper/conf.dist.0
'''
confpath = os.path.join(root, sver, vconfroot, pname, vconf_name)
if cver:
confpath = os.path.join(root, sver, vconfroot, pname, vconf_name + '.' + cver)
if method == "create" and os.path.exists(confpath):
print(confpath+" exist already")
sys.exit(1)
if method == "set" and not os.path.exists(confpath):
print(confpath+" does not exist")
sys.exit(1)
return True
def check(sver, pname, cver, method):
'''
Aggregates chksVer, chkPkg and checks if path exits.
Returns True if all the conditions are met
'''
chksVer(sver)
chkPkg(sver, pname)
chkPath(pname, sver, cver, method)
def drcrtConfDir(pname, sver, cver):
'''
# Not really executing
# e.g. /usr/bigtop/3.2.0/etc/zookeeper/conf.dist.0
'''
chksVer(sver)
chkPkg(sver, pname)
for confmapkey, confmapval in confmap.items():
path = os.path.join(root, sver, vconfroot, confmapkey, vconf_name)
if cver:
path = os.path.join(root, sver, vconfroot, confmapkey, vconf_name+'.'+cver)
print(path)
def crtConfDir(pname, sver, cver):
'''
e.g. /usr/bigtop/3.2.0/etc/zookeeper/conf.dist.0
'''
path = os.path.join(root, sver, vconfroot, pname, vconf_name)
check(sver, pname, cver, "create")
for confmapkey, confmapval in confmap.items():
path = os.path.join(root, sver, vconfroot, confmapkey, vconf_name)
if cver:
path = os.path.join(root, sver, vconfroot, confmapkey, vconf_name+'.'+cver)
try:
os.makedirs(path)
print(path)
except OSError as exc:
if exc.errno == errno.EACCES:
print("Permission denied")
def drsetConfDir(pname, sver):
'''
Not really executing
'''
chksVer(sver)
chkPkg(sver, pname)
for confmapkey, confmapval in confmap.items():
# e.g. /usr/bigtop/3.2.0/usr/lib/hive/conf
confdir = os.path.join(root, sver, lib_root, pname, confmapval)
if os.path.exists(confdir) and os.path.islink(confdir):
print(confdir)
else:
print(confdir+" does not exist")
sys.exit(1)
def setConfDir(pname, sver, cver):
'''
e.g. /usr/bigtop/3.2.0/usr/lib/zookeeper/conf -> /etc/zookeeper/conf -> /usr/bigtop/3.2.0/etc/zookeeper/conf.dist.0
'''
check(sver, pname, cver, "set")
for confmapkey, confmapval in confmap.items():
# e.g. /usr/bigtop/3.2.0/etc/zookeeper/conf.dist.0
path = os.path.join(root, sver, vconfroot, confmapkey, vconf_name)
if cver:
path = os.path.join(root, sver, vconfroot, confmapkey, vconf_name+'.'+cver)
# e.g. /usr/bigtop/3.2.0/usr/lib/zookeeper/conf
confdir = os.path.join(root, sver, lib_root, pname, confmapval)
if os.path.exists(confdir) and not os.path.islink(confdir):
raise Exception("Expected confdir %s to be a symlink." % confdir)
if os.path.islink(confdir) and not os.path.exists(confdir):
os.remove(confdir)
if os.path.exists(confdir):
if path == os.readlink(confdir):
return
else:
os.remove(confdir)
os.symlink('/etc/{0}/conf'.format(pname), confdir)
print(confdir + " -> " + '/etc/{0}/conf'.format(pname))
print('alternatives --install /etc/{0}/conf {0}-conf {1} 30'.format(pname, path))
os.system('alternatives --install /etc/{0}/conf {0}-conf {1} 30'.format(pname, path))
os.system('alternatives --set {0}-conf {1}'.format(pname, path))
print('/etc/{0}/conf'.format(pname) + " -> " + path)
# Start of main
parser = optparse.OptionParser(add_help_option=False)
parser.add_option("-h", "--help", action="store_true", dest="help",
help="print help")
parser.add_option("-p", "--package", action="store", dest="pname",
help="package name")
parser.add_option("-s", "--stack-version", action="store", dest="sver",
help="stack verison number")
parser.add_option("-c", "--conf-version", action="store", dest="cver",
help="conf verison number", default=None)
(options, args) = parser.parse_args()
'''default conf mapping'''
confmap = {options.pname: "conf"}
'''conf mapping if the pkg name is hive-hcatalog'''
if options.pname == "hive-hcatalog":
confmap = {"hive-hcatalog": "etc/hcatalog",
"hive-webhcat": "etc/webhcat"}
if options.help or len(args) == 0:
printHelp()
elif not options.pname or not options.sver:
parser.error("Invalid option")
printHelp()
elif args[0] == 'create-conf-dir':
crtConfDir(options.pname, options.sver, options.cver)
elif args[0] == 'set-conf-dir':
setConfDir(options.pname, options.sver, options.cver)
elif args[0] == 'dry-run-set':
drsetConfDir(options.pname, options.sver)
elif args[0] == 'dry-run-create':
drcrtConfDir(options.pname, options.sver, options.cver)
else:
printHelp()