torquebox-remote-deployer 0.1.0 → 0.1.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 CHANGED
@@ -1,2 +1,3 @@
1
1
  *.gem
2
- .idea
2
+ .idea
3
+ tmp
@@ -1,44 +1,66 @@
1
1
  require "net/ssh"
2
2
  require "net/scp"
3
3
 
4
+ require 'tempfile'
5
+
4
6
  module TorqueBox
5
7
  module RemoteDeployUtils
6
8
  class << self
7
9
 
8
10
  def stage(archive_file)
9
11
  with_config(archive_file) do |config, app_name|
10
- cleanup_stage(config, archive_file, app_name)
11
- prepare_stage(config, app_name)
12
- stage_archive(config, archive_file)
13
- unjar_staged_archive(config, archive_file, app_name)
12
+ # no need to stage if we are local. we'll just run from the app dir
13
+ unless config.local
14
+ cleanup_stage(config, archive_file, app_name)
15
+ prepare_stage(config, app_name)
16
+ stage_archive(config, archive_file)
17
+ unjar_staged_archive(config, archive_file, app_name)
18
+ end
14
19
  end
15
20
  end
16
21
 
17
22
  def deploy(archive_file)
18
23
  with_config(archive_file) do |config, app_name|
19
- scp_upload(config, archive_file, "#{config.torquebox_home}/jboss/standalone/deployments/")
24
+ scp_upload(config, archive_file, "#{config.jboss_home}/standalone/deployments/")
20
25
  do_deploy(config, app_name)
21
26
  end
22
27
  end
23
28
 
24
29
  def deploy_from_stage(archive_file)
25
30
  with_config(archive_file) do |config, app_name|
26
- ssh_exec(config, "cp #{config.torquebox_home}/stage/#{app_name}.knob #{config.torquebox_home}/jboss/standalone/deployments")
31
+ unless config.local
32
+ ssh_exec(config, "cp #{config.torquebox_home}/stage/#{app_name}.knob #{config.jboss_home}/standalone/deployments")
33
+ else
34
+ scp_upload(config, archive_file, "#{config.jboss_home}/standalone/deployments/")
35
+ end
27
36
  do_deploy(config, app_name)
28
37
  end
29
38
  end
30
39
 
31
40
  def undeploy(archive_file)
32
41
  with_config(archive_file) do |config, app_name|
33
- ssh_exec(config, "rm -f #{config.torquebox_home}/jboss/standalone/deployments/#{app_name}.knob*")
42
+ unless config.local
43
+ ssh_exec(config, "rm -f #{config.jboss_home}/standalone/deployments/#{app_name}-knob.yml*")
44
+ ssh_exec(config, "rm -f #{config.jboss_home}/standalone/deployments/#{app_name}.knob")
45
+ else
46
+ FileUtils.rm("#{config.jboss_home}/standalone/deployments/#{app_name}-knob.yml*")
47
+ FileUtils.rm("#{config.jboss_home}/standalone/deployments/#{app_name}.knob")
48
+ end
34
49
  end
35
50
  end
36
51
 
37
52
  def exec_ruby(archive_file, cmd)
38
53
  with_config(archive_file) do |config, app_name|
39
- ssh_exec(config, "cd #{config.torquebox_home}/stage/#{app_name}",
40
- "export PATH=$PATH:#{config.torquebox_home}/jruby/bin",
41
- "#{config.torquebox_home}/jruby/bin/jruby -S #{cmd}")
54
+ unless config.local
55
+ # TODO set RACK_ENV based on env var, and default to production
56
+ ssh_exec(config, "cd #{config.torquebox_home}/stage/#{app_name}",
57
+ "export PATH=$PATH:#{config.torquebox_home}/jruby/bin",
58
+ "export RAILS_ENV=production",
59
+ "export RACK_ENV=production",
60
+ "#{config.torquebox_home}/jruby/bin/jruby -S #{cmd}")
61
+ else
62
+ # not sure what to do here yet
63
+ end
42
64
  end
