Class: Gluez::Resource

Inherits:
Object show all
Defined in:
lib/gluez/resource.rb

Overview

Resources are the building blocks of gluez. Each resource has a specification which list the attributes, their default values and the actual code to execute. The code to execute is listed as separate steps. A step consist of a chunk of code and a check, which ensures the chunk of code has been executed correctly. Complex checks can be broken down as multiple simple checks. These will be AND’ed later on.

Resources will be executed as root per default. The special attribute user can be set, so that the resource will be executed as this user.

Defined Under Namespace

Classes: Step

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, type, name, &block) ⇒ Resource

Returns a new instance of Resource.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gluez/resource.rb', line 33

def initialize(context, type, name, &block)
  @context = context
  @type = type.to_sym
  @name = name
  @steps = []
  
  @as_user = nil
  
  @notifies = []
  @subscriptions = []
  
  @mandatories = []
  @optionals = []
  
  # self.optional :user, :default => "root"
  self.optional :lazy, :default => false
  
  self.accessor :setup
  self.accessor :clean
end

Instance Attribute Details

#cleanObject

Returns the value of attribute clean.



29
30
31
# File 'lib/gluez/resource.rb', line 29

def clean
  @clean
end

#nameObject (readonly)

The name of this resource



22
23
24
# File 'lib/gluez/resource.rb', line 22

def name
  @name
end

#setupObject

A chunk of code which will be executed before any other code defined as steps



28
29
30
# File 'lib/gluez/resource.rb', line 28

def setup
  @setup
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



31
32
33
# File 'lib/gluez/resource.rb', line 31

def subscriptions
  @subscriptions
end

#typeObject (readonly)

The type of this resource



25
26
27
# File 'lib/gluez/resource.rb', line 25

def type
  @type
end

Instance Method Details

#accessor(name, default = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gluez/resource.rb', line 82

def accessor(name, default=nil)
  self.class.instance_eval do
    define_method(name) do |*args|
      if args.length == 0
        v = instance_variable_get("@#{name}")
        v.nil? ? default : v
      else
        instance_variable_set("@#{name}", args.first)
      end
    end
  end
end

#as_user(user) ⇒ Object



54
55
56
# File 'lib/gluez/resource.rb', line 54

def as_user(user)
  @as_user = user
end

#assign(name) ⇒ Object



103
104
105
# File 'lib/gluez/resource.rb', line 103

def assign(name)
  self.send(name, $gluez.get(name))
end

#function_nameObject

Returns a bash-compatible function name for this resource



219
220
221
222
223
224
225
226
227
# File 'lib/gluez/resource.rb', line 219

def function_name
  "#{user}_#{self.type}_#{self.name}".
    gsub('/', '_').
    gsub(':', '_').
    gsub("@", "_").
    gsub("~", "_").
    gsub(" ", "_").
    gsub(".", "_")     
end

#generate(simulate) ⇒ Object

Returns this resource as a bash function. The function body contains all the steps checks/codes in the order as specified for this resource. Test if the check part of a step is up2date. If not, execute the code of the step. If it is up2date now, continue with the next step, fail with an error otherwise.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gluez/resource.rb', line 121

