<commit_before>require 'vagrant-chefzero/version'
require 'ridley'
require 'berkshelf'

module Vagrant
  module ChefzeroPlugin
    module Error
      class MissingProperty < StandardError; end
    end

    class Plugin < Vagrant.plugin("2")
      name 'chefzero'

      config('chefzero', :provisioner) do
        Config
      end

      provisioner 'chefzero' do
        Provisioner
      end

    end

    class Config < Vagrant.plugin("2", :config)
      attr_accessor :ip
      attr_accessor :port
      attr_accessor :setup
      attr_accessor :version

      def initialize
        self.ip = UNSET_VALUE
        self.port = UNSET_VALUE
        self.version = UNSET_VALUE
        self.setup = UNSET_VALUE
      end

      def finalize!
        if self.setup == UNSET_VALUE
          self.setup = lambda {}
        end
        if self.version == UNSET_VALUE
          self.version = default_chef_zero_version
        end
        [:ip, :port].each do |req|
          raise Error::MissingProperty, "chefzero provisioner missing required property '#{req}'" unless send(req)
        end
      end

      def default_chef_zero_version
        '1.0.1'
      end

      def setup(&block)
        @setup = block unless block.nil?
        @setup
      end
    end

    class Provisioner < Vagrant.plugin("2", :provisioner)
      def initialize(machine, config)
        super
      end

      def configure(root_config)
        super
      end

      def provision
        FileUtils.mkdir_p(generated_path)
        install_chef_zero

        config.setup.call(self)
      end

      def import_data_bag_item(bag_name, item_name)
        user_ridley = ridley(user_creds)
        contents = user_ridley.data_bag.find(bag_name).item.find(item_name)

        zero_ridley = ridley(zero_creds)
        databag = zero_ridley.data_bag.find(bag_name) || zero_ridley.data_bag.create(name: bag_name)
        databag.item.find(item_name) ? databag.item.update(contents) : databag.item.create(contents)
      end

      def import_berkshelf_cookbooks(o = {})
        # path_env = o[:path] ? "BERKSHELF_PATH=#{o[:path]} " : ''
        ENV['BERKSHELF_PATH'] = generated_path
        berks(user_creds).install
        berks(zero_creds).upload
      end

      private

      def ridley(creds)
        Ridley.new(creds)
      end

      def berks(creds)
        BerkshelfActor.new(creds: creds, path: generated_path)
      end

      def user_creds
        c = Berkshelf::Chef::Config.instance
        {
          server_url: c[:chef_server_url],
          client_name: c[:node_name],
          client_key: c[:client_key],
        }
      end

      def zero_creds
        {
          server_url: chef_zero_uri,
          client_name: node_name,
          client_key: pemfile,
        }
      end

      def install_chef_zero
        m = MachineRunner.new(machine)
<commit_msg>Add configurable ca_path SSL property<commit_after>require 'vagrant-chefzero/version'
require 'ridley'
require 'berkshelf'

module Vagrant
  module ChefzeroPlugin
    module Error
      class MissingProperty < StandardError; end
    end

    class Plugin < Vagrant.plugin("2")
      name 'chefzero'

      config('chefzero', :provisioner) do
        Config
      end

      provisioner 'chefzero' do
        Provisioner
      end

    end

    class Config < Vagrant.plugin("2", :config)
      attr_accessor :ip
      attr_accessor :port
      attr_accessor :setup
      attr_accessor :version
      attr_accessor :ca_file

      def initialize
        self.ip = UNSET_VALUE
        self.port = UNSET_VALUE
        self.version = UNSET_VALUE
        self.setup = UNSET_VALUE
        self.ca_file = UNSET_VALUE
      end

      def finalize!
        if self.setup == UNSET_VALUE
          self.setup = lambda {}
        end
        if self.version == UNSET_VALUE
          self.version = default_chef_zero_version
        end
        [:ip, :port].each do |req|
          raise Error::MissingProperty, "chefzero provisioner missing required property '#{req}'" unless send(req)
        end
        self.ca_file = nil if self.ca_file == UNSET_VALUE
      end

      def default_chef_zero_version
        '1.0.1'
      end

      def setup(&block)
        @setup = block unless block.nil?
        @setup
      end
    end

    class Provisioner < Vagrant.plugin("2", :provisioner)
      def initialize(machine, config)
        super
      end

      def configure(root_config)
        super
      end

      def provision
        FileUtils.mkdir_p(generated_path)
        install_chef_zero

        config.setup.call(self)
      end

      def import_data_bag_item(bag_name, item_name)
        user_ridley = ridley(user_creds)
        contents = user_ridley.data_bag.find(bag_name).item.find(item_name)

        zero_ridley = ridley(zero_creds)
        databag = zero_ridley.data_bag.find(bag_name) || zero_ridley.data_bag.create(name: bag_name)
        databag.item.find(item_name) ? databag.item.update(contents) : databag.item.create(contents)
      end

      def import_berkshelf_cookbooks(o = {})
        # path_env = o[:path] ? "BERKSHELF_PATH=#{o[:path]} " : ''
        ENV['BERKSHELF_PATH'] = generated_path
        berks(user_creds).install
        berks(zero_creds).upload
      end

      private

      def ssl_opts
        ssl_opts = { verify: true }
        ssl_opts[:ca_file] = config.ca_file if config.ca_file
        ssl_opts
      end

      def ridley(creds)
        Ridley.new(creds.merge( ssl: ssl_opts ))
      end

      def berks(creds)
        BerkshelfActor.new(creds: creds, path: generated_path)
      end

      def user_creds
        c = Berkshelf::Chef::Config.instance
        {
          server_url: c[:chef_server_url],
          client_name: c[:node_name],
          client_key: c[:client_key],
        }
      end

      def zero_creds
        {
          server_url: chef_zero_uri,
          client_name: node_name,
          client_key: pemfile,
        }
      end

      def install_chef_zero
        m = MachineRunner.new(machine)

{'fraction_non_alphanumeric': 0.0833610096313517, 'fraction_numerical': 0.0019926934573231483, 'mean_word_length': 2.091889117043121, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7022731', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1909, 'n_words': 388}