wpcap 0.1.0 → 0.2.0

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.
@@ -0,0 +1,46 @@
1
+ class Wpcap::Backup
2
+
3
+ def initialize(ls)
4
+ @data ||= ls.split(" ")
5
+ end
6
+
7
+ def name
8
+ @name ||= @data[8]
9
+ end
10
+
11
+ def type
12
+ type = name.split(".")[0]
13
+ @type ||= "#{type} "
14
+ end
15
+
16
+ def size
17
+ as_size( @data[4] )
18
+ end
19
+
20
+ def at
21
+ Time.at("#{name.chomp(".sql.bz2").split(".")[1]}.#{name.chomp(".sql.bz2").split(".")[2]}".to_f).strftime("%c")
22
+ end
23
+
24
+ def self.parse(ls_output)
25
+ backups = []
26
+ ls_output.each_line do |line|
27
+
28
+ backups << Wpcap::Backup.new( line ) unless line.include?("total")
29
+ end
30
+ return backups
31
+ end
32
+
33
+
34
+ PREFIX = %W(TiB GiB MiB KiB B).freeze
35
+
36
+ def as_size( s )
37
+ s = s.to_f
38
+ i = PREFIX.length - 1
39
+ while s > 512 && i > 0
40
+ i -= 1
41
+ s /= 1024
42
+ end
43
+ ((s > 9 || s.modulo(1) < 0.1 ? '%d' : '%.1f') % s) + ' ' + PREFIX[i]
44
+ end
45
+
46
+ end
@@ -7,7 +7,7 @@ Capistrano::Configuration.instance(:must_exist) :
7
7
  Capistrano.configuration(:must_exist)
8
8
 
9
9
  configuration.load do
10
-
10
+ default_run_options[:pty] = true
11
11
  def template(from, to)
12
12
  erb = File.read(File.expand_path("../templates/#{from}", __FILE__))
13
13
  put ERB.new(erb).result(binding), to
@@ -21,7 +21,11 @@ configuration.load do
21
21
  chars = (('a'..'z').to_a + ('0'..'9').to_a) - %w(i o 0 1 l 0)
22
22
  (1..size).collect{|a| chars[rand(chars.size)] }.join
23
23
  end
24
-
24
+
25
+ def remote_file_exists?(full_path)
26
+ 'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
27
+ end
28
+
25
29
  def run_with_tty(server, cmd)
26
30
  # looks like total pizdets
27
31
  command = []
@@ -47,4 +51,13 @@ configuration.load do
47
51
  run "#{sudo} apt-get -y install git debconf-utils python-software-properties"
48
52
  end
49
53
  end
54
+
55
+ desc "Tail all or a single remote file"
56
+ task :tail do
57
+ ENV["LOGFILE"] ||= "*.log"
58
+ run "tail -f #{shared_path}/logs/#{ENV["LOGFILE"]}" do |channel, stream, data|
59
+ puts "#{data}"
60
+ break if stream == :err
61
+ end
62
+ end
50
63
  end
@@ -2,6 +2,7 @@ require 'erb'
2
2
  require 'yaml'
3
3
  require 'wpcap'
4
4
  require 'wpcap/utility'
5
+ require 'wpcap/backup'
5
6
 
6
7
  configuration = Capistrano::Configuration.respond_to?(:instance) ?
7
8
  Capistrano::Configuration.instance(:must_exist) :
@@ -12,15 +13,30 @@ configuration.load do
12
13
  namespace :mysql do
13
14
  desc "Install Mysql Database Server"
14
15
  task :install_server, roles: :db do
15
- create_yaml
16
16
  prepare_env
17
- run "#{sudo} apt-get -y update"
17
+ run "#{sudo} apt-get -y update"
18
18
  run "#{sudo} echo 'mysql-server-5.5 mysql-server/root_password password #{db_priv_pass}' | #{sudo} debconf-set-selections"
19
19
  run "#{sudo} echo 'mysql-server-5.5 mysql-server/root_password_again password #{db_priv_pass}' | #{sudo} debconf-set-selections"