def generate(simulate)
  g = []
  
  fun = self.function_name
  
  g << "function #{fun} {"
  g << "su -l #{user} -c \"#{self.setup}\"" if self.setup

  if @steps.map{|s| s.checks}.flatten.empty?
    unless simulate
      g << "su -l #{user} -c \"#{@steps.map{|s| s.code}.join(' && ')}\""

      generate_success_or_failure(g, "echo \"[applied] - #{fun}\"") do
        g << "echo \"[not applied] - #{fun}\""
        g << "exit 1"
      end
      
      generate_notify_and_subscribe(g)
    end
  else
    generate_steps_checks(g, @steps)
    generate_success_or_failure(g, "echo \"[up2date] - #{fun}\"") do
      g << "echo \"[not up2date] - #{fun}\""

      if self.clean
        g << "echo \"[clean] - #{fun}\""
        g << "su -l #{user} -c \"#{self.clean}\""
      end

      unless simulate
        y = @steps.length
        if y > 1
          (0..@steps.length-1).to_a.each do |limit|
            generate_steps_checks(g, @steps[0..limit])
            x = limit + 1

            generate_success_or_failure(g, "echo \"[up2date] #{x}/#{y} - #{fun}\"") do
              g << "echo \"[not up2date] #{x}/#{y} - #{fun}\""
              g << "su -l #{user} -c \"#{@steps[limit].code}\""

              generate_success_or_failure(g, "echo \"[applied] #{x}/#{y} - #{fun}\"") do
                g << "echo \"[not applied] #{x}/#{y} - #{fun}\""
                g << "exit 1"
              end
            end
          end
        else
          g << "su -l #{user} -c \"#{@steps.first.code}\""

          generate_success_or_failure(g, "echo \"[applied] - #{fun}\"") do
            g << "echo \"[not applied] - #{fun}\""
            g << "exit 1"
          end
        end

        generate_notify_and_subscribe(g)

      end
    end
  end

  
  g << "}"
  g
end

#generate_notify_and_subscribe(g) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/gluez/resource.rb', line 187

def generate_notify_and_subscribe(g)
  @notifies.each do |run|
    type, name = run

    resource = @context.root.resources.detect{|r| r.type == type && r.name == name}
    raise "resource #{type} #{name} does not exist" unless resource

    g << resource.function_name
  end

  @context.root.resources.select{|r| r.subscriptions.include?([@type, @name])}.each do |resource|
    g << resource.function_name
  end
end

#generate_steps_checks(g, steps) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/gluez/resource.rb', line 202

def generate_steps_checks(g, steps)
  g << steps.map do |step|
    step.checks
  end.flatten.map do |check|
    "su -l #{user} -c \"test #{check}\""
  end.join(" && ")
end

#generate_success_or_failure(g, success) ⇒ Object



210
211
212
213
214
215
216
# File 'lib/gluez/resource.rb', line 210

def generate_success_or_failure(g, success)
  g << "if [[ $? -eq 0 ]]; then"
  g << success
  g << "else"
  yield
  g << "fi"
end

#home_dirObject



62
63
64
# File 'lib/gluez/resource.rb', line 62

def home_dir
  self.user == "root" ? "/root" : "/home/#{self.user}"
end

#mandatory(name, opts = {}) ⇒ Object



72
73
74
75
# File 'lib/gluez/resource.rb', line 72

def mandatory(name, opts={})
  @mandatories << name
  self.accessor(name)
end

#notify(type, name) ⇒ Object



95
96
97
# File 'lib/gluez/resource.rb', line 95

def notify(type, name)
  @notifies << [type, name]
end

#optional(name, opts = {}) ⇒ Object



77
78
79
80
# File 'lib/gluez/resource.rb', line 77

def optional(name, opts={})
  @optionals << name
  self.accessor(name, opts[:default])
end

#steps {|step| ... } ⇒ Object

Creates a new step for this resource. This method is called within the specification of a resource and should not be called after this phase.

Yields:

  • (step)


108
109
110
111
112
113
114
115
116
117
# File 'lib/gluez/resource.rb', line 108

def steps
  step = Gluez::Resource::Step.new
  yield(step)
  
  step.checks.each do |check|
    check.gsub!("\"", "\\\"")
  end
  
  @steps << step
end

#subscribe(type, name) ⇒ Object



99
100
101
# File 'lib/gluez/resource.rb', line 99

def subscribe(type, name)
  @subscriptions << [type, name]
end

#userObject



58
59
60
# File 'lib/gluez/resource.rb', line 58

def user
  @as_user || @context.user
end

#validate!Object



66
67
68
69
70
# File 'lib/gluez/resource.rb', line 66

def validate!
  @mandatories.each do |it|
    raise "no value for mandatory attribute #{it}" if self.send(it).nil?
  end
end