battlestation 0.0.2 → 0.0.3

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
@@ -3,4 +3,3 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  tmp
6
- Battlestation
data/README.md CHANGED
@@ -4,3 +4,64 @@
4
4
 
5
5
  Battle Station allows you to get your development environment up-and-running *fast*. This is still a work in progress.
6
6
 
7
+ ## Installation
8
+
9
+ Install is as a gem
10
+
11
+ gem install battlestation
12
+
13
+ It's a good idea to add it to the `:development` group in your `Gemfile` as well, but the idea is that you can run
14
+ battlestation right after cloning your repository.
15
+
16
+ Also, battlestation could take care of running `bundle install` for you.
17
+
18
+ ## Usage
19
+
20
+ In the root of your project create a file named `Battlestation`. This file defines your Battlestation plan.
21
+
22
+ You can create tasks or dependencies that will be executed sequencially. Each task is composed of one ore more operations.
23
+
24
+ Operations include:
25
+
26
+ * Checking for an executable file in your path
27
+ * Check if a process is running
28
+ * Run system commands
29
+
30
+ These are three very basic building blocks, but together they allow you to plan your complete development environment. (We'll be happy to add more, but these'll get you started.)
31
+
32
+ See [this example](https://github.com/ariejan/battlestation/blob/master/examples/Battlestation) for details.
33
+
34
+ ## Bugs / Feature Requests
35
+
36
+ Please post them to [Github Issues](https://github.com/ariejan/battlestation/issues).
37
+
38
+ ## Contributing
39
+
40
+ Feel free to help out. Fork the project, write your specs and code and create a pull request.
41
+
42
+ ## Contributors
43
+
44
+ * Ariejan de Vroom <ariejan> - original author
45
+
46
+ ## License
47
+
48
+ Copyright (c) 2012 Ariejan de Vroom
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining
51
+ a copy of this software and associated documentation files (the
52
+ "Software"), to deal in the Software without restriction, including
53
+ without limitation the rights to use, copy, modify, merge, publish,
54
+ distribute, sublicense, and/or sell copies of the Software, and to
55
+ permit persons to whom the Software is furnished to do so, subject to
56
+ the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be
59
+ included in all copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
64
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
65
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
66
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
67
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ Battlestation.plan do
2
+
3
+ dependency :redis do
4
+ executable 'redis-server'
5
+ process 'redis-server', resolution: "Install redis with `brew install redis` or `apt-get install redis`"
6
+ end
7
+
8
+ dependency :postgresql do
9
+ process 'postgres'
10
+ end
11
+
12
+ task :bundler do
13
+ run 'bundle install'
14
+ end
15
+
16
+ task :database do
17
+ file_exists 'config/database.yml', resolution: "Copy config/database.yml.example to config/databse.yml"
18
+ run 'rake db:create'
19
+ run 'rake db:migrate'
20
+ end
21
+
22
+ task :documentation do
23
+ run 'rm -rf .yardoc'
24
+ run 'yard'
25
+ end
26
+ end
@@ -2,23 +2,37 @@ require 'thor'
2
2
 
3
3
  require "battlestation/version"
4
4
  require 'battlestation/plan'
5
- require 'battlestation/dependency'
5
+ require 'battlestation/task'
6
+ require 'battlestation/operations'
6
7
  require 'battlestation/ui'
7
8
  require 'battlestation/cli'
8
9
 
9
10
  module Battlestation
10
-
11
11
  class << self
12
12
  attr_writer :ui
13
13
 
14
- # Plans a new battlestation
14
+ # Load the Battlestation or Battlestation.rb file
15
+ def load(filename)
16
+ plan = eval(File.read(filename))
17
+ return plan
18
+ rescue SyntaxError, NameError
19
+ Battlestation.ui.error("Your Battlestation has syntax errors.")
20
+ return nil
21
+ rescue
22
+ Battlestation.ui.error("Some error occured, but we're not sure what happened.")
23
+ return nil
24
+ end
25
+
26
+ # Plan a new battlestation
27
+ #
28
+ # See README for details
15
29
  def plan(opts = {}, &block)
16
30
  return Plan.new opts, &block
17
31
  end
18
32
 
33
+ # UI output
19
34
  def ui
20
35
  @ui ||= UI.new
21
36
  end
22
37
  end
23
-
24
38
  end
@@ -7,6 +7,9 @@ module Battlestation
7
7
 
8
8
  the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell)
9
9
  Battlestation.ui = UI::Shell.new(the_shell)
10
+
11
+ Battlestation.ui.info("Manning battlestation...")
12
+ Battlestation.ui.info("")
10
13
  end
11
14
 
12
15
  check_unknown_options!
@@ -24,16 +27,31 @@ module Battlestation
24
27
  in order to run this project.
25
28
  D
26
29
  def check
27
- if !File.exists?("Battlestation")
30
+ plan = nil
31
+ filenames = ["Battlestation", "Battlestation.rb"].select { |f| File.exists?(f) }
32
+
33
+ case filenames.size
34
+ when 0
28
35
  Battlestation.ui.error "Could not read your Battlestation file"
29
36
  exit 1
37
+ when 1
38
+ plan = Battlestation.load(filenames.first)
39
+ exit 1 if plan.nil?
40
+ else # >1
41
+ Battlestation.ui.error "You cannot have both Battlestation and Battlestation.rb files. Choose one, remove the other."
42
+ exit 1
30
43
  end
31
44
 
32
- # Parse/evaluate Battlestation
33
- plan = eval(File.read("Battlestation"))
45
+ # Execute tasks in order
46
+ plan.tasks.each_pair do |name, task|
47
+ Battlestation.ui.group task.title do
48
+ # Show a notice for empty tasks
49
+ if task.operations.empty?
50
+ Battlestation.ui.notice("Nothing defined for #{task.name}")
51
+ end
34
52
 
35
- plan.execute.each do |result|
36
- Battlestation.ui.info result
53
+ task.execute
54
+ end
37
55
  end
38
56
  end
39
57
  end
@@ -0,0 +1,14 @@
1
+ module Battlestation
2
+ module Operations
3
+ class Base
4
+ attr_accessor :name, :type, :opts
5
+
6
+ def execute
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ Dir["#{File.expand_path('../operations', __FILE__)}/*.rb"].each do |file|
13
+ require file
14
+ end
@@ -0,0 +1,29 @@
1
+ module Battlestation
2
+ module Operations
3
+ class ExecutableCheck < Base
4
+ attr_accessor :filename
5
+
6
+ def initialize(filename, opts = {})
7
+ @filename = filename
8
+ @type = :executable
9
+ @name = "executable-#{filename}"
10
+ @opts = opts
11
+ end
12
+
13
+ def status
14
+ if executable_exists?
15
+ { status: :okay, msg: "#{filename} executable found", name: name }
16
+ else
17
+ { status: :fail, msg: "#{filename} executable not found", resolution: opts[:resolution], name: name }
18
+ end
19
+ end
20
+
21
+ private
22
+ def executable_exists?
23
+ files = %x(/usr/bin/env which #{filename}).split
24
+ return files.any? && File.executable?(files.first)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,27 @@
1
+ module Battlestation
2
+ module Operations
3
+ class FileExistsCheck < Base
4
+ attr_accessor :filename
5
+
6
+ def initialize(filename, opts = {})
7
+ @filename = filename
8
+ @type = :file
9
+ @name = "file-#{filename}"
10
+ @opts = opts
11
+ end
12
+
13
+ def status
14
+ if file_exists?
15
+ { status: :okay, msg: "#{filename} found", name: name }
16
+ else
17
+ { status: :fail, msg: "#{filename} not found", resolution: opts[:resolution], name: name }
18
+ end
19
+ end
20
+
21
+ private
22
+ def file_exists?
23
+ File.exists?(filename)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module Battlestation
2
+ module Operations
3
+ class ProcessCheck < Base
4
+ attr_accessor :identifier
5
+
6
+ def initialize(identifier, opts = {})
7
+ @identifier = identifier
8
+ @type = :process
9
+ @name = "process-#{identifier}"
10
+ @opts = opts
11
+ end
12
+
13
+ def status
14
+ if process_running?
15
+ { status: :okay, msg: "#{identifier} up and running", name: name }
16
+ else
17
+ { status: opts[:force_failure] ? :fail : :warn, msg: "#{identifier} not running", resolution: opts[:resolution], name: name }
18
+ end
19
+ end
20
+
21
+ private
22
+ def process_running?
23
+ processes = %x(ps aux | grep -i #{identifier} | grep -v grep).split
24
+ return processes.any?
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ module Battlestation
2
+ module Operations
3
+ class Runner < Base
4
+ attr_accessor :cmd
5
+
6
+ def initialize(cmd, opts = {})
7
+ @cmd = cmd
8
+ @type = :runner
9
+ @name = "runner-#{cmd.split(" ").first}"
10
+ @opts = opts
11
+ end
12
+
13
+ def status
14
+ Battlestation.ui.notice "Executing `#{cmd}`"
15
+
16
+ if system("#{cmd} &> /dev/null")
17
+ { status: :okay, msg: "`#{cmd}` executed successfully", name: name }
18
+ else
19
+ { status: :fail, msg: "`#{cmd}` failed to execute", name: name }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,21 +1,27 @@
1
1
  module Battlestation
2
+ # This represents a complete Battlestation Plan,
3
+ # it may include checks and tasks.
2
4
  class Plan
3
- attr_accessor :dependencies
5
+ # Tasks to perform
6
+ attr_accessor :tasks
4
7
 
8
+ # Create a new plan.
9
+ #
10
+ # @example
11
+ # Battlestation.plan do
12
+ # dependency :mysql do
13
+ # executable 'mysql'
14
+ # end
15
+ # end
5
16
  def initialize(opts = {}, &block)
6
- @dependencies = {}
7
-
17
+ @tasks = {}
8
18
  instance_eval &block if block_given?
9
19
  end
10
20
 
11
- def dependency(name, &block)
12
- dependencies[name] = Battlestation::Dependency.new(name, &block)
13
- end
14
-
15
- def execute
16
- results = []
17
- dependencies.each_pair { |name, dep| results << dep.execute }
18
- results.flatten
21
+ # Adds a new task as a dependency.
22
+ def task(name, &block)
23
+ tasks[name] = Battlestation::Task.new(name, &block)
19
24
  end
25
+ alias_method :dependency, :task
20
26
  end
21
27
  end
@@ -0,0 +1,61 @@
1
+ module Battlestation
2
+ class Task
3
+ attr_accessor :name
4
+ attr_accessor :operations
5
+
6
+ def initialize(name, &block)
7
+ @name = name
8
+ @operations = []
9
+
10
+ instance_eval &block if block_given?
11
+ end
12
+
13
+ def title
14
+ "Manning #{name}"
15
+ end
16
+
17
+ # Check if an executable exists
18
+ def executable(filename, opts = {})
19
+ operations << Battlestation::Operations::ExecutableCheck.new(filename, opts)
20
+ end
21
+
22
+ # Check if a file exists
23
+ def file_exists(filename, opts = {})
24
+ operations << Battlestation::Operations::FileExistsCheck.new(filename, opts)
25
+ end
26
+
27
+ # Check if a process is running
28
+ def process(identifier, opts = {})
29
+ operations << Battlestation::Operations::ProcessCheck.new(identifier, opts)
30
+ end
31
+
32
+ # Run the specified command, fail if unsucessful
33
+ def run(cmd, opts = {})
34
+ operations << Battlestation::Operations::Runner.new(cmd, opts)
35
+ end
36
+
37
+ # Execute this dependency
38
+ def execute
39
+ Battlestation.ui.notice("Nothing defined for #{task.name}") and return if operations.empty?
40
+
41
+ statuses = []
42
+ # Report successes and failures
43
+ operations.each do |operation|
44
+ status = operation.status
45
+ statuses << status
46
+
47
+ Battlestation.ui.send(status[:status], status[:msg])
48
+
49
+ if status[:resolution]
50
+ Battlestation.ui.todo(">> " + status[:resolution])
51
+ end
52
+
53
+ # Exit if this task failed. No point continuing
54
+ if status[:status] == :fail
55
+ Battlestation.ui.fail(">> Please resolve the failures above and re-run `battlestation`")
56
+ exit 1
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -2,9 +2,19 @@ require 'rubygems/user_interaction'
2
2
 
3
3
  module Battlestation
4
4
  class UI
5
+
6
+ def fail(message, newline = nil)
7
+ end
8
+
9
+ def okay(message, newline = nil)
10
+ end
11
+
5
12
  def warn(message, newline = nil)
6
13
  end
7
14
 
15
+ def notice(message, newline = nil)
16
+ end
17
+
8
18
  def debug(message, newline = nil)
9
19
  end
10
20
 
@@ -17,6 +27,12 @@ module Battlestation
17
27
  def confirm(message, newline = nil)
18
28
  end
19
29
 
30
+ def todo(message, newline = nil)
31
+ end
32
+
33
+ def group(title, &block)
34
+ end
35
+
20
36
  class Shell < UI
21
37
  attr_writer :shell
22
38
 
@@ -25,8 +41,31 @@ module Battlestation
25
41
  @quiet = false
26
42
  end
27
43
 
44
+ def group(title, &block)
45
+ tell_me("> #{title}")
46
+ tell_me("\n")
47
+ yield
48
+ tell_me("\n")
49
+ end
50
+
51
+ def todo(message, newline = nil)
52
+ tell_me(" [TODO] #{message}", :cyan, newline)
53
+ end
54
+
55
+ def fail(message, newline = nil)
56
+ tell_me(" [FAIL] #{message}", :red, newline)
57
+ end
58
+
59
+ def notice(message, newline = nil)
60
+ tell_me(" [INFO] #{message}", :blue, newline)
61
+ end
62
+
63
+ def okay(message, newline = nil)
64
+ tell_me(" [OKAY] #{message}", :green, newline)
65
+ end
66
+
28
67
  def warn(message, newline = nil)
29
- tell_me(message, :yellow, newline)
68
+ tell_me(" [WARN] #{message}", :yellow, newline)
30
69
  end
31
70
 
32
71
  def debug(message, newline = nil)
@@ -1,3 +1,3 @@
1
1
  module Battlestation
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -5,4 +5,25 @@ describe Battlestation do
5
5
  plan = Battlestation::plan {}
6
6
  plan.should be_a(Battlestation::Plan)
7
7
  end
8
+
9
+ context "loading a Battlestation file" do
10
+ before { remove_battlefiles }
11
+
12
+ it "loads a Battlestation file" do
13
+ filename = battlefile <<-B
14
+ Battlestation.plan do
15
+ end
16
+ B
17
+
18
+ Battlestation.load(filename).should be_a(Battlestation::Plan)
19
+ end
20
+
21
+ it "returns nil on error" do
22
+ filename = battlefile <<-B
23
+ Mumbo Jumbo Syntax Dumbo.
24
+ B
25
+
26
+ Battlestation.load(filename).should be_nil
27
+ end
28
+ end
8
29
  end
@@ -1,18 +1,66 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe Battlestation::CLI do
4
- context "without a valid Battlestation file" do
4
+ context "Battlestation files" do
5
5
  before do
6
6
  remove_battlefile
7
7
  end
8
8
 
9
- it "write an error" do
10
- battlestation :check
11
- out.should eql("Could not read your Battlestation file")
9
+ it "uses a Battlestation file" do
10
+ battlefile <<-B
11
+ Battlestation.plan do
12
+ end
13
+ B
14
+ battlestation(:check)
15
+
16
+ out.should_not =~ /Could not read your Battlestation file/
12
17
  end
13
18
 
14
- it "exits with status 1" do
15
- battlestation(:check, exitstatus: true).should == 1
19
+ it "checks for a Battlestation.rb file" do
20
+ battlefilerb <<-B
21
+ Battlestation.plan do
22
+ end
23
+ B
24
+ battlestation(:check)
25
+
26
+ out.should_not =~ /Could not read your Battlestation file/
27
+ end
28
+
29
+ it "complains when both Battlestation and Battlestation.rb exist" do
30
+ battlefile <<-B
31
+ Battlestation.plan do
32
+ end
33
+ B
34
+
35
+ battlefilerb <<-B
36
+ Battlestation.plan do
37
+ end
38
+ B
39
+
40
+ battlestation(:check)
41
+
42
+ out.should =~ /You cannot have both Battlestation and Battlestation.rb files. Choose one, remove the other./
43
+ end
44
+
45
+ context "without a valid Battlestation file" do
46
+ it "write an error" do
47
+ battlestation :check
48
+ out.should =~ /Could not read your Battlestation file/
49
+ end
50
+
51
+ it "exits with status 1" do
52
+ battlestation(:check, exitstatus: true).should == 1
53
+ end
54
+ end
55
+
56
+ context "with a Battlestation file with syntax errors" do
57
+ it "reports syntax errors in your Battlestation" do
58
+ check_battlefile <<-B
59
+ Syntactic Mumbo Jumbo
60
+ B
61
+
62
+ out.should =~ /Your Battlestation has syntax errors/
63
+ end
16
64
  end
17
65
  end
18
66
 
@@ -26,7 +74,7 @@ describe Battlestation::CLI do
26
74
  end
27
75
  B
28
76
 
29
- out.should =~ /ls found/i
77
+ out.should =~ / \[OKAY\] ls executable found/i
30
78
  end
31
79
 
32
80
  it "reports an error" do
@@ -38,7 +86,31 @@ describe Battlestation::CLI do
38
86
  end
39
87
  B
40
88
 
41
- out.should =~ /unknown-executable-name not found/i
89
+ out.should =~ / \[FAIL\] unknown-executable-name executable not found/i
90
+ end
91
+
92
+ it "exists with error status 1 on failure" do
93
+ battlefile <<-B
94
+ Battlestation.plan do
95
+ dependency :some_dep do
96
+ executable 'unknown-executable-name'
97
+ end
98
+ end
99
+ B
100
+
101
+ battlestation(:check, exitstatus: true).should == 1
102
+ end
103
+
104
+ it "reports resolutions on failure" do
105
+ check_battlefile <<-B
106
+ Battlestation.plan do
107
+ dependency :some_dep do
108
+ executable 'unknown-executable-name', resolution: "Install some app"
109
+ end
110
+ end
111
+ B
112
+
113
+ out.should =~ /Install some app/i
42
114
  end
43
115
  end
44
116
  end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Battlestation::Operations::ExecutableCheck do
4
+ context "with a valid executable" do
5
+ subject { Battlestation::Operations::ExecutableCheck.new('ls') }
6
+
7
+ it { subject.status.should include(status: :okay) }
8
+ it { subject.status.should include(msg: "ls executable found") }
9
+ it { subject.status.should include(name: "executable-ls") }
10
+ end
11
+
12
+ context "with an invalid executable" do
13
+ subject { Battlestation::Operations::ExecutableCheck.new('does-not-exist') }
14
+
15
+ it { subject.status.should include(status: :fail) }
16
+ it { subject.status.should include(msg: "does-not-exist executable not found") }
17
+ it { subject.status.should include(name: "executable-does-not-exist") }
18
+ end
19
+
20
+ context "with an invalid executable and resolution" do
21
+ subject { Battlestation::Operations::ExecutableCheck.new('does-not-exist', resolution: "install some package") }
22
+
23
+ it { subject.status.should include(status: :fail) }
24
+ it { subject.status.should include(msg: "does-not-exist executable not found") }
25
+ it { subject.status.should include(name: "executable-does-not-exist") }
26
+ it { subject.status.should include(resolution: "install some package") }
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Battlestation::Operations::FileExistsCheck do
4
+ context "with a file that exists" do
5
+ subject { Battlestation::Operations::FileExistsCheck.new('battlestation.gemspec') }
6
+
7
+ before { subject.stub(file_exists?: true) }
8
+
9
+ it { subject.status.should include(status: :okay) }
10
+ it { subject.status.should include(msg: "battlestation.gemspec found") }
11
+ it { subject.status.should include(name: "file-battlestation.gemspec") }
12
+ end
13
+
14
+ context "with an invalid executable" do
15
+ subject { Battlestation::Operations::FileExistsCheck.new('does-not-exist') }
16
+
17
+ it { subject.status.should include(status: :fail) }
18
+ it { subject.status.should include(msg: "does-not-exist not found") }
19
+ it { subject.status.should include(name: "file-does-not-exist") }
20
+ end
21
+
22
+ context "with an invalid executable and resolution" do
23
+ subject { Battlestation::Operations::FileExistsCheck.new('does-not-exist', resolution: "copy file") }
24
+
25
+ it { subject.status.should include(status: :fail) }
26
+ it { subject.status.should include(msg: "does-not-exist not found") }
27
+ it { subject.status.should include(name: "file-does-not-exist") }
28
+ it { subject.status.should include(resolution: "copy file") }
29
+ end
30
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Battlestation::Operations::ProcessCheck do
4
+ context "with a valid process" do
5
+ subject { Battlestation::Operations::ProcessCheck.new('ruby') }
6
+
7
+ it { subject.status.should include(status: :okay) }
8
+ it { subject.status.should include(msg: "ruby up and running") }
9
+ it { subject.status.should include(name: "process-ruby") }
10
+ end
11
+
12
+ context "with an invalid process" do
13
+ subject { Battlestation::Operations::ProcessCheck.new('does-not-exist') }
14
+
15
+ it { subject.status.should include(status: :warn) }
16
+ it { subject.status.should include(msg: "does-not-exist not running") }
17
+ it { subject.status.should include(name: "process-does-not-exist") }
18
+ end
19
+
20
+ context "with an invalid process and forced failure" do
21
+ subject { Battlestation::Operations::ProcessCheck.new('does-not-exist', force_failure: true) }
22
+
23
+ it { subject.status.should include(status: :fail) }
24
+ it { subject.status.should include(msg: "does-not-exist not running") }
25
+ it { subject.status.should include(name: "process-does-not-exist") }
26
+ end
27
+
28
+ context "with an invalid process and resolution" do
29
+ subject { Battlestation::Operations::ProcessCheck.new('does-not-exist', resolution: "install some package") }
30
+
31
+ it { subject.status.should include(status: :warn) }
32
+ it { subject.status.should include(msg: "does-not-exist not running") }
33
+ it { subject.status.should include(name: "process-does-not-exist") }
34
+ it { subject.status.should include(resolution: "install some package") }
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Battlestation::Operations::Runner do
4
+ context "with a successfully ran command" do
5
+ subject { Battlestation::Operations::Runner.new('ls') }
6
+
7
+ before { subject.stub(:system).and_return(true) }
8
+
9
+ it { subject.status.should include(status: :okay) }
10
+ it { subject.status.should include(msg: "`ls` executed successfully") }
11
+ it { subject.status.should include(name: "runner-ls") }
12
+ end
13
+
14
+ context "with an invalid executable" do
15
+ subject { Battlestation::Operations::Runner.new('does-not-exist') }
16
+
17
+ before { subject.stub(:system).and_return(false) }
18
+
19
+ it { subject.status.should include(status: :fail) }
20
+ it { subject.status.should include(msg: "`does-not-exist` failed to execute") }
21
+ it { subject.status.should include(name: "runner-does-not-exist") }
22
+ end
23
+ end
@@ -5,14 +5,8 @@ describe Battlestation::Plan do
5
5
  subject { Battlestation.plan { dependency :postgres } }
6
6
 
7
7
  it "are added" do
8
- subject.dependencies.should be_an(Hash)
9
- subject.dependencies[:postgres].should be_a(Battlestation::Dependency)
10
- end
11
-
12
- it "are executed" do
13
- subject.dependencies[:postgres].should_receive(:execute).once
14
- subject.execute
8
+ subject.tasks.should be_an(Hash)
9
+ subject.tasks[:postgres].should be_a(Battlestation::Task)
15
10
  end
16
11
  end
17
12
  end
18
-
@@ -38,8 +38,8 @@ module Spec
38
38
  yield @in_p if block_given?
39
39
  @in_p.close
40
40
 
41
- @out = @out_p.read_available_bytes.strip
42
- @err = @err_p.read_available_bytes.strip
41
+ @out = @out_p.read_available_bytes #.strip
42
+ @err = @err_p.read_available_bytes #.strip
43
43
  end
44
44
 
45
45
  puts @err unless expect_err || @err.empty?
@@ -56,14 +56,21 @@ module Spec
56
56
  def remove_battlefile
57
57
  path = app("Battlestation")
58
58
  FileUtils.rm_rf(path)
59
+ path = app("Battlestation.rb")
60
+ FileUtils.rm_rf(path)
59
61
  end
62
+ alias_method :remove_battlefiles, :remove_battlefile
60
63
 
61
64
  def battlefile(str)
62
65
  path = app("Battlestation")
63
- path.dirname.mkpath
64
- File.open(path.to_s, "w") do |f|
65
- f.puts str
66
- end
66
+ write_battlefile(path, str)
67
+ path
68
+ end
69
+
70
+ def battlefilerb(str)
71
+ path = app("Battlestation.rb")
72
+ write_battlefile(path, str)
73
+ path
67
74
  end
68
75
 
69
76
  def check_battlefile(str)
@@ -74,5 +81,13 @@ module Spec
74
81
  def in_app_root(&block)
75
82
  Dir.chdir(app, &block)
76
83
  end
84
+
85
+ private
86
+ def write_battlefile(path, str)
87
+ path.dirname.mkpath
88
+ File.open(path.to_s, "w") do |f|
89
+ f.puts str
90
+ end
91
+ end
77
92
  end
78
93
  end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Battlestation::Task do
4
+ subject {
5
+ Battlestation::Task.new :test_dep do
6
+ executable 'meh'
7
+ process 'some-server'
8
+ run 'some command'
9
+ file_exists 'some-file'
10
+ end
11
+ }
12
+
13
+ it { subject.operations.should be_an(Array) }
14
+ it { subject.operations.should have(4).elements }
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battlestation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-08 00:00:00.000000000 Z
12
+ date: 2012-03-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70112074127740 !ruby/object:Gem::Requirement
16
+ requirement: &70191472055060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70112074127740
24
+ version_requirements: *70191472055060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70112074151900 !ruby/object:Gem::Requirement
27
+ requirement: &70191472067960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70112074151900
35
+ version_requirements: *70191472067960
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-rspec
38
- requirement: &70112074174300 !ruby/object:Gem::Requirement
38
+ requirement: &70191472089200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70112074174300
46
+ version_requirements: *70191472089200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: thor
49
- requirement: &70112074179700 !ruby/object:Gem::Requirement
49
+ requirement: &70191472094880 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70112074179700
57
+ version_requirements: *70191472094880
58
58
  description: Get your battle station up and running fast
59
59
  email:
60
60
  - ariejan@ariejan.net
@@ -72,21 +72,30 @@ files:
72
72
  - Rakefile
73
73
  - battlestation.gemspec
74
74
  - bin/battlestation
75
- - examples/redis_plan.rb
75
+ - examples/Battlestation
76
76
  - lib/battlestation.rb
77
77
  - lib/battlestation/cli.rb
78
- - lib/battlestation/dependency.rb
78
+ - lib/battlestation/operations.rb
79
+ - lib/battlestation/operations/executable_check.rb
80
+ - lib/battlestation/operations/file_exists_check.rb
81
+ - lib/battlestation/operations/process_check.rb
82
+ - lib/battlestation/operations/runner.rb
79
83
  - lib/battlestation/plan.rb
84
+ - lib/battlestation/task.rb
80
85
  - lib/battlestation/ui.rb
81
86
  - lib/battlestation/version.rb
82
87
  - spec/battle_station_spec.rb
83
88
  - spec/cli_spec.rb
84
- - spec/dependency_spec.rb
89
+ - spec/operations/executable_spec.rb
90
+ - spec/operations/file_exists_spec.rb
91
+ - spec/operations/process_spec.rb
92
+ - spec/operations/runner_spec.rb
85
93
  - spec/plan_spec.rb
86
94
  - spec/spec_helper.rb
87
95
  - spec/support/helpers.rb
88
96
  - spec/support/path.rb
89
97
  - spec/support/ruby_ext.rb
98
+ - spec/task_spec.rb
90
99
  homepage: ''
91
100
  licenses: []
92
101
  post_install_message:
@@ -101,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
110
  version: '0'
102
111
  segments:
103
112
  - 0
104
- hash: -4191635253255446564
113
+ hash: -390528445491031614
105
114
  required_rubygems_version: !ruby/object:Gem::Requirement
106
115
  none: false
107
116
  requirements:
@@ -110,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
119
  version: '0'
111
120
  segments:
112
121
  - 0
113
- hash: -4191635253255446564
122
+ hash: -390528445491031614
114
123
  requirements: []
115
124
  rubyforge_project: battlestation
116
125
  rubygems_version: 1.8.11
@@ -120,9 +129,13 @@ summary: Get your battle station up and running fast
120
129
  test_files:
121
130
  - spec/battle_station_spec.rb
122
131
  - spec/cli_spec.rb
123
- - spec/dependency_spec.rb
132
+ - spec/operations/executable_spec.rb
133
+ - spec/operations/file_exists_spec.rb
134
+ - spec/operations/process_spec.rb
135
+ - spec/operations/runner_spec.rb
124
136
  - spec/plan_spec.rb
125
137
  - spec/spec_helper.rb
126
138
  - spec/support/helpers.rb
127
139
  - spec/support/path.rb
128
140
  - spec/support/ruby_ext.rb
141
+ - spec/task_spec.rb
@@ -1,6 +0,0 @@
1
- Battlestation.plan do
2
- dependency :redis do
3
- executable 'redis-server'
4
- process 'redis-server'
5
- end
6
- end
@@ -1,31 +0,0 @@
1
- module Battlestation
2
- class Dependency
3
- attr_accessor :name
4
- attr_accessor :executables
5
-
6
- def initialize(name, &block)
7
- @name = name
8
- @executables = []
9
-
10
- instance_eval &block if block_given?
11
- end
12
-
13
- def executable(filename)
14
- executables << filename
15
- end
16
-
17
- def execute
18
- result = []
19
-
20
- executables.each do |executable|
21
- if system("/usr/bin/env which #{executable}")
22
- result << "#{executable} found"
23
- else
24
- result << "#{executable} not found"
25
- end
26
- end
27
-
28
- result
29
- end
30
- end
31
- end
@@ -1,41 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- describe Battlestation::Dependency do
4
- context "executables" do
5
- subject {
6
- Battlestation::Dependency.new :test_dep do
7
- executable 'autoexec.bat'
8
- end
9
- }
10
-
11
- it "can be added" do
12
- subject.executables.should be_an(Array)
13
- subject.executables.should have(1).element
14
- subject.executables.should include('autoexec.bat')
15
- end
16
-
17
- it "can be executed" do
18
- subject.should_receive(:system).with('/usr/bin/env which autoexec.bat')
19
- subject.execute
20
- end
21
-
22
- it "returns a result when succesful" do
23
- subject.should_receive(:system).with('/usr/bin/env which autoexec.bat').and_return(true)
24
- result = subject.execute
25
-
26
- result.should be_an(Array)
27
- result.should have(1).element
28
- result.should include("autoexec.bat found")
29
- end
30
-
31
- it "returns a result when not succesful" do
32
- subject.should_receive(:system).with('/usr/bin/env which autoexec.bat').and_return(false)
33
- result = subject.execute
34
-
35
- result.should be_an(Array)
36
- result.should have(1).elements
37
- result.should include("autoexec.bat not found")
38
- end
39
- end
40
- end
41
-