akitaonrails-locarails 1.0.0 → 1.1.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.
data/Manifest CHANGED
@@ -6,6 +6,9 @@ README
6
6
  locarails.gemspec
7
7
  lib/locarails.rb
8
8
  lib/locarails/version.rb
9
- templates/database.locaweb.yml
10
- templates/deploy.rb
11
- templates/locaweb_backup.rb
9
+ templates/database.locaweb.yml.erb
10
+ templates/deploy.rb.erb
11
+ templates/deploy.common.rb
12
+ templates/locaweb_backup.rb
13
+ templates/ssh_helper.rb
14
+ templates/.gitignore
@@ -2,6 +2,7 @@
2
2
  #
3
3
  # Created on 2008-8-19.
4
4
  # Copyright (c) 2008. All rights reserved.
5
+ # Fabio Akita - akitaonrails.com
5
6
 
6
7
  begin
7
8
  require 'rubygems'
@@ -11,15 +12,49 @@ end
11
12
 
12
13
  require 'ostruct'
13
14
  require 'optparse'
15
+ require 'erb'
16
+
17
+ # configuracoes da sua hospedagem
18
+ config = OpenStruct.new
19
+ mysqlcfg = OpenStruct.new
20
+
21
+ # defaults
22
+ config.force = false
23
+ config.mode = 'copy'
14
24
 
15
25
  OptionParser.new do |opts|
16
- opts.banner = "Uso: #{File.basename($0)} [caminho]"
26
+ opts.banner = <<-STR
27
+ LocaRails - configurador de Capistrano para projetos Rails
28
+ especifico para hospedagens Linux da Locaweb
29
+
30
+ Uso: #{File.basename($0)} [caminho] [opcoes]
31
+ STR
17
32
 
18
33
  opts.on("-h", "--help", "Mostra esta tela de ajuda") do
19
34
  puts opts
20
35
  exit 0
21
36
  end
22
-
37
+
38
+ opts.on("-m", "--mode=tipo", "modo de deployment (copy/git)", "Padrao: copy") do |mode|
39
+ config.mode = mode
40
+ unless "copy|git".include?(mode)
41
+ puts "#{mode} nao existe. Escolha copy ou git como --mode."
42
+ exit 0
43
+ end
44
+ end
45
+
46
+ opts.on("-s", "--sshkey=caminho", "local da sua chave privada SSH", "Padrao: ~/.ssh/id_rsa") do |path|
47
+ config.ssh_key_path = path
48
+ unless File.exists?(path)
49
+ puts "Chave inexistente no caminho #{path}."
50
+ exit 0
51
+ end
52
+ end
53
+
54
+ opts.on("-f", "--force", "Sobrescreve seu config/deploy.rb, caso exista.", "Cuidado: voce ira perder sua configuracao anterior") do
55
+ config.force = true
56
+ end
57
+
23
58
  begin
24
59
  opts.parse!(ARGV)
25
60
  rescue OptionParser::ParseError => e
@@ -44,37 +79,40 @@ def get_input(message)
44
79
  gets.strip
45
80
  end
46
81
 
47
- # configuracoes da sua hospedagem
48
- config = OpenStruct.new
49
- mysqlcfg = OpenStruct.new
50
-
51
82
  # configuracoes locais da sua maquina
52
83
  config.bin_path = File.dirname(File.expand_path(__FILE__))
53
84
  config.local_path = File.expand_path(ARGV.shift)
54
85
  config.app_name = config.local_path.split('/').last
55
86
 
56
- if File.exists?(File.join(config.local_path, 'config/deploy.rb'))
87
+ if !config.force && File.exists?(File.join(config.local_path, 'config/deploy.rb'))
57
88
  abort "Voce ja tem capistrano configurado em config/deploy.rb. Configuracao abortada."
58
89
  end
59
90
 
60
91
  puts <<-STR
61
- ==================================================
62
- Bem Vindos ao configurador de projetos da Locaweb
92
+ ========================================================
93
+ Bem Vindos ao configurador de projetos da Locaweb.
94
+
63
95
  Vamos configurar seu projeto Rails para melhor se
