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

Normalize the FQDN by replacing dots (.) with underscores (_).



95
96
97
# File 'lib/chef/knife/tarsnap/core.rb', line 95

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

#fetch_key(fqdn) ⇒ Object

Returns the tarsnap key for a given node’s FQDN.



119
120
121
122
123
124
125
126
# File 'lib/chef/knife/tarsnap/core.rb', line 119

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

Convenience method for returning a Chef::Node object from its FQDN.



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

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

#is_a_tarsnap_node?(fqdn) ⇒ Boolean

Returns a boolean indicating if the node has a tarsnap key and is ready for use, or not.

Returns:

  • (Boolean)


129
130
131
# File 'lib/chef/knife/tarsnap/core.rb', line 129

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

#keygen_toolObject

Returns the path of the tarsnap-keygen command line binary.



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

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

#pending_nodesObject

Return the list of nodes from the data bag marked as pending.



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

def pending_nodes
  Shell::Extensions.extend_context_object(self)
  @_pending_nodes || @_pending_nodes =
    nodes.find('tarsnap_pending:true').map{|n| n.fqdn}
end

#remove_pending_node(fqdn) ⇒ Object

Remove the data bag entry for a pending node.



134
135
136
137
138
# File 'lib/chef/knife/tarsnap/core.rb', line 134

def remove_pending_node(fqdn)
  n = nodes.find("fqdn:#{fqdn}").first
  n.set.delete('tarsnap_pending')
  n.save
end

#tarsnap_data_bagObject

Returns the name of the data bag used to store tarsnap keys.



76
77
78
# File 'lib/chef/knife/tarsnap/core.rb', line 76

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

#tarsnap_nodesObject

Return the list of nodes from the data bag marked as having keys.



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

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

Returns the tarsnap account password, or prompt the user for input.



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

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

Returns the path of the tarsnap command line binary.



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

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

#tarsnap_usernameObject

Returns the tarsnap account username or throw an exception.



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

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