|
| 1 | +### OCI Metadata Script Handler |
| 2 | + |
| 3 | +#### Overview |
| 4 | + |
| 5 | +The intent of this is to make it easy to be able to run a startup or shutdown script on an instance for purposes such |
| 6 | +as configuration, ping back announcements or anything else one may want to do during these lifecycle steps. |
| 7 | + |
| 8 | +Scripts are defined in the instance metadata as being either of `startup` or `shutdown` and can be sourced from |
| 9 | +either local storage, metadata value, remote url or |
| 10 | +[Object Storage](https://docs.cloud.oracle.com/iaas/Content/Object/Concepts/objectstorageoverview.htm). If more |
| 11 | +than one source is supplied both will be executed with remote being run first. |
| 12 | + |
| 13 | +`startup-script` Examples : |
| 14 | + |
| 15 | +``` |
| 16 | +"startup-script" : "/opt/service/start/bootstrap.py" |
| 17 | +``` |
| 18 | + |
| 19 | +``` |
| 20 | +"startup-script" : "IyEvdXNyL2Jpbi9lbnYgYmFzaAoKZWNobyAibXkgYXdlc29tZSBzdGFydHVwIHNjcmlwdCIK" |
| 21 | +``` |
| 22 | + |
| 23 | +The first will execute the script that is contained locally on the host instance. It will be copied into a |
| 24 | +temporary location before execution. |
| 25 | + |
| 26 | +The second is the actual script data base64 encoded and supplied in the instance metadata. It will be base64 decoded |
| 27 | +into a temporary location under a temporary filename and then executed. |
| 28 | + |
| 29 | +`startup-script-url` Examples: |
| 30 | + |
| 31 | +``` |
| 32 | +"startup-script-url" : "oci://bucket@namespace/bootstrap.py" |
| 33 | +``` |
| 34 | + |
| 35 | +``` |
| 36 | +"startup-script-url" : "https://trusted-remote-server.io/boot/bootstrap.py" |
| 37 | +``` |
| 38 | + |
| 39 | +The first will fetch the script from an Object Storage bucket in the namespace, store in a temporary working |
| 40 | +directory and then execute it. It is the responsibility of the owner to make sure that the instances have access to |
| 41 | +the bucket. |
| 42 | + |
| 43 | +The second will make a request to the specified location, download to a temporary directory and execute the |
| 44 | +payload. Be warned that this should be a trusted known site and that egress to the remote location is allowed. |
| 45 | + |
| 46 | +#### Object Storage Example |
| 47 | + |
| 48 | +If Object storage will be the source for the scripts a |
| 49 | +[policy](https://docs.cloud.oracle.com/iaas/Content/Identity/Concepts/policygetstarted.htm) |
| 50 | +will need to be applied such that the instances |
| 51 | +can easily have read-only access to the buckets in the namespace to retrieve the scripts from. A definition for |
| 52 | +[Dynamic Groups](https://docs.cloud.oracle.com/iaas/Content/Identity/Tasks/managingdynamicgroups.htm) is also helpful |
| 53 | +for being able to allow instances in this group to have the policy for read-only access to the buckets. Using |
| 54 | +[Terraform](https://www.terraform.io/) it could be something like : |
| 55 | + |
| 56 | + |
| 57 | +``` |
| 58 | +resource "oci_identity_dynamic_group" "ro_script_buckets" { |
| 59 | + compartment_id = "${var.tenancy_ocid}" |
| 60 | + name = "ro-script-buckets" |
| 61 | + description = "dynamic group for reading startup/shutdown script buckets" |
| 62 | + matching_rule = <<EOF |
| 63 | +Any {instance.compartment.id = 'ocid1.compartment.oc1..aaaaaxutgua', |
| 64 | +instance.compartment.id = 'ocid1.compartment.oc1..aaaaallwh6mv7avuozfsus4yaia', |
| 65 | +instance.compartment.id = 'ocid1.compartment.oc1..aaaaaaaaszz2v4gyfza'} |
| 66 | +EOF |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Where the `instance.compartment.id` values are those of compartments where instances will be created in and will |
| 71 | +require access to the startup or shutdown scripts buckets. |
| 72 | + |
| 73 | +``` |
| 74 | +resource "oci_identity_policy" "script_buckets_read" { |
| 75 | + depends_on = ["oci_identity_dynamic_group.ro_script_buckets"] |
| 76 | + compartment_id = "${var.tenancy_ocid}" |
| 77 | + name = "script-buckets-read" |
| 78 | + description = "read only policy for scripts buckets" |
| 79 | + statements = [ |
| 80 | + "allow dynamic-group ro-script-buckets to read buckets in compartment Bucket_Compartment where any {target.bucket.name='startup-scripts', target.bucket.name='shutdown-scripts'}", |
| 81 | + "allow dynamic-group ro-script-buckets to read objects in compartment Bucket_Compartment where any {target.bucket.name='startup-scripts', target.bucket.name='shutdown-scripts'}" |
| 82 | + ] |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +This will allow the instances in the dynamic group created above to access the buckets with the names `startup-scripts` |
| 87 | +and `shutdown-scripts`. |
0 commit comments