Skip to content

Commit

Permalink
restructure, dynamic options parser for all transfer types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Nazarenus committed Jan 11, 2014
1 parent 48d1cd7 commit 8a25dca
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 65 deletions.
29 changes: 15 additions & 14 deletions bin/travis-custom-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ def usage
puts 'Usage: travis-custom-deploy TRANSFER_TYPE FILES...'
end

# Returns a list of read environment variables in a hash based
# on options.rb. The hash may be used as parameter for deployment.rb.
def get_env_vars(transfer_type)
envs = TravisCustomDeploy::Options.get_options(transfer_type)
options = {}
for e in envs
value = ENV['DEPLOY_' + e.upcase]
options[e] = value
end
options
end

if ARGV.count < 2
usage
end

transfer = ARGV[0]
transfer_type = ARGV[0]
files = ARGV[1..-1]

host = ENV['DEPLOY_HOST']
username = ENV['DEPLOY_USERNAME']
password = ENV['DEPLOY_PASSWORD']
remotedir = ENV['DEPLOY_REMOTEDIR']

transfer_opts = {
:type => transfer,
:host => ENV['DEPLOY_HOST'],
:username => ENV['DEPLOY_USERNAME'],
:password => ENV['DEPLOY_PASSWORD'],
:remotedir => ENV['DEPLOY_REMOTEDIR']
}
options = get_env_vars(transfer_type)

d = TravisCustomDeploy::Deployment.new(transfer_opts, files)
d = TravisCustomDeploy::Deployment.new(transfer_type, options, files)
d.deploy
2 changes: 1 addition & 1 deletion lib/travis-custom-deploy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'travis-custom-deploy/deployment'
require 'travis-custom-deploy/transfer'
require 'travis-custom-deploy/version'
require 'travis-custom-deploy/services'
require 'travis-custom-deploy/options'
require 'travis-custom-deploy/transfer/base'
32 changes: 13 additions & 19 deletions lib/travis-custom-deploy/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,33 @@ module TravisCustomDeploy

class Deployment

attr_reader :remoteopts
attr_reader :options
attr_reader :files

def initialize(remoteopts, files)
check_remoteopts(remoteopts)
@remoteopts = remoteopts
# Initializes a new deployment
#
# remteopts - the options to connect to the remote server
# files - the files to transfer
def initialize(transfer_type, options, files)
raise ArgumentError, 'transfer type must not be nil' if transfer_type.nil?
@files = files
check_services(@files[0])
@transfer = get_transfer(remoteopts[:type])
@options = options
@transfer = get_transfer(transfer_type)
end

# Actually start the deployment
# Starts the deployment
def deploy
@transfer.transfer
end

# Check if the remoteopts are valid
#
# opts - the remote opts to check
def check_remoteopts(opts)
raise ArgumentError, 'host must not be nil' if opts[:host].nil?
raise ArgumentError, 'username must not be nil' if opts[:username].nil?
raise ArgumentError, 'password must not be nil' if opts[:password].nil?
raise ArgumentError, 'transfer type must not be nil' if opts[:type].nil?
end

# Create an instance for the transfer type and return it
# Creates an instance for the transfer type and return it
#
# type - the transfer type like sftp, ftp, etc.
def get_transfer(type)
type = type[0].upcase + type[1..-1]
try_require(type)
Transfer.const_get(type).new(@remoteopts, @files)
Transfer.const_get(type).new(@options, @files)
end

# Try requiring a transfer type class
Expand All @@ -46,7 +40,7 @@ def try_require(name)
end
protected :try_require

# check if the first file matches service:<service-name>
# Check if the first file matches service:<service-name>
# and try to determine the files based on the service.
#
# first_file the first file given
Expand Down
46 changes: 46 additions & 0 deletions lib/travis-custom-deploy/options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module TravisCustomDeploy

# The available services. Each service has an
# unique id and an array of files or directories to deploy.
SERVICES = {

# Jekyll support
# Usage: service:jekyll
'jekyll' => [ '_site/' ]
}

# The possible options of the different transfer types
OPTIONS = {

'sftp' => [
'host', # host name of the sftp server
'username', # username to connect
'password', # password to connect
'remotedir' # remote dir, for example: /public/
],

'ftp' => [
'host', # host name of the ftp server
'username', # username to connect
'password', # password to connect
'remotedir' # remote dir, for example: /public/
],

'copy' => [
'remotedir' # the destination where to copy the files
]

}

class Options

def self.get_options(transfer_type)
OPTIONS.each do |k,v|
if k == transfer_type
return v
end
end
nil
end
end
end
10 changes: 0 additions & 10 deletions lib/travis-custom-deploy/services.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/travis-custom-deploy/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Transfer

