Skip to content

Commit

Permalink
Puppetfile support (with librarian-puppet)
Browse files Browse the repository at this point in the history
  • Loading branch information
desertkun committed Jan 9, 2019
1 parent d202458 commit 2808bf3
Show file tree
Hide file tree
Showing 8 changed files with 10,967 additions and 30 deletions.
7,533 changes: 7,533 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ gem 'rdoc'
gem 'yard'
gem 'puppet'
gem 'puppet-strings'
gem 'certified'
gem 'librarian-puppet'
30 changes: 30 additions & 0 deletions ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
GEM
remote: https://rubygems.org/
specs:
certified (1.0.0)
facter (2.5.1)
facter (2.5.1-x64-mingw32)
ffi (~> 1.9.5)
facter (2.5.1-x86-mingw32)
ffi (~> 1.9.5)
faraday (0.13.1)
multipart-post (>= 1.2, < 3)
faraday_middleware (0.12.2)
faraday (>= 0.7.4, < 1.0)
fast_gettext (1.1.2)
ffi (1.9.25-x64-mingw32)
ffi (1.9.25-x86-mingw32)
gettext (3.2.9)
locale (>= 2.0.5)
text (>= 1.3.0)
gettext-setup (0.30)
fast_gettext (~> 1.1.0)
gettext (>= 3.0.2)
locale
hiera (3.5.0)
hocon (1.2.5)
httpclient (2.8.3)
librarian-puppet (3.0.0)
librarianp (>= 0.6.3)
puppet_forge (~> 2.1)
rsync
librarianp (0.6.4)
thor (~> 0.15)
locale (2.1.2)
minitar (0.6.1)
multi_json (1.13.1)
multipart-post (2.0.0)
puppet (6.1.0)
facter (> 2.0.1, < 4)
fast_gettext (~> 1.1.2)
Expand Down Expand Up @@ -59,9 +78,18 @@ GEM
puppet-strings (2.1.0)
rgen
yard (~> 0.9.5)
puppet_forge (2.2.9)
faraday (>= 0.9.0, < 0.14.0)
faraday_middleware (>= 0.9.0, < 0.13.0)
gettext-setup (~> 0.11)
minitar
semantic_puppet (~> 1.0)
rdoc (6.1.1)
rgen (0.8.2)
rsync (1.0.9)
semantic_puppet (1.0.2)
text (1.3.1)
thor (0.20.3)
win32-dir (0.4.9)
ffi (>= 1.0.0)
win32-process (0.7.5)
Expand All @@ -78,6 +106,8 @@ PLATFORMS
x86-mingw32

DEPENDENCIES
certified
librarian-puppet
puppet
puppet-strings
rdoc
Expand Down
3,218 changes: 3,218 additions & 0 deletions ruby/cacert.pem

Large diffs are not rendered by default.

54 changes: 52 additions & 2 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as child_process from "child_process";

import * as YAML from "yaml";

const stream = require('stream');

export function fileExists(path: string): Promise<boolean>
{
return new Promise<boolean>((resolve, reject) =>
Expand Down Expand Up @@ -251,13 +253,14 @@ export function listFiles(path: string): Promise<string[]>
});
}

