marhan_cli 0.0.11 → 0.0.12

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/Changelog.md CHANGED
@@ -44,4 +44,9 @@
44
44
  ## v0.0.11
45
45
 
46
46
  * First RSpec tests and guard added for TDD development.
47
- * VirtualBox command for start and stop virtual guest systems added. (command => vbox:[start|stop])
47
+ * VirtualBox command for start and stop virtual guest systems added. (command => vbox:[start|stop])
48
+
49
+ ## v0.0.12
50
+
51
+ * VirtualBox command connects automaticaly to ssh server, if guest configuration has 'ssh' connection data. (command => vbox:start)
52
+ * 'VBoxManage controlvm ' uses 'acpipowerbutton' instead of 'poweroff', now. (command => vbox:stop)
data/Guardfile CHANGED
@@ -1,5 +1,5 @@
1
1
  guard 'rspec' do
2
- watch(%r{^spec/(.+)\.rb$})
3
- watch(%r{^lib/(.+)\.rb$})
2
+ watch(%r{^spec/.+}) {"spec"}
3
+ watch(%r{^lib/.+}) {"spec"}
4
4
  end
5
5
 
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module MarhanCli
4
- class TrueCryptApp
4
+ class TrueCrypt
5
5
 
6
6
  def initialize(mount_folder = nil)
7
7
  @binary = "/Applications/TrueCrypt.app/Contents/MacOS/TrueCrypt"
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require 'net/ssh'
3
+ require 'marhan_cli/helper/constraint'
4
+ require 'marhan_cli/network/remote_machine'
5
+
6
+ module MarhanCli
7
+ class VirtualBox
8
+
9
+ attr_reader :guests
10
+
11
+ def initialize(guests)
12
+ @guests = guests
13
+ end
14
+
15
+ def start_guest(guest_config_name)
16
+ "VBoxManage startvm '#{vbox_name(guest_config_name)}'"
17
+ end
18
+
19
+ def stop_guest(guest_config_name)
20
+ "VBoxManage controlvm '#{vbox_name(guest_config_name)}' acpipowerbutton"
21
+ end
22
+
23
+ def guest_ssh_server_up?(guest_config_name)
24
+ remote_machine = RemoteMachine.new("localhost", "2222")
25
+ remote_machine.ssh_server_running?("markus")
26
+ end
27
+
28
+ def vbox_name(guest_config_name)
29
+ Constraint.not_nil_or_empty! guest_config_name, "Guest config name have to be set!"
30
+ Constraint.not_nil_or_empty! @guests[guest_config_name], "No guest with key '#{guest_config_name}' found in configuration!"
31
+ Constraint.not_nil_or_empty! @guests[guest_config_name].name, "Guest name is not configured!"
32
+ @guests[guest_config_name].name
33
+ end
34
+ end
35
+ end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require "thor"
3
- require "ambience"
3
+ require 'marhan_cli/config'
4
4
 
5
5
  module MarhanCli
6
6
  class Command < Thor
@@ -8,8 +8,6 @@ module MarhanCli
8
8
 
9
9
  protected
10
10
 
11
- CONFIG_FILE = ".marhan_cli.yml"
12
-
13
11
  def execute(proc)
14
12
  begin
15
13
  say ""
@@ -29,12 +27,7 @@ module MarhanCli
29
27
  end
30
28
 
31
29
  def load_config
32
- config_file_path = File.join(File.expand_path("~/"), CONFIG_FILE)
33
- unless File.exists?(config_file_path)
34
- raise "Stop processing! Command needs the configuration file '#{CONFIG_FILE}' in you're home directory."
35
- end
36
- config = Ambience.create(config_file_path)
37
- config.to_mash
30
+ MarhanCli::Config.init
38
31
  end
39
32
 
40
33
  def get_or_ask(option_name)
@@ -3,7 +3,7 @@ require 'marhan_cli/network/remote_machine'
3
3
  require 'marhan_cli/command'
4
4
 
5
5
  module MarhanCli
6
- class Network < MarhanCli::Command
6
+ class NetworkCommand < MarhanCli::Command
7
7
 
