Skip to content

Commit

Permalink
feat: add support for Always Free
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Marchesini <[email protected]>
  • Loading branch information
snafuz committed Jan 13, 2022
1 parent 32aaaa2 commit 6c6aec8
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 112 deletions.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ This project enables you to create and configure network and compute resources o

Under *Stack Information* (the first screen), check the box *I have reviewed and accept the Oracle Terms of Use*. Once that box is checked, the information for the stack will be populated automatically.

3. Click **Next** at the bottom of the screen. This will take you to the **Configure Variables** page. On this page you can optionally provide/change these variables:
- **Compartment** (_optional_): select the compartment where do you want to deploy the stack
3. Click **Next** at the bottom of the screen. This will take you to the **Configure Variables** page. On this page you can provide/change these variables:
- **Compartment**: select the compartment where do you want to deploy the stack
- **SSH public key** (_optional_): the key will allow you to login into the instance.
- **Instance Name** (_optional_): Name of the instance [_default: oci-code-server_]
- **Shape** (_optional_): Instance shape [_default: VM.Standard.E4.Flex_]. If you want your Compute instance to function after your Free Trial ends, check the box for an Always Free Shape.
- **OCPUs number** (_optional_): Only if you have selected a Flex shape [_default: 1_]

- **Use Always Free eligible shape**:
If checked, the Always Free shape (_VM.Standard.E2.1.Micro_) is used. Make sure you are in a region where this Always Free shape is available
If not checked:
- **Instance Shape**: (_optional_): select the shape that will be used for the VM [default: _VM.Standard.E4.Flex_]
- **Availability Domain**: select the Availability Domain (AD) you want the instance to be deployed to. If you have checked Always Free, make sure to select the Always Free elegible AD.
- ***Show Advanced***
- **Instance Name** (_optional_): Name of the instance [_default: oci-code-server_]
- **Flex Instance OCPUs number** (_optional_): Only if you have selected a Flex shape, you can select the number of OCPUs to assign to the flexible shape. [_default: 1_]



Then click **Next** again.

