577125ed0c4a930404e26469a79a69872e360890
[packages/precise/mcollective.git] / lib / mcollective / generators / agent_generator.rb
1 module MCollective
2   module Generators
3     class AgentGenerator<Base
4
5       attr_accessor :ddl, :content
6
7       def initialize(plugin_name, actions = [],  name = nil, description = nil, author = nil ,
8                      license = nil, version = nil, url = nil, timeout = nil)
9
10         super(name, description, author, license, version, url, timeout)
11         @plugin_name = plugin_name
12         @actions = actions || []
13         @ddl = create_ddl
14         @mod_name = "Agent"
15         @pclass = "RPC::Agent"
16         @content = create_plugin_content
17         @plugin = create_plugin_string
18         write_plugins
19       end
20
21       def create_ddl
22         action_text = ""
23         @actions.each_with_index do |action, i|
24           action_text += "action \"#{action}\", :description => \"%ACTIONDESCRIPTION%\" do\n"
25           action_text += action_help if i == 0
26           action_text += "end\n"
27           action_text += "\n" unless @actions.size == (i + 1)
28         end
29         # Use inherited method to create metadata part of the ddl
30         create_metadata_string + action_text
31       end
32
33       def create_plugin_content
34         content_text = ""
35
36         # Add actions to agent file
37         @actions.each_with_index do |action, i|
38           content_text +=  "%6s%s" % [" ", "action \"#{action}\" do\n"]
39           content_text +=  "%6s%s" % [" ", "end\n"]
40           content_text += "\n" unless @actions.size == (i + 1)
41         end
42         content_text
43       end
44
45       def action_help
46         action_snippet = File.read(File.join(File.dirname(__FILE__), "templates", "action_snippet.erb"))
47         ERB.new(action_snippet).result
48       end
49     end
50   end
51 end