diff --git a/sahara/plugins/cdh/db_helper.py b/sahara/plugins/cdh/db_helper.py index 320f4cb4..4c85f846 100644 --- a/sahara/plugins/cdh/db_helper.py +++ b/sahara/plugins/cdh/db_helper.py @@ -23,15 +23,30 @@ from sahara import context conductor = conductor.API -def get_cm_password(cluster): +def get_password_from_db(cluster, pwname): ctx = context.ctx() cluster = conductor.cluster_get(ctx, cluster.id) - passwd = cluster.extra.get('cm_password') if cluster.extra else None + passwd = cluster.extra.get(pwname) if cluster.extra else None if passwd: return passwd passwd = six.text_type(uuid.uuid4()) extra = cluster.extra.to_dict() if cluster.extra else {} - extra['cm_password'] = passwd + extra[pwname] = passwd cluster = conductor.cluster_update(ctx, cluster, {'extra': extra}) return passwd + + +def get_cm_password(cluster): + return get_password_from_db(cluster, 'cm_password') + + +def remote_execute_db_script(remote, script_content): + script_name = 'script_to_exec.sql' + remote.write_file_to(script_name, script_content) + + psql_cmd = ('PGPASSWORD=$(sudo head -1 /var/lib/cloudera-scm-server-db' + '/data/generated_password.txt) psql -U cloudera-scm ' + '-h localhost -p 7432 -d scm -f %s') % script_name + remote.execute_command(psql_cmd) + remote.execute_command('rm %s' % script_name) diff --git a/sahara/plugins/cdh/v5/db_helper.py b/sahara/plugins/cdh/v5/db_helper.py index 6fb4ec3f..d66a3652 100644 --- a/sahara/plugins/cdh/v5/db_helper.py +++ b/sahara/plugins/cdh/v5/db_helper.py @@ -13,29 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import uuid - -import six - -from sahara import conductor -from sahara import context +from sahara.plugins.cdh import db_helper as dh from sahara.utils import files -conductor = conductor.API - def get_hive_db_password(cluster): - ctx = context.ctx() - cluster = conductor.cluster_get(ctx, cluster.id) - passwd = cluster.extra.get('hive_db_password') if cluster.extra else None - if passwd: - return passwd - - passwd = six.text_type(uuid.uuid4()) - extra = cluster.extra.to_dict() if cluster.extra else {} - extra['hive_db_password'] = passwd - cluster = conductor.cluster_update(ctx, cluster, {'extra': extra}) - return passwd + return dh.get_password_from_db(cluster, 'hive_db_password') def create_hive_database(cluster, remote): @@ -43,11 +26,4 @@ def create_hive_database(cluster, remote): create_db_script = files.get_file_text( 'plugins/cdh/v5/resources/create_hive_db.sql') create_db_script = create_db_script % db_password - script_name = 'create_hive_db.sql' - remote.write_file_to(script_name, create_db_script) - - psql_cmd = ('PGPASSWORD=$(sudo head -1 /var/lib/cloudera-scm-server-db' - '/data/generated_password.txt) psql -U cloudera-scm ' - '-h localhost -p 7432 -d scm -f %s') % script_name - remote.execute_command(psql_cmd) - remote.execute_command('rm %s' % script_name) + dh.remote_execute_db_script(remote, create_db_script) diff --git a/sahara/plugins/cdh/v5_3_0/db_helper.py b/sahara/plugins/cdh/v5_3_0/db_helper.py index ae719b5a..c4bfccf7 100644 --- a/sahara/plugins/cdh/v5_3_0/db_helper.py +++ b/sahara/plugins/cdh/v5_3_0/db_helper.py @@ -13,29 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import uuid - -import six - -from sahara import conductor -from sahara import context +from sahara.plugins.cdh import db_helper as dh from sahara.utils import files -conductor = conductor.API - def get_hive_db_password(cluster): - ctx = context.ctx() - cluster = conductor.cluster_get(ctx, cluster.id) - passwd = cluster.extra.get('hive_db_password') if cluster.extra else None - if passwd: - return passwd + return dh.get_password_from_db(cluster, 'hive_db_password') - passwd = six.text_type(uuid.uuid4()) - extra = cluster.extra.to_dict() if cluster.extra else {} - extra['hive_db_password'] = passwd - cluster = conductor.cluster_update(ctx, cluster, {'extra': extra}) - return passwd + +def get_sentry_db_password(cluster): + return dh.get_password_from_db(cluster, 'sentry_db_password') def create_hive_database(cluster, remote): @@ -43,28 +30,7 @@ def create_hive_database(cluster, remote): create_db_script = files.get_file_text( 'plugins/cdh/v5_3_0/resources/create_hive_db.sql') create_db_script = create_db_script % db_password - script_name = 'create_hive_db.sql' - remote.write_file_to(script_name, create_db_script) - - psql_cmd = ('PGPASSWORD=$(sudo head -1 /var/lib/cloudera-scm-server-db' - '/data/generated_password.txt) psql -U cloudera-scm ' - '-h localhost -p 7432 -d scm -f %s') % script_name - remote.execute_command(psql_cmd) - remote.execute_command('rm %s' % script_name) - - -def get_sentry_db_password(cluster): - ctx = context.ctx() - cluster = conductor.cluster_get(ctx, cluster.id) - passwd = cluster.extra.get('sentry_db_password') if cluster.extra else None - if passwd: - return passwd - - passwd = six.text_type(uuid.uuid4()) - extra = cluster.extra.to_dict() if cluster.extra else {} - extra['sentry_db_password'] = passwd - cluster = conductor.cluster_update(ctx, cluster, {'extra': extra}) - return passwd + dh.remote_execute_db_script(remote, create_db_script) def create_sentry_database(cluster, remote): @@ -72,11 +38,4 @@ def create_sentry_database(cluster, remote): create_db_script = files.get_file_text( 'plugins/cdh/v5_3_0/resources/create_sentry_db.sql') create_db_script = create_db_script % db_password - script_name = 'create_sentry_db.sql' - remote.write_file_to(script_name, create_db_script) - - psql_cmd = ('PGPASSWORD=$(sudo head -1 /var/lib/cloudera-scm-server-db' - '/data/generated_password.txt) psql -U cloudera-scm ' - '-h localhost -p 7432 -d scm -f %s') % script_name - remote.execute_command(psql_cmd) - remote.execute_command('rm %s' % script_name) + dh.remote_execute_db_script(remote, create_db_script)