AMBARI-21581 - Replace Hard Coded conf-select Structures (jonathanhurley)
[ambari.git] / ambari-server / src / main / resources / common-services / HDFS / 3.0.0.3.0 / package / scripts / zkfc_slave.py
1 """
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 """
19 # this is needed to avoid a circular dependency since utils.py calls this class
20 import utils
21 from hdfs import hdfs
22
23 from ambari_commons import OSConst
24 from ambari_commons.os_family_impl import OsFamilyImpl
25 from resource_management.core.logger import Logger
26 from resource_management.core.exceptions import Fail
27 from resource_management.core.resources.system import Directory
28 from resource_management.core.resources.service import Service
29 from resource_management.core import shell
30 from resource_management.libraries.functions import stack_select
31 from resource_management.libraries.functions.constants import StackFeature
32 from resource_management.libraries.functions.check_process_status import check_process_status
33 from resource_management.libraries.functions.security_commons import build_expectations
34 from resource_management.libraries.functions.security_commons import cached_kinit_executor
35 from resource_management.libraries.functions.security_commons import get_params_from_filesystem
36 from resource_management.libraries.functions.security_commons import validate_security_config_properties
37 from resource_management.libraries.functions.security_commons import FILE_TYPE_XML
38 from resource_management.libraries.functions.stack_features import check_stack_feature
39 from resource_management.libraries.script import Script
40 from resource_management.core.resources.zkmigrator import ZkMigrator
41
42 class ZkfcSlave(Script):
43 def install(self, env):
44 import params
45 env.set_params(params)
46 self.install_packages(env)
47
48 def configure(env):
49 ZkfcSlave.configure_static(env)
50
51 @staticmethod
52 def configure_static(env):
53 import params
54 env.set_params(params)
55 hdfs("zkfc_slave")
56 utils.set_up_zkfc_security(params)
57 pass
58
59 @OsFamilyImpl(os_family=OsFamilyImpl.DEFAULT)
60 class ZkfcSlaveDefault(ZkfcSlave):
61
62 def start(self, env, upgrade_type=None):
63 ZkfcSlaveDefault.start_static(env, upgrade_type)
64
65 @staticmethod
66 def start_static(env, upgrade_type=None):
67 import params
68
69 env.set_params(params)
70 ZkfcSlave.configure_static(env)
71 Directory(params.hadoop_pid_dir_prefix,
72 mode=0755,
73 owner=params.hdfs_user,
74 group=params.user_group
75 )
76
77 # format the znode for this HA setup
78 # only run this format command if the active namenode hostname is set
79 # The Ambari UI HA Wizard prompts the user to run this command
80 # manually, so this guarantees it is only run in the Blueprints case
81 if params.dfs_ha_enabled and \
82 params.dfs_ha_namenode_active is not None:
83 success = initialize_ha_zookeeper(params)
84 if not success:
85 raise Fail("Could not initialize HA state in zookeeper")
86
87 utils.service(
88 action="start", name="zkfc", user=params.hdfs_user, create_pid_dir=True,
89 create_log_dir=True
90 )
91
92 def stop(self, env, upgrade_type=None):
93 ZkfcSlaveDefault.stop_static(env, upgrade_type)
94
95 @staticmethod
96 def stop_static(env, upgrade_type=None):
97 import params
98
99 env.set_params(params)
100 utils.service(
101 action="stop", name="zkfc", user=params.hdfs_user, create_pid_dir=True,
102 create_log_dir=True
103 )
104
105
106 def status(self, env):
107 ZkfcSlaveDefault.status_static(env)
108
109 @staticmethod
110 def status_static(env):
111 import status_params
112 env.set_params(status_params)
113 check_process_status(status_params.zkfc_pid_file)
114
115 def disable_security(self, env):
116 import params
117
118 if not params.stack_supports_zk_security:
119 return
120
121 zkmigrator = ZkMigrator(params.ha_zookeeper_quorum, params.java_exec, params.java_home, params.jaas_file, params.hdfs_user)
122 zkmigrator.set_acls(params.zk_namespace if params.zk_namespace.startswith('/') else '/' + params.zk_namespace, 'world:anyone:crdwa')
123
124
125 def get_log_folder(self):
126 import params
127 return params.hdfs_log_dir
128
129 def get_user(self):
130 import params
131 return params.hdfs_user
132
133 def get_pid_files(self):
134 import status_params
135 return [status_params.zkfc_pid_file]
136
137 def pre_upgrade_restart(self, env, upgrade_type=None):
138 Logger.info("Executing Stack Upgrade pre-restart")
139 import params
140 env.set_params(params)
141 if params.version and check_stack_feature(StackFeature.ZKFC_VERSION_ADVERTISED, params.version) \
142 and check_stack_feature(StackFeature.ROLLING_UPGRADE, params.version):
143 stack_select.select_packages(params.version)
144
145 def initialize_ha_zookeeper(params):
146 try:
147 iterations = 10
148 formatZK_cmd = "hdfs zkfc -formatZK -nonInteractive"
149 Logger.info("Initialize HA state in ZooKeeper: %s" % (formatZK_cmd))
150 for i in range(iterations):
151 Logger.info('Try %d out of %d' % (i+1, iterations))
152 code, out = shell.call(formatZK_cmd, logoutput=False, user=params.hdfs_user)
153 if code == 0:
154 Logger.info("HA state initialized in ZooKeeper successfully")
155 return True
156 elif code == 2:
157 Logger.info("HA state already initialized in ZooKeeper")
158 return True
159 else:
160 Logger.warning('HA state initialization in ZooKeeper failed with %d error code. Will retry' % (code))
161 except Exception as ex:
162 Logger.error('HA state initialization in ZooKeeper threw an exception. Reason %s' %(str(ex)))
163 return False
164
165 @OsFamilyImpl(os_family=OSConst.WINSRV_FAMILY)
166 class ZkfcSlaveWindows(ZkfcSlave):
167 def start(self, env):
168 import params
169 self.configure(env)
170 Service(params.zkfc_win_service_name, action="start")
171
172 def stop(self, env):
173 import params
174 Service(params.zkfc_win_service_name, action="stop")
175
176 def status(self, env):
177 import status_params
178 from resource_management.libraries.functions.windows_service_utils import check_windows_service_status
179
180 env.set_params(status_params)
181 check_windows_service_status(status_params.zkfc_win_service_name)
182
183 if __name__ == "__main__":
184 ZkfcSlave().execute()