ruby-quilt 0.1.10 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/quilt.rb +105 -70
- metadata +2 -2
    
        data/lib/quilt.rb
    CHANGED
    
    | @@ -12,11 +12,14 @@ class Quilt | |
| 12 12 | 
             
              OPTIONAL_KEY = "optional"
         | 
| 13 13 | 
             
              FOOTER_KEY = "footer"
         | 
| 14 14 | 
             
              PREFIX_KEY = "prefix"
         | 
| 15 | 
            +
              DEPENDANCIES_KEY = "dependancies"
         | 
| 16 | 
            +
              POSITION_KEY = "position"
         | 
| 15 17 | 
             
              DEBUG_PREFIX_KEY = "debug_prefix"
         | 
| 16 18 | 
             
              ARCHIVE_SUFFIX = ".tgz"
         | 
| 17 19 | 
             
              DEFAULT_LRU_SIZE = 10
         | 
| 18 20 |  | 
| 19 21 | 
             
              def initialize(config = "quilt", log = Logger.new(STDOUT))
         | 
| 22 | 
            +
                @@fetching_versions = {}
         | 
| 20 23 | 
             
                config_prefix = config ? "#{config}:" : ""
         | 
| 21 24 | 
             
                @config = {
         | 
| 22 25 | 
             
                  :local_path => Ecology.property("#{config_prefix}local_path", :as => String),
         | 
| @@ -59,8 +62,9 @@ class Quilt | |
| 59 62 | 
             
                matches[2]
         | 
| 60 63 | 
             
              end
         | 
| 61 64 |  | 
| 62 | 
            -
              def get_module(filename, dependancies, version_dir)
         | 
| 65 | 
            +
              def get_module(filename, dependancies, position, version_dir)
         | 
| 63 66 | 
             
                tmp_module = {}
         | 
| 67 | 
            +
                tmp_module[:position] = position
         | 
| 64 68 | 
             
                tmp_module[:dependancies] = dependancies.is_a?(Array) ? dependancies :
         | 
| 65 69 | 
             
                                                                        (dependancies.is_a?(String) ? [ dependancies ] :
         | 
| 66 70 | 
             
                                                                        [])
         | 
| @@ -73,6 +77,14 @@ class Quilt | |
| 73 77 | 
             
                tmp_module
         | 
| 74 78 | 
             
              end
         | 
| 75 79 |  | 
| 80 | 
            +
              def version_exists_locally?(local_path, version_name)
         | 
| 81 | 
            +
                begin
         | 
| 82 | 
            +
                  manifest = JSON.parse(File.read(File.join(File.join(local_path, version_name), "manifest.json")))
         | 
| 83 | 
            +
                rescue Exception => e
         | 
| 84 | 
            +
                  return false
         | 
| 85 | 
            +
                end
         | 
| 86 | 
            +
              end
         | 
| 87 | 
            +
             | 
| 76 88 | 
             
              def load_version(local_path, version_name)
         | 
| 77 89 | 
             
                log_debug("Loading Version: "+version_name)
         | 
| 78 90 | 
             
                manifest = {}
         | 
| @@ -117,9 +129,13 @@ class Quilt | |
| 117 129 | 
             
                #    ],
         | 
| 118 130 | 
             
                #    "optional" : {
         | 
| 119 131 | 
             
                #      "<module file>" : [ "<dependancy module name>", ... ],
         | 
| 132 | 
            +
                #      "<module file>" : { "dependancies" : [ "<dependancy module name>", ... ], "position" : "<position>" }
         | 
| 120 133 | 
             
                #      ...
         | 
| 121 134 | 
             
                #    }
         | 
| 122 135 | 
             
                #  }
         | 
| 136 | 
            +
                #
         | 
| 137 | 
            +
                #  where <position> can be one of: before_header, after_header, before_common, after_common,
         | 
| 138 | 
            +
                #                                  before_optional, optional, after_optional, before_footer, after_footer
         | 
| 123 139 | 
             
                module_loader = Proc.new do |prefix|
         | 
| 124 140 | 
             
                  dir = new_version[prefix][:dir]
         | 
| 125 141 | 
             
                  if manifest[HEADER_KEY]
         | 
| @@ -140,10 +156,18 @@ class Quilt | |
| 140 156 | 
             
                    end
         | 
| 141 157 | 
             
                  end
         | 
| 142 158 | 
             
                  if manifest[OPTIONAL_KEY] && manifest[OPTIONAL_KEY].is_a?(Hash)
         | 
| 143 | 
            -
                    manifest[OPTIONAL_KEY].each do |filename,  | 
| 159 | 
            +
                    manifest[OPTIONAL_KEY].each do |filename, value|
         | 
| 160 | 
            +
                      dependancies = []
         | 
| 161 | 
            +
                      position = :optional
         | 
| 162 | 
            +
                      if (value.is_a?(Array))
         | 
| 163 | 
            +
                        dependancies = value
         | 
| 164 | 
            +
                      elsif (value.is_a?(Hash))
         | 
| 165 | 
            +
                        dependancies = value[DEPENDANCIES_KEY] || []
         | 
| 166 | 
            +
                        position = value[POSITION_KEY] ? value[POSITION_KEY].to_sym : :optional
         | 
| 167 | 
            +
                      end
         | 
| 144 168 | 
             
                      tmp_module_name = get_module_name(filename)
         | 
| 145 169 | 
             
                      if (tmp_module_name)
         | 
| 146 | 
            -
                        tmp_module = get_module(filename, dependancies, dir)
         | 
| 170 | 
            +
                        tmp_module = get_module(filename, dependancies, position, dir)
         | 
| 147 171 | 
             
                        if (tmp_module)
         | 
| 148 172 | 
             
                          new_version[prefix][:optional][tmp_module_name] = tmp_module
         | 
| 149 173 | 
             
                        end
         | 
| @@ -168,7 +192,7 @@ class Quilt | |
| 168 192 | 
             
                new_version
         | 
| 169 193 | 
             
              end
         | 
| 170 194 |  | 
| 171 | 
            -
              def resolve_dependancies(modules, version, all_modules = {})
         | 
| 195 | 
            +
              def resolve_dependancies(position, modules, version, all_modules = {})
         | 
| 172 196 | 
             
                out = ''
         | 
| 173 197 | 
             
                return out if !modules || !(modules.is_a?(Array)) || modules.empty?
         | 
| 174 198 | 
             
                my_all_modules = all_modules
         | 
| @@ -184,8 +208,8 @@ class Quilt | |
| 184 208 | 
             
                    break
         | 
| 185 209 | 
             
                  end
         | 
| 186 210 | 
             
                  my_all_modules[name] = 1
         | 
| 187 | 
            -
                  out = "#{out}#{resolve_dependancies(version[:optional][name][:dependancies], version, my_all_modules)}"
         | 
| 188 | 
            -
                  out = "#{out}#{version[:optional][name][:module]}"
         | 
| 211 | 
            +
                  out = "#{out}#{resolve_dependancies(position, version[:optional][name][:dependancies], version, my_all_modules)}"
         | 
| 212 | 
            +
                  out = "#{out}#{version[:optional][name][:module]}" if version[:optional][name][:position] == position
         | 
| 189 213 | 
             
                  my_all_modules[name] = 2
         | 
| 190 214 | 
             
                end
         | 
| 191 215 | 
             
                out
         | 
| @@ -194,61 +218,70 @@ class Quilt | |
| 194 218 | 
             
              def get_version(name)
         | 
| 195 219 | 
             
                return @versions[name] if @versions[name]
         | 
| 196 220 | 
             
                # check local path
         | 
| 197 | 
            -
                 | 
| 198 | 
            -
                 | 
| 199 | 
            -
                 | 
| 200 | 
            -
             | 
| 201 | 
            -
             | 
| 202 | 
            -
             | 
| 203 | 
            -
                end
         | 
| 204 | 
            -
                port = @config[:remote_port] ? @config[:remote_port].to_i : 80
         | 
| 205 | 
            -
                # Fetch the version
         | 
| 206 | 
            -
                filename = "#{name}#{ARCHIVE_SUFFIX}"
         | 
| 207 | 
            -
                version_dir = File.join(@config[:local_path], name)
         | 
| 208 | 
            -
                begin
         | 
| 209 | 
            -
                  res = Net::HTTP.get_response(@config[:remote_host].to_s,
         | 
| 210 | 
            -
                                               File.join(@config[:remote_path].to_s, filename), port)
         | 
| 211 | 
            -
                  if (res.code != "200")
         | 
| 212 | 
            -
                    log_error("no version fetched : #{res.code}")
         | 
| 213 | 
            -
                    return nil
         | 
| 221 | 
            +
                # sleep at most 10 seconds to wait until a version is fetched and untared
         | 
| 222 | 
            +
                @@fetching_versions[name] ||= Mutex.new
         | 
| 223 | 
            +
                @@fetching_versions[name].synchronize do
         | 
| 224 | 
            +
                  if version_exists_locally?(@config[:local_path], name)
         | 
| 225 | 
            +
                    @versions[name] = load_version(@config[:local_path], name)
         | 
| 226 | 
            +
                    return @versions[name] if @versions[name]
         | 
| 214 227 | 
             
                  end
         | 
| 215 | 
            -
                   | 
| 216 | 
            -
                   | 
| 217 | 
            -
                     | 
| 228 | 
            +
                  # check remote path
         | 
| 229 | 
            +
                  if (!@config[:remote_host] || !@config[:remote_path])
         | 
| 230 | 
            +
                    log_error("unable to load from host: #{@config[:remote_host]}, path: #{@config[:remote_path]}")
         | 
| 231 | 
            +
                    return nil
         | 
| 218 232 | 
             
                  end
         | 
| 219 | 
            -
             | 
| 220 | 
            -
                   | 
| 221 | 
            -
                   | 
| 222 | 
            -
             | 
| 223 | 
            -
                # Untar the version
         | 
| 224 | 
            -
                tar_stdout = nil
         | 
| 225 | 
            -
                tar_stderr = nil
         | 
| 226 | 
            -
                tar_status =
         | 
| 227 | 
            -
                  POpen4::popen4("cd #{version_dir} && tar -xzf #{filename} && rm #{filename}") do |stdo, stde, stdi, pid|
         | 
| 228 | 
            -
                  stdi.close
         | 
| 229 | 
            -
                  tar_stdout = stdo.read.strip
         | 
| 230 | 
            -
                  tar_stderr = stde.read.strip
         | 
| 231 | 
            -
                end
         | 
| 232 | 
            -
                if (!tar_status.exitstatus.zero?)
         | 
| 233 | 
            -
                  log_error("unable to untar package: cd #{version_dir} && tar -xzf #{filename} && rm #{filename}")
         | 
| 234 | 
            -
                  log_error("stdout: #{tar_stdout}")
         | 
| 235 | 
            -
                  log_error("stderr: #{tar_stderr}")
         | 
| 233 | 
            +
                  port = @config[:remote_port] ? @config[:remote_port].to_i : 80
         | 
| 234 | 
            +
                  # Fetch the version
         | 
| 235 | 
            +
                  filename = "#{name}#{ARCHIVE_SUFFIX}"
         | 
| 236 | 
            +
                  version_dir = File.join(@config[:local_path], name)
         | 
| 236 237 | 
             
                  begin
         | 
| 237 | 
            -
                     | 
| 238 | 
            +
                    res = Net::HTTP.get_response(@config[:remote_host].to_s,
         | 
| 239 | 
            +
                                                 File.join(@config[:remote_path].to_s, filename), port)
         | 
| 240 | 
            +
                    if (res.code != "200")
         | 
| 241 | 
            +
                      log_error("no version fetched : #{res.code}")
         | 
| 242 | 
            +
                      return nil
         | 
| 243 | 
            +
                    end
         | 
| 244 | 
            +
                    FileUtils.mkdir(version_dir) unless File.exists?(version_dir)
         | 
| 245 | 
            +
                    open(File.join(version_dir, filename), "wb") do |file|
         | 
| 246 | 
            +
                      file.write(res.body)
         | 
| 247 | 
            +
                    end
         | 
| 238 248 | 
             
                  rescue Exception => e
         | 
| 239 | 
            -
                     | 
| 249 | 
            +
                    log_error("could not fetch version", e)
         | 
| 250 | 
            +
                    return nil
         | 
| 240 251 | 
             
                  end
         | 
| 241 | 
            -
                   | 
| 242 | 
            -
             | 
| 243 | 
            -
             | 
| 244 | 
            -
             | 
| 245 | 
            -
             | 
| 246 | 
            -
             | 
| 247 | 
            -
                     | 
| 248 | 
            -
             | 
| 249 | 
            -
                    # do nothing
         | 
| 252 | 
            +
                  # Untar the version
         | 
| 253 | 
            +
                  tar_stdout = nil
         | 
| 254 | 
            +
                  tar_stderr = nil
         | 
| 255 | 
            +
                  tar_status =
         | 
| 256 | 
            +
                    POpen4::popen4("cd #{version_dir} && tar -xzf #{filename} && rm #{filename}") do |stdo, stde, stdi, pid|
         | 
| 257 | 
            +
                    stdi.close
         | 
| 258 | 
            +
                    tar_stdout = stdo.read.strip
         | 
| 259 | 
            +
                    tar_stderr = stde.read.strip
         | 
| 250 260 | 
             
                  end
         | 
| 251 | 
            -
                   | 
| 261 | 
            +
                  if (!tar_status.exitstatus.zero?)
         | 
| 262 | 
            +
                    log_error("unable to untar package: cd #{version_dir} && tar -xzf #{filename} && rm #{filename}")
         | 
| 263 | 
            +
                    log_error("stdout: #{tar_stdout}")
         | 
| 264 | 
            +
                    log_error("stderr: #{tar_stderr}")
         | 
| 265 | 
            +
                    begin
         | 
| 266 | 
            +
                      FileUtils.rm_r(version_dir)
         | 
| 267 | 
            +
                    rescue Exception => e
         | 
| 268 | 
            +
                      # do nothing
         | 
| 269 | 
            +
                    end
         | 
| 270 | 
            +
                    return nil
         | 
| 271 | 
            +
                  end
         | 
| 272 | 
            +
                  # Load the version
         | 
| 273 | 
            +
                  @versions[name] = load_version(@config[:local_path], name)
         | 
| 274 | 
            +
                  if (!@versions[name])
         | 
| 275 | 
            +
                    begin
         | 
| 276 | 
            +
                      FileUtils.rm_r(version_dir)
         | 
| 277 | 
            +
                    rescue Exception => e
         | 
| 278 | 
            +
                      # do nothing
         | 
| 279 | 
            +
                    end
         | 
| 280 | 
            +
                    return nil
         | 
| 281 | 
            +
                  end
         | 
| 282 | 
            +
                end
         | 
| 283 | 
            +
                @@fetching_versions.reject! do |key, value|
         | 
| 284 | 
            +
                  key == name
         | 
| 252 285 | 
             
                end
         | 
| 253 286 | 
             
                @versions[name]
         | 
| 254 287 | 
             
              end
         | 
| @@ -275,39 +308,40 @@ class Quilt | |
| 275 308 | 
             
                # resolve dependancies
         | 
| 276 309 | 
             
                dynamics = dynamic_modules && dynamic_modules.is_a?(Hash) ? dynamic_modules : {}
         | 
| 277 310 | 
             
                output = "#{
         | 
| 278 | 
            -
                  resolve_dynamic(:before_header, dynamics, outversion)
         | 
| 311 | 
            +
                  resolve_dynamic(:before_header, modules, dynamics, outversion)
         | 
| 279 312 | 
             
                }#{
         | 
| 280 313 | 
             
                  outversion[:header]
         | 
| 281 314 | 
             
                }#{
         | 
| 282 | 
            -
                  resolve_dynamic(:after_header, dynamics, outversion)
         | 
| 315 | 
            +
                  resolve_dynamic(:after_header, modules, dynamics, outversion)
         | 
| 283 316 | 
             
                }#{
         | 
| 284 | 
            -
                  resolve_dynamic(:before_common, dynamics, outversion)
         | 
| 317 | 
            +
                  resolve_dynamic(:before_common, modules, dynamics, outversion)
         | 
| 285 318 | 
             
                }#{
         | 
| 286 319 | 
             
                  outversion[:common]
         | 
| 287 320 | 
             
                }#{
         | 
| 288 | 
            -
                  resolve_dynamic(:after_common, dynamics, outversion)
         | 
| 321 | 
            +
                  resolve_dynamic(:after_common, modules, dynamics, outversion)
         | 
| 289 322 | 
             
                }#{
         | 
| 290 | 
            -
                  resolve_dynamic(:before_optional, dynamics, outversion)
         | 
| 323 | 
            +
                  resolve_dynamic(:before_optional, modules, dynamics, outversion)
         | 
| 291 324 | 
             
                }#{
         | 
| 292 | 
            -
                   | 
