akitaonrails-locarails 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/locarails CHANGED
@@ -13,6 +13,7 @@ end
13
13
  require 'ostruct'
14
14
  require 'optparse'
15
15
  require 'erb'
16
+ require 'highline/import'
16
17
 
17
18
  # configuracoes da sua hospedagem
18
19
  config = OpenStruct.new
@@ -74,9 +75,14 @@ elsif ARGV.length > 1
74
75
  abort "Argumentos demais; por favor especifique somente o diretorio da aplicacao Rails.."
75
76
  end
76
77
 
77
- def get_input(message)
78
- print "* #{message} > "
79
- gets.strip
78
+ def get_input(message, default_value = nil)
79
+ result = if message =~ /senha/
80
+ ask("* #{message} > ") { |q| q.echo = "*" }
81
+ else
82
+ ask("* #{message} > ")
83
+ end
84
+ result = default_value if default_value && result.empty?
85
+ result
80
86
  end
81
87
 
82
88
  # configuracoes locais da sua maquina
@@ -112,10 +118,16 @@ STR
112
118
  config.app_name = get_input( "Nome da sua aplicacao" ) if config.app_name.nil? || config.app_name.empty?
113
119
  config.dominio = get_input "Dominio do seu site (ex. teste.tempsite.ws)"
114
120
  config.usuario = get_input "Seu usuario de hospedagem"
115
- mysqlcfg.db = get_input "Nome do seu banco mysql"
116
- mysqlcfg.user = get_input "Seu usuario de mysql"
121
+ mysqlcfg.db = get_input "Nome do seu banco mysql (enter para '#{config.usuario}')", config.usuario
122
+ mysqlcfg.user = get_input "Seu usuario de mysql (enter para #{mysqlcfg.db})", mysqlcfg.db
117
123
  mysqlcfg.pass = get_input "Sua senha de mysql"
118
- mysqlcfg.host = get_input "Seu servidor mysql (ex. mysqlxxx.locaweb.com.br)"
124
+ mysqlcfg.host = get_input "Seu servidor mysql (ex. mysqlxxxx.locaweb.com.br)"
125
+ mysqlcfg.host = "#{mysqlcfg.host}.locaweb.com.br" unless mysqlcfg.host =~ /locaweb\.com\.br/
126
+ if config.mode == 'git'
127
+ config.branch = get_input "Git branch (enter para 'master')", 'master'
128
+ config.remote_repo = get_input "Nome do repositorio remoto (enter para 'locaweb')", 'locaweb'
129
+ config.remote_git = "#{config.usuario}@#{config.dominio}:~/repo/#{config.app_name}.git"
130
+ end
119
131
 
120
132
  # forca rodar capistrano
121
133
  unless File.exists?("#{config.local_path}/Capfile")
@@ -131,8 +143,10 @@ STR
131
143
  end
132
144
  end
133
145
 
134
- FileUtils.copy_file("#{config.bin_path}/../templates/locaweb_backup.rb",
135
- "#{config.local_path}/config/locaweb_backup.rb")
146
+ if config.mode == 'git'
147
+ FileUtils.copy_file("#{config.bin_path}/../templates/.gitignore",
148
+ "#{config.local_path}/.gitignore") unless File.exists?("#{config.local_path}/.gitignore")
149
+ end
136
150
 
137
151
  File.open("#{config.local_path}/config/deploy.rb", 'w+') do |out|
138
152
  erb = ERB.new(File.read("#{config.bin_path}/../templates/deploy.rb.erb"))
@@ -2,7 +2,7 @@ module Locarails
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
4
  MINOR = 1
5
- TINY = 1
5
+ TINY = 2
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/locarails.gemspec CHANGED
@@ -1,8 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{locarails}
3
- s.version = "1.1.1"
4
-
5
- s.specification_version = 2 if s.respond_to? :specification_version=
3
+ s.version = "1.1.2"
6
4
 
7
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
6
  s.authors = ["Fabio Akita"]