64
- Adequar nas nossas hospedagens Linux compartilhada
96
+ Adequar nas nossas hospedagens Linux compartilhada.
97
+
98
+ O comando locarails deve ser executado *dentro* do
99
+ diretorio do seu projeto, na sua maquina local.
100
+
65
101
  Para tanto precisaremos de algumas informacoes:
66
- ==================================================
102
+ ========================================================
103
+
104
+ Estrategia de instalacao via: #{config.mode}
67
105
 
68
- Garanta que a seguinte pasta contem sua aplicacao
69
- Rails: #{config.local_path}
106
+ Garanta que a seguinte pasta contem sua aplicacao Rails:
107
+ #{config.local_path}
70
108
 
71
109
  STR
72
110
 
73
111
  # configuracoes inseridas manualmente pelo usuario
74
- config.app_name = get_input( "O nome da sua aplicacao" ) if config.app_name.nil? || config.app_name.empty?
75
- config.dominio = get_input "O dominio do seu site (ex. teste.tempsite.ws)"
112
+ config.app_name = get_input( "Nome da sua aplicacao" ) if config.app_name.nil? || config.app_name.empty?
113
+ config.dominio = get_input "Dominio do seu site (ex. teste.tempsite.ws)"
76
114
  config.usuario = get_input "Seu usuario de hospedagem"
77
- mysqlcfg.db = get_input "O nome do seu banco mysql"
115
+ mysqlcfg.db = get_input "Nome do seu banco mysql"
78
116
  mysqlcfg.user = get_input "Seu usuario de mysql"
79
117
  mysqlcfg.pass = get_input "Sua senha de mysql"
80
118
  mysqlcfg.host = get_input "Seu servidor mysql (ex. mysqlxxx.locaweb.com.br)"
@@ -82,36 +120,40 @@ mysqlcfg.host = get_input "Seu servidor mysql (ex. mysqlxxx.locaweb.com.br)"
82
120
  # forca rodar capistrano
83
121
  unless File.exists?("#{config.local_path}/Capfile")
84
122
  puts "- Executando Capistrano no seu projeto ..."
85
- `capify .`
123
+ begin
124
+ `capify .`
125
+ rescue Exception => e
126
+ puts <<-STR
127
+ ERRO: Voce provavelmente nao tem Capistrano instalado
128
+ Rode: sudo gem install capistrano
129
+
130
+ STR
131
+ end
86
132
  end
87
133
 
88
134
  FileUtils.copy_file("#{config.bin_path}/../templates/locaweb_backup.rb",
89
135
  "#{config.local_path}/config/locaweb_backup.rb")
90
-
136
+
91
137
  File.open("#{config.local_path}/config/deploy.rb", 'w+') do |out|
92
- buffer = File.open("#{config.bin_path}/../templates/deploy.rb", 'r') do | f |
93
- f.read.gsub('seu.usuario', config.usuario).
94
- gsub('seu.dominio', config.dominio).
95
- gsub('sua.aplicacao', config.app_name).
96
- gsub('seu.projeto', config.local_path)
97
- end
138
+ erb = ERB.new(File.read("#{config.bin_path}/../templates/deploy.rb.erb"))
139
+ buffer = erb.result(config.send(:binding))
140
+ buffer << File.read("#{config.bin_path}/../templates/deploy.common.rb")
98
141
  out.puts buffer
99
142
  end
100
143
 
101
144
  File.open("#{config.local_path}/config/database.locaweb.yml", 'w+') do |out|
102
- buffer = File.open("#{config.bin_path}/../templates/database.locaweb.yml", 'r') do | f |
103
- f.read.gsub('mysql.database', mysqlcfg.db).
104
- gsub('mysql.username', mysqlcfg.user).
105
- gsub('mysql.password', mysqlcfg.pass).
106
- gsub('mysql.hostname', mysqlcfg.host)
107
- end
108
- out.puts buffer
145
+ erb = ERB.new(File.read("#{config.bin_path}/../templates/database.locaweb.yml.erb"))
146
+ out.puts erb.result(mysqlcfg.send(:binding))
109
147
  end
110
148
 
