Skip to content

Commit

Permalink
Merge pull request #1 from rightscale-cookbooks/white_13_07_acu111890…
Browse files Browse the repository at this point in the history
…_migrate_rs_mysql_application_cookbook

White 13 07 acu111890 migrate rs mysql application cookbook
  • Loading branch information
Lopaka Delp committed Jan 16, 2014
2 parents a17b564 + 07934ab commit 605e764
Show file tree
Hide file tree
Showing 23 changed files with 868 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ bin/*
VERSION
.kitchen/
.kitchen.local.yml

doc/
.yardoc/
40 changes: 30 additions & 10 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,43 @@ driver_config:

platforms:
- name: ubuntu-12.04
run_list:
- recipe[apt::default]
driver_config:
box: opscode-ubuntu-12.04
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box
- name: ubuntu-10.04
driver_config:
box: opscode-ubuntu-10.04
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box
customize:
cpus: 2
memory: 512
- name: centos-6.4
run_list:
- recipe[yum::epel]
driver_config:
box: opscode-centos-6.4
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-6.4_provisionerless.box
- name: centos-5.9
driver_config:
box: opscode-centos-5.9
box_url: https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_centos-5.9_provisionerless.box
customize:
cpus: 2
memory: 512

suites:
- name: default
run_list: ["recipe[rs-mysql]"]
attributes: {}
run_list:
- recipe[rs-mysql::server]
- recipe[fake::database_mysql]
attributes:
rs-mysql:
server_root_password: rootpass
server_usage: dedicated
application_username: appuser
application_password: apppass
application_database_name: app_test

- name: shared_server
run_list:
- recipe[rs-mysql::server]
- recipe[fake::database_mysql]
attributes:
rs-mysql:
server_root_password: rootpass
server_usage: shared
application_database_name: app_test
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: ruby
rvm:
- 2.0.0
- 1.9.3
before_script: bundle exec berks install
script: bundle exec strainer test --except kitchen
notifications:
irc:
channels: ["chat.freenode.net#rightscale"]
use_notice: true
skip_join: true
1 change: 1 addition & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--markup markdown
8 changes: 8 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
site :opscode

metadata

cookbook 'collectd', github: 'EfrainOlivares/chef-collectd', ref: 'ec50609ed6eb193e0411f30aced91befa571940f'

group :integration do
cookbook 'apt', '~> 2.3.0'
cookbook 'yum', '~> 2.4.2'
cookbook 'fake', path: './test/cookbooks/fake'
end
11 changes: 8 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ source 'https://rubygems.org'

gem 'berkshelf'
gem 'thor-foodcritic'
gem 'thor-scmversion'
gem 'test-kitchen', '~> 1.0.0.beta.3', :group => :integration
gem 'kitchen-vagrant', :group => :integration

group :integration do
gem 'test-kitchen', '~> 1.1.0'
gem 'kitchen-vagrant'
gem 'strainer', '~> 3.3.0'
gem 'chefspec', '~> 3.1.0'
gem 'travis-lint'
end
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
# rs-mysql cookbook

[![Build Status](https://travis-ci.org/rightscale-cookbooks/rs-mysql.png?branch=master)](https://travis-ci.org/rightscale-cookbooks/rs-mysql)

Sets up a MySQL server and tunes the attributes used in `my.cnf` based on available system memory and the server usage
type.

Github Repository: [https://github.com/rightscale-cookbooks/rs-mysql](https://github.com/rightscale-cookbooks/rs-mysql)

# Requirements

* Requires Chef 11 or higher
* Platform
* Ubuntu 12.04
* CentOS 6
* Cookbooks
* [marker](http://community.opscode.com/cookbooks/marker)
* [mysql](http://community.opscode.com/cookbooks/mysql)
* [collectd](http://community.opscode.com/cookbooks/collectd)

# Usage

To setup a MySQL server, place the `rs-mysql::server` recipe in the runlist.

# Attributes

* `node['rs-mysql']['server_usage']` - The server usage type. It should be either `'dedicated'` or `'shared'`. Default
is `'dedicated'`.

# Recipes

## `rs-mysql::server`

Installs the MySQL server and tunes the attributes used in the `my.cnf` based on the available system memory and the
server usage type. If the server usage type is `'dedicated'`, all resources in the system are dedicated to the MySQL
server and if the usage type is `'shared'`, only half of the resources are used for the MySQL server. This `'shared'`
usage will be used in building a LAMP stack where the same system is used to run both the MySQL server and the PHP
application server. This recipe also installs the collectd plugins for MySQL.

# Author

Author:: RightScale, Inc. (<[email protected]>)
4 changes: 4 additions & 0 deletions Strainerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
knife: bundle exec knife cookbook test $COOKBOOK
foodcritic: bundle exec foodcritic -f any $SANDBOX/$COOKBOOK
rspec: bundle exec rspec --color --format documentation
kitchen: bundle exec kitchen test --parallel
12 changes: 6 additions & 6 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Vagrant.configure("2") do |config|
config.vm.hostname = "rs-mysql-berkshelf"

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "RightImage_Ubuntu_12.04_x64_v13.5.0.1"
config.vm.box = "opscode-ubuntu-12.04"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "https://rightscale-vagrant.s3.amazonaws.com/virtualbox/ubuntu/12.04/RightImage_Ubuntu_12.04_x64_v13.5.0.1.box"
config.vm.box_url = "https://opscode-vm-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box"

# Assign this VM to a host-only network IP, allowing you to access it
# via the IP. Host-only networks can talk to the host machine as well as
Expand Down Expand Up @@ -52,9 +52,6 @@ Vagrant.configure("2") do |config|
# View the documentation for the provider you're using for more
# information on available options.

config.ssh.max_tries = 40
config.ssh.timeout = 120

# The path to the Berksfile to use with Vagrant Berkshelf
# config.berkshelf.berksfile_path = "./Berksfile"

Expand All @@ -66,6 +63,8 @@ Vagrant.configure("2") do |config|
# to exclusively install and copy to Vagrant's shelf.
# config.berkshelf.only = []

config.omnibus.chef_version = :latest

# An array of symbols representing groups of cookbook described in the Vagrantfile
# to skip installing and copying to Vagrant's shelf.
# config.berkshelf.except = []
Expand All @@ -80,7 +79,8 @@ Vagrant.configure("2") do |config|
}

chef.run_list = [
"recipe[rs-mysql::default]"
"recipe[apt::default]",
"recipe[rs-mysql::server]"
]
end
end
38 changes: 38 additions & 0 deletions attributes/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Cookbook Name:: rs-mysql
# Attribute:: server
#
# Copyright (C) 2013 RightScale, Inc.
#
# Licensed 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.
#

# The server usage method. It should either be 'dedicated' or 'shared'. In a 'dedicated' server, all
# resources are dedicated to MySQL. In a 'shared' server, MySQL utilizes only half of the server resources.
#
default['rs-mysql']['server_usage'] = 'dedicated'

# The MySQL server's root password
default['rs-mysql']['server_root_password'] = nil

# The MySQL database application username
default['rs-mysql']['application_username'] = nil

# The MySQL database application password
default['rs-mysql']['application_password'] = nil

# The previleges given to the application user
default['rs-mysql']['application_user_privileges'] = [:select, :update, :insert]

# The name of MySQL database
default['rs-mysql']['application_database_name'] = nil
122 changes: 122 additions & 0 deletions libraries/tuning.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#
# Cookbook Name:: rs-mysql
# Library:: tuning
#
# Copyright (C) 2013 RightScale, Inc.
#
# Licensed 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.
#

# The top level module for helpers in this cookbook.
module RsMysql
# The helper for calculating the MySQL tuning attributes.
module Tuning

# The constant multiplied with megabytes to obtain the value in gigabytes
GB = 1024

# Tunes the MySQL attributes based on the available memory and server usage type.
#
# @param node_tuning [Chef::Node] the chef node containing the MySQL tuning attributes
# @param memory [String, Integer] the total available system memory
# @param server_usage [String, Symbol] the server usage type. should be `'dedicated'` or `'shared'`
#
def self.tune_attributes(node_tuning, memory, server_usage)
factor = server_usage.to_s == 'dedicated' ? 1 : 0.5
memory = memory_in_megabytes(memory)
node_tuning['query_cache_size'] = (memory * factor * 0.01).to_i.to_s + 'M'
node_tuning['innodb_buffer_pool_size'] = (memory * factor * 0.8).to_i.to_s + 'M'

# Fixed parameters, common value for all memory categories.
#
node_tuning['thread_cache_size'] = (50 * factor).to_i
node_tuning['max_connections'] = (800 * factor).to_i
node_tuning['wait_timeout'] = (28800 * factor).to_i
node_tuning['net_read_timeout'] = (30 * factor).to_i
node_tuning['net_write_timeout'] = (30 * factor).to_i
node_tuning['back_log'] = (128 * factor).to_i
node_tuning['max_heap_table_size'] = (32 * factor).to_i.to_s + 'M'
node_tuning['read_buffer_size'] = (1 * factor).to_i.to_s + 'M'
node_tuning['read_rnd_buffer_size'] = (4 * factor).to_i.to_s + 'M'
node_tuning['long_query_time'] = 5

# Sets buffer sizes and InnoDB log properties. Overrides buffer sizes for really small servers.
#
if memory < 1 * GB
node_tuning['key_buffer_size'] = (16 * factor).to_i.to_s + 'M'
node_tuning['max_allowed_packet'] = (20 * factor).to_i.to_s + 'M'
node_tuning['innodb_log_file_size'] = (4 * factor).to_i.to_s + 'M'
node_tuning['innodb_log_buffer_size'] = (16 * factor).to_i.to_s + 'M'
else
node_tuning['key_buffer_size'] = (128 * factor).to_i.to_s + 'M'
node_tuning['max_allowed_packet'] = (128 * factor).to_i.to_s + 'M'
node_tuning['innodb_log_file_size'] = (64 * factor).to_i.to_s + 'M'
node_tuning['innodb_log_buffer_size'] = (8 * factor).to_i.to_s + 'M'
end

# Adjusts tunable values based on available system memory range.
# The memory ranges used are:
# < 3 GB
# 3 GB - 10 GB
# 10 GB - 25 GB
# 25 GB - 50 GB
# > 50 GB
#
if memory < 3 * GB
node_tuning['table_open_cache'] = (256 * factor).to_i
node_tuning['sort_buffer_size'] = (2 * factor).to_i.to_s + 'M'
node_tuning['innodb_additional_mem_pool_size'] = (50 * factor).to_i.to_s + 'M'
node_tuning['myisam_sort_buffer_size'] = (64 * factor).to_i.to_s + 'M'
elsif memory < 10 * GB
node_tuning['table_open_cache'] = (512 * factor).to_i
node_tuning['sort_buffer_size'] = (4 * factor).to_i.to_s + 'M'
node_tuning['innodb_additional_mem_pool_size'] = (200 * factor).to_i.to_s + 'M'
node_tuning['myisam_sort_buffer_size'] = (96 * factor).to_i.to_s + 'M'
elsif memory < 25 * GB
node_tuning['table_open_cache'] = (1024 * factor).to_i
node_tuning['sort_buffer_size'] = (8 * factor).to_i.to_s + 'M'
node_tuning['innodb_additional_mem_pool_size'] = (300 * factor).to_i.to_s + 'M'
node_tuning['myisam_sort_buffer_size'] = (128 * factor).to_i.to_s + 'M'
elsif memory < 50 * GB
node_tuning['table_open_cache'] = (2048 * factor).to_i
node_tuning['sort_buffer_size'] = (16 * factor).to_i.to_s + 'M'
node_tuning['innodb_additional_mem_pool_size'] = (400 * factor).to_i.to_s + 'M'
node_tuning['myisam_sort_buffer_size'] = (256 * factor).to_i.to_s + 'M'
else
node_tuning['table_open_cache'] = (4096 * factor).to_i
node_tuning['sort_buffer_size'] = (32 * factor).to_i.to_s + 'M'
node_tuning['innodb_additional_mem_pool_size'] = (500 * factor).to_i.to_s + 'M'
node_tuning['myisam_sort_buffer_size'] = (512 * factor).to_i.to_s + 'M'
end
end

private

# Given the memory in either kilobytes as a String or in bytes as an Integer (the formats Ohai returns),
# this method will return the memory in megabytes.
#
# @param memory [String, Integer] memory in kilobytes or bytes
#
# @return [Numeric] memory in megabytes
#
def self.memory_in_megabytes(memory)
# If ohai returns the total memory in String, it is assumed to be in kB.
if memory.is_a?(String) && memory =~ /\d+kB/i
memory.to_i / 1024
# If it returns an integer, it is assumed to be in bytes.
else
memory / (1024 * 1024)
end
end
end
end
Loading

0 comments on commit 605e764

Please sign in to comment.