forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
204 lines (181 loc) · 7.54 KB
/
index.ts
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
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi";
import * as scp2 from "scp2";
import * as ssh2 from "ssh2";
import { Provisioner } from "./provisioner";
// ConnectionArgs tells a provisioner how to access a remote resource. For example, it may need to use
// SSH or WinRM to connect to the resource in order to run a command or copy a file.
export interface ConnectionArgs {
type?: ConnectionType;
host: pulumi.Input<string>;
port?: pulumi.Input<number>;
username?: pulumi.Input<string>;
password?: pulumi.Input<string>;
privateKey?: pulumi.Input<string>;
privateKeyPassphrase?: pulumi.Input<string>;
}
// ConnectionType is the set of legal connection mechanisms to use. Default is SSH.
export type ConnectionType = "ssh" | "winrm";
function connToSsh2(conn: pulumi.Unwrap<ConnectionArgs>): any {
return {
host: conn.host,
port: conn.port,
username: conn.username,
password: conn.password,
privateKey: conn.privateKey,
passphrase: conn.privateKeyPassphrase,
};
}
function copyFile(conn: pulumi.Unwrap<ConnectionArgs>, src: string, dest: string): Promise<never> {
const connType = conn.type || "ssh";
if (connType !== "ssh") {
throw new Error("only SSH connection types currently supported");
}
let connectionFailCount = 0;
return new Promise((resolve, reject) => {
function scp() {
scp2.scp(
src,
{ path: dest, ...connToSsh2(conn) },
(err) => {
if (err) {
connectionFailCount++;
if (connectionFailCount > 10) {
reject(err);
} else {
setTimeout(scp, connectionFailCount * 500);
}
return;
}
resolve();
},
);
}
scp();
});
}
// RunCommandResult is the result of running a command.
export interface RunCommandResult {
// The stdout of the command that was executed.
stdout: string;
// The stderr of the command that was executed.
stderr: string;
// The exit code of the command that was executed.
code: number;
}
function runCommand(conn: pulumi.Unwrap<ConnectionArgs>, cmd: string): Promise<RunCommandResult> {
const connType = conn.type || "ssh";
if (connType !== "ssh") {
throw new Error("only SSH connection types currently supported");
}
const sshConn = connToSsh2(conn);
let connectionFailCount = 0;
return new Promise((resolve, reject) => {
const conn = new ssh2.Client();
function connect() {
conn.on("ready", () => {
conn.exec(cmd, (err, stream) => {
if (err) {
reject(err);
return;
}
let stdout = "";
let stderr = "";
stream.on("close", (code, signal) => {
conn.end();
if (code) {
reject(new Error("Command exited with " + code));
} else {
resolve({ stdout, stderr, code });
}
}).on("data", (data) => {
stdout += data.toString("utf8");
}).stderr.on("data", (data) => {
stderr += data.toString("utf8");
});
});
}).on("error", (err) => {
connectionFailCount++;
if (connectionFailCount > 10) {
reject(err);
} else {
setTimeout(connect, connectionFailCount * 500);
}
}).connect(sshConn);
}
connect();
});
}
// CopyFile is a provisioner step that can copy a file from the machine running Pulumi to the newly created resource.
export class CopyFile extends pulumi.ComponentResource {
private readonly provisioner: Provisioner<ConnectionArgs, never>;
constructor(name: string, args: CopyFileArgs, opts?: pulumi.ComponentResourceOptions) {
super("pulumi:provisioners:CopyFile", name, args, opts);
this.provisioner = new Provisioner<ConnectionArgs, never>(
`${name}-provisioner`,
{
dep: args.conn,
changeToken: args.changeToken,
onCreate: (conn) => copyFile(conn, args.src, args.dest),
},
{ parent: this },
);
}
}
interface ChangeTrackingProvisioner {
// changeToken allows you to specify a value that controls the replacement of the dependent dynamic resources.
// Specifing a value that changes with the content of the file(s) allows the provisioner resources to be
// replaced every time the content(s) change.
changeToken?: string;
}
export interface CopyFileArgs extends ChangeTrackingProvisioner {
// conn contains information on how to connect to the destination, in addition to dependency information.
conn: pulumi.Input<ConnectionArgs>;
// src is the source of the file or directory to copy. It can be specified as relative to the current
// working directory or as an absolute path. This cannot be specified if content is set.
src: string;
// dest is required and specifies the absolute path on the target where the file will be copied to.
dest: string;
}
// RemoteExec runs remote commands and/or invokes scripts. If commands and scripts are specified, they are
// run in the following order: command, commands, script, and finally then scripts.
export class RemoteExec extends pulumi.ComponentResource {
private readonly provisioner: Provisioner<ConnectionArgs, RunCommandResult[]>;
// The results of all commands executed remotely.
public readonly results: pulumi.Output<RunCommandResult[]>;
// The result of the first command executed remotely.
public readonly result: pulumi.Output<RunCommandResult>;
constructor(name: string, args: RemoteExecArgs, opts?: pulumi.ComponentResourceOptions) {
super("pulumi:provisioners:RemoteExec", name, args, opts);
if (args.command !== undefined && args.commands !== undefined) {
throw new Error("Exactly one of 'command' or 'commands' should be provided.");
}
this.provisioner = new Provisioner<ConnectionArgs, RunCommandResult[]>(
`${name}-provisioner`,
{
dep: args.conn,
onCreate: async (conn) => {
const commands = args.commands || [args.command];
const results: RunCommandResult[] = [];
for (const cmd of commands) {
const result = await runCommand(conn, cmd);
results.push(result);
}
return results;
},
changeToken: args.changeToken,
},
{ parent: this },
);
this.result = this.provisioner.result[0];
this.results = this.provisioner.result;
}
}
export interface RemoteExecArgs extends ChangeTrackingProvisioner {
// The connection to use for the remote command execution.
conn: ConnectionArgs;
// The command to execute. Exactly one of 'command' and 'commands' is required.
command?: string;
// The commands to execute. Exactly one of 'command' and 'commands' is required.
commands?: string[];
}