111
149
  puts <<-STR
112
150
 
113
- # Parabéns, voce terminou de configurar sua aplicacao Rails!
114
- # Para configurar sua hospedagem execute 'cap deploy:setup'.
115
- # Para subir seu projeto execute 'cap deploy'
151
+ # Parabens, voce terminou de configurar sua aplicacao Rails!
152
+ # Execute apenas uma vez 'cap deploy:setup' para configurar os
153
+ diretorios remotamente na sua hospedagem.
154
+ # Em seguida execute 'cap deploy' para transportar sua aplicacao
155
+ para a hospedagem. Sempre que quiser atualizar sua aplicacao
156
+ no servidor apenas execute novamente 'cap deploy'
157
+
116
158
  [finalizado!]
117
159
  STR
@@ -1,7 +1,7 @@
1
1
  module Locarails
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 1
4
- MINOR = 0
4
+ MINOR = 1
5
5
  TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{locarails}
3
- s.version = "1.0.0"
3
+ s.version = "1.1.0"
4
4
 
5
5
  s.specification_version = 2 if s.respond_to? :specification_version=
6
6
 
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.description = %q{A maneira mais simples para instalar aplicacoes Rails na hospedagem Linux da Locaweb.}
12
12
  s.email = %q{fabio.akita@locaweb.com.br}
13
13
  s.executables = ["locarails"]
14
- s.files = ["bin/locarails", "bin/locarails.cmd", "LICENSE", "Manifest", "README", "lib/locarails.rb", "lib/locarails/version.rb", "templates/database.locaweb.yml", "templates/deploy.rb", "templates/locaweb_backup.rb", "locarails.gemspec"]
14
+ s.files = ["bin/locarails", "bin/locarails.cmd", "LICENSE", "Manifest", "README", "locarails.gemspec", "lib/locarails.rb", "lib/locarails/version.rb", "templates/database.locaweb.yml.erb", "templates/deploy.rb.erb", "templates/deploy.common.rb", "templates/locaweb_backup.rb", "templates/ssh_helper.rb", "templates/.gitignore"]
15
15
  s.has_rdoc = true