8
8
  namespace :net
9
9
 
@@ -1,9 +1,9 @@
1
1
  # encoding: utf-8
2
2
  require 'marhan_cli/command'
3
- require 'marhan_cli/app/true_crypt_app'
3
+ require 'marhan_cli/apps/true_crypt'
4
4
 
5
5
  module MarhanCli
6
- class TrueCrypt < MarhanCli::Command
6
+ class TrueCryptCommand < MarhanCli::Command
7
7
 
8
8
  namespace :crypt
9
9
 
@@ -40,7 +40,7 @@ module MarhanCli
40
40
  def mount_proc
41
41
  Proc.new do
42
42
  config = load_crypt_config
43
- true_crypt = TrueCryptApp.new(config.mount_folder)
43
+ true_crypt = TrueCrypt.new(config.mount_folder)
44
44
  device = get_or_ask(:device)
45
45
  run true_crypt.mount_command(config.encrypted_devices[device], device)
46
46
  end
@@ -49,7 +49,7 @@ module MarhanCli
49
49
  def unmount_proc
50
50
  Proc.new do
51
51
  config = load_crypt_config
52
- true_crypt = TrueCryptApp.new(config.mount_folder)
52
+ true_crypt = TrueCrypt.new(config.mount_folder)
53
53
  device = get_or_ask(:device)
54
54
  run true_crypt.unmount_command(device)
55
55
  end
@@ -57,7 +57,7 @@ module MarhanCli
57
57
 
58
58
  def unmount_all_proc
59
59
  Proc.new do
60
- true_crypt = TrueCryptApp.new
60
+ true_crypt = TrueCrypt.new
61
61
  run true_crypt.unmount_all_command
62
62
  end
63
63
  end
@@ -1,9 +1,9 @@
1
1
  # encoding: utf-8
2
2
  require 'marhan_cli/command'
3
- require 'marhan_cli/app/virtual_box_app'
3
+ require 'marhan_cli/apps/virtual_box'
4
4
 
5
5
  module MarhanCli
6
- class VBox < MarhanCli::Command
6
+ class VBoxCommand < MarhanCli::Command
7
7
 
8
8
  namespace :vbox
9
9
 
@@ -31,21 +31,35 @@ module MarhanCli
31
31
 
32
32
  private
33
33
 
34
+ def start_ssh_client
35
+
36
+ end
37
+
34
38
  def start_guest
35
39
  Proc.new do
36
- config = load_vbox_config
37
- virtual_box = VirtualBoxApp.new(config.guests)
40
+ vbox_config = load_vbox_config
41
+ virtual_box = VirtualBox.new(vbox_config.guests)
38
42
  guest_to_start = get_or_ask(:guest)
39
43
  run virtual_box.start_guest(guest_to_start)
44
+
45
+ if vbox_config.guests[guest_to_start].key?(:ssh)
46
+ say ""
47
+ say "Waiting for response from SSH server...", :green
48
+ if virtual_box.guest_ssh_server_up?(guest_to_start)
49
+ say "SSH server is up. Trying to connect...", :green
50
+ say ""
51
+ run "ssh -p 2222 localhost"
52
+ end
53
+ end
40
54
  end
41
55
  end
42
56
 
43
57
  def stop_guest
44
58
  Proc.new do
45
59
  config = load_vbox_config
46
- virtual_box = VirtualBoxApp.new(config.guests)
47
- guest_to_start = get_or_ask(:guest)
48
- run virtual_box.stop_guest(guest_to_start)
60
+ guest_name = get_or_ask(:guest)
61
+ virtual_box = VirtualBox.new(config.guests)
62
+ run virtual_box.stop_guest(guest_name)
49
63
  end
50
64
  end
51
65
 
@@ -3,7 +3,7 @@ require 'net/http'
3
3
  require 'marhan_cli/command'
4
4
 
5
5
  module MarhanCli
6
- class Web < MarhanCli::Command
6
+ class WebCommand < MarhanCli::Command
7
7
 
8
8
  namespace :web
