2024-04-19 01:27:05 +02:00
|
|
|
"""A Linode Python Pulumi program"""
|
|
|
|
|
|
|
|
import pulumi
|
|
|
|
import pulumi_linode
|
|
|
|
import pulumi_command as command
|
2024-09-07 23:45:15 +02:00
|
|
|
import pulumi_cloudflare as cloudflare
|
2024-04-19 01:27:05 +02:00
|
|
|
import yaml
|
|
|
|
from dotmap import DotMap
|
|
|
|
|
|
|
|
config = DotMap(yaml.safe_load(open("input.yaml").read()))
|
2024-09-08 02:39:46 +02:00
|
|
|
hostname = pulumi.Config().require("hostname")
|
|
|
|
domainname = pulumi.Config().require("domainname")
|
2024-09-07 23:45:15 +02:00
|
|
|
|
2024-04-19 01:27:05 +02:00
|
|
|
# Create a Linode resource (Linode Instance)
|
2024-09-08 02:39:46 +02:00
|
|
|
instance = pulumi_linode.Instance(hostname,
|
|
|
|
type=pulumi.Config().require("linodeType"),
|
|
|
|
region=pulumi.Config().require("linodeRegion"),
|
|
|
|
image=pulumi.Config().require("linodeImage"),
|
|
|
|
label=hostname,
|
|
|
|
root_pass=config.rootpassword,
|
|
|
|
authorized_keys=config.sshkeys,
|
2024-04-19 01:27:05 +02:00
|
|
|
opts=pulumi.ResourceOptions(
|
|
|
|
custom_timeouts=pulumi.CustomTimeouts(create="2m")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-09-08 02:39:46 +02:00
|
|
|
zone = cloudflare.get_zone(name=domainname)
|
|
|
|
dnsrecord = cloudflare.Record(hostname,
|
|
|
|
name=hostname,
|
2024-09-07 23:45:15 +02:00
|
|
|
zone_id=zone.id,
|
|
|
|
type="A",
|
|
|
|
content=instance.ip_address,
|
|
|
|
comment=f"Pulumi-generated linode"
|
|
|
|
)
|
|
|
|
|
2024-04-19 01:27:05 +02:00
|
|
|
# Creating the inventory file
|
|
|
|
inventory = command.local.Command("a-inventory",
|
|
|
|
create = instance.ip_address.apply(
|
2024-09-08 02:39:46 +02:00
|
|
|
lambda ipaddr: f"echo '{hostname} ansible_host={ipaddr} ansible_user=root hostname={hostname}.{domainname}' >./inventory"
|
2024-04-19 01:27:05 +02:00
|
|
|
),
|
|
|
|
delete = "rm -f ./inventory",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ instance ])
|
|
|
|
)
|
|
|
|
|
2024-09-08 02:39:46 +02:00
|
|
|
for playbook in config.playbooks:
|
|
|
|
shortname = playbook.split('.')[0]
|
|
|
|
# Applying the command
|
|
|
|
command.local.Command(f"a-deploy-{shortname}",
|
|
|
|
create = f"ansible-playbook {playbook}",
|
|
|
|
delete = "rm -f ./ansible.log",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
|
|
)
|
2024-04-19 01:27:05 +02:00
|
|
|
|
|
|
|
# Export the Instance label of the instance
|
|
|
|
pulumi.export('ip_address', instance.ip_address)
|