Skip to content

Commit

Permalink
Add infrataster command.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryotarai committed Jun 22, 2014
1 parent 1767e16 commit 809ff33
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/infrataster
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require 'infrataster/cli'

Infrataster::CLI.start(ARGV)

1 change: 1 addition & 0 deletions infrataster.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "capybara"
spec.add_runtime_dependency "poltergeist"
spec.add_runtime_dependency "faraday"
spec.add_runtime_dependency "thor"

spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
Expand Down
48 changes: 48 additions & 0 deletions lib/infrataster/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'thor'
require 'fileutils'
require 'erb'

module Infrataster
class CLI < Thor
desc "init", "Initialize Infrataster specs."
option :path, type: :string, default: '.'
def init
path = File.expand_path(options[:path])
FileUtils.mkdir_p(path)
create_file_from_template(File.expand_path("Gemfile", path))
create_file_from_template(File.expand_path("Rakefile", path))
FileUtils.mkdir(File.expand_path("spec", path))
if ask_yes_or_no("Use Vagrant?")
create_file_from_template(File.expand_path("spec/Vagrantfile", path))
end
create_file_from_template(File.expand_path("spec/spec_helper.rb", path))
create_file_from_template(File.expand_path("spec/app_spec.rb", path))
end

private
def ask_yes_or_no(question)
print "#{question} (y/N): "
answer = $stdin.gets
if answer =~ /^y/i
true
else
false
end
end

def create_file_from_template(path)
basename = File.basename(path)

if File.exist?(path)
puts "#{basename} exists already. Skip."
return
end

open(path, 'w') do |f|
f.write(ERB.new(File.read(File.expand_path("../fixtures/#{basename}.erb", __FILE__))).result)
end

puts "Created: #{basename}"
end
end
end
7 changes: 7 additions & 0 deletions lib/infrataster/fixtures/Gemfile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by `infrataster init`
source "https://rubygems.org"

gem "infrataster"

# If you would like to test mysql, uncomment the next line.
# gem "infrataster-plugin-mysql"
42 changes: 42 additions & 0 deletions lib/infrataster/fixtures/Rakefile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by `infrataster init`

require "rspec/core/rake_task"

def exec_and_abort_if_fail(cmd)
system cmd
unless $?.exitstatus == 0
$stderr.puts "'#{cmd}' failed."
abort
end
end

desc 'Run tests'
task :spec => ['spec:integration']

namespace :spec do
RSpec::Core::RakeTask.new("infrataster") do |task|
task.pattern = "./spec/{,/*/**}/*_spec.rb"
end

desc 'Prepare'
task :prepare do
exec_and_abort_if_fail '/usr/bin/vagrant up'
exec_and_abort_if_fail '/usr/bin/vagrant provision'
end

desc 'Provision'
task :provision do
exec_and_abort_if_fail '/usr/bin/vagrant provision'
end

desc 'Restart VMs'
task :restart do
exec_and_abort_if_fail '/usr/bin/vagrant reload --provision'
end

desc 'Clean'
task :clean do
exec_and_abort_if_fail '/usr/bin/vagrant destroy -f'
end
end

13 changes: 13 additions & 0 deletions lib/infrataster/fixtures/Vagrantfile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# Generated by `infrataster init`

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise64"

config.vm.define :app do |c|
c.vm.network "private_network", ip: "192.168.44.10"
end
end
11 changes: 11 additions & 0 deletions lib/infrataster/fixtures/app_spec.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Generated by `infrataster init`
require 'spec_helper'

describe server(:app) do
describe http('http://app') do
it "responds OK 200" do
expect(response.status).to eq(200)
end
end
end

18 changes: 18 additions & 0 deletions lib/infrataster/fixtures/spec_helper.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by `infrataster init`
require 'infrataster/rspec'

ENV['VAGRANT_CWD'] = File.dirname(__FILE__)

Infrataster::Server.define(
:app,
'192.168.0.0/16',
vagrant: true,
)

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

config.order = 'random'
end

0 comments on commit 809ff33

Please sign in to comment.