9
9
 
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require "ambience"
3
+
4
+ module MarhanCli
5
+ class Config
6
+
7
+ def self.init(config_file = nil)
8
+ @config_file = config_file || default_config_file
9
+ load_config(@config_file)
10
+ end
11
+
12
+ def self.load_config(config_file)
13
+ unless File.exists?(config_file)
14
+ raise "Stop processing! Command needs the configuration file '#{config_file}' in you're home directory."
15
+ end
16
+ begin
17
+ Ambience.create(config_file).to_mash
18
+ rescue Exception => e
19
+ raise "Configuration file could not pared: #{e}"
20
+ end
21
+
22
+ end
23
+
24
+ def self.default_config_file
25
+ File.join(File.expand_path("~/"), ".marhan_cli.yml")
26
+ end
27
+
28
+ end
29
+ end
@@ -26,7 +26,40 @@ module MarhanCli
26
26
  end
27
27
 
28
28
  raise RemoteMachineError, "#{stderr}" unless stderr.empty?
29
+ end
30
+ end
31
+
32
+ def wait_for_ssh_server_status(user)
33
+ Net::SSH.start(@home, user, :port => @port) do |ssh|
34
+ status = "";
35
+ channel = ssh.open_channel do |ch|
36
+ ch.exec "status ssh" do |ch, success|
37
+ raise "could not execute command" unless success
38
+ ch.on_data do |c, data|
39
+ status << data
40
+ end
41
+ ch.on_extended_data do |c, type, data|
42
+ $STDERR.print data
43
+ end
44
+ end
45
+ end
46
+ channel.wait
47
+ return status
48
+ end
49
+ end
29
50
 
51
+ def ssh_server_running?(user)
52
+ status = wait_for_ssh_server_status(user)
53
+ status.match(%r{ssh start/running.+})
54
+ end
55
+
56
+ def exec_remote_command(user, remote_command)
57
+ Net::SSH.start(@host, user, :port => @port) do |ssh|
58
+ stderr = ""
59
+ ssh.exec!(remote_command) do |channel, stream, data|
60
+ stderr << data if stream == :stderr
61
+ end
62
+ raise RemoteMachineError, "#{stderr}" unless stderr.empty?
30
63
  end
31
64
  end
32
65
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module MarhanCli
3
- VERSION = "0.0.11"
3
+ VERSION = "0.0.12"
4
4
  end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'marhan_cli/apps/virtual_box'
4
+
5
+ describe "VirtualBoxApp" do
6
+
7
+ let(:config_param) { Hashie::Mash.new(:linux => 'Ubuntu Linux') }
8
+ let(:subject) { MarhanCli::VirtualBox.new(config_param) }
9
+
10
+ context ".guest_ssh_server_up?" do
11
+ describe "when system is running" do
12
+ it "returns true" do
13
+ subject.guest_ssh_server_up?("linux").should be_true
14
+ end
15
+ end
16
+ end
17
+
18
+ context ".shutdown_guest_system" do
19
+ describe "when system is running" do
20
+ it "system shutdown" do
21
+ subject.shutdown_guest_system("linux").should be_true
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ crypt:
2
+ mount_folder: /Volumes
3
+ encrypted_devices:
4
+ hdd: /dev/rdisk2s2
5
+ vbox:
6
+ guests:
7
+ linux:
8
+ name: Ubuntu Linux
9
+ ssh:
10
+ user: markus
11
+ host: localhost
12
+ port: 2222
13
+ windows:
14
+ name: Windows XP
@@ -1,22 +1,23 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
- require 'marhan_cli/app/virtual_box_app'
3
+ require 'marhan_cli/apps/virtual_box'
4
+ require 'marhan_cli/config'
4
5
 
5
6
  describe "VirtualBoxApp" do
6
7
 
7
- let(:config_param) { Hashie::Mash.new(:linux => 'Ubuntu Linux') }
8
+ let(:config_param) { MarhanCli::Config.init(
9
+ File.join("spec", "unit", "fixtures", "config.yml")).vbox.guests }
10
+
11
+ let(:subject) { MarhanCli::VirtualBox.new(config_param) }
8
12
 
