Class: Sunshine::DefaultCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/default.rb

Overview

Default sunshine behavior when no command is passed. Outputs help.

Direct Known Subclasses

ListCommand, RunCommand

Class Method Summary collapse

Class Method Details

.build_response(status, data) ⇒ Object

Build a Sunshine command response



202
203
204
# File 'lib/commands/default.rb', line 202

def self.build_response status, data
  {'status' => status, 'data' => data}.to_yaml
end

.copy_middleware(path) ⇒ Object

Copy middleware to specified location.



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

def self.copy_middleware path
  middleware_dir = "#{Sunshine::ROOT}/templates/sunshine/middleware/."

  FileUtils.cp_r middleware_dir, path

  puts "Copied Sunshine middleware to #{path}"
end

.copy_rakefile(path) ⇒ Object

Copy template rakefile to specified location.



29
30
31
32
33
34
35
# File 'lib/commands/default.rb', line 29

def self.copy_rakefile path
  template_rakefile = "#{Sunshine::ROOT}/templates/sunshine/sunshine.rake"

  FileUtils.cp template_rakefile, path

  puts "Copied Sunshine template rakefile to #{path}"
end

.exec(argv, config) ⇒ Object

Takes an array and a hash, runs the command and returns:

true: success
false: failed
exitcode:
  code == 0: success
  code != 0: failed

and optionally an accompanying message.



17
18
19
20
21
22
23
# File 'lib/commands/default.rb', line 17

def self.exec argv, config

  copy_rakefile(config['rakefile'])     if config.has_key? 'rakefile'
  copy_middleware(config['middleware']) if config.has_key? 'middleware'

  return true
end

.opt_parser(options = nil) ⇒ Object

Base option parser constructor used by all commands.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/commands/default.rb', line 53

def self.opt_parser options=nil
  OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = Sunshine::VERSION
    opt.release = nil

    yield opt if block_given?

    if options
      opt.on('-R', '--require lib1,lib2', Array,
             'Require a library or gem.') do |value|
        options['require'] = value
      end

      opt.on('-S', '--sudo [USER]',
             'Run remote commands using sudo or sudo -u USER.') do |value|
        options['sudo'] = value || true
      end
    end
  end
end

.parse_args(argv) ⇒ Object

Returns the main sunshine help when no arguments are passed.



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/commands/default.rb', line 79

def self.parse_args argv
  options = {}

  opts = opt_parser do |opt|
    opt.banner = <<-EOF

Sunshine is an object oriented deploy tool for rack applications. 

  Usage:
#{opt.program_name} -h/--help
#{opt.program_name} -v/--version
#{opt.program_name} command [options...] [arguments...]

  Examples:
#{opt.program_name} deploy deploy_script.rb
#{opt.program_name} restart -r [email protected],[email protected] myapp
#{opt.program_name} list --health -r [email protected] myapp myotherapp
#{opt.program_name} list --status myapp

  Commands:
add       Register an app with #{opt.program_name}
deploy    Run a deploy script
list      Display deployed apps
restart   Restart a deployed app
rm        Unregister an app with #{opt.program_name}
run       Run a Sunshine ruby file
script    Run an app script
start     Start a deployed app
stop      Stop a deployed app

  Options:
    EOF

    opt.on('--rakefile [PATH]',
           'Copy the Sunshine template rakefile.') do |path|
      options['rakefile'] = path || File.join(Dir.pwd, "sunshine.rake")
    end

    opt.on('--middleware [PATH]',
           'Copy Sunshine rack middleware files.') do |path|
      options['middleware'] =
        path || File.join(Dir.pwd, ".")
    end

    opt.separator nil
    opt.separator "For more help on sunshine commands, "+
                  "use '#{opt.program_name} COMMAND --help'"
    opt.separator nil
  end


  opts.parse! argv

  if options.empty?
    puts opts
    exit 1
  end

  options
end

.parse_remote_args(argv, &block) ⇒ Object

Parse arguments for a command that acts remotely.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/commands/default.rb', line 144

def self.parse_remote_args argv, &block
  options = {}

  opts = opt_parser(options) do |opt|
    opt.separator nil
    opt.separator "Options:"

    yield(opt, options) if block_given?

    opt.on('-u', '--user USER',
           'User to use for ssh login. Use with -r.') do |value|
      options['user'] = value
    end

    opt.on('-r', '--remote server1,server2', Array,
           'Run on one or more remote servers.') do |servers|
      options['servers'] = servers
    end

    formats = %w{txt yml json}
    opt.on('-f', '--format FORMAT', formats,
           "Set the output format (#{formats.join(', ')})") do |format|
      options['format'] = "#{format}_format".to_sym
    end

    opt.on('-v', '--verbose',
           'Run in verbose mode.') do
      options['verbose'] = true
    end
  end

  opts.parse! argv


  if options['servers']
    options['servers'].map! do |host|
      RemoteShell.new host, :user => options['user']
    end
  else
    options['servers'] = [Sunshine.shell]
  end

  options['format'] ||= :txt_format


  if options['sudo']
    options['servers'].each do |ds|
      ds.sudo = options['sudo']
    end
  end

  options
end