From 9ca703f2c1d01d6b568d5dd6cc7f734f51345346 Mon Sep 17 00:00:00 2001 From: Justin Steele Date: Fri, 14 Feb 2025 15:31:01 -0500 Subject: [PATCH 1/2] feat: extend post_create_script to accept a list of script files --- README.md | 36 +++++++++++++++++++++---- lib/kitchen/driver/oci.rb | 2 +- lib/kitchen/driver/oci/mixin/actions.rb | 24 ++++++++++++++++- lib/kitchen/driver/oci_version.rb | 4 ++- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2638f90..6102684 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Kitchen::OCI +[![Gem Version](https://badge.fury.io/rb/kitchen-oci.svg)](https://badge.fury.io/rb/kitchen-oci) + A Test Kitchen Driver for Oracle Cloud Infrastructure (OCI) `kitchen-oci` can create two types of instances: @@ -51,7 +53,7 @@ The following driver parameters are common to both instance types, but are not r - `oci_config`, Hash of additional `OCI::Config` settings. Allows you to test without an oci config file [[more](#use-without-oci-config-file)] - `ssh_keypath`, SSH public key (default: `~/.ssh/id_rsa.pub`) - `ssh_keygen`, Automatically generate the rsa key pair for an instance (default: `false`) [[more](#ssh-keygen)] - - `post_create_script`, run a script on an instance after deployment + - `post_create_script`, run a script on an instance after deployment [[more](#post-create-script)] - `post_create_reboot`, reboot the instance after instance creation (default: `false`) - `proxy_url`, Connect via the specified proxy URL [[more](#proxy-support)] - `defined_tags`, Hash containing tag name(s) and values(s). Each key must be predefined and scoped into a namespace. @@ -152,7 +154,6 @@ Optional settings for WinRM support in Windows [[more](#windows-support)]: custom_metadata: key1: value1 key2: value2 - post_create_script: >- ``` ## DBaaS Instance Type @@ -275,6 +276,32 @@ Alternately, if you simply pass a string to the user_data, it will be base64 enc gid: 1000 ``` +## Post Create Script + +The `post_create_script` property is another means of executing a command or series of commands on the instance. The scripts or commands specified in `post_create_script` are executed after `cloud-init`, but before `post_create` lifecycle hooks. + +This property accepts both inline scripts and list of files. Examples: + +Passing a command as a string: + +```yml + driver: + name: oci + post_create_script: echo "here i am running a post_create_script" +``` + +Passing a list of script files: + +```yml + driver: + name: oci + post_create_script: + - ./path/to/my/fixture/script.sh + - ./path/to/a/second/fixture/script.sh +``` + +These scripts are executed by the user specified as the transport username (most likely `opc` or `ubuntu`) so any privilege elevation would need to be gained via `sudo` or the `elevated: true` property of the `WinRM` transport. + ## SSH Keygen The driver can generate an ssh key pair for an instance during creation. In order to turn this feature on, add the `ssh_keygen` property to the `driver` and set the value to `true`. This can be set in the `driver` section on a @@ -481,9 +508,8 @@ WIN_IMAGE_ID These environment variables should align with your tenancy and the region in which you intend to test. -There is a kitchen.yml file in the `test/integration/fixtures` directory in this project that can be used to test the gem. From this -directory, execute `bundle exec kitchen list` to see a list of instances. All normal kitchen commands work within the `bundle exec` -context from here. +There is a [kitchen.yml](test/integration/fixtures/kitchen.yml) in this project that can be used to test the gem. From the same directory as the [kitchen.yml](test/integration/fixtures/kitchen.yml), +execute `bundle exec kitchen list` to see a list of instances. All normal kitchen commands work within the `bundle exec` context from here. ## Maintainer diff --git a/lib/kitchen/driver/oci.rb b/lib/kitchen/driver/oci.rb index 2d289e7..ce5747d 100644 --- a/lib/kitchen/driver/oci.rb +++ b/lib/kitchen/driver/oci.rb @@ -128,7 +128,7 @@ def create(state) inst = instance_class(config, state, oci, api, __method__) launch(state, inst) create_and_attach_volumes(config, state, oci, api) - process_post_script(state) + execute_post_create_script(state) reboot(state, inst) end diff --git a/lib/kitchen/driver/oci/mixin/actions.rb b/lib/kitchen/driver/oci/mixin/actions.rb index 0a233a2..7ea22f0 100644 --- a/lib/kitchen/driver/oci/mixin/actions.rb +++ b/lib/kitchen/driver/oci/mixin/actions.rb @@ -38,14 +38,36 @@ def launch(state, inst) # Executes the post script on the instance. # # @param state [Hash] (see Kitchen::StateFile) - def process_post_script(state) + def execute_post_create_script(state) return if config[:post_create_script].nil? + if config[:post_create_script].is_a?(String) + execute_post_create_string(state) + elsif config[:post_create_script].is_a?(Array) + execute_post_create_file(state) + end + end + + # Executes the post create script from a String. + # + # @param state [Hash] (see Kitchen::StateFile) + def execute_post_create_string(state) info("Running post create script") script = config[:post_create_script] instance.transport.connection(state).execute(script) end + # Reads the specified file and executes as a post create script. + # + # @param state [Hash] (see Kitchen::StateFile) + def execute_post_create_file(state) + config[:post_create_script].each do |script| + info("Running post create script #{File.basename(script)}") + script = File.read script + instance.transport.connection(state).execute(script) + end + end + # Reboots an instance. # # @param state [Hash] (see Kitchen::StateFile) diff --git a/lib/kitchen/driver/oci_version.rb b/lib/kitchen/driver/oci_version.rb index a216574..5b75929 100644 --- a/lib/kitchen/driver/oci_version.rb +++ b/lib/kitchen/driver/oci_version.rb @@ -20,6 +20,8 @@ module Kitchen module Driver # Version string for Oracle OCI Kitchen driver - OCI_VERSION = "1.26.0" + # + # @author Stephen Pearson () + OCI_VERSION = "1.27.0" end end From 6bed8b2f5d3efd56e2027ad49bf9b4e4dc368baf Mon Sep 17 00:00:00 2001 From: Justin Steele Date: Fri, 14 Feb 2025 15:42:49 -0500 Subject: [PATCH 2/2] chore: update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d782969..5ea094f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # kitchen-oci CHANGELOG +# 1.27.0 +- feat: extend `post_create_script` to accept a list of script files +- doc: add YARD tags to all methods and classes and add `metadata` to the gemspec + +# 1.26.0 +- feat: generate rsa key pair automatically per instance + # 1.25.0 - feat: allow `image_name` to function the same for ARM as it does for Linux