vagrant-cloner-wops 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +125 -0
- data/Rakefile +4 -0
- data/lib/vagrant-cloner/base_cloner.rb +76 -0
- data/lib/vagrant-cloner/cloner_container.rb +31 -0
- data/lib/vagrant-cloner/cloners/mysql.rb +165 -0
- data/lib/vagrant-cloner/cloners/mysql_cleaner.rb +40 -0
- data/lib/vagrant-cloner/cloners/testcloner.rb +34 -0
- data/lib/vagrant-cloner/config.rb +19 -0
- data/lib/vagrant-cloner/plugin.rb +22 -0
- data/lib/vagrant-cloner/provisioner.rb +13 -0
- data/lib/vagrant-cloner.rb +2 -0
- data/lib/vagrant_init.rb +1 -0
- data/spec/cloners/mysql_spec.rb +41 -0
- data/spec/spec_helper.rb +0 -0
- data/vagrant-cloner.gemspec +21 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 550a8b27fa95de06205f15dd4fae5a1b8c941976
|
4
|
+
data.tar.gz: f9047855b6174d0fda551dab5ec25998e088c6ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f196f77b0802ff520df792b582a229d2e02e2f7beb9e616df579e7012c647cfa4f2c71f7613276fbfc74ce0398ea4b5ce5e652486ca5bca13309a33bedce0a6d
|
7
|
+
data.tar.gz: b1750e0826beb19f4a38b9e103a0111bcd597dc586d60f3673f61bc386ea8344a20b464d20bca2caf0c5d4c48dbd7d691a7a2e772d0a919830d761945fce505f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Rob Yurkowski
|
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,125 @@
|
|
1
|
+
# vagrant-cloner
|
2
|
+
|
3
|
+
Copy down production resources to your new Vagrant VM.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Use vagrant's built-in plugin system:
|
8
|
+
|
9
|
+
vagrant plugin install vagrant-cloner
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
You will need to add a `config.vm.provision :cloner` section to your config in
|
14
|
+
order for Cloner to work. This should come after your other provisioners; if
|
15
|
+
Cloner runs before Chef or Puppet, for example, it's quite conceivable there
|
16
|
+
would be no database to restore to!
|
17
|
+
|
18
|
+
Each cloner has its own section inside the configuration, and this is the
|
19
|
+
recommended way to set them:
|
20
|
+
|
21
|
+
``` ruby
|
22
|
+
Vagrant::Config.run do |config|
|
23
|
+
|
24
|
+
config.vm.provision :chef_solo do |chef|
|
25
|
+
# ...
|
26
|
+
end
|
27
|
+
|
28
|
+
config.vm.provision :cloner do |cfg|
|
29
|
+
cfg.cloner.mysql.tap do |c|
|
30
|
+
# Set options here.
|
31
|
+
c.enabled = true
|
32
|
+
c.run_order = 10
|
33
|
+
# ...
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
The following keys are valid:
|
40
|
+
|
41
|
+
- **cloner**
|
42
|
+
- **(all cloners)**
|
43
|
+
- **enabled** - Required: Boolean whether to use this cloner or not. Defaults to false.
|
44
|
+
- **run_order** - Suggested: Integer value that dictates which order cloners run in. Lower orders run first. Defaults to 1000.
|
45
|
+
- **mysql**
|
46
|
+
- **use_ssh** - If false, will attempt to connect to the remote MySQL server at the `remote_host` using the `remote_user` and `remote_password` settings. Elsewise, will SSH into the remote host, export the database, download it locally, upload it to the VM, and install it that way.
|
47
|
+
- **remote_host** - String containing the remote server's FQDN.
|
48
|
+
- **remote_user** - Username to connect to remote server.
|
49
|
+
- **remote_password** - Optional: Password to connect to remote server. (Can be ignored if using publickey auth.)
|
50
|
+
- **remote_db_user** - Username to remote database server.
|
51
|
+
- **remote_db_password** - Password to remote database server.
|
52
|
+
- **vm_db_user** - Username to database server on VM.
|
53
|
+
- **vm_db_password** - Password to database server on VM.
|
54
|
+
- **databases_to_clone** - Optional: Array of databases to copy down. Defaults to 'all'.
|
55
|
+
- **remote_backup_path** - Optional: Where to dump databases to on remote server. Defaults to '/tmp'.
|
56
|
+
- **local_backup_path** - Optional: Where to store databases on host machine. Defaults to '/tmp'.
|
57
|
+
- **vm_backup_path** - Optional: Where to upload databases on VM. Defaults to '/tmp'.
|
58
|
+
- **backup_file** - Optional: Name for database dump. Defaults to mysql-dump-YYYY-MM-DD.sql.
|
59
|
+
- **disable_cleanup** - Optional: Don't remove database dumps after completion. Defaults to false.
|
60
|
+
- **testcloner**
|
61
|
+
- **foo** - String containing a message to print to console.
|
62
|
+
- **mysqlcleaner**
|
63
|
+
- **scripts** -- Array containing strings of URLs of SQL files to be run against the VM's database (curl is used to fetch these).
|
64
|
+
- **vm_db_user** - Username to database server on VM.
|
65
|
+
- **vm_db_password** - Password to database server on VM.
|
66
|
+
|
67
|
+
If you have some concern about storing passwords in this file (i.e. your Vagrantfile
|
68
|
+
is under version control), remember that the Vagrantfile is fully executed, so you can
|
69
|
+
simply require a file from elsewhere or read values in.
|
70
|
+
|
71
|
+
## Current List of Supplied Cloners
|
72
|
+
|
73
|
+
- `mysql` - Import a MySQL database(s)
|
74
|
+
- `testcloner` - A simple example of a cloner not meant for use.
|
75
|
+
- `mysqlcleaner` - Runs arbitrary SQL scripts against the MySQL server. Useful for sanitizing databases imported by the `mysql` cloner.
|
76
|
+
|
77
|
+
## Extra Cloners
|
78
|
+
|
79
|
+
You can write your own cloners to use with the tool. Unfortunately, because of how Vagrant loads its configuration settings, it's not possible to store these in a directory that is not in the gem itself.
|
80
|
+
|
81
|
+
Our suggestion is as follows:
|
82
|
+
|
83
|
+
1. Fork the gem and git-clone it;
|
84
|
+
2. Add your own cloner inside lib/vagrant-cloner/cloners/;
|
85
|
+
3. Add your configuration settings inside your Vagrantfile;
|
86
|
+
4. Run `rake build`;
|
87
|
+
5. Run `vagrant gem install vagrant-cloner --local ./pkg/`
|
88
|
+
6. Resume using vagrant as usual.
|
89
|
+
|
90
|
+
If you make an error in your script, you may have a hard time uninstalling it with `vagrant gem uninstall`. In a trice, you can remove directories in `~/.vagrant.d/gems/gems/` to manually remove troublesome gems. (Note that this was tested on a Linux distribution, so this may vary for Mac and Windows users.)
|
91
|
+
|
92
|
+
## How to Write a Cloner
|
93
|
+
|
94
|
+
To operate as a cloner, a class must inherit from `Vagrant::Cloners::Cloner`, and implement at a bare minimum these methods:
|
95
|
+
|
96
|
+
- `name` - Returns a string representation of the cloner's name; used for namespacing config.
|
97
|
+
- `validate(machine, errors)` - Can be used to call `errors.add` if there are validations that need to be performed on configuration values.
|
98
|
+
- `call` - Executes the cloner's routine.
|
99
|
+
|
100
|
+
A cloner must also be registered in the config to be run. This is best done after the class has been closed, at the bottom of the file:
|
101
|
+
|
102
|
+
`Vagrant::ClonerContainer.instance.send("#{<Class>.instance.name}=".to_sym, <Class>.instance)`
|
103
|
+
|
104
|
+
So for the MySQL cloner (which is `Vagrant::Cloners::MysqlCloner`), the line would read
|
105
|
+
|
106
|
+
`Vagrant::ClonerContainer.instance.send("#{Vagrant::Cloners::MysqlCloner.instance.name}=".to_sym, Vagrant::Cloners::MysqlCloner.instance)`
|
107
|
+
|
108
|
+
A very minimal example [can be found in the cloners directory](lib/vagrant-cloner/cloners/testcloner.rb). For more detailed examples, look at the other cloners there!
|
109
|
+
|
110
|
+
### How is this possibly useful for me?
|
111
|
+
|
112
|
+
`Cloner` exposes the `ssh`, `scp`, and `vm` methods to your class, so, in combination with `Kernel#system`, you can do pretty much anything on either host, VM, or remote server that you can do in (z|ba)sh.
|
113
|
+
|
114
|
+
The `vm` method, as an aside, is just a reference to the SSH communicator of Vagrant, so you can see what it provides [here](https://github.com/mitchellh/vagrant/blob/master/plugins/communicators/ssh/communicator.rb). If you need to actually access the environment, that is made available through the `env` method.
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
1. Fork it
|
119
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
120
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
121
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
122
|
+
5. Create new Pull Request
|
123
|
+
6. Explain why you think this belongs in the code here, and not inside your own gem that requires this one.
|
124
|
+
|
125
|
+
Pull requests most graciously accepted.
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'net/ssh'
|
2
|
+
require 'net/scp'
|
3
|
+
require 'date'
|
4
|
+
require 'singleton'
|
5
|
+
|
6
|
+
module VagrantCloner
|
7
|
+
# Cloner defines the base API that a cloner has to modify a VM or remote systems.
|
8
|
+
# TODO: Create a method that downloads to local and then uploads to VM?
|
9
|
+
|
10
|
+
class BaseCloner
|
11
|
+
include Singleton
|
12
|
+
|
13
|
+
attr_accessor :enabled, :machine, :options, :run_order
|
14
|
+
attr_reader :env
|
15
|
+
|
16
|
+
def name
|
17
|
+
raise "Cloner must define #name and return a string."
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate(machine, errors)
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
def enabled?
|
25
|
+
@enabled.nil? ? false : @enabled
|
26
|
+
end
|
27
|
+
|
28
|
+
def run_order
|
29
|
+
@run_order ||= 1000
|
30
|
+
end
|
31
|
+
|
32
|
+
# Shorthand for addressing the SSH communicator to the VM. View available methods
|
33
|
+
# here:
|
34
|
+
# https://github.com/mitchellh/vagrant/blob/master/plugins/communicators/ssh/communicator.rb
|
35
|
+
def vm
|
36
|
+
@machine.communicate
|
37
|
+
end
|
38
|
+
|
39
|
+
# Opens an SSH connection to an arbitrary server and passes it to a supplied block.
|
40
|
+
#
|
41
|
+
# See netssh documentation for further method options.
|
42
|
+
# http://net-ssh.github.com/net-ssh/
|
43
|
+
def ssh(*args, &block)
|
44
|
+
Net::SSH.start(*args, &block)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Opens an SCP connection to an arbitrary server, downloading or uploading to the
|
48
|
+
# machine currently in scope (whether the host machine or the VM).
|
49
|
+
# Recommended params:
|
50
|
+
# remote_host, remote_user, {:password => remote_password}
|
51
|
+
#
|
52
|
+
# See netscp documentation for further method options.
|
53
|
+
# https://github.com/net-ssh/net-scp
|
54
|
+
def scp(*args, &block)
|
55
|
+
Net::SCP.start(*args, &block)
|
56
|
+
end
|
57
|
+
|
58
|
+
def datestring
|
59
|
+
Date.today.to_s
|
60
|
+
end
|
61
|
+
protected :datestring
|
62
|
+
|
63
|
+
# Wrap debugging options.
|
64
|
+
%w(info warn error success).each do |meth|
|
65
|
+
define_method(meth) do |message|
|
66
|
+
machine.env.ui.send(meth.to_sym, message)
|
67
|
+
end
|
68
|
+
protected meth.to_sym
|
69
|
+
end
|
70
|
+
|
71
|
+
def debug(message)
|
72
|
+
machine.env.ui.info message
|
73
|
+
end
|
74
|
+
protected :debug
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
module VagrantCloner
|
5
|
+
class ClonerContainer < OpenStruct
|
6
|
+
include Singleton
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
def members
|
10
|
+
methods(false).grep(/=/).map {|m| m[0...-1] }
|
11
|
+
end
|
12
|
+
|
13
|
+
def each
|
14
|
+
members.each {|m| yield send(m) }
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def each_pair
|
19
|
+
members.each {|m| yield m, send(m)}
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def enabled_by_order
|
24
|
+
members.collect {|m| send(m)} # Get all plugin instances
|
25
|
+
.select {|m| m.enabled? } # Only enabled
|
26
|
+
.sort_by(&:run_order) # Sort by run order
|
27
|
+
.each {|m| yield m} # Yield up
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
module VagrantCloner
|
2
|
+
module Cloners
|
3
|
+
class MysqlCloner < ::VagrantCloner::BaseCloner
|
4
|
+
|
5
|
+
attr_accessor :remote_host, :remote_user, :remote_password,
|
6
|
+
:remote_db_user, :remote_db_password, :use_ssh,
|
7
|
+
:vm_db_user, :vm_db_password,
|
8
|
+
:databases_to_clone,
|
9
|
+
:remote_backup_path, :local_backup_path, :vm_backup_path, :backup_file,
|
10
|
+
:disable_cleanup, :warned_about_password,
|
11
|
+
:mysqldump_tags
|
12
|
+
|
13
|
+
def name
|
14
|
+
"mysql"
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate(machine, errors)
|
18
|
+
failures = []
|
19
|
+
failures.push "Must specify a remote user and host." unless remote_user && remote_host
|
20
|
+
failures.push "Must specify a remote database user and password." unless (remote_db_user && remote_db_password) || use_ssh
|
21
|
+
unless warned_about_password or remote_password
|
22
|
+
machine.env.ui.warn "You haven't specified a remote password. Pulling down MySQL databases may fail unless you have proper publickey authentication enabled."
|
23
|
+
@warned_about_password = true
|
24
|
+
end
|
25
|
+
errors.merge(name.to_sym => failures) if failures.any?
|
26
|
+
end
|
27
|
+
|
28
|
+
def remote_credentials
|
29
|
+
return @remote_credentials unless @remote_credentials.nil?
|
30
|
+
|
31
|
+
creds = [@remote_host, @remote_user]
|
32
|
+
creds.push :password => remote_password if remote_password
|
33
|
+
creds
|
34
|
+
end
|
35
|
+
|
36
|
+
def databases_to_clone
|
37
|
+
@databases_to_clone ||= []
|
38
|
+
end
|
39
|
+
|
40
|
+
def remote_backup_path
|
41
|
+
@remote_backup_path ||= "/tmp"
|
42
|
+
end
|
43
|
+
|
44
|
+
def local_backup_path
|
45
|
+
@local_backup_path ||= "/tmp"
|
46
|
+
end
|
47
|
+
|
48
|
+
def vm_backup_path
|
49
|
+
@vm_backup_path ||= "/tmp"
|
50
|
+
end
|
51
|
+
|
52
|
+
def disable_cleanup
|
53
|
+
@disable_cleanup.nil? ? false : @disable_cleanup
|
54
|
+
end
|
55
|
+
alias_method :disable_cleanup?, :disable_cleanup
|
56
|
+
|
57
|
+
def use_ssh
|
58
|
+
@use_ssh.nil? ? true : @use_ssh
|
59
|
+
end
|
60
|
+
alias_method :use_ssh?, :use_ssh
|
61
|
+
|
62
|
+
|
63
|
+
def extract_relevant_options
|
64
|
+
@enabled = options.enabled
|
65
|
+
@remote_host = options.remote_host
|
66
|
+
@remote_user = options.remote_user
|
67
|
+
@remote_password = options.remote_password
|
68
|
+
@remote_db_user = options.remote_db_user
|
69
|
+
@remote_db_password = options.remote_db_password
|
70
|
+
|
71
|
+
@use_ssh = options.use_ssh
|
72
|
+
|
73
|
+
@databases_to_clone = options.databases_to_clone
|
74
|
+
|
75
|
+
@mysqldump_tags = options.mysqldump_tags
|
76
|
+
|
77
|
+
@vm_db_user = options.vm_db_user
|
78
|
+
@vm_db_password = options.vm_db_password
|
79
|
+
|
80
|
+
@remote_credentials = options.remote_credentials
|
81
|
+
|
82
|
+
@remote_backup_path = options.remote_backup_path
|
83
|
+
@local_backup_path = options.local_backup_path
|
84
|
+
@vm_backup_path = options.vm_backup_path
|
85
|
+
@backup_file = options.backup_file || "mysql-backup-#{datestring}.sql"
|
86
|
+
|
87
|
+
@remote_backup_location = File.join(remote_backup_path, @backup_file)
|
88
|
+
@local_backup_location = File.join(local_backup_path, @backup_file)
|
89
|
+
@vm_backup_location = File.join(vm_backup_path, @backup_file)
|
90
|
+
end
|
91
|
+
|
92
|
+
def mysql_database_flag
|
93
|
+
if @databases_to_clone.include?("*") || @databases_to_clone.empty?
|
94
|
+
"--all-databases"
|
95
|
+
else
|
96
|
+
"--databases #{Array(@databases_to_clone).join(' ')}"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# We don't want to output -p if password is nil, because that will prompt
|
101
|
+
# for a password.
|
102
|
+
def mysql_password_flag(password)
|
103
|
+
if password.nil? || password.empty?
|
104
|
+
""
|
105
|
+
else
|
106
|
+
%Q{ -p"#{password}"}
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def call
|
111
|
+
extract_relevant_options
|
112
|
+
if use_ssh?
|
113
|
+
dump_remote_database
|
114
|
+
download_remote_database
|
115
|
+
import_database
|
116
|
+
clean_up
|
117
|
+
else
|
118
|
+
remote_dump_and_pipe
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def remote_dump_and_pipe
|
123
|
+
info "Doing remote mysqldump..."
|
124
|
+
command = %Q{mysqldump --verbose -h #{@remote_host} -u"#{@remote_user}"#{mysql_password_flag(@remote_password)} #{mysqldump_tags} #{mysql_database_flag} | mysql -u"#{@vm_db_user}"#{mysql_password_flag(@vm_db_password)}}
|
125
|
+
vm.tap do |host|
|
126
|
+
vm.execute command
|
127
|
+
end
|
128
|
+
info "All done!"
|
129
|
+
end
|
130
|
+
|
131
|
+
def dump_remote_database
|
132
|
+
command = %Q{mysqldump -u"#{@remote_db_user}"#{mysql_password_flag(@remote_db_password)} #{mysqldump_tags} #{mysql_database_flag} > #{@remote_backup_location}}
|
133
|
+
ssh(*remote_credentials) {|s| s.exec! command }
|
134
|
+
info "Finished dumping database!"
|
135
|
+
end
|
136
|
+
|
137
|
+
def download_remote_database
|
138
|
+
scp(*remote_credentials) {|s| s.download! @remote_backup_location, @local_backup_path }
|
139
|
+
info "Finished downloading file."
|
140
|
+
end
|
141
|
+
|
142
|
+
def import_database
|
143
|
+
vm.tap do |host|
|
144
|
+
host.upload @local_backup_location, @vm_backup_location
|
145
|
+
command = %Q{mysql -u"#{@vm_db_user}"#{mysql_password_flag(@vm_db_password)} < #{@vm_backup_location}}
|
146
|
+
host.execute command
|
147
|
+
end
|
148
|
+
info "Done loading database."
|
149
|
+
end
|
150
|
+
|
151
|
+
def clean_up
|
152
|
+
unless disable_cleanup?
|
153
|
+
ssh(*remote_credentials) {|s| s.exec! "rm #{@remote_backup_location}" } and info "Removed remote backup file."
|
154
|
+
system "rm #{@local_backup_location}" and info "Removed local backup file."
|
155
|
+
vm.execute "rm #{@vm_backup_location}" and info "Removed vm backup file."
|
156
|
+
success "Done!"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
if defined? VagrantCloner::ClonerContainer
|
164
|
+
VagrantCloner::ClonerContainer.instance.send("#{VagrantCloner::Cloners::MysqlCloner.instance.name}=".to_sym, VagrantCloner::Cloners::MysqlCloner.instance)
|
165
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Etailer
|
2
|
+
class MysqlCleanerCloner < ::VagrantCloner::BaseCloner
|
3
|
+
attr_accessor :vm_db_user, :vm_db_password
|
4
|
+
|
5
|
+
def name
|
6
|
+
"mysqlcleaner"
|
7
|
+
end
|
8
|
+
|
9
|
+
def scripts
|
10
|
+
@scripts ||= []
|
11
|
+
end
|
12
|
+
|
13
|
+
def scripts=(scripts)
|
14
|
+
@scripts = Array(scripts).flatten
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate(machine, errors)
|
18
|
+
failures = []
|
19
|
+
failures.push "You have to specify at least one script to run!" if scripts.nil? || scripts.empty?
|
20
|
+
failures.push "You must specify a VM database user and password!" unless vm_db_user && vm_db_password
|
21
|
+
|
22
|
+
errors.merge(name.to_sym => failures) if failures.any?
|
23
|
+
end
|
24
|
+
|
25
|
+
def mysql_connection_string
|
26
|
+
"-u#{vm_db_user} -p#{vm_db_password}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def call
|
30
|
+
@scripts.each do |script|
|
31
|
+
info "Fetching script #{script}..."
|
32
|
+
basename = script.split("/").last
|
33
|
+
vm.execute "curl #{script} -o /tmp/#{basename}"
|
34
|
+
vm.execute "mysql #{mysql_connection_string} < /tmp/#{basename}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
VagrantCloner::ClonerContainer.instance.send("#{Etailer::MysqlCleanerCloner.instance.name}=".to_sym, Etailer::MysqlCleanerCloner.instance)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module VagrantCloner
|
2
|
+
module Cloners
|
3
|
+
class TestCloner < ::VagrantCloner::BaseCloner
|
4
|
+
attr_accessor :foo
|
5
|
+
|
6
|
+
def name
|
7
|
+
"testcloner"
|
8
|
+
end
|
9
|
+
|
10
|
+
def validate(machine, errors)
|
11
|
+
failures = []
|
12
|
+
failures.push "Must specify a foo" unless foo
|
13
|
+
|
14
|
+
failures.merge(name.to_sym => failures) if failures.any?
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
info "Foo is #{foo}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
VagrantCloner::ClonerContainer.instance.send("#{VagrantCloner::Cloners::TestCloner.instance.name}=".to_sym, VagrantCloner::Cloners::TestCloner.instance)
|
25
|
+
|
26
|
+
# Inside your vagrant file, make sure you add the section for this cloner!
|
27
|
+
# # ...
|
28
|
+
#
|
29
|
+
# config.vm.provision :cloner do |cfg|
|
30
|
+
# cfg.cloners.testcloner.tap do |c|
|
31
|
+
# c.enabled = true
|
32
|
+
# c.foo = "Roses are red, violets are foo."
|
33
|
+
# end
|
34
|
+
# end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module VagrantCloner
|
2
|
+
class Config < Vagrant.plugin("2", :config)
|
3
|
+
class << self
|
4
|
+
attr_accessor :registered_cloners
|
5
|
+
end
|
6
|
+
|
7
|
+
def cloner
|
8
|
+
self.class.registered_cloners ||= []
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate(machine)
|
12
|
+
errors = {}
|
13
|
+
cloner.select {|c| c.enabled? }.each do |c|
|
14
|
+
c.validate(machine, errors)
|
15
|
+
end
|
16
|
+
errors
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'vagrant-cloner/cloner_container'
|
2
|
+
require 'vagrant-cloner/base_cloner'
|
3
|
+
Dir[File.join(File.dirname(__FILE__), 'cloners', '*.rb')].each {|f| require f }
|
4
|
+
|
5
|
+
module VagrantCloner
|
6
|
+
class Plugin < ::Vagrant.plugin("2")
|
7
|
+
name "Cloner"
|
8
|
+
|
9
|
+
config(:cloner, :provisioner) do
|
10
|
+
require 'vagrant-cloner/config'
|
11
|
+
::VagrantCloner::Config.tap do |c|
|
12
|
+
c.registered_cloners = ::VagrantCloner::ClonerContainer.instance
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
provisioner(:cloner) do
|
17
|
+
require 'vagrant-cloner/provisioner'
|
18
|
+
::VagrantCloner::Provisioner
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module VagrantCloner
|
2
|
+
class Provisioner < Vagrant.plugin("2", :provisioner)
|
3
|
+
def provision
|
4
|
+
@machine.env.ui.info "Vagrant-Cloner beginning back-up process."
|
5
|
+
config.cloner.enabled_by_order do |cloner|
|
6
|
+
cloner.tap {|c|
|
7
|
+
c.options = config.cloner.send(cloner.name)
|
8
|
+
c.machine = @machine
|
9
|
+
}.call
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/vagrant_init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'vagrant-cloner'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pry'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
# Mock this
|
6
|
+
module VagrantCloner
|
7
|
+
class BaseCloner
|
8
|
+
def datestring; "2013-01-01"; end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'vagrant-cloner/cloners/mysql'
|
13
|
+
|
14
|
+
module VagrantCloner
|
15
|
+
module Cloners
|
16
|
+
describe MysqlCloner do
|
17
|
+
describe "#extract_relevant_options" do
|
18
|
+
describe "when missing keys" do
|
19
|
+
before do
|
20
|
+
subject.stub(:options).and_return(OpenStruct.new({:remote_db_password => ''}))
|
21
|
+
subject.extract_relevant_options
|
22
|
+
end
|
23
|
+
|
24
|
+
its(:remote_db_password) { should be_empty }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#mysql_password_flag" do
|
29
|
+
it "produces nothing if password is nil or empty" do
|
30
|
+
result = subject.mysql_password_flag("")
|
31
|
+
result.should be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
it "outputs the flag if password is present" do
|
35
|
+
result = subject.mysql_password_flag("test")
|
36
|
+
result.should == %Q{ -p"test"}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
version = '2.1.2'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = "vagrant-cloner-wops"
|
9
|
+
gem.version = version
|
10
|
+
gem.authors = ["Robert Coleman", "Rob Yurkowski"]
|
11
|
+
gem.email = ["github@robert.net.nz", "rob@yurkowski.net"]
|
12
|
+
gem.description = %q{Copy production resources down to your new VM.}
|
13
|
+
gem.summary = %q{Copy production resources down to your new VM.}
|
14
|
+
gem.homepage = "https://github.com/ScottCrass/vagrant-cloner/tree/with-mysqldump-opts/"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.test_files = gem.files.grep(%r{^(spec)/})
|
18
|
+
# gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vagrant-cloner-wops
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Coleman
|
8
|
+
- Rob Yurkowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-11-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Copy production resources down to your new VM.
|
15
|
+
email:
|
16
|
+
- github@robert.net.nz
|
17
|
+
- rob@yurkowski.net
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/vagrant-cloner.rb
|
28
|
+
- lib/vagrant-cloner/base_cloner.rb
|
29
|
+
- lib/vagrant-cloner/cloner_container.rb
|
30
|
+
- lib/vagrant-cloner/cloners/mysql.rb
|
31
|
+
- lib/vagrant-cloner/cloners/mysql_cleaner.rb
|
32
|
+
- lib/vagrant-cloner/cloners/testcloner.rb
|
33
|
+
- lib/vagrant-cloner/config.rb
|
34
|
+
- lib/vagrant-cloner/plugin.rb
|
35
|
+
- lib/vagrant-cloner/provisioner.rb
|
36
|
+
- lib/vagrant_init.rb
|
37
|
+
- spec/cloners/mysql_spec.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- vagrant-cloner.gemspec
|
40
|
+
homepage: https://github.com/ScottCrass/vagrant-cloner/tree/with-mysqldump-opts/
|
41
|
+
licenses: []
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.0.14
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Copy production resources down to your new VM.
|
63
|
+
test_files:
|
64
|
+
- spec/cloners/mysql_spec.rb
|
65
|
+
- spec/spec_helper.rb
|