autoload :Sftp, 'travis-custom-deploy/transfer/sftp'
autoload :Ftp, 'travis-custom-deploy/transfer/ftp'
autoload :Copy, 'travis-custom-deploy/transfer/copy'

end

Expand Down
19 changes: 10 additions & 9 deletions lib/travis-custom-deploy/transfer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module Transfer

class Base

attr_reader :remoteopts
attr_reader :options
attr_reader :files

def initialize(remoteopts, files)
@remoteopts = remoteopts
def initialize(options, files)
@options = options
@files = files
prepare_remotedir
check_options(options)
end

# The method which needs to be implemented by subclasses
Expand All @@ -19,12 +19,13 @@ def transfer
raise NotImplementedError
end

# Prepares the remote directory (remote trailing slash)
def prepare_remotedir
@remotedir = @remoteopts[:remotedir]
@remotedir = "" if @remotedir.nil?
@remotedir = @remotedir[0..-2] if @remotedir[-1] == "/"
# The method which needs to be implemented by subclasses
# to check if the remote options are sufficient for the
# defined transfer type
def check_options
raise NotImplementedError
end

end
end
end
12 changes: 12 additions & 0 deletions lib/travis-custom-deploy/transfer/copy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module TravisCustomDeploy

module Transfer

class Copy < Base

def initialize
super(@remoteopts, files)
end
end
end
end
17 changes: 17 additions & 0 deletions lib/travis-custom-deploy/transfer/ftp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module TravisCustomDeploy

module Transfer

# The class which transfers files via the File Transfer protocol (FTP).
class Ftp < Base

def initialize(remoteopts, files)
super(remoteopts, files)
end

def transfer

end
end
end
end
23 changes: 19 additions & 4 deletions lib/travis-custom-deploy/transfer/sftp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,37 @@ module TravisCustomDeploy

module Transfer

# The class which transfers files via the SSH File Transfer Protocol
# (SFTP).
class Sftp < Base

def initialize(remoteopts, files)
super(remoteopts, files)
def initialize(options, files)
super(options, files)
prepare_remotedir
end

# transfers the given files via sftp
def transfer
Net::SFTP.start(@remoteopts[:host], @remoteopts[:username], :password => @remoteopts[:password]) do |sftp|
Net::SFTP.start(@options['host'], @options['username'],
:password => @options['password']) do |sftp|
for e in @files
sftp.upload!(e, "#{@remotedir}/")
end
end
end

def check_options(options)
raise ArgumentError, 'host name must not be nil' if options['host'].nil?
raise ArgumentError, 'username must not be nil' if options['username'].nil?
raise ArgumentError, 'password must not be nil' if options['password'].nil?
end

# Prepares the remote directory (remote trailing slash)
def prepare_remotedir
@remotedir = @options[:remotedir]
@remotedir = "" if @remotedir.nil?
@remotedir = @remotedir[0..-2] if @remotedir[-1] == "/"
end

end
end
end
2 changes: 1 addition & 1 deletion lib/travis-custom-deploy/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module TravisCustomDeploy

VERSION = "0.0.1"
VERSION = "0.0.2"

end
13 changes: 6 additions & 7 deletions test/test_deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ class TestDeployment < Test::Unit::TestCase
context "A deployment" do
setup do
@opts = {
:type => 'sftp',
:host => 'example.org',
:username => 'username',
:password => 'password',
:remotedir => '/public/'
'host' => 'example.org',
'username' => 'username',
'password' => 'password',
'remotedir' => '/public/'
}
end

context "with files" do
setup do
@deployment = Deployment.new(@opts, ['foo', 'bar', 'foo2'])
@deployment = Deployment.new('sftp', @opts, ['foo', 'bar', 'foo2'])
end

should "return file names" do
Expand All @@ -25,7 +24,7 @@ class TestDeployment < Test::Unit::TestCase

context "with service identifier as first file name" do
setup do
@deployment = Deployment.new(@opts, ['service:jekyll'])
@deployment = Deployment.new('sftp', @opts, ['service:jekyll'])
end

should "return file names of service" do
Expand Down
17 changes: 17 additions & 0 deletions test/test_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'helper'

class TestOptions < Test::Unit::TestCase

context "Options" do

context "environment variables" do
setup do
@env = Options.get_options('sftp')
end

should "return sftp options" do
assert_equal ['host', 'username', 'password', 'remotedir'], @env
end
end
end
end

0 comments on commit 8a25dca

Please sign in to comment.