13
13
// ----------------------------------------------------------------------------------
14
14
15
15
using System ;
16
+ using System . Diagnostics ;
17
+ using System . IO ;
16
18
using System . Management . Automation ;
17
19
using Microsoft . Azure . Commands . Aks . Models ;
18
20
using Microsoft . Azure . Commands . Aks . Properties ;
@@ -31,10 +33,16 @@ public class NewAzureRmAks : NewKubeBase
31
33
[ Parameter ( Mandatory = false , HelpMessage = "Create cluster even if it already exists" ) ]
32
34
public SwitchParameter Force { get ; set ; }
33
35
36
+ [ Parameter (
37
+ Mandatory = false ,
38
+ HelpMessage = "Generate ssh key file to {HOME}/.ssh/id_rsa." ) ]
39
+ public SwitchParameter GenerateSshKey { get ; set ; }
40
+
34
41
public override void ExecuteCmdlet ( )
35
42
{
36
43
base . ExecuteCmdlet ( ) ;
37
44
PreValidate ( ) ;
45
+ PrepareParameter ( ) ;
38
46
39
47
Action action = ( ) =>
40
48
{
@@ -72,6 +80,76 @@ private void PreValidate()
72
80
if ( ( this . IsParameterBound ( c => c . NodeMinCount ) || this . IsParameterBound ( c => c . NodeMaxCount ) || this . EnableNodeAutoScaling . IsPresent ) &&
73
81
! ( this . IsParameterBound ( c => c . NodeMinCount ) && this . IsParameterBound ( c => c . NodeMaxCount ) && this . EnableNodeAutoScaling . IsPresent ) )
74
82
throw new PSInvalidCastException ( Resources . AksNodePoolAutoScalingParametersMustAppearTogether ) ;
83
+
84
+ if ( this . IsParameterBound ( c => c . GenerateSshKey ) && this . IsParameterBound ( c => c . SshKeyValue ) )
85
+ {
86
+ throw new ArgumentException ( string . Format ( Resources . DonotUseGenerateSshKeyWithSshKeyValue ) ) ;
87
+ }
88
+ }
89
+
90
+ private void VerifySshKeyGenBinaryExist ( )
91
+ {
92
+ using ( Process process = new Process ( ) )
93
+ {
94
+ if ( Environment . OSVersion . Platform . ToString ( ) . Contains ( "Win" ) )
95
+ {
96
+ process . StartInfo . FileName = "where.exe" ;
97
+ }
98
+ else
99
+ {
100
+ process . StartInfo . FileName = "whereis" ;
101
+ }
102
+ process . StartInfo . Arguments = "ssh-keygen" ;
103
+ process . StartInfo . UseShellExecute = false ;
104
+ process . StartInfo . RedirectStandardOutput = true ;
105
+
106
+ process . Start ( ) ;
107
+ process . WaitForExit ( ) ;
108
+
109
+ string result = process . StandardOutput . ReadLine ( ) ;
110
+ if ( result . Contains ( "not found" ) || result . Contains ( "Could not find" ) || result . Trim ( ) . Equals ( "ssh-keygen:" ) )
111
+ {
112
+ throw new ArgumentException ( string . Format ( Resources . EnableSsh ) ) ;
113
+ }
114
+
115
+ if ( process . ExitCode != 0 )
116
+ {
117
+ throw new ArgumentException ( string . Format ( Resources . EnableSsh ) ) ;
118
+ }
119
+ }
120
+ }
121
+
122
+ private string GenerateSshKeyValue ( )
123
+ {
124
+ VerifySshKeyGenBinaryExist ( ) ;
125
+ String generateSshKeyPath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".ssh" , "id_rsa" ) ; ;
126
+ if ( File . Exists ( generateSshKeyPath ) )
127
+ {
128
+ throw new ArgumentException ( string . Format ( Resources . DefaultSshKeyAlreadyExist ) ) ;
129
+ }
130
+ using ( Process process = new Process ( ) )
131
+ {
132
+ process . StartInfo . FileName = "ssh-keygen" ;
133
+ process . StartInfo . Arguments = "-f " + generateSshKeyPath ;
134
+ process . StartInfo . UseShellExecute = false ;
135
+ process . StartInfo . RedirectStandardInput = true ;
136
+ process . StartInfo . RedirectStandardError = true ;
137
+ process . StartInfo . RedirectStandardOutput = true ;
138
+ process . Start ( ) ;
139
+
140
+ Console . WriteLine ( process . StandardOutput . ReadToEnd ( ) ) ;
141
+
142
+ process . WaitForExit ( ) ;
143
+ }
144
+ return GetSshKey ( generateSshKeyPath ) ;
145
+ }
146
+
147
+ protected void PrepareParameter ( )
148
+ {
149
+ if ( this . IsParameterBound ( c => c . GenerateSshKey ) )
150
+ {
151
+ SshKeyValue = GenerateSshKeyValue ( ) ;
152
+ }
75
153
}
76
154
}
77
155
}
0 commit comments