Skip to content

Commit

Permalink
move cloud: close #12
Browse files Browse the repository at this point in the history
  • Loading branch information
schonfinkel committed Nov 26, 2024
1 parent 84e90f1 commit dd7a82f
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ terraform.rc
id_ed25519*
tfplan
output.json
inputs.tfvars

# Nix
.env*
result
*.qcow2

# Magalu CLI
mgc_cli
86 changes: 86 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
];

# Nix configuration
nix.settings.trusted-users = ["@wheel"];
nix.settings.trusted-users = [ "@wheel" ];
nix = {
package = pkgs.nixVersions.stable;
extraOptions = ''
Expand Down
5 changes: 2 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@

# Modules
bootstrap = [
./modules/configuration.nix
./configuration.nix
./modules/extras.nix
./modules/networking.nix
./modules/users.nix
];

cloud = [
./configuration.nix
./modules/erlang.nix
./modules/nginx.nix
./modules/postgres.nix
./modules/secrets.nix
] ++ bootstrap;

# Qemu Setup
Expand Down
12 changes: 8 additions & 4 deletions hardware-configuration.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{modulesPath, ...}: {
imports = [(modulesPath + "/profiles/qemu-guest.nix")];
{ modulesPath, ... }:
{
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];

boot = {
initrd.availableKernelModules = ["ata_piix" "uhci_hcd"];
kernelModules = ["kvm-intel"];
initrd.availableKernelModules = [
"ata_piix"
"uhci_hcd"
];
kernelModules = [ "kvm-intel" ];
};

zramSwap.enable = true;
Expand Down
16 changes: 8 additions & 8 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ rekey:
cd secrets && nix run github:ryantm/agenix -- -r

# ------------------
# Terraform Commands
# Tofu Commands
# ------------------

# Updates terraform variables
# Updates tofu variables
update-vars:
@./generate-inputs.sh --flake ".#{{ target_vm_bootstap }}" --region {{ target_region }}

# Runs `terraform plan`
# Runs `tofu plan`
plan:
terraform plan -var-file="inputs.tfvars" -out tfplan
tofu plan -var-file="inputs.tfvars" -out tfplan

# Runs `terraform apply`
# Runs `tofu apply`
apply:
terraform apply "tfplan"
tofu apply "tfplan"

# Destroys Terraform infra
# Destroys tofu infra
destroy:
terraform apply -destroy -var-file="inputs.tfvars"
tofu apply -destroy -var-file="inputs.tfvars"
49 changes: 30 additions & 19 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ variable "flake" {
default = "bootstrap"
}

variable "vm_type" {
type = string
default = "BV2-8-40"
}

# ---------
# Provider
# ---------
Expand All @@ -45,10 +50,12 @@ provider "mgc" {
# -----------
# Networking
# -----------
resource "mgc_network_vpcs" "vpc" {
name = "${var.prefix}-vpc"
description = "VPC"
}
# TODO: Add VPC
#resource "mgc_network_vpc" "vpc" {
# provider = mgc.se
# name = "${var.prefix}-vpc"
# description = "${var.prefix}-vpc"
#}

resource "mgc_network_security_groups" "sg" {
name = "${var.prefix}-${var.region}-sg"
Expand Down Expand Up @@ -128,20 +135,23 @@ resource "mgc_block_storage_volumes" "volume" {
}

resource "mgc_virtual_machine_instances" "vm" {
provider = mgc.sudeste
provider = mgc.se
name = var.prefix

machine_type = {
name = "cloud-bs1.xsmall"
name = var.vm_type
}

image = {
name = "cloud-ubuntu-22.04 LTS"
name = "cloud-debian-12 LTS"
}

network = {
associate_public_ip = true
delete_public_ip = true

#vpc = {
# id = mgc_network_vpc.vpc.network_id
#}

interface = {
security_groups = [{
Expand All @@ -150,39 +160,40 @@ resource "mgc_virtual_machine_instances" "vm" {
}
}

user_data = filebase64("${path.module}/templates/user_data.sh")

ssh_key_name = mgc_ssh_keys.ssh_key.name
}

# Attaching the VM with Block Storage
resource "mgc_block_storage_volume-attachment" "attached_block_storage" {
block_storage_id = mgc_block-storage_volumes.volume.id
virtual_machine_id = mgc_virtual-machine_instances.vm.id
resource "mgc_block_storage_volume_attachment" "va" {
block_storage_id = mgc_block_storage_volumes.volume.id
virtual_machine_id = mgc_virtual_machine_instances.vm.id
}

# This ensures that the instance is reachable via `ssh` before we deploy NixOS
resource "null_resource" "wait" {
provisioner "remote-exec" {
connection {
host = mgc_virtual_machine_instances.vm.network.public_address
host = mgc_virtual_machine_instances.vm.network.public_address
private_key = tls_private_key.ssh_key.private_key_openssh
}

inline = [":"] # Do nothing; we're just testing SSH connectivity
}
}


# -------------
# Provisioning
# -------------
module "deploy" {
source = "github.com/nix-community/nixos-anywhere//terraform/all-in-one"
nixos_system_attr = ".#nixosConfigurations.${var.flake}.config.system.build.toplevel"
source = "github.com/nix-community/nixos-anywhere//terraform/all-in-one"
nixos_system_attr = ".#nixosConfigurations.${var.flake}.config.system.build.toplevel"
nixos_partitioner_attr = ".#nixosConfigurations.${var.flake}.config.system.build.diskoScript"
debug_logging = true
debug_logging = true

instance_id = mgc_virtual_machine_instances.vm.id
target_host = mgc_virtual_machine_instances.vm.network.public_address
instance_id = mgc_virtual_machine_instances.vm.id
target_host = mgc_virtual_machine_instances.vm.network.public_address
install_user = "debian"
}

Expand All @@ -203,7 +214,7 @@ resource "local_file" "nix_output" {

resource "local_file" "output" {
content = jsonencode({
public_ip = mgc_virtual_machine_instances.vm.network.public_address
public_ip = mgc_virtual_machine_instances.vm.network.public_address
})
filename = "${path.module}/output.json"
}
2 changes: 2 additions & 0 deletions templates/user_data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
(umask 377; echo '${tls_private_key.ssh_key.private_key_openssh}' > /var/lib/id_ed25519)

0 comments on commit dd7a82f

Please sign in to comment.