From 4aa82c496f719f358a596ca019c2ae651d224597 Mon Sep 17 00:00:00 2001 From: Antoine Date: Sun, 26 Jul 2020 18:10:07 +0200 Subject: [PATCH] create one compute with terraform onto qemu --- .gitignore | 2 + .gitmodules | 3 + cloud_init.cfg | 22 ++++++ docker-kvm-qemu-libvirt | 1 + libvirtd.tf | 55 ++++++++++++++ terraform.tfstate | 164 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 247 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 cloud_init.cfg create mode 160000 docker-kvm-qemu-libvirt create mode 100644 libvirtd.tf create mode 100644 terraform.tfstate diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ebec91b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.terraform +*.tfstate.backup diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5edea30 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "docker-kvm-qemu-libvirt"] + path = docker-kvm-qemu-libvirt + url = gitolite@antoine-roux.ml:container/docker-kvm-qemu-libvirt.git diff --git a/cloud_init.cfg b/cloud_init.cfg new file mode 100644 index 0000000..5a537ce --- /dev/null +++ b/cloud_init.cfg @@ -0,0 +1,22 @@ +#cloud-config +# vim: syntax=yaml + +# *********************** +# ---- documentation : +# ---> https://cloudinit.readthedocs.io/en/latest/topics/examples.html +# ****************************** + +ssh_pwauth: False +chpasswd: + expire: False + +groups: + - whell: [antoine] + +users: + - name: antoine + ssh_authorized_keys: + - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux + sudo: ['ALL=(ALL) NOPASSWD:ALL'] + shell: /bin/bash + groups: wheel diff --git a/docker-kvm-qemu-libvirt b/docker-kvm-qemu-libvirt new file mode 160000 index 0000000..1681c69 --- /dev/null +++ b/docker-kvm-qemu-libvirt @@ -0,0 +1 @@ +Subproject commit 1681c69dbeaf04335db05cf045c3de6f2e662462 diff --git a/libvirtd.tf b/libvirtd.tf new file mode 100644 index 0000000..8b49834 --- /dev/null +++ b/libvirtd.tf @@ -0,0 +1,55 @@ +provider "libvirt" { + uri = "qemu+tcp://127.0.0.1/system" +# uri = "qemu+ssh://root@192.168.100.10/system" +} + +resource "libvirt_volume" "centos7-qcow2" { + name = "centos7.qcow2" + pool = "default" + source = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2" + format = "qcow2" +} + +data "template_file" "user_data" { + template = "${file("${path.module}/cloud_init.cfg")}" +} + +# Use CloudInit to add the instance +resource "libvirt_cloudinit_disk" "commoninit" { + name = "commoninit.iso" + user_data = data.template_file.user_data.rendered +} + +# Define KVM domain to create +resource "libvirt_domain" "db1" { + name = "db1" + memory = "1024" + vcpu = 1 + + network_interface { + network_name = "default" + } + + disk { + volume_id = libvirt_volume.centos7-qcow2.id + } + + cloudinit = libvirt_cloudinit_disk.commoninit.id + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } + + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +# Output Server IP +output "ip" { + value = libvirt_domain.db1.network_interface.0.addresses.0 +} \ No newline at end of file diff --git a/terraform.tfstate b/terraform.tfstate new file mode 100644 index 0000000..2a58124 --- /dev/null +++ b/terraform.tfstate @@ -0,0 +1,164 @@ +{ + "version": 4, + "terraform_version": "0.12.28", + "serial": 40, + "lineage": "e57b8df5-87bd-3954-0ba8-9cbedff9b460", + "outputs": { + "ip": { + "value": "192.168.122.156", + "type": "string" + } + }, + "resources": [ + { + "mode": "data", + "type": "template_file", + "name": "user_data", + "provider": "provider.template", + "instances": [ + { + "schema_version": 0, + "attributes": { + "filename": null, + "id": "924d6b37ed35e7f71aa4793775cf6098a2dd31c5c3cdbfbcbd02cf4893f856cc", + "rendered": "#cloud-config\n# vim: syntax=yaml\n\n# ***********************\n# ---- documentation : \n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n\nssh_pwauth: False\nchpasswd:\n expire: False\n\ngroups:\n - whell: [antoine]\n\nusers:\n - name: antoine\n ssh_authorized_keys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n shell: /bin/bash\n groups: wheel\n", + "template": "#cloud-config\n# vim: syntax=yaml\n\n# ***********************\n# ---- documentation : \n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n\nssh_pwauth: False\nchpasswd:\n expire: False\n\ngroups:\n - whell: [antoine]\n\nusers:\n - name: antoine\n ssh_authorized_keys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n shell: /bin/bash\n groups: wheel\n", + "vars": null + } + } + ] + }, + { + "mode": "managed", + "type": "libvirt_cloudinit_disk", + "name": "commoninit", + "provider": "provider.libvirt", + "instances": [ + { + "schema_version": 0, + "attributes": { + "id": "/var/lib/libvirt/images/commoninit.iso;5f1da8d5-7b33-986f-3888-179eec23b9b3", + "meta_data": "", + "name": "commoninit.iso", + "network_config": "", + "pool": "default", + "user_data": "#cloud-config\n# vim: syntax=yaml\n\n# ***********************\n# ---- documentation : \n# ---\u003e https://cloudinit.readthedocs.io/en/latest/topics/examples.html\n# ******************************\n\nssh_pwauth: False\nchpasswd:\n expire: False\n\ngroups:\n - whell: [antoine]\n\nusers:\n - name: antoine\n ssh_authorized_keys:\n - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCuLVDINxqYow17v9oovyqG3Ok5mOa36U3f+D7CtY8cS7jZavx7A6h2DAtlNzzJl3AxRtk3JY1rmV2fhwB+lVA1S8WT61/qHZH+smjbt6YCp6/ICMKPR8tKtbFUnXn+o9tAhqa7cryWs2LerJasXaSUUguIb6Lr0teSM6enzmtNShjiUta+6HlN+M91n/tp4pjn+tvNUSFETg1JVX1DIG/6U43N8V+fu6xsN5ceoPNGpBpikP0ZYmKYdcPMqqcWJkkaLGwh/u4R98BUVSPor/OFaMw3IZ6Nr7yCdWPCe/8SvEkbd2iMQm5dQjqEFEJUyPwoE18lfM2VnVwlaKsFN9Cmu2J5H4w9qlkZCSGkLa6eckJ3dOHDf6PZO409jbU7sV5rEspuo4gWqEzx5l2YKnASxhy+hylCj+sYrgMcEo9XMVSSYgj5ynykr3LyxyaFklZEuVxXJUYrOVcVIRdkJu9cVnhj6LvaUHY1JqdbhOAjPpSIVqKLPUYJIMU5nVKOh53Dd/rXdOx6BQXiA44DGuvYvHMohK7rXA92Sau0Hbb30DDMkPSPDXknIB+anELGCCx6j8HC38FzJyqtvtrDsG6sc/D3UQGApAS0+O6FXeH+v2OiPhJjdQMwNl5YMrJt6AUBVZZAZtFgUlhhZXRbyhuUuhmWYmzsNbWX7N5pzgYPyQ== antoine@archlinux\n sudo: ['ALL=(ALL) NOPASSWD:ALL']\n shell: /bin/bash\n groups: wheel\n" + }, + "private": "bnVsbA==" + } + ] + }, + { + "mode": "managed", + "type": "libvirt_domain", + "name": "db1", + "provider": "provider.libvirt", + "instances": [ + { + "schema_version": 0, + "attributes": { + "arch": "x86_64", + "autostart": false, + "boot_device": [], + "cloudinit": "/var/lib/libvirt/images/commoninit.iso;5f1da8d5-7b33-986f-3888-179eec23b9b3", + "cmdline": [], + "console": [ + { + "source_host": "127.0.0.1", + "source_path": "", + "source_service": "0", + "target_port": "0", + "target_type": "serial", + "type": "pty" + } + ], + "coreos_ignition": null, + "cpu": null, + "description": "", + "disk": [ + { + "block_device": "", + "file": "", + "scsi": false, + "url": "", + "volume_id": "/var/lib/libvirt/images/centos7.qcow2", + "wwn": "" + } + ], + "emulator": "/usr/bin/qemu-system-x86_64", + "filesystem": [], + "firmware": "", + "fw_cfg_name": "opt/com.coreos/config", + "graphics": [ + { + "autoport": true, + "listen_address": "127.0.0.1", + "listen_type": "address", + "type": "spice" + } + ], + "id": "47a45870-bd85-4705-a835-3985a44e871e", + "initrd": "", + "kernel": "", + "machine": "pc", + "memory": 1024, + "metadata": null, + "name": "db1", + "network_interface": [ + { + "addresses": [ + "192.168.122.156" + ], + "bridge": "", + "hostname": "", + "mac": "52:54:00:4E:19:24", + "macvtap": "", + "network_id": "a27a7ae4-4a14-4ac4-8c52-f7b44bcbb7f3", + "network_name": "default", + "passthrough": "", + "vepa": "", + "wait_for_lease": false + } + ], + "nvram": [], + "qemu_agent": false, + "running": true, + "timeouts": null, + "vcpu": 1, + "video": [], + "xml": [] + }, + "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjozMDAwMDAwMDAwMDB9fQ==", + "dependencies": [ + "libvirt_cloudinit_disk.commoninit", + "libvirt_volume.centos7-qcow2" + ] + } + ] + }, + { + "mode": "managed", + "type": "libvirt_volume", + "name": "centos7-qcow2", + "provider": "provider.libvirt", + "instances": [ + { + "schema_version": 0, + "attributes": { + "base_volume_id": null, + "base_volume_name": null, + "base_volume_pool": null, + "format": "qcow2", + "id": "/var/lib/libvirt/images/centos7.qcow2", + "name": "centos7.qcow2", + "pool": "default", + "size": 8589934592, + "source": "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2", + "xml": [] + }, + "private": "bnVsbA==" + } + ] + } + ] +}