9
13
  context ".new" do
10
14
  describe "with empty hash as argument" do
11
- let(:subject) { MarhanCli::VirtualBoxApp.new({}) }
12
15
  it "creates a new objects" do
13
- subject.should be_a(MarhanCli::VirtualBoxApp)
16
+ subject.should be_a(MarhanCli::VirtualBox)
14
17
  end
15
18
  end
16
19
 
17
20
  describe "with config hash as argument" do
18
- let(:subject) { MarhanCli::VirtualBoxApp.new(config_param) }
19
-
20
21
  it "has set config hash" do
21
22
  subject.guests.should eq(config_param)
22
23
  end
@@ -24,8 +25,6 @@ describe "VirtualBoxApp" do
24
25
  end
25
26
 
26
27
  context ".start_guest" do
27
- let(:subject) { MarhanCli::VirtualBoxApp.new(config_param) }
28
-
29
28
  describe "with configured guest as argument" do
30
29
  it "returns correct command" do
31
30
  subject.start_guest('linux').should eq("VBoxManage startvm 'Ubuntu Linux'")
@@ -35,8 +34,6 @@ describe "VirtualBoxApp" do
35
34
  end
36
35
 
37
36
  context ".vbox_name" do
38
- let(:subject) { MarhanCli::VirtualBoxApp.new(config_param) }
39
-
40
37
  describe "with configured guest as argument" do
41
38
  it "returns correct command" do
42
39
  subject.vbox_name('linux').should eq('Ubuntu Linux')
@@ -49,7 +46,7 @@ describe "VirtualBoxApp" do
49
46
  subject.vbox_name('minix')
50
47
  }.to raise_error(
51
48
  error=ArgumentError,
52
- message="No guest with key 'minix' found in configuration")
49
+ message="No guest with key 'minix' found in configuration!")
53
50
  end
54
51
  end
55
52
 
@@ -65,14 +62,10 @@ describe "VirtualBoxApp" do
65
62
  end
66
63
 
67
64
  context ".stop_guest" do
68
-
69
- let(:subject) { MarhanCli::VirtualBoxApp.new(config_param) }
70
-
71
65
  describe "with configured guest as argument" do
72
66
  it "returns correct command" do
73
- subject.stop_guest('linux').should eq("VBoxManage controlvm 'Ubuntu Linux' poweroff")
67
+ subject.stop_guest('linux').should eq("VBoxManage controlvm 'Ubuntu Linux' acpipowerbutton")
74
68
  end
75
69
  end
76
70
  end
77
-
78
71
  end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'marhan_cli/config'
4
+
5
+ describe "Config" do
6
+
7
+ context "self.init" do
8
+ let(:subject) { MarhanCli::Config.init(
9
+ File.join("spec", "unit", "fixtures", "config.yml")) }
10
+
11
+ describe "returns config" do
12
+ it "as Hashie::Mash" do
13
+ subject.should be_a(Hashie::Mash)
14
+ end
15
+ end
16
+
17
+ describe "returns TrueCrypt config" do
18
+
19
+ let(:crypt) { subject.crypt }
20
+
21
+ it "with with mount_folder" do
22
+ crypt.mount_folder.should eq("/Volumes")
23
+ end
24
+
25
+ it "with with encrypted_devices" do
26
+ crypt.encrypted_devices.hdd.should eq("/dev/rdisk2s2")
27
+ end
28
+ end
29
+
30
+ describe "returns VirtualBox config" do
31
+ let(:vbox) { subject.vbox }
32
+ it "with 'Ubuntu' for guests.linux.name" do
33
+ vbox.guests.linux.name.should eq("Ubuntu Linux")
34
+ end
35
+
36
+ it "with 'Ubuntu' for guests['linux'].name" do
37
+ vbox.guests['linux'].name.should eq("Ubuntu Linux")
38
+ end
39
+
40
+ it "with 'localhost' for guests.linux.ssh.host" do
41
+ vbox.guests.linux.ssh.host.should eq("localhost")
42
+ end
43
+
44
+ it "with 'markus' for guests.linux.ssh.user" do
45
+ vbox.guests.linux.ssh.user.should eq("markus")
46
+ end
47
+
48
+ it "with 'nil' for guests.windows.ssh.nil?" do
49
+ vbox.guests.windows.key?(:ssh).should be_false
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marhan_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
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-12-15 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
@@ -141,21 +141,25 @@ files:
141
141
  - Rakefile
