Initial Chef repository

This commit is contained in:
Greg Karékinian
2015-07-21 19:45:23 +02:00
parent 7e5401fc71
commit ee4079fa85
1151 changed files with 185163 additions and 0 deletions

View File

@@ -0,0 +1,562 @@
mysql Cookbook CHANGELOG
========================
v6.0.22 (2015-05-07)
--------------------
- Debian 8 (Jessie) support
v6.0.21 (2015-04-08)
--------------------
- Fix to Upstart prestart script when using custom socket
- Adding --explicit_defaults_for_timestamp mysql_install_db_cmd for
5.6 and above
v6.0.20 (2015-03-27)
--------------------
- #318 - Fixing Upstart pre-start script to handle custom socket paths
v6.0.19 (2015-03-25)
--------------------
- Adding support for Amazon Linux 2015.03
v6.0.18 (2015-03-24)
--------------------
- Adding support for 5.6 and 5.7 packages from dotdeb repos on Debian 7
v6.0.17 (2015-03-13)
--------------------
- Updated for MySQL 5.7.6.
- Handing removal of mysql_install_db and mysqld_safe
v6.0.16 (2015-03-10)
--------------------
- Moved --defaults-file as first option to mysql_install_db_script
v6.0.15 (2015-02-26)
--------------------
- Updating docker detection fix to pass specs
v6.0.14 (2015-02-26)
--------------------
- Fixed debian system service :disable action. Now survives reboot
- Fixing centos-7 instance :enable action. Now survives
- Not applying Apparmor policy if running in a Docker container
v6.0.13 (2015-02-15)
--------------------
- Adding support for special characters in initial_root_password
- Fixing failure status bug in sysvinit script
v6.0.12 (2015-02-30)
--------------------
- No changes. Released a 6.0.11 that was identical to 6.0.10.
Git before coffee.
v6.0.11 (2015-02-30)
--------------------
- Adding support for configurable socket files
v6.0.10 (2015-01-19)
------------------
- Fix #282 - Fixing up data_dir template variable
v6.0.9 (2015-01-19)
------------------
- Fix #282 - undefined method `parsed_data_dir' bug
v6.0.8 (2015-01-19)
------------------
- Refactoring helper methods out of resource classes
v6.0.7 (2015-01-14)
------------------
- Fixing timing issue with Upstart provider :restart and :reload
actions where service returns before being available
v6.0.6 (2014-12-26)
------------------
- Fixing subtle bug where MysqlCookbook::Helper methods were polluting Chef::Resource
v6.0.5 (2014-12-25)
------------------
- Using 'include_recipe' instead of 'recipe_eval' in LWRP
- Fixing type checking on package_name attribute in mysql_client resource.
v6.0.4 (2014-12-21)
------------------
- Suggest available versions if current is not available for current platform.
v6.0.3 (2014-12-17)
------------------
- Adding bind_address parameter to mysql_service resource
v6.0.2 (2014-12-17)
------------------
- Fixing sysvinit provider to survive reboots
v6.0.1 (2014-12-16)
------------------
- Fixing Upstart template to survive reboots
v6.0.0 (2014-12-15)
------------------
- Major version update
- Cookbook now provides LWRPs instead of recipes
- Platform providers re-factored into init system providers
- Separated :create and :start actions for use in recipes that build containers
- mysql_service now supports multiple instances on the same machine
- mysql_service no longer attempts to manage user records
- Removal of debian-sys-maint
- Unified Sysvinit script that works on all platforms
- mysql_config resource introduced
- mysql_client fixed up
- Refactored acceptance tests
- Temporarily dropped FreeBSD support
v5.6.1 (2014-10-29)
------------------
- Use Gem::Version instead of Chef::Version
v5.6.0 (2014-10-29)
------------------
- Changing default charset to utf8
- Quoting passwords in debian.cnf.erb
- Amazon 2014.09 support
- Ubuntu 14.10 support
- Only hide passwords from STDOUT via "sensitive true" in chef-client higher than 11.14
- Updating test harness
v5.5.4 (2014-10-07)
------------------
- Adding sensitive flag to execute resources to protect passwords from logs
v5.5.3 (2014-09-24)
------------------
- Reverting back to Upstart on Ubuntu 14.04
v5.5.2 (2014-09-8)
------------------
- Reverting commit that broke Debian pass_string
v5.5.1 (2014-09-2)
------------------
- Switching Ubuntu service provider to use SysVinit instead of Upstart
v5.5.0 (2014-08-27)
-------------------
- Adding package version and action parameters to mysql_service resource
- Fixing Debian pass_string
v5.4.4 (2014-08-27)
-------------------
- Changing module namespace to MysqlCookbook
v5.4.3 (2014-08-25)
-------------------
- More refactoring. Moving helper function bits into resource parsed_parameters
v5.4.2 (2014-08-25)
-------------------
- Moving provider local variables into definitions for RHEL provider
v5.4.1 (2014-08-25)
-------------------
- Refactoring resources into the LWRP style with parsed parameters
- Moving provider local variables into definitions
v5.4.0 (2014-08-25)
-------------------
- #212 - support for centos-7 (mysql55 and mysql56)
- Adding (untested) Debian-6 support
- Adding Suse support to metadata.rb
- Adding ability to change MySQL root password
- Added libmysqlclient-devel package to SuSE client provider
- Appeasing AppArmor
- Reducing duplication in client provider
v5.3.6 (2014-06-18)
-------------------
- Fixing pid path location. Updating tests to include real RHEL
v5.3.4 (2014-06-16)
-------------------
- Fixing specs for Amazon Linux server package names
v5.3.2 (2014-06-16)
-------------------
- Fixing Amazon Linux support
v5.3.0 (2014-06-11)
-------------------
- #189 - Fix server_repl_password description
- #191 - Adding support for server55 and server56 on el-6
- #193 - Fix syntax in mysql_service example
- #199 - Adding Suse support
v5.2.12 (2014-05-19)
--------------------
PR #192 - recipes/server.rb should honor parameter node['mysql']['version']
v5.2.10 (2014-05-15)
--------------------
- COOK-4394 - restore freebsd support
v5.2.8 (2014-05-15)
-------------------
- [COOK-4653] - Missing mySQL 5.6 support for Ubuntu 14.04
v5.2.6 (2014-05-07)
-------------------
- [COOK-4625] - Fix password resource parameter consumption on Debian and Ubuntu
- Fix up typos and version numbers in PLATFORMS.md
- Fix up specs from COOK-4613 changes
v5.2.4 (2014-05-02)
-------------------
- [COOK-4613] - Fix permissions on mysql data_dir to allow global access to mysql.sock
v5.2.2 (2014-04-24)
-------------------
- [COOK-4564] - Using positive tests for datadir move
v5.2.0 (2014-04-22)
-------------------
- [COOK-4551] - power grants.sql from resource parameters
v5.1.12 (2014-04-21)
--------------------
- [COOK-4554] - Support for Debian Sid
v5.1.10 (2014-04-21)
--------------------
- [COOK-4565] Support for Ubuntu 14.04
- [COOK-4565] Adding Specs and TK platform
- Removing non-LTS 13.10 specs and TK platform
v5.1.8 (2014-04-12)
-------------------
Adding Ubuntu 13.04 to Platforminfo
v5.1.6 (2014-04-11)
-------------------
- [COOK-4548] - Add template[/etc/mysql/debian.cnf] to Ubuntu provider
v5.1.4 (2014-04-11)
-------------------
- [COOK-4547] - Shellescape server_root_password
v5.1.2 (2014-04-09)
-------------------
- [COOK-4519] - Fix error in run_dir for Ubuntu
- [COOK-4531] - Fix pid and run_dir for Debian
v5.1.0 (2014-04-08)
-------------------
[COOK-4523] - Allow for both :restart and :reload
v5.0.6 (2014-04-07)
-------------------
- [COOK-4519] - Updating specs to reflect pid file change on Ubuntu
v5.0.4 (2014-04-07)
-------------------
- [COOK-4519] - Fix path to pid file on Ubuntu
v5.0.2 (2014-04-01)
-------------------
- Moving server_deprecated into recipes directory
v5.0.0 (2014-03-31)
-------------------
- Rewriting as a library cookbook
- Exposing mysql_service and mysql_client resources
- User now needs to supply configuration
- Moving attribute driven recipe to server-deprecated
v4.1.2 (2014-02-28)
-------------------
- [COOK-4349] - Fix invalid platform check
- [COOK-4184] - Better handling of Ubuntu upstart service
- [COOK-2100] - Changing innodb_log_file_size tunable results in inability to start MySQL
v4.1.1 (2014-02-25)
-------------------
- **[COOK-2966] - Address foodcritic failures'
- **[COOK-4182] - Template parse failure in /etc/init/mysql.conf (data_dir)'
- **[COOK-4198] - Added missing tunable'
- **[COOK-4206] - create root@127.0.0.1, as well as root@localhost'
v4.0.20 (2014-01-18)
--------------------
* [COOK-3931] - MySQL Server Recipe Regression for Non-LTS Ubuntu Versions
* [COOK-3945] - MySQL cookbook fails on Ubuntu 13.04/13.10
* [COOK-3966] - mysql::server recipe can't find a template with debian 7.x
* [COOK-3985] - Missing /etc/mysql/debian.cnf template on mysql::_server_debian.rb recipe (mysql 4.0.4)
* [COOK-3974] - debian.cnf not updated
* [COOK-4001] - Pull request: Fixes for broken mysql::server on Debian
* [COOK-4071] - Mysql cookbook doesn't work on debian 7.2
v4.0.14
-------
Fixing style cops
v4.0.12
-------
### Bug
- **[COOK-4068](https://tickets.chef.io/browse/COOK-4068)** - rework MySQL Windows recipe
### Improvement
- **[COOK-3801](https://tickets.chef.io/browse/COOK-3801)** - Add innodb_adaptive_flushing_method and innodb_adaptive_checkpoint
v4.0.10
-------
fixing metadata version error. locking to 3.0
v4.0.8
------
Locking yum dependency to '< 3'
v4.0.6
------
# Bug
- [COOK-3943] Notifying service restart on grants update
v4.0.4
------
[COOK-3952] - Adding 'recursive true' to directory resources
v4.0.2
------
### BUGS
- Adding support for Amazon Linux in attributes/server_rhel.rb
- Fixing bug where unprivileged users cannot connect over a local socket. Adding integration test.
- Fixing bug in mysql_grants_cmd generation
v4.0.0
------
- [COOK-3928] Heavily refactoring for readability. Moving platform implementation into separate recipes
- Moving integration tests from minitest to serverspec, removing "improper" tests
- Moving many attributes into the ['mysql']['server']['whatever'] namespace
- [COOK-3481] - Merged Lucas Welsh's Windows bits and moved into own recipe
- [COOK-3697] - Adding security hardening attributes
- [COOK-3780] - Fixing data_dir on Debian and Ubuntu
- [COOK-3807] - Don't use execute[assign-root-password] on Debian and Ubuntu
- [COOK-3881] - Fixing /etc being owned by mysql user
v3.0.12
-------
### Bug
- **[COOK-3752](https://tickets.chef.io/browse/COOK-3752)** - mysql service fails to start in mysql::server recipe
v3.0.10
-------
- Fix a failed release attempt for v3.0.8
v3.0.8
------
### Bug
- **[COOK-3749](https://tickets.chef.io/browse/COOK-3749)** - Fix a regression with Chef 11-specific features
v3.0.6
------
### Bug
- **[COOK-3674](https://tickets.chef.io/browse/COOK-3674)** - Fix an issue where the MySQL server fails to set the root password correctly when `data_dir` is a non-default value
- **[COOK-3647](https://tickets.chef.io/browse/COOK-3647)** - Fix README typo (databas => database)
- **[COOK-3477](https://tickets.chef.io/browse/COOK-3477)** - Fix log-queries-not-using-indexes not working
- **[COOK-3436](https://tickets.chef.io/browse/COOK-3436)** - Pull percona repo in compilation phase
- **[COOK-3208](https://tickets.chef.io/browse/COOK-3208)** - Fix README typo (LitenPort => ListenPort)
- **[COOK-3149](https://tickets.chef.io/browse/COOK-3149)** - Create my.cnf before installing
- **[COOK-2681](https://tickets.chef.io/browse/COOK-2681)** - Fix log_slow_queries for 5.5+
- **[COOK-2606](https://tickets.chef.io/browse/COOK-2606)** - Use proper bind address on cloud providers
### Improvement
- **[COOK-3498](https://tickets.chef.io/browse/COOK-3498)** - Add support for replicate_* variables in my.cnf
v3.0.4
------
### Bug
- **[COOK-3310](https://tickets.chef.io/browse/COOK-3310)** - Fix missing `GRANT` option
- **[COOK-3233](https://tickets.chef.io/browse/COOK-3233)** - Fix escaping special characters
- **[COOK-3156](https://tickets.chef.io/browse/COOK-3156)** - Fix GRANTS file when `remote_root_acl` is specified
- **[COOK-3134](https://tickets.chef.io/browse/COOK-3134)** - Fix Chef 11 support
- **[COOK-2318](https://tickets.chef.io/browse/COOK-2318)** - Remove redundant `if` block around `node.mysql.tunable.log_bin`
v3.0.2
------
### Bug
- [COOK-2158]: apt-get update is run twice at compile time
- [COOK-2832]: mysql grants.sql file has errors depending on attrs
- [COOK-2995]: server.rb is missing a platform_family comparison value
### Sub-task
- [COOK-2102]: `innodb_flush_log_at_trx_commit` value is incorrectly set based on CPU count
v3.0.0
------
**Note** This is a backwards incompatible version with previous versions of the cookbook. Tickets that introduce incompatibility are COOK-2615 and COOK-2617.
- [COOK-2478] - Duplicate 'read_only' server attribute in base and tunable
- [COOK-2471] - Add tunable to set slave_compressed_protocol for reduced network traffic
- [COOK-1059] - Update attributes in mysql cookbook to support missing options for my.cnf usable by Percona
- [COOK-2590] - Typo in server recipe to do with conf_dir and confd_dir
- [COOK-2602] - Add `lower_case_table_names` tunable
- [COOK-2430] - Add a tunable to create a network ACL when allowing `remote_root_access`
- [COOK-2619] - mysql: isamchk deprecated
- [COOK-2515] - Better support for SUSE distribution for mysql cookbook
- [COOK-2557] - mysql::percona_repo attributes missing and key server typo
- [COOK-2614] - Duplicate `innodb_file_per_table`
- [COOK-2145] - MySQL cookbook should remove anonymous and password less accounts
- [COOK-2553] - Enable include directory in my.cnf template for any platform
- [COOK-2615] - Rename `key_buffer` to `key_buffer_size`
- [COOK-2626] - Percona repo URL is being constructed incorrectly
- [COOK-2616] - Unneeded attribute thread_cache
- [COOK-2618] - myisam-recover not using attribute value
- [COOK-2617] - open-files is a duplicate of open-files-limit
v2.1.2
------
- [COOK-2172] - Mysql cookbook duplicates `binlog_format` configuration
v2.1.0
------
- [COOK-1669] - Using platform("ubuntu") in default attributes always returns true
- [COOK-1694] - Added additional my.cnf fields and reorganized cookbook to avoid race conditions with mysql startup and sql script execution
- [COOK-1851] - Support server-id and binlog_format settings
- [COOK-1929] - Update msyql server attributes file because setting attributes without specifying a precedence is deprecated
- [COOK-1999] - Add read_only tunable useful for replication slave servers
v2.0.2
------
- [COOK-1967] - mysql: trailing comma in server.rb platform family
v2.0.0
------
**Important note for this release**
Under Chef Solo, you must set the node attributes for the root, debian and repl passwords or the run will completely fail. See COOK-1737 for background on this.
- [COOK-1390] - MySQL service cannot start after reboot
- [COOK-1610] - Set root password outside preseed (blocker for drop-in mysql replacements)
- [COOK-1624] - Mysql cookbook fails to even compile on windows
- [COOK-1669] - Using platform("ubuntu") in default attributes always returns true
- [COOK-1686] - Add mysql service start
- [COOK-1687] - duplicate `innodb_buffer_pool_size` attribute
- [COOK-1704] - mysql cookbook fails spec tests when minitest-handler cookbook enabled
- [COOK-1737] - Fail a chef-solo run when `server_root_password`, `server_debian_password`, and/or `server_repl_password` is not set
- [COOK-1769] - link to database recipe in mysql README goes to old chef/cookbooks repo instead of chef-cookbook organization
- [COOK-1963] - use `platform_family`
v1.3.0
------
**Important note for this release**
This version no longer installs Ruby bindings in the client recipe by default. Use the ruby recipe if you'd like the RubyGem. If you'd like packages from your distribution, use them in your application's specific cookbook/recipe, or modify the client packages attribute. This resolves the following tickets:
- COOK-932
- COOK-1009
- COOK-1384
Additionally, this cookbook now has tests (COOK-1439) for use under test-kitchen.
The following issues are also addressed in this release.
- [COOK-1443] - MySQL (>= 5.1.24) does not support `innodb_flush_method` = fdatasync
- [COOK-1175] - Add Mac OS X support
- [COOK-1289] - handle additional tunable attributes
- [COOK-1305] - add auto-increment-increment and auto-increment-offset attributes
- [COOK-1397] - make the port an attribute
- [COOK-1439] - Add MySQL cookbook tests for test-kitchen support
- [COOK-1236] - Move package names into attributes to allow percona to free-ride
- [COOK-934] - remove deprecated mysql/libraries/database.rb, use the database cookbook instead.
- [COOK-1475] - fix restart on config change
v1.2.6
------
- [COOK-1113] - Use an attribute to determine if upstart is used
- [COOK-1121] - Add support for Windows
- [COOK-1140] - Fix conf.d on Debian
- [COOK-1151] - Fix server_ec2 handling /var/lib/mysql bind mount
- [COOK-1321] - Document setting password attributes for solo
v1.2.4
------
- [COOK-992] - fix FATAL nameerror
- [COOK-827] - `mysql:server_ec2` recipe can't mount `data_dir`
- [COOK-945] - FreeBSD support
v1.2.2
------
- [COOK-826] mysql::server recipe doesn't quote password string
- [COOK-834] Add 'scientific' and 'amazon' platforms to mysql cookbook
v1.2.1
------
- [COOK-644] Mysql client cookbook 'package missing' error message is confusing
- [COOK-645] RHEL6/CentOS6 - mysql cookbook contains 'skip-federated' directive which is unsupported on MySQL 5.1
v1.2.0
------
- [COOK-684] remove mysql_database LWRP
v1.0.8
------
- [COOK-633] ensure "cloud" attribute is available
v1.0.7
------
- [COOK-614] expose all mysql tunable settings in config
- [COOK-617] bind to private IP if available
v1.0.6
------
- [COOK-605] install mysql-client package on ubuntu/debian
v1.0.5
------
- [COOK-465] allow optional remote root connections to mysql
- [COOK-455] improve platform version handling
- externalize conf_dir attribute for easier cross platform support
- change datadir attribute to data_dir for consistency
v1.0.4
------
- fix regressions on debian platform
- [COOK-578] wrap root password in quotes
- [COOK-562] expose all tunables in my.cnf

560
cookbooks/mysql/README.md Normal file
View File

@@ -0,0 +1,560 @@
MySQL Cookbook
=====================
[![Join the chat at https://gitter.im/chef-cookbooks/mysql](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/chef-cookbooks/mysql?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
The Mysql Cookbook is a library cookbook that provides resource primitives
(LWRPs) for use in recipes. It is designed to be a reference example for
creating highly reusable cross-platform cookbooks.
Scope
-----
This cookbook is concerned with the "MySQL Community Server",
particularly those shipped with F/OSS Unix and Linux distributions. It
does not address forks or value-added repackaged MySQL distributions
like Drizzle, MariaDB, or Percona.
Requirements
------------
- Chef 11 or higher
- Ruby 1.9 or higher (preferably from the Chef full-stack installer)
- Network accessible package repositories
- 'recipe[selinux::disabled]' on RHEL platforms
Platform Support
----------------
The following platforms have been tested with Test Kitchen:
```
|----------------+-----+-----+-----+-----+-----|
| | 5.0 | 5.1 | 5.5 | 5.6 | 5.7 |
|----------------+-----+-----+-----+-----+-----|
| debian-7 | | | X | | |
|----------------+-----+-----+-----+-----+-----|
| ubuntu-10.04 | | X | | | |
|----------------+-----+-----+-----+-----+-----|
| ubuntu-12.04 | | | X | | |
|----------------+-----+-----+-----+-----+-----|
| ubuntu-14.04 | | | X | X | |
|----------------+-----+-----+-----+-----+-----|
| centos-5 | X | X | X | X | X |
|----------------+-----+-----+-----+-----+-----|
| centos-6 | | X | X | X | X |
|----------------+-----+-----+-----+-----+-----|
| centos-7 | | | X | X | X |
|----------------+-----+-----+-----+-----+-----|
| amazon | | | X | X | X |
|----------------+-----+-----+-----+-----+-----|
| fedora-20 | | | X | X | X |
|----------------+-----+-----+-----+-----+-----|
| suse-11.3 | | | X | | |
|----------------+-----+-----+-----+-----+-----|
| omnios-151006 | | | X | X | |
|----------------+-----+-----+-----+-----+-----|
| smartos-14.3.0 | | | X | X | |
|----------------+-----+-----+-----+-----+-----|
```
Cookbook Dependencies
------------
- yum-mysql-community
- smf
Usage
-----
Place a dependency on the mysql cookbook in your cookbook's metadata.rb
```ruby
depends 'mysql', '~> 6.0'
```
Then, in a recipe:
```ruby
mysql_service 'foo' do
port '3306'
version '5.5'
initial_root_password 'change me'
action [:create, :start]
end
```
The service name on the OS is `mysql-foo`. You can manually start and
stop it with `service mysql-foo start` and `service mysql-foo stop`.
The configuration file is at `/etc/mysql-foo/my.cnf`. It contains the
minimum options to get the service running. It looks like this.
```
# Chef generated my.cnf for instance mysql-default
[client]
default-character-set = utf8
port = 3306
socket = /var/run/mysql-foo/mysqld.sock
[mysql]
default-character-set = utf8
[mysqld]
user = mysql
pid-file = /var/run/mysql-foo/mysqld.pid
socket = /var/run/mysql-foo/mysqld.sock
port = 3306
datadir = /var/lib/mysql-foo
tmpdir = /tmp
log-error = /var/log/mysql-foo/error.log
!includedir /etc/mysql-foo/conf.d
[mysqld_safe]
socket = /var/run/mysql-foo/mysqld.sock
```
You can put extra configuration into the conf.d directory by using the
`mysql_config` resource, like this:
```ruby
mysql_service 'foo' do
port '3306'
version '5.5'
initial_root_password 'change me'
action [:create, :start]
end
mysql_config 'foo' do
source 'my_extra_settings.erb'
notifies :restart, 'mysql_service[foo]'
action :create
end
```
You are responsible for providing `my_extra_settings.erb` in your own
cookbook's templates folder.
Connecting with the mysql CLI command
-------------------------------------
Logging into the machine and typing `mysql` with no extra arguments
will fail. You need to explicitly connect over the socket with `mysql
-S /var/run/mysql-foo/mysqld.sock`, or over the network with `mysql -h
127.0.0.1`
Upgrading from older version of the mysql cookbook
--------------------------------------------------
- It is strongly recommended that you rebuild the machine from
scratch. This is easy if you have your `data_dir` on a dedicated
mount point. If you *must* upgrade in-place, follow the instructions
below.
- The 6.x series supports multiple service instances on a single
machine. It dynamically names the support directories and service
names. `/etc/mysql becomes /etc/mysql-instance_name`. Other support
directories in `/var` `/run` etc work the same way. Make sure to
specify the `data_dir` property on the `mysql_service` resource to
point to the old `/var/lib/mysql` directory.
Resources Overview
------------------
### mysql_service
The `mysql_service` resource manages the basic plumbing needed to get a
MySQL server instance running with minimal configuration.
The `:create` action handles package installation, support
directories, socket files, and other operating system level concerns.
The internal configuration file contains just enough to get the
service up and running, then loads extra configuration from a conf.d
directory. Further configurations are managed with the `mysql_config` resource.
- If the `data_dir` is empty, a database will be initialized, and a
root user will be set up with `initial_root_password`. If this
directory already contains database files, no action will be taken.
The `:start` action starts the service on the machine using the
appropriate provider for the platform. The `:start` action should be
omitted when used in recipes designed to build containers.
#### Example
```ruby
mysql_service 'default' do
version '5.7'
bind_address '0.0.0.0'
port '3306'
data_dir '/data'
initial_root_password 'Ch4ng3me'
action [:create, :start]
end
```
Please note that when using `notifies` or `subscribes`, the resource
to reference is `mysql_service[name]`, not `service[mysql]`.
#### Parameters
- `charset` - specifies the default character set. Defaults to `utf8`.
- `data_dir` - determines where the actual data files are kept
on the machine. This is useful when mounting external storage. When
omitted, it will default to the platform's native location.
- `initial_root_password` - allows the user to specify the initial
root password for mysql when initializing new databases.
This can be set explicitly in a recipe, driven from a node
attribute, or from data_bags. When omitted, it defaults to
`ilikerandompasswords`. Please be sure to change it.
- `instance` - A string to identify the MySQL service. By convention,
to allow for multiple instances of the `mysql_service`, directories
and files on disk are named `mysql-<instance_name>`. Defaults to the
resource name.
- `package_action` - Defaults to `:install`.
- `package_name` - Defaults to a value looked up in an internal map.
- `package_version` - Specific version of the package to install,
passed onto the underlying package manager. Defaults to `nil`.
- `bind_address` - determines the listen IP address for the mysqld service. When
omitted, it will be determined by MySQL. If the address is "regular" IPv4/IPv6
address (e.g 127.0.0.1 or ::1), the server accepts TCP/IP connections only for
that particular address. If the address is "0.0.0.0" (IPv4) or "::" (IPv6), the
server accepts TCP/IP connections on all IPv4 or IPv6 interfaces.
- `port` - determines the listen port for the mysqld service. When
omitted, it will default to '3306'.
- `run_group` - The name of the system group the `mysql_service`
should run as. Defaults to 'mysql'.
- `run_user` - The name of the system user the `mysql_service` should
run as. Defaults to 'mysql'.
- `socket` - determines where to write the socket file for the
`mysql_service` instance. Useful when configuring clients on the
same machine to talk over socket and skip the networking stack.
Defaults to a calculated value based on platform and instance name.
- `version` - allows the user to select from the versions available
for the platform, where applicable. When omitted, it will install
the default MySQL version for the target platform. Available version
numbers are `5.0`, `5.1`, `5.5`, `5.6`, and `5.7`, depending on platform.
#### Actions
- `:create` - Configures everything but the underlying operating system service.
- `:delete` - Removes everything but the package and data_dir.
- `:start` - Starts the underlying operating system service
- `:stop`- Stops the underlying operating system service
- `:restart` - Restarts the underlying operating system service
- `:reload` - Reloads the underlying operating system service
#### Providers
Chef selects the appropriate provider based on platform and version,
but you can specify one if your platform support it.
```ruby
mysql_service[instance-1] do
port '1234'
data_dir '/mnt/lottadisk'
provider Chef::Provider::MysqlService::Sysvinit
action [:create, :start]
end
```
- `Chef::Provider::MysqlService` - Configures everything needed t run
a MySQL service except the platform service facility. This provider
should never be used directly. The `:start`, `:stop`, `:restart`, and
`:reload` actions are stubs meant to be overridden by the providers
below.
- `Chef::Provider::MysqlService::Smf` - Starts a `mysql_service` using
the Service Management Facility, used by Solaris and IllumOS. Manages
the FMRI and method script.
- `Chef::Provider::MysqlService::Systemd` - Starts a `mysql_service`
using SystemD. Manages the unit file and activation state
- `Chef::Provider::MysqlService::Sysvinit` - Starts a `mysql_service`
using SysVinit. Manages the init script and status.
- `Chef::Provider::MysqlService::Upstart` - Starts a `mysql_service`
using Upstart. Manages job definitions and status.
### mysql_config
The `mysql_config` resource is a wrapper around the core Chef
`template` resource. Instead of a `path` parameter, it uses the
`instance` parameter to calculate the path on the filesystem where
file is rendered.
#### Example
```ruby
mysql_config[default] do
source 'site.cnf.erb'
action :create
end
```
#### Parameters
- `config_name` - The base name of the configuration file to be
rendered into the conf.d directory on disk. Defaults to the resource
name.
- `cookbook` - The name of the cookbook to look for the template
source. Defaults to nil
- `group` - System group for file ownership. Defaults to 'mysql'.
- `instance` - Name of the `mysql_service` instance the config is
meant for. Defaults to 'default'.
- `owner` - System user for file ownership. Defaults to 'mysql'.
- `source` - Template in cookbook to be rendered.
- `variables` - Variables to be passed to the underlying `template`
resource.
- `version` - Version of the `mysql_service` instance the config is
meant for. Used to calculate path. Only necessary when using
packages with unique configuration paths, such as RHEL Software
Collections or OmniOS. Defaults to 'nil'
#### Actions
- `:create` - Renders the template to disk at a path calculated using
the instance parameter.
- `:delete` - Deletes the file from the conf.d directory calculated
using the instance parameter.
#### More Examples
```ruby
mysql_service 'instance-1' do
action [:create, :start]
end
mysql_service 'instance-2' do
action [:create, :start]
end
mysql_config 'logging' do
instance 'instance-1'
source 'logging.cnf.erb'
action :create
notifies :restart, 'mysql_service[instance-1]'
end
mysql_config 'security settings for instance-2' do
config_name 'security'
instance 'instance-2'
source 'security_stuff.cnf.erb'
variables(:foo => 'bar')
action :create
notifies :restart, 'mysql_service[instance-2]'
end
```
### mysql_client
The `mysql_client` resource manages the MySQL client binaries and
development libraries.
It is an example of a "singleton" resource. Declaring two
`mysql_client` resources on a machine usually won't yield two separate
copies of the client binaries, except for platforms that support
multiple versions (RHEL SCL, OmniOS).
#### Example
```ruby
mysql_client 'default' do
action :create
end
```
#### Parameters
- `package_name` - An array of packages to be installed. Defaults to a
value looked up in an internal map.
- `package_version` - Specific versions of the package to install,
passed onto the underlying package manager. Defaults to `nil`.
- `version` - Major MySQL version number of client packages. Only
valid on for platforms that support multiple versions, such as RHEL
via Software Collections and OmniOS.
#### Actions
- `:create` - Installs the client software
- `:delete` - Removes the client software
Advanced Usage Examples
-----------------------
There are a number of configuration scenarios supported by the use of
resource primitives in recipes. For example, you might want to run
multiple MySQL services, as different users, and mount block devices
that contain pre-existing databases.
### Multiple Instances as Different Users
```ruby
# instance-1
user 'alice' do
action :create
end
directory '/mnt/data/mysql/instance-1' do
owner 'alice'
action :create
end
mount '/mnt/data/mysql/instance-1' do
device '/dev/sdb1'
fstype 'ext4'
action [:mount, :enable]
end
mysql_service 'instance-1' do
port '3307'
run_user 'alice'
data_dir '/mnt/data/mysql/instance-1'
action [:create, :start]
end
mysql_config 'site config for instance-1' do
instance 'instance-1'
source 'instance-1.cnf.erb'
notifies :restart, 'mysql_service[instance-1]'
end
# instance-2
user 'bob' do
action :create
end
directory '/mnt/data/mysql/instance-2' do
owner 'bob'
action :create
end
mount '/mnt/data/mysql/instance-2' do
device '/dev/sdc1'
fstype 'ext3'
action [:mount, :enable]
end
mysql_service 'instance-2' do
port '3308'
run_user 'bob'
data_dir '/mnt/data/mysql/instance-2'
action [:create, :start]
end
mysql_config 'site config for instance-2' do
instance 'instance-2'
source 'instance-2.cnf.erb'
notifies :restart, 'mysql_service[instance-2]'
end
```
### Replication Testing
Use multiple `mysql_service` instances to test a replication setup.
This particular example serves as a smoke test in Test Kitchen because
it exercises different resources and requires service restarts.
https://github.com/chef-cookbooks/mysql/blob/master/test/fixtures/cookbooks/mysql_replication_test/recipes/default.rb
Frequently Asked Questions
--------------------------
### How do I run this behind my firewall?
On Linux, the `mysql_service` resource uses the platform's underlying
package manager to install software. For this to work behind
firewalls, you'll need to either:
- Configure the system yum/apt utilities to use a proxy server that
can reach the Internet
- Host a package repository on a network that the machine can talk to
On the RHEL platform_family, applying the `yum::default` recipe will
allow you to drive the `yum_globalconfig` resource with attributes to
change the global yum proxy settings.
If hosting repository mirrors, applying one of the following recipes
and adjust the settings with node attributes.
- `recipe[yum-centos::default]` from the Supermarket
https://supermarket.chef.io/cookbooks/yum-centos
https://github.com/chef-cookbooks/yum-centos
- `recipe[yum-mysql-community::default]` from the Supermarket
https://supermarket.chef.io/cookbooks/yum-mysql-community
https://github.com/chef-cookbooks/yum-mysql-community
### The mysql command line doesn't work
If you log into the machine and type `mysql`, you may see an error
like this one:
`Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'`
This is because MySQL is hardcoded to read the defined default my.cnf
file, typically at /etc/my.cnf, and this LWRP deletes it to prevent
overlap among multiple MySQL configurations.
To connect to the socket from the command line, check the socket in the relevant my.cnf file and use something like this:
`mysql -S /var/run/mysql-default/mysqld.sock -Pwhatever`
Or to connect over the network, use something like this:
connect over the network..
`mysql -h 127.0.0.1 -Pwhatever`
These network or socket ssettings can also be put in you
$HOME/.my.cnf, if preferred.
### What about MariaDB, Percona, Drizzle, WebScaleSQL, etc.
MySQL forks are purposefully out of scope for this cookbook. This is
mostly to reduce the testing matrix to a manageable size. Cookbooks
for these technologies can easily be created by copying and adapting
this cookbook. However, there will be differences.
Package repository locations, package version names, software major
version numbers, supported platform matrices, and the availability of
software such as XtraDB and Galera are the main reasons that creating
multiple cookbooks to make sense.
Warnings
--------
Hacking / Testing / TODO
-------------------------
Please refer to the HACKING.md
License & Authors
-----------------
- Author:: Joshua Timberman (<joshua@chef.io>)
- Author:: AJ Christensen (<aj@chef.io>)
- Author:: Seth Chisamore (<schisamo@chef.io>)
- Author:: Brian Bianco (<brian.bianco@gmail.com>)
- Author:: Jesse Howarth (<him@jessehowarth.com>)
- Author:: Andrew Crump (<andrew@kotirisoftware.com>)
- Author:: Christoph Hartmann (<chris@lollyrock.com>)
- Author:: Sean OMeara (<sean@chef.io>)
```text
Copyright:: 2009-2014 Chef Software, 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.
```

View File

@@ -0,0 +1,416 @@
require 'shellwords'
module MysqlCookbook
module Helpers
include Chef::DSL::IncludeRecipe
def base_dir
prefix_dir || '/usr'
end
def configure_package_repositories
# we need to enable the yum-mysql-community repository to get packages
return unless %w(rhel fedora).include? node['platform_family']
case parsed_version
when '5.5'
# Prefer packages from native repos
return if node['platform_family'] == 'rhel' && node['platform_version'].to_i == 5
return if node['platform_family'] == 'fedora'
include_recipe('yum-mysql-community::mysql55')
when '5.6'
include_recipe('yum-mysql-community::mysql56')
when '5.7'
include_recipe('yum-mysql-community::mysql57')
end
end
def client_package_name
return new_resource.package_name if new_resource.package_name
client_package
end
def defaults_file
"#{etc_dir}/my.cnf"
end
def error_log
"#{log_dir}/error.log"
end
def etc_dir
return "/opt/mysql#{pkg_ver_string}/etc/#{mysql_name}" if node['platform_family'] == 'omnios'
return "#{prefix_dir}/etc/#{mysql_name}" if node['platform_family'] == 'smartos'
"#{prefix_dir}/etc/#{mysql_name}"
end
def include_dir
"#{etc_dir}/conf.d"
end
def lc_messages_dir
end
def log_dir
return "/var/adm/log/#{mysql_name}" if node['platform_family'] == 'omnios'
"#{prefix_dir}/var/log/#{mysql_name}"
end
def mysql_name
"mysql-#{new_resource.instance}"
end
def pkg_ver_string
parsed_version.gsub('.', '') if node['platform_family'] == 'omnios'
end
def prefix_dir
return "/opt/mysql#{pkg_ver_string}" if node['platform_family'] == 'omnios'
return '/opt/local' if node['platform_family'] == 'smartos'
return "/opt/rh/#{scl_name}/root" if scl_package?
end
def scl_name
return unless node['platform_family'] == 'rhel'
return 'mysql51' if parsed_version == '5.1' && node['platform_version'].to_i == 5
return 'mysql55' if parsed_version == '5.5' && node['platform_version'].to_i == 5
end
def scl_package?
return unless node['platform_family'] == 'rhel'
return true if parsed_version == '5.1' && node['platform_version'].to_i == 5
return true if parsed_version == '5.5' && node['platform_version'].to_i == 5
false
end
def system_service_name
return 'mysql51-mysqld' if node['platform_family'] == 'rhel' && scl_name == 'mysql51'
return 'mysql55-mysqld' if node['platform_family'] == 'rhel' && scl_name == 'mysql55'
return 'mysqld' if node['platform_family'] == 'rhel'
return 'mysqld' if node['platform_family'] == 'fedora'
return 'mysql' if node['platform_family'] == 'debian'
return 'mysql' if node['platform_family'] == 'suse'
return 'mysql' if node['platform_family'] == 'omnios'
return 'mysql' if node['platform_family'] == 'smartos'
end
def v56plus
return false if parsed_version.split('.')[0].to_i < 5
return false if parsed_version.split('.')[1].to_i < 6
true
end
def v57plus
return false if parsed_version.split('.')[0].to_i < 5
return false if parsed_version.split('.')[1].to_i < 7
true
end
# database and initial records
# initialization commands
def mysqld_initialize_cmd
cmd = mysqld_bin
cmd << " --defaults-file=#{etc_dir}/my.cnf"
cmd << ' --initialize'
cmd << ' --explicit_defaults_for_timestamp' if v56plus
return "scl enable #{scl_name} \"#{cmd}\"" if scl_package?
cmd
end
def mysql_install_db_cmd
cmd = mysql_install_db_bin
cmd << " --defaults-file=#{etc_dir}/my.cnf"
cmd << " --datadir=#{parsed_data_dir}"
cmd << ' --explicit_defaults_for_timestamp' if v56plus
return "scl enable #{scl_name} \"#{cmd}\"" if scl_package?
cmd
end
def record_init
cmd = v56plus ? mysqld_bin : mysqld_safe_bin
cmd << " --defaults-file=#{etc_dir}/my.cnf"
cmd << " --init-file=/tmp/#{mysql_name}/my.sql"
cmd << ' --explicit_defaults_for_timestamp' if v56plus
cmd << ' &'
return "scl enable #{scl_name} \"#{cmd}\"" if scl_package?
cmd
end
def db_init
return mysqld_initialize_cmd if v57plus
mysql_install_db_cmd
end
def init_records_script
<<-EOS
set -e
rm -rf /tmp/#{mysql_name}
mkdir /tmp/#{mysql_name}
cat > /tmp/#{mysql_name}/my.sql <<-EOSQL
DELETE FROM mysql.user ;
CREATE USER 'root'@'%' IDENTIFIED BY '#{Shellwords.escape(new_resource.initial_root_password)}' ;
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ;
FLUSH PRIVILEGES;
DROP DATABASE IF EXISTS test ;
EOSQL
#{db_init}
#{record_init}
while [ ! -f #{pid_file} ] ; do sleep 1 ; done
kill `cat #{pid_file}`
while [ -f #{pid_file} ] ; do sleep 1 ; done
rm -rf /tmp/#{mysql_name}
EOS
end
def mysql_bin
return "#{prefix_dir}/bin/mysql" if node['platform_family'] == 'smartos'
return "#{base_dir}/bin/mysql" if node['platform_family'] == 'omnios'
"#{prefix_dir}/usr/bin/mysql"
end
def mysql_install_db_bin
return "#{base_dir}/scripts/mysql_install_db" if node['platform_family'] == 'omnios'
return "#{prefix_dir}/bin/mysql_install_db" if node['platform_family'] == 'smartos'
'mysql_install_db'
end
def mysql_version
new_resource.version
end
def mysqladmin_bin
return "#{prefix_dir}/bin/mysqladmin" if node['platform_family'] == 'smartos'
return 'mysqladmin' if scl_package?
"#{prefix_dir}/usr/bin/mysqladmin"
end
def mysqld_bin
return "#{prefix_dir}/libexec/mysqld" if node['platform_family'] == 'smartos'
return "#{base_dir}/bin/mysqld" if node['platform_family'] == 'omnios'
return '/usr/sbin/mysqld' if node['platform_family'] == 'fedora' && v56plus
return '/usr/libexec/mysqld' if node['platform_family'] == 'fedora'
return 'mysqld' if scl_package?
"#{prefix_dir}/usr/sbin/mysqld"
end
def mysqld_safe_bin
return "#{prefix_dir}/bin/mysqld_safe" if node['platform_family'] == 'smartos'
return "#{base_dir}/bin/mysqld_safe" if node['platform_family'] == 'omnios'
return 'mysqld_safe' if scl_package?
"#{prefix_dir}/usr/bin/mysqld_safe"
end
def pid_file
"#{run_dir}/mysqld.pid"
end
def run_dir
return "#{prefix_dir}/var/run/#{mysql_name}" if node['platform_family'] == 'rhel'
return "/run/#{mysql_name}" if node['platform_family'] == 'debian'
"/var/run/#{mysql_name}"
end
def sensitive_supported?
Gem::Version.new(Chef::VERSION) >= Gem::Version.new('11.14.0')
end
def socket_file
return new_resource.socket if new_resource.socket
"#{run_dir}/mysqld.sock"
end
def socket_dir
return File.dirname(new_resource.socket) if new_resource.socket
run_dir
end
def tmp_dir
'/tmp'
end
#######
# FIXME: There is a LOT of duplication here..
# There has to be a less gnarly way to look up this information. Refactor for great good!
#######
class Pkginfo
def self.pkginfo
# Autovivification is Perl.
@pkginfo = Chef::Node.new
@pkginfo.set['debian']['10.04']['5.1']['client_package'] = %w(mysql-client-5.1 libmysqlclient-dev)
@pkginfo.set['debian']['10.04']['5.1']['server_package'] = 'mysql-server-5.1'
@pkginfo.set['debian']['12.04']['5.5']['client_package'] = %w(mysql-client-5.5 libmysqlclient-dev)
@pkginfo.set['debian']['12.04']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['debian']['13.04']['5.5']['client_package'] = %w(mysql-client-5.5 libmysqlclient-dev)
@pkginfo.set['debian']['13.04']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['debian']['13.10']['5.5']['client_package'] = %w(mysql-client-5.5 libmysqlclient-dev)
@pkginfo.set['debian']['13.10']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['debian']['14.04']['5.5']['client_package'] = %w(mysql-client-5.5 libmysqlclient-dev)
@pkginfo.set['debian']['14.04']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['debian']['14.04']['5.6']['client_package'] = %w(mysql-client-5.6 libmysqlclient-dev)
@pkginfo.set['debian']['14.04']['5.6']['server_package'] = 'mysql-server-5.6'
@pkginfo.set['debian']['14.10']['5.5']['client_package'] = %w(mysql-client-5.5 libmysqlclient-dev)
@pkginfo.set['debian']['14.10']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['debian']['14.10']['5.6']['client_package'] = %w(mysql-client-5.6 libmysqlclient-dev)
@pkginfo.set['debian']['14.10']['5.6']['server_package'] = 'mysql-server-5.6'
@pkginfo.set['debian']['6']['5.1']['client_package'] = %w(mysql-client libmysqlclient-dev)
@pkginfo.set['debian']['6']['5.1']['server_package'] = 'mysql-server-5.1'
@pkginfo.set['debian']['7']['5.5']['client_package'] = %w(mysql-client libmysqlclient-dev)
@pkginfo.set['debian']['7']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['debian']['7']['5.6']['client_package'] = %w(mysql-client libmysqlclient-dev) # apt-repo from dotdeb
@pkginfo.set['debian']['7']['5.6']['server_package'] = 'mysql-server-5.6'
@pkginfo.set['debian']['7']['5.7']['client_package'] = %w(mysql-client libmysqlclient-dev) # apt-repo from dotdeb
@pkginfo.set['debian']['7']['5.7']['server_package'] = 'mysql-server-5.7'
@pkginfo.set['debian']['8']['5.5']['client_package'] = %w(mysql-client libmysqlclient-dev)
@pkginfo.set['debian']['8']['5.5']['server_package'] = 'mysql-server-5.5'
@pkginfo.set['fedora']['20']['5.5']['client_package'] = %w(community-mysql community-mysql-devel)
@pkginfo.set['fedora']['20']['5.5']['server_package'] = 'community-mysql-server'
@pkginfo.set['fedora']['20']['5.6']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['fedora']['20']['5.6']['server_package'] = 'mysql-community-server'
@pkginfo.set['fedora']['20']['5.7']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['fedora']['20']['5.7']['server_package'] = 'mysql-community-server'
@pkginfo.set['freebsd']['10']['5.5']['client_package'] = %w(mysql55-client)
@pkginfo.set['freebsd']['10']['5.5']['server_package'] = 'mysql55-server'
@pkginfo.set['freebsd']['9']['5.5']['client_package'] = %w(mysql55-client)
@pkginfo.set['freebsd']['9']['5.5']['server_package'] = 'mysql55-server'
@pkginfo.set['omnios']['151006']['5.5']['client_package'] = %w(database/mysql-55/library)
@pkginfo.set['omnios']['151006']['5.5']['server_package'] = 'database/mysql-55'
@pkginfo.set['omnios']['151006']['5.6']['client_package'] = %w(database/mysql-56)
@pkginfo.set['omnios']['151006']['5.6']['server_package'] = 'database/mysql-56'
@pkginfo.set['rhel']['2014.09']['5.1']['server_package'] = %w(mysql51 mysql51-devel)
@pkginfo.set['rhel']['2014.09']['5.1']['server_package'] = 'mysql51-server'
@pkginfo.set['rhel']['2014.09']['5.5']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['2014.09']['5.5']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['2014.09']['5.6']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['2014.09']['5.6']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['2014.09']['5.7']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['2014.09']['5.7']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['2015.03']['5.1']['server_package'] = %w(mysql51 mysql51-devel)
@pkginfo.set['rhel']['2015.03']['5.1']['server_package'] = 'mysql51-server'
@pkginfo.set['rhel']['2015.03']['5.5']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['2015.03']['5.5']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['2015.03']['5.6']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['2015.03']['5.6']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['2015.03']['5.7']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['2015.03']['5.7']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['5']['5.0']['client_package'] = %w(mysql mysql-devel)
@pkginfo.set['rhel']['5']['5.0']['server_package'] = 'mysql-server'
@pkginfo.set['rhel']['5']['5.1']['client_package'] = %w(mysql51-mysql)
@pkginfo.set['rhel']['5']['5.1']['server_package'] = 'mysql51-mysql-server'
@pkginfo.set['rhel']['5']['5.5']['client_package'] = %w(mysql55-mysql mysql55-mysql-devel)
@pkginfo.set['rhel']['5']['5.5']['server_package'] = 'mysql55-mysql-server'
@pkginfo.set['rhel']['5']['5.6']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['5']['5.6']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['5']['5.7']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['5']['5.7']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['6']['5.1']['client_package'] = %w(mysql mysql-devel)
@pkginfo.set['rhel']['6']['5.1']['server_package'] = 'mysql-server'
@pkginfo.set['rhel']['6']['5.5']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['6']['5.5']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['6']['5.6']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['6']['5.6']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['6']['5.7']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['6']['5.7']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['7']['5.5']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['7']['5.5']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['7']['5.6']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['7']['5.6']['server_package'] = 'mysql-community-server'
@pkginfo.set['rhel']['7']['5.7']['client_package'] = %w(mysql-community-client mysql-community-devel)
@pkginfo.set['rhel']['7']['5.7']['server_package'] = 'mysql-community-server'
@pkginfo.set['smartos']['5.11']['5.5']['client_package'] = %w(mysql-client)
@pkginfo.set['smartos']['5.11']['5.5']['server_package'] = 'mysql-server'
@pkginfo.set['smartos']['5.11']['5.6']['client_package'] = %w(mysql-client)
@pkginfo.set['smartos']['5.11']['5.6']['server_package'] = 'mysql-server'
@pkginfo.set['suse']['11.3']['5.5']['client_package'] = %w(mysql-client)
@pkginfo.set['suse']['11.3']['5.5']['server_package'] = 'mysql'
@pkginfo
end
end
def package_name_for(platform, platform_family, platform_version, version, type)
keyname = keyname_for(platform, platform_family, platform_version)
info = Pkginfo.pkginfo[platform_family.to_sym][keyname]
type_label = type.to_s.gsub('_package', '').capitalize
unless info[version]
# Show availabe versions if the requested is not available on the current platform
Chef::Log.error("Unsupported Version: You requested to install a Mysql #{type_label} version that is not supported by your platform")
Chef::Log.error("Platform: #{platform_family} #{platform_version} - Request Mysql #{type_label} version: #{version}")
Chef::Log.error("Availabe versions for your platform are: #{info.map { |k, _v| k }.join(' - ')}")
fail "Unsupported Mysql #{type_label} Version"
end
info[version][type]
end
def keyname_for(platform, platform_family, platform_version)
return platform_version if platform_family == 'debian' && platform == 'ubuntu'
return platform_version if platform_family == 'fedora'
return platform_version if platform_family == 'omnios'
return platform_version if platform_family == 'rhel' && platform == 'amazon'
return platform_version if platform_family == 'smartos'
return platform_version if platform_family == 'suse'
return platform_version.to_i.to_s if platform_family == 'debian'
return platform_version.to_i.to_s if platform_family == 'rhel'
return platform_version.to_s if platform_family == 'debian' && platform_version =~ /sid$/
return platform_version.to_s if platform_family == 'freebsd'
end
def parsed_data_dir
return new_resource.data_dir if new_resource.data_dir
return "/opt/local/lib/#{mysql_name}" if node['os'] == 'solaris2'
return "/var/lib/#{mysql_name}" if node['os'] == 'linux'
return "/var/db/#{mysql_name}" if node['os'] == 'freebsd'
end
def client_package
package_name_for(
node['platform'],
node['platform_family'],
node['platform_version'],
parsed_version,
:client_package
)
end
def server_package
package_name_for(
node['platform'],
node['platform_family'],
node['platform_version'],
parsed_version,
:server_package
)
end
def server_package_name
return new_resource.package_name if new_resource.package_name
server_package
end
def parsed_version
return new_resource.version if new_resource.version
return '5.0' if node['platform_family'] == 'rhel' && node['platform_version'].to_i == 5
return '5.1' if node['platform_family'] == 'debian' && node['platform_version'] == '10.04'
return '5.1' if node['platform_family'] == 'debian' && node['platform_version'].to_i == 6
return '5.1' if node['platform_family'] == 'rhel' && node['platform_version'].to_i == 6
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'] == '12.04'
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'] == '13.04'
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'] == '13.10'
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'] == '14.04'
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'] == '14.10'
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'].to_i == 7
return '5.5' if node['platform_family'] == 'debian' && node['platform_version'].to_i == 8
return '5.5' if node['platform_family'] == 'fedora'
return '5.5' if node['platform_family'] == 'freebsd'
return '5.5' if node['platform_family'] == 'omnios'
return '5.5' if node['platform_family'] == 'rhel' && node['platform_version'].to_i == 2014
return '5.5' if node['platform_family'] == 'rhel' && node['platform_version'].to_i == 2015
return '5.5' if node['platform_family'] == 'rhel' && node['platform_version'].to_i == 7
return '5.5' if node['platform_family'] == 'smartos'
return '5.5' if node['platform_family'] == 'suse'
end
end
end

View File

@@ -0,0 +1,28 @@
if defined?(ChefSpec)
# config
def create_mysql_config(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:mysql_config, :create, resource_name)
end
def delete_mysql_config(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:mysql_config, :delete, resource_name)
end
# service
def create_mysql_service(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:mysql_service, :create, resource_name)
end
def delete_mysql_service(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:mysql_service, :delete, resource_name)
end
# client
def create_mysql_client(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:mysql_client, :create, resource_name)
end
def delete_mysql_client(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:mysql_client, :delete, resource_name)
end
end

View File

@@ -0,0 +1,38 @@
require 'chef/provider/lwrp_base'
require_relative 'helpers'
class Chef
class Provider
class MysqlClient < Chef::Provider::LWRPBase
include MysqlCookbook::Helpers
use_inline_resources if defined?(use_inline_resources)
def whyrun_supported?
true
end
action :create do
# From helpers.rb
configure_package_repositories
client_package_name.each do |p|
package "#{new_resource.name} :create #{p}" do
package_name p
version new_resource.version if node['platform'] == 'smartos'
version new_resource.package_version
action :install
end
end
end
action :delete do
parsed_package_name.each do |p|
package "#{new_resource.name} :delete #{p}" do
action :remove
end
end
end
end
end
end

View File

@@ -0,0 +1,58 @@
require 'chef/provider/lwrp_base'
require_relative 'helpers'
class Chef
class Provider
class MysqlConfig < Chef::Provider::LWRPBase
include MysqlCookbook::Helpers
use_inline_resources if defined?(use_inline_resources)
def whyrun_supported?
true
end
action :create do
group "#{new_resource.name} :create #{new_resource.group}" do
group_name new_resource.group
system true if new_resource.name == 'mysql'
action :create
end
user "#{new_resource.name} :create #{new_resource.owner}" do
username new_resource.owner
gid new_resource.owner
system true if new_resource.name == 'mysql'
action :create
end
directory "#{new_resource.name} :create #{include_dir}" do
path include_dir
owner new_resource.owner
group new_resource.group
mode '0750'
recursive true
action :create
end
template "#{new_resource.name} :create #{include_dir}/#{new_resource.config_name}.cnf" do
path "#{include_dir}/#{new_resource.config_name}.cnf"
owner new_resource.owner
group new_resource.group
mode '0640'
variables(new_resource.variables)
source new_resource.source
cookbook new_resource.cookbook
action :create
end
end
action :delete do
file "#{new_resource.name} :delete #{include_dir}/#{new_resource.config_name}.conf" do
path "#{include_dir}/#{new_resource.config_name}.conf"
action :delete
end
end
end
end
end

View File

@@ -0,0 +1,251 @@
require 'chef/provider/lwrp_base'
require_relative 'helpers'
class Chef
class Provider
class MysqlService < Chef::Provider::LWRPBase
# Chef 11 LWRP DSL Methods
use_inline_resources if defined?(use_inline_resources)
def whyrun_supported?
true
end
# Mix in helpers from libraries/helpers.rb
include MysqlCookbook::Helpers
# Service related methods referred to in the :create and :delete
# actions need to be implemented in the init system subclasses.
#
# create_stop_system_service
# delete_stop_service
# All other methods are found in libraries/helpers.rb
#
# etc_dir, run_dir, log_dir, etc
action :create do
# Yum, Apt, etc. From helpers.rb
configure_package_repositories
# Software installation
package "#{new_resource.name} :create #{server_package_name}" do
package_name server_package_name
version parsed_version if node['platform'] == 'smartos'
version new_resource.package_version
action new_resource.package_action
end
create_stop_system_service
# Apparmor
configure_apparmor
# System users
group "#{new_resource.name} :create mysql" do
group_name 'mysql'
action :create
end
user "#{new_resource.name} :create mysql" do
username 'mysql'
gid 'mysql'
action :create
end
# Yak shaving secion. Account for random errata.
#
# Turns out that mysqld is hard coded to try and read
# /etc/mysql/my.cnf, and its presence causes problems when
# setting up multiple services.
file "#{new_resource.name} :create #{prefix_dir}/etc/mysql/my.cnf" do
path "#{prefix_dir}/etc/mysql/my.cnf"
action :delete
end
file "#{new_resource.name} :create #{prefix_dir}/etc/my.cnf" do
path "#{prefix_dir}/etc/my.cnf"
action :delete
end
# mysql_install_db is broken on 5.6.13
link "#{new_resource.name} :create #{prefix_dir}/usr/share/my-default.cnf" do
target_file "#{prefix_dir}/usr/share/my-default.cnf"
to "#{etc_dir}/my.cnf"
action :create
end
# Support directories
directory "#{new_resource.name} :create #{etc_dir}" do
path etc_dir
owner new_resource.run_user
group new_resource.run_group
mode '0750'
recursive true
action :create
end
directory "#{new_resource.name} :create #{include_dir}" do
path include_dir
owner new_resource.run_user
group new_resource.run_group
mode '0750'
recursive true
action :create
end
directory "#{new_resource.name} :create #{run_dir}" do
path run_dir
owner new_resource.run_user
group new_resource.run_group
mode '0755'
recursive true
action :create
end
directory "#{new_resource.name} :create #{log_dir}" do
path log_dir
owner new_resource.run_user
group new_resource.run_group
mode '0750'
recursive true
action :create
end
directory "#{new_resource.name} :create #{parsed_data_dir}" do
path parsed_data_dir
owner new_resource.run_user
group new_resource.run_group
mode '0750'
recursive true
action :create
end
# Main configuration file
template "#{new_resource.name} :create #{etc_dir}/my.cnf" do
path "#{etc_dir}/my.cnf"
source 'my.cnf.erb'
cookbook 'mysql'
owner new_resource.run_user
group new_resource.run_group
mode '0600'
variables(
config: new_resource,
error_log: error_log,
include_dir: include_dir,
lc_messages_dir: lc_messages_dir,
pid_file: pid_file,
socket_file: socket_file,
tmp_dir: tmp_dir,
data_dir: parsed_data_dir
)
action :create
end
# initialize database and create initial records
bash "#{new_resource.name} :create initial records" do
code init_records_script
returns [0, 1, 2] # facepalm
not_if "/usr/bin/test -f #{parsed_data_dir}/mysql/user.frm"
action :run
end
end
action :delete do
# Stop the service before removing support directories
delete_stop_service
directory "#{new_resource.name} :delete #{etc_dir}" do
path etc_dir
recursive true
action :delete
end
directory "#{new_resource.name} :delete #{run_dir}" do
path run_dir
recursive true
action :delete
end
directory "#{new_resource.name} :delete #{log_dir}" do
path log_dir
recursive true
action :delete
end
end
#
# Platform specific bits
#
def configure_apparmor
# Do not add these resource if inside a container
# Only valid on Ubuntu
unless ::File.exist?('/.dockerenv') || ::File.exist?('/.dockerinit')
if node['platform'] == 'ubuntu'
# Apparmor
package "#{new_resource.name} :create apparmor" do
package_name 'apparmor'
action :install
end
directory "#{new_resource.name} :create /etc/apparmor.d/local/mysql" do
path '/etc/apparmor.d/local/mysql'
owner 'root'
group 'root'
mode '0755'
recursive true
action :create
end
template "#{new_resource.name} :create /etc/apparmor.d/local/usr.sbin.mysqld" do
path '/etc/apparmor.d/local/usr.sbin.mysqld'
cookbook 'mysql'
source 'apparmor/usr.sbin.mysqld-local.erb'
owner 'root'
group 'root'
mode '0644'
action :create
notifies :restart, "service[#{new_resource.name} :create apparmor]", :immediately
end
template "#{new_resource.name} :create /etc/apparmor.d/usr.sbin.mysqld" do
path '/etc/apparmor.d/usr.sbin.mysqld'
cookbook 'mysql'
source 'apparmor/usr.sbin.mysqld.erb'
owner 'root'
group 'root'
mode '0644'
action :create
notifies :restart, "service[#{new_resource.name} :create apparmor]", :immediately
end
template "#{new_resource.name} :create /etc/apparmor.d/local/mysql/#{new_resource.instance}" do
path "/etc/apparmor.d/local/mysql/#{new_resource.instance}"
cookbook 'mysql'
source 'apparmor/usr.sbin.mysqld-instance.erb'
owner 'root'
group 'root'
mode '0644'
variables(
data_dir: parsed_data_dir,
mysql_name: mysql_name,
log_dir: log_dir,
run_dir: run_dir,
pid_file: pid_file,
socket_file: socket_file
)
action :create
notifies :restart, "service[#{new_resource.name} :create apparmor]", :immediately
end
service "#{new_resource.name} :create apparmor" do
service_name 'apparmor'
action :nothing
end
end
end
end
end
end
end

View File

@@ -0,0 +1,85 @@
class Chef
class Provider
class MysqlService
class Smf < Chef::Provider::MysqlService
action :start do
method_script_path = "/lib/svc/method/#{mysql_name}" if node['platform'] == 'omnios'
method_script_path = "/opt/local/lib/svc/method/#{mysql_name}" if node['platform'] == 'smartos'
template "#{new_resource.name} :start #{method_script_path}" do
path method_script_path
cookbook 'mysql'
source 'smf/svc.method.mysqld.erb'
owner 'root'
group 'root'
mode '0555'
variables(
base_dir: base_dir,
data_dir: parsed_data_dir,
defaults_file: defaults_file,
error_log: error_log,
mysql_name: mysql_name,
mysqld_bin: mysqld_bin,
pid_file: pid_file
)
action :create
end
smf "#{new_resource.name} :start #{mysql_name}" do
name mysql_name
user new_resource.run_user
group new_resource.run_group
start_command "#{method_script_path} start"
end
service "#{new_resource.name} :start #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Solaris
supports restart: true
action [:enable]
end
end
action :stop do
service "#{new_resource.name} :stop #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Solaris
supports restart: true
action :stop
end
end
action :restart do
service "#{new_resource.name} :restart #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Solaris
supports restart: true
action :restart
end
end
action :reload do
service "#{new_resource.name} :reload #{mysql_name}" do
provider Chef::Provider::Service::Solaris
service_name mysql_name
supports reload: true
action :reload
end
end
def create_stop_system_service
# nothing to do here
end
def delete_stop_service
service "#{new_resource.name} :delete #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Solaris
supports restart: true
action :stop
end
end
end
end
end
end

View File

@@ -0,0 +1,121 @@
class Chef
class Provider
class MysqlService
class Systemd < Chef::Provider::MysqlService
action :start do
# this script is called by the main systemd unit file, and
# spins around until the service is actually up and running.
template "#{new_resource.name} :start /usr/libexec/#{mysql_name}-wait-ready" do
path "/usr/libexec/#{mysql_name}-wait-ready"
source 'systemd/mysqld-wait-ready.erb'
owner 'root'
group 'root'
mode '0755'
variables(socket_file: socket_file)
cookbook 'mysql'
action :create
end
# this is the main systemd unit file
template "#{new_resource.name} :start /usr/lib/systemd/system/#{mysql_name}.service" do
path "/usr/lib/systemd/system/#{mysql_name}.service"
source 'systemd/mysqld.service.erb'
owner 'root'
group 'root'
mode '0644'
variables(
config: new_resource,
etc_dir: etc_dir,
base_dir: base_dir,
mysqld_bin: mysqld_bin
)
cookbook 'mysql'
notifies :run, "execute[#{new_resource.name} :start systemctl daemon-reload]", :immediately
action :create
end
# avoid 'Unit file changed on disk' warning
execute "#{new_resource.name} :start systemctl daemon-reload" do
command '/usr/bin/systemctl daemon-reload'
action :nothing
end
# tmpfiles.d config so the service survives reboot
template "#{new_resource.name} :start /usr/lib/tmpfiles.d/#{mysql_name}.conf" do
path "/usr/lib/tmpfiles.d/#{mysql_name}.conf"
source 'tmpfiles.d.conf.erb'
owner 'root'
group 'root'
mode '0644'
variables(
run_dir: run_dir,
run_user: new_resource.run_user,
run_group: new_resource.run_group
)
cookbook 'mysql'
action :create
end
# service management resource
service "#{new_resource.name} :start #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Systemd
supports restart: true, status: true
action [:enable, :start]
end
end
action :stop do
# service management resource
service "#{new_resource.name} :stop #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Systemd
supports status: true
action [:disable, :stop]
only_if { ::File.exist?("/usr/lib/systemd/system/#{mysql_name}.service") }
end
end
action :restart do
# service management resource
service "#{new_resource.name} :restart #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Systemd
supports restart: true
action :restart
end
end
action :reload do
# service management resource
service "#{new_resource.name} :reload #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Systemd
action :reload
end
end
def create_stop_system_service
# service management resource
service "#{new_resource.name} :create mysql" do
service_name 'mysqld'
provider Chef::Provider::Service::Systemd
supports status: true
action [:stop, :disable]
end
end
def delete_stop_service
# service management resource
service "#{new_resource.name} :delete #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Systemd
supports status: true
action [:disable, :stop]
only_if { ::File.exist?("/usr/lib/systemd/system/#{mysql_name}.service") }
end
end
end
end
end
end

View File

@@ -0,0 +1,87 @@
class Chef
class Provider
class MysqlService
class Sysvinit < Chef::Provider::MysqlService
action :start do
template "#{new_resource.name} :start /etc/init.d/#{mysql_name}" do
path "/etc/init.d/#{mysql_name}"
source 'sysvinit/mysqld.erb'
owner 'root'
group 'root'
mode '0755'
variables(
config: new_resource,
defaults_file: defaults_file,
error_log: error_log,
mysql_name: mysql_name,
mysqladmin_bin: mysqladmin_bin,
mysqld_safe_bin: mysqld_safe_bin,
pid_file: pid_file,
scl_name: scl_name
)
cookbook 'mysql'
action :create
end
service "#{new_resource.name} :start #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Init::Redhat if node['platform_family'] == 'redhat'
provider Chef::Provider::Service::Init::Insserv if node['platform_family'] == 'debian'
supports restart: true, status: true
action [:enable, :start]
end
end
action :stop do
service "#{new_resource.name} :stop #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Init::Redhat if node['platform_family'] == 'redhat'
provider Chef::Provider::Service::Init::Insserv if node['platform_family'] == 'debian'
supports restart: true, status: true
action [:stop]
end
end
action :restart do
service "#{new_resource.name} :restart #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Init::Redhat if node['platform_family'] == 'redhat'
provider Chef::Provider::Service::Init::Insserv if node['platform_family'] == 'debian'
supports restart: true
action :restart
end
end
action :reload do
service "#{new_resource.name} :reload #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Init::Redhat if node['platform_family'] == 'redhat'
provider Chef::Provider::Service::Init::Insserv if node['platform_family'] == 'debian'
action :reload
end
end
def create_stop_system_service
service "#{new_resource.name} :create #{system_service_name}" do
service_name system_service_name
provider Chef::Provider::Service::Init::Redhat if node['platform_family'] == 'redhat'
provider Chef::Provider::Service::Init::Insserv if node['platform_family'] == 'debian'
supports status: true
action [:stop, :disable]
end
end
def delete_stop_service
service "#{new_resource.name} :delete #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Init::Redhat if node['platform_family'] == 'redhat'
provider Chef::Provider::Service::Init::Insserv if node['platform_family'] == 'debian'
supports status: true
action [:disable, :stop]
only_if { ::File.exist?("#{etc_dir}/init.d/#{mysql_name}") }
end
end
end
end
end
end

View File

@@ -0,0 +1,107 @@
class Chef
class Provider
class MysqlService
class Upstart < Chef::Provider::MysqlService
action :start do
template "#{new_resource.name} :start /usr/sbin/#{mysql_name}-wait-ready" do
path "/usr/sbin/#{mysql_name}-wait-ready"
source 'upstart/mysqld-wait-ready.erb'
owner 'root'
group 'root'
mode '0755'
variables(socket_file: socket_file)
cookbook 'mysql'
action :create
end
template "#{new_resource.name} :start /etc/init/#{mysql_name}.conf" do
path "/etc/init/#{mysql_name}.conf"
source 'upstart/mysqld.erb'
owner 'root'
group 'root'
mode '0644'
variables(
defaults_file: defaults_file,
mysql_name: mysql_name,
run_group: new_resource.run_group,
run_user: new_resource.run_user,
socket_dir: socket_dir
)
cookbook 'mysql'
action :create
end
service "#{new_resource.name} :start #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
supports status: true
action [:start]
end
end
action :stop do
service "#{new_resource.name} :stop #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
supports restart: true, status: true
action [:stop]
end
end
action :restart do
# With Upstart, restarting the service doesn't behave "as expected".
# We want the post-start stanzas, which wait until the
# service is available before returning
#
# http://upstart.ubuntu.com/cookbook/#restart
service "#{new_resource.name} :restart stop #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
action :stop
end
service "#{new_resource.name} :restart start #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
action :start
end
end
action :reload do
# With Upstart, reload just sends a HUP signal to the process.
# As far as I can tell, this doesn't work the way it's
# supposed to, so we need to actually restart the service.
service "#{new_resource.name} :reload stop #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
action :stop
end
service "#{new_resource.name} :reload start #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
action :start
end
end
def create_stop_system_service
service "#{new_resource.name} :create #{system_service_name}" do
service_name system_service_name
provider Chef::Provider::Service::Upstart
supports status: true
action [:stop, :disable]
end
end
def delete_stop_service
service "#{new_resource.name} :delete #{mysql_name}" do
service_name mysql_name
provider Chef::Provider::Service::Upstart
action [:disable, :stop]
only_if { ::File.exist?("#{etc_dir}/init/#{mysql_name}") }
end
end
end
end
end
end

View File

@@ -0,0 +1,16 @@
require 'chef/resource/lwrp_base'
class Chef
class Resource
class MysqlClient < Chef::Resource::LWRPBase
self.resource_name = :mysql_client
actions :create, :delete
default_action :create
attribute :client_name, kind_of: String, name_attribute: true, required: true
attribute :package_name, kind_of: Array, default: nil
attribute :package_version, kind_of: String, default: nil
attribute :version, kind_of: String, default: nil # mysql_version
end
end
end

View File

@@ -0,0 +1,20 @@
require 'chef/resource/lwrp_base'
class Chef
class Resource
class MysqlConfig < Chef::Resource::LWRPBase
self.resource_name = :mysql_config
actions :create, :delete
default_action :create
attribute :config_name, kind_of: String, name_attribute: true, required: true
attribute :cookbook, kind_of: String, default: nil
attribute :group, kind_of: String, default: 'mysql'
attribute :instance, kind_of: String, default: 'default'
attribute :owner, kind_of: String, default: 'mysql'
attribute :source, kind_of: String, default: nil
attribute :variables, kind_of: [Hash], default: nil
attribute :version, kind_of: String, default: nil
end
end
end

View File

@@ -0,0 +1,25 @@
require 'chef/resource/lwrp_base'
class Chef
class Resource
class MysqlService < Chef::Resource::LWRPBase
self.resource_name = :mysql_service
actions :create, :delete, :start, :stop, :restart, :reload
default_action :create
attribute :charset, kind_of: String, default: 'utf8'
attribute :data_dir, kind_of: String, default: nil
attribute :initial_root_password, kind_of: String, default: 'ilikerandompasswords'
attribute :instance, kind_of: String, name_attribute: true
attribute :package_action, kind_of: Symbol, default: :install
attribute :package_name, kind_of: String, default: nil
attribute :package_version, kind_of: String, default: nil
attribute :bind_address, kind_of: String, default: nil
attribute :port, kind_of: String, default: '3306'
attribute :run_group, kind_of: String, default: 'mysql'
attribute :run_user, kind_of: String, default: 'mysql'
attribute :socket, kind_of: String, default: nil
attribute :version, kind_of: String, default: nil
end
end
end

View File

@@ -0,0 +1,47 @@
# provider mappings for Chef 11
#########
# service
#########
Chef::Platform.set platform: :amazon, resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :centos, version: '< 7.0', resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :centos, version: '>= 7.0', resource: :mysql_service, provider: Chef::Provider::MysqlService::Systemd
Chef::Platform.set platform: :debian, resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :fedora, version: '< 19', resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :fedora, version: '>= 19', resource: :mysql_service, provider: Chef::Provider::MysqlService::Systemd
Chef::Platform.set platform: :omnios, resource: :mysql_service, provider: Chef::Provider::MysqlService::Smf
Chef::Platform.set platform: :redhat, version: '< 7.0', resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :redhat, version: '>= 7.0', resource: :mysql_service, provider: Chef::Provider::MysqlService::Systemd
Chef::Platform.set platform: :scientific, version: '< 7.0', resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :scientific, version: '>= 7.0', resource: :mysql_service, provider: Chef::Provider::MysqlService::Systemd
Chef::Platform.set platform: :smartos, resource: :mysql_service, provider: Chef::Provider::MysqlService::Smf
Chef::Platform.set platform: :suse, resource: :mysql_service, provider: Chef::Provider::MysqlService::Sysvinit
Chef::Platform.set platform: :ubuntu, resource: :mysql_service, provider: Chef::Provider::MysqlService::Upstart
#########
# config
#########
Chef::Platform.set platform: :amazon, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :centos, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :debian, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :fedora, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :omnios, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :redhat, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :scientific, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :smartos, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :suse, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
Chef::Platform.set platform: :ubuntu, resource: :mysql_config, provider: Chef::Provider::MysqlConfig
#########
# client
#########
Chef::Platform.set platform: :amazon, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :centos, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :debian, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :fedora, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :omnios, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :redhat, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :scientific, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :smartos, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :suse, resource: :mysql_client, provider: Chef::Provider::MysqlClient
Chef::Platform.set platform: :ubuntu, resource: :mysql_client, provider: Chef::Provider::MysqlClient

View File

@@ -0,0 +1 @@
{"name":"mysql","version":"6.0.22","description":"Provides mysql_service, mysql_config, and mysql_client resources","long_description":"","maintainer":"Chef Software, Inc.","maintainer_email":"cookbooks@chef.io","license":"Apache 2.0","platforms":{"amazon":">= 0.0.0","redhat":">= 0.0.0","centos":">= 0.0.0","scientific":">= 0.0.0","fedora":">= 0.0.0","debian":">= 0.0.0","ubuntu":">= 0.0.0","smartos":">= 0.0.0","omnios":">= 0.0.0","suse":">= 0.0.0"},"dependencies":{"yum-mysql-community":">= 0.0.0","smf":">= 0.0.0"},"recommendations":{},"suggestions":{},"conflicting":{},"providing":{},"replacing":{},"attributes":{},"groupings":{},"recipes":{}}

View File

@@ -0,0 +1,13 @@
/etc/<%= @mysql_name %>/*.pem r,
/etc/<%= @mysql_name %>/conf.d/ r,
/etc/<%= @mysql_name %>/conf.d/* r,
/etc/<%= @mysql_name %>/my.cnf r,
<%= @log_dir %>/ r,
<%= @log_dir %>/* rw,
<%= @data_dir %>/ r,
<%= @data_dir %>/** rwk,
<%= @run_dir %>/** rw,
<%= @pid_file %> rw,
<%= @socket_file %> rw,
/tmp/<%= @mysql_name %>/ r,
/tmp/<%= @mysql_name %>/my.sql r,

View File

@@ -0,0 +1 @@
#include <local/mysql>

View File

@@ -0,0 +1,45 @@
# vim:syntax=apparmor
# Last Modified: Tue Jun 19 17:37:30 2007
#include <tunables/global>
/usr/sbin/mysqld {
#include <abstractions/base>
#include <abstractions/nameservice>
#include <abstractions/user-tmp>
#include <abstractions/mysql>
#include <abstractions/winbind>
capability dac_override,
capability sys_resource,
capability setgid,
capability setuid,
network tcp,
/etc/hosts.allow r,
/etc/hosts.deny r,
/etc/mysql/*.pem r,
/etc/mysql/conf.d/ r,
/etc/mysql/conf.d/* r,
/etc/mysql/*.cnf r,
/usr/lib/mysql/plugin/ r,
/usr/lib/mysql/plugin/*.so* mr,
/usr/sbin/mysqld mr,
/usr/share/mysql/** r,
/var/log/mysql.log rw,
/var/log/mysql.err rw,
/var/lib/mysql/ r,
/var/lib/mysql/** rwk,
/var/log/mysql/ r,
/var/log/mysql/* rw,
/var/run/mysqld/mysqld.pid rw,
/var/run/mysqld/mysqld.sock w,
/run/mysqld/mysqld.pid rw,
/run/mysqld/mysqld.sock w,
/sys/devices/system/cpu/ r,
# Site-specific additions and overrides. See local/README for details.
#include <local/usr.sbin.mysqld>
}

View File

@@ -0,0 +1,54 @@
# Chef generated my.cnf for instance mysql-<%= @config.name %>
[client]
<% if @config.charset %>
default-character-set = <%= @config.charset %>
<% end %>
<% if @config.port %>
port = <%= @config.port %>
<% end %>
<% if @socket_file %>
socket = <%= @socket_file %>
<% end %>
[mysql]
<% if @config.charset %>
default-character-set = <%= @config.charset %>
<% end %>
[mysqld]
<% if @config.run_user %>
user = <%= @config.run_user %>
<% end %>
<% if @pid_file %>
pid-file = <%= @pid_file %>
<% end %>
<% if @socket_file %>
socket = <%= @socket_file %>
<% end %>
<% if @config.bind_address %>
bind-address = <%= @config.bind_address %>
<% end %>
<% if @config.port %>
port = <%= @config.port %>
<% end %>
<% if @data_dir %>
datadir = <%= @data_dir %>
<% end %>
<% if @tmp_dir %>
tmpdir = <%= @tmp_dir %>
<% end %>
<% if @lc_messages_dir %>
lc-messages-dir = <%= @lc_messages_dir %>
<% end %>
<% if @error_log %>
log-error = <%= @error_log %>
<% end %>
<% if @include_dir %>
!includedir <%= @include_dir %>
<% end %>
[mysqld_safe]
<% if @socket_file %>
socket = <%= @socket_file %>
<% end %>

View File

@@ -0,0 +1,28 @@
#!/sbin/sh
#
# Generated by Chef
#
. /lib/svc/share/smf_include.sh
ulimit -n 10240
case "$1" in
start)
<%= @mysqld_bin %> \
--defaults-file=<%= @defaults_file %> \
--basedir=<%= @base_dir %> \
--datadir=<%= @data_dir %> \
--pid-file=<%= @pid_file %> \
--log-error=<%= @error_log %> &
;;
stop)
[ -f <%= @pid_file %> ] && kill `/usr/bin/head -1 <%= @pid_file %>`
;;
*)
echo "Usage: $0 {start|stop}" >&2
exit 1
;;
esac
exit $SMF_EXIT_OK

View File

@@ -0,0 +1,30 @@
#!/bin/sh
daemon_pid="$1"
# Wait for the server to come up or for the mysqld process to disappear
ret=0
while /bin/true; do
RESPONSE=`/usr/bin/mysqladmin --no-defaults --socket="<%= @socket_file %>" --user=UNKNOWN_MYSQL_USER ping 2>&1`
mret=$?
if [ $mret -eq 0 ]; then
break
fi
# exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,
# anything else suggests a configuration error
if [ $mret -ne 1 -a $mret -ne 11 ]; then
ret=1
break
fi
# "Access denied" also means the server is alive
echo "$RESPONSE" | grep -q "Access denied for user" && break
# Check process still exists
if ! /bin/kill -0 $daemon_pid 2>/dev/null; then
ret=1
break
fi
sleep 1
done
exit $ret

View File

@@ -0,0 +1,16 @@
[Unit]
Description=mysql_service[mysql-<%= @config.instance %>]
After=syslog.target
After=network.target
[Service]
Type=simple
User=<%= @config.run_user %>
Group=<%= @config.run_group %>
ExecStart=<%= @mysqld_bin %> --defaults-file=<%= @etc_dir %>/my.cnf --basedir=<%= @base_dir %>
ExecStartPost=/usr/libexec/mysql-<%= @config.instance %>-wait-ready $MAINPID
TimeoutSec=300
PrivateTmp=true
[Install]
WantedBy=multi-user.target

View File

@@ -0,0 +1,266 @@
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: <%= @mysql_name %>
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $network $time
# Should-Stop: $network $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop the mysql database server daemon
# Description: Controls the main MySQL database server daemon "mysqld"
# and its wrapper script "mysqld_safe".
### END INIT INFO
# set -e
# set -u
### Exit code reference
# http://fedoraproject.org/wiki/Packaging:SysVInitScript
# http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
# Source functions
<% if node['platform_family'] == 'rhel' %>
# Source RHEL function library.
. /etc/rc.d/init.d/functions
<% end %>
####
# Variables
####
STARTTIMEOUT=30
STOPTIMEOUT=15
####
# Helper functions
###
# Boolean function to see if MYSQL_PID exists and is a number
pid_exists() {
PID_EXISTS=1
if [ -f <%= @pid_file %> ]; then
MYSQLD_PID=`cat <%= @pid_file %> 2>/dev/null`
if [ -n "$MYSQLD_PID" ] && [ -d "/proc/$MYSQLD_PID" ] ; then
PID_EXISTS=0
fi
fi
return $PID_EXISTS
}
# Use mysqladmin to ping the service as an invalid user over a socket
running() {
RUNNING=1
RESPONSE=`<%= @mysqladmin_bin %> --defaults-file=<%= @defaults_file %> --user=UNKNOWN_MYSQL_USER ping 2>&1`
local mret=$?
if pid_exists \
&& [ $mret -eq 0 ] \
|| [ `echo $RESPONSE | grep -q "Access denied for user"` ]; then
RUNNING=0
fi
return $RUNNING
}
writable_error_log() {
WRITABLE_ERROR_LOG=1
touch "<%= @error_log %>" 2>/dev/null
touchret=$?
if [ $touchret -eq 0 ]; then
chown <%= @config.run_user %>:<%= @config.run_group %> <%= @error_log %>
return 0
else
return $WRITABLE_ERROR_LOG
fi
}
print_start_success() {
<% if node['platform_family'] == 'rhel' %>
action $"Starting <%= @mysql_name %>: " /bin/true
<% else %>
echo "Staring MySQL instance <%= @mysql_name %>"
<% end %>
return 0;
}
print_start_failure() {
<% if node['platform_family'] == 'rhel' %>
action $"Starting <%= @mysql_name %>: " /bin/false
<% else %>
echo "Could not start MySQL instance <%= @mysql_name %>"
<% end %>
return 0;
}
print_reload_success() {
<% if node['platform_family'] == 'rhel' %>
action $"Reloading <%= @mysql_name %>" /bin/true
<% else %>
echo "Reload success for <%= @mysql_name %>"
<% end %>
return 0;
}
print_reload_failure() {
<% if node['platform_family'] == 'rhel' %>
action $"Reloading <%= @mysql_name %>" /bin/false
<% else %>
echo "Reload failed for <%= @mysql_name %>"
<% end %>
return 0;
}
print_stop_success() {
<% if node['platform_family'] == 'rhel' %>
action $"Stopping <%= @mysql_name %>: " /bin/true
<% else %>
echo "Stopping MySQL instance <%= @mysql_name %>"
<% end %>
return 0;
}
print_stop_failure() {
<% if node['platform_family'] == 'rhel' %>
action $"Stopping <%= @mysql_name %>: " /bin/false
<% else %>
echo "Could not stop MySQL instance <%= @mysql_name %>"
<% end %>
return 0;
}
start_command() {
# Attempt to start <%= @mysql_name %>
echo "Starting MySQL instance <%= @mysql_name %>"
local scl_name="<%= @scl_name %>"
if [ -z $scl_name ]; then
<%= @mysqld_safe_bin %> \
--defaults-file=<%= @defaults_file %> \
>/dev/null 2>&1 &
local pid=$!
else
scl enable $scl_name "<%= @mysqld_safe_bin %> \
--defaults-file=<%= @defaults_file %> \
>/dev/null 2>&1 &"
local pid=$!
fi
return $pid
}
####
# Init script actions
###
# Start <%= @mysql_name %>
start() {
# exit 0 if already running.
if running; then
print_start_success
return 0;
fi
# exit 4 if we can't write to error_log
if ! writable_error_log; then
print_start_failure
return 4
fi
# run program
start_command;
start_pid=$?
# Timeout loop
local TIMEOUT=$STARTTIMEOUT
while [ $TIMEOUT -gt 0 ]; do
if running; then
break
fi
sleep 1
let TIMEOUT=${TIMEOUT}-1
done
# Handle timeout
if [ $TIMEOUT -eq 0 ]; then
print_start_failure
# clean up
kill $start_pid 2>/dev/null
return 1
fi
# successbaby.gif
print_start_success
return 0
}
# Reload <%= @mysql_name %>
reload() {
<%= @mysqladmin_bin %> reload
local ret=$?
if [ $ret -eq 0 ]; then
print_reload_success;
else
print_reload_failure;
fi
return $ret
}
# Status of <%= @mysql_name %>
status() {
if running; then
echo "<%= @mysql_name %> is running"
return 0
else
echo "<%= @mysql_name %> is not running"
return 1
fi
}
# Stop <%= @mysql_name %>
stop() {
if running; then
echo "Stopping MySQL instance <%= @mysql_name %>"
if [ -f <%= @pid_file %> ]; then
/bin/kill `cat <%= @pid_file %> 2>/dev/null`
kstat=$?
fi
# Timeout loop
local TIMEOUT=$STARTTIMEOUT
while [ $TIMEOUT -gt 0 ]; do
if [ -e <%= @pid_file %> ]; then
sleep 1
fi
let TIMEOUT=${TIMEOUT}-1
done
return $kstat
else
echo "MySQL instance <%= @mysql_name %> Stopped."
return 0
fi
}
# main()
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop ; start
;;
reload)
reload
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 2
esac
exit $?

View File

@@ -0,0 +1 @@
d <%= @run_dir %> 0755 <%= @run_user %> <%= @run_group %> -

View File

@@ -0,0 +1,22 @@
#!/bin/sh
# Wait for the server to come up
ret=0
while /bin/true; do
RESPONSE=`/usr/bin/mysqladmin --no-defaults --socket="<%= @socket_file %>" --user=UNKNOWN_MYSQL_USER ping 2>&1`
mret=$?
if [ $mret -eq 0 ]; then
break
fi
# exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,
# anything else suggests a configuration error
if [ $mret -ne 1 -a $mret -ne 11 ]; then
ret=1
break
fi
# "Access denied" also means the server is alive
echo "$RESPONSE" | grep -q "Access denied for user" && break
sleep 1
done
exit $ret

View File

@@ -0,0 +1,26 @@
# <%= @mysql_name %> Service
description "MySQL service <%= @mysql_name %>"
author "chef-client"
start on runlevel [2345]
stop on starting rc RUNLEVEL=[016]
respawn
respawn limit 2 5
env HOME=/etc/<%= @mysql_name %>
umask 007
kill timeout 300
pre-start script
[ -d /run/<%= @mysql_name %> ] || install -m 755 -o <%= @run_user %> -g <%= @run_group %> -d /run/<%= @mysql_name %>
[ -d <%= @socket_dir %> ] || install -m 755 -o <%= @run_user %> -g <%= @run_group %> -d <%= @socket_dir %>
end script
exec /usr/sbin/mysqld --defaults-file=<%= @defaults_file %>
post-start script
/usr/sbin/<%= @mysql_name %>-wait-ready
end script