This repository was archived by the owner on Mar 5, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathGetPuppetModuleCommands.ps1
More file actions
277 lines (255 loc) · 13.5 KB
/
Copy pathGetPuppetModuleCommands.ps1
File metadata and controls
277 lines (255 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null;
try {
$Script:SettingsPath = Join-Path -Path:(Split-Path -Parent -Path:$MyInvocation.MyCommand.Path) -ChildPath:'GetPuppetModuleCommands.xml';
if ($Script:SettingsPath | Test-Path -PathType:Leaf) {
$Script:Settings = $Script:SettingsPath | Import-Clixml;
} else {
$Script:Settings = @{
Module = @{
UserName = "platinumStorm";
ModuleName = "my_module_name";
Version = "0.1.0";
};
System = @{
PuppetPath = "/usr/local/bin/puppet";
ModulePath = "/etc/puppetlabs/puppet/modules";
WorkingDir = "~/puppet_dev";
}
};
}
} catch {
$Error[0] | Out-String | Write-Host -ForegroundColor:Red;
return;
}
Function Invoke-WriteAndCopy {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[string[]]$Text
)
Begin {
$Result = @();
}
Process {
$Result = @($Result) + $_;
}
End {
[Windows.Forms.Clipboard]::SetText(($Result | Out-String).Trim());
(@("", "Copied to clipboard:", "") + $Result) | Write-Host;
}
}
$Script:DevCmdChoices = [System.Management.Automation.Host.ChoiceDescription[]]@(
(New-Object System.Management.Automation.Host.ChoiceDescription("&Create Boilerplate Code", "Commands for creating boilerplate module code")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Validate code", "Commands for validating your manifest/init.pp file")),
(New-Object System.Management.Automation.Host.ChoiceDescription("Build &Module", "Commands for building your module into a package file")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Remove Package", "Commands for removing the package files where were created when the module package was built")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Back", "Go back to main menu"))
);
Function Invoke-DevCmd {
[CmdletBinding()]
Param()
$selectedCmd = 0;
while ($selectedCmd -ne ($Script:DevCmdChoices.Length - 1)) {
$selectedCmd = $host.ui.PromptforChoice("Select Command","Select Linux command you'd like to generate", $Script:DevCmdChoices, 4);
switch ($selectedCmd) {
0 {
@(
("cd {0}" -f $Script:Settings.System.WorkingDir),
("{0} module generate {1}-{2}" -f $Script:Settings.System.PuppetPath, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName)
) | Invoke-WriteAndCopy;
}
1 {
@(
("cd {0}/{1}-{2}" -f $Script:Settings.System.WorkingDir, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName),
("{0} parser validate manifests/init.pp" -f $Script:Settings.System.PuppetPath)
) | Invoke-WriteAndCopy;
}
2 {
@(
("cd {0}" -f $Script:Settings.System.WorkingDir),
("{0} module build {1}-{2}" -f $Script:Settings.System.PuppetPath, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName),
("cd {0}-{1}" -f $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName),
'find . ! -type d -exec /bin/chmod 644 {} \;',
'find . -type d -exec /bin/chmod 755 {} \;'
) | Invoke-WriteAndCopy;
}
3 {
@(
("cd {0}/{1}-{2}" -f $Script:Settings.System.WorkingDir, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName),
"sudo rm -r pkg"
) | Invoke-WriteAndCopy;
}
}
}
}
$Script:InstallCmdChoices = [System.Management.Automation.Host.ChoiceDescription[]]@(
(New-Object System.Management.Automation.Host.ChoiceDescription("&Install", "Command to install your module from the package file")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Uninstall", "Command to uninstall your module")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&List installed modules", "Command to produce a list of installed modules")),
(New-Object System.Management.Automation.Host.ChoiceDescription("View Module &Path", "Command to display module paths")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Back", "Go back to main menu"))
);
Function Invoke-InstallCmd {
[CmdletBinding()]
Param()
$selectedCmd = 0;
while ($selectedCmd -ne $Script:InstallCmdChoices.Length - 1) {
"" | Write-Host;
$selectedCmd = $host.ui.PromptforChoice("Select Command","Select Linux command you'd like to generate", $Script:InstallCmdChoices, 4);
switch ($selectedCmd) {
0 {
@(
("cd {0}/{1}-{2}" -f $Script:Settings.System.WorkingDir, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName),
("sudo {0} module install pkg/{1}-{2}-{3}.tar.gz" -f $Script:Settings.System.PuppetPath, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName, $Script:Settings.Module.Version),
("cd {0}/{1}" -f $Script:Settings.System.ModulePath, $Script:Settings.Module.ModuleName),
'sudo find . ! -type d -exec chmod 644 {} \;',
'sudo find . -type d -exec chmod 755 {} \;'
) | Invoke-WriteAndCopy;
}
1 {
("sudo {0} module uninstall {1}-{2}" -f $Script:Settings.System.PuppetPath, $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName) | Invoke-WriteAndCopy;
}
2 {
("sudo {0} module list --tree" -f $Script:Settings.System.WorkingDir) | Invoke-WriteAndCopy;
}
3 {
("sudo ls -al {0}" -f $Script:Settings.System.ModulePath) | Invoke-WriteAndCopy;
}
}
}
}
$Script:ChangeModuleCmdChoices = [System.Management.Automation.Host.ChoiceDescription[]]@(
(New-Object System.Management.Automation.Host.ChoiceDescription("&UserName", "Change UserName associated with your module")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&ModuleName", "Change name of module")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Version", "Change moduel version")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Back", "Go back to main menu"))
);
Function Invoke-ChangeModule {
[CmdletBinding()]
Param()
$selectedCmd = 0;
while ($selectedCmd -ne $Script:ChangeModuleCmdChoices.Length - 1) {
@(
"",
"Change Module Parameters",
"",
(" User Name: {0}" -f $Script:Settings.Module.UserName),
(" Module Name: {0}" -f $Script:Settings.Module.ModuleName),
(" Version: {0}" -f $Script:Settings.Module.Version)
("Package File Name: {0}-{1}-{2}.tar.gz" -f $Script:Settings.Module.UserName, $Script:Settings.Module.ModuleName, $Script:Settings.Module.Version),
""
) | Write-Host;
$selectedCmd = $host.ui.PromptforChoice("Select Command","Please select parameter you'd like to change", $Script:ChangeModuleCmdChoices, 3);
switch ($selectedCmd) {
0 {
@(
"",
("Current user name: {0}" -f $Script:Settings.Module.UserName),
""
) | Write-Host;
$v = (Read-Host -Prompt:"Enter User Name portion of module (enter blank to accept current value)");
if ($v -ne $null -and $v.Trim().Length -gt 0) { $Script:Settings.Module.UserName = $v }
}
1 {
@(
"",
("Current module name: {0}" -f $Script:Settings.Module.ModuleName),
""
) | Write-Host;
$v = (Read-Host -Prompt:"Enter Module Name portion of module (enter blank to accept current value)");
if ($v -ne $null -and $v.Trim().Length -gt 0) { $Script:Settings.Module.ModuleName = $v }
}
2 {
@(
"",
("Current version: {0}" -f $Script:Settings.Module.Version),
""
) | Write-Host;
$v = (Read-Host -Prompt:"Enter Module Version (enter blank to accept current value)");
if ($v -ne $null -and $v.Trim().Length -gt 0) { $Script:Settings.Module.Version = $v }
}
}
Export-Clixml -Path:$Script:SettingsPath -InputObject:$Script:Settings -Force
}
}
$Script:ChangeSysParamCmdChoices = [System.Management.Automation.Host.ChoiceDescription[]]@(
(New-Object System.Management.Automation.Host.ChoiceDescription("&Puppet Path", "Change path to the Puppet executable")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Module Path", "Change path where puppet places your module when it is installed")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Working Dir", "Change your working directory on the Linux server")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Back", "Go back to main menu"))
);
Function Invoke-ChangeSysParam {
[CmdletBinding()]
Param()
$selectedCmd = 0;
while ($selectedCmd -ne $Script:ChangeSysParamCmdChoices.Length - 1) {
@(
"",
"Change Linux System Parameters",
"",
(" Puppet Path: {0}" -f $Script:Settings.System.PuppetPath),
(" Module Path: {0}" -f $Script:Settings.System.ModulePath),
("Your Working Dir: {0}" -f $Script:Settings.System.WorkingDir),
""
) | Write-Host;
$selectedCmd = $host.ui.PromptforChoice("Select Command","Please select the parameter you'd like to change", $Script:ChangeSysParamCmdChoices, 3);
switch ($selectedCmd) {
0 {
@(
"",
("Current Puppet path: {0}" -f $Script:Settings.System.PuppetPath),
""
) | Write-Host;
$v = (Read-Host -Prompt:"Enter path to Puppet Executable (enter blank to accept current value)");
if ($v -ne $null -and $v.Trim().Length -gt 0) { $Script:Settings.System.PuppetPath = $v }
}
1 {
@(
"",
("Current Module path: {0}" -f $Script:Settings.System.ModulePath),
""
) | Write-Host;
$v = (Read-Host -Prompt:"Enter path to installed modules (enter blank to accept current value)");
if ($v -ne $null -and $v.Trim().Length -gt 0) { $Script:Settings.System.ModulePath = $v }
}
2 {
@(
"",
("Current Working Directory: {0}" -f $Script:Settings.System.WorkingDir),
""
) | Write-Host;
$v = (Read-Host -Prompt:"Enter path to your working directory on linux server (enter blank to accept current value)");
if ($v -ne $null -and $v.Trim().Length -gt 0) { $Script:Settings.System.WorkingDir = $v }
}
}
Export-Clixml -Path:$Script:SettingsPath -InputObject:$Script:Settings -Force
}
}
$selectedCmdGroup = 0;
$cmdGroupChoices = [System.Management.Automation.Host.ChoiceDescription[]]@(
(New-Object System.Management.Automation.Host.ChoiceDescription("&Development Commands", "Generate bash commands relevant to Puppet script development")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Install/Uninstall Commands", "Generate bash commands relevant to installing and uninstalling your module")),
(New-Object System.Management.Automation.Host.ChoiceDescription("Change &Module Name", "Change module name and version")),
(New-Object System.Management.Automation.Host.ChoiceDescription("Change &System Parameters", "Change parametrs which tells this script about your Linux environment")),
(New-Object System.Management.Automation.Host.ChoiceDescription("&Quit", "Quit this PowerShell app"))
);
while ($selectedCmdGroup -ne $cmdGroupChoices.Length - 1) {
@(
"",
"Puppet Module Development Command Generator",
"",
(" Puppet Path: {0}" -f $Script:Settings.System.PuppetPath),
(" Module Path: {0}" -f $Script:Settings.System.ModulePath),
("Your Working Dir: {0}" -f $Script:Settings.System.WorkingDir),
(" User Name: {0}" -f $Script:Settings.Module.UserName),
(" Module Name: {0}" -f $Script:Settings.Module.ModuleName),
(" Version: {0}" -f $Script:Settings.Module.Version)
) | Write-Host;
$selectedCmdGroup = $host.ui.PromptforChoice("Select Command Group","Please select the command type", $cmdGroupChoices, 4);
switch ($selectedCmdGroup) {
0 { Invoke-DevCmd }
1 { Invoke-InstallCmd }
2 { Invoke-ChangeModule }
3 { Invoke-ChangeSysParam }
}
}