Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Tamir added MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamir Korem committed Jul 25, 2012
1 parent 0a61179 commit f61a439
Show file tree
Hide file tree
Showing 22 changed files with 1,407 additions and 0 deletions.
131 changes: 131 additions & 0 deletions services/mysql/README.md
@@ -0,0 +1,131 @@
# tomcat

**Status**: Tested
**Description**: MySQL
**Maintainer**: Cloudify
**Maintainer email**: cloudifysource@gigaspaces.com
**Contributors**: [tamirko](https://github.com/tamirko)
**Homepage**: [http://www.cloudifysource.org](http://www.cloudifysource.org)
**License**: Apache 2.0
**Build**: [2.1.1 GA](http://repository.cloudifysource.org/org/cloudifysource/2.1.1/gigaspaces-cloudify-2.1.1-ga-b1400.zip)
**Linux* sudoer permissions**: Mandatory
**Windows* Admin permissions**: Not required
**Release Date**: July 25th 2012


Tested on:
--------

* <strong>EC2</strong>: Ubuntu and CentOs
* <strong>OpenStack</strong>: CentOs
* <strong>Rackspace</strong>: CentOs



Synopsis
--------

This folder contains a service recipe for MySQL.

Its default port is 3306, but it can be modified in the mysql-service.properties.

You can inherit and extend this recipe very easily, just by changing the mysql-service.properties file, without changing even one line of code in the recipe.

This is achieved thanks to the following :

1. A Start Detection Query
In most of our recipes, we use something like : ServiceUtils.isPortOccupied(port).
In this case, it is usually NOT enough, because the port just means that the DB is up, but we also need the schema to be ready.
So I added a startDetectionQuery property (to the properties file) in which you can insert an SQL query.
The service instance is alive only if the port is occupied AND the startDetectionQuery result is true.

2. Post Start Actions :
In the properties file you can insert an array of postStart commands (as many commands as you want).
These post start commands will be invoked during the... postStart lifecycle event.
There are four types of postStart commands :
a) mysqladmin: for invoking any administrative command ( for example : creating a new DB )
b) mysql : for invoking any SQL statement: insert, update, grant permissions etc.
c) import : for importing a DB schema ( by providing a full URL of the zip file that contains the schema )
d) mysqldump : for creating a db dump (snapshot)

ActionType can be one of the four following: mysqladmin,mysql,mysqldump or import
Examples :

// In this case, dbName is a property which is defined in this properties file
// All the occurrences of MYSQLHOST in actionQuery, will be replaced with the private IP address on which this service instance resides
[
"actionType" : "mysqladmin",
"actionQuery" : "create" ,
"actionUser" : "root",
"actionDbName" : "${dbName}",
"debugMsg" : "Creating db - Name : ${dbName} ... "
] ,


// In this case, dbUser and dbPassW are properties which are defined in this properties file
// All the occurrences of MYSQLHOST in actionQuery, will be replaced with the private IP address on which this service instance resides
[
"actionType" : "mysql",
"actionQuery" : "\"CREATE USER '${dbUser}'@'localhost' IDENTIFIED BY '${dbPassW}';\"",
"actionUser" : "root",
"actionDbName" : "${dbName}",
"debugMsg" : "Creating db user ${dbUser} at localhost, passw ${dbPassW} in ${dbName} db... "
],

// In this case:
// dbName,currDBZip,currImportSql are properties which are defined in this properties file
// currDBZip is the local name of the zip file ( after download )
// currImportSql is the name of the sql file which is stored in currDBZip.
/ All the occurrences of REPLACE_WITH_DB_NAME in currImportSql, will be replaced with ${dbName}
[
"actionType" : "import",
"importedZip" : "${currDBZip}",
"importedFile" : "${currImportSql}",
"importedFileUrl" : "http://dropbox/1/222/mysql.zip",
"actionUser" : "root",
"actionDbName" : "${dbName}",
"debugMsg" : "Importing to ${dbName} ..."
]

// In this case:
// dbName is a property which is defined in this properties file.
// if actionDbName is an empty string, then --all-databases will be used
// actionArgs contain the flags that you want to use with this mysqldump command
// Do NOT database flags, because they will be set according to the actionDbName.
// So do NOT use the following : --all-databases,-A,--databases
// Do NOT -u flag flags, because it will be set according to the actionUser
[
"actionType" : "mysqldump",
"actionArgs" : "--add-drop-database -c --lock-all-tables -F",
"actionUser" : "root",
"actionDbName" : "${dbName}",
"dumpPrefix" : "myDumpFile_",
"debugMsg" : "Invoking mysqldump ..."
]