20
20
  run "#{sudo} apt-get -y install mysql-server"
21
21
  end
22
22
  after "deploy:install", "db:mysql:install_server"
23
23
 
24
+ desc "Save db passwords on server in enviroment so new wpcap installs may provision local databases. "
25
+ task :set_priv_environment, roles: :db do
26
+ #Generate a password for the mysql install (this will be saved in the enviroment vars of the server)
27
+ unless remote_file_exists?("/etc/wpcap/database.yml")
28
+
29
+ set :db_priv_pass, random_password(16)
30
+ run "#{sudo} mkdir -p /etc/wpcap"
31
+ save_yaml({"db_priv_pass" => db_priv_pass, "db_priv_user" => "root"}, "/tmp/privdb.yml")
32
+ upload "/tmp/privdb.yml", "/etc/wpcap/database.yml"
33
+
34
+ else
35
+ Wpcap::Utility.error("MYSQL Server Already Configured")
36
+ end
37
+ end
38
+ before "db:mysql:install_server", "db:mysql:set_priv_environment"
39
+
24
40
  desc "Install Mysql Database Client Bindings"
25
41
  task :install_client, roles: :web do
26
42
  run "#{sudo} apt-get -y update"
@@ -37,7 +53,7 @@ configuration.load do
37
53
 
38
54
  prepare_env
39
55
  run "mkdir -p #{backups_path}"
40
- filename = "db_backup.#{Time.now.utc.to_i}.sql.bz2"
56
+ filename = "db_backup_#{Time.now.to_f}.sql.bz2"
41
57
  filepath = "#{backups_path}/#{filename}"
42
58
  on_rollback { run "rm #{filepath}" }
43
59
  run "mysqldump --user=#{db_username} -p --host=#{db_host} #{db_database} | bzip2 -z9 > #{filepath}" do |ch, stream, out|
@@ -54,7 +70,24 @@ configuration.load do
54
70
  puts out
55
71
  end
56
72
  end
57
-
73
+
74
+ desc "Restores the database from the latest compressed dump"
75
+ task :restore, :roles => :db, :only => { :primary => true } do
76
+ prepare_env
77
+ backup_list = capture "cd #{backups_path}; ls -lrt"
78
+ backups = Wpcap::Backup.parse( backup_list )
79
+
80
+ backups.each_with_index do |backup, count|
81
+ printf "%-5s %-20s %-10s %s\n", count + 1 , backup.type , backup.size , backup.at
82
+ end
83
+ puts "Select a Backup you wish to restore (1-#{backups.size})"
84
+ restore_index = $stdin.gets.chomp
85
+ if restore_index
86
+ backup_to_restore = backups[restore_index.to_i - 1 ]
87
+ puts "#{backups_path}/#{backup_to_restore.name}"
88
+ end
89
+ end
90
+
58
91
  desc "Restores the database from the latest downloaded compressed dump"
59
92
  task :local_restore do
60
93
  prepare_env(:development)
@@ -74,7 +107,7 @@ configuration.load do
74
107
  prepare_env(:development)
75
108
  run_locally "#{local_mysql_path}mysqldump --user #{db_username} --password=#{db_password} #{db_database} | bzip2 -z9 > #{local_dump}"
76
109
  run "mkdir -p #{backups_path}"
77
- filename = "local_upload.#{Time.now.to_f}.sql.bz2"
110
+ filename = "local_upload_#{Time.now.to_f}.sql.bz2"
78
111
  filepath = "#{backups_path}/#{filename}"
79
112
  upload "#{local_dump}" , "#{filepath}"
80
113
  end
@@ -97,6 +130,7 @@ configuration.load do
97
130
 
98
131
  desc "Create MySQL database and user for this stage using database.yml values"
99
132
  task :create, :roles => :db, :only => { :primary => true } do
133
+ create_yaml
100
134
  prepare_env
101
135
  create_db_if_missing
102
136
  end
@@ -121,14 +155,13 @@ configuration.load do
121
155
 
