Module: Chef::Knife::Tarsnap::Core

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

:nodoc: Would prefer to do this in a rational way, but can’t be done b/c of Mixlib::CLI’s design :(



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/knife/tarsnap/core.rb', line 25

def self.included(includer)
  includer.class_eval do

    deps do
      require 'chef/knife'
      require 'chef/shell/ext'
      Chef::Knife.load_deps
    end

    option :tarsnap_username,
      :short => "-A USERNAME",
      :long => "--tarsnap-username KEY",
      :description => "Your Tarsnap Username",
      :proc => Proc.new { |key| Chef::Config[:knife][:tarsnap_username] = key }

    option :tarsnap_password,
      :short => "-K SECRET",
      :long => "--tarsnap-password SECRET",
      :description => "Your Tarsnap Password",
      :proc => Proc.new { |key| Chef::Config[:knife][:tarsnap_password] = key }

    option :tarsnap_data_bag,
      :short => "-B BAG_NAME",
      :long => "--tarsnap-data-bag BAG_NAME",
      :description => "The data bag containing Tarsnap keys",
      :proc => Proc.new { |key| Chef::Config[:knife][:tarsnap_data_bag] = key },
      :default => "tarsnap_keys"

  end
end

Instance Method Details

#canonicalize(fqdn) ⇒ Object

Helpers



89
90
91
# File 'lib/chef/knife/tarsnap/core.rb', line 89

def canonicalize(fqdn)
  fqdn.gsub(".","_")
end

#fetch_key(fqdn) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/chef/knife/tarsnap/core.rb', line 106

def fetch_key(fqdn)
  bag_item = fetch_tarsnap_bag_item(fqdn)
  if bag_item
    bag_item['key']
  else
    nil
  end
end

#fetch_node(fqdn) ⇒ Object



101
102
103
104
# File 'lib/chef/knife/tarsnap/core.rb', line 101

def fetch_node(fqdn)
  Shell::Extensions.extend_context_object(self)
  nodes.find("fqdn:#{fqdn}").first
end

#is_a_tarsnap_node?(fqdn) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/chef/knife/tarsnap/core.rb', line 115

def is_a_tarsnap_node?(fqdn)
  tarsnap_nodes.include?(fqdn)
end

#keygen_toolObject

Required tools



79
80
81
# File 'lib/chef/knife/tarsnap/core.rb', line 79

def keygen_tool
  @_keygen_path || @_keygen_path = which('tarsnap-keygen')
end

#pending_nodesObject



93
94
95
# File 'lib/chef/knife/tarsnap/core.rb', line 93

def pending_nodes
  @_pending_nodes || @_pending_nodes = Chef::DataBag.load(tarsnap_data_bag).keep_if{|k,v| k =~ /^__/}.map{|k,v| k.gsub(/^__/, '').gsub("_",".")}
end

#remove_pending_node(fqdn) ⇒ Object



119
120
121
# File 'lib/chef/knife/tarsnap/core.rb', line 119

def remove_pending_node(fqdn)
  rest.delete_rest("data/#{tarsnap_data_bag}/__#{canonicalize(fqdn)}")
end

#tarsnap_data_bagObject



73
74
75
# File 'lib/chef/knife/tarsnap/core.rb', line 73

def tarsnap_data_bag
  Chef::Config[:knife][:tarsnap_data_bag] || config[:tarsnap_data_bag]
end

#tarsnap_nodesObject



97
98
99
# File 'lib/chef/knife/tarsnap/core.rb', line 97

def tarsnap_nodes
  @_tarsnap_nodes || @_tarsnap_nodes = Chef::DataBag.load(tarsnap_data_bag).keep_if{|k,v| k !~ /^__/}.map{|k,v| k.gsub("_",".")}
end

#tarsnap_passwordObject



66
67
68
69
70
71
# File 'lib/chef/knife/tarsnap/core.rb', line 66

def tarsnap_password
  if Chef::Config[:knife][:tarsnap_password].nil?
    Chef::Config[:knife][:tarsnap_password] = ui.ask('Tarsnap Password: ') { |q| q.echo = '*' }
  end
  Chef::Config[:knife][:tarsnap_password]
end

#tarsnap_toolObject



83
84
85
# File 'lib/chef/knife/tarsnap/core.rb', line 83

def tarsnap_tool
  @_tarsnap_path || @_tarsnap_path = which('tarsnap')
end

#tarsnap_usernameObject

Convenience methods for options



58
59
60
61
62
63
64
# File 'lib/chef/knife/tarsnap/core.rb', line 58

def tarsnap_username
  if Chef::Config[:knife][:tarsnap_username]
    Chef::Config[:knife][:tarsnap_username]
  else
    raise StandardError, "Please provide the tarsnap account username"
  end
end