-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhadoop-xml-conf.sh
148 lines (122 loc) · 3.2 KB
/
hadoop-xml-conf.sh
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
#!/bin/bash
#
# Utility functions for processing Hadoop 2 XML configuration files.
#
# Depends on Python built-in XML processing and libxml2 for formatting
#
installed=false
if [ -f /etc/profile.d/hadoop.sh ]; then
source /etc/profile.d/hadoop.sh
source $HADOOP_HOME/etc/hadoop/hadoop-env.sh
installed=true
fi
create_config()
{
local filename=
case $1 in
'') echo $"$0: Usage: create_config --file"
return 1;;
--file)
filename=$2
;;
esac
echo " create_config $filename"
python - <<END
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
conf = Element('configuration')
conf_file = open("$filename",'w')
conf_file.write(ElementTree.tostring(conf))
conf_file.close()
END
write_file $filename
}
put_config()
{
local filename= property= value=
while [ "$1" != "" ]; do
case $1 in
'') echo $"$0: Usage: put_config --file --property --value"
return 1;;
--file)
filename=$2
shift 2
;;
--property)
property=$2
shift 2
;;
--value)
value=$2
shift 2
;;
esac
done
echo " put_config $filename"
python - <<END
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
def putconfig(root, name, value):
for existing_prop in root.getchildren():
if existing_prop.find('name').text == name:
root.remove(existing_prop)
break
property = SubElement(root, 'property')
name_elem = SubElement(property, 'name')
name_elem.text = name
value_elem = SubElement(property, 'value')
value_elem.text = value
path = ''
if "$installed" == 'true':
path = "$HADOOP_CONF_DIR" + '/'
conf = ElementTree.parse(path + "$filename").getroot()
putconfig(root = conf, name = "$property", value = "$value")
conf_file = open("$filename",'w')
conf_file.write(ElementTree.tostring(conf))
conf_file.close()
END
write_file $filename
}
del_config()
{
local filename= property=
while [ "$1" != "" ]; do
case $1 in
'') echo $"$0: Usage: del_config --file --property"
return 1;;
--file)
filename=$2
shift 2
;;
--property)
property=$2
shift 2
;;
esac
done
python - <<END
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
def delconfig(root, name):
for existing_prop in root.getchildren():
if existing_prop.find('name').text == name:
root.remove(existing_prop)
break
path = ''
if "$installed" == 'true':
path = "$HADOOP_CONF_DIR" + '/'
conf = ElementTree.parse(path + "$filename").getroot()
delconfig(root = conf, name = "$property")
conf_file = open("$filename",'w')
conf_file.write(ElementTree.tostring(conf))
conf_file.close()
END
write_file $filename
}
write_file()
{
local file=$1
xmllint --format "$file" > "$file".pp && mv "$file".pp "$file"
}