Browse Source

allow export to set up a different key to use for the server

If the export instructions list a separate key for the server, use
that key when creating the new instance.

Change-Id: I11d3f24857bbd1017ca6f888a0173d9bff8e2402
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
Doug Hellmann 2 years ago
parent
commit
ce5d35840c
4 changed files with 32 additions and 4 deletions
  1. 4
    1
      demo/tiny-resources.yml
  2. 21
    1
      demo/tiny.yml
  3. 3
    1
      downpour/export.py
  4. 4
    1
      downpour/resolver.py

+ 4
- 1
demo/tiny-resources.yml View File

@@ -1,7 +1,10 @@
1 1
 # Resource file for downpour using the instance created in tiny.yml.
2 2
 servers:
3 3
   - name: downpour-demo-tiny
4
+    # Create the server using a separate key than it was created with in tiny.yml.
5
+    key_name: downpour-demo2
4 6
 keypairs:
5 7
   - name: downpour-demo
8
+  - name: downpour-demo2
6 9
 images:
7
-  - name: cirros-0.3.5-x86_64-disk
10
+  - name: cirros-0.3.5-x86_64-disk

+ 21
- 1
demo/tiny.yml View File

@@ -3,7 +3,8 @@
3 3
   connection: local
4 4
   gather_facts: no
5 5
   tasks:
6
-    # Set up the keypair, saving the values locally.
6
+    # Set up the keypair we will use with the server,
7
+    # saving the values locally.
7 8
     - name: Creating key pair downpour-demo
8 9
       os_keypair:
9 10
         state: present
@@ -23,6 +24,25 @@
23 24
         dest: "{{ inventory_dir }}/keys/{{ keypair.key.name }}"
24 25
         mode: 0600
25 26
       when: "'private_key' in keypair.key"  # only save key if we created a new key
27
+    - name: Creating key pair downpour-demo2
28
+      os_keypair:
29
+        state: present
30
+        name: downpour-demo2
31
+      register: keypair
32
+    - name: Create local public key
33
+      local_action:
34
+        module: copy
35
+        content: "{{ keypair.key.public_key }}"
36
+        dest: "{{ inventory_dir }}/keys/{{ keypair.key.name }}.pub"
37
+        mode: 0600
38
+      when: "'private_key' in keypair.key"  # only save key if we created a new key
39
+    - name: Create local private key
40
+      local_action:
41
+        module: copy
42
+        content: "{{ keypair.key.private_key }}"
43
+        dest: "{{ inventory_dir }}/keys/{{ keypair.key.name }}"
44
+        mode: 0600
45
+      when: "'private_key' in keypair.key"  # only save key if we created a new key
26 46
 
27 47
     # Create a security group and the rules needed for the ports we'll
28 48
     # be using.

+ 3
- 1
downpour/export.py View File

@@ -74,7 +74,9 @@ def export_data(cloud, config, args):
74 74
 
75 75
     for server_info in to_export.servers:
76 76
         server = cloud.get_server(server_info.name)
77
-        tasks.extend(res.server(server, save_state=server_info.save_state))
77
+        tasks.extend(res.server(server,
78
+                                save_state=server_info.save_state,
79
+                                key_name=server_info.get('key_name')))
78 80
 
79 81
     playbook = [
80 82
         # The default playbook is configured to run instructions

+ 4
- 1
downpour/resolver.py View File

@@ -182,7 +182,7 @@ class Resolver:
182 182
             }
183 183
             yield self._map_uuids('subnet', subnet.name, subnet.id, 'subnet.id')
184 184
 
185
-    def server(self, server, save_state):
185
+    def server(self, server, save_state, key_name=None):
186 186
         # The ssh key needed to login to the server.
187 187
         keypair = self.cloud.get_keypair(server.key_name)
188 188
         yield from self.keypair(keypair)
@@ -215,6 +215,9 @@ class Resolver:
215 215
             'nics': list(server.networks.keys()),
216 216
             # 'image': image.name if image else server.image.id,
217 217
         }
218
+        key_name = key_name or server.key_name
219
+        if key_name:
220
+            server_data['key_name'] = key_name
218 221
         if vol_names:
219 222
             server_data['volumes'] = vol_names
220 223
         yield {

Loading…
Cancel
Save