Sha256: 6be94e4971ac434f376f7a5122400b861937fb98427d4a839a5c7e88a13bf638

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'thor'
require 'chef'

module Teststrap
  class Teststrap < Thor
    include Thor::Actions

    attr_accessor :cookbook_name

    default_task :generate_tests

    desc "generate_tests", "generate test files"
    def generate_tests
      @cookbook_name = current_directory_name
      puts "generating testfiles::"
      copy_file ".gitignore"
      copy_file "Berksfile"
      copy_file "chefignore"
      copy_file "Gemfile"
      copy_file "LICENSE"
      copy_file "Strainerfile"
      copy_file "Thorfile"
      template "spec/default_spec.rb"
      template "spec/chefspec_helper.rb"
      template "test/integration/default/serverspec/localhost/default_spec.rb"
      template "test/integration/default/serverspec/spec_helper.rb"
      template ".kitchen.yml"
      template "metadata.rb"
      template "README.md"
    end

    desc "encrypt [data_path] [secret_path]", "encrypt plain databag item to encrypted databag item"
    def encrypt(data_path, secret_path)
      if !File.exist?(data_path)
        puts "file: #{data_path} doesn't exists"
        return nil
      end

      if !File.exist?(secret_path)
        puts "secret key file: #{secret_path} doesn't exists"
        return nil
      end

      plain_data = JSON.parse(File.read(data_path))
      secret = Chef::EncryptedDataBagItem.load_secret(secret_path)
      encrypted_data = Chef::EncryptedDataBagItem.encrypt_data_bag_item(plain_data, secret)
      puts JSON.pretty_generate(encrypted_data)
      return encrypted_data
    end

    no_commands do
      def current_directory_name
        Dir.pwd.split('/').last
      end

      def run_command(cmd)
        exec(cmd)
      end

      def self.source_root
        generator_files_path
      end

      def self.generator_files_path
        File.expand_path('../../generator_files', __FILE__)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
teststrap-1.1.8 lib/teststrap.rb