module TapeBoxer class Installer < ExecutionModule TapeBoxer.register_module :installer, self action :dependencies, proc { dependencies }, 'Install dependencies' action :install, proc {install}, 'Creates all nessisary hosts and config files' action :uninstall, proc {uninstall}, 'Cleans up files generated by the installer' def initialize(*args) super end protected def dependencies puts 'Dependencies:' if system "ansible-galaxy install -r #{tape_dir}/requirements.yml -p #{tape_dir}/vendor --force" print 'Installing/updating dependencies: ' puts '✔'.green else puts '✘'.red end end def install dependencies File.open('.gitignore', 'r+') { |f| f.puts '.tape' unless f.read =~/^\.tape$/ } mkdir 'roles' if fe_app? && !rails_app? puts '🔎 JS/HTML app detected'.pink copy_static_app_examples else rails_app? puts '🔎 Rails app detected'.pink copy_basic_examples end copy_example 'templates/base/hosts.example', 'hosts' mkdir 'dev_keys' print 'Are you going to use vagrant? (y/n): ' if gets.chomp == 'y' copy_example 'Vagrantfile', 'Vagrantfile' end end def fe_app? !Dir["#{local_dir}/gulpfile.*"].empty? end def rails_app? !Dir["#{local_dir}/config.ru"].empty? end def copy_static_app_examples copy_example 'templates/static_html/omnibox.example.yml', 'omnibox.yml' copy_example 'templates/static_html/deploy.example.yml', 'deploy.yml' copy_example 'templates/static_html/tape_vars.example.yml', 'tape_vars.yml' end def copy_basic_examples copy_example 'templates/base/omnibox.example.yml', 'omnibox.yml' copy_example 'templates/base/deploy.example.yml', 'deploy.yml' copy_example 'templates/base/tape_vars.example.yml', 'tape_vars.yml' end def uninstall rm 'omnibox.yml' rm 'deploy.yml' rm 'tape_vars.yml' rm 'roles' rm 'hosts' rm 'dev_keys' rm 'Vagrantfile' end def rm(file) print 'Deleting '.red FileUtils.rm_r "#{local_dir}/#{file}" puts file end def mkdir(name) print "#{name}: " begin FileUtils.mkdir name success rescue Errno::EEXIST exists rescue Exception => e error raise e end end def touch(file) File.new "#{local_dir}/#{file}", 'w' end def copy_example(file, cp_file) print "#{cp_file}: " begin if File.exists?("#{local_dir}/#{cp_file}") exists else FileUtils.cp("#{tape_dir}/#{file}", "#{local_dir}/#{cp_file}") success end rescue Exception => e error raise e end end def success puts '✔'.green end def error puts '✘'.red end def exists puts '✘ (Exists)'.yellow end end end class String # colorization 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