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
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-07-17 03:43:00 +02:00
|
|
|
desc "deploy [options]", Middleman::Deploy::TAGLINE
|
2013-06-11 06:42:12 +02:00
|
|
|
method_option "build_before",
|
|
|
|
:type => :boolean,
|
|
|
|
:aliases => "-b",
|
|
|
|
:desc => "Run `middleman build` before the deploy step"
|
2013-07-17 03:53:11 +02:00
|
|
|
|
2012-08-22 20:45:33 +02:00
|
|
|
def deploy
|
2013-06-11 06:42:12 +02:00
|
|
|
if options.has_key? "build_before"
|
|
|
|
build_before = options.build_before
|
|
|
|
else
|
|
|
|
build_before = self.deploy_options.build_before
|
|
|
|
end
|
2013-07-17 03:24:45 +02:00
|
|
|
if build_before
|
|
|
|
# http://forum.middlemanapp.com/t/problem-with-the-build-task-in-an-extension
|
2013-07-24 03:43:15 +02:00
|
|
|
run("middleman build") || exit(1)
|
2013-07-17 03:24:45 +02:00
|
|
|
end
|
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
|
|
|
|
|
2013-07-17 04:25:06 +02:00
|
|
|
You should follow one of the four 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
|
2013-08-09 01:39:10 +02:00
|
|
|
# host and path *must* be set
|
2012-09-04 19:25:21 +02:00
|
|
|
deploy.host = "www.example.com"
|
|
|
|
deploy.path = "/srv/www/site"
|
2013-08-09 01:39:10 +02:00
|
|
|
# user is optional (no default)
|
|
|
|
deploy.user = "tvaughan"
|
2013-08-09 01:41:46 +02:00
|
|
|
# port is optional (default is 22)
|
|
|
|
deploy.port = 5309
|
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
|
|
|
|
2013-07-17 04:25:06 +02:00
|
|
|
# To deploy the build directory to a remote host via ftp:
|
2012-11-08 16:53:45 +01:00
|
|
|
activate :deploy do |deploy|
|
2013-07-17 04:25:06 +02:00
|
|
|
deploy.method = :ftp
|
2012-11-08 16:53:45 +01:00
|
|
|
# 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
|
2013-07-17 04:25:06 +02:00
|
|
|
|
|
|
|
# To deploy the build directory to a remote host via sftp:
|
|
|
|
activate :deploy do |deploy|
|
|
|
|
deploy.method = :sftp
|
|
|
|
# host, user, passwword and path *must* be set
|
|
|
|
deploy.host = "sftp.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
|
|
|
|
2013-05-06 02:13:14 +02:00
|
|
|
def inst
|
|
|
|
::Middleman::Application.server.inst
|
|
|
|
end
|
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
def deploy_options
|
|
|
|
options = nil
|
|
|
|
|
|
|
|
begin
|
2013-05-06 02:13:14 +02:00
|
|
|
options = inst.options
|
2013-08-24 17:16:22 +02:00
|
|
|
rescue NoMethodError
|
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
|
|
|
|
2013-07-17 04:25:06 +02:00
|
|
|
case options.method
|
|
|
|
when :rsync
|
2013-08-09 01:39:10 +02:00
|
|
|
if (!options.host || !options.path)
|
|
|
|
print_usage_and_die "The rsync deploy method requires host and path to be set."
|
2012-09-04 19:25:21 +02:00
|
|
|
end
|
2013-09-12 20:30:09 +02:00
|
|
|
when :ftp
|
2013-01-20 14:51:22 +01:00
|
|
|
if (!options.host || !options.user || !options.password || !options.path)
|
2013-09-12 20:30:09 +02:00
|
|
|
print_usage_and_die "The ftp deploy method requires host, user, password, and path to be set."
|
|
|
|
end
|
|
|
|
when :sftp
|
|
|
|
if (!options.host || !options.user || !options.path)
|
|
|
|
print_usage_and_die "The sftp deploy method requires host, user and path to be set."
|
2013-01-20 14:51:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-04 19:25:21 +02:00
|
|
|
options
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_rsync
|
|
|
|
host = self.deploy_options.host
|
|
|
|
port = self.deploy_options.port
|
|
|
|
path = self.deploy_options.path
|
|
|
|
|
2013-08-09 01:39:10 +02:00
|
|
|
# Append "@" to user if provided.
|
|
|
|
user = self.deploy_options.user
|
|
|
|
user = "#{user}@" if user && !user.empty?
|
|
|
|
|
|
|
|
dest_url = "#{user}#{host}:#{path}"
|
|
|
|
|
|
|
|
puts "## Deploying via rsync to #{dest_url} port=#{port}"
|
2012-10-05 05:44:02 +02:00
|
|
|
|
2013-08-09 01:39:10 +02:00
|
|
|
command = "rsync -avze '" + "ssh -p #{port}" + "' #{self.inst.build_dir}/ #{dest_url}"
|
2012-08-22 23:09:26 +02:00
|
|
|
|
2013-07-17 03:53:11 +02:00
|
|
|
if self.deploy_options.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
|
|
|
|
2013-05-06 02:13:14 +02:00
|
|
|
Dir.chdir(self.inst.build_dir) do
|
2012-11-27 13:33:04 +01:00
|
|
|
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
|
2013-05-30 16:25:03 +02:00
|
|
|
if `git branch`.split("\n").any? { |b| b =~ /#{branch}/i }
|
2013-01-02 22:38:24 +01:00
|
|
|
`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`
|
2013-08-04 00:05:19 +02:00
|
|
|
`git commit --allow-empty -am 'Automated commit at #{Time.now.utc} by #{Middleman::Deploy::PACKAGE} #{Middleman::Deploy::VERSION}'`
|
2012-11-27 13:33:04 +01:00
|
|
|
`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
|
|
|
|
|
2013-05-06 02:13:14 +02:00
|
|
|
Dir.chdir(self.inst.build_dir) do
|
2013-01-13 19:41:58 +01:00
|
|
|
files = Dir.glob('**/*', File::FNM_DOTMATCH)
|
|
|
|
files.reject { |a| a =~ Regexp.new('\.$') }.each do |f|
|
2012-11-08 16:45:57 +01:00
|
|
|
if File.directory?(f)
|
|
|
|
begin
|
|
|
|
ftp.mkdir(f)
|
2013-01-20 14:51:22 +01:00
|
|
|
puts "Created directory #{f}"
|
2012-11-08 16:45:57 +01:00
|
|
|
rescue
|
|
|
|
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
|
2013-01-20 14:51:22 +01:00
|
|
|
puts "Copied #{f}"
|
2012-11-08 16:45:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ftp.close
|
|
|
|
end
|
|
|
|
|
2013-06-08 22:46:43 +02:00
|
|
|
def deploy_sftp
|
|
|
|
require 'net/sftp'
|
|
|
|
require 'ptools'
|
2013-07-17 03:40:33 +02:00
|
|
|
|
2013-06-08 22:46:43 +02:00
|
|
|
host = self.deploy_options.host
|
|
|
|
user = self.deploy_options.user
|
|
|
|
pass = self.deploy_options.password
|
|
|
|
path = self.deploy_options.path
|
2013-07-17 03:40:33 +02:00
|
|
|
|
2013-06-08 22:46:43 +02:00
|
|
|
puts "## Deploying via sftp to #{user}@#{host}:#{path}"
|
2013-07-17 03:40:33 +02:00
|
|
|
|
2013-06-08 22:46:43 +02:00
|
|
|
Net::SFTP.start(host, user, :password => pass) do |sftp|
|
|
|
|
sftp.mkdir(path)
|
|
|
|
Dir.chdir(self.inst.build_dir) do
|
|
|
|
files = Dir.glob('**/*', File::FNM_DOTMATCH)
|
|
|
|
files.reject { |a| a =~ Regexp.new('\.$') }.each do |f|
|
|
|
|
if File.directory?(f)
|
|
|
|
begin
|
|
|
|
sftp.mkdir("#{path}/#{f}")
|
|
|
|
puts "Created directory #{f}"
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
sftp.upload(f, "#{path}/#{f}")
|
|
|
|
rescue Exception => e
|
|
|
|
reply = e.message
|
|
|
|
err_code = reply[0,3].to_i
|
|
|
|
if err_code == 550
|
|
|
|
sftp.upload(f, "#{path}/#{f}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
puts "Copied #{f}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|