## Custom Commands

#mysqldump - This custom command enables users to create a database snapshot (mysqldump).

Usage : <strong>invoke mysql mysqldump actionUser dumpPrefix [dbName]</strong>
Example: <strong>invoke mysql mysqldump root myPrefix_ myDbName</strong>

#query - This custom command enables users to invoke an SQL statement.

Usage : <strong>invoke mysql query actionUser dbName query</strong>

Examples

1. If you want to update the users table in myDbName with the following statement :
<strong>update users set name='James' where uid=1</strong>
- then you need to run the following custom command :
<strong>invoke mysql query root myDbName \\"update users set name=\\'James\\' where uid=1\\"</strong>

2. If you want to insert a new user named Dan, into the users table in myDbName, and you need the following SQL statement:
<strong>INSERT INTO users VALUES (17,'Dan','hisPassword','hisemail@his.com',0)</strong>
- then you need to run the following custom command :
<strong>invoke mysql query root tamirDB \\"INSERT INTO users VALUES \\(17,\\'Dan\\',\\'hisPassword\\',\\'hisemail@his.com\\',0\\)\\"</strong>

75 changes: 75 additions & 0 deletions services/mysql/install.sh
@@ -0,0 +1,75 @@
#!/bin/bash

# args:
# $1 the error code of the last command (should be explicitly passed)
# $2 the message to print in case of an error
#
# an error message is printed and the script exists with the provided error code
function error_exit {
echo "$2 : error code: $1"
exit ${1}
}

function killMySqlProcess {
ps -ef | grep -iE "mysql" | grep -ivE "gigaspaces|GSC|GSA|grep"
if [ $? -eq 0 ] ; then
ps -ef | grep -iE "mysql" | grep -ivE "gigaspaces|GSC|GSA|grep" | awk '{print $2}' | xargs sudo kill -9
fi
}

export PATH=$PATH:/usr/sbin:/sbin:/usr/bin || error_exit $? "Failed on: export PATH=$PATH:/usr/sbin:/sbin"

# The existence of the usingYum file in the ext folder will later serve as a flag that "we" are on Red Hat or CentOS or Fedora or Amazon
echo "Using yum. Updating yum on one of the following : Red Hat, CentOS, Fedora, Amazon. " > usingYum
sudo yum -y -q update || error_exit $? "Failed on: sudo yum -y -q update"

echo "#1 Killing old mysql process if exists..."
killMySqlProcess

echo "Removing previous mysql installation if exists..."
sudo yum -y -q remove mysql mysql-server

# The following two statements are used since in some cases, there are leftovers after uninstall
echo "Removing old stuff if exists..."
sudo rm -rf /usr/lib/mysql* || error_exit $? "Failed on: sudo rm -rf /usr/lib/mysql*"
sudo rm -rf /var/lib/mysql* || error_exit $? "Failed on: sudo rm -rf /var/lib/mysql*"
sudo rm -rf /usr/share/mysql* || error_exit $? "Failed on: sudo rm -rf /usr/sharemysql*"
sudo rm -rf /usr/bin/mysql* || error_exit $? "Failed on: sudo rm -rf /usr/bin/mysql*"
sudo rm -rf /var/run/mysql* || error_exit $? "Failed on: sudo rm -rf /var/run/mysql*"
sudo rm -rf /var/bin/mysql* || error_exit $? "Failed on: sudo rm -rf /var/bin/mysql*"
sudo rm -rf /etc/mysql* || error_exit $? "Failed on: sudo rm -rf /etc/mysql*"
sudo rm -rf /etc/rc.d/init.d/mysql* || error_exit $? "Failed on: sudo rm -rf /etc/rc.d/init.d/mysql*"
sudo rm -rf /usr/libexec/mysql* || error_exit $? "Failed on: sudo rm -rf /usr/libexec/mysqld*"
sudo rm -rf /etc/my.cnf || error_exit $? "Failed on: sudo rm -rf /etc/my.cnf"
sudo rm -rf /var/log/mysql* || error_exit $? "Failed on: sudo rm -rf /var/log/mysql*"
#sudo rm -f /home/`whoami`/{.,}*mysql* || error_exit $? "Failed on: sudo rm -f /home/`whoami`/{.,}*mysql*"

echo "Using yum. Installing mysql on one of the following : Red Hat, CentOS, Fedora, Amazon"
sudo yum install -y -q mysql mysql-server || error_exit $? "Failed on: sudo yum install -y -q mysql mysql-server "
echo "Reinstalling mysql-libs ..."
sudo yum reinstall -y -q mysql-libs || error_exit $? "Failed on: sudo yum install -y -q mysql mysql-server "

