Skip to content

Commit 212495e

Browse files
authored
Merge pull request #513 from jpogran/GH-502-auto-detect-pdk-or-agent
(GH-502) Auto detect PDK or Puppet-Agent
2 parents 092f8aa + 74bf5b0 commit 212495e

File tree

7 files changed

+96
-51
lines changed

7 files changed

+96
-51
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,21 +331,22 @@ You can also install the extension without access to the internet by following t
331331

332332
## Configuration
333333

334-
The Puppet VSCode extension can use the PDK or the Puppet Agent to provide advanced features, which can be set using two configuration options: `puppet.installType` and `puppet.installDirectory`.
334+
### Puppet Source Configuration
335335

336-
- By default the `puppet.installType` is set to `agent`, and allows you to choose to use `pdk` or `agent`.
337-
- The `puppet.installDirectory` allows you to choose a custom path to either a PDK install or a Puppet-Agent install, and must match the correct setting in `puppet.installType`.
336+
The Puppet VSCode extension can use either the PDK or the Puppet Agent as a source to provide advanced features. Which source it uses, and what location to find them in, can be set using two configuration options: `puppet.installType` and `puppet.installDirectory`.
338337

339-
If no settings are chosen, the extension will attempt to use the Puppet Agent, to force it to use the PDK you have to select `pdk` for `puppet.installType`.
338+
- The `puppet.installType` setting allows you to choose to use `auto`, `pdk` or `agent`, and is set to `auto` by default.
339+
- The `puppet.installDirectory` allows you to choose a custom path to either a PDK install or a Puppet-Agent install, and must match the correct setting in `puppet.installType`.
340340

341+
> The default values for these settings will require no configuration by the user in order to use the extension if the PDK or Puppet Agent are installed in the default locations.
341342
342-
> Note: The `puppet.installDirectory` setting name will be changed in the `0.12.0` release to `puppet.installDirectory`
343+
#### Automatic Configuration
343344

344-
> Note: Only PDK v1.5.0 and above are supported.
345+
By default the extension attempts to automatically find a valid installation of the PDK on your system. If a PDK installation is not found, it attempts to find a Puppet Agent installation. The locations it looks for are the default install locations for each product. Exact default values for these locations can be found in the following sections for manually configuring the extension.
345346

346-
### Configure extension to use PDK
347+
#### Configure extension to use PDK
347348

348-
To ensure that the extension uses the PDK, set the `puppet.installType` setting to the `pdk` value like so:
349+
To ensure that the extension uses the `PDK`, set the `puppet.installType` setting to the `pdk` value like so:
349350

350351
```json
351352
{
@@ -379,15 +380,17 @@ Linux:
379380
}
380381
```
381382

382-
To use a custom install path for the PDK, set the `puppet.installDirectory` setting to the path you installed the PDK to:
383+
To use a custom install path for the `PDK`, set the `puppet.installDirectory` setting to the path you installed the `PDK` to:
383384

384385
```json
385386
{
386387
"puppet.installDirectory":"D:/programs/pdk"
387388
}
388389
```
389390

390-
The PDK has many versions of Puppet which can be used. Set the `puppet.editorService.puppet.version` setting to the version you would like to use, for example, if you wanted to use version 5.4.0, then set the configuration to:
391+
#### Configure Puppet Version in PDK
392+
393+
The `PDK` has many versions of Puppet which can be used. Set the `puppet.editorService.puppet.version` setting to the version you would like to use, for example, if you wanted to use version 5.4.0, then set the configuration to:
391394

