lib/command/run_detached.rb in cpl-0.5.1 vs lib/command/run_detached.rb in cpl-0.6.0

- old
+ new

@@ -5,11 +5,12 @@ NAME = "run:detached" USAGE = "run:detached COMMAND" REQUIRES_ARGS = true OPTIONS = [ app_option(required: true), - image_option + image_option, + workload_option ].freeze DESCRIPTION = "Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`)" LONG_DESCRIPTION = <<~DESC - Runs one-off **_non-interactive_** replicas (close analog of `heroku run:detached`) - Uses `Cron` workload type with log async fetching @@ -31,20 +32,23 @@ cpl run:detached rails db:migrate -a $APP_NAME --image latest # Uses a different image (which may not be promoted yet). cpl run:detached rails db:migrate -a $APP_NAME --image appimage:123 # Exact image name cpl run:detached rails db:migrate -a $APP_NAME --image latest # Latest sequential image + + # Uses a different workload + cpl run:detached rails db:migrate:status -a $APP_NAME -w other-workload ``` EX WORKLOAD_SLEEP_CHECK = 2 - attr_reader :location, :workload, :one_off + attr_reader :location, :workload, :one_off, :container def call @location = config[:default_location] - @workload = config[:one_off_workload] + @workload = config.options["workload"] || config[:one_off_workload] @one_off = "#{workload}-runner-#{rand(1000..9999)}" clone_workload wait_for_workload(one_off) show_logs_waiting @@ -58,18 +62,19 @@ def clone_workload # rubocop:disable Metrics/MethodLength progress.puts "- Cloning workload '#{workload}' on '#{config.options[:app]}' to '#{one_off}'" # Get base specs of workload spec = cp.fetch_workload!(workload).fetch("spec") - container = spec["containers"].detect { _1["name"] == workload } || spec["containers"].first + container_spec = spec["containers"].detect { _1["name"] == workload } || spec["containers"].first + @container = container_spec["name"] # remove other containers if any - spec["containers"] = [container] + spec["containers"] = [container_spec] # Set runner - container["command"] = "bash" - container["args"] = ["-c", 'eval "$CONTROLPLANE_RUNNER"'] + container_spec["command"] = "bash" + container_spec["args"] = ["-c", 'eval "$CONTROLPLANE_RUNNER"'] # Ensure one-off workload will be running spec["defaultOptions"]["suspend"] = false # Ensure no scaling @@ -78,20 +83,20 @@ spec["defaultOptions"]["capacityAI"] = false # Override image if specified image = config.options[:image] image = "/org/#{config.org}/image/#{latest_image}" if image == "latest" - container["image"] = image if image + container_spec["image"] = image if image # Set cron job props spec["type"] = "cron" spec["job"] = { "schedule" => "* * * * *", "restartPolicy" => "Never" } spec["defaultOptions"]["autoscaling"] = {} - container.delete("ports") + container_spec.delete("ports") - container["env"] ||= [] - container["env"] << { "name" => "CONTROLPLANE_TOKEN", "value" => ControlplaneApiDirect.new.api_token } - container["env"] << { "name" => "CONTROLPLANE_RUNNER", "value" => runner_script } + container_spec["env"] ||= [] + container_spec["env"] << { "name" => "CONTROLPLANE_TOKEN", "value" => ControlplaneApiDirect.new.api_token } + container_spec["env"] << { "name" => "CONTROLPLANE_RUNNER", "value" => runner_script } # Create workload clone cp.apply("kind" => "workload", "name" => one_off, "spec" => spec) end