Class: Cupper::Recipe

Inherits:
Object
  • Object
show all
Includes:
Entity
Defined in:
lib/cupper/recipe.rb

Overview

Represents the recipe of the cookbook

Constant Summary

Constants included from Entity

Entity::DIR, Entity::FILE

Instance Method Summary collapse

Methods included from Entity

#content, #dir?, #exist?, #file?, #full_path, #render_template, #save

Constructor Details

#initialize(dest_path, collector, erb_file = nil, recipe_name = 'default', recipe_deps = nil) ⇒ Recipe

Returns a new instance of Recipe.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cupper/recipe.rb', line 6

def initialize(dest_path, collector, erb_file = nil, recipe_name = 'default', recipe_deps = nil)
  @recipe_deps  = recipe_deps
  @packages     = Array.new
  @services     = Array.new
  @templates    = Array.new
  @users        = Array.new
  @groups       = Array.new
  @execute      = Array.new
  @links        = Array.new
  @directories  = Array.new
  @files        = Array.new
  @collector    = collector
  super(recipe_name, dest_path, erb_file, nil, '.rb')
end

Instance Method Details

#convert_mode(mode) ⇒ Object

TODO: this should be on ohai plugin



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cupper/recipe.rb', line 71

def convert_mode(mode)
  return 'ERROR' if not mode
  result = case mode.split('').last(9).join # Common modes
           when 'rwxrwxrwx' then '777'
           when 'rwxr-xr-x' then '755'
           when 'rwx------' then '700'
           when 'rw-rw-rw-' then '666'
           when 'rw-r--r--' then '644'
           when 'rw-------' then '600'
           else 'Unknown'
           end
  result
end

#createObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/cupper/recipe.rb', line 21

def create
  @sources_list  = expand_sources_list(@collector.extract 'files')
  @packages = expand_packages(@collector.extract 'packages')
  @services = expand_services(@collector.extract 'services')
  @users    = expand_users(@collector.extract 'users')
  @groups   = expand_groups(@collector.extract 'groups')
  @links    = expand_links(@collector.extract 'files')
  @files    = expand_files(@collector.extract 'files')
  super
end

#dir_type?(file) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/cupper/recipe.rb', line 62

def dir_type?(file)
  file[1]['type'].match('directory')
end

#expand_files(files) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cupper/recipe.rb', line 138

def expand_files(files)
  att = Array.new
  files.each do |attr|
    if text_type?(attr) and !(attr[1]['related'].nil?)
      path = attr[0]
      group = attr[1]['group']
      mode = attr[1]['mode']
      owner = attr[1]['owner']

      att.push(new_file(group, mode, owner, path))
    end
  end
  att
end

#expand_groups(groups) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cupper/recipe.rb', line 126

def expand_groups(groups)
  att = Array.new
  groups.each do |attr|
    grp = attr[0]
    gid = attr[1]['gid']
    members = attr[1]['members']

    att.push(new_group(grp, gid, members))
  end
  att
end


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cupper/recipe.rb', line 96

def expand_links(links)
  att = Array.new
  links.each do |attr|
    if link_type?(attr)
      target = attr[0]
      to = attr[1]['type'].split.last(1).join
      group = attr[1]['group']
      mode = attr[1]['mode']
      owner = attr[1]['owner']

      att.push(new_link(group, mode, owner, target, to))
    end
  end
  att
end

#expand_packages(packages) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/cupper/recipe.rb', line 47

def expand_packages(packages)
  att = Array.new
  packages.each do |attr|
    pkg = attr[0]
    version = attr[1]['version']

    att.push(new_package(pkg,version))
  end
  att
end

#expand_services(services) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/cupper/recipe.rb', line 85

def expand_services(services)
  att = Array.new
  services.each do |attr|
    srv = attr[0]
    action = attr[1]['action']

    att.push(new_service(srv,action))
  end
  att
end

#expand_sources_list(files) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cupper/recipe.rb', line 32

def expand_sources_list(files)
  att = Array.new
  files.each do |attr|
    # TODO: Doesn't works for arch, this should be a plugin responsability
    if attr[0].include?("/etc/apt/sources.list") and (convert_mode(attr[1]['mode']) != 'Unknown') and text_type?(attr)
      path = attr[0]
      group = attr[1]['group']
      mode = attr[1]['mode']
      owner = attr[1]['owner']
      att.push(new_file(group, mode, owner, path))
    end
  end
  att
end

#expand_users(users) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cupper/recipe.rb', line 112

def expand_users(users)
  att = Array.new
  users.each do |attr|
    usr = attr[0]
    uid = attr[1]['uid']
    gid = attr[1]['gid']
    dir = attr[1]['dir']
    shell = attr[1]['shell']

    att.push(new_user(usr, uid, gid, dir, shell))
  end
  att
end

Returns:

  • (Boolean)


58
59
60
# File 'lib/cupper/recipe.rb', line 58

def link_type?(file)
  (file[1]['type'].split.first(2).join(' ').match('symbolic link'))
end

#new_file(group, mode, owner, path, source = '') ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/cupper/recipe.rb', line 223

def new_file(group, mode, owner, path, source='')
  file = Attribute.new
  class << file
    attr_accessor :path
    attr_accessor :source
    attr_accessor :group
    attr_accessor :mode
    attr_accessor :owner
  end
  file.path         = path
  file.source       = source
  file.group        = group
  file.mode         = convert_mode(mode)
  file.owner        = owner
  file
end

#new_group(name, gid, members) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/cupper/recipe.rb', line 210

def new_group(name, gid, members)
  group = Attribute.new
  class << group
    attr_accessor :name
    attr_accessor :gid
    attr_accessor :members
  end
  group.name         = name
  group.gid          = gid
  group.members      = members
  group
end


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cupper/recipe.rb', line 176

def new_link(group, mode, owner, target_file, to)
  link = Attribute.new
  class << link
    attr_accessor :group
    attr_accessor :mode
    attr_accessor :owner
    attr_accessor :target_file
    attr_accessor :to
  end
  link.group        = group
  link.mode         = convert_mode(mode)
  link.owner        = owner
  link.target_file  = target_file
  link.to           = to
  link
end

#new_package(name, version) ⇒ Object

Every attribute object is created dynamic



154
155
156
157
158
159
160
161
162
163
# File 'lib/cupper/recipe.rb', line 154

def new_package(name, version)
  package = Attribute.new
  class << package
    attr_accessor :name
    attr_accessor :version
  end
  package.name = name
  package.version = version
  package
end

#new_service(name, action) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/cupper/recipe.rb', line 165

def new_service(name, action)
  service = Attribute.new
  class << service
    attr_accessor :name
    attr_accessor :action
  end
  service.name = name
  service.action = action
  service
end

#new_user(name, uid, gid, dir, shell) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cupper/recipe.rb', line 193

def new_user(name, uid, gid, dir, shell)
  user = Attribute.new
  class << user
    attr_accessor :name
    attr_accessor :uid
    attr_accessor :gid
    attr_accessor :dir
    attr_accessor :shell
  end
  user.name         = name
  user.uid          = uid 
  user.gid          = gid
  user.dir          = dir
  user.shell        = shell 
  user
end

#text_type?(file) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/cupper/recipe.rb', line 66

def text_type?(file)
  file[1]['type'].match('text') or file[1]['type'].match('ASCII')
end