add ssh_import_id cloud-config module
This commit is contained in:
		
							
								
								
									
										29
									
								
								cloudinit/CloudConfig/cc_ssh_import_id.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								cloudinit/CloudConfig/cc_ssh_import_id.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					import cloudinit.util as util
 | 
				
			||||||
 | 
					import subprocess
 | 
				
			||||||
 | 
					import traceback
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def handle(name,cfg,cloud,log,args):
 | 
				
			||||||
 | 
					    if len(args) != 0:
 | 
				
			||||||
 | 
					        user = args[0]
 | 
				
			||||||
 | 
					        ids = [ ]
 | 
				
			||||||
 | 
					        if len(args) > 1:
 | 
				
			||||||
 | 
					            ids = args[1:]
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        user = util.get_cfg_option_str(cfg,"user","ubuntu")
 | 
				
			||||||
 | 
					        ids = util.get_cfg_option_list_or_str(cfg,"ssh_import_id",[])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log.warn("here, args = %s.  user = %s ids = %s" % ( args, user, ids ))
 | 
				
			||||||
 | 
					    if len(ids) == 0: return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cmd = [ "sudo", "-Hu", user, "ssh-import-lp-id" ] + ids
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log.debug("importing ssh ids. cmd = %s" % cmd)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        subprocess.check_call(cmd)
 | 
				
			||||||
 | 
					    except subprocess.CalledProcessError as e:
 | 
				
			||||||
 | 
					        log.debug(traceback.format_exc(e))
 | 
				
			||||||
 | 
					        raise Exception("Cmd returned %s: %s" % ( e.returncode, cmd))
 | 
				
			||||||
 | 
					    except OSError as e:
 | 
				
			||||||
 | 
					        log.debug(traceback.format_exc(e))
 | 
				
			||||||
 | 
					        raise Exception("Cmd failed to execute: %s" % ( cmd ))
 | 
				
			||||||
@@ -39,6 +39,7 @@ disable_root: 1
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
cloud_config_modules:
 | 
					cloud_config_modules:
 | 
				
			||||||
 - mounts
 | 
					 - mounts
 | 
				
			||||||
 | 
					 - ssh-import-id
 | 
				
			||||||
 - ssh
 | 
					 - ssh
 | 
				
			||||||
 - apt-update-upgrade
 | 
					 - apt-update-upgrade
 | 
				
			||||||
 - puppet
 | 
					 - puppet
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -205,3 +205,10 @@ cloud_config_modules:
 | 
				
			|||||||
 - config-puppet
 | 
					 - config-puppet
 | 
				
			||||||
 - config-ssh
 | 
					 - config-ssh
 | 
				
			||||||
 - disable-ec2-metadata
 | 
					 - disable-ec2-metadata
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# ssh_import_id: [ user1, user2 ]
 | 
				
			||||||
 | 
					# ssh_import_id will feed the list in that variable to
 | 
				
			||||||
 | 
					#  ssh-import-lp-id, so that public keys stored in launchpad
 | 
				
			||||||
 | 
					#  can easily be imported into the configured user
 | 
				
			||||||
 | 
					# This can be a single string ('smoser') or a list ([smoser, kirkland])
 | 
				
			||||||
 | 
					ssh_import_id: [smoser]
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user