Class: Chef::Provider::FogKeyPair

Inherits:
LWRPBase
  • Object
show all
Includes:
ChefMetal::ProviderActionHandler
Defined in:
lib/chef/provider/fog_key_pair.rb

Instance Method Summary collapse

Instance Method Details

#computeObject



124
125
126
# File 'lib/chef/provider/fog_key_pair.rb', line 124

def compute
  new_resource.provisioner.compute
end

#create_keyObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/provider/fog_key_pair.rb', line 38

def create_key
  if current_resource_exists?
    # If the public keys are different, update the server public key
    if !current_resource.private_key_path
      if new_resource.allow_overwrite
        ensure_keys
      else
        raise "#{key_description} already exists on the server, but the private key #{new_resource.private_key_path} does not exist!"
      end
    else
      ensure_keys
    end

    new_fingerprint = case new_resource.provisioner.compute_options[:provider]
    when 'DigitalOcean'
      Cheffish::KeyFormatter.encode(desired_key, :format => :openssh)
    when 'OpenStack'
      Cheffish::KeyFormatter.encode(desired_key, :format => :openssh)
    else
      Cheffish::KeyFormatter.encode(desired_key, :format => :fingerprint)
    end

    if new_fingerprint != @current_fingerprint
      if new_resource.allow_overwrite
        converge_by "update #{key_description} to match local key at #{new_resource.private_key_path}" do
          case new_resource.provisioner.compute_options[:provider]
          when 'DigitalOcean'
            compute.create_ssh_key(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
          when 'OpenStack'
            compute.create_key_pair(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
          else
            compute.import_key_pair(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
          end
        end
      else
        raise "#{key_description} does not match local private key, and allow_overwrite is false!"
      end
    end
  else
    # Generate the private and/or public keys if they do not exist
    ensure_keys

    # Create key
    converge_by "create #{key_description} from local key at #{new_resource.private_key_path}" do
      case new_resource.provisioner.compute_options[:provider]
      when 'DigitalOcean'
        compute.create_ssh_key(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
      when 'OpenStack'
        compute.create_key_pair(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
      else
        compute.import_key_pair(new_resource.name, Cheffish::KeyFormatter.encode(desired_key, :format => :openssh))
      end
    end
  end
end

#current_public_keyObject



128
129
130
# File 'lib/chef/provider/fog_key_pair.rb', line 128

def current_public_key
  current_resource.source_key
end

#current_resource_exists?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/chef/provider/fog_key_pair.rb', line 120

def current_resource_exists?
  @current_resource.action != [ :delete ]
end

#desired_keyObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/provider/fog_key_pair.rb', line 108

def desired_key
  @desired_key ||= begin
    if new_resource.public_key_path
      public_key, format = Cheffish::KeyFormatter.decode(IO.read(new_resource.public_key_path))
      public_key
    else
      private_key, format = Cheffish::KeyFormatter.decode(IO.read(new_resource.private_key_path))
      private_key.public_key
    end
  end
end

#ensure_keysObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef/provider/fog_key_pair.rb', line 94

def ensure_keys
  resource = new_resource
  Cheffish.inline_resource(self) do
    private_key resource.private_key_path do
      public_key_path resource.public_key_path
      if resource.private_key_options
        resource.private_key_options.each_pair do |key,value|
          send(key, value)
        end
      end
    end
  end
end

#key_descriptionObject



34
35
36
# File 'lib/chef/provider/fog_key_pair.rb', line 34

def key_description
  "#{new_resource.name} on #{new_resource.provisioner.provisioner_url}"
end

#load_current_resourceObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chef/provider/fog_key_pair.rb', line 132

def load_current_resource
  if !new_resource.provisioner.kind_of?(ChefMetalFog::FogProvisioner)
    raise 'ec2_key_pair only works with fog_provisioner'
  end
  @current_resource = Chef::Resource::FogKeyPair.new(new_resource.name)
  case new_resource.provisioner.compute_options[:provider]
  when 'DigitalOcean'
    current_key_pair = compute.ssh_keys.select { |key| key.name == new_resource.name }.first
    if current_key_pair
      @current_id = current_key_pair.id
      @current_fingerprint = current_key_pair ? compute.ssh_keys.get(@current_id).ssh_pub_key : nil
    else
      current_resource.action :delete
    end
  when 'OpenStack'
    current_key_pair = compute.key_pairs.get(new_resource.name)
    if current_key_pair
      @current_id = current_key_pair.name
      @current_fingerprint = current_key_pair ? compute.key_pairs.get(@current_id).public_key : nil
    else
      current_resource.action :delete
    end
  else
    current_key_pair = compute.key_pairs.get(new_resource.name)
    if current_key_pair
      @current_fingerprint = current_key_pair ? current_key_pair.fingerprint : nil
    else
      current_resource.action :delete
    end
  end

  if new_resource.private_key_path && ::File.exist?(new_resource.private_key_path)
    current_resource.private_key_path new_resource.private_key_path
  end
  if new_resource.public_key_path && ::File.exist?(new_resource.public_key_path)
    current_resource.public_key_path new_resource.public_key_path
  end
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/chef/provider/fog_key_pair.rb', line 11

def whyrun_supported?
  true
end