diff --git a/.gitignore b/.gitignore index 485dee64b..d3417e6cc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,18 @@ .idea + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +### Terraform ### +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Terraform plan file +*.tfplan.* diff --git a/Makefile b/Makefile index c1796274f..6c8990dea 100644 --- a/Makefile +++ b/Makefile @@ -52,13 +52,19 @@ test: # Run unit test integration: deps-cgo ## Run integration test go test -v sigs.k8s.io/cluster-api-provider-libvirt/test/integration +.PHONY: e2e +e2e: deps-cgo ## Run end-to-end test + hack/packet-provision.sh install + #TODO run tests + hack/packet-provision.sh destroy + .PHONY: lint lint: ## Go lint your code hack/go-lint.sh $(go list -f '{{ .ImportPath }}' ./...) .PHONY: fmt fmt: ## Go fmt your code - hack/verify-gofmt.sh + hack/go-fmt.sh .PHONY: vet vet: ## Apply go vet to all go files diff --git a/hack/packet-provision.sh b/hack/packet-provision.sh new file mode 100755 index 000000000..4bd04ca50 --- /dev/null +++ b/hack/packet-provision.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set +e + +# Your Packet user account +if [ "$PACKET_AUTH_TOKEN" == "" ]; then + echo "You need to set PACKET_AUTH_TOKEN variable first." + echo "Make sure that your SSH key is also set in packet.net" + exit 1 +fi + +# Your Packet user account +if [ "$TF_VAR_packet_project_id" == "" ]; then + echo "You need to set TF_VAR_packet_project_id variable first." + exit 1 +fi + +export TF_VAR_id=${ID:-$(uuidgen | cut -c1-8)} + +cd ./prebuild +case ${1} in + "install") + ssh_path="$TF_VAR_ssh_key_path" + if [ "$TF_VAR_ssh_key_path" == "" ]; then + echo -e "\e[33mCreating temporary SSH file\e[0m" + ssh-keygen -t rsa -b 4096 -C "temporary packet.net key" -P "" -f "/tmp/packet_id_rsa" -q + ssh_path="/tmp/packet_id_rsa" + fi + terraform init -input=false + terraform plan -input=false -out=tfplan.out && terraform apply -input=false -auto-approve tfplan.out + echo -e "\e[32m" + echo -e "*** Your packet.net host is called ${TF_VAR_environment_id}" + echo -e "*** You can also access it via SSH with key located in ${ssh_path}" + echo -e "\e[0m" + ;; + "destroy") + terraform destroy -input=false -auto-approve + rm /tmp/packet_id_rsa* 2>/dev/null || : + ;; + *) + echo "Use '$0 install' or '$0 destroy'." + ;; +esac diff --git a/hack/prebuild/init.sh b/hack/prebuild/init.sh new file mode 100755 index 000000000..85807f6c9 --- /dev/null +++ b/hack/prebuild/init.sh @@ -0,0 +1,24 @@ +#/bin/bash + +yum install -y -d1 libvirt libvirt-daemon-kvm +usermod -aG libvirt root + +# Enable ssh+qemu access mode +cat < /etc/libvirt/libvirtd.conf +unix_sock_group = "libvirt" +unix_sock_rw_perms = "0770" +EOF + +# Next lines are here if we would like to enable tcp+qemu conection mode +#cat < /etc/libvirt/libvirtd.conf +#unix_sock_group = "libvirt" +#unix_sock_rw_perms = "0770" +#listen_tls = 0 +#listen_tcp = 1 +#auth_tcp="none" +#tcp_port = "16509" +#EOF +#echo 'LIBVIRTD_ARGS="--listen"' >> /etc/sysconfig/libvirtd +#iptables -I INPUT -p tcp --dport 16509 -j ACCEPT -m comment --comment "Allow insecure libvirt clients" + +systemctl start libvirtd diff --git a/hack/prebuild/main.tf b/hack/prebuild/main.tf new file mode 100644 index 000000000..6c8e589f9 --- /dev/null +++ b/hack/prebuild/main.tf @@ -0,0 +1,29 @@ +resource "packet_ssh_key" "key" { + name = "unlikely_tf_ssh_key_name-${var.id}" + public_key = "${file("${var.ssh_key_path}")}" +} + +resource "packet_device" "libvirt" { + hostname = "libvirt-${var.id}" + plan = "baremetal_0" + facility = "ewr1" + operating_system = "centos_7" + billing_cycle = "hourly" + project_id = "${var.packet_project_id}" + user_data = "#!/bin/bash\nsed -i 's/PasswordAuthentication.*$/PasswordAuthentication yes/g' /etc/ssh/sshd_config && systemctl restart sshd" + provisioner "remote-exec" { + script = "init.sh" + connection = { + type = "ssh" + user = "root" + password = "${self.root_password}" + agent = false + } + } + depends_on = ["packet_ssh_key.key"] +} + +output "ip" { + value = "${packet_device.libvirt.access_public_ipv4}" +} + diff --git a/hack/prebuild/variables.tf b/hack/prebuild/variables.tf new file mode 100644 index 000000000..c56d21242 --- /dev/null +++ b/hack/prebuild/variables.tf @@ -0,0 +1,14 @@ +variable "ssh_key_path" { + type = "string" + default = "/tmp/packet_id_rsa.pub" +} + +variable "id" { + type = "string" + default = "randomid" +} + +variable "packet_project_id" { + type = "string" + default = "" +}