From ade519a9df654f8b351e0179405ad696700d444f Mon Sep 17 00:00:00 2001
From: Bence Orosz <oroszbence0929@gmail.com>
Date: Sun, 24 Oct 2021 00:44:15 +0200
Subject: [PATCH] cloud init not working

---
 .gitignore           | 44 +++++++++++++++++++++++++++++++++
 destroy.sh           |  4 +++
 hosts.tf             |  8 ++++++
 how-to-import        |  1 +
 initState.sh         | 17 +++++++++++++
 login.sh.example     |  4 +++
 login.tfvars.example |  2 ++
 networks.tf          | 58 ++++++++++++++++++++++++++++++++++++++++++++
 run.sh               |  4 +++
 vms.tf               | 48 ++++++++++++++++++++++++++++++++++++
 vmware.tf            | 55 +++++++++++++++++++++++++++++++++++++++++
 11 files changed, 245 insertions(+)
 create mode 100644 .gitignore
 create mode 100755 destroy.sh
 create mode 100644 hosts.tf
 create mode 100644 how-to-import
 create mode 100755 initState.sh
 create mode 100755 login.sh.example
 create mode 100644 login.tfvars.example
 create mode 100644 networks.tf
 create mode 100755 run.sh
 create mode 100644 vms.tf
 create mode 100644 vmware.tf

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..72a935d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,44 @@
+### Terraform template
+# Local .terraform directories
+**/.terraform/*
+
+# .tfstate files
+*.tfstate
+*.tfstate.*
+
+# Crash log files
+crash.log
+
+# Exclude all .tfvars files, which are likely to contain sentitive data, such as
+# password, private keys, and other secrets. These should not be part of version
+# control as they are data points which are potentially sensitive and subject
+# to change depending on the environment.
+#
+*.tfvars
+
+# Ignore override files as they are usually used to override resources locally and so
+# are not checked in
+override.tf
+override.tf.json
+*_override.tf
+*_override.tf.json
+
+# Include override files you do wish to add to version control using negated pattern
+#
+# !example_override.tf
+
+# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
+# example: *tfplan*
+
+# Ignore CLI configuration files
+.terraformrc
+terraform.rc
+
+# IDEs
+.idea
+.vscode
+.code
+.vs
+
+login.tfvars
+login.sh
diff --git a/destroy.sh b/destroy.sh
new file mode 100755
index 0000000..7e5e394
--- /dev/null
+++ b/destroy.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+source login.sh
+terraform destroy --var-file login.tfvars
diff --git a/hosts.tf b/hosts.tf
new file mode 100644
index 0000000..87f5337
--- /dev/null
+++ b/hosts.tf
@@ -0,0 +1,8 @@
+data "vsphere_host" "NOC-A" {
+  name = "noc-a-test.internal"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+data "vsphere_host" "NOC-B" {
+  name = "noc-b-test.internal"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
\ No newline at end of file
diff --git a/how-to-import b/how-to-import
new file mode 100644
index 0000000..e331508
--- /dev/null
+++ b/how-to-import
@@ -0,0 +1 @@
+terraform import --var-file login.tfvars 'vsphere_distributed_port_group.dvpg["DnPG-Internal-152"]' /NOC/network/DnPG-Internal-152
\ No newline at end of file
diff --git a/initState.sh b/initState.sh
new file mode 100755
index 0000000..dbbfe53
--- /dev/null
+++ b/initState.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+source login.sh
+
+PROJ_ID="3585"
+STATE_NAME="petalinux"
+
+terraform init \
+    -backend-config="address=https://git.sch.bme.hu/api/v4/projects/${PROJ_ID}/terraform/state/${STATE_NAME}" \
+    -backend-config="lock_address=https://git.sch.bme.hu/api/v4/projects/${PROJ_ID}/terraform/state/${STATE_NAME}/lock" \
+    -backend-config="unlock_address=https://git.sch.bme.hu/api/v4/projects/${PROJ_ID}/terraform/state/${STATE_NAME}/lock" \
+    -backend-config="username=${GITLAB_USER}" \
+    -backend-config="password=${GITLAB_REPO_PAT}" \
+    -backend-config="lock_method=POST" \
+    -backend-config="unlock_method=DELETE" \
+    -backend-config="retry_wait_min=5" \
+    -reconfigure
diff --git a/login.sh.example b/login.sh.example
new file mode 100755
index 0000000..f416953
--- /dev/null
+++ b/login.sh.example
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+export GITLAB_USER=kovacs_bela
+export GITLAB_REPO_PAT=sokbetu_sokszam
\ No newline at end of file
diff --git a/login.tfvars.example b/login.tfvars.example
new file mode 100644
index 0000000..3681aa5
--- /dev/null
+++ b/login.tfvars.example
@@ -0,0 +1,2 @@
+vsphere_user="bela_kisnyul"
+vsphere_password="almafa123"
diff --git a/networks.tf b/networks.tf
new file mode 100644
index 0000000..3f14658
--- /dev/null
+++ b/networks.tf
@@ -0,0 +1,58 @@
+data "vsphere_network" "DnPG-Internal-151" {
+  name = "DnPG-Internal-151"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-NAT-51" {
+  name = "DnPG-NAT-51"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-Internal-152" {
+  name = "DnPG-Internal-152"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-WiFi-69" {
+  name = "DnPG-WiFi-69"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-mgmt-lan" {
+  name = "DnPG-mgmt-lan"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-Vlan-172" {
+  name = "DnPG-Vlan-172"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-Internal-29" {
+  name = "DnPG-Internal-29"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-Internal-209" {
+  name = "DnPG-Internal-209"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_network" "DnPG-NATGWTEST-64" {
+  name = "DnPG-NATGWTEST-64"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+data "vsphere_network" "DnPG-Internal-211" {
+  name = "DnPG-Internal-211"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_distributed_virtual_switch" "switch" {
+  name          = "DNocSwitch"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_distributed_virtual_switch" "Switch-mgmt" {
+  name          = "DNocSwitch-mgmt"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
diff --git a/run.sh b/run.sh
new file mode 100755
index 0000000..ae11db2
--- /dev/null
+++ b/run.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+source login.sh
+terraform apply --var-file login.tfvars
\ No newline at end of file
diff --git a/vms.tf b/vms.tf
new file mode 100644
index 0000000..ecbf061
--- /dev/null
+++ b/vms.tf
@@ -0,0 +1,48 @@
+#https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.ova
+data "vsphere_virtual_machine" "ubuntu_template" {
+  name          = "ubuntu-20.04-server-cloudimg-amd64v2"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_virtual_machine" "ubuntu_template-richard" {
+  name          = "ubu1804-template"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+variable "pubkeys_woranhun" {
+  type    = string
+  default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCa/EVELPE/fowuT4xPa9EpcDEOvdilmU5P8ZFmbsApF9ch6COXUv60TAOKMkTUTHswgAgaqU2zlkrFcxb/pS5N9uv8MaRNRZmdg5lZyYnbk90oY08sEyWzNUdPFo1UcpIUm/rojkW35ikWXabc6VKbn5uvM4c3cxT+ryxDXojLV/rkIW8MmrW1GndiANz047xyAPxBW7LqHIa7M8Q/dHB7rV1zEDVyiu6oe9+zV4wreU+5NFODw2+znNs5BicvxL0YAW2kp1IVg6OUVSHyPH+7eW+VYD6bDVmA+xtyIMsWZBCHki4bup1eouNO8ej5h9zj6GmANZZVV8MiHSVHpJGdT7QOLEKZo+SR4ybgfFrqmcyY54HM+PdHsrycHmVuY30lraUk1/P4yHkmuYUP1/0LdfemLPw81T/W8ndRpw2aIVjQYa97mpIj8FRaA9nbEw7hcsXlGfrVXE46lALt3cJXzS9IoW9FCGMV4/aOUWCb65k0pKlBVMrJDsogDVgO5zAvZp11WYwzjZzZkpGD3Yg4YVGm1DhBaWBuMvcb/peNFgVMItTqJe+DcgCjyyq/iDPosfuyoCkYL3NwNUcBzMXcHgLFCVTeHZlieRKajw5GO2S5toAC+gpzmr31CP8jBGlwAuHotb0CbeMR0Dynk58ewdpiwK0stppKShToyQZJbQ== woranhun@woranhunX220ubu"
+}
+
+variable "pubkeys_richard" {
+  type    = string
+  default = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIONs0OwDN2sMIPotBEwm6l/g4qB0z7k31uXS3tmcZLo4 szabo_richard (git.sch.bme.hu)"
+}
+resource "vsphere_virtual_machine" "richardvm" {
+  resource_pool_id = data.vsphere_resource_pool.NOC-A.id
+  datastore_id     = data.vsphere_datastore.NOC-A-Storage.id
+  name             = "richardvm"
+  guest_id         = "ubuntu64Guest"
+
+  memory               = 32768
+  firmware             = "efi"
+  num_cores_per_socket = 15
+
+  host_system_id = data.vsphere_host.NOC-A.id
+
+  clone {
+    template_uuid = data.vsphere_virtual_machine.ubuntu_template-richard.id
+  }
+  network_interface {
+    network_id     = data.vsphere_network.DnPG-Internal-211.id
+    use_static_mac = true
+    mac_address    = "00:50:56:10:3d:21"
+  }
+  disk {
+    thin_provisioned = false
+    label            = "disk0"
+    size             = 300
+  }
+  cdrom {
+    client_device = true
+  }
+}
\ No newline at end of file
diff --git a/vmware.tf b/vmware.tf
new file mode 100644
index 0000000..64cf784
--- /dev/null
+++ b/vmware.tf
@@ -0,0 +1,55 @@
+terraform {
+  required_providers {
+    vsphere = {
+      source = "hashicorp/vsphere"
+      version = "2.0.0"
+    }
+  }
+  backend "http" {}
+}
+
+# Vsphere provider config
+
+variable "vsphere_user" {
+  description = "Administrator user"
+  type        = string
+  sensitive   = false
+}
+
+variable "vsphere_password" {
+  description = "Administrator password"
+  type        = string
+  sensitive   = true
+}
+
+provider "vsphere" {
+  user           = var.vsphere_user
+  password       = var.vsphere_password
+  vsphere_server = "horizont.sch.bme.hu"
+
+  allow_unverified_ssl = true
+}
+
+data "vsphere_datacenter" "DC" {
+  name = "NOC"
+}
+
+data "vsphere_datastore" "NOC-A-Storage" {
+  name          = "NOC-A-Test-Storage1"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_datastore" "NOC-B-Storage" {
+  name          = "NOC-B-Test-Storage1"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_resource_pool" "NOC-A" {
+  name          = "noc-a-test.internal/Resources"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
+
+data "vsphere_resource_pool" "NOC-B" {
+  name          = "noc-b-test.internal/Resources"
+  datacenter_id = data.vsphere_datacenter.DC.id
+}
\ No newline at end of file
-- 
GitLab