echo "Killing old mysql process if exists b4 ending the installation..."
killMySqlProcess


myCnfPath=`sudo find / -name "my.cnf"`
if [ -f "${myCnfPath}" ] ; then
allZeroes="0.0.0.0"
bindcount=`grep -c "bind-address" $myCnfPath`
if [ $bindcount -eq 0 ] ; then
bindStr="bind-address=${allZeroes}"
mysqldStr="\[mysqld\]"
jointStr="${mysqldStr}\n${bindStr}"
echo "Adding ${bindStr} $myCnfPath ... "
sudo sed -i -e "s/$mysqldStr/$jointStr/g" $myCnfPath
else
orig127="127.0.0.1"
echo "Replacing $orig127 with $allZeroes in $myCnfPath ... "
sudo sed -i -e "s/$orig127/$allZeroes/g" $myCnfPath
fi
fi

echo "End of $0"


72 changes: 72 additions & 0 deletions services/mysql/installOnUbuntu.sh
@@ -0,0 +1,72 @@
#!/bin/bash -x

# args:
# $1 the error code of the last command (should be explicitly passed)
# $2 the message to print in case of an error
#
# an error message is printed and the script exists with the provided error code
function error_exit {
echo "$2 : error code: $1"
exit ${1}
}

function killMySqlProcess {
sudo service mysql stop
ps -ef | grep -iE "mysqld" | grep -ivE "gigaspaces|GSC|GSA|grep"
if [ $? -eq 0 ] ; then
ps -ef | grep -iE "mysqld" | grep -ivE "gigaspaces|GSC|GSA|grep" | awk '{print $2}' | xargs sudo kill -9
fi
}

export PATH=$PATH:/usr/sbin:/sbin:/usr/bin || error_exit $? "Failed on: export PATH=$PATH:/usr/sbin:/sbin"

if sudo grep -q -E '[^!]requiretty' /etc/sudoers; then
echo "Defaults:`whoami` !requiretty" | sudo tee /etc/sudoers.d/`whoami` >/dev/null
sudo chmod 0440 /etc/sudoers.d/`whoami`
fi

# The existence of the usingAptGet file in the ext folder will later serve as a flag that "we" are on Ubuntu or Debian or Mint
echo "Using apt-get. Updating apt-get on one of the following : Ubuntu, Debian, Mint" > usingAptGet
sudo apt-get -y -q update || error_exit $? "Failed on: sudo apt-get -y update"

echo "#1 Killing old mysql process if exists..."
killMySqlProcess

# Removing previous mysql installation if exists
echo "Purging previous mysql installation if exists..."
sudo apt-get -y -q purge mysql-client* mysql-server* mysql-common*

# The following two statements are used since in some cases, there are leftovers after uninstall
echo "Removing old stuff if exists..."
sudo rm -rf /etc/mysql || error_exit $? "Failed on: sudo rm -rf /etc/mysql"


echo "Using apt-get. Updating apt-get on one of the following : Ubuntu, Debian, Mint"
sudo DEBIAN_FRONTEND='noninteractive' apt-get -o Dpkg::Options::='--force-confnew' -q -y install mysql-server-core-5.1 mysql-server-5.1 mysql-client-core-5.1 mysql-client-5.1 mysql-common || error_exit $? "Failed on: sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -q mysql-server ... "

echo "Killing old mysql process if exists b4 ending the installation..."
killMySqlProcess

myCnfPath=`sudo find / -name "my.cnf"`
if [ -f "${myCnfPath}" ] ; then
allZeroes="0.0.0.0"
bindcount=`grep -c "bind-address" $myCnfPath`
if [ $bindcount -eq 0 ] ; then
bindStr="bind-address=${allZeroes}"
mysqldStr="\[mysqld\]"
jointStr="${mysqldStr}\n${bindStr}"
echo "Adding ${bindStr} $myCnfPath ... "
sudo sed -i -e "s/$mysqldStr/$jointStr/g" $myCnfPath
else
orig127="127.0.0.1"
echo "Replacing $orig127 with $allZeroes in $myCnfPath ... "
sudo sed -i -e "s/$orig127/$allZeroes/g" $myCnfPath
fi
fi

echo "End of $0"



#sudo service mysql stop
#sudo service mysql start
1 change: 1 addition & 0 deletions services/mysql/killAllMysql.bat
@@ -0,0 +1 @@
taskkill /t /im mysql* /f

0 comments on commit f61a439

Please sign in to comment.