Skip to content

Commit

Permalink
Merged
Browse files Browse the repository at this point in the history
  • Loading branch information
t0yv0 committed Aug 16, 2021
2 parents e259c9a + 3bc7190 commit a3cc62c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
11 changes: 11 additions & 0 deletions aws-py-ec2-provisioners/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pulumi
import pulumi_aws as aws
import provisioners
import base64

# Get the config ready to go.
config = pulumi.Config()
Expand All @@ -12,12 +13,22 @@
key_name = config.get('keyName')
public_key = config.get('publicKey')


# The privateKey associated with the selected key must be provided (either directly or base64 encoded),
# along with an optional passphrase if needed.
def decode_key(key):

try:
key = base64.b64decode(key.encode('ascii')).decode('ascii')
except:
pass

if key.startswith('-----BEGIN RSA PRIVATE KEY-----'):
return key

return key.encode('ascii')


private_key = config.require_secret('privateKey').apply(decode_key)
private_key_passphrase = config.get_secret('privateKeyPassphrase')

Expand Down
19 changes: 14 additions & 5 deletions aws-py-ec2-provisioners/provisioners.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pulumi import dynamic
import sys
import time
from typing import Any, Optional
from typing import Any, Optional, List
from typing_extensions import TypedDict
from uuid import uuid4
import hashlib
Expand Down Expand Up @@ -65,6 +65,9 @@ def connect(conn: ConnectionArgs) -> paramiko.SSHClient:
class ProvisionerProvider(dynamic.ResourceProvider):
__metaclass__ = abc.ABCMeta

def ignore_properties(self) -> List[str]:
return []

@abc.abstractmethod
def on_create(self, inputs: Any) -> Any:
return
Expand All @@ -78,15 +81,18 @@ def diff(self, _id, olds, news):
diffs = []
for key in olds:
if key not in news:
diffs.append(key)
if key not in self.ignore_properties():
diffs.append(key)
else:
olds_value = json.dumps(olds[key], sort_keys=True, indent=2)
news_value = json.dumps(news[key], sort_keys=True, indent=2)
if olds_value != news_value:
diffs.append(key)
if key not in self.ignore_properties():
diffs.append(key)
for key in news:
if key not in olds:
diffs.append(key)
if key not in self.ignore_properties():
diffs.append(key)

return dynamic.DiffResult(changes=len(diffs) > 0, replaces=diffs, delete_before_replace=True)

Expand Down Expand Up @@ -138,6 +144,9 @@ class RunCommandResult(TypedDict):

# RemoteExecProvider implements the resource lifecycle for the RemoteExec resource type below.
class RemoteExecProvider(ProvisionerProvider):
def ignore_properties(self) -> List[str]:
return ['results']

def on_create(self, inputs: Any) -> Any:
ssh = connect(inputs['conn'])
try:
Expand All @@ -157,7 +166,7 @@ def on_create(self, inputs: Any) -> Any:
# RemoteExec runs remote one or more commands over an SSH connection. It returns the resulting
# stdout and stderr from the commands in the results property.
class RemoteExec(dynamic.Resource):
results: pulumi.Output[list]
results: pulumi.Output[List[Any]]

def __init__(self, name: str, conn: ConnectionArgs, commands: list, opts: Optional[pulumi.ResourceOptions] = None):
self.conn = conn
Expand Down
14 changes: 12 additions & 2 deletions misc/test/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@ func TestAccAwsTsContainers(t *testing.T) {
}

func TestAccAwsTsEc2Provisioners(t *testing.T) {
checkAccAwsEc2Provisioners(t, "aws-ts-ec2-provisioners")
}

func TestAccAwsPyEc2Provisioners(t *testing.T) {
checkAccAwsEc2Provisioners(t, "aws-py-ec2-provisioners")
}

func checkAccAwsEc2Provisioners(t *testing.T, dir string) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(getAwsRegion())},
)
Expand All @@ -346,6 +354,9 @@ func TestAccAwsTsEc2Provisioners(t *testing.T) {
KeyName: aws.String(keyName),
})
assert.NoError(t, err)
if err != nil {
return
}
defer func() {
t.Logf("Deleting keypair %s.\n", keyName)
_, err := svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
Expand All @@ -355,7 +366,7 @@ func TestAccAwsTsEc2Provisioners(t *testing.T) {
}()
test := getAWSBase(t).
With(integration.ProgramTestOptions{
Dir: path.Join(getCwd(t), "..", "..", "aws-ts-ec2-provisioners"),
Dir: path.Join(getCwd(t), "..", "..", dir),
Config: map[string]string{
"keyName": aws.StringValue(key.KeyName),
},
Expand All @@ -367,7 +378,6 @@ func TestAccAwsTsEc2Provisioners(t *testing.T) {
assert.Equal(t, "[test]\nx = 42\n", catConfigStdout)
},
})

integration.ProgramTest(t, &test)
}

Expand Down

0 comments on commit a3cc62c

Please sign in to comment.