class Sunzi::Cli

Public Class Methods

source_root() click to toggle source
# File lib/sunzi/cli.rb, line 33
def self.source_root
  File.expand_path('../../',__FILE__)
end

Public Instance Methods

based(file) click to toggle source
# File lib/sunzi/cli.rb, line 150
def based(file)
  File.expand_path("config/sunzi/#{file.sub('config/sunzi/', '')}")
end
build_install() click to toggle source
# File lib/sunzi/cli.rb, line 143
def build_install
  _install_path = compiled("_install.sh")
  template based("install.sh"), _install_path
  content = File.binread(_install_path) << "\n" << File.binread(compiled("roles/#{@role}.sh"))
  create_file compiled("install.sh"), content
end
compile(first, *args) click to toggle source
# File lib/sunzi/cli.rb, line 21
def compile(first, *args)
  do_compile(first, *args)
end
compile_attributes() click to toggle source
# File lib/sunzi/cli.rb, line 116
def compile_attributes
  # Break down attributes into individual files
  @attributes.each {|key, value| create_file compiled("attributes/#{key}"), value }
  @attributes = OpenStruct.new(@attributes)
end
compiled(file) click to toggle source
# File lib/sunzi/cli.rb, line 154
def compiled(file)
  File.expand_path("compiled/#{file.sub('config/sunzi/', '')}")
end
copy_local_files() click to toggle source
# File lib/sunzi/cli.rb, line 131
def copy_local_files
  files = Dir["{config/sunzi/recipes,config/sunzi/roles,config/sunzi/files}/**/*"].select { |file| File.file?(file) }

  files.each do |file|
    template based(file), compiled(file)
  end

  (@config['files'] || []).each do |file|
    template based(file), compiled("files/#{File.basename(file)}")
  end
end
copy_remote_recipes() click to toggle source
# File lib/sunzi/cli.rb, line 122
def copy_remote_recipes
  # Retrieve remote recipes via HTTP
  cache_remote_recipes = @config['preferences'] && @config['preferences']['cache_remote_recipes']
  (@config['recipes'] || []).each do |key, value|
    next if cache_remote_recipes and File.exists?(compiled("recipes/#{key}.sh"))
    get value, "compiled/recipes/#{key}.sh"
  end
end
create() click to toggle source
# File lib/sunzi/cli.rb, line 10
def create
  do_create
end
deploy(first, *args) click to toggle source
# File lib/sunzi/cli.rb, line 16
def deploy(first, *args)
  do_deploy(first, *args)
end
do_compile(first, *args) click to toggle source
# File lib/sunzi/cli.rb, line 78
def do_compile(first, *args)
  load_env(first, *args)

  compile_attributes
  copy_remote_recipes
  copy_local_files
  build_install
end
do_create() click to toggle source
# File lib/sunzi/cli.rb, line 37
def do_create
  directory 'templates/create', 'config/sunzi'
end
do_deploy(first, *args) click to toggle source
# File lib/sunzi/cli.rb, line 41
      def do_deploy(first, *args)
        # compile attributes and recipes
        do_compile(first, *args)

        # The host key might change when we instantiate a new VM, so
        # we remove (-R) the old host key from known_hosts.
        `ssh-keygen -R #{@host} 2> /dev/null`

        remote_commands = <<-EOS
        rm -rf ~/sunzi &&
        mkdir ~/sunzi &&
        cd ~/sunzi &&
        tar xz &&
        #{@sudo}bash install.sh
        EOS

        remote_commands.strip! << ' && rm -rf ~/sunzi' if @config['preferences'] and @config['preferences']['erase_remote_folder']

        local_commands = <<-EOS
        cd compiled
        tar cz . | ssh -o 'StrictHostKeyChecking no' #{@user}@#{@host} -p #{@port} '#{remote_commands}'
        EOS

        Open3.popen3(local_commands) do |stdin, stdout, stderr|
          stdin.close
          t = Thread.new do
            while (line = stderr.gets)
              print line.color(:red)
            end
          end
          while (line = stdout.gets)
            print line.color(:green)
          end
          t.join
        end
      end
load_env(first, *args) click to toggle source
# File lib/sunzi/cli.rb, line 87
def load_env(first, *args)
  @stage = first
  @role = args[0]

  abort_with 'You must have a sunzi folder' unless File.exists?(based("sunzi.yml"))
  abort_with "#{@role} doesn't exist!"       unless File.exists?(based("roles/#{@role}.sh"))

  cap = Capistrano.load_env(@stage)
  if options.sudo?
    @sudo = 'sudo '
    @user = cap[:admin_name]
  else
    @user = cap[:deployer_name]
  end
  @host = cap[:server]
  @port = cap[:port]

  @config = YAML.load(File.read(based("sunzi.yml")))

  @attributes = cap.merge(Database.load_env(@stage)).merge(Secrets.load_env(@stage)).merge(
    env_stage: @stage,
    env_role: @role,
    env_sudo: options.sudo?,
    env_user: @user,
    env_host: @host,
    env_port: @port
  ).merge(@config['attributes'] || {})
end
version() click to toggle source
# File lib/sunzi/cli.rb, line 26
def version
  puts Gem.loaded_specs['sunzi'].version.to_s
end