43
65
  end
44
66
 
@@ -49,7 +71,25 @@ module TorqueBox
49
71
  end
50
72
 
51
73
  def do_deploy(config, app_name)
52
- ssh_exec(config, "touch #{config.torquebox_home}/jboss/standalone/deployments/#{app_name}.knob.dodeploy")
74
+ # TODO set RACK_ENV based on env var, and default to production
75
+ knob_yml = <<-YAML
76
+ application:
77
+ root: #{config.jboss_home}/standalone/deployments/#{app_name}.knob
78
+ environment:
79
+ RACK_ENV: production
80
+ RAILS_ENV: production
81
+ YAML
82
+
83
+ unless config.local
84
+ knob_yml_file = Tempfile.new("#{app_name}-knob.yml")
85
+ knob_yml_file.write(knob_yml)
86
+ knob_yml_file.rewind
87
+ scp_upload(config, knob_yml_file.path, "#{config.jboss_home}/standalone/deployments/#{app_name}-knob.yml")
88
+ ssh_exec(config, "touch #{config.jboss_home}/standalone/deployments/#{app_name}-knob.yml.dodeploy")
89
+ else
90
+ # todo copy temp file to somewhere
91
+ File.open("#{config.jboss_home}/standalone/deployments/#{app_name}-knob.yml.dodeploy", "w") {}
92
+ end
53
93
  end
54
94
 
55
95
  def app_name(archive_file)
@@ -70,11 +110,17 @@ module TorqueBox
70
110
  end
71
111
 
72
112
  def prepare_stage(config, app_name)
73
- ssh_exec(config, "mkdir -p #{config.torquebox_home}/stage/#{app_name}")
113
+ unless config.local
114
+ ssh_exec(config, "mkdir -p #{config.torquebox_home}/stage/#{app_name}")
115
+ else
116
+ FileUtils.mkdir_p("#{config.torquebox_home}/stage/#{app_name}")
117
+ end
74
118
  end
75
119
 
76
120
  def with_config(archive_file)
77
- yield read_config, app_name(archive_file)
121
+ read_config.each do |config|
122
+ yield config, app_name(archive_file)
123
+ end
78
124
  end
79
125
 
80
126
  def ssh_exec(config, *cmd)
@@ -84,16 +130,21 @@ module TorqueBox
84
130
  end
85
131
 
86
132
  def scp_upload(config, local_file, remote_file)
87
- Net::SCP.upload!(config.hostname, config.user, local_file, remote_file,
88
- :ssh => {:port => config.port, :keys => [config.key]}
89
- ) do |ch, name, sent, total|
90
- print "\rCopying #{name}: #{sent}/#{total}"
133
+ unless config.local
134
+ Net::SCP.upload!(config.hostname, config.user, local_file, remote_file,
135
+ :ssh => {:port => config.port, :keys => [config.key]}
136
+ ) do |ch, name, sent, total|
137
+ print "\rCopying #{name}: #{sent}/#{total}"
138
+ end
139
+ print "\n"
140
+ else
141
+ FileUtils.cp(local_file, remote_file)
91
142
  end
92
- print "\n"
93
143
  end
94
144
 
95
145
  def read_config
96
- eval(File.read("config/torquebox_remote.rb")).config
146
+ config_file = ENV["CONFIG_FILE"] || ENV["config_file"] || "config/torquebox_remote.rb"
147
+ eval(File.read(config_file)).configurations
97
148
  end
98
149
  end
99
150
  end
@@ -105,11 +156,17 @@ module TorqueBox
105
156
 
106
157
  def initialize(blk)
107
158
  @config = RemoteConfig.new
159
+ @configs = []
108
160
  instance_eval &blk
109
161
  end
110
162
 
111
163
  attr_reader :config
112
164
 
