Project

ops-cli

0.0
The project is in a healthy, maintained state
Write task classes under ops/tasks/ and bin/ops becomes the one entry point for a repo's operational work: discovery and dispatch (Ops::Task, Ops::Runner), standard-library option parsing, and helpers for running commands safely (Ops::Shell), sealing Kubernetes secrets (Ops::SecretSealer) and reaching pods (Ops::PodExec).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 13.0
~> 3.13
~> 1.0
 Project Readme

ops-cli

A small framework for repo-local operational CLIs. Drop the gem into a project, write task classes under ops/tasks/, and bin/ops becomes the single entry-point for build/deploy/console/whatever-else operational work lives in that repo. Plain Ruby with no runtime dependencies: option parsing is the standard library's OptionParser.

What's in the box

  • Ops::Task — base class with a small dispatch protocol (desc, task_options, call, optional already_done?).
  • Ops::Runner + Ops::Registry — discovery + dispatch. Runner reads argv via Ops::Arguments, looks up a class from the Registry, calls task.perform, and renders the resulting Ops::TaskResult. Registry walks Ops::Tasks:: and registers any class that descends from Ops::Task; the constant path becomes the colon-namespaced, kebab-cased command name (Ops::Tasks::Secrets::RotateMaster → secrets:rotate-master).
  • Ops::TaskResult — boundary value object with three terminal states (successful, skipped, failed).
  • Ops::PodExec — kubectl-based helpers (find_pod, exec_in_pod, open_in_pod) for tasks that need to land in a running pod.
  • Ops::Shell — Shell.run(*cmd, display: false, stdin: nil) for side-effect commands; Shell.capture(*cmd, stdin: nil) for data-flow commands. cmd is argv (program, then arguments), optionally after a Hash of environment variables; the program is always run directly, never through a shell, so a lone string is a program name, not a command line. Both raise Ops::Shell::Error on non-zero exit, carrying command, stdout, stderr and status; its message names the program and exit status with the tool's stderr, never the arguments or environment, which may hold secrets.
  • Ops::SecretSealer — produces sealed-secret encrypted-data blobs with kubeseal. One sealer per namespace; #seal(secret_name:, values:, type: :generic) per target. It builds the Secret manifest itself and hands it to kubeseal on stdin, so plaintext never appears on a command line. Types: :generic (Opaque) and :basic_auth; TLS / docker-registry would be separate sealer classes.
  • exe/ops — the bin-stubbable executable.

Installation

# Gemfile
gem "ops-cli", "~> 1.0.0.pre"

Until 1.0.0 is out, releases are prereleases (1.0.0.pre.N), which Bundler only picks up when the requirement names one, as above; it also takes 1.0.0 and later 1.0.x when they come.

Then:

bundle install
bundle binstubs ops-cli   # creates ./bin/ops

The gem was first published as janus-ops; require "janus_ops" still works, as an alias for require "ops/cli".

Writing tasks

Tasks live under ops/tasks/ in the consuming repo. Each file defines a class under Ops::Tasks:: that inherits from Ops::Task. The constant path is the command name: each segment kebab-cased, joined with colons (Ops::Tasks::Secrets::RotateMaster → secrets:rotate-master).

# ops/tasks/build.rb
module Ops
  module Tasks
    class Build < Ops::Task
      desc "build [TAG]", "Build and push the application image"

      def task_options
        opts.option :force_rebuild, type: :boolean, default: false
      end

      def call
        tag = opts.positional.first || "latest"
        # ...use opts.force_rebuild
      end
    end
  end
end

Invoke it: bin/ops build v1.2.3 --force-rebuild.

desc records the task's usage and summary (class-level metadata; used by bin/ops for the listing). task_options is the instance method where you declare options on opts. call is the work method (no args — read parsed argv via opts); its return value is wrapped into a successful Ops::TaskResult. If call raises, the runner reports it and exits 1. Override already_done? (default false, no args) to short-circuit into a skipped result when the work has already been applied.

Declared options are accessible as methods on opts: opts.force_rebuild returns the parsed value for option :force_rebuild. Positional args live at opts.positional. Typos raise NoMethodError rather than silently returning nil. Reserved names (the methods that already exist on Ops::TaskOptions — parsed, positional, usage, description, etc.) raise at declaration time.

Arguments are checked before the task runs: an unrecognized flag fails the task (exit 1) before already_done? or call, even if the task never reads its options.

Options

opts.option NAME, **settings (or its alias opts.declare) declares one option. The flag is the name with underscores turned into dashes:

Setting Effect
type: :boolean --[no-]name; the value is true or false
type: :numeric --name=VALUE, converted with Integer()
no type --name=VALUE, a string
default: the value when the flag isn't given (otherwise nil)
desc: the flag's line in bin/ops <cmd> --help
aliases: extra flag spellings, e.g. ["-n"]

Nested namespaces become colon names automatically:

ops/tasks/console/rails.rb  →  Ops::Tasks::Console::Rails  →  bin/ops console:rails

Repo-specific base classes

Layer your own base class on top of Ops::Task in ops/lib/ to share options or helpers across tasks. Subclasses inherit their parent's options by calling super inside task_options; skip super to start fresh. Plain Ruby method override.

# ops/lib/tenant_task.rb
class TenantTask < Ops::Task
  def task_options
    opts.option :namespace, aliases: ["-n"]
  end
end

# ops/tasks/deploy.rb
class Ops::Tasks::Deploy < TenantTask
  desc "deploy NAME", "deploy a tenant"

  def task_options
    super                                        # inherits :namespace
    opts.option :dry_run, type: :boolean, default: false
  end

  def call
    # opts.namespace and opts.dry_run are both available here
  end
end

Interactive tasks

ops-cli doesn't prompt for anything itself, but a task that needs to ask (a confirmation before something destructive, a password that mustn't echo) can use cli-ui. Add it to the consuming repo's Gemfile:

gem "cli-ui", "~> 2.7"

and in the task:

require "cli/ui"

def call
  return unless CLI::UI::Prompt.confirm("Remove tenant #{name}?", default: false)

  password = CLI::UI::Prompt.ask_password("New password:")
  # ...
end

Its Prompt (ask, ask_password, confirm, ask with options:) and Spinner primitives fit tasks well. Its framing (CLI::UI::Frame.open) and inline {{color:...}} formatting are a bigger visual layer, best left out so task output stays plain and readable in logs.

Where tasks are discovered

The ops/ directory in the consuming repo — containing lib/ (eager-loaded on startup so task files can reference shared base classes) and tasks/ (lazy-walked when commands are resolved). Resolution order:

  1. ENV["OPS_ROOT"] (escape hatch, mostly for tests)
  2. Bundler.root / "ops" (the normal case — consuming app has a Gemfile)
  3. Dir.pwd / "ops" (fallback if Bundler isn't loaded)

bin/ops runs the Ruby under that ops/ directory, the way rake runs a Rakefile: treat it as running the repo's code.

Top-level flags

  • bin/ops / bin/ops --help / bin/ops -T — list all commands
  • bin/ops <cmd> --help (or -h, or help) — per-command help: the task's desc usage and description, then its options

Development

bundle install
bundle exec rspec
bundle exec rubocop