|
| 1 | +// Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + |
| 3 | +# This is an example of how to resize a volume that’s attached and online without having to reattach it. |
| 4 | +# The null provisioner ensures that a rescan command is run when the volume size has changed |
| 5 | + |
| 6 | +variable "tenancy_ocid" {} |
| 7 | +variable "user_ocid" {} |
| 8 | +variable "fingerprint" {} |
| 9 | +variable "private_key_path" {} |
| 10 | +variable "region" {} |
| 11 | + |
| 12 | +variable "compartment_ocid" {} |
| 13 | + |
| 14 | +variable "ssh_public_key" { |
| 15 | + default = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDM+NcnBp1i4iKNeYf853h0UA2GVLOaa+85lZGykQJMzOWcS3oB3OHVN1nRrS6foQQ7EuNpEMlxfKESCFKh4UQynzb3pWmy/dczPP/EwQY061v0uPJ5tb2SlYgExFt+gzgBpKejEVdN+OH8Jt63+YIadaSbPHdjUYU2xckMXbeYe0mGXJEUdAtcurML81V0BfMEA7TCPVxXf8Fl2JcIHhuMdJv/VfjxAIqA8tj7fI2hqFs4Cf2zMF4nKvHHgkDCXIiqhjOY+R2B44eQHXzXtTlqUr5XgHD3tGbvt+mcfuQr/eV1pnBbIteop/XHpyS0MKJctfeod8+glEukjSfzQwhL0hJ8JVxlidOcoYEIrAIKm0RLDYd4kO5FvChUps1yzm+bVYgQrFkUtzUu9oMpqc053Y0CPJ5YZu50nlx8syzhXtDKItXLifFrf8ekcmsPA2X6G9/v/YXcVUX4Rq1fftwua4Ftl4JQU/3A6Jiw5OSEPz8jf4Z5edGNcBCnPzeCdFkpxbkN/XFNb0azDq+6ke1XhKdj+WrWZedJSXzxP8Iur2ZVlMLIytFhypnZj82hImCRJnXGthrxvklzWT6Wm2znzUvZL0kUbZVSRCIA0oULc1l9pX22zgjfqtifjPiCH+d41FUINBKBHwkRObQ3xQ/RttRqYDYLjSPphqR3phO6fw== <[email protected]>" |
| 16 | +} |
| 17 | + |
| 18 | +variable "ssh_private_key" { |
| 19 | + default = "~/.ssh/id_rsa" |
| 20 | +} |
| 21 | + |
| 22 | +provider "oci" { |
| 23 | + tenancy_ocid = "${var.tenancy_ocid}" |
| 24 | + user_ocid = "${var.user_ocid}" |
| 25 | + fingerprint = "${var.fingerprint}" |
| 26 | + private_key_path = "${var.private_key_path}" |
| 27 | + region = "${var.region}" |
| 28 | +} |
| 29 | + |
| 30 | +variable "instance_shape" { |
| 31 | + default = "VM.Standard2.1" |
| 32 | +} |
| 33 | + |
| 34 | +variable "instance_image_ocid" { |
| 35 | + type = "map" |
| 36 | + |
| 37 | + default = { |
| 38 | + # See https://docs.us-phoenix-1.oraclecloud.com/images/ |
| 39 | + # Oracle-provided image "CentOS-6.10-2020.04.21-0" |
| 40 | + eu-frankfurt-1 = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaajubunwtkoob57bkjgsdinb6xjqgggwkn7hjt3j3lqdtrh72jcnaq" |
| 41 | + |
| 42 | + uk-london-1 = "ocid1.image.oc1.uk-london-1.aaaaaaaaqxjy3ushb72q45cu2ekzvllnkgaf2pdnqyw7q3sv3krybzlcarxa" |
| 43 | + us-ashburn-1 = "ocid1.image.oc1.iad.aaaaaaaa27fldayogs4sre3l6arufehn6ist4u77hbylux5oisnarijlne7a" |
| 44 | + us-phoenix-1 = "ocid1.image.oc1.phx.aaaaaaaa66qzvli64ojblgjgzrqcutcynjqfinoiyib3u7vn4stauloyig3q" |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +variable "size" { |
| 49 | + default = "130" # size in GBs |
| 50 | +} |
| 51 | + |
| 52 | +resource "oci_core_instance" "test_instance" { |
| 53 | + availability_domain = "${data.oci_identity_availability_domain.ad.name}" |
| 54 | + compartment_id = "${var.compartment_ocid}" |
| 55 | + display_name = "TestInstance1" |
| 56 | + shape = "${var.instance_shape}" |
| 57 | + |
| 58 | + shape_config { |
| 59 | + ocpus = 1 |
| 60 | + } |
| 61 | + |
| 62 | + create_vnic_details { |
| 63 | + subnet_id = "${oci_core_subnet.test_subnet.id}" |
| 64 | + display_name = "Primaryvnic" |
| 65 | + assign_public_ip = true |
| 66 | + hostname_label = "tfexampleinstance1" |
| 67 | + } |
| 68 | + |
| 69 | + source_details { |
| 70 | + source_type = "image" |
| 71 | + source_id = "${var.instance_image_ocid[var.region]}" |
| 72 | + |
| 73 | + # Apply this to set the size of the boot volume that's created for this instance. |
| 74 | + # Otherwise, the default boot volume size of the image is used. |
| 75 | + # This should only be specified when source_type is set to "image". |
| 76 | + #boot_volume_size_in_gbs = "60" |
| 77 | + } |
| 78 | + |
| 79 | + metadata = { |
| 80 | + ssh_authorized_keys = "${var.ssh_public_key}" |
| 81 | + user_data = "${base64encode(file("./userdata/bootstrap"))}" |
| 82 | + } |
| 83 | + |
| 84 | + # Apply the following flag only if you wish to preserve the attached boot volume upon destroying this instance |
| 85 | + # Setting this and destroying the instance will result in a boot volume that should be managed outside of this config. |
| 86 | + # When changing this value, make sure to run 'terraform apply' so that it takes effect before the resource is destroyed. |
| 87 | + #preserve_boot_volume = true |
| 88 | + timeouts { |
| 89 | + create = "60m" |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +# Define the volumes that are attached to the compute instances. |
| 94 | + |
| 95 | +resource "oci_core_volume" "test_block_volume" { |
| 96 | + availability_domain = "${data.oci_identity_availability_domain.ad.name}" |
| 97 | + compartment_id = "${var.compartment_ocid}" |
| 98 | + display_name = "TestBlock0" |
| 99 | + size_in_gbs = "${var.size}" |
| 100 | +} |
| 101 | + |
| 102 | +resource "oci_core_volume_attachment" "test_block_attach" { |
| 103 | + attachment_type = "iscsi" |
| 104 | + instance_id = "${oci_core_instance.test_instance.*.id[0]}" |
| 105 | + volume_id = "${oci_core_volume.test_block_volume.*.id[0]}" |
| 106 | + device = "/dev/oracleoci/oraclevdb" |
| 107 | + |
| 108 | + # Set this to enable CHAP authentication for an ISCSI volume attachment. The oci_core_volume_attachment resource will |
| 109 | + # contain the CHAP authentication details via the "chap_secret" and "chap_username" attributes. |
| 110 | + use_chap = true |
| 111 | + |
| 112 | + # Set this to attach the volume as read-only. |
| 113 | + #is_read_only = true |
| 114 | +} |
| 115 | + |
| 116 | +resource "null_resource" "remote-exec" { |
| 117 | + depends_on = ["oci_core_instance.test_instance", "oci_core_volume_attachment.test_block_attach"] |
| 118 | + |
| 119 | + provisioner "remote-exec" { |
| 120 | + connection { |
| 121 | + agent = false |
| 122 | + timeout = "30m" |
| 123 | + host = "${oci_core_instance.test_instance.*.public_ip[0]}" |
| 124 | + user = "opc" |
| 125 | + private_key = "${file(var.ssh_private_key)}" |
| 126 | + } |
| 127 | + |
| 128 | + inline = [ |
| 129 | + "touch ~/IMadeAFile.Right.Here", |
| 130 | + "sudo iscsiadm -m node -o new -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -p ${oci_core_volume_attachment.test_block_attach.*.ipv4[0]}:${oci_core_volume_attachment.test_block_attach.*.port[0]}", |
| 131 | + "sudo iscsiadm -m node -o update -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -n node.startup -v automatic", |
| 132 | + "sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -p ${oci_core_volume_attachment.test_block_attach.*.ipv4[0]}:${oci_core_volume_attachment.test_block_attach.*.port[0]} -o update -n node.session.auth.authmethod -v CHAP", |
| 133 | + "sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -p ${oci_core_volume_attachment.test_block_attach.*.ipv4[0]}:${oci_core_volume_attachment.test_block_attach.*.port[0]} -o update -n node.session.auth.username -v ${oci_core_volume_attachment.test_block_attach.*.chap_username[0]}", |
| 134 | + "sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -p ${oci_core_volume_attachment.test_block_attach.*.ipv4[0]}:${oci_core_volume_attachment.test_block_attach.*.port[0]} -o update -n node.session.auth.password -v ${oci_core_volume_attachment.test_block_attach.*.chap_secret[0]}", |
| 135 | + "sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -p ${oci_core_volume_attachment.test_block_attach.*.ipv4[0]}:${oci_core_volume_attachment.test_block_attach.*.port[0]} -l", |
| 136 | + "sudo iscsiadm -m node -T ${oci_core_volume_attachment.test_block_attach.*.iqn[0]} -p ${oci_core_volume_attachment.test_block_attach.*.ipv4[0]}:${oci_core_volume_attachment.test_block_attach.*.port[0]} -l", |
| 137 | + |
| 138 | + # if you are using the Oracle Linux 7.7, Ubuntu 16.04 or CentOS 7, please use the following commands. |
| 139 | + # If you are using windows, please comment it out. |
| 140 | + "sudo dd iflag=direct if=/dev/sdb of=/dev/null count=1", |
| 141 | + |
| 142 | + "echo '1' | sudo tee /sys/class/block/sdb/device/rescan", |
| 143 | + ] |
| 144 | + } |
| 145 | + |
| 146 | + triggers = { |
| 147 | + always_run = "${oci_core_volume.test_block_volume.size_in_gbs}" |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +resource "oci_core_vcn" "test_vcn" { |
| 152 | + cidr_block = "10.1.0.0/16" |
| 153 | + compartment_id = "${var.compartment_ocid}" |
| 154 | + display_name = "TestVcn" |
| 155 | + dns_label = "testvcn" |
| 156 | +} |
| 157 | + |
| 158 | +resource "oci_core_internet_gateway" "test_internet_gateway" { |
| 159 | + compartment_id = "${var.compartment_ocid}" |
| 160 | + display_name = "TestInternetGateway" |
| 161 | + vcn_id = "${oci_core_vcn.test_vcn.id}" |
| 162 | +} |
| 163 | + |
| 164 | +resource "oci_core_default_route_table" "default_route_table" { |
| 165 | + manage_default_resource_id = "${oci_core_vcn.test_vcn.default_route_table_id}" |
| 166 | + display_name = "DefaultRouteTable" |
| 167 | + |
| 168 | + route_rules { |
| 169 | + destination = "0.0.0.0/0" |
| 170 | + destination_type = "CIDR_BLOCK" |
| 171 | + network_entity_id = "${oci_core_internet_gateway.test_internet_gateway.id}" |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +resource "oci_core_subnet" "test_subnet" { |
| 176 | + availability_domain = "${data.oci_identity_availability_domain.ad.name}" |
| 177 | + cidr_block = "10.1.20.0/24" |
| 178 | + display_name = "TestSubnet" |
| 179 | + dns_label = "testsubnet" |
| 180 | + security_list_ids = ["${oci_core_vcn.test_vcn.default_security_list_id}"] |
| 181 | + compartment_id = "${var.compartment_ocid}" |
| 182 | + vcn_id = "${oci_core_vcn.test_vcn.id}" |
| 183 | + route_table_id = "${oci_core_vcn.test_vcn.default_route_table_id}" |
| 184 | + dhcp_options_id = "${oci_core_vcn.test_vcn.default_dhcp_options_id}" |
| 185 | +} |
| 186 | + |
| 187 | +data "oci_identity_availability_domain" "ad" { |
| 188 | + compartment_id = "${var.tenancy_ocid}" |
| 189 | + ad_number = 1 |
| 190 | +} |
0 commit comments