Class: Volley::Dsl::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/volley/dsl/plan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, o = { }, &block) ⇒ Plan

Returns a new instance of Plan.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/volley/dsl/plan.rb', line 16

def initialize(name, o={ }, &block)
  options     = {
      :name      => name,
      :project   => nil,
      :output    => false,
      :encrypt   => false,
      :remote    => true,
      :pack      => true,
      :pack_type => "tgz",
  }.merge(o)
  @attributes = OpenStruct.new(options)
  raise "project instance must be set" if @attributes.project.nil?

  @name        = name.to_sym
  @project     = options[:project]
  @stopped     = false
  @block       = block
  @files       = []
  @arguments   = { }
  @argv        = []
  @stages      = {
      :pre  => Volley::Dsl::Stage.new(:pre, :plan => self),
      :main => Volley::Dsl::Stage.new(:main, :plan => self),
      :post => Volley::Dsl::Stage.new(:post, :plan => self),
  }
  @stage_order = [:pre, :main, :post]

  instance_eval &block if block_given?

  if @attributes.remote
    argument :descriptor, :required => true, :convert => :descriptor
  else
    argument :descriptor, :convert => :descriptor, :convert_opts => { :partial => true }
  end
  argument :force
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(n, *args) ⇒ Object



108
109
110
111
# File 'lib/volley/dsl/plan.rb', line 108

def method_missing(n, *args)
  Volley::Log.warn "** plan DSL does not support method: #{n} #{args.join(',')}"
  raise "not supported"
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



10
11
12
# File 'lib/volley/dsl/plan.rb', line 10

def arguments
  @arguments
end

#argvObject

Returns the value of attribute argv.



6
7
8
# File 'lib/volley/dsl/plan.rb', line 6

def argv
  @argv
end

#attributesObject (readonly)

Returns the value of attribute attributes.



11
12
13
# File 'lib/volley/dsl/plan.rb', line 11

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/volley/dsl/plan.rb', line 7

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



8
9
10
# File 'lib/volley/dsl/plan.rb', line 8

def project
  @project
end

#stagesObject (readonly)

Returns the value of attribute stages.



9
10
11
# File 'lib/volley/dsl/plan.rb', line 9

def stages
  @stages
end

Instance Method Details

#action(name, stage = :main, &block) ⇒ Object



175
176
177
# File 'lib/volley/dsl/plan.rb', line 175

def action(name, stage=:main, &block)
  @stages[stage].action(name, :plan => self, :stage => stage, &block)
end

#argsObject



113
114
115
# File 'lib/volley/dsl/plan.rb', line 113

def args
  @args = OpenStruct.new(@arguments.inject({ }) { |h, e| (k, v)=e; h[k] = v.value; h })
end

#argument(name, opts = { }, &block) ⇒ Object



166
167
168
# File 'lib/volley/dsl/plan.rb', line 166

def argument(name, opts={ }, &block)
  @arguments[name.to_sym] = Volley::Dsl::Argument.new(name, opts.merge(:plan => self), &block)
end

#branchObject



122
123
124
125
126
# File 'lib/volley/dsl/plan.rb', line 122

def branch
  return args.descriptor.branch if args.descriptor
  return source.branch if publishing?
  nil
end

#call(options = { }) ⇒ Object



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

def call(options={ })
  @mode = @name.to_s =~ /deploy/i ? :deploy : :publish
  Volley::Log.debug "## #{@project.name}:#@name  (#@mode)"
  @origargs = options[:args]
  data      = @origargs

  process_arguments(data)

  raise "descriptor must be specified" if @attributes.remote && !args.descriptor

  Volley::Log.info ">> #{@project.name}:#@name"

  begin
    run_actions
  rescue ExecutionStopped => e
    Volley::Log.debug "stopping execution at #{e.backtrace.first}"
  rescue => e
    Volley::Log.debug "plan#call error: #{e.message} at #{e.backtrace.first}"
    raise e
  end
  [branch, version].join(":")
end

#command(*args) ⇒ Object



224
225
226
227
228
229
# File 'lib/volley/dsl/plan.rb', line 224

def command(*args)
  name = args.join(" ").parameterize.to_sym
  action name do
    plan.shellout(*args)
  end
end

#configObject



162
163
164
# File 'lib/volley/dsl/plan.rb', line 162

def config
  Volley.config
end

#default(&block) ⇒ Object



179
180
181
# File 'lib/volley/dsl/plan.rb', line 179

def default(&block)
  action(:default, :main, &block)
end

#deploying?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/volley/dsl/plan.rb', line 76

def deploying?
  @mode == :deploy
end

#file(file) ⇒ Object



207
208
209
# File 'lib/volley/dsl/plan.rb', line 207

