@pulumi/command 0.0.1-alpha.1639756016 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,22 +1,313 @@
1
- # command Pulumi Provider
1
+ # Pulumi Command Provider (preview)
2
2
 
3
- Use cases:
4
- * Post create hook
5
- * Readiness/health check
6
- * `aws` CLI
7
- * `kubectl patch`
3
+ The Pulumi Command Provider enables you to execute commands and scripts either locally or remotely as part of the Pulumi resource model. Resources in the command package support running scripts on `create` and `destroy` operations, supporting stateful local and remote command execution.
8
4
 
9
- ## Build and Test
5
+ There are many scenarios where the Command package can be useful:
10
6
 
11
- ```bash
12
- # build and install the resource provider plugin
13
- $ make build install
7
+ * Running a command locally after creating a resource, to register it with an external service
8
+ * Running a command locally before deleting a resource, to deregister it with an external service
9
+ * Running a command remotely on a remote host immediately after creating it
10
+ * Copying a file to a remote host after creating it (potentially as a script to be executed afterwards)
11
+ * As a simple alternative to some use cases for Dynamic Providers (especially in languages which do not yet support Dynamic Providers).
14
12
 
15
- # test
13
+ Some users may have experience with Terraform "provisioners", and the Command package offers support for similar scenarios. However, the Command package is provided as independent resources which can be combined with other resources in many interesting ways. This has many strengths, but also some differences, such as the fact that a Command resource failing does not cause a resource it is operating on to fail.
14
+
15
+ You can use the Command package from a Pulumi program written in any Pulumi language: C#, Go, JavaScript/TypeScript, and Python.
16
+ You'll need to [install and configure the Pulumi CLI](https://pulumi.com/docs/get-started/install) if you haven't already.
17
+
18
+
19
+ > **NOTE**: The Command package is in preview. The API design may change ahead of general availability based on [user feedback](https://github.com/pulumi/pulumi-command/issues).
20
+
21
+ ## Examples
22
+
23
+ ### A simple local resource (random)
24
+
25
+ The simplest use case for `local.Command` is to just run a command on `create`, which can return some value which will be stored in the state file, and will be persistent for the life of the stack (or until the resource is destroyed or replaced). The example below uses this as an alternative to the `random` package to create some randomness which is stored in Pulumi state.
26
+
27
+ ```ts
28
+ import { local } from "@pulumi/command";
29
+
30
+ const random = new local.Command("random", {
31
+ create: "openssl rand -hex 16",
32
+ });
33
+
34
+ export const output = random.stdout;
35
+ ```
36
+
37
+ ```go
38
+ package main
39
+
40
+ import (
41
+ "github.com/pulumi/pulumi-command/sdk/v3/go/command/local"
42
+ "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
43
+ )
44
+
45
+ func main() {
46
+ pulumi.Run(func(ctx *pulumi.Context) error {
47
+
48
+ random, err := local.NewCommand(ctx, "my-bucket", &local.CommandArgs{
49
+ Create: pulumi.String("openssl rand -hex 16"),
50
+ })
51
+ if err != nil {
52
+ return err
53
+ }
54
+
55
+ ctx.Export("output", random.Stdout)
56
+ return nil
57
+ })
58
+ }
59
+ ```
60
+
61
+ ### Remote provisioning of an EC2 instance
62
+
63
+ This example creates and EC2 instance, and then uses `remote.Command` and `remote.CopyFile` to run commands and copy files to the remote instance (via SSH). Similar things are possible with Azure, Google Cloud and other cloud provider virtual machines. Support for Windows-based VMs is being tracked [here](https://github.com/pulumi/pulumi-command/issues/15).
64
+
65
+ Note that implicit and explicit (`dependsOn`) dependencies can be used to control the order that these `Command` and `CopyFile` resources are constructed relative to each other and to the cloud resources they depend on. This ensures that the `create` operations run after all dependencies are created, and the `delete` operations run before all dependencies are deleted.
66
+
67
+ Because the `Command` and `CopyFile` resources replace on changes to their connection, if the EC2 instance is replaced, the commands will all re-run on the new instance (and the `delete` operations will run on the old instance).
68
+
69
+ Note also that `deleteBeforeReplace` can be composed with `Command` resources to ensure that the `delete` operation on an "old" instance is run before the `create` operation of the new instance, in case a scarce resource is managed by the command. Similarly, other resource options can naturally be applied to `Command` resources, like `ignoreChanges`.
70
+
71
+ ```ts
72
+ import { interpolate, Config } from "@pulumi/pulumi";
73
+ import { local, remote, types } from "@pulumi/command";
74
+ import * as aws from "@pulumi/aws";
75
+ import * as fs from "fs";
76
+ import * as os from "os";
77
+ import * as path from "path";
78
+ import { size } from "./size";
79
+
80
+ const config = new Config();
81
+ const keyName = config.get("keyName") ?? new aws.ec2.KeyPair("key", { publicKey: config.require("publicKey") }).keyName;
82
+ const privateKeyBase64 = config.get("privateKeyBase64");
83
+ const privateKey = privateKeyBase64 ? Buffer.from(privateKeyBase64, 'base64').toString('ascii') : fs.readFileSync(path.join(os.homedir(), ".ssh", "id_rsa")).toString("utf8");
84
+
85
+ const secgrp = new aws.ec2.SecurityGroup("secgrp", {
86
+ description: "Foo",
87
+ ingress: [
88
+ { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
89
+ { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
90
+ ],
91
+ });
92
+
93
+ const ami = aws.ec2.getAmiOutput({
94
+ owners: ["amazon"],
95
+ mostRecent: true,
96
+ filters: [{
97
+ name: "name",
98
+ values: ["amzn2-ami-hvm-2.0.????????-x86_64-gp2"],
99
+ }],
100
+ });
101
+
102
+ const server = new aws.ec2.Instance("server", {
103
+ instanceType: size,
104
+ ami: ami.id,
105
+ keyName: keyName,
106
+ vpcSecurityGroupIds: [secgrp.id],
107
+ }, { replaceOnChanges: ["instanceType"] });
108
+
109
+ // Now set up a connection to the instance and run some provisioning operations on the instance.
110
+
111
+ const connection: types.input.remote.ConnectionArgs = {
112
+ host: server.publicIp,
113
+ user: "ec2-user",
114
+ privateKey: privateKey,
115
+ };
116
+
117
+ const hostname = new remote.Command("hostname", {
118
+ connection,
119
+ create: "hostname",
120
+ });
121
+
122
+ new remote.Command("remotePrivateIP", {
123
+ connection,
124
+ create: interpolate`echo ${server.privateIp} > private_ip.txt`,
125
+ delete: `rm private_ip.txt`,
126
+ }, { deleteBeforeReplace: true });
127
+
128
+ new local.Command("localPrivateIP", {
129
+ create: interpolate`echo ${server.privateIp} > private_ip.txt`,
130
+ delete: `rm private_ip.txt`,
131
+ }, { deleteBeforeReplace: true });
132
+
133
+ const sizeFile = new remote.CopyFile("size", {
134
+ connection,
135
+ localPath: "./size.ts",
136
+ remotePath: "size.ts",
137
+ })
138
+
139
+ const catSize = new remote.Command("checkSize", {
140
+ connection,
141
+ create: "cat size.ts",
142
+ }, { dependsOn: sizeFile })
143
+
144
+ export const confirmSize = catSize.stdout;
145
+ export const publicIp = server.publicIp;
146
+ export const publicHostName = server.publicDns;
147
+ export const hostnameStdout = hostname.stdout;
148
+ ```
149
+
150
+ ### Invoking a Lambda during Pulumi deployment
151
+
152
+ There may be cases where it is useful to run some code within an AWS Lambda or other serverless function during the deployment. For example, this may allow running some code from within a VPC, or with a specific role, without needing to have persistent compute available (such as the EC2 example above).
153
+
154
+ Note that the Lambda function itself can be created within the same Pulumi program, and then invoked after creation.
155
+
156
+ The example below simply creates some random value within the Lambda, which is a very roundabout way of doing the same thing as the first "random" example above, but this pattern can be used for more complex scenarios where the Lambda does things a local script could not.
157
+
158
+ ```ts
159
+ import { local } from "@pulumi/command";
160
+ import * as aws from "@pulumi/aws";
161
+ import * as crypto from "crypto";
162
+
163
+ const f = new aws.lambda.CallbackFunction("f", {
164
+ publish: true,
165
+ callback: async (ev: any) => {
166
+ return crypto.randomBytes(ev.len/2).toString('hex');
167
+ }
168
+ });
169
+
170
+ const rand = new local.Command("execf", {
171
+ create: `aws lambda invoke --function-name "$FN" --payload '{"len": 10}' --cli-binary-format raw-in-base64-out out.txt >/dev/null && cat out.txt | tr -d '"' && rm out.txt`,
172
+ environment: {
173
+ FN: f.qualifiedArn,
174
+ AWS_REGION: aws.config.region!,
175
+ AWS_PAGER: "",
176
+ },
177
+ })
178
+
179
+ export const output = rand.stdout;
180
+ ```
181
+
182
+ ### Using `local.Command `with CURL to manage external REST API
183
+
184
+ This example uses `local.Command` to create a simple resource provider for managing GitHub labels, by invoking `curl` commands on `create` and `delete` commands against the GitHub REST API. A similar approach could be applied to build other simple providers against any REST API directly from within Pulumi programs in any language. This approach is somewhat limited by the fact that `local.Command` does not yet support `diff`/`update`/`read`. Support for those may be [added in the future](https://github.com/pulumi/pulumi-command/issues/20).
185
+
186
+ This example also shows how `local.Command` can be used as an implementation detail inside a nicer abstraction, like the `GitHubLabel` component defined below.
187
+
188
+ ```ts
189
+ import * as pulumi from "@pulumi/pulumi";
190
+ import * as random from "@pulumi/random";
191
+ import { local } from "@pulumi/command";
192
+
193
+ interface LabelArgs {
194
+ owner: pulumi.Input<string>;
195
+ repo: pulumi.Input<string>;
196
+ name: pulumi.Input<string>;
197
+ githubToken: pulumi.Input<string>;
198
+ }
199
+
200
+ class GitHubLabel extends pulumi.ComponentResource {
201
+ public url: pulumi.Output<string>;
202
+
203
+ constructor(name: string, args: LabelArgs, opts?: pulumi.ComponentResourceOptions) {
204
+ super("example:github:Label", name, args, opts);
205
+
206
+ const label = new local.Command("label", {
207
+ create: "./create_label.sh",
208
+ delete: "./delete_label.sh",
209
+ environment: {
210
+ OWNER: args.owner,
211
+ REPO: args.repo,
212
+ NAME: args.name,
213
+ GITHUB_TOKEN: args.githubToken,
214
+ }
215
+ }, { parent: this });
216
+
217
+ const response = label.stdout.apply(JSON.parse);
218
+ this.url = response.apply((x: any) => x.url as string);
219
+ }
220
+ }
221
+
222
+ const config = new pulumi.Config();
223
+ const rand = new random.RandomString("s", { length: 10, special: false });
224
+
225
+ const label = new GitHubLabel("l", {
226
+ owner: "pulumi",
227
+ repo: "pulumi-command",
228
+ name: rand.result,
229
+ githubToken: config.requireSecret("githubToken"),
230
+ });
231
+
232
+ export const labelUrl = label.url;
233
+ ```
234
+
235
+ ```sh
236
+ # create_label.sh
237
+ curl \
238
+ -s \
239
+ -X POST \
240
+ -H "authorization: Bearer $GITHUB_TOKEN" \
241
+ -H "Accept: application/vnd.github.v3+json" \
242
+ https://api.github.com/repos/$OWNER/$REPO/labels \
243
+ -d "{\"name\":\"$NAME\"}"
244
+ ```
245
+
246
+ ```sh
247
+ # delete_label.sh
248
+ curl \
249
+ -s \
250
+ -X DELETE \
251
+ -H "authorization: Bearer $GITHUB_TOKEN" \
252
+ -H "Accept: application/vnd.github.v3+json" \
253
+ https://api.github.com/repos/$OWNER/$REPO/labels/$NAME
254
+ ```
255
+
256
+ ### Graceful cleanup of workloads in a Kubernetes cluster
257
+
258
+ There are cases where it's important to run some cleanup operation before destroying a resource, in case destroying the resource does not properly handle orderly cleanup. For example, destroying an EKS Cluster will not ensure that all Kubernetes object finalizers are run, which may lead to leaking external resources managed by those Kubernetes resources. This example shows how we can use a `delete`-only `Command` to ensure some cleanup is run within a cluster before destroying it.
259
+
260
+ ```ts
261
+ import { local } from "@pulumi/command";
262
+ import * as eks from "@pulumi/eks";
263
+ import * as random from "@pulumi/random";
264
+ import { interpolate } from "@pulumi/pulumi";
265
+
266
+ const cluster = new eks.Cluster("cluster", {});
267
+
268
+ // We could also use `RemoteCommand` to run this from within a node in the cluster
269
+ const cleanupKubernetesNamespaces = new local.Command("cleanupKubernetesNamespaces", {
270
+ // This will run before the cluster is destroyed. Everything else will need to
271
+ // depend on this resource to ensure this cleanup doesn't happen too early.
272
+ delete: "kubectl delete --all namespaces",
273
+ environment: {
274
+ KUBECONFIG: cluster.kubeconfig,
275
+ },
276
+ });
277
+ ```
278
+
279
+ ## Building
280
+
281
+ ### Dependencies
282
+
283
+ - Go 1.16
284
+ - NodeJS 10.X.X or later
285
+ - Python 3.6 or later
286
+ - .NET Core 3.1
287
+
288
+ Please refer to [Contributing to Pulumi](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) for installation
289
+ guidance.
290
+
291
+ ### Building locally
292
+
293
+ Run the following commands to install Go modules, generate all SDKs, and build the provider:
294
+
295
+ ```
296
+ $ make ensure
297
+ $ make build
298
+ $ make install
299
+ ```
300
+
301
+ Add the `bin` folder to your `$PATH` or copy the `bin/pulumi-resource-aws-native` file to another location in your `$PATH`.
302
+
303
+ ### Running an example
304
+
305
+ Navigate to the simple example and run Pulumi:
306
+
307
+ ```
16
308
  $ cd examples/simple
17
309
  $ yarn link @pulumi/command
18
310
  $ yarn install
19
- $ pulumi stack init test
20
311
  $ pulumi up
21
312
  ```
22
313
 
package/package.json CHANGED
@@ -1,9 +1,18 @@
1
1
  {
2
2
  "name": "@pulumi/command",
3
- "version": "v0.0.1-alpha.1639756016+b25da03b",
3
+ "version": "v0.0.2",
4
+ "keywords": [
5
+ "pulumi",
6
+ "command",
7
+ "category/utility",
8
+ "kind/native"
9
+ ],
10
+ "homepage": "https://pulumi.com",
11
+ "repository": "https://github.com/pulumi/pulumi-command",
12
+ "license": "Apache-2.0",
4
13
  "scripts": {
5
14
  "build": "tsc",
6
- "install": "node scripts/install-pulumi-plugin.js resource command v0.0.1-alpha.1639756016+b25da03b"
15
+ "install": "node scripts/install-pulumi-plugin.js resource command v0.0.2"
7
16
  },
8
17
  "dependencies": {
9
18
  "@pulumi/pulumi": "^3.0.0"
package/package.json.bak CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "@pulumi/command",
3
3
  "version": "${VERSION}",
4
+ "keywords": [
5
+ "pulumi",
6
+ "command",
7
+ "category/utility",
8
+ "kind/native"
9
+ ],
10
+ "homepage": "https://pulumi.com",
11
+ "repository": "https://github.com/pulumi/pulumi-command",
12
+ "license": "Apache-2.0",
4
13
  "scripts": {
5
14
  "build": "tsc"
6
15
  },
package/package.json.dev CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "@pulumi/command",
3
- "version": "v0.0.1-alpha.1639756016+b25da03b",
3
+ "version": "v0.0.2",
4
+ "keywords": [
5
+ "pulumi",
6
+ "command",
7
+ "category/utility",
8
+ "kind/native"
9
+ ],
10
+ "homepage": "https://pulumi.com",
11
+ "repository": "https://github.com/pulumi/pulumi-command",
12
+ "license": "Apache-2.0",
4
13
  "scripts": {
5
14
  "build": "tsc"
6
15
  },
package/types/input.d.ts CHANGED
@@ -20,6 +20,9 @@ export declare namespace remote {
20
20
  * The contents of an SSH key to use for the connection. This takes preference over the password if provided.
21
21
  */
22
22
  privateKey?: pulumi.Input<string>;
23
+ /**
24
+ * The user that we should use for the connection.
25
+ */
23
26
  user?: pulumi.Input<string>;
24
27
  }
25
28
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"input.js","sourceRoot":"","sources":["../../types/input.ts"],"names":[],"mappings":";AAAA,yDAAyD;AACzD,iFAAiF;;;AAOjF,IAAiB,MAAM,CAiCtB;AAjCD,WAAiB,MAAM;IAuBnB;;OAEG;IACH,SAAgB,6BAA6B,CAAC,GAAmB;;QAC7D,uCACO,GAAG,KACN,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,EAAE,EACtB,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,MAAM,IAC5B;IACN,CAAC;IANe,oCAA6B,gCAM5C,CAAA;AACL,CAAC,EAjCgB,MAAM,GAAN,cAAM,KAAN,cAAM,QAiCtB"}
1
+ {"version":3,"file":"input.js","sourceRoot":"","sources":["../../types/input.ts"],"names":[],"mappings":";AAAA,yDAAyD;AACzD,iFAAiF;;;AAOjF,IAAiB,MAAM,CAoCtB;AApCD,WAAiB,MAAM;IA0BnB;;OAEG;IACH,SAAgB,6BAA6B,CAAC,GAAmB;;QAC7D,uCACO,GAAG,KACN,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,EAAE,EACtB,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,MAAM,IAC5B;IACN,CAAC;IANe,oCAA6B,gCAM5C,CAAA;AACL,CAAC,EApCgB,MAAM,GAAN,cAAM,KAAN,cAAM,QAoCtB"}
package/types/output.d.ts CHANGED
@@ -19,6 +19,9 @@ export declare namespace remote {
19
19
  * The contents of an SSH key to use for the connection. This takes preference over the password if provided.
20
20
  */
21
21
  privateKey?: string;
22
+ /**
23
+ * The user that we should use for the connection.
24
+ */
22
25
  user?: string;
23
26
  }
24
27
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"output.js","sourceRoot":"","sources":["../../types/output.ts"],"names":[],"mappings":";AAAA,yDAAyD;AACzD,iFAAiF;;;AAOjF,IAAiB,MAAM,CAkCtB;AAlCD,WAAiB,MAAM;IAuBnB;;OAEG;IACH,SAAgB,yBAAyB,CAAC,GAAe;;QACrD,uCACO,GAAG,KACN,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,EAAE,EACtB,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,MAAM,IAC5B;IACN,CAAC;IANe,gCAAyB,4BAMxC,CAAA;AAEL,CAAC,EAlCgB,MAAM,GAAN,cAAM,KAAN,cAAM,QAkCtB"}
1
+ {"version":3,"file":"output.js","sourceRoot":"","sources":["../../types/output.ts"],"names":[],"mappings":";AAAA,yDAAyD;AACzD,iFAAiF;;;AAOjF,IAAiB,MAAM,CAqCtB;AArCD,WAAiB,MAAM;IA0BnB;;OAEG;IACH,SAAgB,yBAAyB,CAAC,GAAe;;QACrD,uCACO,GAAG,KACN,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,EAAE,EACtB,IAAI,EAAE,MAAA,CAAC,GAAG,CAAC,IAAI,CAAC,mCAAI,MAAM,IAC5B;IACN,CAAC;IANe,gCAAyB,4BAMxC,CAAA;AAEL,CAAC,EArCgB,MAAM,GAAN,cAAM,KAAN,cAAM,QAqCtB"}