165
+ def configurations
166
+ # evenually, we should merge the base @config settings into the @configs
167
+ @configs.empty? ? [@config] : @configs
168
+ end
169
+
113
170
  def hostname(h)
114
171
  @config.hostname = h
115
172
  end
@@ -130,18 +187,39 @@ module TorqueBox
130
187
  @config.torquebox_home = tbh
131
188
  end
132
189
 
190
+ def jboss_home(jbh)
191
+ @config.jboss_home = jbh
192
+ end
193
+
133
194
  def sudo(sudo)
134
195
  @config.sudo = sudo
135
196
  end
197
+
198
+ def local(l)
199
+ @config.local = l
200
+ end
201
+
202
+ def host(&block)
203
+ @configs << RemoteDeploy.new(block).config
204
+ end
136
205
  end
137
206
 
138
207
  class RemoteConfig
139
- attr_accessor :hostname, :port, :user, :key, :torquebox_home, :sudo
208
+ attr_accessor :hostname, :port, :user, :key, :torquebox_home, :sudo, :local
209
+
210
+ def jboss_home=(jbh)
211
+ @jboss_home = jbh
212
+ end
213
+
214
+ def jboss_home
215
+ @jboss_home || "#{@torquebox_home}/jboss"
216
+ end
140
217
 
141
218
  def initialize
142
219
  @user = "torquebox"
143
220
  @torquebox_home = "/opt/torquebox"
144
221
  @sudo = false
222
+ @local = false
145
223
  end
146
224
  end
147
225
  end