122
156
  desc "Create database.yml in shared path with settings for current stage and test env"
123
157
  task :create_yaml do
124
- prepare_env
125
- template_path = "#{shared_path}/config/database.yml"
126
-
127
- unless db_config[rails_env]
128
- set :db_priv_pass, random_password(16)
158
+ #Create a new database enviroment unless it already exists in config.
159
+ unless db_config[stage]
160
+ template_path = "#{shared_path}/config/database.yml"
129
161
  set :db_username, "#{application.split(".").first}_#{stage}"
130
162
  set :db_database, "#{application.split(".").first}_#{stage}"
131
163
  set :db_password, random_password(16)
164
+ set :db_prefix, db_config["development"]["prefix"]
132
165
  run "mkdir -p #{shared_path}/config"
133
166
  template "mysql.yml.erb", template_path
134
167
  server_yaml = capture "cat #{template_path}"
@@ -141,46 +174,76 @@ configuration.load do
141
174
  @db_config ||= fetch_db_config
142
175
  end
143
176
 
144
- def fetch_db_config
145
- YAML.load(File.open("config/database.yml"))
177
+ def remote_config(key)
178
+ @remote_config ||= fetch_db_config(true)
179
+
180
+ return @remote_config[key.to_s]
181
+ end
182
+
183
+ def fetch_db_config(remote = false)
184
+ if remote
185
+ YAML.load( capture("cat /etc/wpcap/database.yml"))
186
+ else
187
+ YAML.load(File.open("config/database.yml"))
188
+ end
146
189
  end
147
190
 
148
191
  # Sets database variables from remote database.yaml
149
- def prepare_env(rails_env = stage)
150
- abort "No Database Configuratons Found" if !db_config
192
+ def prepare_env(load_stage = stage)
151
193
 
152
- rails_env = rails_env.to_s
153
- set(:local_dump) { "/tmp/#{application}.sql.bz2" }
154
-
155
- if db_config[rails_env]
156
- set(:db_priv_user) { db_config[rails_env]["priv_username"].nil? ? db_config[rails_env]["username"] : db_config[rails_env]["priv_username"] }
157
- set(:db_priv_pass) { db_config[rails_env]["priv_password"].nil? ? db_config[rails_env]["password"] : db_config[rails_env]["priv_password"] }
158
- set(:db_host) { db_config[rails_env]["host"] }
159
- set(:db_database) { db_config[rails_env]["database"] }
160
- set(:db_username) { db_config[rails_env]["username"] }
161
- set(:db_password) { db_config[rails_env]["password"] }
162
- set(:db_encoding) { db_config[rails_env]["encoding"] }
163
- set(:db_prefix) { db_config[rails_env]["prefix"].nil? ? db_config["development"]["prefix"] : db_config[rails_env]["prefix"] }
164
- else
165
- Wpcap::Utility.error "No Database Configuration for #{rails_env} Found"
194
+ load_stage = load_stage.to_s
195
+
196
+ if !db_config
197
+ Wpcap::Utility.error("No Database Configuratons Found")
198
+ abort
199
+ end
200
+
201
+ if remote_config(:db_priv_pass).nil?
202
+ Wpcap::Utility.error "This no privleged user for this server found in servers ssh enviroment profile (did you set it up with wpcap?)"
166
203
  abort
167
204
  end
205
+
206
+ set(:local_dump) { "/tmp/#{application}.sql.bz2" }
207
+
208
+ if db_config[load_stage]
209
+
210
+ set(:db_priv_user) { remote_config(:db_priv_user).nil? ? db_config[load_stage]["username"] : remote_config(:db_priv_user) }
211
+ set(:db_priv_pass) { remote_config(:db_priv_pass).nil? ? db_config[load_stage]["password"] : remote_config(:db_priv_pass) }
212
+ set(:db_host) { db_config[load_stage]["host"] }
213
+ set(:db_database) { db_config[load_stage]["database"] }
214
+ set(:db_username) { db_config[load_stage]["username"] }
215
+ set(:db_password) { db_config[load_stage]["password"] }
216
+ set(:db_encoding) { db_config[load_stage]["encoding"] }
217
+ set(:db_prefix) { db_config[load_stage]["prefix"] }
218
+
219
+ end
168
220
 
