Skip to content

Commit

Permalink
feat: add gcloud template and tailscale for oci
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Urbanek <[email protected]>
  • Loading branch information
shanduur committed Jan 25, 2025
1 parent 92fa86a commit 72013b6
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/autopilot.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: autopilot

on: pull_request_target

jobs:
auto-approve:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
name: semantic-pr

on:
pull_request_target:
types:
- opened
- reopened
- edited
- synchronize

permissions:
pull-requests: read

jobs:
pr-title:
runs-on: ubuntu-latest
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/tofu.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
name: tofu

on:
push:
branches:
- "main"
schedule:
- cron: "0 4 * * *"

concurrency:
group: ${{ github.workflow }}

jobs:
linode:
runs-on: ubuntu-latest
Expand All @@ -30,6 +33,7 @@ jobs:
run: |
tofu -chdir=./terraform/linode \
apply -auto-approve -input=false -lock=true -no-color
oci:
runs-on: ubuntu-latest
steps:
Expand All @@ -40,6 +44,8 @@ jobs:
TF_VAR_fingerprint: ${{ secrets.OCI_FINGERPRINT }}
TF_VAR_private_key: ${{ secrets.OCI_PEM_PRV }}
TF_VAR_ssh_public_keys: ${{ secrets.SSH_PUB_KEY }}
TF_VAR_tailscale_oauth_client_id: ${{ secrets.TAILSCALE_OAUTH_CLIENT_ID }}
TF_VAR_tailscale_oauth_secret: ${{ secrets.TAILSCALE_OAUTH_SECRET }}
TF_VAR_tenancy_ocid: ${{ secrets.OCI_TENANCY_OCID }}
TF_VAR_user_ocid: ${{ secrets.OCI_USER_OCID }}
run: |
Expand All @@ -49,8 +55,25 @@ jobs:
TF_VAR_fingerprint: ${{ secrets.OCI_FINGERPRINT }}
TF_VAR_private_key: ${{ secrets.OCI_PEM_PRV }}
TF_VAR_ssh_public_keys: ${{ secrets.SSH_PUB_KEY }}
TF_VAR_tailscale_oauth_client_id: ${{ secrets.TAILSCALE_OAUTH_CLIENT_ID }}
TF_VAR_tailscale_oauth_secret: ${{ secrets.TAILSCALE_OAUTH_SECRET }}
TF_VAR_tenancy_ocid: ${{ secrets.OCI_TENANCY_OCID }}
TF_VAR_user_ocid: ${{ secrets.OCI_USER_OCID }}
run: |
tofu -chdir=./terraform/oci \
apply -auto-approve -input=false -lock=true -no-color
gcloud:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: opentofu/setup-opentofu@v1
- env:
PG_CONN_STR: ${{ secrets.PG_CONN_STR }}
run: |
tofu -chdir=./terraform/gcloud init -upgrade
- env:
PG_CONN_STR: ${{ secrets.PG_CONN_STR }}
run: |
tofu -chdir=./terraform/gcloud \
apply -auto-approve -input=false -lock=true -no-color
28 changes: 28 additions & 0 deletions terraform/gcloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.8 |
| <a name="requirement_google"></a> [google](#requirement\_google) | 6.14.1 |

## Providers

No providers.

## Modules

No modules.

## Resources

No resources.

## Inputs

No inputs.

## Outputs

No outputs.
<!-- END_TF_DOCS -->
Empty file added terraform/gcloud/main.tf
Empty file.
25 changes: 25 additions & 0 deletions terraform/gcloud/modules/always_free/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

No requirements.

## Providers

No providers.

## Modules

No modules.

## Resources

No resources.

## Inputs

No inputs.

## Outputs

No outputs.
<!-- END_TF_DOCS -->
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions terraform/gcloud/modules/always_free/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# tflint-ignore: terraform_required_version
terraform {
required_providers {
# tflint-ignore: terraform_required_providers
google = {
source = "hashicorp/google"
}
}
}
Empty file added terraform/gcloud/variables.tf
Empty file.
14 changes: 14 additions & 0 deletions terraform/gcloud/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 1.8"

backend "pg" {
schema_name = "tofu_remote_state_gcloud"
}

required_providers {
google = {
source = "hashicorp/google"
version = "6.14.1"
}
}
}
3 changes: 3 additions & 0 deletions terraform/oci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.8 |
| <a name="requirement_oci"></a> [oci](#requirement\_oci) | 6.23.0 |
| <a name="requirement_tailscale"></a> [tailscale](#requirement\_tailscale) | 0.17.2 |

## Providers

Expand All @@ -30,6 +31,8 @@ No resources.
| <a name="input_private_key"></a> [private\_key](#input\_private\_key) | Contents of OCI API Private Key used. | `string` | n/a | yes |
| <a name="input_region"></a> [region](#input\_region) | The oci region where resources will be created. | `string` | `"eu-frankfurt-1"` | no |
| <a name="input_ssh_public_keys"></a> [ssh\_public\_keys](#input\_ssh\_public\_keys) | Public SSH keys to be included in the ~/.ssh/authorized\_keys file for the default user on the instance. | `string` | n/a | yes |
| <a name="input_tailscale_oauth_client_id"></a> [tailscale\_oauth\_client\_id](#input\_tailscale\_oauth\_client\_id) | OAuth Client ID for Tailscale. | `string` | n/a | yes |
| <a name="input_tailscale_oauth_secret"></a> [tailscale\_oauth\_secret](#input\_tailscale\_oauth\_secret) | OAuth Secret for Tailscale. | `string` | n/a | yes |
| <a name="input_tenancy_ocid"></a> [tenancy\_ocid](#input\_tenancy\_ocid) | Tenancy ocid where to create the sources. | `string` | n/a | yes |
| <a name="input_user_ocid"></a> [user\_ocid](#input\_user\_ocid) | Ocid of user that terraform will use to create the resources. | `string` | n/a | yes |

Expand Down
8 changes: 5 additions & 3 deletions terraform/oci/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ module "oci_amd" {
instance_shape = "VM.Standard.E2.1.Micro"
subnet_id = module.oci_core.subnet_id

tenancy_ocid = var.tenancy_ocid
ssh_public_keys = var.ssh_public_keys
availability_domain = var.availability_domain
tenancy_ocid = var.tenancy_ocid
ssh_public_keys = var.ssh_public_keys
availability_domain = var.availability_domain
tailscale_oauth_client_id = var.tailscale_oauth_client_id
tailscale_oauth_secret = var.tailscale_oauth_secret
}
4 changes: 4 additions & 0 deletions terraform/oci/modules/always_free/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ No requirements.
| Name | Version |
|------|---------|
| <a name="provider_oci"></a> [oci](#provider\_oci) | n/a |
| <a name="provider_tailscale"></a> [tailscale](#provider\_tailscale) | n/a |

## Modules

Expand All @@ -18,6 +19,7 @@ No modules.
| Name | Type |
|------|------|
| [oci_core_instance.instance](https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_instance) | resource |
| [tailscale_tailnet_key.tailscale_key](https://registry.terraform.io/providers/tailscale/tailscale/latest/docs/resources/tailnet_key) | resource |
| [oci_core_images.instance_images](https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/core_images) | data source |
| [oci_identity_availability_domain.ad](https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/identity_availability_domain) | data source |

Expand All @@ -32,6 +34,8 @@ No modules.
| <a name="input_region"></a> [region](#input\_region) | The oci region where resources will be created. | `string` | `"eu-frankfurt-1"` | no |
| <a name="input_ssh_public_keys"></a> [ssh\_public\_keys](#input\_ssh\_public\_keys) | Public SSH keys to be included in the ~/.ssh/authorized\_keys file for the default user on the instance. | `string` | n/a | yes |
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id) | Name of the instance. | `string` | n/a | yes |
| <a name="input_tailscale_oauth_client_id"></a> [tailscale\_oauth\_client\_id](#input\_tailscale\_oauth\_client\_id) | OAuth Client ID for Tailscale. | `string` | n/a | yes |
| <a name="input_tailscale_oauth_secret"></a> [tailscale\_oauth\_secret](#input\_tailscale\_oauth\_secret) | OAuth Secret for Tailscale. | `string` | n/a | yes |
| <a name="input_tenancy_ocid"></a> [tenancy\_ocid](#input\_tenancy\_ocid) | Tenancy ocid where to create the sources. | `string` | n/a | yes |

## Outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ locals {
boot_volume = var.instance_shape == "VM.Standard.E2.1.Micro" ? 100 : 200
}

resource "tailscale_tailnet_key" "tailscale_key" {
description = "OCI Always-Free VM key"
expiry = 3600
reusable = true
preauthorized = true
recreate_if_invalid = "always"
tags = ["${var.instance_shape}", "OCI", "Always-Free-VM"]
}

resource "oci_core_instance" "instance" {
count = local.count
availability_domain = data.oci_identity_availability_domain.ad.name
Expand Down Expand Up @@ -49,6 +58,12 @@ resource "oci_core_instance" "instance" {

metadata = {
ssh_authorized_keys = var.ssh_public_keys
user_data = base64encode(templatefile(
"./templates/user_data.tftpl",
{
tailscale_key = tailscale_tailnet_key.key,
}
))
}

timeouts {
Expand Down
8 changes: 8 additions & 0 deletions terraform/oci/modules/always_free/templates/user_data.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#cloud-config

runcmd:
# Tailscale install
- ['sh', '-c', 'curl -fsSL https://tailscale.com/install.sh | sh']
- ['sh', '-c', "echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf && echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf && sudo sysctl -p /etc/sysctl.d/99-tailscale.conf" ]
- ['tailscale', 'up', '--auth-key=${tailscale_key}']
- ['tailscale', 'set', '--ssh']
10 changes: 10 additions & 0 deletions terraform/oci/modules/always_free/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ variable "instance_source_type" {
description = "The source type for the instance."
default = "image"
}

variable "tailscale_oauth_client_id" {
type = string
description = "OAuth Client ID for Tailscale."
}

variable "tailscale_oauth_secret" {
type = string
description = "OAuth Secret for Tailscale."
}
9 changes: 9 additions & 0 deletions terraform/oci/modules/always_free/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@ terraform {
oci = {
source = "oracle/oci"
}

# tflint-ignore: terraform_required_providers
tailscale = {
source = "tailscale/tailscale"
}
}
}

provider "tailscale" {
api_key = var.tailscale_api_key
}
File renamed without changes.
10 changes: 10 additions & 0 deletions terraform/oci/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ variable "availability_domain" {
description = "Availability Domain of the instance."
default = 3
}

variable "tailscale_oauth_client_id" {
type = string
description = "OAuth Client ID for Tailscale."
}

variable "tailscale_oauth_secret" {
type = string
description = "OAuth Secret for Tailscale."
}
5 changes: 5 additions & 0 deletions terraform/oci/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ terraform {
source = "oracle/oci"
version = "6.23.0"
}

tailscale = {
source = "tailscale/tailscale"
version = "0.17.2"
}
}
}

0 comments on commit 72013b6

Please sign in to comment.