lib/sunzi/cli.rb in sunzi-0.2.1 vs lib/sunzi/cli.rb in sunzi-0.3.0

- old
+ new

@@ -16,41 +16,35 @@ end # map "c" => :create # map "d" => :deploy - desc "create [PROJECT]", "Create sunzi project" + desc "create", "Create sunzi project" def create(project = 'sunzi') empty_directory project empty_directory "#{project}/remote" empty_directory "#{project}/remote/recipes" - template "templates/attributes.yml", "#{project}/attributes.yml" - template "templates/recipes.yml", "#{project}/recipes.yml" + template "templates/sunzi.yml", "#{project}/sunzi.yml" template "templates/remote/install.sh", "#{project}/remote/install.sh" template "templates/remote/recipes/ssh_key.sh", "#{project}/remote/recipes/ssh_key.sh" end - desc "deploy [USER@HOST] [PORT]", "Deploy sunzi project" - def deploy(*target) - if target.empty? or !target.first.match(/@/) - say shell.set_color("Usage: sunzi deploy root@example.com", :red, true) - abort - end + desc "deploy example.com (or user@example.com:2222)", "Deploy sunzi project" + def deploy(target) + user, host, port = parse_target(target) + endpoint = "#{user}@#{host}" + # compile attributes and recipes compile - host, port = target - port ||= 22 - user, domain = host.split('@') - # 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 #{domain} 2> /dev/null` + `ssh-keygen -R #{host} 2> /dev/null` commands = <<-EOS cd remote - tar cz . | ssh -o 'StrictHostKeyChecking no' #{host} -p #{port} ' + tar cz . | ssh -o 'StrictHostKeyChecking no' #{endpoint} -p #{port} ' rm -rf ~/sunzi && mkdir ~/sunzi && cd ~/sunzi && tar xz && bash install.sh' @@ -71,26 +65,33 @@ end desc "compile", "Compile sunzi project" def compile # Check if you're in the sunzi directory - unless File.exists?('attributes.yml') + unless File.exists?('sunzi.yml') say shell.set_color("You must be in the sunzi folder", :red, true) abort end - # Compile attributes.yml - hash = YAML.load(File.read('attributes.yml')) + # Load sunzi.yml + hash = YAML.load(File.read('sunzi.yml')) empty_directory 'remote/attributes' - hash.each do |key, value| + empty_directory 'remote/recipes' + + # Compile attributes.yml + hash['attributes'].each do |key, value| File.open("remote/attributes/#{key}", 'w'){|file| file.write(value) } end - # Compile recipes.yml - hash = YAML.load(File.read('recipes.yml')) - empty_directory 'remote/recipes' - hash.each do |key, value| + hash['recipes'].each do |key, value| get value, "remote/recipes/#{key}.sh" + end + end + + no_tasks do + def parse_target(target) + target.match(/(.*@)?(.*?)(:.*)?$/) + [ ($1 && $1.delete('@') || 'root'), $2, ($3 && $3.delete(':') || '22') ] end end end end