Class: Remote::App

Inherits:
Object
  • Object
show all
Defined in:
lib/remote/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



5
6
7
8
9
10
11
# File 'lib/remote/app.rb', line 5

def initialize(options={})
  if options[:config]
    @config_file = [options[:config]].flatten
  end

  self.extend Printer  if options[:console]
end

Instance Method Details

#configObject

Returns the configuration hash.



24
25
26
27
# File 'lib/remote/app.rb', line 24

def config
  verify_config
  @config ||= YAML::load_file(config_file)
end

#config_fileObject

Returns the config file location.



14
15
16
# File 'lib/remote/app.rb', line 14

def config_file
  config_file_locations.detect { |f| File.exists? (f) }
end

#config_file_locationsObject

Returns a list of where configuration files are expected to be present.



19
20
21
# File 'lib/remote/app.rb', line 19

def config_file_locations
  @config_file || ['config/remotes.yml', 'remotes.yml']
end

#help(cmd = 'remote') ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/remote/app.rb', line 74

def help(cmd='remote')
  log "Usage: #{cmd} <server>"
  log "       Opens a console session in the given server."
  log ""
  log "Usage: #{cmd} <server> <command>"
  log "       Executes the given command in the given server."
  log ""
  log "Usage: remote <svr1>,<svr2> <command>"
  log "       Executes the given command in multiple servers."
  log ""
  log "Usage: remote --list"
  log "       Lists available servers."
  log ""
  log "Configuration"
  log "-------------"
  log ""
  log "Servers are defined in a config file. Use `#{cmd} --sample` to"
  log "create a sample config file."
  log ""

  if config_file.nil?
    log "Config files are searched for in:"
    log "  " + config_file_locations.join(", ")
  else
    log "Your configuration file is in #{config_file}."
  end

  log ""
  log "Examples"
  log "--------"
  log ""
  log "1) Executes 'irb -r./init' in the server called 'live'."
  log "   #{cmd} live irb -r./init"
  log ""
  log "2) Starts a console for the 'live' server."
  log "   #{cmd} live"
  log ""
end

#listObject



36
37
38
# File 'lib/remote/app.rb', line 36

def list
  servers.keys.each { |name| log "  #{name}" }
end

#run(to, *cmd) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/remote/app.rb', line 40

def run(to, *cmd)
  verify_config
  [to].flatten.each do |server_name|
    svr = servers[server_name]
    if svr.nil?
      log "Unknown server '#{server_name}'. Available servers are:"
      list
      log "See #{config_file} for more information."
      exit
    end

    command = svr.to_cmd(*cmd)
    what = cmd.any? ? cmd.join(' ') : 'console'
    status svr.to_s, what
    system command
  end
end

#serversObject



29
30
31
32
33
34
# File 'lib/remote/app.rb', line 29

def servers
  return @servers  unless @servers.nil?
  @servers = Hash.new
  config.each { |name, data| @servers[name] = Server.new(name, data) }
  @servers
end

#write_sampleObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/remote/app.rb', line 58

def write_sample
  unless config_file.nil?
    log "A configuration file already exists in #{config_file}."
    return
  end

  fname = config_file_locations.last
  begin
    File.open(fname, 'w') { |f| f.write(sample_data) }
    log "Created the file #{fname}."
    log "Edit this file with your list of servers, then type 'remote <yourserver>' to try it out."
  rescue => e
    log "Error: unable to save to #{fname}."
  end
end