@@ -0,0 +1,5 @@
1
+ TorqueBox::RemoteDeploy.configure do
2
+ torquebox_home "#{File.expand_path("tmp/torquebox", File.dirname(__FILE__))}"
3
+ hostname "localhost"
4
+ local true
5
+ end
@@ -0,0 +1,18 @@
1
+ TorqueBox::RemoteDeploy.configure do
2
+ host do
3
+ torquebox_home "/my/tb/dir"
4
+ jboss_home "/my/jboss/dir"
5
+ hostname "1.2.3.4"
6
+ port "2222"
7
+ user "torquebox"
8
+ key "~/.ssh/id_rsa.pub"
9
+ sudo true
10
+ end
11
+
12
+ host do
13
+ hostname "4.4.4.4"
14
+ port "22"
15
+ user "deploy"
16
+ key "~/.ssh/torquebox.pub"
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ this is not really a knob file
@@ -0,0 +1,7 @@
1
+ TorqueBox::RemoteDeploy.configure do
2
+ torquebox_home "/opt/torquebox"
3
+ hostname "localhost"
4
+ port "2222"
5
+ user "torquebox"
6
+ key "~/.ssh/id_rsa.pub"
7
+ end
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+
3
+ describe TorqueBox::RemoteDeploy do
4
+ describe ".configure" do
5
+ context "defaults" do
6
+ subject do
7
+ TorqueBox::RemoteDeploy.configure do
8
+ hostname "1.2.3.4"
9
+ port "2222"
10
+ user "torquebox"
11
+ key "~/.ssh/id_rsa.pub"
12
+ end
13
+ end
14
+
15
+ it "has one configurations" do
16
+ subject.configurations.size.should == 1
17
+ subject.config.should == subject.configurations[0]
18
+ end
19
+
20
+ it "has hostname 1.2.3.4" do
21
+ subject.config.hostname.should == "1.2.3.4"
22
+ end
23
+
24
+ it "has port 2222" do
25
+ subject.config.port.should == "2222"
26
+ end
27
+
28
+ it "has user torquebox" do
29
+ subject.config.user.should == "torquebox"
30
+ end
31
+
32
+ it "has key '~/.ssh/id_rsa.pub'" do
33
+ subject.config.key.should == "~/.ssh/id_rsa.pub"
34
+ end
35
+
36
+ it "has torquebox_home '/opt/torquebox'" do
37
+ subject.config.torquebox_home.should == "/opt/torquebox"
38
+ end
39
+
40
+ it "has jboss_home '/opt/torquebox/jboss'" do
41
+ subject.config.jboss_home.should == "/opt/torquebox/jboss"
42
+ end
43
+
44
+ it "has sudo false" do
45
+ subject.config.sudo.should == false
46
+ end
47
+ end
48
+
49
+ context "overrides" do
50
+ subject do
51
+ TorqueBox::RemoteDeploy.configure do
52
+ torquebox_home "/my/tb/dir"
53
+ jboss_home "/my/jboss/dir"
54
+ hostname "1.2.3.4"
55
+ port "2222"
56
+ user "torquebox"
57
+ key "~/.ssh/id_rsa.pub"
58
+ sudo true
59
+ end
60
+ end
61
+
62
+ it "has one configurations" do
63
+ subject.configurations.size.should == 1
64
+ subject.config.should == subject.configurations[0]
65
+ end
66
+
67
+ it "has hostname 1.2.3.4" do
68
+ subject.config.hostname.should == "1.2.3.4"
69
+ end
70
+
71
+ it "has port 2222" do
72
+ subject.config.port.should == "2222"
73
+ end
74
+
75
+ it "has user torquebox" do
76
+ subject.config.user.should == "torquebox"
77
+ end
78
+
79
+ it "has key '~/.ssh/id_rsa.pub'" do
80
+ subject.config.key.should == "~/.ssh/id_rsa.pub"
81
+ end
82
+
83
+ it "has torquebox_home '/my/tb/dir'" do
84
+ subject.config.torquebox_home.should == "/my/tb/dir"
85
+ end
86
+
87
+ it "has jboss_home '/my/jboss/dir'" do
88
+ subject.config.jboss_home.should == "/my/jboss/dir"
89
+ end
90
+
91
+ it "has sudo true" do
92
+ subject.config.sudo.should == true
93
+ end
94
+ end
95
+
96
+ context "multiple hosts" do
97
+ subject do
98
+ TorqueBox::RemoteDeploy.configure do
99
+ host do
100
+ torquebox_home "/my/tb/dir"
101
+ jboss_home "/my/jboss/dir"
102
+ hostname "1.2.3.4"
103
+ port "2222"
104
+ user "torquebox"
105
+ key "~/.ssh/id_rsa.pub"
106
+ sudo true
107
+ end
108
+
109
+ host do
110
+ hostname "4.4.4.4"
111
+ port "22"
112
+ user "deploy"
113
+ key "~/.ssh/torquebox.pub"
114
+ end
115
+ end
116
+ end
117
+
118
+ it "has one configurations" do
119
+ subject.configurations.size.should == 2
120
+ end
121
+
122
+ it "has correct config" do
123
+ test_config(subject.configurations[0])
124
+ test_config(subject.configurations[1])
125
+ end
126
+
127
+ def test_config(configuration)
128
+ if configuration.hostname == "4.4.4.4"
129
+ configuration.torquebox_home.should == "/opt/torquebox"
130
+ elsif configuration.hostname == "1.2.3.4"
131
+ configuration.torquebox_home.should == "/my/tb/dir"
132
+ else
133
+ fail
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe TorqueBox::RemoteDeployUtils do
4
+
5
+ before(:each) do
6
+ @util = TorqueBox::RemoteDeployUtils
7
+ end
8
+
9
+ describe ".stage" do
10
+ it "stages one host" do
11
+ ENV["config_file"] = File.join(File.dirname(__FILE__), "fixtures/simple_torquebox_remote.rb")
12
+ @util.stub(:ssh_exec)
13
+ @util.should_receive(:scp_upload).with(anything(), "myapp.knob", "/opt/torquebox/stage/myapp.knob")
14
+ @util.stage("myapp.knob")
15
+ end
16
+
17
+ it "stages two hosts" do
18
+ ENV["config_file"] = File.join(File.dirname(__FILE__), "fixtures/multihost_torquebox_remote.rb")
19
+ @util.stub(:ssh_exec)
20
+ @util.should_receive(:scp_upload).with(anything(), "myapp.knob", "/my/tb/dir/stage/myapp.knob")
21
+ @util.should_receive(:scp_upload).with(anything(), "myapp.knob", "/opt/torquebox/stage/myapp.knob")
22
+ @util.stage("myapp.knob")
23
+ end
24
+
25
+ context "local" do
26
+ it "stages" do
27
+ ENV["config_file"] = File.join(File.dirname(__FILE__), "fixtures/local_torquebox_remote.rb")
28
+ @util.stage("myapp.knob") # does nothing
29
+ end
30
+ end
31
+ end
32
+
33
+ describe ".deploy" do
34
+ context "local" do
35
+ before do
36
+ FileUtils.mkdir_p(deploy_dir)
37
+ end
38
+
39
+ after do
40
+ FileUtils.remove_dir("#{File.dirname(__FILE__)}/../tmp/torquebox", true)
41
+ end
42
+
43
+ it "deploys" do
44
+ ENV["config_file"] = File.join(File.dirname(__FILE__), "fixtures/local_torquebox_remote.rb")
45
+ @util.deploy(File.join(File.dirname(__FILE__), "fixtures/myapp.knob"))
46
+ File.exists?("#{deploy_dir}/myapp.knob").should == true
47
+ File.exists?("#{deploy_dir}/myapp.knob.dodeploy").should == true
48
+ end
49
+
50
+ def deploy_dir
51
+ "#{File.dirname(__FILE__)}/../tmp/torquebox/jboss/standalone/deployments/"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ require 'java'
2
+
3
+ $: << File.dirname(__FILE__) + '/../lib'
4
+
5
+ require 'torquebox-remote-deployer'
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "torquebox-remote-deployer"
6
- s.version = "0.1.0"
6
+ s.version = "0.1.1"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Joe Kutner"]
9
9
  s.email = ["jpkutner@gmail.com"]
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: torquebox-remote-deployer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joe Kutner
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-01-18 00:00:00 Z
13
+ date: 2012-03-24 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: jruby-openssl
@@ -83,6 +83,13 @@ files:
83
83
  - lib/torquebox-remote-deployer.rb
