Module: ServerRemote::Util

Included in:
Command
Defined in:
lib/server_remote/server_remote.rb

Constant Summary collapse

DEFAULT_PROFILE =
'app'

Instance Method Summary collapse

Instance Method Details

#app_rootObject



80
81
82
83
# File 'lib/server_remote/server_remote.rb', line 80

def app_root
  raise 'app_root not set!' unless @app_root
  @app_root
end

#app_root=(app_root) ⇒ Object



76
77
78
# File 'lib/server_remote/server_remote.rb', line 76

def app_root=(app_root)
  @app_root = app_root
end

#cd_to_app_actionObject



52
53
54
# File 'lib/server_remote/server_remote.rb', line 52

def cd_to_app_action
  "cd #{config[:app_path]}"
end

#configObject



89
90
91
# File 'lib/server_remote/server_remote.rb', line 89

def config
  @config ||= {}
end

#config=(cfg) ⇒ Object



85
86
87
# File 'lib/server_remote/server_remote.rb', line 85

def config=(cfg)
  @config = cfg
end

#config_pathObject



93
94
95
# File 'lib/server_remote/server_remote.rb', line 93

def config_path
  options[:config_path] ? options[:config_path] : File.join(app_root, 'config', 'server_remote.yml')
end

#console_actionObject



48
49
50
# File 'lib/server_remote/server_remote.rb', line 48

def console_action
  "./script/console #{config[:environment]}"
end

#default_options_pathObject



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

def default_options_path
  File.join(*([GEM_ROOT] + %w{config defaults.yml}))
end

#execute(cmd) ⇒ Object



18
19
20
21
# File 'lib/server_remote/server_remote.rb', line 18

def execute(cmd)
  print "--> calling '#{cmd}'\n"
  Kernel.system(cmd)
end

#keyfile_optionObject



28
29
30
# File 'lib/server_remote/server_remote.rb', line 28

def keyfile_option
  "-i #{File.join(app_root, config[:keyfile])} " if config[:keyfile]
end

#load_configObject



97
98
99
100
# File 'lib/server_remote/server_remote.rb', line 97

def load_config
  load_default_config
  load_app_config(config_path)
end

#parse_common_argsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/server_remote/server_remote.rb', line 102

def parse_common_args
  if args.first == '-p'
    args.shift
    p = args.shift
    if p
      config[:profile] = p
    else
      raise "Missing profile argument for -p"
    end
  end
  
  # get around simplecli's usage call when there are no arguments
  # instead of calling the default action
  args << '--nullarg' if args.empty?
end

#remote_command(args) ⇒ Object



66
67
68
69
# File 'lib/server_remote/server_remote.rb', line 66

def remote_command(args)
  args = args.join(' ') if args.respond_to?(:join)
  "#{ssh_command} '#{args}'"
end

#remote_command_in_app(args) ⇒ Object



71
72
73
74
# File 'lib/server_remote/server_remote.rb', line 71

def remote_command_in_app(args)
  args = args.join(' ') if args.respond_to?(:join)
  remote_command("#{cd_to_app_action};#{args}")
end

#scp_commandObject



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

def scp_command
  "scp #{keyfile_option}"
end

#scp_file_argument(arg) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/server_remote/server_remote.rb', line 40

def scp_file_argument(arg)
  if arg and arg[0..0] == ':'
    user_and_host + arg
  else
    arg
  end
end

#ssh_commandObject



32
33
34
# File 'lib/server_remote/server_remote.rb', line 32

def ssh_command
  "ssh -t #{keyfile_option}#{user_and_host}"
end

#tail_action(args) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/server_remote/server_remote.rb', line 56

def tail_action(args)
  if args and !args.empty?
    file = args.join(' ')
  else
    file = "log/#{config[:environment]}.log"
  end

  "tail -n #{config[:tail_initial_lines]} -f #{file}"
end

#user_and_hostObject



23
24
25
26
# File 'lib/server_remote/server_remote.rb', line 23

def user_and_host
  user = "#{config[:user]}@" if config[:user]
  "#{user}#{config[:host]}"
end