crafterm / sprinkle

Sprinkle is a software provisioning tool you can use to build remote servers with. eg. to install a Rails, or Sinatra stack on a brand new slice directly after its been created

This URL has Read+Write access

sprinkle / lib / sprinkle / package.rb
100644 299 lines (252 sloc) 9.682 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
module Sprinkle
  # = Packages
  #
  # A package defines one or more things to provision onto the server.
  # There is a lot of flexibility in a way a package is defined but
  # let me give you a basic example:
  #
  # package :ruby do
  # description 'Ruby MRI'
  # version '1.8.6'
  # apt 'ruby'
  #
  # verify { has_executable 'ruby' }
  # end
  #
  # The above would define a package named 'ruby' and give it a description
  # and explicitly say its version. It is installed via apt and to verify
  # the installation was successful sprinkle will check for the executable
  # 'ruby' being availble. Pretty simple, right?
  #
  # <b>Note:</b> Defining a package does not INSTALL it. To install a
  # package, you must require it in a Sprinkle::Policy block.
  #
  # == Pre-Requirements
  #
  # Most packages have some sort of pre-requisites in order to be installed.
  # Sprinkle allows you to define the requirements of the package, which
  # will be installed before the package itself. An example below:
  #
  # package :rubygems do
  # source 'http://rubyforge.org/rubygems.tgz'
  # requires :ruby
  # end
  #
  # In this case, when rubygems is being installed, Sprinkle will first
  # provision the server with Ruby to make sure the requirements are met.
  # In turn, if ruby has requirements, it installs those first, and so on.
  #
  # == Verifications
  #
  # Most of the time its important to know whether the software you're
  # attempting to install was installed successfully or not. For this,
  # Sprinkle provides verifications. Verifications are one or more blocks
  # which define rules with which Sprinkle can check if it installed
  # the package successfully. If these verification blocks fail, then
  # Sprinkle will gracefully stop the entire process. An example below:
  #
  # package :rubygems do
  # source 'http://rubyforge.org/rubygems.tgz'
  # requires :ruby
  #
  # verify { has_executable 'gem' }
  # end
  #
  # In addition to verifying an installation was successfully, by default
  # Sprinkle runs these verifications <em>before</em> the installation to
  # check if the package is already installed. If the verifications pass
  # before installing the package, it skips the package. To override this
  # behavior, set the -f flag on the sprinkle script or set the
  # :force option to true in Sprinkle::OPTIONS
  #
  # For more information on verifications and to see all the available
  # verifications, see Sprinkle::Verify
  #
  # == Virtual Packages
  #
  # Sometimes, there are multiple packages available for a single task. An
  # example is a database package. It can contain mySQL, postgres, or sqlite!
  # This is where virtual packages come in handy. They are defined as follows:
  #
  # package :sqlite3, :provides => :database do
  # apt 'sqlite3'
  # end
  #
  # The :provides option allows you to reference this package either by :sqlite3
  # or by :database. But whereas the package name is unique, multiple packages may
  # share the same provision. If this is the case, when running Sprinkle, the
  # script will ask you which provision you want to install. At this time, you
  # can only install one.
  #
  # == Meta-Packages
  #
  # A package doesn't require an installer. If you want to define a package which
  # merely encompasses other packages, that is fine too. Example:
  #
  # package :meta do
  # requires :magic_beans
  # requires :magic_sauce
  # end
  #
  #--
  # FIXME: Should probably document recommendations.
  #++
  module Package
    PACKAGES = {}
 
    def package(name, metadata = {}, &block)
      package = Package.new(name, metadata, &block)
      PACKAGES[name] = package
 
      if package.provides
        (PACKAGES[package.provides] ||= []) << package
      end
 
      package
    end
 
    class Package #:nodoc:
      include ArbitraryOptions
      attr_accessor :name, :provides, :installer, :dependencies, :recommends, :verifications
 
      def initialize(name, metadata = {}, &block)
        raise 'No package name supplied' unless name
 
        @name = name
        @provides = metadata[:provides]
        @dependencies = []
        @recommends = []
        @optional = []
        @verifications = []
        self.instance_eval &block
      end
 
      def freebsd_pkg(*names, &block)
        @installer = Sprinkle::Installers::FreebsdPkg.new(self, *names, &block)
      end
      
      def openbsd_pkg(*names, &block)
        @installer = Sprinkle::Installers::OpenbsdPkg.new(self, *names, &block)
      end
      
      def opensolaris_pkg(*names, &block)
        @installer = Sprinkle::Installers::OpensolarisPkg.new(self, *names, &block)
      end
      
      def bsd_port(port, &block)
        @installer = Sprinkle::Installers::BsdPort.new(self, port, &block)
      end
      
      def mac_port(port, &block)
        @installer = Sprinkle::Installers::MacPort.new(self, port, &block)
      end
 
      def apt(*names, &block)
        @installer = Sprinkle::Installers::Apt.new(self, *names, &block)
      end
 
      def deb(*names, &block)
        @installer = Sprinkle::Installers::Deb.new(self, *names, &block)
      end
 
      def rpm(*names, &block)
        @installer = Sprinkle::Installers::Rpm.new(self, *names, &block)
      end
 
      def yum(*names, &block)
        @installer = Sprinkle::Installers::Yum.new(self, *names, &block)
      end
 
      def gem(name, options = {}, &block)
        @recommends << :rubygems
        @installer = Sprinkle::Installers::Gem.new(self, name, options, &block)
      end
 
      def source(source, options = {}, &block)
        @recommends << :build_essential # Ubuntu/Debian
        @installer = Sprinkle::Installers::Source.new(self, source, options, &block)
      end
      
      def rake(name, options = {}, &block)
        @installer = Sprinkle::Installers::Rake.new(self, name, options, &block)
      end
      
      def noop(&block)
        @installer = Sprinkle::Installers::Noop.new(self, name, options, &block)
      end
      
      def push_text(text, path, options = {}, &block)
        @installer = Sprinkle::Installers::PushText.new(self, text, path, options, &block)
      end
 
