mysql_isolated_server 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql_isolated_server.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ben Osheroff
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MysqlIsolatedServer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mysql_isolated_server'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mysql_isolated_server
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class MysqlIsolatedServer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,123 @@
1
+ require 'tmpdir'
2
+ require 'socket'
3
+ require 'mysql2'
4
+
5
+ class MysqlIsolatedServer
6
+ attr_reader :pid, :base, :port
7
+ MYSQL_BASE_DIR="/usr"
8
+
9
+ def initialize(options = {})
10
+ @base = Dir.mktmpdir("/tmp/mysql_isolated")
11
+ @mysql_data_dir="#{@base}/mysqld"
12
+ @mysql_socket="#{@mysql_data_dir}/mysqld.sock"
13
+ @params = options[:params]
14
+ @load_data_path = options[:data_path]
15
+ @port = options[:port]
16
+ @allow_output = options[:allow_output]
17
+ end
18
+
19
+ def make_slave_of(master)
20
+ master_binlog_info = master.connection.query("show master status").first
21
+ connection.query(<<-EOL
22
+ change master to master_host='127.0.0.1',
23
+ master_port=#{master.port},
24
+ master_user='root', master_password='',
25
+ master_log_file='#{master_binlog_info['File']}',
26
+ master_log_pos=#{master_binlog_info['Position']}
27
+ EOL
28
+ )
29
+ connection.query("SLAVE START")
30
+ end
31
+
32
+ def connection
33
+ @cx ||= Mysql2::Client.new(:host => "127.0.0.1", :port => @port, :username => "root", :password => "", :database => "mysql")
34
+ end
35
+
36
+ def set_rw(rw)
37
+ ro = rw ? 0 : 1
38
+ connection.query("SET GLOBAL READ_ONLY=#{ro}")
39
+ end
40
+
41
+
42
+ def locate_executable(*candidates)
43
+ output = `which #{candidates.join(' ')}`
44
+ return nil if output == "\n"
45
+ output.split("\n").first
46
+ end
47
+
48
+ def boot!
49
+ @port ||= grab_free_port
50
+ system("rm -Rf #{@mysql_data_dir}")
51
+ system("mkdir #{@mysql_data_dir}")
52
+ if @load_data_path
53
+ system("cp -a #{@load_data_path}/* #{@mysql_data_dir}")
54
+ system("rm -f #{@mysql_data_dir}/relay-log.info")
55
+ else
56
+ mysql_install_db = `which mysql_install_db`
57
+ idb_path = File.dirname(mysql_install_db)
58
+ system("(cd #{idb_path}/..; mysql_install_db --datadir=#{@mysql_data_dir} --user=`whoami`) >/dev/null 2>&1")
59
+ system("cp #{File.expand_path(File.dirname(__FILE__))}/tables/user.* #{@mysql_data_dir}/mysql")
60
+ end
61
+
62
+ exec_server <<-EOL
63
+ mysqld --no-defaults --default-storage-engine=innodb \
64
+ --datadir=#{@mysql_data_dir} --pid-file=#{@base}/mysqld.pid --port=#{@port} \
65
+ #{@params} --socket=#{@mysql_data_dir}/mysql.sock --log-bin --log-slave-updates
66
+ EOL
67
+
68
+ while !system("mysql -h127.0.0.1 --port=#{@port} --database=mysql -u root -e 'select 1' >/dev/null 2>&1")
69
+ sleep(0.1)
70
+ end
71
+
72
+ tzinfo_to_sql = locate_executable("mysql_tzinfo_to_sql5", "mysql_tzinfo_to_sql")
73
+ raise "could not find mysql_tzinfo_to_sql" unless tzinfo_to_sql
74
+ system("#{tzinfo_to_sql} /usr/share/zoneinfo 2>/dev/null | mysql -h127.0.0.1 --database=mysql --port=#{@port} -u root mysql ")
75
+
76
+ system(%Q(mysql -h127.0.0.1 --port=#{@port} --database=mysql -u root -e "SET GLOBAL time_zone='UTC'"))
77
+ system(%Q(mysql -h127.0.0.1 --port=#{@port} --database=mysql -u root -e "GRANT SELECT ON *.* to 'zdslave'@'localhost'"))
78
+ end
79
+
80
+ def grab_free_port
81
+ while true
82
+ candidate=9000 + rand(50_000)
83
+
84
+ begin
85
+ socket = Socket.new(:INET, :STREAM, 0)
86
+ socket.bind(Addrinfo.tcp("127.0.0.1", candidate))
87
+ socket.close
88
+ return candidate
89
+ rescue Exception => e
90
+ puts e
91
+ end
92
+ end
93
+ end
94
+
95
+ attr_reader :pid
96
+ def exec_server(cmd)
97
+ cmd.strip!
98
+ cmd.gsub!(/\\\n/, ' ')
99
+ devnull = File.open("/dev/null", "w")
100
+ system("mkdir -p #{base}/tmp")
101
+ system("chmod 0777 #{base}/tmp")
102
+ pid = fork do
103
+ ENV["TMPDIR"] = "#{base}/tmp"
104
+ if !@allow_output
105
+ STDOUT.reopen(devnull)
106
+ STDERR.reopen(devnull)
107
+ end
108
+
109
+ exec(cmd)
110
+ end
111
+ at_exit {
112
+ Process.kill("TERM", pid)
113
+ system("rm -Rf #{base}")
114
+ }
115
+ @pid = pid
116
+ devnull.close
117
+ end
118
+
119
+ def kill!
120
+ return unless @pid
121
+ system("kill -KILL #{@pid}")
122
+ end
123
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_isolated_server/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "mysql_isolated_server"
8
+ gem.version = MysqlIsolatedServer::VERSION
9
+ gem.authors = ["Ben Osheroff"]
10
+ gem.email = ["ben@zendesk.com"]
11
+ gem.description = %q{A small library that allows you to easily spin up new local mysql servers for testing purposes.}
12
+ gem.summary = %q{A small library that allows you to easily spin up new local mysql servers for testing purposes.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_isolated_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Osheroff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A small library that allows you to easily spin up new local mysql servers
15
+ for testing purposes.
16
+ email:
17
+ - ben@zendesk.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/mysql_isolated_server.rb
28
+ - lib/mysql_isolated_server/version.rb
29
+ - lib/tables/user.MYD
30
+ - lib/tables/user.MYI
31
+ - lib/tables/user.frm
32
+ - mysql_isolated_server.gemspec
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: A small library that allows you to easily spin up new local mysql servers
57
+ for testing purposes.
58
+ test_files: []