def file(file)
  @files << file
end

#files(*list) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/volley/dsl/plan.rb', line 211

def files(*list)
  list = [*list].flatten
  list.each {|e| file e} if list.count > 0
  #if @files.count > 0 && list.count > 0
  #  Volley::Log.warn "overriding file list"
  #  Volley::Log.debug "files: #{@files.inspect}"
  #  Volley::Log.debug "new: #{list.inspect}"
  #end
  #@files = list if list.count > 0
  #@files
  @files
end

#load(file) ⇒ Object



155
156
157
158
159
160
# File 'lib/volley/dsl/plan.rb', line 155

def load(file)
  Volley::Dsl.file(file)
rescue => e
  Volley::Log.error "failed to load file: #{e.message}: #{file} (#{real})"
  Volley::Log.debug e
end

#log(msg) ⇒ Object



151
152
153
# File 'lib/volley/dsl/plan.rb', line 151

def log(msg)
  Volley::Log.info msg
end

#output(tf = true) ⇒ Object



170
171
172
173
# File 'lib/volley/dsl/plan.rb', line 170

def output(tf=true)
  @attributes.output = tf
  argument :output, :attr => true, :default => tf, :convert => :boolean
end

#publishing?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/volley/dsl/plan.rb', line 80

def publishing?
  @mode == :publish
end

#pull(&block) ⇒ Object



187
188
189
# File 'lib/volley/dsl/plan.rb', line 187

def pull(&block)
  Volley::Dsl::PullAction.new(:pull_dummy, :plan => self, :stage => :main, &block)
end

#push(&block) ⇒ Object



183
184
185
# File 'lib/volley/dsl/plan.rb', line 183

def push(&block)
  Volley::Dsl::PushAction.new(:push_dummy, :plan => self, :stage => :main, &block)
end

#remote(tf) ⇒ Object



117
118
119
120
# File 'lib/volley/dsl/plan.rb', line 117

def remote(tf)
  raise "remote can only be set to true or false" unless [true, false].include?(tf)
  @attributes.remote = tf
end

#run_actionsObject



92
93
94
95
96
97
# File 'lib/volley/dsl/plan.rb', line 92

def run_actions
  @stage_order.each do |stage|
    @current_stage = stage
    @stages[stage].call
  end
end

#shellout(*args) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/volley/dsl/plan.rb', line 231

def shellout(*args)
  require "mixlib/shellout"
  opts    = args.last.is_a?(Hash) ? args.pop : { }
  options = {
      :output  => @attributes.output,
      :prepend => ">> ",
  }.merge(opts)
  command = ::Mixlib::ShellOut.new(*args)
  command.run_command
  command.stdout.lines.each { |l| Volley::Log.info "#{options[:prepend]}#{l}" } if options[:output] && command.stdout
  command.stderr.lines.each { |l| Volley::Log.info "#{options[:prepend]}#{l}" } if options[:output] && command.stderr
  command.error!
  { :out => command.stdout, :err => command.stderr }
end

#stopObject

Raises:



99
100
101
102
# File 'lib/volley/dsl/plan.rb', line 99

def stop
  @stopped = true
  raise ExecutionStopped, "stopped"
end

#stopped?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/volley/dsl/plan.rb', line 104

def stopped?
  @stopped
end

#usageObject



84
85
86
87
88
89
90
# File 'lib/volley/dsl/plan.rb', line 84

def usage
  out = []
  @arguments.each do |n, arg|
    out << arg.usage
  end
  out.join(" ")
end

#versionObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/volley/dsl/plan.rb', line 128

def version
  v = args.descriptor ? args.descriptor.version : nil
  if v.nil? || v == "latest"
    v = begin
      if deploying?
        Volley::Dsl.publisher.latest_version(args.descriptor.project, args.descriptor.branch)
      elsif publishing?
        begin
          source.revision
        rescue Volley::ScmNotConfigured => e
          Volley::Log.debug "failed to get version from source, trying publisher"
          Volley::Dsl.publisher.latest_version(args.descriptor.project, args.descriptor.branch)
        end
      end
    rescue => e
      Volley::Log.debug "failed to get version? #{v.inspect} : #{e.message}"
      #Volley::Log.debug e
      v
    end
  end
  v
end

#volley(plan, options = {}, &block) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/volley/dsl/plan.rb', line 191

def volley(plan, options={}, &block)
  o = {
      :run => plan,
      :plan => self,
      :stage => :main,
      :descriptor => args.descriptor,
      :args => {},
  }.merge(options)
  action = Volley::Dsl::VolleyAction.new("volley-#{plan}", o)
  #if @current_stage == :post
  #  action.call
  #else
    @stages[:main].add action
  #end
end