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
20
# 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
  @files_path   = "#{dest_path.chomp("recipes")}/files"
  @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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cupper/recipe.rb', line 42

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'
           when 'r--r-----' then '440'
           else 'Unknown'
           end
  result
end

#createObject



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

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

#expand_files(files) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cupper/recipe.rb', line 140

def expand_files(files)
  att = Array.new
  files.each do |attr|
    if valid_cookbook_file?(attr)
      path = attr[0]
      source = attr[0]
      group = attr[1]['group']
      mode = attr[1]['mode']
      owner = attr[1]['owner']
      att.push(new_file(group, mode, owner, path,source))

      # Related file
      source = attr[0].split('/').last
      content = attr[1]['content']
      full_path = attr[0]
      CookbookFile.new(@files_path, source, content, 'cookbook_file', full_path).create
    end
  end
  att
end

#expand_groups(groups) ⇒ Object



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

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


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

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



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

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
95
# File 'lib/cupper/recipe.rb', line 85

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

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

#expand_sources_list(files) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cupper/recipe.rb', line 58

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 text_type?(attr)
      path = attr[0]
      source = attr[0]
      group = attr[1]['group']
      mode = attr[1]['mode']
      owner = attr[1]['owner']
      att.push(new_file(group, mode, owner, path,source))
    end
  end
  att
end

#expand_users(users) ⇒ Object



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

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']
    manage_home = if dir.include?("home") then "true" else "false" end

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

Returns:

  • (Boolean)


33
34
35
# File 'lib/cupper/recipe.rb', line 33

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

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cupper/recipe.rb', line 246

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



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cupper/recipe.rb', line 233

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


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/cupper/recipe.rb', line 197

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



173
174
175
176
177
178
179
180
181
182
# File 'lib/cupper/recipe.rb', line 173

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, provider) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cupper/recipe.rb', line 184

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

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/cupper/recipe.rb', line 214

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

#sensible_files?(attr) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/cupper/recipe.rb', line 168

def sensible_files?(attr)
  !!(Config.all_sensibles.find { |file| attr[0].match Regexp.new("^#{file}$") })
end

#text_type?(file) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cupper/recipe.rb', line 37

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

#valid_cookbook_file?(attr) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
# File 'lib/cupper/recipe.rb', line 161

def valid_cookbook_file?(attr)
  (text_type?(attr) and
  !sensible_files?(attr) and
  (!(attr[1]['related'].nil?) or
  attr[0].include? "/etc/apt/sources.list"))
end