From a27aa404e1e22767df8b840a7a178c3f6ac70889 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Thu, 17 Oct 2024 21:40:05 +0400 Subject: [PATCH 1/6] add lxd virtualization --- molecule/shared/substrate/lxd/create.yml | 84 +++++++++++++++++++++++ molecule/shared/substrate/lxd/destroy.yml | 39 +++++++++++ 2 files changed, 123 insertions(+) create mode 100644 molecule/shared/substrate/lxd/create.yml create mode 100644 molecule/shared/substrate/lxd/destroy.yml diff --git a/molecule/shared/substrate/lxd/create.yml b/molecule/shared/substrate/lxd/create.yml new file mode 100644 index 00000000..b65eed8a --- /dev/null +++ b/molecule/shared/substrate/lxd/create.yml @@ -0,0 +1,84 @@ +--- +- name: Manage LXD containers + hosts: localhost + gather_facts: false + connection: local + vars: + molecule_inventory: + molecule: + hosts: {} + molecule_yml: + platforms: + - name: instance + image: ubuntu:20.04 + user: ansible + + tasks: + - name: Ensure LXD is installed + ansible.builtin.command: + cmd: snap install lxd + become: true + + - name: Initialize LXD (if not already initialized) + command: lxd init --auto + args: + creates: /var/lib/lxd/lxd.db + become: true + + - name: Check if LXD container exists + command: lxc list "{{ platform['name'] }}" --format=json + register: lxd_container_info + ignore_errors: true + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Debug container existence + ansible.builtin.debug: + var: lxd_container_info + + - name: Create LXD containers if not exists + community.general.lxd_container: + name: "{{ platform['name'] }}" + state: started + source: + type: image + mode: pull + server: https://cloud-images.ubuntu.com/releases + protocol: simplestreams + alias: "f" + when: lxd_container_info.results[0].stdout == '[]' + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Add LXD container to molecule_inventory + vars: + inventory_partial_yaml: | + molecule: + hosts: + {{ platform['name'] }}: + ansible_user: {{ platform['user'] }} + ansible.builtin.set_fact: + molecule_inventory: > + {{ molecule_inventory | combine(inventory_partial_yaml | from_yaml, recursive=true) }} + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Dump molecule_inventory + ansible.builtin.copy: + content: | + {{ molecule_inventory | to_nice_yaml }} + dest: ../../inventory/molecule_inventory.yml + mode: '0600' + + - name: Force inventory refresh + ansible.builtin.meta: refresh_inventory + + - name: Fail if molecule group is missing + ansible.builtin.assert: + that: "'molecule' in groups" + fail_msg: | + molecule group was not found inside inventory groups: {{ groups }} + run_once: true \ No newline at end of file diff --git a/molecule/shared/substrate/lxd/destroy.yml b/molecule/shared/substrate/lxd/destroy.yml new file mode 100644 index 00000000..62d1786a --- /dev/null +++ b/molecule/shared/substrate/lxd/destroy.yml @@ -0,0 +1,39 @@ +--- +- name: Destroy LXD containers + hosts: localhost + gather_facts: false + connection: local + vars: + molecule_yml: + platforms: + - name: instance + image: ubuntu:20.04 + user: ansible + + tasks: + - name: Check if LXD container exists + command: lxc list "{{ platform['name'] }}" --format=json + register: lxd_container_info + ignore_errors: true + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Debug container existence + ansible.builtin.debug: + var: lxd_container_info + + - name: Destroy LXD containers if exists + community.general.lxd_container: + name: "{{ platform['name'] }}" + state: absent + when: lxd_container_info.results[0].stdout != '[]' + loop: "{{ molecule_yml['platforms'] }}" + loop_control: + loop_var: platform + + - name: Remove molecule_inventory file + ansible.builtin.file: + path: ../../inventory/molecule_inventory.yml + state: absent + From fa657d04e4b018887ca064ddac6b8569dfa4c3a0 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Fri, 15 Nov 2024 18:48:09 +0400 Subject: [PATCH 2/6] draft fix lxd --- molecule/shared/substrate/lxd/create.yml | 38 ++++++++++++++++-------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/molecule/shared/substrate/lxd/create.yml b/molecule/shared/substrate/lxd/create.yml index b65eed8a..2489c217 100644 --- a/molecule/shared/substrate/lxd/create.yml +++ b/molecule/shared/substrate/lxd/create.yml @@ -10,20 +10,32 @@ molecule_yml: platforms: - name: instance - image: ubuntu:20.04 + image: ubuntu:noble user: ansible tasks: - - name: Ensure LXD is installed - ansible.builtin.command: - cmd: snap install lxd - become: true + - name: Check if LXD is installed + ansible.builtin.command: lxd --version + register: lxd_check + ignore_errors: true + + - name: Fail if LXD is not installed + ansible.builtin.fail: + msg: | + LXD is not installed on this system. + Please install LXD before running this playbook. + For installation instructions, visit: https://linuxcontainers.org/lxd/getting-started-cli/ + when: lxd_check.rc != 0 + + - name: Check if LXD is initialized + ansible.builtin.command: lxd waitready --timeout=5 + register: lxd_initialized + ignore_errors: true - - name: Initialize LXD (if not already initialized) - command: lxd init --auto - args: - creates: /var/lib/lxd/lxd.db - become: true + - name: Fail if LXD is not initialized + ansible.builtin.fail: + msg: "LXD is not initialized or not ready. Please run 'sudo lxd init' manually." + when: lxd_initialized.rc != 0 - name: Check if LXD container exists command: lxc list "{{ platform['name'] }}" --format=json @@ -46,7 +58,7 @@ mode: pull server: https://cloud-images.ubuntu.com/releases protocol: simplestreams - alias: "f" + alias: "{{ platform['image'] }}" when: lxd_container_info.results[0].stdout == '[]' loop: "{{ molecule_yml['platforms'] }}" loop_control: @@ -80,5 +92,5 @@ ansible.builtin.assert: that: "'molecule' in groups" fail_msg: | - molecule group was not found inside inventory groups: {{ groups }} - run_once: true \ No newline at end of file + Molecule group was not found inside inventory groups: {{ groups }} + run_once: true From e36294c9c4effb1528165ac74843db80815c0287 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Sun, 17 Nov 2024 22:33:00 +0400 Subject: [PATCH 3/6] fix lxd --- molecule/shared/substrate/lxd/create.yml | 54 ++++-------------------- 1 file changed, 9 insertions(+), 45 deletions(-) diff --git a/molecule/shared/substrate/lxd/create.yml b/molecule/shared/substrate/lxd/create.yml index 2489c217..6f71ca63 100644 --- a/molecule/shared/substrate/lxd/create.yml +++ b/molecule/shared/substrate/lxd/create.yml @@ -1,8 +1,9 @@ --- -- name: Manage LXD containers +- name: Manage LXD containers using command module hosts: localhost gather_facts: false connection: local + become: true vars: molecule_inventory: molecule: @@ -10,7 +11,8 @@ molecule_yml: platforms: - name: instance - image: ubuntu:noble + image_fingerprint: "602f1cb373c046923c69c17136eee708c6ea5e8b1d6b7618275ee0ec246b4fe5" + architecture: x86_64 user: ansible tasks: @@ -38,7 +40,7 @@ when: lxd_initialized.rc != 0 - name: Check if LXD container exists - command: lxc list "{{ platform['name'] }}" --format=json + ansible.builtin.command: lxc list "{{ platform['name'] }}" --format=json register: lxd_container_info ignore_errors: true loop: "{{ molecule_yml['platforms'] }}" @@ -49,48 +51,10 @@ ansible.builtin.debug: var: lxd_container_info - - name: Create LXD containers if not exists - community.general.lxd_container: - name: "{{ platform['name'] }}" - state: started - source: - type: image - mode: pull - server: https://cloud-images.ubuntu.com/releases - protocol: simplestreams - alias: "{{ platform['image'] }}" + - name: Create LXD containers if not exists using lxc launch + ansible.builtin.command: > + lxc launch ubuntu-cloud:{{ platform['image_fingerprint'] }} {{ platform['name'] }} when: lxd_container_info.results[0].stdout == '[]' loop: "{{ molecule_yml['platforms'] }}" loop_control: - loop_var: platform - - - name: Add LXD container to molecule_inventory - vars: - inventory_partial_yaml: | - molecule: - hosts: - {{ platform['name'] }}: - ansible_user: {{ platform['user'] }} - ansible.builtin.set_fact: - molecule_inventory: > - {{ molecule_inventory | combine(inventory_partial_yaml | from_yaml, recursive=true) }} - loop: "{{ molecule_yml['platforms'] }}" - loop_control: - loop_var: platform - - - name: Dump molecule_inventory - ansible.builtin.copy: - content: | - {{ molecule_inventory | to_nice_yaml }} - dest: ../../inventory/molecule_inventory.yml - mode: '0600' - - - name: Force inventory refresh - ansible.builtin.meta: refresh_inventory - - - name: Fail if molecule group is missing - ansible.builtin.assert: - that: "'molecule' in groups" - fail_msg: | - Molecule group was not found inside inventory groups: {{ groups }} - run_once: true + loop_var: platform \ No newline at end of file From 8a4cf4c2b1c98eafb01e1d0528659e6bd9ec7608 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Sun, 17 Nov 2024 22:47:53 +0400 Subject: [PATCH 4/6] update lxd destroy --- molecule/shared/substrate/lxd/destroy.yml | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/molecule/shared/substrate/lxd/destroy.yml b/molecule/shared/substrate/lxd/destroy.yml index 62d1786a..16069602 100644 --- a/molecule/shared/substrate/lxd/destroy.yml +++ b/molecule/shared/substrate/lxd/destroy.yml @@ -1,18 +1,17 @@ --- -- name: Destroy LXD containers - hosts: localhost +- name: Destroy LXD containers using command module + hosts: all gather_facts: false - connection: local + become: true vars: molecule_yml: platforms: - name: instance - image: ubuntu:20.04 user: ansible tasks: - name: Check if LXD container exists - command: lxc list "{{ platform['name'] }}" --format=json + ansible.builtin.command: lxc list "{{ platform['name'] }}" --format=json register: lxd_container_info ignore_errors: true loop: "{{ molecule_yml['platforms'] }}" @@ -23,17 +22,10 @@ ansible.builtin.debug: var: lxd_container_info - - name: Destroy LXD containers if exists - community.general.lxd_container: - name: "{{ platform['name'] }}" - state: absent + - name: Destroy LXD containers if exists using lxc delete + ansible.builtin.command: > + lxc delete {{ platform['name'] }} --force when: lxd_container_info.results[0].stdout != '[]' loop: "{{ molecule_yml['platforms'] }}" loop_control: loop_var: platform - - - name: Remove molecule_inventory file - ansible.builtin.file: - path: ../../inventory/molecule_inventory.yml - state: absent - From e00c567b429054b2de0d3bbd5ba3c5b5345a2f7c Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Mon, 18 Nov 2024 14:41:12 +0400 Subject: [PATCH 5/6] change fingerprint to noble --- molecule/shared/substrate/lxd/create.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/molecule/shared/substrate/lxd/create.yml b/molecule/shared/substrate/lxd/create.yml index 6f71ca63..e8564cc4 100644 --- a/molecule/shared/substrate/lxd/create.yml +++ b/molecule/shared/substrate/lxd/create.yml @@ -11,7 +11,7 @@ molecule_yml: platforms: - name: instance - image_fingerprint: "602f1cb373c046923c69c17136eee708c6ea5e8b1d6b7618275ee0ec246b4fe5" + image_fingerprint: "noble" architecture: x86_64 user: ansible @@ -53,8 +53,8 @@ - name: Create LXD containers if not exists using lxc launch ansible.builtin.command: > - lxc launch ubuntu-cloud:{{ platform['image_fingerprint'] }} {{ platform['name'] }} + lxc launch ubuntu:{{ platform['image_fingerprint'] }} {{ platform['name'] }} when: lxd_container_info.results[0].stdout == '[]' loop: "{{ molecule_yml['platforms'] }}" loop_control: - loop_var: platform \ No newline at end of file + loop_var: platform From 9209aa328845eeb0cfcd8369c7a2ac6e1624a448 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Thu, 28 Nov 2024 19:43:21 +0400 Subject: [PATCH 6/6] add pulumi --- molecule/shared/substrate/lxd/create.yml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/molecule/shared/substrate/lxd/create.yml b/molecule/shared/substrate/lxd/create.yml index e8564cc4..3a1f1d1c 100644 --- a/molecule/shared/substrate/lxd/create.yml +++ b/molecule/shared/substrate/lxd/create.yml @@ -1,5 +1,5 @@ --- -- name: Manage LXD containers using command module +- name: Manage LXD containers and set Pulumi ESC hosts: localhost gather_facts: false connection: local @@ -39,6 +39,23 @@ msg: "LXD is not initialized or not ready. Please run 'sudo lxd init' manually." when: lxd_initialized.rc != 0 + - name: Get ESC from Pulumi + ansible.builtin.command: esc open deeep-network/dev/services --format dotenv + changed_when: false + register: pulumi_esc + + - name: Ensure cloud-init directory exists + ansible.builtin.file: + path: /etc/cloud + state: directory + + - name: Write Pulumi ESC to cloud-init file + ansible.builtin.blockinfile: + path: /etc/cloud/cloud.cfg.d/01-pulumi-esc.cfg + block: | + # Pulumi ESC Configuration + {{ pulumi_esc.stdout }} + - name: Check if LXD container exists ansible.builtin.command: lxc list "{{ platform['name'] }}" --format=json register: lxd_container_info @@ -54,6 +71,8 @@ - name: Create LXD containers if not exists using lxc launch ansible.builtin.command: > lxc launch ubuntu:{{ platform['image_fingerprint'] }} {{ platform['name'] }} + args: + creates: /var/lib/lxd/lxd.db when: lxd_container_info.results[0].stdout == '[]' loop: "{{ molecule_yml['platforms'] }}" loop_control: