Sha256: 70a30a64956be4199556deb41605a794cc488890655cf480bddd72b066c8da69

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'tempfile'
require 'json'
require 'chef/application/solo'

module Natives
  class App
    def install(catalog_name, packages, configs={})
      create_solo_json_tempfile(
        catalog_name.to_s,
        Array(packages),
        default_configs.merge(configs || {})

      ) do |attrs_file|
        run_chef_solo(attrs_file)
      end
    end

    def run_chef_solo(json_attrs_file)
      ARGV.clear
      [
        '-c', File.join(gem_base_path, 'chef', 'solo.rb'),
        '-o', 'natives',
        '-j', json_attrs_file.to_path
      ].each do |token|
        ARGV << token
      end
      Chef::Application::Solo.new.run
    end

    def create_solo_json_tempfile(catalog_name, packages, configs, &block)
      file = Tempfile.new('natives.temp_attrs_file')
      file.write(json_attrs(catalog_name, packages, configs))
      file.flush
      file.rewind
      begin
        block.call(file)
      ensure
        file.close!
      end
    end

    def default_configs
      {
        working_dir: current_working_dir
      }
    end
    protected

    def current_working_dir
      Dir.pwd
    end

    def gem_base_path
      File.absolute_path(File.join(File.dirname(__FILE__), '..', '..'))
    end

    def json_attrs(catalog_name, packages, configs)
      {
        "natives" => {
          "install_list" => {
            catalog_name => packages
          },
          "configs" => configs
        }
      }.to_json
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
natives-0.4.0 lib/natives/app.rb