4. On the **Review** page, be sure *Run Apply* is checked, and click **Create**.
Expand All @@ -50,6 +57,11 @@ For more information about `code-server`:
* [code-server](https://github.com/coder/code-server)
* [deploy-code-server](https://github.com/coder/deploy-code-server)

## Know Issues
The code-server instance is launched with the `--link ` [flag](https://coder.com/docs/code-server/latest/link) to provide authentication through GitHub, TLS and a dedicated URL for accessing your VS Code. This feature presents some issues when you try to connect from a different geographic region than the one the instance is running and you encounter an error after the GitHub authentication. Current workaround is to use one of the other options provided by code-server guide to [expose it](https://github.com/coder/code-server/blob/main/docs/guide.md#expose-code-server):
- using [SSH port forwarding](https://github.com/coder/code-server/blob/main/docs/guide.md#port-forwarding-via-ssh)
- using [Let's Encrypt](https://letsencrypt.org/) with [Caddy](https://github.com/coder/code-server/blob/main/docs/guide.md#using-lets-encrypt-with-caddy) or [NGINX](https://github.com/coder/code-server/blob/main/docs/guide.md#using-lets-encrypt-with-nginx)

## Contributing
This project is open source. Please submit your contributions by forking this repository and submitting a pull request! Oracle appreciates any contributions that are made by the open source community.

Expand Down
12 changes: 11 additions & 1 deletion datasources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# get latest Ubuntu Linux 16.04 image
data "oci_core_images" "ubuntu-20-04" {
compartment_id = var.compartment_ocid
compartment_id = local.compartment_id
operating_system = "Canonical Ubuntu"
filter {
name = "display_name"
Expand All @@ -12,3 +12,13 @@ data "oci_core_images" "ubuntu-20-04" {
}
}

data "oci_identity_availability_domains" "ad" {
compartment_id = local.compartment_id
}

locals{
ad_map = {for ad_number,ad in data.oci_identity_availability_domains.ad.availability_domains : "${ad.name}" => (ad_number+1) }
}



110 changes: 51 additions & 59 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
# Copyright (c) 2019, 2021, Oracle Corporation and/or affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl

locals {
compartment_id = var.compartment_ocid == "" ? var.tenancy_ocid : var.compartment_ocid
ad_number = var.instance_ad_name == "" ? var.instance_ad_number : local.ad_map[var.instance_ad_name]
}

module "vsc_instance" {
# source = "oracle-terraform-modules/compute-instance/oci"
source = "github.com/oracle-terraform-modules/terraform-oci-compute-instance"
source = "oracle-terraform-modules/compute-instance/oci"
# general oci parameters
compartment_ocid = var.compartment_ocid
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
compartment_ocid = local.compartment_id
freeform_tags = var.freeform_tags
defined_tags = var.defined_tags
# compute instance parameters
ad_number = var.instance_ad_number
ad_number = local.ad_number
instance_count = 1
instance_display_name = var.instance_display_name
instance_state = "RUNNING"
shape = var.shape
shape = var.use_always_free ? local.always_free_shape : var.shape
source_ocid = data.oci_core_images.ubuntu-20-04.images.0.id
source_type = "image"
instance_flex_memory_in_gbs = var.instance_flex_memory_in_gbs
instance_flex_ocpus = var.instance_flex_ocpus
instance_flex_memory_in_gbs = var.use_always_free ? null : var.instance_flex_memory_in_gbs
instance_flex_ocpus = var.use_always_free ? 1 : var.instance_flex_ocpus
# operating system parameters
ssh_public_keys = var.ssh_public_keys != "" ? var.ssh_public_keys : var.ssh_public_key_path != "" ? file(var.ssh_public_key_path) :""
user_data = base64encode(data.template_file.cloud-config.rendered)
ssh_public_keys = var.ssh_public_keys != "" ? var.ssh_public_keys : var.ssh_public_key_path != "" ? file(var.ssh_public_key_path) : ""
user_data = base64encode(data.template_file.cloud-config.rendered)
# networking parameters
public_ip = var.public_ip
subnet_ocids = [oci_core_subnet.sub.id]
primary_vnic_nsg_ids = [oci_core_network_security_group.nsg.id]
public_ip = var.public_ip
subnet_ocids = [oci_core_subnet.sub.id]
primary_vnic_nsg_ids = [oci_core_network_security_group.nsg.id]

# storage parameters
boot_volume_backup_policy = "disabled"
block_storage_sizes_in_gbs = [50]
}

data "template_file" "cloud-config" {
template = <<YAML
#cloud-config
runcmd:
- iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
- netfilter-persistent save
- while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do sleep 50; done;
- wget https://raw.githubusercontent.com/coder/deploy-code-server/main/deploy-vm/launch-code-server.sh -O - | sh
YAML
boot_volume_backup_policy = "disabled"
block_storage_sizes_in_gbs = [50]
}


module "vcn" {
source = "oracle-terraform-modules/vcn/oci"

# general oci parameters
compartment_id = var.compartment_ocid
vcn_name = "vsc-network"
compartment_id = local.compartment_id
vcn_name = "vsc-network"

# vcn parameters
lockdown_default_seclist = false
vcn_cidrs = [var.vcn_cidr]
create_internet_gateway=true
lockdown_default_seclist = false
vcn_cidrs = [var.vcn_cidr]
create_internet_gateway = true

}

resource "oci_core_network_security_group" "nsg" {
#Required
compartment_id = var.compartment_ocid
compartment_id = local.compartment_id
vcn_id = module.vcn.vcn_id

#Optional
Expand All @@ -70,45 +62,45 @@ resource "oci_core_network_security_group" "nsg" {

resource "oci_core_subnet" "sub" {
#Required
cidr_block = cidrsubnet(var.vcn_cidr, lookup(var.subnets["vsc"], "newbits"), lookup(var.subnets["vsc"], "netnum"))
compartment_id = var.compartment_ocid
vcn_id = module.vcn.vcn_id
cidr_block = cidrsubnet(var.vcn_cidr, lookup(var.subnets["vsc"], "newbits"), lookup(var.subnets["vsc"], "netnum"))
compartment_id = local.compartment_id
vcn_id = module.vcn.vcn_id

#Optional
display_name = "vsc-sub"
dns_label = "vscsub"
prohibit_public_ip_on_vnic = false
route_table_id = module.vcn.ig_route_id
freeform_tags = var.freeform_tags
route_table_id = module.vcn.ig_route_id
freeform_tags = var.freeform_tags
}


module "oci_security_policies" {
source = "github.com/oracle-terraform-modules/terraform-oci-tdf-network-security"
default_compartment_id = var.compartment_ocid
default_freeform_tags = var.freeform_tags
vcn_id = module.vcn.vcn_id
standalone_nsg_rules = {
ingress_rules = [
source = "github.com/oracle-terraform-modules/terraform-oci-tdf-network-security"

default_compartment_id = local.compartment_id
default_freeform_tags = var.freeform_tags
vcn_id = module.vcn.vcn_id

standalone_nsg_rules = {
ingress_rules = [
{
nsg_id = oci_core_network_security_group.nsg.id
description = "code-server"
stateless = false
protocol = "6"
src = "0.0.0.0/0"
src_type = "CIDR_BLOCK"
src_port = null
dst_port = {
min = "80"
max = "80"
nsg_id = oci_core_network_security_group.nsg.id
description = "code-server"
stateless = false
protocol = "6"
src = "0.0.0.0/0"
src_type = "CIDR_BLOCK"
src_port = null
dst_port = {
min = "80"
max = "80"
}
icmp_code = null
icmp_type = null
icmp_code = null
icmp_type = null
}
]
egress_rules = []
egress_rules = []
}
}

Expand Down
103 changes: 72 additions & 31 deletions orm/schema.yaml
Original file line number Diff line number Diff line change
@@ -1,66 +1,107 @@
# Title shown in Application Information tab.
title: Launch code-server instance
informationalText: "Run VS Code on OCI compute instance and access it in the browser."
# Sub Title shown in Application Information tab.
description: Launch a code-server instance on OCI
description: code-server instance
schemaVersion: 1.1.0
version: 1.0
locale: en
variableGroups:
- title: "Hidden"
variables:
- ${tenancy_ocid}
- ${user_ocid}
- ${region}
- ${api_fingerprint}
- ${api_private_key_path}
- ${region}
- ${compartment_ocid}
- ${ssh_public_key_path}
- ${freeform_tags}
- ${defined_tags}
- ${instance_flex_memory_in_gbs}
- ${instance_ad_number}
- ${public_ip}
- ${vcn_cidr}
- ${subnets}
- tenancy_ocid
- user_ocid
- region
- api_fingerprint
- api_private_key_path
- ssh_public_key_path
- freeform_tags
- defined_tags
- instance_flex_memory_in_gbs
- public_ip
- vcn_cidr
- subnets
- instance_ad_number
visible: false

- title: "Configuration"
variables:
- ${compartment_ocid}
- ${ssh_public_keys}
- ${instance_display_name}
- ${shape}
- ${instance_flex_ocpus}
- compartment_ocid
- ssh_public_keys
- use_always_free
- shape
- instance_ad_name
- show_advanced

- title: "Advanced"
visible:
and:
- show_advanced
variables:
- instance_display_name
- instance_flex_ocpus


variables:
compartment_ocid:
title: Compartment
description: Compartment where to deploy the stack
type: oci:identity:compartment:id
required: false
required: true
visible: true
ssh_public_keys:
title: Compute SSH Public Key
type: oci:core:ssh:publickey
description: The public key to install on the instance for SSH access.
required: false
instance_display_name:
title: Instance Name
type: string
description: The name to use for the compute instance
default: oci-code-server
required: false
visible: true
use_always_free:
type: boolean
title: "Use Always Free elegible shape"
required: true
default: true
visible: true
shape:
title: Instance Shape
type: string
type: oci:core:instanceshape:name
description: The shape name to use for the compute instance
default: VM.Standard.E4.Flex
#default: VM.Standard.E4.Flex
required: false
visible:
not:
- use_always_free
dependsOn:
compartmentId: compartment_ocid
instance_ad_name:
title: Availability Domain
type: oci:identity:availabilitydomain:name
description: If you have checked Always Free, make sure to select the Always Free elegible Availability Domain.
#default: 1
required: true
dependsOn:
compartmentId: compartment_ocid
show_advanced:
type: boolean
title: "Show advanced options?"
description: "Shows advanced options."
visible: true
default: false
instance_flex_ocpus:
title: Flex Instance OCPU number
type: string
description: The number of OCPUs. It applies to Flex Instance only
description: The number of OCPUs. It applies to Flex Instances only
default: 1
required: false
visible:
not:
- use_always_free
instance_display_name:
title: Instance Name
type: string
description: The name to use for the compute instance
default: oci-code-server
required: false


outputs:
code-server-publicIP:
Expand Down
1 change: 0 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ output "vsc_instance" {
"OS version" = data.oci_core_images.ubuntu-20-04.images.0.display_name,
}
}

13 changes: 13 additions & 0 deletions user-data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) 2019, 2021, Oracle Corporation and/or affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl

data "template_file" "cloud-config" {
template = <<YAML
#cloud-config
runcmd:
- iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
- netfilter-persistent save
- while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do sleep 50; done;
- wget https://raw.githubusercontent.com/coder/deploy-code-server/main/deploy-vm/launch-code-server.sh -O - | sh
YAML
}
Loading

0 comments on commit 6c6aec8

Please sign in to comment.