In Files

Parent

Included Modules

Class/Module Index [+]

Quicksearch

DO::Server

Attributes

host[R]
name[R]
options[R]
user[R]

Public Class Methods

new(name, host, user, options={}) click to toggle source

Initialize a new DO Server

name

is a shortcut useful in rake tasks

host

is the host where we connect

user

the user of our server

options

an hash of options used by ssh/sftpd, where normally we provide :keys => [‘path/to/key.pem’]

Examples:

srv1 = DO::Server.new(:srv1, 'srv1.lipsiasoft.biz', 'root', :keys => %w[/path/to/key.pem]
# File lib/do/server.rb, line 21
def initialize(name, host, user, options={})
 @name, @host, @user, @options = name, host, user, options
end

Public Instance Methods

append(pattern, file, where=:bottom) click to toggle source

Append text into a given file in a specified location

Locations can be:

:top, :start

Add your text before the old content

:bottom, :end

Append your text at bottom of your file

Examples:

append "By default Im at bottom", "/tmp/file"
append "Im on top", "/tmp/file", :top
# File lib/do/server.rb, line 201
def append(pattern, file, where=:bottom)
  was = sftp.file.open(file, "r") { |f| f.read }

  if was.include?(pattern)
    log "'%s' already match your pattern" % file
    return false
  else
    replacement = case where
      when :top, :start  then pattern+was
      when :bottom, :end then was+pattern
      else raise "%s is not a valid, available values are (:top, :start, :bottom, :end)" % where.inspect
    end
  end

  log "append to '%s' in '%s'" % [where, file]
  sftp.file.open(file, "w") { |f| f.write(replacement) }
end
close() click to toggle source

Method used to close the ssh connection

# File lib/do/server.rb, line 58
def close
  ssh.close if @_ssh
end
download(from, to, options={}) click to toggle source

Download a file o a directory from a remote location to a local location As for upload we can download an entire directory providing

:recursive => true

Examples

download(/tmp/file, /my/file)
get(/tmp/dir, /my, :recursive => true)
# File lib/do/server.rb, line 144
def download(from, to, options={})
  log "download from '%s' to '%s'" % [from, to]
  sftp.download!(from, to)
end
Also aliased as: get
exist?(file) click to toggle source

Returns true if a given file exist on the remote server

# File lib/do/server.rb, line 107
def exist?(file)
  run("test -e #{file} && echo True", :silent => true) == "True"
end
get(from, to, options={}) click to toggle source
Alias for: download
gsub(pattern, replacement, file) click to toggle source
Alias for: replace
log(text="", new_line=true) click to toggle source

Method used to print a formatted version of our commands using DO::Server::DO_LOGGER_FORMAT, by default we have a nice colored version like:

srv1@root ~ # ls -al

If you don’t like colors or our format feel free to edit:

Examples:

DO::Server::DO_LOGGER_FORMAT = "%s@%s$ %s"
# File lib/do/server.rb, line 37
def log(text="", new_line=true)
  super(DO_LOGGER_FORMAT % [user, name, text], new_line)
end
read(file) click to toggle source

Return the content of a given file

# File lib/do/server.rb, line 114
def read(file)
  run("cat #{file}", :silent => true)
end
replace(pattern, replacement, file) click to toggle source

Replace a pattern with text in a given file.

Pattern can be:

  • string

  • regexp

  • symbol (:all, :any, :everything) => replace all content

Examples

replace :all, my_template, "/root/.gemrc"
replace /^motd/, "New motd", "/etc/motd"
replace "Old motd, "New motd", "/etc/motd"
# File lib/do/server.rb, line 164
def replace(pattern, replacement, file)
  was = sftp.file.open(file, "r") { |f| f.read }
  found = case pattern
    when :all, :any, :everything
      log "replace \e[1m%s\e[0m in '%s'" % [pattern, file]
      true
    when Regexp
      log "replace \e[1m%s\e[0m in '%s'" % [pattern.inspect, file]
      replacement = was.gsub(pattern, replacement)
      was =~ pattern
    when String
      log "replace \e[1m%s\e[0m in '%s'" % ["String", file]
      replacement = was.gsub(pattern, replacement)
      was.include?(pattern)
    else raise "%s is not a valid. You can use a String, Regexp or :all, :any and :everything" % pattern.inspect
  end

  if found
    sftp.file.open(file, "w") { |f| f.write replacement }
  else
    log "\e[31m '%s' does not include your \e[1mpattern\e[0m" % file unless was =~ pattern
  end
end
Also aliased as: gsub
run(*args) click to toggle source

Run commands on remote server

Examples:

run 'ls -al'
run 'ls', '-al'
run 'mysqladmin -u root -p password 'new', :input => 'oldpassword'
# File lib/do/server.rb, line 70
def run(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  cmd = args.join(" ")
  if options[:as]
    if options[:as] == 'root'
      cmd = "sudo #{cmd.gsub(/'/, "\'")}"
    else
      cmd = "su #{options[:as]} -c '#{cmd.gsub(/'/, "\'")}'"
    end
  end
  log cmd
  result = ""
  ssh.open_channel do |channel|
    channel.request_pty do |c, success|
      raise "could not request pty" unless success
      channel.exec cmd
      channel.on_data do |c_, data|
        result << data
        DO_LOGGER.print(data) unless options[:silent]
        if options[:input]
          match = options[:match] || /password/
          if data =~ match
            options[:input] += "\n" if options[:input][-1] != \n\
            channel.send_data(options[:input])
            DO_LOGGER.puts(options[:input]) unless options[:silent] || data =~ /password/
          end
        end
      end
    end
  end
  ssh.loop
  result.chomp
end
sftp() click to toggle source

The sftp connection used to perform uploads, downloads

# File lib/do/server.rb, line 51
def sftp
  @_sftp ||= Net::SFTP.start(host, user, options)
end
ssh() click to toggle source

This is the ssh connection

# File lib/do/server.rb, line 44
def ssh
  @_ssh ||= Net::SSH.start(host, user, options)
end
up(from, to, options={}) click to toggle source
Alias for: upload
upload(from, to, options={}) click to toggle source

Upload a file or directory from a local location to the remote location When you need to upload a directory you need to provide:

:recursive => true

Examples

upload(/my/file, /tmp/file)
up(/my/dir, /tmp, :recursive => true)
# File lib/do/server.rb, line 128
def upload(from, to, options={})
  log "upload from '%s' to '%s'" % [from, to]
  sftp.upload!(from, to)
end
Also aliased as: up

[Validate]

Generated with the Darkfish Rdoc Generator 2.