84
84
  - lib/torquebox/rake/tasks/remote.rb
85
85
  - lib/torquebox/remote_deploy_utils.rb
86
+ - spec/fixtures/local_torquebox_remote.rb
87
+ - spec/fixtures/multihost_torquebox_remote.rb
88
+ - spec/fixtures/myapp.knob
89
+ - spec/fixtures/simple_torquebox_remote.rb
90
+ - spec/remote_deploy_spec.rb
91
+ - spec/remote_deploy_utils_spec.rb
92
+ - spec/spec_helper.rb
86
93
  - torquebox-remote-deployer.gemspec
87
94
  homepage: https://github.com/jkutner/torquebox-remote-deployer
88
95
  licenses: []
@@ -107,9 +114,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
114
  requirements: []
108
115
 
109
116
  rubyforge_project: torquebox-remote-deployer
110
- rubygems_version: 1.8.9
117
+ rubygems_version: 1.8.15
111
118
  signing_key:
112
119
  specification_version: 3
113
120
  summary: Deploy Knob files to a remote server with ease.
114
- test_files: []
115
-
121
+ test_files:
122
+ - spec/fixtures/local_torquebox_remote.rb
123
+ - spec/fixtures/multihost_torquebox_remote.rb
124
+ - spec/fixtures/myapp.knob
125
+ - spec/fixtures/simple_torquebox_remote.rb
126
+ - spec/remote_deploy_spec.rb
127
+ - spec/remote_deploy_utils_spec.rb
128
+ - spec/spec_helper.rb