16
16
  s.homepage = %q{http://www.locaweb.com.br/rails}
17
17
  s.require_paths = ["lib"]
@@ -0,0 +1,22 @@
1
+ log/*.log
2
+ log/*.out
3
+ log/*.pid
4
+ tmp/**/*
5
+ .DS_Store
6
+ db/schema.rb
7
+ db/schema.sql
8
+ db/*.sqlite3*
9
+ config/database.yml
10
+ config/database.locaweb.yml
11
+ public/photos/*
12
+ public/upload/*
13
+ public/assets/*
14
+ .project
15
+ .#*
16
+ tmp/.*
17
+ \#*
18
+ *~
19
+ vendor/**/**/doc/*
20
+ rsa_key*
21
+ coverage/
22
+ coverage
@@ -0,0 +1,7 @@
1
+ production:
2
+ adapter: mysql
3
+ encoding: utf8
4
+ database: <%= mysqlcfg.db %>
5
+ username: <%= mysqlcfg.user %>
6
+ password: <%= mysqlcfg.pass %>
7
+ host: <%= mysqlcfg.host %>
@@ -0,0 +1,86 @@
1
+ # =============================================================================
2
+ # TAREFAS - NAO MEXER A MENOS QUE SAIBA O QUE ESTA FAZENDO
3
+ # =============================================================================
4
+ desc "Garante que o database.yml foi corretamente configurado"
5
+ task :before_symlink do
6
+ on_rollback {}
7
+ run "test -d #{release_path}/tmp || mkdir -m 755 #{release_path}/tmp"
8
+ run "test -d #{release_path}/db || mkdir -m 755 #{release_path}/db"
9
+ run "cp #{deploy_to}/etc/database.yml #{release_path}/config/database.yml"
10
+ run "cd #{release_path} && rake db:migrate RAILS_ENV=production"
11
+ end
12
+
13
+ desc "Garante que as configuracoes estao adequadas"
14
+ task :before_setup do
15
+ ts = Time.now.strftime("%y%m%d%H%M%S")
16
+ # git folder
17
+ run "test -d #{git_repo} || mkdir -p -m 755 #{git_repo}"
18
+ run "test -d #{git_repo}/.git || cd #{git_repo} && git init"
19
+ git_config = File.join(File.dirname(__FILE__), "../.git/config")
20
+ has_git = false
21
+ if File.exists?(git_config) && File.read(git_config) !~ /locaweb/
22
+ `git remote add locaweb #{repository}`
23
+ `git push locaweb #{branch}`
24
+ has_git = true
25
+ end
26
+
27
+ run "if [ -d #{deploy_to} ]; then mv #{deploy_to} #{deploy_to}-#{ts}.old ; fi"
28
+ run "test -d #{deploy_to} || mkdir -m 755 #{deploy_to}"
29
+ run "test -d #{deploy_to}/etc || mkdir -m 755 #{deploy_to}/etc"
30
+ run "if [ -d #{site_path} ]; then mv #{site_path} #{site_path}-#{ts}.old ; fi"
31
+ run "if [ -h #{site_path} ]; then mv #{site_path} #{site_path}-#{ts}.old ; fi"
32
+ run "ln -s #{deploy_to}/current/public #{public_html}/#{application}"
33
+ put File.read(File.dirname(__FILE__) + "/database.locaweb.yml"), "#{deploy_to}/etc/database.yml"
34
+
35
+ # ssh keygen
36
+ put File.read(File.dirname(__FILE__) + "/ssh_helper.rb"), "#{deploy_to}/etc/ssh_helper.rb"
37
+ run "test -f .ssh/id_rsa || ruby #{deploy_to}/etc/ssh_helper.rb /home/#{user}/.ssh/id_rsa #{domain} #{user}"
38
+
39
+ unless has_git
40
+ 3.times { puts "" }
41
+ puts "==============================================================="
42
+ puts "Rode os seguintes comandos depois de criar seu repositorio Git:"
43
+ puts ""
44
+ puts " git remote add locaweb #{repository}"
45
+ puts " git push locaweb #{branch}"
46
+ puts "==============================================================="
47
+ 3.times { puts "" }
48
+ end
49
+ end
50
+
51
+ desc "Prepare the production database before migration"
52
+ task :before_cold do
53
+ end
54
+
55
+ namespace :deploy do
56
+ desc "Pede restart ao servidor Passenger"
57
+ task :restart, :roles => :app do
58
+ run "touch #{deploy_to}/current/tmp/restart.txt"
59
+ end
60
+ end
61
+
62
+ [:start, :stop].each do |t|
63
+ desc "A tarefa #{t} nao eh necessaria num ambiente com Passenger"
64
+ task t, :roles => :app do ; end
65
+ end
66
+
67
+ namespace :log do
68
+ desc "tail production log files"
69
+ task :tail, :roles => :app do
70
+ run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
71
+ puts # para uma linha extra
72
+ puts "#{channel[:host]}: #{data}"
73
+ break if stream == :err
74
+ end
75
+ end
76
+ end
77
+
78
+ namespace :db do
79
+ desc "Faz o backup remoto do banco de dados MySQL e ja faz o download"
80
+ task :backup, :roles => :db do
81
+ backup_rb ||= "#{deploy_to}/current/config/locaweb_backup.rb"
82
+ run "if [ -f #{backup_rb} ]; then ruby #{backup_rb} #{deploy_to} ; fi"
83
+ get "#{deploy_to}/etc/dump.tar.gz", "dump.tar.gz"
84
+ run "rm #{deploy_to}/etc/dump.tar.gz"
85
+ end
86
+ end
@@ -57,11 +57,13 @@
57
57
  # =============================================================================
58
58
  # CONFIGURE OS VALORES DE ACORDO COM SUA HOSPEDAGEM
59
59
  # =============================================================================
60
- set :user, "seu.usuario"
61
- set :domain, "seu.dominio"
62
- set :application, "sua.aplicacao"
63
- set :repository, "seu.projeto"
64
- #ssh_options[:keys] = File.expand_path("~/.ssh/id_rsa") # apenas descomente se tiver chave
60
+ set :user, "<%= config.usuario %>"
61
+ set :domain, "<%= config.dominio %>"
62
+ set :application, "<%= config.app_name %>"
63
+ set :repository, "<%= config.local_path %>"
64
+ <% if config.ssh_key_path %>
65
+ ssh_options[:keys] = File.expand_path("<%= config.ssh_key_path %>") # apenas descomente se tiver chave
66
+ <% end %>
65
67
 
66
68
  # =============================================================================
67
69
  # NAO MEXER DAQUI PARA BAIXO
@@ -77,70 +79,23 @@ set :runner, nil
77
79
  set :use_sudo, false
78
80
  set :no_release, true
79
81
 
82
+ <% if config.mode == 'git' %>
83
+ set :git_repo, "/home/#{user}/repo/#{application}.git"
84
+
85
+ set :scm, :git
86
+ set :branch, 'local'
87
+ set :deploy_via, :remote_cache
88
+ set :git_shallow_clone, 1
89
+ set :copy_exclude, %w(.git/* .svn/* log/* tmp/* .gitignore)
90
+ set :remote, "akitaonrails1"
91
+ set :scm_verbose, true
92
+ set :keep_releases, 5
93
+ <% else %>
80
94
  set :scm, :none # nenhum repositorio
81
95
  set :deploy_via, :copy
82
96
  set :copy_exclude, %w(.git/* .svn/* log/* tmp/*)
97
+ set :keep_releases, 5
98
+ <% end %>
83
99
 
84
100
  ssh_options[:username] = user
85
- ssh_options[:paranoid] = false
86
-
87
- set :backup_rb, "#{deploy_to}/current/config/locaweb_backup.rb"
88
-
89
- # =============================================================================
90
- # TAREFAS - NAO MEXER A MENOS QUE SAIBA O QUE ESTA FAZENDO
91
- # =============================================================================
92
- desc "Garante que o database.yml foi corretamente configurado"
93
- task :before_symlink do
94
- on_rollback {}
95
- run "test -d #{release_path}/tmp || mkdir -m 755 #{release_path}/tmp"
96
- run "cp #{deploy_to}/etc/database.yml #{release_path}/config/database.yml"
97
- run "cd #{release_path} && rake db:migrate RAILS_ENV=production"
98
- end
99
-
100
- desc "Garante que as configuracoes estao adequadas"
101
- task :before_setup do
102
- ts = Time.now.strftime("%y%m%d%H%M%S")
103
- run "if [ -d #{deploy_to} ]; then mv #{deploy_to} #{deploy_to}-#{ts}.old ; fi"
104
- run "test -d #{deploy_to} || mkdir -m 755 #{deploy_to}"
105
- run "test -d #{deploy_to}/etc || mkdir -m 755 #{deploy_to}/etc"
106
- run "if [ -d #{site_path} ]; then mv #{site_path} #{site_path}-#{ts}.old ; fi"
107
- run "if [ -h #{site_path} ]; then mv #{site_path} #{site_path}-#{ts}.old ; fi"
108
- run "ln -s #{deploy_to}/current/public #{public_html}/#{application}"
109
- put File.read(File.dirname(__FILE__) + "/database.locaweb.yml"), "#{deploy_to}/etc/database.yml"
110
- end
111
-
112
- desc "Prepare the production database before migration"
113
- task :before_cold do
114
- end
115
-
116
- namespace :deploy do
117
- desc "Pede restart ao servidor Passenger"
118
- task :restart, :roles => :app do
119
- run "touch #{deploy_to}/current/tmp/restart.txt"
120
- end
121
- end
122
-
123
- [:start, :stop].each do |t|
124
- desc "A tarefa #{t} nao eh necessaria num ambiente com Passenger"
125
- task t, :roles => :app do ; end
126
- end
127
-
128
- namespace :log do
129
- desc "tail production log files"
130
- task :tail, :roles => :app do
131
- run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
132
- puts # para uma linha extra
133
- puts "#{channel[:host]}: #{data}"
134
- break if stream == :err
135
- end
136
- end
137
- end
138
-
139
- namespace :db do
140
- desc "Faz o backup remoto do banco de dados MySQL e ja faz o download"
141
- task :backup, :roles => :db do
142
- run "if [ -f #{backup_rb} ]; then ruby #{backup_rb} #{deploy_to} ; fi"
143
- get "#{deploy_to}/etc/dump.tar.gz", "dump.tar.gz"
144
- run "rm #{deploy_to}/etc/dump.tar.gz"
145
- end
146
- end
101
+ ssh_options[:paranoid] = false
@@ -0,0 +1,317 @@
1
+ require 'rubygems'
2
+ gem "net-ssh", ">= 1.1.3"
3
+ require 'base64'
4
+ require 'net/ssh'
5
+
6
+ #--
7
+ # =============================================================================
8
+ # Copyright (c) 2004,2005 Jamis Buck (jamis@37signals.com)
9
+ # All rights reserved.
10
+ #
11
+ # This source file is distributed as part of the Net::SSH Secure Shell Client
12
+ # library for Ruby. This file (and the library as a whole) may be used only as
13
+ # allowed by either the BSD license, or the Ruby license (or, by association
14
+ # with the Ruby license, the GPL). See the "doc" subdirectory of the Net::SSH
15
+ # distribution for the texts of these licenses.
16
+ # -----------------------------------------------------------------------------
17
+ # net-ssh website : http://net-ssh.rubyforge.org
18
+ # project website: http://rubyforge.org/projects/net-ssh
19
+ # =============================================================================
20
+ #++
21
+
22
+ module Net
23
+ module SSH
24
+
25
+ module Util
26
+
27
+ # The abstract ancestor module of both ReaderBufferImpl and
28
+ # WriterBufferImpl. It defines the common interface for both submodules.
29
+ module BufferBase
30
+
31
+ # exposes the content of the buffer
32
+ attr_reader :content
33
+
34
+ # the length of the buffer's content.
35
+ def length
36
+ @content.length
37
+ end
38
+
39
+ # returns a copy of the buffer's content.
40
+ def to_s
41
+ ( @content || "" ).dup
42
+ end
43
+
44
+ # Compares the contents of the two buffers.
45
+ def ==( buffer )
46
+ to_s == buffer.to_s
47
+ end
48
+
49
+ # Resets the buffer, making it empty.
50
+ def clear!
51
+ @content = ""
52
+ end
53
+
54
+ def init_BufferBase( content="" )
55
+ @content = content
56
+ end
57
+ private :init_BufferBase
58
+
59
+ end
60
+
61
+ # A convenience module for representing a string of encoded data. It
62
+ # provides an interface for easily reading and decoding the buffer.
63
+ module ReaderBufferImpl
64
+
65
+ # the current position of the pointer in the buffer
66
+ attr_reader :position
67
+
68
+ # used by derived modules and classes to perform any
69
+ # reader-buffer-specific initialization.
70
+ def init_ReaderBufferImpl
71
+ @position = 0
72
+ end
73
+ private :init_ReaderBufferImpl
74
+
75
+ # Appends the given text to the end of the buffer.
76
+ def append( text )
77
+ @content << text
78
+ end
79
+
80
+ # Returns all text from the current pointer to the end of the buffer as
81
+ # a new buffer as the same class as the receiver.
82
+ def remainder_as_buffer
83
+ self.class.new( @content[ @position..-1 ] )
84
+ end
85
+
86
+ # Reads +count+ bytes from the buffer. If +count+ is +nil+, this will
87
+ # return all remaining text in the buffer. This method will increment
88
+ # the pointer.
89
+ def read( count = nil )
90
+ count = length - @position unless count
91
+ return nil if @position + count > length
92
+
93
+ @position += count
94
+ @content[ @position-count, count ]
95
+ end
96
+
97
+ # Return the next 8 bytes as a 64-bit integer (in network byte order).
98
+ def read_int64
99
+ hi = read_long
100
+ lo = read_long
101
+ return ( hi << 32 ) + lo
102
+ end
103
+
104
+ # Return the next four bytes as a long integer (in network byte order).
105
+ def read_long
106
+ b = read( 4 ) or return nil
107
+ b.unpack( "N" ).first
108
+ end
109
+
110
+ # Read the next two bytes as a short integer (in network byte order).
111
+ def read_short
112
+ b = read( 2 ) or return nil
113
+ b.unpack( "n" ).first
114
+ end
115
+
116
+ # Read and return the next byte in the buffer.
117
+ def read_byte
118
+ b = read( 1 ) or return nil
119
+ b[0]
120
+ end
121
+
122
+ # Read and return an SSH2-encoded string. The string starts with a long
123
+ # integer that describes the number of bytes remaining in the string.
124
+ def read_string
125
+ length = read_long or return nil
126
+ read( length )
127
+ end
128
+
129
+ # Read a single byte and convert it into a boolean, using 'C' rules
130
+ # (i.e., zero is false, non-zero is true).
131
+ def read_bool
132
+ b = read( 1 ) or return nil
133
+ b[0] != 0
134
+ end
135
+
136
+ # Resets the pointer to the start of the buffer.
137
+ def reset!
138
+ @position = 0
139
+ end
140
+
141
+ # Returns true if the pointer is at the end of the buffer.
142
+ def eof?
143
+ @position >= length
144
+ end
145
+
146
+ # Resets the buffer, making it empty.
147
+ def clear!
148
+ @content = ""
149
+ @position = 0
150
+ end
151
+
152
+ end # ReaderBufferImpl
153
+
154
+ # A convenience module for writing a string of encoded data. It provides
155
+ # an interface for easily writing and encoding data.
156
+ module WriterBufferImpl
157
+
158
+ def init_WriterBufferImpl
159
+ # nothing
160
+ end
161
+ private :init_WriterBufferImpl
162
+
163
+ # Writes the given data literally into the string.
164
+ def write( *data )
165
+ @content << data.join
166
+ end
167
+
168
+ # Writes each argument to the buffer as a network-byte-order-encoded
169
+ # 64-bit integer (8 bytes).
170
+ def write_int64( *n )
171
+ n.each do |i|
172
+ hi = ( i >> 32 ) & 0xFFFFFFFF
173
+ lo = i & 0xFFFFFFFF
174
+ @content << [ hi, lo ].pack( "N2" )
175
+ end
176
+ end
177
+
178
+ # Writes each argument to the buffer as a network-byte-order-encoded
179
+ # long (4-byte) integer.
180
+ def write_long( *n )
181
+ @content << n.pack( "N*" )
182
+ end
183
+
184
+ # Writes each argument to the buffer as a network-byte-order-encoded
185
+ # short (2-byte) integer.
186
+ def write_short( *n )
187
+ @content << n.pack( "n*" )
188
+ end
189
+
190
+ # Writes each argument to the buffer as a byte.
191
+ def write_byte( *n )
192
+ @content << n.map { |c| c.chr }.join
193
+ end
194
+
195
+ # Writes each argument to the buffer as an SSH2-encoded string. Each
196
+ # string is prefixed by its length, encoded as a 4-byte long integer.
197
+ def write_string( *text )
198
+ text.each do |string|
199
+ write_long( string.length )
200
+ write( string )
201
+ end
202
+ end
203
+
204
+ # Writes each argument to the buffer as a (C-style) boolean, with 1
205
+ # meaning true, and 0 meaning false.
206
+ def write_bool( *b )
207
+ @content << b.map { |v| ( v ? 1 : 0 ).chr }.join
208
+ end
209
+
210
+ # Writes each argument to the buffer as a bignum (SSH2-style). No
211
+ # checking is done to ensure that the arguments are, in fact, bignums.
212
+ def write_bignum( *n )
213
+ @content << n.map { |b| b.to_ssh }.join
214
+ end
215
+
216
+ # Writes the given arguments to the buffer as SSH2-encoded keys.
217
+ def write_key( *key )
218
+ key.each do |k|
219
+ write_string( k.ssh_type )
220
+
221
+ case k.ssh_type
222
+ when "ssh-dss"
223
+ write_bignum( k.p )
224
+ write_bignum( k.q )
225
+ write_bignum( k.g )
226
+ write_bignum( k.pub_key )
227
+
228
+ when "ssh-rsa"
229
+ write_bignum( k.e )
230
+ write_bignum( k.n )
231
+
232
+ else
233
+ raise NotImplementedError,
234
+ "unsupported key type '#{k.ssh_type}'"
235
+ end
236
+ end
237
+ end
238
+
239
+ end # class WriterBufferImpl
240
+
241
+ # A convenience class for a read-only buffer.
242
+ class ReaderBuffer
243
+ include BufferBase
244
+ include ReaderBufferImpl
245
+
246
+ def initialize( content )
247
+ init_BufferBase( content )
248
+ init_ReaderBufferImpl
249
+ end
250
+ end
251
+
252
+ # A convenience class for a write-only buffer.
253
+ class WriterBuffer
254
+ include BufferBase
255
+ include WriterBufferImpl
256
+
257
+ def initialize( content="" )
258
+ init_BufferBase( content )
259
+ init_WriterBufferImpl
260
+ end
261
+ end
262
+
263
+ # A convenience class for a read/write buffer.
264
+ class Buffer
265
+ include BufferBase
266
+ include ReaderBufferImpl
267
+ include WriterBufferImpl
268
+
269
+ def initialize( content="" )
270
+ init_BufferBase( content )
271
+ init_ReaderBufferImpl
272
+ init_WriterBufferImpl
273
+ end
274
+ end
275
+
276
+ end # module Util
277
+
278
+ end # module SSH
279
+ end # module Net
280
+
281
+ class SSHHelper
282
+ def self.generate_keys(filename)
283
+ authorized_file = "#{File.dirname(filename)}/authorized_keys"
284
+
285
+ private_key = OpenSSL::PKey::RSA.new(1024)
286
+ public_pem = export_ssh_pubkey( private_key )
287
+
288
+ File.open( filename, "w" ) { |file| file.write private_key.export }
289
+ File.open( "#{filename}.pub", "w" ) { |file| file.write public_pem }
290
+ File.open( authorized_file, 'w') { |file| file.write public_pem + "\n"}
291
+
292
+ FileUtils.chmod 0755, authorized_file
293
+ FileUtils.chmod 0600, filename
294
+ end
295
+
296
+ def self.test_connection(hostname, username)
297
+ # will create known_host
298
+ Net::SSH.start(hostname, username) { |ssh| puts ssh.exec!('hostname') }
299
+ end
300
+
301
+ private
302
+ def self.export_ssh_pubkey( key )
303
+ s = key.ssh_type + " "
304
+
305
+ writer = Net::SSH::Util::WriterBuffer.new
306
+ writer.write_key key
307
+ s << Base64.encode64( writer.to_s ).strip.gsub( /[\n\r\t ]/, "" )
308
+ s << " #{ENV['USER']}@#{ENV['HOSTNAME']}"
309
+
310
+ s
311
+ end
312
+ end
313
+
314
+ if ARGV && ARGV.size == 3
315
+ SSHHelper.generate_keys(ARGV[0])
316
+ SSHHelper.test_connection(ARGV[1], ARGV[2])
317
+ end
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.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Akita
@@ -27,12 +27,15 @@ files:
27
27
  - LICENSE
28
28
  - Manifest
29
29
  - README
30
+ - locarails.gemspec
30
31
  - lib/locarails.rb
31
32
  - lib/locarails/version.rb
32
- - templates/database.locaweb.yml
33
- - templates/deploy.rb
33
+ - templates/database.locaweb.yml.erb
34
+ - templates/deploy.rb.erb
35
+ - templates/deploy.common.rb
34
36
  - templates/locaweb_backup.rb
35
- - locarails.gemspec
37
+ - templates/ssh_helper.rb
38
+ - templates/.gitignore
36
39
  has_rdoc: true
37
40
  homepage: http://www.locaweb.com.br/rails
38
41
  post_install_message:
@@ -1,7 +0,0 @@
1
- production:
2
- adapter: mysql
3
- encoding: utf8
4
- database: mysql.database
5
- username: mysql.username
6
- password: mysql.password
7
- host: mysql.hostname