Skip to content

Commit

Permalink
Some more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dkniffin committed Aug 11, 2016
1 parent 43dd765 commit 3156c92
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 72 deletions.
4 changes: 2 additions & 2 deletions bin/tape
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby

require 'tape'
require 'taperole'

TapeBoxer::Tape.start(ARGV)
Taperole::Commands::Tape.start(ARGV)
1 change: 0 additions & 1 deletion lib/tape.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/tape/commands/ansible.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/tape/commands/tape.rb

This file was deleted.

27 changes: 0 additions & 27 deletions lib/tape/detector.rb

This file was deleted.

18 changes: 18 additions & 0 deletions lib/taperole.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'thor'
require 'taperole/helpers/string_color_monkeypatch'

module Taperole
autoload :VERSION, 'taperole/version'
autoload :AnsibleRunner, 'taperole/core/ansible_runner'
autoload :Installer, 'taperole/core/installer'

module Commands
autoload :Tape, 'taperole/commands/tape'
autoload :Installer, 'taperole/commands/installer'
autoload :Ansible, 'taperole/commands/ansible'
end

module Helpers
autoload :Files, 'taperole/helpers/files'
end
end
26 changes: 26 additions & 0 deletions lib/taperole/commands/ansible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Taperole
module Commands
class Ansible < Thor
include Taperole::AnsibleRunner

class_option :limit,
type: :string,
aliases: :l,
desc: "Limits ansible runs to hosts matching the given pattern"
class_option :task,
type: :string,
aliases: :T,
desc: "Name of the rake task to execute"

desc 'everything', 'Initial setup of a server'
def everything
valid_preconfigs ? ansible : puts("Not a Rails or JS app")
end

desc 'deploy', 'Deploy the latest version of the app'
def deploy
puts 'deploying'
end
end
end
end
17 changes: 17 additions & 0 deletions lib/taperole/commands/installer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Taperole
module Commands
class Installer < Thor
include Taperole::Installer

desc 'install', 'Creates all necessary hosts and config files'
def install
install_tape
end

desc 'uninstall', 'Cleans up files generated by the installer'
def uninstall
uninstall_tape
end
end
end
end
22 changes: 22 additions & 0 deletions lib/taperole/commands/tape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'thor'

module Taperole
module Commands
class Tape < Thor
class_option :verbose, type: :boolean

map %w[--version -v] => :__print_version

desc "--version, -v", "print the version"
def __print_version
puts Taperole::VERSION
end

desc 'ansible [COMMAND]', 'run tapes ansible commands'
subcommand 'ansible', Ansible

desc 'installer [COMMAND]', 'install and uninstall tape'
subcommand 'installer', Installer
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'tape/detector'

module TapeBoxer
module Taperole
module AnsibleRunner
include TapeBoxer::Detector
include Helpers

protected

Expand Down
116 changes: 116 additions & 0 deletions lib/taperole/core/installer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
require 'pry-byebug'

module Taperole
module Installer
include Helpers::Files
include Thor::Actions

protected

def install_tape
File.open('.gitignore', 'r+') { |f| f.puts '.tape' unless f.read =~ /^\.tape$/ }
mkdir tapefiles_dir
if fe_app? && !rails_app?
puts '🔎 JS/HTML app detected'.pink
copy_static_app_examples
elsif rails_app?
puts '🔎 Rails app detected'.pink
copy_basic_examples
end
create_roles_dir
create_inventory_file
create_ssh_keys_dir
if ask 'Are you going to use vagrant? (y/n): '
copy_example 'Vagrantfile', 'Vagrantfile'
end
end

def create_roles_dir
mkdir "#{tapefiles_dir}/roles"
`touch #{tapefiles_dir}/roles/.keep`
end

def create_ssh_keys_dir
mkdir "#{tapefiles_dir}/dev_keys"
end

def create_inventory_file
copy_example 'templates/base/hosts.example', "#{tapefiles_dir}/hosts"
end

def copy_static_app_examples
%w(omnibox deploy tape_vars).each do |base_filename|
copy_example(
"templates/static_html/#{base_filename}.example.yml",
"#{tapefiles_dir}/#{base_filename}.yml"
)
end
end

def copy_basic_examples
%w(omnibox deploy tape_vars rake).each do |base_filename|
copy_example(
"templates/base/#{base_filename}.example.yml",
"#{tapefiles_dir}/#{base_filename}.yml"
)
end
end

def uninstall_tape
rm "#{tapefiles_dir}/omnibox.yml"
rm "#{tapefiles_dir}/deploy.yml"
rm "#{tapefiles_dir}/tape_vars.yml"
rm "#{tapefiles_dir}/rake.yml"
rm "#{tapefiles_dir}/roles"
rm "#{tapefiles_dir}/hosts"
rm "#{tapefiles_dir}/dev_keys"
rm "#{local_dir}/Vagrantfile"
end

def rm(file)
print 'Deleting '.red
puts file
FileUtils.rm_r file
end

def mkdir(name)
print "#{::Pathname.new(name).basename}: "
begin
FileUtils.mkdir name
success
rescue Errno::EEXIST
exists
rescue StandardError => e
error
raise e
end
end

def copy_example(file, cp_file)
print "#{::Pathname.new(cp_file).basename}: "
begin
if File.exist?(cp_file.to_s)
exists
else
FileUtils.cp("#{tape_dir}/#{file}", cp_file.to_s)
success
end
rescue StandardError => e
error
raise e
end
end

def success
puts '✔'.green
end

def error
puts '✘'.red
end

def exists
puts '✘ (Exists)'.yellow
end
end
end
31 changes: 31 additions & 0 deletions lib/taperole/helpers/files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'pry-byebug'

module Taperole
module Helpers
module Files
def fe_app?
!Dir["#{local_dir}/package.json"].empty?
end

def rails_app?
!Dir["#{local_dir}/config.ru"].empty?
end

def tape_dir
File.realpath(File.join(__dir__, '../../../'))
end

def local_dir
Dir.pwd
end

def tapefiles_dir
local_dir + '/taperole'
end

def tapecfg_dir
local_dir + '/.tape'
end
end
end
end
25 changes: 25 additions & 0 deletions lib/taperole/helpers/string_color_monkeypatch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class String
def initialize(string)
@string = string
end

def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end

def red
colorize(31)
end

def green
colorize(32)
end

def yellow
colorize(33)
end

def pink
colorize(35)
end
end
3 changes: 3 additions & 0 deletions lib/taperole/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Taperole
VERSION = '1.7.0'.freeze
end
6 changes: 5 additions & 1 deletion taperole.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
$:.push File.expand_path("../lib", __FILE__)
require 'taperole/version'

Gem::Specification.new do |spec|
spec.name = "taperole"
spec.version = '1.7.0'
spec.version = Taperole::VERSION.dup
spec.authors = ['Jack Forrest', 'Smashing Boxes', 'Brandon Mathis']
spec.description = "General purpose server provisioning and application deployment toolkit"
spec.email = ['jack@smashingboxes.com', 'brandon@sbox.es']
Expand All @@ -11,4 +14,5 @@ Gem::Specification.new do |spec|
spec.files = `git ls-files`.split("\n")
spec.executables = 'tape'
spec.add_runtime_dependency 'slack-notifier', '~> 1.5'
spec.require_paths = ['lib']
end

0 comments on commit 3156c92

Please sign in to comment.