392395
```json
393396
{
@@ -399,7 +402,7 @@ You can also change the version from the status bar in Visual Studio Code. Clic
399402

400403
![changing_puppet_version](https://raw.githubusercontent.com/lingua-pupuli/puppet-vscode/master/docs/assets/change-puppet-version.gif)
401404

402-
### Configure extension to use Puppet-Agent
405+
#### Configure extension to use Puppet-Agent
403406

404407
To ensure that the extension uses the Puppet-Agent, set the `puppet.installType` setting to the `puppet` value like so:
405408

package-lock.json

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,10 @@
402402
},
403403
"puppet.installType": {
404404
"type": "string",
405-
"default": "pdk",
406-
"description": "The type of Puppet installation. Either the Puppet Development Kit (pdk) or the Puppet Agent (agent)",
405+
"default": "auto",
406+
"description": "The type of Puppet installation. Either the Puppet Development Kit (pdk) or the Puppet Agent (agent). Choose 'auto' to have the extension detect which to use automatically based on default install locations",
407407
"enum": [
408+
"auto",
408409
"pdk",
409410
"agent"
410411
]

src/configuration.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ export class AggregateConfiguration implements IAggregateConfiguration {
6363
constructor(settings:ISettings) {
6464
this.workspace = settings;
6565

66+
// If the user has set the installType to 'auto' then we need
67+
// to resolve which install type we will actually use
68+
if (settings.installType === PuppetInstallType.AUTO) {
69+
if (fs.existsSync(this.getPdkBasePath())) {
70+
settings.installType = PuppetInstallType.PDK;
71+
} else if(fs.existsSync(this.getAgentBasePath())) {
72+
settings.installType = PuppetInstallType.PUPPET;
73+
} else {
74+
// We can't automatically figure it out so, assume PDK
75+
// TODO: Should we log this?
76+
settings.installType = PuppetInstallType.PDK;
77+
}
78+
}
79+
6680
const puppetBaseDir = this.calculatePuppetBaseDir(settings);
6781
const puppetDir = this.safeJoin(puppetBaseDir, 'puppet');
6882
const facterDir = this.safeJoin(puppetBaseDir, 'facter');
@@ -180,32 +194,36 @@ export class AggregateConfiguration implements IAggregateConfiguration {
180194
return settings.installDirectory;
181195
}
182196

183-
let programFiles = PathResolver.getprogramFiles();
184197
switch (settings.installType) {
185198
case PuppetInstallType.PDK:
186-
switch (process.platform) {
187-
case 'win32':
188-
return path.join(programFiles, 'Puppet Labs', 'DevelopmentKit');
189-
default:
190-
return path.join(programFiles, 'puppetlabs', 'pdk');
191-
}
199+
return this.getPdkBasePath();
192200
case PuppetInstallType.PUPPET:
193-
switch (process.platform) {
194-
case 'win32':
195-
// On Windows we have a subfolder called 'Puppet' that has
196-
// every product underneath
197-
return path.join(programFiles, 'Puppet Labs', 'Puppet');
198-
default:
199-
// On *nix we don't have a sub folder called 'Puppet'
200-
return path.join(programFiles, 'puppetlabs');
201-
}
201+
return this.getAgentBasePath();
202202
default:
203-
switch (process.platform) {
204-
case 'win32':
205-
return path.join(programFiles, 'Puppet Labs', 'DevelopmentKit');
206-
default:
207-
return path.join(programFiles, 'puppetlabs', 'pdk');
208-
}
203+
return this.getPdkBasePath();
204+
}
205+
}
206+
207+
private getAgentBasePath() {
208+
let programFiles = PathResolver.getprogramFiles();
209+
switch (process.platform) {
210+
case 'win32':
211+
// On Windows we have a subfolder called 'Puppet' that has
212+
// every product underneath
213+
return path.join(programFiles, 'Puppet Labs', 'Puppet');
214+
default:
215+
// On *nix we don't have a sub folder called 'Puppet'
216+
return path.join(programFiles, 'puppetlabs');
217+
}
218+
}
219+
220+
private getPdkBasePath() {
221+
let programFiles = PathResolver.getprogramFiles();
222+
switch (process.platform) {
223+
case 'win32':
224+
return path.join(programFiles, 'Puppet Labs', 'DevelopmentKit');
225+
default:
226+
return path.join(programFiles, 'puppetlabs', 'pdk');
209227
}
210228
}
211229

src/configuration/pdkResolver.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class PDKRubyInstances implements IPDKRubyInstances {
8181
if (!fs.existsSync(this.pdkDirectory)) { return this.rubyInstances; }
8282

8383
var rubyDir = path.join(this.pdkDirectory, 'private', 'ruby');
84+
if (!fs.existsSync(rubyDir)) { return this.rubyInstances; }
85+
8486
fs.readdirSync(rubyDir).forEach( (item) => {
8587
this.rubyInstances.push(new PDKRubyInstance(
8688
this.pdkDirectory,

src/extension.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import { ConnectionHandler } from './handler';
1616
import { DockerConnectionHandler } from './handlers/docker';
1717
import { StdioConnectionHandler } from './handlers/stdio';
1818
import { TcpConnectionHandler } from './handlers/tcp';
19-
import { ConnectionType, ProtocolType } from './settings';
19+
import { ConnectionType, ProtocolType, PuppetInstallType } from './settings';
2020
import { ILogger } from './logging';
2121
import { OutputChannelLogger } from './logging/outputchannel';
22-
//import { PuppetStatusBar } from './PuppetStatusBar';
2322
import { legacySettings, SettingsFromWorkspace } from './settings';
2423
import { Reporter, reporter } from './telemetry/telemetry';
2524

@@ -30,7 +29,6 @@ const debugType = 'Puppet'; // don't change this
3029
let extContext: vscode.ExtensionContext;
3130
let connectionHandler: ConnectionHandler;
3231
let logger: OutputChannelLogger;
33-
//let statusBar: PuppetStatusBar;
3432
let configSettings: IAggregateConfiguration;
3533
let extensionFeatures: IFeature[] = [];
3634

@@ -43,14 +41,19 @@ export function activate(context: vscode.ExtensionContext) {
4341
context.subscriptions.push(new Reporter(extContext));
4442

4543
const settings = SettingsFromWorkspace();
44+
const previousInstallType = settings.installType;
45+
configSettings = CreateAggregrateConfiguration(settings);
46+
logger = new OutputChannelLogger(configSettings.workspace.editorService.loglevel);
47+
if (configSettings.workspace.installType !== previousInstallType) {
48+
logger.debug(`Installation type has changed from ${previousInstallType} to ${configSettings.workspace.installType}`);
49+
}
50+
4651
reporter.sendTelemetryEvent('config', {
47-
'installType' : settings.installType,
48-
'protocol' : settings.editorService.protocol,
49-
'imageName' : settings.editorService.docker.imageName
52+
'installType' : configSettings.workspace.installType,
53+
'protocol' : configSettings.workspace.editorService.protocol,
54+
'imageName' : configSettings.workspace.editorService.docker.imageName
5055
});
51-
configSettings = CreateAggregrateConfiguration(settings);
5256

53-
logger = new OutputChannelLogger(configSettings.workspace.editorService.loglevel);
5457
const statusBar = new PuppetStatusBarFeature([puppetLangID, puppetFileLangID], configSettings, logger, context);
5558

5659
extensionFeatures = [
@@ -133,12 +136,25 @@ function checkInstallDirectory(config: IAggregateConfiguration, logger: ILogger)
133136

134137
// we want to check directory if STDIO or Local TCP
135138
if (!fs.existsSync(config.ruby.puppetBaseDir)) {
136-
showErrorMessage(
137-
`Could not find a valid Puppet installation at '${
139+
let message = '';
140+
// Need to use SettingsFromWorkspace() here because the AggregateConfiguration
141+
// changes the installType from Auto, to its calculated value
142+
if (SettingsFromWorkspace().installType === PuppetInstallType.AUTO) {
143+
let m = [
144+
'The extension failed to find a Puppet installation automatically in the default locations for PDK and for Puppet Agent.',
145+
'While syntax highlighting and grammar detection will still work, intellisense and other advanced features will not.',
146+
];
147+
message = m.join(' ');
148+
}else{
149+
message = `Could not find a valid Puppet installation at '${
138150
config.ruby.puppetBaseDir
139-
}'. While syntax highlighting and grammar detection will still work, intellisense and other advanced features will not.`,
140-
'Troubleshooting Information',
141-
'https://github.com/lingua-pupuli/puppet-vscode#experience-a-problem',
151+
}'. While syntax highlighting and grammar detection will still work, intellisense and other advanced features will not.`;
152+
}
153+
154+
showErrorMessage(
155+
message,
156+
'Configuration Information',
157+
'https://github.com/lingua-pupuli/puppet-vscode#configuration',
142158
logger
143159
);
144160
return false;

src/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import vscode = require("vscode");
55
export enum PuppetInstallType{
66
PDK = "pdk",
77
PUPPET = "agent",
8+
AUTO = "auto",
89
}
910

1011
export enum ProtocolType {
@@ -163,7 +164,7 @@ export function DefaultWorkspaceSettings(): ISettings {
163164
enable: true
164165
},
165166
installDirectory: undefined,
166-
installType: PuppetInstallType.PDK,
167+
installType: PuppetInstallType.AUTO,
167168
lint: {
168169
enable: true,
169170
},

0 commit comments

Comments
 (0)