default.rb 3.23 KB
Newer Older
Victor Penso's avatar
Victor Penso committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Cookbook Name:: base
# Recipe:: package
#
# Author:: Victor Penso
#
# Copyright:: 2017
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

21
resource_list = %w(
Victor Penso's avatar
Victor Penso committed
22
23
  group
  user
24
25
26
27
28
  directory
  file
  remote_file
  link
  template
Victor Penso's avatar
Victor Penso committed
29
30
  apt_repository
  apt_update
Victor Penso's avatar
Victor Penso committed
31
  apt_package
Victor Penso's avatar
Victor Penso committed
32
  yum_repository
Victor Penso's avatar
Victor Penso committed
33
  yum_package
Victor Penso's avatar
Victor Penso committed
34
  package
Victor Penso's avatar
Victor Penso committed
35
36
  git
  subversion
Victor Penso's avatar
Victor Penso committed
37
38
  execute
  bash
Victor Penso's avatar
Victor Penso committed
39
  script
Victor Penso's avatar
Victor Penso committed
40
  service
Victor Penso's avatar
Victor Penso committed
41
  systemd_unit
Victor Penso's avatar
Victor Penso committed
42
43
  route
  mount
44
45
46
47
48
49
50
)

if not node['base']['resources'].empty?
  resource_list += node['base']['resources']
end

resource_list.each do |resource|
Victor Penso's avatar
Victor Penso committed
51
52
53
54

  next unless node.has_key? resource
  next if node[resource].empty?

Victor Penso's avatar
Victor Penso committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  # Ignore platform dependent resources
  case resource
  when 'apt_repository'
  when 'apt_update'
  when 'apt_package'
    next unless node['platform'] == 'debian'
  when 'yum_repository'
  when 'yum_package'
    next unless node['platform'] == 'centos'
  end

  # Convenience for package deployment
  if %w(apt_package yum_package package).include? resource
    if node[resource].is_a? Array
      package node[resource]
      next
    end
Victor Penso's avatar
Victor Penso committed
72
73
74
75
76
  end

  node[resource].each do |name,conf|

    public_send(resource, name) do
Victor Penso's avatar
Victor Penso committed
77

Victor Penso's avatar
Victor Penso committed
78
      conf.each do |key,value|
Victor Penso's avatar
Victor Penso committed
79
      
Victor Penso's avatar
Victor Penso committed
80
        case key
Victor Penso's avatar
Victor Penso committed
81

Victor Penso's avatar
Victor Penso committed
82
        when 'content'
Victor Penso's avatar
Victor Penso committed
83
          if resource.eql? 'file'
Victor Penso's avatar
Victor Penso committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

            # Disable the header comment by attribute 
            banner = if not node[resource][name].has_key? 'banner' 
                       true
                     else
                       node[resource][name]['banner']
                     end

            # Add a banner to indicate that the written file is
            # managed by Chef
            if banner 
              value = "
                #
                # DO NOT CHANGE THIS FILE MANUALLY!
                #
                # This file is managed by the Chef configuration management system
                #
              #{value}
              "
            end

Victor Penso's avatar
Victor Penso committed
105
          end
Victor Penso's avatar
Victor Penso committed
106

Victor Penso's avatar
Victor Penso committed
107
108
109
          value = value.gsub(/^ */,'')
          value = value.split("\n")
          value = value[1..-1] if value[0] =~ /^$/
Victor Penso's avatar
Victor Penso committed
110
111
          value = value.join("\n") << "\n"
          send(key,value)
Victor Penso's avatar
Victor Penso committed
112
        
113
        when 'template','not_if','only_if'
Victor Penso's avatar
Victor Penso committed
114
          send(key, *value)
Victor Penso's avatar
Victor Penso committed
115

116
117
118
119
120
121
122
123
124
125
126
127
128
        when 'notifies','subscribes'
          # nested arrays indicate multiple notifies/subscribes
          if value[0].kind_of? Array
            # loop over the notifies/subscribes
            value.each do |sub_value|
              # and send them individually
              send(key, *sub_value)
            end
          # a single notifies/subscribes
          else
            send(key, *value)
          end

Victor Penso's avatar
Victor Penso committed
129
130
131
132
133
        # Ignore the following keys...
        when 'banner'
          next

        # Pass all attributes as resource properties by default
Victor Penso's avatar
Victor Penso committed
134
135
        else
          send(key,value)
Victor Penso's avatar
Victor Penso committed
136
        end
Victor Penso's avatar
Victor Penso committed
137

Victor Penso's avatar
Victor Penso committed
138
      end
Victor Penso's avatar
Victor Penso committed
139

Victor Penso's avatar
Victor Penso committed
140
    end
Victor Penso's avatar
Victor Penso committed
141

Victor Penso's avatar
Victor Penso committed
142
143
144
  end

end