Class: Statistrano::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/statistrano/remote.rb,
lib/statistrano/remote/file.rb

Overview

a remote is a databag of config specific for an individual target for deployment including it’s own ssh connection to it’s target server

Defined Under Namespace

Classes: File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Remote

Returns a new instance of Remote.

Raises:

  • (ArgumentError)


12
13
14
15
# File 'lib/statistrano/remote.rb', line 12

def initialize config
  @config = config
  raise ArgumentError, "a hostname is required" unless config.hostname
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/statistrano/remote.rb', line 10

def config
  @config
end

Instance Method Details

#create_remote_dir(path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/statistrano/remote.rb', line 53

def create_remote_dir path
  unless path[0] == "/"
    raise ArgumentError, "path must be absolute"
  end

  Log.info "Setting up directory at '#{path}' on #{config.hostname}"
  resp = run "mkdir -p -m #{config.dir_permissions} #{path}"
  unless resp.success?
    Log.error "Unable to create directory '#{path}' on #{config.hostname}",
              resp.stderr
    abort()
  end
end

#doneObject



49
50
51
# File 'lib/statistrano/remote.rb', line 49

def done
  session.close_session
end

#rsync_to_remote(local_path, remote_path) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/statistrano/remote.rb', line 67

def rsync_to_remote local_path, remote_path
  local_path  = local_path.chomp("/")
  remote_path = remote_path.chomp("/")

  Log.info "Syncing files from '#{local_path}' to '#{remote_path}' on #{config.hostname}"

  time_before = Time.now
  resp = run_local "rsync #{rsync_options} " +
                   "-e ssh #{local_path}/ " +
                   "#{host_connection}:#{remote_path}/"
  time_after = Time.now
  total_time = (time_after - time_before).round(2)

  if resp.success?
    Log.info :success, "Files synced to remote on #{config.hostname} in #{total_time}s"
  else
    Log.error "Error syncing files to remote on #{config.hostname}",
              resp.stderr
  end

  resp
end

#run(command) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/statistrano/remote.rb', line 33

def run command
  if config.verbose
    Log.info :"#{config.hostname}", "running cmd: #{command}"
  end

  session.run command
end

#run_local(command) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/statistrano/remote.rb', line 41

def run_local command
  if config.verbose
    Log.info :local, "running cmd: #{command}"
  end

  Shell.run_local command
end

#test_connectionObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/statistrano/remote.rb', line 17

def test_connection
  Log.info "testing connection to #{config.hostname}"

  resp = run 'whoami'
  done

  if resp.success?
    Log.info "#{config.hostname} says \"Hello #{resp.stdout.strip}\""
    return true
  else
    Log.error "connection failed for #{config.hostname}",
              resp.stderr
    return false
  end
end