Class: AnsiblePlaybook
- Inherits:
-
Object
- Object
- AnsiblePlaybook
- Defined in:
- modules/ansible/db.rb
Overview
AnsiblePlaybook model implementation
Defined Under Namespace
Classes: AnsiblePlaybookSyntaxError
Constant Summary collapse
- TABLE =
DB Table name
'ansible_playbook'
- FIELDS =
DB Table columns names
%w(uid gid name description body extra_data create_time)
- DB =
Getting table object from DB object
$db[TABLE.to_sym]
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#create_time ⇒ Object
Returns the value of attribute create_time.
-
#description ⇒ Object
Returns the value of attribute description.
-
#extra_data ⇒ Object
Returns the value of attribute extra_data.
-
#gid ⇒ Object
Returns the value of attribute gid.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#uid ⇒ Object
Returns the value of attribute uid.
Class Method Summary collapse
-
.check_syntax(body) ⇒ Object
Checks Playbook Syntax 1.
-
.list ⇒ Object
Returns all AnsiblePlaybook objects from DB.
-
.new_with_id(id, _client = nil) ⇒ AnsiblePlaybook
OpenNebula::PoolElement-like initializer.
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes object from DB.
-
#initialize(**args) ⇒ AnsiblePlaybook
constructor
A new instance of AnsiblePlaybook.
-
#run(host, vars: nil, ione: IONe.new($client)) ⇒ Object
Creates AnsiblePlaybookProcess with given args host -> => ['IP_Address:ssh_port', 'username:password'] vars -> => 'value'.
-
#runnable(vars = {}) ⇒ Object
Returns Playbook body with variables inserted.
-
#sync ⇒ Object
Synchronizes object from DB.
-
#to_hash ⇒ Object
Returns AnsiblePlaybook object as Hash.
-
#update ⇒ Object
Writes object to DB.
-
#vars ⇒ Object
Lists variables from Playbook.
Constructor Details
#initialize(**args) ⇒ AnsiblePlaybook
Returns a new instance of AnsiblePlaybook.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'modules/ansible/db.rb', line 38 def initialize **args args.to_s! if args['id'].nil? then @uid, @gid, @name, @description, @body, @extra_data = args.get(*FIELDS) @uid, @gid, @extra_data = @uid || 0, @gid || 0, @extra_data || {} @extra_data['PERMISSIONS'] = @extra_data['PERMISSIONS'] || { "PERMISSIONS" => "111000000" } r, msg = self.class.check_syntax(@body) raise RuntimeError.new(msg) unless r @create_time = Time.now.to_i allocate else begin @id = args['id'] sync rescue NoMethodError raise "Object not exists" end end raise "Unhandlable, id is nil" if @id.nil? end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def body @body end |
#create_time ⇒ Object
Returns the value of attribute create_time.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def create_time @create_time end |
#description ⇒ Object
Returns the value of attribute description.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def description @description end |
#extra_data ⇒ Object
Returns the value of attribute extra_data.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def extra_data @extra_data end |
#gid ⇒ Object
Returns the value of attribute gid.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def gid @gid end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
28 29 30 |
# File 'modules/ansible/db.rb', line 28 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def name @name end |
#uid ⇒ Object
Returns the value of attribute uid.
29 30 31 |
# File 'modules/ansible/db.rb', line 29 def uid @uid end |
Class Method Details
.check_syntax(body) ⇒ Object
Playbook should be written following next rules:
Checks Playbook Syntax
-
It must be written in ruby-yaml parse-able YAML syntax
-
Playbook must be array (body should start from ' - ')
-
hosts must be equal to <%group%>
-
Using of “local_action” key is restricted
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'modules/ansible/db.rb', line 123 def self.check_syntax body body = YAML.load(body) raise AnsiblePlaybookSyntaxError.new("Playbook must be array (body should start from ' - ')") unless body.class == Array raise AnsiblePlaybookSyntaxError.new("hosts must be equal to <%group%>") unless body.first['hosts'] == "<%group%>" raise AnsiblePlaybookSyntaxError.new("key local_action isn't acceptable") if body.first.has_key? 'local_action' return true, "" rescue Psych::SyntaxError => e return false, e. rescue AnsiblePlaybookSyntaxError => e return false, e. rescue => e return false, 'Unknown error: ' + e. end |
.list ⇒ Object
Returns all AnsiblePlaybook objects from DB
181 182 183 184 185 186 187 188 |
# File 'modules/ansible/db.rb', line 181 def self.list result = DB.all result.map { |pb| pb.to_s! } result.size.times do | i | result[i]['extra_data'] = JSON.parse result[i]['extra_data'] end result end |
.new_with_id(id, _client = nil) ⇒ AnsiblePlaybook
OpenNebula::PoolElement-like initializer
70 71 72 |
# File 'modules/ansible/db.rb', line 70 def self.new_with_id id, _client = nil self.new(id: id) end |
Instance Method Details
#delete ⇒ Object
Deletes object from DB
82 83 84 85 |
# File 'modules/ansible/db.rb', line 82 def delete DB.where(id: @id).delete nil end |
#run(host, vars: nil, ione: IONe.new($client)) ⇒ Object
Creates AnsiblePlaybookProcess with given args host -> => ['IP_Address:ssh_port', 'username:password'] vars -> => 'value'
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'modules/ansible/db.rb', line 145 def run host, vars: nil, ione: IONe.new($client) r, msg = self.class.check_syntax(@body) raise RuntimeError.new(msg) unless r unless vars.nil? then body = YAML.safe_load @body body[0]['vars'].merge! vars @body = YAML.dump body end ione.AnsibleController({ 'host' => host, 'services' => [ runnable ] }) end |
#runnable(vars = {}) ⇒ Object
Returns Playbook body with variables inserted
163 164 165 166 167 168 169 170 171 172 173 |
# File 'modules/ansible/db.rb', line 163 def runnable vars = {} r, msg = self.class.check_syntax(@body) raise RuntimeError.new(msg) unless r unless vars == {} then body = YAML.safe_load @body body[0]['vars'].merge! vars @body = YAML.dump body end return { @name => @body } end |
#sync ⇒ Object
Synchronizes object from DB
75 76 77 78 79 |
# File 'modules/ansible/db.rb', line 75 def sync get_me.each do |var, value| instance_variable_set('@' + var, value) end end |
#to_hash ⇒ Object
Returns AnsiblePlaybook object as Hash
176 177 178 |
# File 'modules/ansible/db.rb', line 176 def to_hash get_me end |
#update ⇒ Object
Writes object to DB
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'modules/ansible/db.rb', line 88 def update r, msg = self.class.check_syntax(@body) raise RuntimeError.new(msg) unless r args = {} FIELDS.each do | var | next if var == 'create_time' value = instance_variable_get(('@' + var).to_sym) value = var == 'extra_data' ? JSON.generate(value) : value args[var.to_sym] = value.nil? ? '' : value end DB.where(id: @id).update(**args) nil end |
#vars ⇒ Object
Lists variables from Playbook
106 107 108 109 110 111 112 113 114 |
# File 'modules/ansible/db.rb', line 106 def vars sync body = YAML.safe_load(@body).first body['vars'] rescue => e if e..split(':').first == 'TypeError' then raise "SyntaxError: Check if here is now hyphens at the playbook beginning. Playbook parse result should be Hash" end end |