AMBARI-21581 - Replace Hard Coded conf-select Structures (jonathanhurley)
[ambari.git] / ambari-server / src / main / resources / common-services / DRUID / 0.9.2 / package / scripts / superset.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 import os
20
21 from resource_management import Script
22 from resource_management.core.logger import Logger
23 from resource_management.core.resources import File
24 from resource_management.core.resources.system import Directory
25 from resource_management.core.resources.system import Execute
26 from resource_management.core.source import InlineTemplate
27 from resource_management.core.source import Template
28 from resource_management.libraries.functions import StackFeature
29 from resource_management.libraries.functions import stack_select
30 from resource_management.libraries.functions.check_process_status import check_process_status
31 from resource_management.libraries.functions.format import format
32 from resource_management.libraries.functions.show_logs import show_logs
33 from resource_management.libraries.functions.stack_features import check_stack_feature
34 from resource_management.libraries.resources.properties_file import PropertiesFile
35
36 class Superset(Script):
37
38 def install(self, env):
39 self.install_packages(env)
40
41 def configure(self, env, upgrade_type=None):
42 import params
43 Directory(
44 [params.superset_pid_dir, params.superset_log_dir, params.superset_config_dir, params.superset_home_dir],
45 mode=0755,
46 cd_access='a',
47 owner=params.druid_user,
48 group=params.user_group,
49 create_parents=True,
50 recursive_ownership=True
51 )
52
53 File(format("{params.superset_config_dir}/superset-env.sh"),
54 mode=0755,
55 owner=params.druid_user,
56 group=params.user_group,
57 content=InlineTemplate(params.superset_env_sh_template)
58 )
59
60 File(os.path.join(params.superset_bin_dir, 'superset.sh'),
61 owner=params.druid_user,
62 group=params.user_group,
63 mode=0755,
64 content=Template("superset.sh")
65 )
66 superset_config = mutable_config_dict(params.config["configurations"]["druid-superset"])
67
68 if params.superset_db_uri:
69 superset_config["SQLALCHEMY_DATABASE_URI"] = params.superset_db_uri
70
71 PropertiesFile("superset_config.py",
72 dir=params.superset_config_dir,
73 properties=quote_string_values(superset_config),
74 owner=params.druid_user,
75 group=params.user_group
76 )
77
78 # Initialize DB and create admin user.
79 Execute(format("source {params.superset_config_dir}/superset-env.sh ; {params.superset_bin_dir}/superset db upgrade"),
80 user=params.druid_user)
81 Execute(format("source {params.superset_config_dir}/superset-env.sh ; {params.superset_bin_dir}/fabmanager create-admin --app superset --username '{params.superset_admin_user}' --password '{params.superset_admin_password!p}' --firstname '{params.superset_admin_firstname}' --lastname '{params.superset_admin_lastname}' --email '{params.superset_admin_email}'"),
82 user=params.druid_user)
83 Execute(format("source {params.superset_config_dir}/superset-env.sh ; {params.superset_bin_dir}/superset init"),
84 user=params.druid_user)
85
86 # Configure Druid Cluster in superset DB
87 Execute(format("source {params.superset_config_dir}/superset-env.sh ; {params.superset_bin_dir}/superset configure_druid_cluster --name druid-ambari --coordinator-host {params.druid_coordinator_host} --coordinator-port {params.druid_coordinator_port} --broker-host {params.druid_router_host} --broker-port {params.druid_router_port} --coordinator-endpoint druid/coordinator/v1/metadata --broker-endpoint druid/v2"),
88 user=params.druid_user)
89
90 def pre_upgrade_restart(self, env, upgrade_type=None):
91 Logger.info("Executing druid-superset Upgrade pre-restart")
92 import params
93
94 env.set_params(params)
95
96 if params.stack_version and check_stack_feature(StackFeature.ROLLING_UPGRADE, params.stack_version):
97 stack_select.select_packages(params.version)
98
99 def start(self, env, upgrade_type=None):
100 import params
101 env.set_params(params)
102 self.configure(env, upgrade_type=upgrade_type)
103 daemon_cmd = self.get_daemon_cmd(params, "start")
104 try:
105 Execute(daemon_cmd,
106 user=params.druid_user
107 )
108 except:
109 show_logs(params.druid_log_dir, params.druid_user)
110 raise
111
112 def stop(self, env, upgrade_type=None):
113 import params
114 env.set_params(params)
115 self.configure(env, upgrade_type=upgrade_type)
116 daemon_cmd = self.get_daemon_cmd(params, "stop")
117 try:
118 Execute(daemon_cmd,
119 user=params.druid_user
120 )
121 except:
122 show_logs(params.druid_log_dir, params.druid_user)
123 raise
124
125 def status(self, env):
126 import status_params
127 env.set_params(status_params)
128 pid_file = status_params.superset_pid_dir + '/superset.pid'
129 check_process_status(pid_file)
130
131 def get_log_folder(self):
132 import params
133 return params.druid_log_dir
134
135 def get_user(self):
136 import params
137 return params.druid_user
138
139 def get_daemon_cmd(self, params=None, command=None):
140 return format('source {params.superset_config_dir}/superset-env.sh ; {params.superset_bin_dir}/superset.sh {command}')
141
142 def mutable_config_dict(config):
143 rv = {}
144 for key, value in config.iteritems():
145 rv[key] = value
146 return rv
147
148 def quote_string_values(config):
149 rv = {}
150 for key, value in config.iteritems():
151 rv[key] = quote_string_value(value)
152 return rv
153
154 def quote_string_value(value):
155 if value.lower() == "true" or value.lower() == "false" or value.isdigit():
156 return value
157 else:
158 return "'{0}'".format(value)
159
160
161
162 if __name__ == "__main__":
163 Superset().execute()