Repository: stratos Updated Branches: refs/heads/4.0.0-grouping 961e58483 -> fde396a03
add mysql-python puppet changes Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/82701ff9 Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/82701ff9 Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/82701ff9 Branch: refs/heads/4.0.0-grouping Commit: 82701ff975c47a4339315d6170859e2facb0557f Parents: a456b4e Author: Udara Liyanage <[email protected]> Authored: Mon Oct 13 09:03:07 2014 +0530 Committer: Udara Liyanage <[email protected]> Committed: Mon Oct 13 09:03:07 2014 +0530 ---------------------------------------------------------------------- tools/puppet3/modules/mysql/manifests/init.pp | 10 +++ .../mysql/templates/instance-activated.sh.erb | 28 ++++++ .../mysql/templates/instance-started.sh.erb | 43 +++++++++ .../templates/mysql_root_password_setter.sh.erb | 16 ++++ .../mysql/templates/publish-metadata.py.erb | 95 ++++++++++++++++++++ 5 files changed, 192 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/82701ff9/tools/puppet3/modules/mysql/manifests/init.pp ---------------------------------------------------------------------- diff --git a/tools/puppet3/modules/mysql/manifests/init.pp b/tools/puppet3/modules/mysql/manifests/init.pp index 070862b..be6c77f 100644 --- a/tools/puppet3/modules/mysql/manifests/init.pp +++ b/tools/puppet3/modules/mysql/manifests/init.pp @@ -17,6 +17,14 @@ class mysql{ + require java + + $custom_agent_templates = ['extensions/instance-started.sh','extensions/instance-activated.sh', 'extensions/publish-metadata.py', 'extensions/mysql_root_password_setter.sh'] + class {'agent': + custom_templates => $custom_agent_templates, + module=>'mysql' + } + if $stratos_mysql_password { $root_password = $stratos_mysql_password } @@ -96,4 +104,6 @@ class mysql{ require => File['/etc/apache2/sites-enabled/000-default']; } + # install stratos_base before java before mysql before agent + Class['stratos_base'] -> Class['java'] -> Class['agent'] -> Class['mysql'] } http://git-wip-us.apache.org/repos/asf/stratos/blob/82701ff9/tools/puppet3/modules/mysql/templates/instance-activated.sh.erb ---------------------------------------------------------------------- diff --git a/tools/puppet3/modules/mysql/templates/instance-activated.sh.erb b/tools/puppet3/modules/mysql/templates/instance-activated.sh.erb new file mode 100755 index 0000000..7bd6ef6 --- /dev/null +++ b/tools/puppet3/modules/mysql/templates/instance-activated.sh.erb @@ -0,0 +1,28 @@ +#!/bin/bash +# -------------------------------------------------------------- +# +# 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. +# +# -------------------------------------------------------------- +# This extension script will be executed once the instance is started. +# -------------------------------------------------------------- +# + +log=/var/log/apache-stratos/cartridge-agent-extensions.log +echo `date`": Instance Activated Event: " | tee -a $log +python publish-metadata.py http://git-wip-us.apache.org/repos/asf/stratos/blob/82701ff9/tools/puppet3/modules/mysql/templates/instance-started.sh.erb ---------------------------------------------------------------------- diff --git a/tools/puppet3/modules/mysql/templates/instance-started.sh.erb b/tools/puppet3/modules/mysql/templates/instance-started.sh.erb new file mode 100755 index 0000000..1babac5 --- /dev/null +++ b/tools/puppet3/modules/mysql/templates/instance-started.sh.erb @@ -0,0 +1,43 @@ +#!/bin/bash +# -------------------------------------------------------------- +# +# 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. +# +# -------------------------------------------------------------- +# This extension script will be executed once the instance is started. +# -------------------------------------------------------------- +# + +log=/var/log/apache-stratos/cartridge-agent-extensions.log +echo `date`": Instance Started Event: " | tee -a $log + +export PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin' + +PASSWD=`facter -p stratos_mysql_password` + +/etc/init.d/mysql start + +# Set mysql password +mysqladmin -uroot password "$PASSWD" + +# Remove other users +mysql -uroot -p"$PASSWD" -Bse "DELETE from mysql.user WHERE password=''" + +# Set root user with remote access +mysql -uroot -p"$PASSWD" -Bse "CREATE USER 'root'@'%' IDENTIFIED BY '${PASSWD}'" + http://git-wip-us.apache.org/repos/asf/stratos/blob/82701ff9/tools/puppet3/modules/mysql/templates/mysql_root_password_setter.sh.erb ---------------------------------------------------------------------- diff --git a/tools/puppet3/modules/mysql/templates/mysql_root_password_setter.sh.erb b/tools/puppet3/modules/mysql/templates/mysql_root_password_setter.sh.erb new file mode 100755 index 0000000..427e31e --- /dev/null +++ b/tools/puppet3/modules/mysql/templates/mysql_root_password_setter.sh.erb @@ -0,0 +1,16 @@ +#!/bin/bash + +export PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin' + +PASSWD=$1 + +service mysql start +echo $PASSWD> /tmp/udara +# Set mysql password +mysqladmin -uroot password "$PASSWD" + +# Remove other users +mysql -uroot -p"$PASSWD" -Bse "DELETE from mysql.user WHERE password=''" + +# Set root user with remote access +mysql -uroot -p"$PASSWD" -Bse "CREATE USER 'root'@'%' IDENTIFIED BY '${PASSWD}'" http://git-wip-us.apache.org/repos/asf/stratos/blob/82701ff9/tools/puppet3/modules/mysql/templates/publish-metadata.py.erb ---------------------------------------------------------------------- diff --git a/tools/puppet3/modules/mysql/templates/publish-metadata.py.erb b/tools/puppet3/modules/mysql/templates/publish-metadata.py.erb new file mode 100755 index 0000000..2a4f734 --- /dev/null +++ b/tools/puppet3/modules/mysql/templates/publish-metadata.py.erb @@ -0,0 +1,95 @@ +__author__ = 'udara' + +import urllib2 +from urllib2 import URLError, HTTPError +import json +import base64 +import logging +import os,subprocess +from subprocess import Popen, PIPE +import random,string + +logging.basicConfig(filename='publish_metadata.log',level=logging.DEBUG,format='%(asctime)s: [%(levelname)s] %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') + +def get_launch_param_file_location(): + launch_params_file = os.path.abspath(os.path.dirname(__file__)).split("extensions")[0] + "payload/launch-params" + logging.debug("Launch param file location " + launch_params_file) + return launch_params_file + +def get_paylod_property(name): + logging.debug("[property " + name + " = " + properties[name] + "]") + return properties[name] + + +def get_metadataserviceurl(): + return properties['METADATASERVICE_URL'] + +def do_post(url, data): + req = urllib2.Request(url) + base64string = base64.encodestring('%s:%s' % ("admin", "admin")).replace('\n', '') + req.add_header("Authorization", "Basic %s" % base64string) + req.add_header('Content-Type', 'application/json') + + try: + logging.info('sending to ' + url) + logging.info('sent data ' + json.dumps(data)) + response = urllib2.urlopen(req, json.dumps(data)) + except HTTPError as e: + logging.error('The server couldn\'t fulfill the request.') + logging.error('Error code .' + e.code) + except URLError as e: + print 'We failed to reach a server.' + logging.error('We failed to reach a server.') + logging.error('Reason: ' + e.reason) + +def generate_mysql_password(size=6, chars=string.ascii_uppercase + string.digits): + return ''.join(random.choice(chars) for _ in range(size)) + +def set_mysql_password(password): + command = "./mysql_root_password_setter.sh " + password + logging.debug(" Setting mysql password [" + command + "]") + p = subprocess.Popen(command , stdout=PIPE, stderr=PIPE, shell=True) + stdout_data, stderr_data = p.communicate() + if p.returncode != 0: + logging.error(" failed, status code %s stdout %r stderr %r" % ( p.returncode, stdout_data, stderr_data)) + raise RuntimeError(" failed, status code %s stdout %r stderr %r" % ( p.returncode, stdout_data, stderr_data)) + print(stdout_data) + +param_file = get_launch_param_file_location() +print param_file +try: + metadata_file = open(param_file, "r") +except IOError: + logging.error( 'Cannot open' + param_file) + raise RuntimeError("Could not open payload params, halting....") + +metadata_payload_content = metadata_file.read() +logging.debug("Payload : " + metadata_payload_content) +properties = {} +for param in metadata_payload_content.split(","): + if param.strip() != "": + param_value = param.strip().split("=") + properties[param_value[0]] = param_value[1] + +logging.debug("Payload properties : ") +logging.debug(properties) + +my_alias = get_paylod_property('CARTRIDGE_ALIAS') + +my_ip = urllib2.urlopen('http://ip.42.pl/raw').read() +my_username = "root" +my_password = generate_mysql_password() +set_mysql_password(my_password) +app_id= get_paylod_property('APP_ID') + +resource_url = get_metadataserviceurl()+ "/stratosmetadataservice/application/" + app_id + "/cluster/" + my_alias + "/property" +data = {"key":"MYSQL_IP","values":my_ip} +do_post(resource_url, data) +data = {"key":"MYSQL_PASS","values":my_password} +do_post(resource_url, data) + +data = {"key":"MYSQL_UNAME","values":my_username} +do_post(resource_url, data) + +logging.info("************************") +
