Sha256: 5a698cf5708a61392f3cd1c5bd278ea05ce2997a4e5eb9633ce9cd66a4102cc8

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require 'helpers-blog'
require 'runeblog'

class RuneBlog::Publishing
  attr_reader :user, :server, :docroot, :path

  BadRemoteLogin = Exception.new("Can't login remotely")
  BadRemotePerms = Exception.new("Bad remote permissions")

  def initialize(*params)
    @blog = RuneBlog.blog
    # Clunky...
    if params.size == 1 && params[0].is_a?(OpenStruct)
      obj = params[0]
      array = obj.to_h.values_at(:user, :server, :docroot, 
                                 :path, :proto)
      @user, @server, @docroot, @path, @proto = *array
    else
      @user, @server, @docroot, @path, @proto = *obj
    end
  end

  def to_h
    {user: @user, server: @server, docroot: @docroot,
     path: @path, proto: @proto}
  end

  def url
    url = "#@proto://#@server/#@path"
  end

  def no_files?(files)
    if files.empty?
      STDSCR.up
      puts fx("\n  No files to publish", :bold)
      true
    else
      false
    end
  end
 
  def publish(files)
    return false if no_files?(files)
#   print " Publishing #{files.size} files... "
#   debug "files = #{files.inspect}"
    dir = "#@docroot/#@path"
    result = system("ssh #@user@#@server -x mkdir -p #{dir}") 
    list = files.join(' ')
    cmd = "scp -r #{list} #@user@#@server:#{dir} >/dev/null 2>/tmp/wtf"
    debug "cmd = #{cmd.inspect}  - see /tmp/wtf"
    result = system(cmd)
    raise PublishError if !result
    dump(files, "#{@blog.view.dir}/last_published")
    true
  end

  def remote_login?
    cmd = "ssh -o BatchMode=yes #@user@#@server -x date >/dev/null 2>&1"
    result = system(cmd)
    return nil unless result
    true
  end

  def remote_permissions?
    dir = "#@docroot/#@path"
    temp = "#@path/__only_testing" 
    try1 = system("ssh -o BatchMode=yes -o ConnectTimeout=1 #@user@#@server -x mkdir -p #{temp} >/dev/null 2>&1")
    return nil unless try1
    try2 = system("ssh -o BatchMode=yes -o ConnectTimeout=1 #@user@#@server -x rmdir #{temp} >/dev/null 2>&1")
    return nil unless try2
    true
  end
end


Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
runeblog-0.1.23 lib/publish.rb
runeblog-0.1.22 lib/publish.rb