2021-09-20 14:19:14 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shutil
|
2022-06-27 18:51:03 +00:00
|
|
|
import tempfile
|
2021-09-20 14:19:14 +00:00
|
|
|
import importlib
|
|
|
|
import pulumi
|
2022-06-28 13:30:13 +00:00
|
|
|
import subprocess
|
2023-10-27 10:44:44 +00:00
|
|
|
import semver
|
2022-06-28 13:30:13 +00:00
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
from functools import wraps
|
2021-09-20 14:19:14 +00:00
|
|
|
|
|
|
|
import logging
|
2022-02-22 10:04:29 +00:00
|
|
|
|
2023-10-27 10:44:44 +00:00
|
|
|
from . import __version__
|
|
|
|
|
2021-09-20 14:19:14 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-06-28 13:30:13 +00:00
|
|
|
# Disable Pulumis version check globally
|
|
|
|
os.environ["PULUMI_SKIP_UPDATE_CHECK"] = "true"
|
|
|
|
|
|
|
|
|
|
|
|
def get_pulumi_version():
|
|
|
|
p = shutil.which("pulumi")
|
|
|
|
if not p:
|
|
|
|
return None
|
|
|
|
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
[p, "version"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
|
2022-06-27 18:51:03 +00:00
|
|
|
)
|
2022-06-28 13:30:13 +00:00
|
|
|
if not proc.returncode:
|
|
|
|
return proc.communicate()[0].decode().strip()
|
|
|
|
else:
|
|
|
|
return None
|
2021-09-20 14:19:14 +00:00
|
|
|
|
|
|
|
|
2022-07-01 12:56:53 +00:00
|
|
|
def resolve_outputs(outputs):
|
|
|
|
my_outputs = {}
|
|
|
|
|
2022-07-04 14:15:14 +00:00
|
|
|
for k, v in outputs.items():
|
2023-10-27 12:52:46 +00:00
|
|
|
if isinstance(v, pulumi.automation._output.OutputValue):
|
2022-07-01 12:56:53 +00:00
|
|
|
if v.secret:
|
|
|
|
my_outputs[k] = "***"
|
|
|
|
else:
|
|
|
|
my_outputs[k] = v.value
|
|
|
|
else:
|
|
|
|
my_outputs[k] = v
|
|
|
|
|
|
|
|
return my_outputs
|
|
|
|
|
2022-07-04 14:15:14 +00:00
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
def pulumi_ws(func):
|
|
|
|
@wraps(func)
|
|
|
|
def decorated(self, *args, **kwargs):
|
|
|
|
# setup temp workspace
|
|
|
|
if self.mode == "pulumi":
|
|
|
|
self.work_dir = tempfile.mkdtemp(
|
|
|
|
dir=tempfile.gettempdir(), prefix="cloudbender-"
|
|
|
|
)
|
2021-09-20 14:19:14 +00:00
|
|
|
|
2023-10-27 12:52:46 +00:00
|
|
|
# add all artifact_paths/pulumi to the search path for easier
|
|
|
|
# imports in the pulumi code
|
2022-06-27 18:51:03 +00:00
|
|
|
for artifacts_path in self.ctx["artifact_paths"]:
|
|
|
|
_path = "{}/pulumi".format(artifacts_path.resolve())
|
|
|
|
sys.path.append(_path)
|
2021-09-20 14:19:14 +00:00
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
# Try local implementation first, similar to Jinja2 mode
|
|
|
|
_found = False
|
2021-09-20 14:19:14 +00:00
|
|
|
try:
|
2022-06-27 18:51:03 +00:00
|
|
|
_stack = importlib.import_module(
|
2023-10-27 12:52:46 +00:00
|
|
|
"config.{}.{}".format(
|
|
|
|
self.rel_path, self.template).replace(
|
|
|
|
"/", "."))
|
2021-09-20 14:19:14 +00:00
|
|
|
_found = True
|
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
except ImportError:
|
|
|
|
for artifacts_path in self.ctx["artifact_paths"]:
|
|
|
|
try:
|
|
|
|
spec = importlib.util.spec_from_file_location(
|
|
|
|
"_stack",
|
2022-06-28 13:30:13 +00:00
|
|
|
"{}/pulumi/{}.py".format(
|
|
|
|
artifacts_path.resolve(), self.template
|
|
|
|
),
|
2022-06-27 18:51:03 +00:00
|
|
|
)
|
|
|
|
_stack = importlib.util.module_from_spec(spec)
|
|
|
|
spec.loader.exec_module(_stack)
|
|
|
|
_found = True
|
|
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if not _found:
|
|
|
|
raise FileNotFoundError(
|
2023-10-27 12:52:46 +00:00
|
|
|
"Cannot find Pulumi implementation for {}".format(
|
|
|
|
self.stackname))
|
2021-09-20 14:19:14 +00:00
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
# Store internal pulumi code reference
|
|
|
|
self._pulumi_code = _stack
|
2021-09-20 14:19:14 +00:00
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
# Use legacy Conglomerate as Pulumi project_name
|
|
|
|
project_name = self.parameters["Conglomerate"]
|
|
|
|
|
2023-10-27 12:52:46 +00:00
|
|
|
# Remove stacknameprefix if equals Conglomerate as Pulumi
|
|
|
|
# implicitly prefixes project_name
|
2022-06-28 13:30:13 +00:00
|
|
|
self.pulumi_stackname = re.sub(
|
|
|
|
r"^" + project_name + "-?", "", self.stackname
|
|
|
|
)
|
2022-06-27 18:51:03 +00:00
|
|
|
try:
|
|
|
|
pulumi_backend = "{}/{}/{}".format(
|
|
|
|
self.pulumi["backend"], project_name, self.region
|
|
|
|
)
|
2021-09-20 14:19:14 +00:00
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
except KeyError:
|
|
|
|
raise KeyError("Missing pulumi.backend setting !")
|
|
|
|
|
2022-08-26 09:44:33 +00:00
|
|
|
# Ugly hack as Pulumi currently doesnt support MFA_TOKENs during role assumptions
|
2023-10-27 12:52:46 +00:00
|
|
|
# Do NOT set them via 'aws:secretKey' as they end up in the
|
|
|
|
# self.json in plain text !!!
|
2022-06-27 18:51:03 +00:00
|
|
|
account_id = self.connection_manager.call(
|
2023-10-27 12:52:46 +00:00
|
|
|
"sts",
|
|
|
|
"get_caller_identity",
|
|
|
|
profile=self.profile,
|
|
|
|
region=self.region)["Account"]
|
2022-07-04 14:15:14 +00:00
|
|
|
self.connection_manager.exportProfileEnv()
|
2022-06-27 18:51:03 +00:00
|
|
|
|
|
|
|
# Secrets provider
|
2023-10-02 08:58:32 +00:00
|
|
|
secrets_provider = None
|
2022-06-27 18:51:03 +00:00
|
|
|
if "secretsProvider" in self.pulumi:
|
|
|
|
secrets_provider = self.pulumi["secretsProvider"]
|
|
|
|
if (
|
|
|
|
secrets_provider == "passphrase"
|
|
|
|
and "PULUMI_CONFIG_PASSPHRASE" not in os.environ
|
|
|
|
):
|
2022-06-28 13:30:13 +00:00
|
|
|
raise ValueError(
|
|
|
|
"Missing PULUMI_CONFIG_PASSPHRASE environment variable!"
|
|
|
|
)
|
2022-06-27 18:51:03 +00:00
|
|
|
|
|
|
|
# Set tag for stack file name and version
|
|
|
|
_tags = {}
|
|
|
|
try:
|
|
|
|
_version = self._pulumi_code.VERSION
|
|
|
|
except AttributeError:
|
|
|
|
_version = "undefined"
|
|
|
|
|
2023-10-27 10:44:44 +00:00
|
|
|
# bail out if we need a minimal cloudbender version for a template
|
|
|
|
try:
|
|
|
|
_min_version = self._pulumi_code.MIN_CLOUDBENDER_VERSION
|
2023-10-27 12:52:46 +00:00
|
|
|
if semver.compare(
|
2023-12-06 18:05:32 +00:00
|
|
|
semver.Version.parse(__version__.strip("v")).finalize_version(),
|
2023-10-27 12:52:46 +00:00
|
|
|
_min_version.strip("v")) < 0:
|
2023-10-27 10:44:44 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"Minimal required CloudBender version is {_min_version}, but we are {__version__}!"
|
|
|
|
)
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
# Tag all resources with our metadata, allowing "prune" eventually
|
|
|
|
_tags["zdt:cloudbender.source"] = "{}:{}".format(
|
|
|
|
os.path.basename(self._pulumi_code.__file__), _version
|
|
|
|
)
|
|
|
|
_tags["zdt:cloudbender.owner"] = f"{project_name}.{self.pulumi_stackname}"
|
|
|
|
|
2023-05-23 19:29:15 +00:00
|
|
|
# Inject all stack tags
|
|
|
|
_tags.update(self.tags)
|
|
|
|
|
2022-06-28 13:30:13 +00:00
|
|
|
self.pulumi_config.update(
|
|
|
|
{
|
|
|
|
"aws:region": self.region,
|
|
|
|
"aws:defaultTags": {"tags": _tags},
|
|
|
|
"zdt:region": self.region,
|
|
|
|
"zdt:awsAccountId": account_id,
|
|
|
|
"zdt:projectName": project_name,
|
|
|
|
"zdt:stackName": self.pulumi_stackname,
|
|
|
|
}
|
|
|
|
)
|
2022-06-27 18:51:03 +00:00
|
|
|
|
|
|
|
# inject all parameters as config in the <Conglomerate> namespace
|
|
|
|
for p in self.parameters:
|
2022-06-28 13:30:13 +00:00
|
|
|
self.pulumi_config[
|
|
|
|
"{}:{}".format(self.parameters["Conglomerate"], p)
|
|
|
|
] = self.parameters[p]
|
2022-06-27 18:51:03 +00:00
|
|
|
|
|
|
|
stack_settings = pulumi.automation.StackSettings(
|
|
|
|
config=self.pulumi_config,
|
|
|
|
secrets_provider=secrets_provider,
|
|
|
|
encryption_salt=self.pulumi.get("encryptionsalt", None),
|
|
|
|
encrypted_key=self.pulumi.get("encryptedkey", None),
|
|
|
|
)
|
|
|
|
|
|
|
|
project_settings = pulumi.automation.ProjectSettings(
|
2024-11-27 17:50:09 +00:00
|
|
|
name=project_name, runtime="python", backend=pulumi.automation.ProjectBackend(url=pulumi_backend)
|
2022-06-27 18:51:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.pulumi_ws_opts = pulumi.automation.LocalWorkspaceOptions(
|
|
|
|
work_dir=self.work_dir,
|
|
|
|
project_settings=project_settings,
|
|
|
|
stack_settings={self.pulumi_stackname: stack_settings},
|
|
|
|
secrets_provider=secrets_provider,
|
|
|
|
)
|
|
|
|
|
2024-12-03 12:49:16 +00:00
|
|
|
# self.pulumi_workspace = pulumi.automation.LocalWorkspace(self.pulumi_ws_opts)
|
|
|
|
|
2022-06-27 18:51:03 +00:00
|
|
|
response = func(self, *args, **kwargs)
|
|
|
|
|
|
|
|
# Cleanup temp workspace
|
|
|
|
if self.work_dir and os.path.exists(self.work_dir):
|
|
|
|
shutil.rmtree(self.work_dir)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
return decorated
|