@@ -41,12 +41,19 @@ def get_parser(self, prog_name):
4141 metavar = '<name>' ,
4242 help = _ ("New public or private key name" )
4343 )
44- parser .add_argument (
44+ key_group = parser .add_mutually_exclusive_group ()
45+ key_group .add_argument (
4546 '--public-key' ,
4647 metavar = '<file>' ,
4748 help = _ ("Filename for public key to add. If not used, "
4849 "creates a private key." )
4950 )
51+ key_group .add_argument (
52+ '--private-key' ,
53+ metavar = '<file>' ,
54+ help = _ ("Filename for private key to save. If not used, "
55+ "print private key in console." )
56+ )
5057 return parser
5158
5259 def take_action (self , parsed_args ):
@@ -69,13 +76,31 @@ def take_action(self, parsed_args):
6976 public_key = public_key ,
7077 )
7178
79+ private_key = parsed_args .private_key
80+ # Save private key into specified file
81+ if private_key :
82+ try :
83+ with io .open (
84+ os .path .expanduser (parsed_args .private_key ), 'w+'
85+ ) as p :
86+ p .write (keypair .private_key )
87+ except IOError as e :
88+ msg = _ ("Key file %(private_key)s can not be saved: "
89+ "%(exception)s" )
90+ raise exceptions .CommandError (
91+ msg % {"private_key" : parsed_args .private_key ,
92+ "exception" : e }
93+ )
7294 # NOTE(dtroyer): how do we want to handle the display of the private
7395 # key when it needs to be communicated back to the user
7496 # For now, duplicate nova keypair-add command output
7597 info = {}
76- if public_key :
98+ if public_key or private_key :
7799 info .update (keypair ._info )
78- del info ['public_key' ]
100+ if 'public_key' in info :
101+ del info ['public_key' ]
102+ if 'private_key' in info :
103+ del info ['private_key' ]
79104 return zip (* sorted (six .iteritems (info )))
80105 else :
81106 sys .stdout .write (keypair .private_key )
0 commit comments