def transfer(source, destination, options = {}, &block)
@installer = Sprinkle::Installers::Transfer.new(self, source, destination, options, &block)
      end
 
      def verify(description = '', &block)
        @verifications << Sprinkle::Verify.new(self, description, &block)
      end
      
      def process(deployment, roles)
        return if meta_package?
        
        # Run a pre-test to see if the software is already installed. If so,
        # we can skip it, unless we have the force option turned on!
        unless @verifications.empty? || Sprinkle::OPTIONS[:force]
          begin
            process_verifications(deployment, roles, true)
            
            logger.info "--> #{self.name} already installed for roles: #{roles}"
            return
          rescue Sprinkle::VerificationFailed => e
            # Continue
          end
        end
 
        @installer.defaults(deployment)
        @installer.process(roles)
        
        process_verifications(deployment, roles)
      end
      
      def process_verifications(deployment, roles, pre = false)
        return if @verifications.blank?
        
        if pre
          logger.info "--> Checking if #{self.name} is already installed for roles: #{roles}"
        else
          logger.info "--> Verifying #{self.name} was properly installed for roles: #{roles}"
        end
        
        @verifications.each do |v|
          v.defaults(deployment)
          v.process(roles)
        end
      end
 
      def requires(*packages)
        @dependencies << packages
        @dependencies.flatten!
      end
 
      def recommends(*packages)
        @recommends << packages
        @recommends.flatten!
      end
 
      def optional(*packages)
        @optional << packages
        @optional.flatten!
      end
 
      def tree(depth = 1, &block)
        packages = []
 
        @recommends.each do |dep|
          package = PACKAGES[dep]
          next unless package # skip missing recommended packages as they're allowed to not exist
          block.call(self, package, depth) if block
          packages << package.tree(depth + 1, &block)
        end
 
        @dependencies.each do |dep|
          package = PACKAGES[dep]
          package = select_package(dep, package) if package.is_a? Array
          
          raise "Package definition not found for key: #{dep}" unless package
          block.call(self, package, depth) if block
          packages << package.tree(depth + 1, &block)
        end
 
        packages << self
 
        @optional.each do |dep|
          package = PACKAGES[dep]
          next unless package # skip missing optional packages as they're allow to not exist
          block.call(self, package, depth) if block
          packages << package.tree(depth + 1, &block)
        end
 
        packages
      end
 
      def to_s; @name; end
 
      private
 
        def select_package(name, packages)
          if packages.size <= 1
            package = packages.first
          else
            package = choose do |menu|
              menu.prompt = "Multiple choices exist for virtual package #{name}"
              menu.choices *packages.collect(&:to_s)
            end
            package = Sprinkle::Package::PACKAGES[package]
          end
 
          cloud_info "Selecting #{package.to_s} for virtual package #{name}"
 
          package
        end
 
        def meta_package?
          @installer == nil
        end
    end
  end
end