2012-08-21 19:31:24 +02:00
|
|
|
require "middleman-core/cli"
|
2012-08-22 20:45:33 +02:00
|
|
|
|
|
|
|
require "middleman-deploy/extension"
|
2012-10-05 05:44:02 +02:00
|
|
|
require "middleman-deploy/pkg-info"
|
2012-08-22 20:45:33 +02:00
|
|
|
|
2012-10-05 05:44:02 +02:00
|
|
|
PACKAGE = "#{Middleman::Deploy::PACKAGE}"
|
|
|
|
VERSION = "#{Middleman::Deploy::VERSION}"
|
2012-08-30 05:58:44 +02:00
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
module Middleman
|
|
|
|
module Cli
|
2012-08-22 23:15:37 +02:00
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
# This class provides a "deploy" command for the middleman CLI.
|
|
|
|
class Deploy < Thor
|
|
|
|
include Thor::Actions
|
|
|
|
|
|
|
|
check_unknown_options!
|
|
|
|
|
|
|
|
namespace :deploy
|
|
|
|
|
|
|
|
# Tell Thor to exit with a nonzero exit code on failure
|
|
|
|
def self.exit_on_failure?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-10-05 05:44:02 +02:00
|
|
|
desc "deploy", "Deploy build directory to a remote host via rsync or git"
|
2012-08-22 23:09:26 +02:00
|
|
|
method_option "clean",
|
|
|
|
:type => :boolean,
|
|
|
|
:aliases => "-c",
|
|
|
|
:desc => "Remove orphaned files or directories on the remote host"
|
2012-08-22 20:45:33 +02:00
|
|
|
def deploy
|
2012-09-04 19:25:21 +02:00
|
|
|
send("deploy_#{self.deploy_options.method}")
|
2012-08-30 05:50:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
def print_usage_and_die(message)
|
|
|
|
raise Error, "ERROR: " + message + "\n" + <<EOF
|
|
|
|
|
2012-11-14 00:33:13 +01:00
|
|
|
You should follow one of the three examples below to setup the deploy
|
2012-09-04 19:25:21 +02:00
|
|
|
extension in config.rb.
|
|
|
|
|
2012-10-05 05:44:02 +02:00
|
|
|
# To deploy the build directory to a remote host via rsync:
|
2012-09-04 19:25:21 +02:00
|
|
|
activate :deploy do |deploy|
|
|
|
|
deploy.method = :rsync
|
|
|
|
# host, user, and path *must* be set
|
|
|
|
deploy.user = "tvaughan"
|
|
|
|
deploy.host = "www.example.com"
|
|
|
|
deploy.path = "/srv/www/site"
|
2012-09-04 20:04:09 +02:00
|
|
|
# clean is optional (default is false)
|
2012-09-04 19:25:21 +02:00
|
|
|
deploy.clean = true
|
|
|
|
end
|
|
|
|
|
2012-10-05 05:44:02 +02:00
|
|
|
# To deploy to a remote branch via git (e.g. gh-pages on github):
|
2012-09-04 19:25:21 +02:00
|
|
|
activate :deploy do |deploy|
|
|
|
|
deploy.method = :git
|
2012-10-05 05:44:02 +02:00
|
|
|
# remote is optional (default is "origin")
|
|
|
|
# run `git remote -v` to see a list of possible remotes
|
|
|
|
deploy.remote = "some-other-remote-name"
|
|
|
|
# branch is optional (default is "gh-pages")
|
|
|
|
# run `git branch -a` to see a list of possible branches
|
|
|
|
deploy.branch = "some-other-branch-name"
|
2012-09-04 19:25:21 +02:00
|
|
|
end
|
2012-11-08 16:53:45 +01:00
|
|
|
|
|
|
|
# To deploy the build directory to a remote host via ftp:
|
|
|
|
activate :deploy do |deploy|
|
|
|
|
deploy.method = :ftp
|
|
|
|
# host, user, passwword and path *must* be set
|
|
|
|
deploy.host = "ftp.example.com"
|
|
|
|
deploy.user = "tvaughan"
|
|
|
|
deploy.password = "secret"
|
|
|
|
deploy.path = "/srv/www/site"
|
|
|
|
end
|
2012-09-04 19:25:21 +02:00
|
|
|
EOF
|
2012-08-30 05:50:23 +02:00
|
|
|
end
|
2012-08-22 23:09:26 +02:00
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
def deploy_options
|
|
|
|
options = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
options = ::Middleman::Application.server.inst.options
|
|
|
|
rescue
|
2012-09-04 20:04:09 +02:00
|
|
|
print_usage_and_die "You need to activate the deploy extension in config.rb."
|
2012-09-04 19:25:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if (!options.method)
|
2012-09-04 20:04:09 +02:00
|
|
|
print_usage_and_die "The deploy extension requires you to set a method."
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
2012-08-22 23:09:26 +02:00
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
if (options.method == :rsync)
|
|
|
|
if (!options.host || !options.user || !options.path)
|
2012-09-04 20:04:09 +02:00
|
|
|
print_usage_and_die "The rsync deploy method requires host, user, and path to be set."
|
2012-09-04 19:25:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_rsync
|
|
|
|
host = self.deploy_options.host
|
|
|
|
port = self.deploy_options.port
|
|
|
|
user = self.deploy_options.user
|
|
|
|
path = self.deploy_options.path
|
|
|
|
|
2012-10-05 05:44:02 +02:00
|
|
|
puts "## Deploying via rsync to #{user}@#{host}:#{path} port=#{port}"
|
|
|
|
|
2012-08-22 23:09:26 +02:00
|
|
|
command = "rsync -avze '" + "ssh -p #{port}" + "' build/ #{user}@#{host}:#{path}"
|
|
|
|
|
|
|
|
if options.has_key? "clean"
|
|
|
|
clean = options.clean
|
|
|
|
else
|
2012-09-04 19:25:21 +02:00
|
|
|
clean = self.deploy_options.clean
|
2012-08-22 23:09:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if clean
|
2012-08-23 01:17:46 +02:00
|
|
|
command += " --delete"
|
2012-08-22 23:09:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
run command
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
2012-08-22 23:15:37 +02:00
|
|
|
|
2012-08-30 05:50:23 +02:00
|
|
|
def deploy_git
|
2012-10-05 05:44:02 +02:00
|
|
|
remote = self.deploy_options.remote
|
2012-09-27 00:11:13 +02:00
|
|
|
branch = self.deploy_options.branch
|
|
|
|
|
2012-10-05 05:44:02 +02:00
|
|
|
puts "## Deploying via git to remote=\"#{remote}\" and branch=\"#{branch}\""
|
|
|
|
|
2012-11-27 13:33:04 +01:00
|
|
|
#check if remote is not a git url
|
|
|
|
unless remote =~ /\.git$/
|
|
|
|
remote = `git config --get remote.#{remote}.url`.chop
|
|
|
|
end
|
2012-10-05 05:44:02 +02:00
|
|
|
|
2012-11-27 13:33:04 +01:00
|
|
|
#if the remote name doesn't exist in the main repo
|
|
|
|
if remote == ''
|
|
|
|
puts "Can't deploy! Please add a remote with the name '#{self.deploy_options.remote}' to your repo."
|
|
|
|
exit
|
|
|
|
end
|
2012-08-30 05:50:23 +02:00
|
|
|
|
2012-11-27 13:33:04 +01:00
|
|
|
Dir.chdir('build') do
|
|
|
|
unless File.exists?('.git')
|
|
|
|
`git init`
|
|
|
|
`git remote add origin #{remote}`
|
|
|
|
else
|
|
|
|
#check if the remote repo has changed
|
|
|
|
unless remote == `git config --get remote.origin.url`.chop
|
|
|
|
`git remote rm origin`
|
|
|
|
`git remote add origin #{remote}`
|
|
|
|
end
|
|
|
|
end
|
2012-08-30 05:50:23 +02:00
|
|
|
|
2013-01-02 22:38:24 +01:00
|
|
|
#if there is a branch with that name, switch to it, otherwise create a new one and switch to it
|
|
|
|
if `git branch`.split("\n").keep_if{ |r| r =~ Regexp.new(branch,true) }.count > 0
|
|
|
|
`git checkout #{branch}`
|
2012-11-27 13:33:04 +01:00
|
|
|
else
|
|
|
|
`git checkout -b #{branch}`
|
|
|
|
end
|
2012-10-05 05:44:02 +02:00
|
|
|
|
2012-11-27 13:33:04 +01:00
|
|
|
`git add -A`
|
|
|
|
`git commit --allow-empty -am 'Automated commit at #{Time.now.utc} by #{PACKAGE} #{VERSION}'`
|
|
|
|
`git push -f origin #{branch}`
|
|
|
|
end
|
2012-08-30 05:50:23 +02:00
|
|
|
end
|
|
|
|
|
2012-11-08 16:45:57 +01:00
|
|
|
def deploy_ftp
|
|
|
|
require 'net/ftp'
|
|
|
|
require 'ptools'
|
|
|
|
|
|
|
|
host = self.deploy_options.host
|
|
|
|
user = self.deploy_options.user
|
|
|
|
pass = self.deploy_options.password
|
|
|
|
path = self.deploy_options.path
|
|
|
|
|
|
|
|
puts "## Deploying via ftp to #{user}@#{host}:#{path}"
|
|
|
|
|
|
|
|
ftp = Net::FTP.new(host)
|
|
|
|
ftp.login(user, pass)
|
|
|
|
ftp.chdir(path)
|
|
|
|
ftp.passive = true
|
|
|
|
|
|
|
|
Dir.chdir('build/') do
|
|
|
|
Dir['**/*'].each do |f|
|
|
|
|
if File.directory?(f)
|
|
|
|
begin
|
|
|
|
ftp.mkdir(f)
|
|
|
|
rescue
|
|
|
|
puts "Folder '#{f}' exists. skipping..."
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
if File.binary?(f)
|
|
|
|
ftp.putbinaryfile(f, f)
|
|
|
|
else
|
|
|
|
ftp.puttextfile(f, f)
|
|
|
|
end
|
|
|
|
rescue Exception => e
|
|
|
|
reply = e.message
|
|
|
|
err_code = reply[0,3].to_i
|
|
|
|
if err_code == 550
|
|
|
|
if File.binary?(f)
|
|
|
|
ftp.putbinaryfile(f, f)
|
|
|
|
else
|
|
|
|
ftp.puttextfile(f, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ftp.close
|
|
|
|
end
|
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Alias "d" to "deploy"
|
|
|
|
Base.map({ "d" => "deploy" })
|
2012-08-22 23:15:37 +02:00
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
end
|
|
|
|
end
|