| 325 | 
            +
                  resolve_dynamic(:optional, modules, dynamics, outversion)
         | 
| 293 326 | 
             
                }#{
         | 
| 294 | 
            -
                  resolve_dynamic(:after_optional, dynamics, outversion)
         | 
| 327 | 
            +
                  resolve_dynamic(:after_optional, modules, dynamics, outversion)
         | 
| 295 328 | 
             
                }#{
         | 
| 296 | 
            -
                  resolve_dynamic(:before_footer, dynamics, outversion)
         | 
| 329 | 
            +
                  resolve_dynamic(:before_footer, modules, dynamics, outversion)
         | 
| 297 330 | 
             
                }#{
         | 
| 298 331 | 
             
                  outversion[:footer]
         | 
| 299 332 | 
             
                }#{
         | 
| 300 | 
            -
                  resolve_dynamic(:after_footer, dynamics, outversion)
         | 
| 333 | 
            +
                  resolve_dynamic(:after_footer, modules, dynamics, outversion)
         | 
| 301 334 | 
             
                }"
         | 
| 302 335 | 
             
              end
         | 
| 303 336 |  | 
| 304 | 
            -
              def resolve_dynamic( | 
| 305 | 
            -
                dynamics[ | 
| 337 | 
            +
              def resolve_dynamic(position, modules, dynamics, version)
         | 
| 338 | 
            +
                dynamics[position].is_a?(Array) ? resolve_dependancies(position, dynamics[position] + modules, version, {}) :
         | 
| 339 | 
            +
                  "#{(dynamics[position] || '')}#{resolve_dependancies(position, modules, version, {})}"
         | 
| 306 340 | 
             
              end
         | 
| 307 341 |  | 
| 308 342 | 
             
              def health
         | 
| 309 343 | 
             
                # return true if no remote info
         | 
| 310 | 
            -
                return [true, nil] if !@config[:remote_host] || !@config[:remote_path]
         | 
| 344 | 
            +
                return [true, nil, nil] if !@config[:remote_host] || !@config[:remote_path]
         | 
| 311 345 | 
             
                # fetch health_check.txt from remote URL
         | 
| 312 346 | 
             
                host = @config[:remote_host].to_s
         | 
| 313 347 | 
             
                port = @config[:remote_port] ? @config[:remote_port].to_i : 80
         | 
| @@ -315,12 +349,13 @@ class Quilt | |
| 315 349 | 
             
                begin
         | 
| 316 350 | 
             
                  res = Net::HTTP.get_response(host, path, port)
         | 
| 317 351 | 
             
                  if (res.code != "200")
         | 
| 318 | 
            -
                    return [false, "Could not fetch heath check file: http://#{host}:#{port}#{path} - status #{res.code}" | 
| 352 | 
            +
                    return [false, "Could not fetch heath check file: http://#{host}:#{port}#{path} - status #{res.code}",
         | 
| 353 | 
            +
                            nil]
         | 
| 319 354 | 
             
                  end
         | 
| 320 355 | 
             
                rescue Exception => e
         | 
| 321 | 
            -
                  return [false, "Could not fetch heath check file: http://#{host}:#{port}#{path} - #{e.message}"]
         | 
| 356 | 
            +
                  return [false, "Could not fetch heath check file: http://#{host}:#{port}#{path} - #{e.message}", e]
         | 
| 322 357 | 
             
                end
         | 
| 323 | 
            -
                [true, nil]
         | 
| 358 | 
            +
                [true, nil, nil]
         | 
| 324 359 | 
             
              end
         | 
| 325 360 |  | 
| 326 361 | 
             
              def status
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: ruby-quilt
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.2.0
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2012-06- | 
| 12 | 
            +
            date: 2012-06-27 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: json
         |