169
221
  end
170
-
171
- def update_db_config(hash_to_save)
172
- database_yaml = db_config.merge(hash_to_save)
173
- print "Saving Database Config file to local Repo"
174
- File.open('config/database.yml', 'w') do |f|
175
- f.puts YAML::dump(database_yaml).sub("---","").split("\n").map(&:rstrip).join("\n").strip
222
+
223
+ def save_yaml(hash, path)
224
+ File.open(path, 'w') do |f|
225
+ f.puts YAML::dump(hash).sub("---","").split("\n").map(&:rstrip).join("\n").strip
176
226
  end
177
227
  end
178
-
228
+
229
+ def update_db_config(stage_config)
230
+ print "Saving Database Config file to local Repo"
231
+ database_yaml = db_config.merge(stage_config)
232
+ save_yaml(database_yaml, 'config/database.yml')
233
+ end
234
+
179
235
  def most_recent_backup
180
236
  most_recent_sql = capture "cd #{backups_path}; ls -lrt | awk '{ f=$NF }; END{ print f }'"
181
237
  return "#{backups_path}/#{most_recent_sql}".strip
182
238
  end
183
-
239
+
240
+ def restore_dump(dump_path)
241
+ run "bzcat #{dump_path} | mysql --user=#{db_username} -p --host=#{db_host} #{db_database}" do |ch, stream, out|
242
+ ch.send_data "#{db_password}\n" if out =~ /^Enter password:/
243
+ puts out
244
+ end
245
+ end
246
+
184
247
  def database_exits?(environment = stage)
185
248
  exists = false
186
249
  databases = run_mysql_command("show databases;", environment)
@@ -200,7 +263,7 @@ configuration.load do
200
263
  print "databases exists -- skipping"
201
264
  end
202
265
  end
203
-
266
+
204
267
  def run_mysql_command(sql, environment = stage)
205
268
  environment = environment.to_s
206
269
  prepare_env(environment)
@@ -213,13 +276,14 @@ configuration.load do
213
276
  else
214
277
  run "mysql --user=#{db_username} --password --execute=\"#{sql}\"" do |channel, stream, data|
215
278
  if data =~ /^Enter password:/
216
- channel.send_data "#{db_password}\n"
279
+ channel.send_data "#{db_password}\n"
217
280
  end
218
281
  output += data
219
282
  end
220
283
  end
221
284
  return output
222
285
  end
286
+
223
287
  before "db:mysql:push", "db:mysql:create"
224
288
  before "db:mysql:pull", "db:mysql:create_local_db"
225
289
  after "deploy:update_code" , "db:mysql:prepare_enviroment"
@@ -27,5 +27,10 @@ configuration.load do
27
27
  end
28
28
  end
29
29
 
30
+ desc "Server Enviroment"
31
+ task :get_server_enviroment do
32
+ run "printenv"
33
+ end
34
+
30
35
  end
31
36
  end
@@ -50,6 +50,7 @@ configuration.load do
50
50
  run "touch #{shared_path}/config/nginx.conf"
51
51
 
52
52
  # set correct permissions
53
+ run "chmod -R 755 #{latest_release}/app/wp-content/plugins"
53
54
  run "chmod -R 755 #{shared_path}/uploads"
54
55
  run "chmod -R 755 #{shared_path}/cache"
55
56
  end
@@ -1,3 +1,3 @@
1
1
  module Wpcap
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wpcap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-29 00:00:00.000000000 Z
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capistrano
@@ -58,6 +58,7 @@ files:
58
58
  - Rakefile
59
59
  - bin/wpcap
60
60
  - lib/wpcap.rb
61
+ - lib/wpcap/backup.rb
61
62
  - lib/wpcap/command.rb
62
63
  - lib/wpcap/recipes/base.rb
63
64
  - lib/wpcap/recipes/check.rb