142
142
  - bin/mcli
143
143
  - lib/marhan_cli.rb
144
- - lib/marhan_cli/app/true_crypt_app.rb
145
- - lib/marhan_cli/app/virtual_box_app.rb
144
+ - lib/marhan_cli/apps/true_crypt.rb
145
+ - lib/marhan_cli/apps/virtual_box.rb
146
146
  - lib/marhan_cli/command.rb
147
- - lib/marhan_cli/commands/network.rb
148
- - lib/marhan_cli/commands/true_crypt.rb
149
- - lib/marhan_cli/commands/vbox.rb
150
- - lib/marhan_cli/commands/web.rb
147
+ - lib/marhan_cli/commands/network_command.rb
148
+ - lib/marhan_cli/commands/true_crypt_command.rb
149
+ - lib/marhan_cli/commands/vbox_command.rb
150
+ - lib/marhan_cli/commands/web_command.rb
151
+ - lib/marhan_cli/config.rb
151
152
  - lib/marhan_cli/helper/constraint.rb
152
153
  - lib/marhan_cli/network/errors.rb
153
154
  - lib/marhan_cli/network/remote_machine.rb
154
155
  - lib/marhan_cli/version.rb
155
156
  - marhan_cli.gemspec
156
- - spec/marhan_cli/app/virtual_box_app_spec.rb
157
- - spec/marhan_cli/helper/constraint_spec.rb
157
+ - spec/integration/marhan_cli/apps/virtual_box_scene.rb
158
158
  - spec/spec_helper.rb
159
+ - spec/unit/fixtures/config.yml
160
+ - spec/unit/lib/marhan_cli/apps/virtual_box_spec.rb
161
+ - spec/unit/lib/marhan_cli/config_spec.rb
162
+ - spec/unit/lib/marhan_cli/helper/constraint_spec.rb
159
163
  homepage: https://github.com/marhan/marhan_cli
160
164
  licenses: []
161
165
  post_install_message:
@@ -181,6 +185,9 @@ signing_key:
181
185
  specification_version: 3
182
186
  summary: Helper routines for my computers
183
187
  test_files:
184
- - spec/marhan_cli/app/virtual_box_app_spec.rb
185
- - spec/marhan_cli/helper/constraint_spec.rb
188
+ - spec/integration/marhan_cli/apps/virtual_box_scene.rb
186
189
  - spec/spec_helper.rb
190
+ - spec/unit/fixtures/config.yml
191
+ - spec/unit/lib/marhan_cli/apps/virtual_box_spec.rb
192
+ - spec/unit/lib/marhan_cli/config_spec.rb
193
+ - spec/unit/lib/marhan_cli/helper/constraint_spec.rb
@@ -1,28 +0,0 @@
1
- # encoding: utf-8
2
- require 'marhan_cli/helper/constraint'
3
-
4
- module MarhanCli
5
- class VirtualBoxApp
6
-
7
- attr_reader :guests
8
-
9
- def initialize(guests)
10
- @guests = guests
11
- end
12
-
13
- def start_guest(guest_config_name)
14
- "VBoxManage startvm '#{vbox_name(guest_config_name)}'"
15
- end
16
-
17
- def stop_guest(guest_config_name)
18
- "VBoxManage controlvm '#{vbox_name(guest_config_name)}' poweroff"
19
- end
20
-
21
- def vbox_name(guest_config_name)
22
- Constraint.not_nil_or_empty! guest_config_name, "Guest config name have to be set!"
23
- vbox_name = @guests[guest_config_name]
24
- Constraint.not_nil_or_empty! vbox_name, "No guest with key '#{guest_config_name}' found in configuration"
25
- vbox_name
26
- end
27
- end
28
- end