Class: AnsiblePlaybook

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ AnsiblePlaybook

Returns a new instance of AnsiblePlaybook

Parameters:

Options Hash (**args):

  • uid (Fixnum)
    • Playbooks owner user ID

  • gid (Fixnum)
    • Playbooks group ID

  • name (String)
    • Playbooks name

  • description (String)
    • Playbooks description(only latin supported)

  • body (String)
    • Playbooks YAML written body

  • extra_data (Hash)
    • Any data you want to specify, usually only PERMISSIONS are mandatory



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

#bodyObject

Returns the value of attribute body



29
30
31
# File 'modules/ansible/db.rb', line 29

def body
  @body
end

#create_timeObject

Returns the value of attribute create_time



29
30
31
# File 'modules/ansible/db.rb', line 29

def create_time
  @create_time
end

#descriptionObject

Returns the value of attribute description



29
30
31
# File 'modules/ansible/db.rb', line 29

def description
  @description
end

#extra_dataObject

Returns the value of attribute extra_data



29
30
31
# File 'modules/ansible/db.rb', line 29

def extra_data
  @extra_data
end

#gidObject

Returns the value of attribute gid



29
30
31
# File 'modules/ansible/db.rb', line 29

def gid
  @gid
end

#idObject (readonly)

Returns the value of attribute id



28
29
30
# File 'modules/ansible/db.rb', line 28

def id
  @id
end

#nameObject

Returns the value of attribute name



29
30
31
# File 'modules/ansible/db.rb', line 29

def name
  @name
end

#uidObject

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

Note:

Playbook should be written following next rules:

Checks Playbook Syntax

  1. It must be written in ruby-yaml parse-able YAML syntax

  2. Playbook must be array (body should start from ' - ')

  3. hosts must be equal to <%group%>

  4. Using of “local_action” key is restricted

Parameters:

  • body (String)
    • Playbooks body written in YAML



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'modules/ansible/db.rb', line 106

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.message
rescue AnsiblePlaybookSyntaxError => e
    return false, e.message
rescue => e
    return false, 'Unknown error: ' + e.message
end

.listObject

Returns all AnsiblePlaybook objects from DB



161
162
163
164
165
166
167
168
# File 'modules/ansible/db.rb', line 161

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

Instance Method Details

#deleteObject

Deletes object from DB



69
70
71
72
# File 'modules/ansible/db.rb', line 69

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'

Examples:

Params template:

Parameters:

  • host (Hash)
    • host or hosts, whereto Playbook will be deployed, see example

  • vars (Hash)
    • variables for playbook, see example

  • ione (IONe)
    • IONe client

Raises:

  • (RuntimeError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'modules/ansible/db.rb', line 127

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.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

Raises:

  • (RuntimeError)


144
145
146
147
148
149
150
151
152
153
154
# File 'modules/ansible/db.rb', line 144

def runnable vars={}
    r, msg = self.class.check_syntax(@body)
    raise RuntimeError.new(msg) unless r

    unless vars == {} then
        body = YAML.load @body
        body[0]['vars'].merge! vars
        @body = YAML.dump body
    end
    return { @name => @body }
end

#syncObject

Synchronizes object from DB



62
63
64
65
66
# File 'modules/ansible/db.rb', line 62

def sync
    get_me.each do |var, value|
        instance_variable_set('@' + var, value)
    end
end

#to_hashObject

Returns AnsiblePlaybook object as Hash



156
157
158
# File 'modules/ansible/db.rb', line 156

def to_hash
    get_me
end

#updateObject

Writes object to DB

Raises:

  • (RuntimeError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'modules/ansible/db.rb', line 74

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

#varsObject

Lists variables from Playbook



90
91
92
93
94
95
96
97
98
# File 'modules/ansible/db.rb', line 90

def vars
    sync
    body = YAML.load(@body).first
        body['vars']
rescue => e
    if e.message.split(':').first == 'TypeError' then
        raise "SyntaxError: Check if here is now hyphens at the playbook beginning. Playbook parse result should be Hash"
    end
end