From b113990fda85e8b6a736cd92003db501cd431945 Mon Sep 17 00:00:00 2001 From: Emiliano Vavassori Date: Sat, 21 Sep 2024 19:53:58 +0200 Subject: [PATCH] Aggiunta codice per semplificare. --- linode-ansible/__main__.py | 44 ++++++++++------------- linode-ansible/additional_configs.py | 11 ++++++ linode-ansible/ansible.py | 52 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 26 deletions(-) create mode 100644 linode-ansible/additional_configs.py create mode 100644 linode-ansible/ansible.py diff --git a/linode-ansible/__main__.py b/linode-ansible/__main__.py index a84aea2..b07fc56 100644 --- a/linode-ansible/__main__.py +++ b/linode-ansible/__main__.py @@ -4,21 +4,22 @@ import pulumi import pulumi_linode import pulumi_command as command import pulumi_cloudflare as cloudflare -import yaml -from dotmap import DotMap +import additional_configs +import ansible -config = DotMap(yaml.safe_load(open("input.yaml").read())) -hostname = pulumi.Config().require("hostname") -domainname = pulumi.Config().require("domainname") +config = pulumi.Config() + +hostname = config.require("hostname") +domainname = config.require("domainname") # Create a Linode resource (Linode Instance) instance = pulumi_linode.Instance(hostname, - type=pulumi.Config().require("linodeType"), - region=pulumi.Config().require("linodeRegion"), - image=pulumi.Config().require("linodeImage"), + type=config.require("linodeType"), + region=config.require("linodeRegion"), + image=config.require("linodeImage"), label=hostname, - root_pass=config.rootpassword, - authorized_keys=config.sshkeys, + root_pass=additional_configs.DEFAULT_ROOT_PASSWORD, + authorized_keys=additional_configs.SSH_KEYS, opts=pulumi.ResourceOptions( custom_timeouts=pulumi.CustomTimeouts(create="2m") ) @@ -33,23 +34,14 @@ dnsrecord = cloudflare.Record(hostname, comment=f"Pulumi-generated linode" ) -# Creating the inventory file -inventory = command.local.Command("a-inventory", - create = instance.ip_address.apply( - lambda ipaddr: f"echo '{hostname} ansible_host={ipaddr} ansible_user=root hostname={hostname}.{domainname}' >./inventory" - ), - delete = "rm -f ./inventory", - opts = pulumi.ResourceOptions(depends_on = [ instance ]) -) +environment = { + 'name': hostname, + 'hostname': hostname + '.' + domainname, +} -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 ]) - ) +adeploy = ansible.AnsibleDeployment(instance, environment, config.get('playbooks')) +adeploy.create_inventory() +adeploy.run_playbooks() # Export the Instance label of the instance pulumi.export('ip_address', instance.ip_address) diff --git a/linode-ansible/additional_configs.py b/linode-ansible/additional_configs.py new file mode 100644 index 0000000..f9fe0c7 --- /dev/null +++ b/linode-ansible/additional_configs.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +# encoding: utf-8 + +DEFAULT_ROOT_PASSWORD = 'cicciopasticcio' + +SSH_KEYS = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILH5q/ObtC4VhNT88gebezP/svpvCoQLoZCh4DvUn4xq syntaxerrormmm@taz", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGdTHkPCPUhvrcGgU9M6/BaEeirStM/kBnFxsLyXyelt syntaxerrormmm@kurotsuchi" +] diff --git a/linode-ansible/ansible.py b/linode-ansible/ansible.py new file mode 100644 index 0000000..552c690 --- /dev/null +++ b/linode-ansible/ansible.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# encoding: utf-8 + +import pulumi +from pulumi_command.local import Command + +class AnsibleDeployment(): + + def __init__(self, virtual_machine, environment, additionals = None, deploy = None): + """Creates an ansible deployment and attaches resources.""" + + self.inventory = 'inventory' + self.virtual_machine = virtual_machine + self.environment = environment + self.deploy = str(deploy) or 'deploy.yml' + self.additionals = str(additionals) or '' + + def create_inventory(self, dependencies = []): + """Returns a basic inventory from a virtual_machine object.""" + deps = [] + if len(dependencies) == 0: + deps.append(self.virtual_machine) + else: + deps.extend(dependencies) + + self.inventory = Command("a inventory", + create = self.virtual_machine.ip_address.apply( + lambda ipaddr: f"echo '{self.environment['name']} ansible_host={ipaddr} ansible_user=root hostname={self.environment['hostname']} fqdn={self.environment['hostname']}' >./inventory" + ), + delete = "rm -f ./inventory", + opts = pulumi.ResourceOptions(depends_on = dependencies) + ) + + def run_playbooks(self): + """Execute the rest of the playbooks.""" + + self.deploy_resource = Command("ap deploy", + create = "ansible-playbook deploy.yml", + delete = "rm -f ansible.log", + opts = pulumi.ResourceOptions(depends_on = [ self.inventory ]) + ) + + # Running additionals if present + if len(self.additionals) != 0: + + for playbook in sorted(self.additionals.split(',')): + name = playbook.split('.')[0] + Command(f"ap {name}", + create = f"ansible-playbook {playbook}", + delete = "rm -f ansible.log", + opts = pulumi.ResourceOptions(depends_on = [ self.inventory, self.deploy_resource ]) + )