@@ -16,6 +14,23 @@ Gem::Specification.new do |s|
16
14
  s.homepage = %q{http://www.locaweb.com.br/rails}
17
15
  s.require_paths = ["lib"]
18
16
  s.rubyforge_project = %q{locarails}
19
- s.rubygems_version = %q{1.1.1}
17
+ s.rubygems_version = [s.version]
20
18
  s.summary = %q{Configuracao de Capistrano automatica para hospedagens Linux Locaweb.}
19
+
20
+ if s.respond_to? :specification_version then
21
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
+ s.specification_version = 2
23
+
24
+ if current_version >= 3 then
25
+ s.add_runtime_dependency(%q<capistrano>, [">= 2.0.0"])
26
+ s.add_runtime_dependency(%q<highline>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<capistrano>, [">= 2.0.0"])
29
+ s.add_dependency(%q<highline>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<capistrano>, [">= 2.0.0"])
33
+ s.add_dependency(%q<highline>, [">= 0"])
34
+ end
35
+
21
36
  end
@@ -15,13 +15,18 @@ task :before_setup do
15
15
  ts = Time.now.strftime("%y%m%d%H%M%S")
16
16
  <% if config.mode == 'git' %>
17
17
  # git folder
18
- has_git = false
19
18
  run "test -d #{git_repo} || mkdir -p -m 755 #{git_repo}"
20
19
  run "test -d #{git_repo}/.git || cd #{git_repo} && git init"
21
20
  git_config = File.join(File.dirname(__FILE__), "../.git/config")
22
- if File.exists?(git_config) && File.read(git_config) !~ /locaweb/
23
- `git remote add locaweb #{repository}`
24
- `git push locaweb #{branch}`
21
+
22
+ has_git = false
23
+ if File.exists?(git_config)
24
+ `git remote rm #{remote_repo}` if File.read(git_config) =~ /\[remote\s+\"#{remote_repo}\"\]/
25
+ `git remote add #{remote_repo} #{repository}`
26
+ output = `git branch`.strip
27
+ `git checkout master && git checkout -b #{branch}` if output !~ /#{branch}/
28
+ `git checkout #{branch}` if output !~ /^\*\s+#{branch}/
29
+ `git push #{remote_repo} #{branch}`
25
30
  has_git = true
26
31
  end
27
32
  <% end %>
@@ -32,22 +37,23 @@ task :before_setup do
32
37
  run "if [ -d #{site_path} ]; then mv #{site_path} #{site_path}-#{ts}.old ; fi"
33
38
  run "if [ -h #{site_path} ]; then mv #{site_path} #{site_path}-#{ts}.old ; fi"
34
39
  run "ln -s #{deploy_to}/current/public #{public_html}/#{application}"
35
- put File.read(File.dirname(__FILE__) + "/database.locaweb.yml"), "#{deploy_to}/etc/database.yml"
40
+ upload File.join(File.dirname(__FILE__), "database.locaweb.yml"), "#{deploy_to}/etc/database.yml"
41
+ upload File.join(File.dirname(__FILE__), "locaweb_backup.rb"), "#{deploy_to}/etc/locaweb_backup.rb"
42
+ upload File.join(File.dirname(__FILE__), "ssh_helper.rb"), "#{deploy_to}/etc/ssh_helper.rb"
36
43
 
37
44
  <% if config.mode == 'git' %>
38
- if has_git
39
- # ssh keygen
40
- put File.read(File.dirname(__FILE__) + "/ssh_helper.rb"), "#{deploy_to}/etc/ssh_helper.rb"
41
- run "test -f .ssh/id_rsa || ruby #{deploy_to}/etc/ssh_helper.rb /home/#{user}/.ssh/id_rsa #{domain} #{user}"
42
- else
43
- 3.times { puts "" }
45
+ # ssh keygen
46
+ run "test -f ~/.ssh/id_rsa || ruby #{deploy_to}/etc/ssh_helper.rb /home/#{user}/.ssh/id_rsa #{domain} #{user}"
47
+
48
+ unless has_git
49
+ 2.times { puts "" }
44
50
  puts "==============================================================="
45
51
  puts "Rode os seguintes comandos depois de criar seu repositorio Git:"
46
52
  puts ""
47
- puts " git remote add locaweb #{repository}"
48
- puts " git push locaweb #{branch}"
53
+ puts " git remote add #{remote_repo} #{repository}"
54
+ puts " git push #{remote_repo} #{branch}"
49
55
  puts "==============================================================="
50
- 3.times { puts "" }
56
+ 2.times { puts "" }
51
57
  end
52
58
  <% end %>
53
59
  end
@@ -80,11 +86,48 @@ namespace :log do
80
86
  end
81
87
 
82
88
  namespace :db do
83
- desc "Faz o backup remoto do banco de dados MySQL e ja faz o download"
89
+ desc "Faz o backup remoto e download do banco de dados MySQL"
84
90
  task :backup, :roles => :db do
85
- backup_rb ||= "#{deploy_to}/current/config/locaweb_backup.rb"
86
- run "if [ -f #{backup_rb} ]; then ruby #{backup_rb} #{deploy_to} ; fi"
91
+ backup_rb ||= "#{deploy_to}/etc/locaweb_backup.rb"
92
+ run "if [ -f #{backup_rb} ]; then ruby #{backup_rb} backup #{deploy_to} ; fi"
87
93
  get "#{deploy_to}/etc/dump.tar.gz", "dump.tar.gz"
88
94
  run "rm #{deploy_to}/etc/dump.tar.gz"
89
95
  end
96
+
97
+ desc "Faz o upload e o restore remoto do banco de dados MySQL"
98
+ task :restore, :roles => :db do
99
+ unless File.exists?("dump.tar.gz")
100
+ puts "Backup dump.tar.gz nao encontrado"
101
+ exit 0
102
+ end
103
+ backup_rb ||= "#{deploy_to}/etc/locaweb_backup.rb"
104
+ upload "dump.tar.gz", "#{deploy_to}/etc/dump.tar.gz"
105
+ run "if [ -f #{backup_rb} ]; then ruby #{backup_rb} restore #{deploy_to} ; fi"
106
+ end
107
+
108
+ desc "Apaga todas as tabelas do banco de dados [CUIDADO! OPERACAO SEM VOLTA!]"
109
+ task :drop_all, :roles => :db do
110
+ backup_rb ||= "#{deploy_to}/etc/locaweb_backup.rb"
111
+ run "if [ -f #{backup_rb} ]; then ruby #{backup_rb} drop_all #{deploy_to} ; fi"
112
+ end
90
113
  end
114
+
115
+ namespace :ssh do
116
+ desc "Faz upload da sua chave publica"
117
+ task :upload_key, :roles => :app do
118
+ public_key_path = File.expand_path("~/.ssh/id_rsa.pub")
119
+ unless File.exists?(public_key_path)
120
+ puts %{
121
+ Chave publica nao encontrada em #{public_key_path}
122
+ Crie sua chave - sem passphrase - com o comando:
123
+ ssh_keygen -t rsa
124
+ }
125
+ exit 0
126
+ end
127
+ run "test -d ~/.ssh || mkdir -m 755 ~/.ssh"
128
+ upload public_key_path, "~/.ssh"
129
+ run "test -f ~/.ssh/authorized_keys || touch ~/.ssh/authorized_keys"
130
+ run "cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys"
131
+ run "chmod 755 ~/.ssh/authorized_keys"
132
+ end
133
+ end
@@ -60,7 +60,14 @@
60
60
  set :user, "<%= config.usuario %>"
61
61
  set :domain, "<%= config.dominio %>"
62
62
  set :application, "<%= config.app_name %>"
63
+ <% if config.mode == 'git' %>
64
+ set :repository, "<%= config.remote_git %>"
65
+ set :remote_repo, '<%= config.remote_repo %>'
66
+ set :branch, '<%= config.branch %>'
67
+ set :git_repo, "/home/#{user}/repo/#{application}.git"
68
+ <% else %>
63
69
  set :repository, "<%= config.local_path %>"
70
+ <% end %>
64
71
  <% if config.ssh_key_path %>
65
72
  ssh_options[:keys] = File.expand_path("<%= config.ssh_key_path %>") # apenas descomente se tiver chave
66
73
  <% end %>
@@ -79,23 +86,18 @@ set :runner, nil
79
86
  set :use_sudo, false
80
87
  set :no_release, true
81
88
 
82
- <% if config.mode == 'git' %>
83
- set :git_repo, "/home/#{user}/repo/#{application}.git"
84
-
89
+ <% if config.mode == 'git' %>
85
90
  set :scm, :git
86
- set :branch, 'local'
87
91
  set :deploy_via, :remote_cache
88
92
  set :git_shallow_clone, 1
89
- set :copy_exclude, %w(.git/* .svn/* log/* tmp/* .gitignore)
90
- set :remote, "akitaonrails1"
93
+ set :remote, user
91
94
  set :scm_verbose, true
92
- set :keep_releases, 5
93
95
  <% else %>
94
- set :scm, :none # nenhum repositorio
96
+ set :scm, :none
95
97
  set :deploy_via, :copy
96
- set :copy_exclude, %w(.git/* .svn/* log/* tmp/*)
97
- set :keep_releases, 5
98
98
  <% end %>
99
+ set :copy_exclude, %w(.git/* .svn/* log/* tmp/* .gitignore)
100
+ set :keep_releases, 5
99
101
 
100
102
  ssh_options[:username] = user
101
103
  ssh_options[:paranoid] = false
@@ -1,19 +1,30 @@
1
1
  #!/usr/bin/env ruby -wKU
2
2
  require 'yaml'
3
+ require 'erb'
3
4
 
4
- deploy_to = ARGV[0]
5
- cfg = YAML::load(File.open("#{deploy_to}/etc/database.yml"))
6
- exit! unless cfg['production'] # sai se nao encontrar o arquivo de banco
5
+ operation, deploy_to = ARGV
6
+ cfg = YAML::load(ERB.new(IO.read("#{deploy_to}/etc/database.yml")).result)
7
+ exit 0 unless cfg['production'] # sai se nao encontrar o arquivo de banco
8
+ prd = cfg['production']
9
+ mysql_opts = "-u #{prd['username']} -p#{prd['password']} -h #{prd['host']} #{prd['database']}"
10
+ mysql_opts_no_data = "-u #{prd['username']} -p#{prd['password']} -h #{prd['host']} --add-drop-table --no-data #{prd['database']}"
7
11
 
8
- cfg = cfg['production']
9
- cmd_mysql = "mysqldump -u #{cfg['username']} -p#{cfg['password']} -h #{cfg['host']} #{cfg['database']} > #{deploy_to}/etc/dump.sql"
10
- cmd_compress = "cd #{deploy_to}/etc && tar cvfz dump.tar.gz dump.sql"
11
- cmd_rm = "rm #{deploy_to}/etc/dump.sql"
12
+ commands = []
12
13
 
13
- puts "executing: #{cmd_mysql.gsub(cfg['password'], 'xxxxxxxx')}"
14
- `#{cmd_mysql}`
15
- puts "executing: #{cmd_compress}"
16
- `#{cmd_compress}`
17
- puts "executing: #{cmd_rm}"
18
- `#{cmd_rm}`
19
- puts "db backup finished."
14
+ case operation
15
+ when 'backup'
16
+ commands << "mysqldump #{mysql_opts} > #{deploy_to}/etc/dump.sql"
17
+ commands << "cd #{deploy_to}/etc && tar cvfz dump.tar.gz dump.sql"
18
+ commands << "rm #{deploy_to}/etc/dump.sql"
19
+ when 'restore'
20
+ commands << "cd #{deploy_to}/etc && if [ -f dump.tar.gz ]; then tar xvfz dump.tar.gz dump.sql ; fi"
21
+ commands << "if [ -f #{deploy_to}/etc/dump.sql ]; then mysql -u #{mysql_opts} < #{deploy_to}/etc/dump.sql && rm #{deploy_to}/etc/dump.sql ; fi"
22
+ when 'drop_all'
23
+ commands << "mysqldump #{mysql_opts_no_data} | grep ^DROP | mysql #{mysql_opts}"
24
+ end
25
+
26
+ commands.each do |cmd|
27
+ puts "executando: #{cmd.gsub(cfg['password'], 'xxxxxxxx')}"
28
+ `#{cmd}`
29
+ end
30
+ puts "operacao #{operation} finalizada."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: akitaonrails-locarails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Akita
@@ -11,8 +11,25 @@ cert_chain: []
11
11
 
12
12
  date: 2008-08-19 00:00:00 -07:00
13
13
  default_executable: locarails
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: capistrano
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: highline
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
16
33
  description: A maneira mais simples para instalar aplicacoes Rails na hospedagem Linux da Locaweb.
17
34
  email: fabio.akita@locaweb.com.br
18
35
  executables: