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



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



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

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

#dir_type?(file) ⇒ Boolean



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

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

#expand_files(files) ⇒ Object



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

def expand_files(files)
  att = Array.new
  files.each do |attr|
    if text_type?(attr) and (!(attr[1]['related'].nil?) or attr[0].include? "/etc/apt/sources.list")
      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



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

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


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

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



77
78
79
80
81
82
83
84
85
86
# File 'lib/cupper/recipe.rb', line 77

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



88
89
90
91
92
93
94
95
96
97
# File 'lib/cupper/recipe.rb', line 88

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cupper/recipe.rb', line 62

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



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

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


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



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

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



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cupper/recipe.rb', line 219

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


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cupper/recipe.rb', line 185

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



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

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



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

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



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cupper/recipe.rb', line 202

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



41
42
43
# File 'lib/cupper/recipe.rb', line 41

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