class Bagboy::Chef::DataBags::Item

Attributes

name[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 11
def initialize(opts = {})
  @path = opts[:path] || ''
  @file = opts[:file] || Core::FileHelper.instance
  @name = opts[:name] || ''
  @bag = opts[:bag] || ''
  @scm = opts[:scm] || Core::SCMHelper.instance
  @knife = opts[:knife] || Bagboy::Chef::Knife.instance
  @data = {}
end

Public Instance Methods

data() click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 21
def data
  if File.exists?(@path.to_s) and @data.empty?
    @data = JSON.parse @file.read(@path)
  else
    @data
  end
end
sync() click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 38
def sync
  @knife.update_item(@bag, @name)
end
update(values, fields) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 29
def update (values, fields)
  pull
  text = parse_data(values, fields)
  @file.write @path, text
  sync
  commit
  @data
end

Private Instance Methods

clean_password() click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 88
def clean_password
  data['password'].to_s.tr("\n", '')
end
clean_value(key, value, fields) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 65
def clean_value(key, value, fields)
  if fields[key] == 'text' or fields[key] == 'password'
    strip value
  elsif fields[key] == 'array'
    value.reject { |c| c.to_s.empty? }
  else
    value
  end
end
commit() click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 48
def commit
  @scm.commit(@path, "Data Bag: #{@bag} | Item: #{@name}")
end
create_password(password) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 80
def create_password (password)
  if password.to_s.empty?
    clean_password
  else
    hash_password password
  end
end
hash_password(password) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 92
def hash_password password
  cmd = "openssl passwd -1 '#{password}'"
  `#{cmd}`.tr("\n", "")
end
parse_data(values, fields) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 52
def parse_data (values, fields)
  values.try(:each) do |key, value|
    value = clean_value key, value, fields
    if fields[key] == 'password'
      values[key] = create_password value
    else
      values[key] = value
    end
  end
  @data = values.to_h
  JSON.pretty_generate(@data)
end
pull() click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 44
def pull
  @scm.pull
end
strip(string) click to toggle source
# File lib/bagboy/chef/data_bags/item.rb, line 75
def strip(string)
  tmp = string.strip!
  (tmp != nil) ? tmp : string
end