Class: AdminModule::Rake::PpmTasks

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/admin_module/rake/ppm_tasks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name = '', desc = "") {|_self| ... } ⇒ PpmTasks

Returns a new instance of PpmTasks.

Yields:

  • (_self)

Yield Parameters:



26
27
28
29
30
31
32
33
34
35
# File 'lib/admin_module/rake/ppm_tasks.rb', line 26

def initialize(task_name = '', desc = "")
  @valid_actions = ['import', 'export', 'dups', 'list']
  @task_name, @desc = task_name, desc

  @stop_on_exception = true

  yield self if block_given?

  define_task
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



21
22
23
# File 'lib/admin_module/rake/ppm_tasks.rb', line 21

def action
  @action
end

#envObject

Returns the value of attribute env.



17
18
19
# File 'lib/admin_module/rake/ppm_tasks.rb', line 17

def env
  @env
end

#formatObject

Returns the value of attribute format.



24
25
26
# File 'lib/admin_module/rake/ppm_tasks.rb', line 24

def format
  @format
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/admin_module/rake/ppm_tasks.rb', line 18

def name
  @name
end

#pathObject

Returns the value of attribute path.



20
21
22
# File 'lib/admin_module/rake/ppm_tasks.rb', line 20

def path
  @path
end

#stop_on_exceptionObject

Returns the value of attribute stop_on_exception.



23
24
25
# File 'lib/admin_module/rake/ppm_tasks.rb', line 23

def stop_on_exception
  @stop_on_exception
end

#toObject

Returns the value of attribute to.



19
20
21
# File 'lib/admin_module/rake/ppm_tasks.rb', line 19

def to
  @to
end

#valid_actionsObject (readonly)

Returns the value of attribute valid_actions.



22
23
24
# File 'lib/admin_module/rake/ppm_tasks.rb', line 22

def valid_actions
  @valid_actions
end

Class Method Details

.installObject



218
219
220
# File 'lib/admin_module/rake/ppm_tasks.rb', line 218

def install
  new.install
end

Instance Method Details

#assert_env_is_configured(arg) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/admin_module/rake/ppm_tasks.rb', line 153

def assert_env_is_configured arg
  unless AdminModule.configuration.credentials.key? arg
    init_msg = "Have you initialized your config file?\n Try: admin_module config init <filedir>"
    env_msg = "Have you configured your environments?\n Try: admin_module config add env <envname> <url>"
    raise "Unknown environment: #{arg}\n#{init_msg}\n\n#{env_msg}"
  end
end

#assert_provided(value, msg) ⇒ Object



147
148
149
150
151
# File 'lib/admin_module/rake/ppm_tasks.rb', line 147

def assert_provided value, msg
  if value.nil? || value.empty?
    raise msg
  end
end

#commitObject

Execute the task (action)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/admin_module/rake/ppm_tasks.rb', line 78

def commit
  validate_params

  client = AdminModule::Client.new
  client.env = env

  if self.respond_to? action
    self.send(action, client)
    return
  else
    raise "Unknown action - #{action}"
  end

rescue Exception => e
  raise e if stop_on_exception == true
ensure
  client.quit
end

#define_taskObject

:nodoc:



37
38
39
40
41
42
43
# File 'lib/admin_module/rake/ppm_tasks.rb', line 37

def define_task #:nodoc:
  desc @desc
  task(@task_name, required_args_for_action) do |t,args|
    set_vars args
    commit  # Call method to perform when invoked.
  end
end

#dups(client) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/admin_module/rake/ppm_tasks.rb', line 106

def dups client
  result = Array.new
  result = client.ppms.dups
  output_method = "output_as_#{@format}"

  if self.respond_to? output_method
    self.send(output_method, result)
  else
    $stderr << "Invalid format: #{@format}"
  end
end

#export(client) ⇒ Object



122
123
124
# File 'lib/admin_module/rake/ppm_tasks.rb', line 122

def export client
  $stdout << client.ppms.export(path)
end

#import(client) ⇒ Object



118
119
120
# File 'lib/admin_module/rake/ppm_tasks.rb', line 118

def import client
  $stdout << client.ppms.import(path)
end

#installObject

Instance method to generate the tasks; For each environment where we have configured credentials, generate a task for each available action.



229
230
231
232
233
234
235
236
237
238
# File 'lib/admin_module/rake/ppm_tasks.rb', line 229

def install
  AdminModule.configuration.credentials.keys.each do |e|
    valid_actions.each do |action|
      AdminModule::Rake::PpmTasks.new("am:#{e}:ppm:#{action}", "#{action} #{e} ppms") do |t|
        t.env = e
        t.action = action
      end
    end
  end
end

#list(client) ⇒ Object

Actions



101
102
103
104
# File 'lib/admin_module/rake/ppm_tasks.rb', line 101

def list client
  $stdout << client.ppms.list.join("\n")
  $stdout << "\n"
end

#output_as_csv(data) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/admin_module/rake/ppm_tasks.rb', line 200

def output_as_csv data
  if data.count > 0
    $stdout << "Name,ID\n"
  else
    return
  end

  data.each do |dp|
    $stdout << "#{dp[:name]},#{dp[:id]}\n"
  end
end

#output_as_txt(data) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/admin_module/rake/ppm_tasks.rb', line 187

def output_as_txt data
  if data.count > 0
    $stdout << "        Name                ID\n"
    $stdout << '-'*79 << "\n"
  else
    return
  end

  data.each do |dp|
    $stdout << "#{dp[:name]}\t#{dp[:id]}\n"
  end
end

#required_args_for_actionObject

Define the task args, based on the action. Used when programatically generating the tasks.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/admin_module/rake/ppm_tasks.rb', line 166

def required_args_for_action
  args = []

  case action
  when 'import'
    args << :path

  when 'export'
    args << :path

  when 'dups'
    args << :format

  else
    # Noop
  end

  args
end

#set_vars(args) ⇒ Object

Add each arg passed to the task, as an instance variable of the task



49
50
51
52
53
54
55
# File 'lib/admin_module/rake/ppm_tasks.rb', line 49

def set_vars args
  args.each do |arg,val|
    instance_variable_set "@#{arg}", val
  end

  args
end

#validate_paramsObject

Verify we have the needed parameters (arguments) to perform the task action.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/admin_module/rake/ppm_tasks.rb', line 131

def validate_params
  assert_provided env, 'Missing "env"'
  assert_provided action, 'Missing "action"'

  case action
  when 'import'
    assert_provided path, 'Missing "path"'

  when 'export'
    assert_provided path, 'Missing "path"'

  end

  assert_env_is_configured env
end