export function execFile(path: string, args: Array<string>, cwd: string): Promise<any>
export function execFile(path: string, args: Array<string>, cwd: string, env?: any): Promise<any>
{
return new Promise<boolean>((resolve, reject) =>
{
const options: child_process.ExecFileOptions = {
'cwd': cwd,
'maxBuffer': 1024000
'maxBuffer': 1024000,
'env': env
};

child_process.execFile(path, args, options, (error: Error, stdout: string, stderr: string) =>
Expand All @@ -274,6 +277,53 @@ export function execFile(path: string, args: Array<string>, cwd: string): Promis
});
}

export type ExecFileLineCallback = (line: string) => void;

export function execFileReadIn(command: string, cwd: string, env?: any, cb?: ExecFileLineCallback): Promise<any>
{
return new Promise<boolean>((resolve, reject) =>
{
const options: child_process.ExecFileOptions = {
'cwd': cwd,
'maxBuffer': 1024000,
'env': env
};

const process = child_process.exec(command, options, (error: Error, stdout: string, stderr: string) =>
{
if (error != null)
{
reject(stdout);
}
else
{
resolve();
}
});

if (cb != null)
{
process.stdout.setEncoding('utf8');
process.stdout.on('data', function(data)
{
var str = data.toString(), lines = str.split(/\r?\n/g);

for (let i = lines.length - 1; i >= 0; i--)
{
const line = lines[i];
if (line != "")
{
cb(line);
break;
}
}

return true;
});
}
});
}

export function execFileInOut(path: string, args: Array<string>, cwd: string, data: string): Promise<string>
{
return new Promise<string>((resolve, reject) =>
Expand Down
6 changes: 3 additions & 3 deletions src/ipc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class IpcServer implements IpcAPI
}

const files = await listFiles(path);

if (files.length > 0)
{
throw new Error("Please choose an empty directory");
Expand Down Expand Up @@ -319,8 +319,8 @@ export class IpcServer implements IpcAPI

await workspace.refresh((progress: number) => {
workspace_window.browserWindow.webContents.send("refreshWorkspaceProgress", progress);
}, (text: string) => {
workspace_window.browserWindow.webContents.send("refreshWorkspaceCategory", text);
}, (text: string, showProgress: boolean) => {
workspace_window.browserWindow.webContents.send("refreshWorkspaceCategory", text, showProgress);
});
}

Expand Down
141 changes: 118 additions & 23 deletions src/puppet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ export module puppet
return null;
}

public static async CallBin(command: string, args: string[], cwd: string, cb?: async.ExecFileLineCallback): Promise<boolean>
{
const argsTotal = [
Ruby.Path().rubyPath,
path.join(Ruby.Path().path, command)
];

for (let arg of args)
{
argsTotal.push(arg);
}

const env: any = {
"SSL_CERT_FILE": require('app-root-path').resolve("ruby", "cacert.pem"),
"PATH": process.env["PATH"] + path.delimiter + Ruby.Path().path
};

try
{
await async.execFileReadIn("\"" + argsTotal.join("\" \"") + "\"", cwd, env, cb);
return true;
}
catch (e)
{
console.log("Failed to execute command " + command + ": " + e);
return false;
}
}

public static async Call(script: string, args: Array<string>, cwd: string): Promise<boolean>
{
const rubyScript = require('app-root-path').resolve(path.join("ruby", script));
Expand Down Expand Up @@ -544,15 +573,22 @@ export module puppet
if (cachedStats[clazz.file])
{
const cachedStat = cachedStats[clazz.file];
const cachedTime: Number = cachedStat.mtimeMs;

const realStat = realStats[clazz.file];
const realTime: Number = realStat.mtimeMs;

if (cachedTime >= realTime)
if (cachedStat != null)
{
// compiled file is up-to-date
continue;
const cachedTime: Number = cachedStat.mtimeMs;

const realStat = realStats[clazz.file];

if (realStat != null)
{
const realTime: Number = realStat.mtimeMs;

if (cachedTime >= realTime)
{
// compiled file is up-to-date
continue;
}
}
}
}

Expand All @@ -566,15 +602,20 @@ export module puppet
if (cachedStats[definedType.file])
{
const cachedStat = cachedStats[definedType.file];
const cachedTime: Number = cachedStat.mtimeMs;

const realStat = realStats[definedType.file];
const realTime: Number = realStat.mtimeMs;

if (cachedTime >= realTime)
if (cachedStat != null)
{
// compiled file is up-to-date
continue;
const cachedTime: Number = cachedStat.mtimeMs;

const realStat = realStats[definedType.file];
if (realStat != null)
{
const realTime: Number = realStat.mtimeMs;
if (cachedTime >= realTime)
{
// compiled file is up-to-date
continue;
}
}
}
}

Expand Down Expand Up @@ -799,6 +840,31 @@ export module puppet
{
return this._name;
}

private async installModules(updateProgressCategory: any): Promise<void>
{
if (await async.isFile(path.join(this.path, "Puppetfile")))
{
if (updateProgressCategory) updateProgressCategory("Installing modules...", false);

try
{
await Ruby.CallBin("librarian-puppet", ["install", "--verbose"], this._path, (line: string) =>
{
if (line.length > 80)
{
line = line.substr(0, 80) + " ...";
}

if (updateProgressCategory) updateProgressCategory(line, false);
});
}
catch (e)
{
throw new WorkspaceError("Failed to install modules: " + e.toString());
}
}
}

public async refresh(progressCallback: any = null, updateProgressCategory: any = null): Promise<any>
{
Expand All @@ -815,9 +881,11 @@ export module puppet
}
}

await this.installModules(updateProgressCategory);

let upToDate: boolean = false;

if (updateProgressCategory) updateProgressCategory("Processing classes...");
if (updateProgressCategory) updateProgressCategory("Processing classes...", false);

const bStat = await async.fileStat(this.cacheModulesFilePath);
if (bStat)
Expand All @@ -835,7 +903,7 @@ export module puppet

if (!upToDate)
{
if (updateProgressCategory) updateProgressCategory("Extracting class info...");
if (updateProgressCategory) updateProgressCategory("Extracting class info...", false);

const a = JSON.stringify([
"*/manifests/**/*.pp", "*/functions/**/*.pp", "*/types/**/*.pp", "*/lib/**/*.rb"
Expand Down Expand Up @@ -877,18 +945,18 @@ export module puppet
};
}

if (updateProgressCategory) updateProgressCategory("Compiling classes...");
if (updateProgressCategory) updateProgressCategory("Compiling classes...", true);

await pool.start();
}

for (const env of await this.listEnvironments())
{
if (updateProgressCategory) updateProgressCategory("Processing environment: " + env.name);
await env.refresh(progressCallback)
if (updateProgressCategory) updateProgressCategory("Processing environment: " + env.name, false);
await env.refresh(progressCallback, updateProgressCategory)
}

if (updateProgressCategory) updateProgressCategory("Processing environments complete!");
if (updateProgressCategory) updateProgressCategory("Processing environments complete!", false);
}

private async loadModulesInfo(): Promise<PuppetModulesInfo>
Expand Down Expand Up @@ -1239,7 +1307,32 @@ export module puppet
}
}

public async refresh(progressCallback: any = null): Promise<any>
private async installModules(updateProgressCategory: any): Promise<void>
{
if (await async.isFile(path.join(this.path, "Puppetfile")))
{
if (updateProgressCategory) updateProgressCategory("Installing modules...", false);

try
{
await Ruby.CallBin("librarian-puppet", ["install", "--verbose"], this._path, (line: string) =>
{
if (line.length > 80)
{
line = line.substr(0, 80) + " ...";
}

if (updateProgressCategory) updateProgressCategory(line, false);
});
}
catch (e)
{
throw new WorkspaceError("Failed to install modules: " + e.toString());
}
}
}

public async refresh(progressCallback: any = null, updateProgressCategory: any = null): Promise<any>
{
if (!await async.isDirectory(this.cachePath))
{
Expand All @@ -1249,6 +1342,8 @@ export module puppet
}
}

await this.installModules(updateProgressCategory);

if (await async.isDirectory(this.modulesPath))
{
let upToDate: boolean = false;
Expand Down
Loading

0 comments on commit 2808bf3

Please sign in to comment.