project.rb 4.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# 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.
#

18
19
20
21
22
23
#
# omnibus project dsl reader
#
module Omnibus
  class Project
    include Rake::DSL
Stephen Delano's avatar
Stephen Delano committed
24

25
26
    NULL_ARG = Object.new

Stephen Delano's avatar
Stephen Delano committed
27
28
    attr_reader :dependencies

29
30
31
32
    def self.load(filename)
      new(IO.read(filename), filename)
    end

33
34
35
36
    def self.all_projects
      @@projects ||= []
    end

37
    def initialize(io, filename)
38
      @exclusions = Array.new
Stephen Delano's avatar
Stephen Delano committed
39
40
41
42
      instance_eval(io)
      render_tasks
    end

43
44
45
    def name(val=NULL_ARG)
      @name = val unless val.equal?(NULL_ARG)
      @name
Stephen Delano's avatar
Stephen Delano committed
46
47
    end

48
49
50
51
52
    def install_path(val=NULL_ARG)
      @install_path = val unless val.equal?(NULL_ARG)
      @install_path
    end

53
54
55
56
    def iteration
      if platform_family == 'rhel'
        platform_version =~ /^(\d+)/
        maj = $1
57
        return "#{build_iteration}.el#{maj}"
58
      end
59
      return "#{build_iteration}.#{platform}.#{platform_version}"
60
61
    end

62
63
64
    def description(val=NULL_ARG)
      @description = val unless val.equal?(NULL_ARG)
      @description
Stephen Delano's avatar
Stephen Delano committed
65
66
    end

67
68
69
70
71
    def replaces(val=NULL_ARG)
      @replaces = val unless val.equal?(NULL_ARG)
      @replaces
    end

72
73
74
75
76
77
78
79
80
81
    def build_version(val=NULL_ARG)
      @build_version = val unless val.equal?(NULL_ARG)
      @build_version
    end

    def build_iteration(val=NULL_ARG)
      @build_iteration = val unless val.equal?(NULL_ARG)
      @build_iteration
    end

Stephen Delano's avatar
Stephen Delano committed
82
83
84
85
    def dependencies(val)
      @dependencies = val
    end

86
87
88
89
    def exclude(pattern)
      @exclusions << pattern
    end

90
91
92
93
94
95
96
97
    def platform_version
      OHAI.platform_version
    end

    def platform
      OHAI.platform
    end

98
99
100
101
    def platform_family
      OHAI.platform_family
    end

102
103
104
105
    def config
      Omnibus.config
    end

Noah Kantrowitz's avatar
Noah Kantrowitz committed
106
    def package_scripts_path
107
      "#{Omnibus.root}/package-scripts/#{name}"
Noah Kantrowitz's avatar
Noah Kantrowitz committed
108
109
    end

Daniel DeLeo's avatar
Daniel DeLeo committed
110
    def package_types
111
112
      case platform_family
      when 'debian'
Daniel DeLeo's avatar
Daniel DeLeo committed
113
114
115
        [ "deb" ]
      when 'fedora', 'rhel'
        [ "rpm" ]
116
117
      when 'solaris2'
        [ "solaris" ]
118
119
      else
        []
Daniel DeLeo's avatar
Daniel DeLeo committed
120
121
122
      end
    end

Stephen Delano's avatar
Stephen Delano committed
123
124
125
    private

    def render_tasks
126
      directory config.package_dir
127
      directory "pkg"
128

Stephen Delano's avatar
Stephen Delano committed
129
      namespace :projects do
130

Daniel DeLeo's avatar
Daniel DeLeo committed
131
        package_types.each do |pkg_type|
132
133
134
          namespace @name do
            desc "package #{@name} into a #{pkg_type}"
            task pkg_type => (@dependencies.map {|dep| "software:#{dep}"}) do
135
136
137
138
139

              # build the fpm command
              fpm_command = ["fpm",
                             "-s dir",
                             "-t #{pkg_type}",
140
                             "-v #{build_version}",
141
                             "-n #{@name}",
142
                             "--iteration #{iteration}",
143
                             install_path,
Noah Kantrowitz's avatar
Noah Kantrowitz committed
144
                             "--post-install '#{package_scripts_path}/postinst'",
Lamont Granquist's avatar
Lamont Granquist committed
145
                             "--pre-uninstall '#{package_scripts_path}/prerm'",
146
147
148
149
                             "-m 'Opscode, Inc.'",
                             "--description 'The full stack of #{@name}'",
                             "--url http://www.opscode.com"]

Stephen Delano's avatar
Stephen Delano committed
150
151
152
153
154
              # solaris packages don't support --post-uninstall
              unless pkg_type == "solaris"
                fpm_command << "--post-uninstall '#{package_scripts_path}/postrm'"
              end

155
156
157
              @exclusions.each do |pattern|
                fpm_command << "--exclude '#{pattern}'"
              end
158
              fpm_command << " --replaces #{@replaces}" if @replaces
159
160

              shell = Mixlib::ShellOut.new(fpm_command.join(" "),
161
162
                                           :live_stream => STDOUT,
                                           :timeout => 3600,
163
                                           :cwd => config.package_dir)
164
165
166
              shell.run_command
              shell.error!
            end
167

168
            task pkg_type => config.package_dir
169
            task pkg_type => "#{@name}:health_check"
170
          end
Stephen Delano's avatar
Stephen Delano committed
171
        end
172

173
174
175
176
177
178
179
180
        task "#{@name}:copy" => (package_types.map {|pkg_type| "#{@name}:#{pkg_type}"}) do
          cp_cmd = "cp #{config.package_dir}/* pkg/"
          shell = Mixlib::ShellOut.new(cp_cmd)
          shell.run_command
          shell.error!
        end
        task "#{@name}:copy" => "pkg"

181
        desc "package #{@name}"
182
        task @name => "#{@name}:copy"
183
184
185
186
187

        desc "run the health check on the #{@name} install path"
        task "#{@name}:health_check" do
          Omnibus::HealthCheck.run(install_path)
        end
Stephen Delano's avatar
Stephen Delano committed
188
189
      end
    end
190
191
  end
end