module Cult::CLI

Public Instance Methods

ask(prompt) click to toggle source

Asks the user a question, and returns the response. Ensures a newline exists after the response.

# File lib/cult/cli/common.rb, line 92
def ask(prompt)
  print "#{prompt}: "
  $stdin.gets.chomp
end
commands() click to toggle source
# File lib/cult/cli/load.rb, line 15
def commands
  Cult::CLI.methods(false).select do |m|
    m.to_s.match(/_cmd\z/)
  end.map do |m|
    Cult::CLI.send(m)
  end
end
console_cmd() click to toggle source
# File lib/cult/cli/console_cmd.rb, line 46
    def console_cmd
      Cri::Command.define do
        name        'console'
        summary     'Launch a REPL with the project loaded'
        description <<~EOD.format_description
          The Cult console loads your project, and starts a Ruby REPL.  This can
          be useful for troubleshooting, or just poking around the project.

          A few convenience global variables are set to inspect.
        EOD

        flag :i,  :irb,    'IRB (default)'
        flag :r,  :ripl,   'Ripl'
        flag :p,  :pry,    'Pry'
        flag nil, :reexec, 'Console has been exec\'d for a reload'

        run(arguments: none) do |opts, args, cmd|
          context = ConsoleContext.new(Cult.project, ARGV)

          if opts[:reexec]
            $stderr.puts "Reloaded."
          else
            $stderr.puts <<~EOD

              Welcome to the #{Rainbow('Cult').green} Console.

              Your project has been made accessible via 'project', and forwards
              via 'self':

                => #{context.inspect}

              Useful methods: nodes, roles, providers

            EOD
          end

          context.load_rc
          context_binding = context.instance_eval { binding }

          if opts[:ripl]
            require 'ripl'
            ARGV.clear
            # Look, something reasonable:
            Ripl.start(binding: context_binding)

          elsif opts[:pry]
            require 'pry'
            context_binding.pry
          else
            # irb: This is ridiculous.
            require 'irb'
            ARGV.clear
            IRB.setup(nil)

            irb = IRB::Irb.new(IRB::WorkSpace.new(context_binding))
            IRB.conf[:MAIN_CONTEXT] = irb.context
            IRB.conf[:IRB_RC].call(irb.context) if IRB.conf[:IRB_RC]

            trap("SIGINT") do
              irb.signal_handle
            end

            begin
              catch(:IRB_EXIT) do
                irb.eval_input
              end
            ensure
              IRB::irb_at_exit
            end
          end
        end
      end
    end
fetch_item(v, from:, label: nil, exist: true, method: :fetch) click to toggle source

v is an option or argv value from a user, label: is the name of it.

This asserts that `v` is in the collection `from`, and returns it. if `exist` is false, it verifies that v is NOT in the collection and returns v.

As a convenience, `from` can be a class like Role, which will imply 'Cult.project.roles'

CLIError is raised if these invariants are violated

# File lib/cult/cli/common.rb, line 156
def fetch_item(v, from:, label: nil, exist: true, method: :fetch)
  implied_from = case
    when from == Driver;   Cult::Drivers.all
    when from == Provider; Cult.project.providers
    when from == Role;     Cult.project.roles
    when from == Node;     Cult.project.nodes
    else;                  nil
  end

  label ||= implied_from ? from.name.split('::')[-1].downcase : nil
  from = implied_from

  fail ArgumentError, "label cannot be implied" if label.nil?

  unless [:fetch, :all].include?(method)
    fail ArgumentError, "method must be :fetch or :all"
  end

  # We got no argument
  fail CLIError, "Expected #{label}" if v.nil?

  if exist
    begin
      from.send(method, v).tap do |r|
        # Make sure
        fail KeyError if method == :all && r.empty?
      end
    rescue KeyError
      fail CLIError, "#{label} does not exist: #{v}"
    end
  else
    if from.key?(v)
      fail CLIError, "#{label} already exists: #{v}"
    end
    v
  end
end
fetch_items(*keys, **kw) click to toggle source

Takes a list of keys and returns an array of objects that correspond to any of them.

# File lib/cult/cli/common.rb, line 197
def fetch_items(*keys, **kw)
  keys.flatten.map do |key|
    fetch_item(key, method: :all, **kw)
  end.flatten
end
init_cmd() click to toggle source
# File lib/cult/cli/init_cmd.rb, line 8
    def init_cmd
      Cri::Command.define do
        drivers = Cult::Drivers.all.map{|d| d.driver_name }.join ", "

        optional_project
        name        'init'
        aliases     'new'
        usage       'init DIRECTORY'
        summary     'Create a new Cult project'
        description <<~EOD.format_description
          Generates a new Cult project, based on a project skeleton.

          The most useful option is --driver, which both specifies a driver and
          sets up a provider of the same name.  This will make sure the
          dependencies for using the driver are install, and any bookkeeping
          required to start interacting with your VPS provider is handled.

          This usually involves entering an account name or getting an API key.

          The default provider is "script", which isn't too pleasant, but has
          no dependencies.  The "script" driver manages your fleet by executing
          scripts in $CULT_PROJECT/script, which you have to implement.  This is
          tedious, but very doable.  However, if Cult knows about your provider,
          it can handle all of this without you having to do anything.

          Cult knows about the following providers:

          > #{drivers}

          The init process just gets you started, and it's nothing that couldn't
          be accomplished by hand, so if you want to change anything later, it's
          not a big deal.

          The project generated sets up a pretty common configuration: a `base`
          role, a 'bootstrap' role, and a demo task that puts a colorful banner
          in each node's MOTD.
        EOD

        required :d, :driver,   'Driver with which to create your provider'
        required :p, :provider, 'Specify an explicit provider name'
        flag     :g, :git,      'Enable Git integration'

        run(arguments: 1) do |opts, args, cmd|
          project = Project.new(args[0])
          if project.exist?
            fail CLIError, "a Cult project already exists in #{project.path}"
          end

          project.git_integration = opts[:git]

          driver_cls = if !opts[:provider] && !opts[:driver]
            opts[:provider] ||= 'scripts'
            CLI.fetch_item(opts[:provider], from: Driver)
          elsif opts[:provider] && !opts[:driver]
            CLI.fetch_item(opts[:provider], from: Driver)
          elsif opts[:driver] && !opts[:provider]
            CLI.fetch_item(opts[:driver], from: Driver).tap do |dc|
              opts[:provider] = dc.driver_name
            end
          elsif opts[:driver]
            CLI.fetch_item(opts[:driver], from: Driver)
          end

          fail CLIError, "Hmm, no driver class" if driver_cls.nil?

          skel = Skel.new(project)
          skel.copy!

          provider_conf = {
            name: opts[:provider],
            driver: driver_cls.driver_name
          }

          CLI.offer_gem_install do
            driver_conf = driver_cls.setup!
            provider_conf.merge!(driver_conf)


            provider_dir = File.join(project.location_of("providers"),
                                     provider_conf[:name])
            FileUtils.mkdir_p(provider_dir)


            provider_file = File.join(provider_dir, "provider.json")
            File.write(provider_file, JSON.pretty_generate(provider_conf))


            defaults_file = File.join(provider_dir, "defaults.json")
            defaults = Provider.generate_defaults(provider_conf)
            File.write(defaults_file, JSON.pretty_generate(defaults))
          end

          if opts[:git]
            Dir.chdir(project.path) do
              `git init .`
              `git add -A`
              `git commit -m "[Cult] Created new project"`
            end
          end

        end

      end
    end
launch_browser(url) click to toggle source

it's common for drivers to need the user to visit a URL to confirm an API key or similar. This does this in the most compatable way I know.

# File lib/cult/cli/common.rb, line 118
def launch_browser(url)
  case RUBY_PLATFORM
    when /darwin/
      system "open", url
    when /mswin|mingw|cygwin/
      system "start", url
    else
      system "xdg-open", url
  end
end
load_commands!() click to toggle source
# File lib/cult/cli/load.rb, line 8
def load_commands!
  Dir.glob(File.join(__dir__, "*_cmd.rb")).each do |file|
    require file
  end
end
node_cmd() click to toggle source
# File lib/cult/cli/node_cmd.rb, line 10
    def node_cmd
      node = Cri::Command.define do
        optional_project
        name        'node'
        aliases     'nodes'
        summary     'Manage nodes'
        description <<~EOD.format_description
          The node commands manipulate your local index of nodes.  A node is
          conceptually description of a server.
        EOD

        run(arguments: none) do |opts, args, cmd|
          puts cmd.help
          exit
        end
      end

      node_ssh = Cri::Command.define do
        name        'ssh'
        usage       'ssh /NODE+/ [command...]'
        summary     'Starts an SSH shell to NODE'

        flag :i, :interactive,       "Force interactive mode"
        flag :I, :'non-interactive', "Force non-interactive mode"

        description <<~EOD.format_description
          With no additional arguments, initiates an interactive SSH connection
          to a node, authenticated with the node's public key.

          Additional arguments are passed to the 'ssh' command to allow for
          scripting or running one-off commands on the node.

          By default, cult assumes an interactive SSH session when no extra
          SSH arguments are passed, and a non-interactive session otherwise.
          You can force this behavior one way or the other with --interactive
          or --non-interactive.

          Cult will run SSH commands over all matching nodes in parallel if it
          considers your command non-interactive.
        EOD

        esc = ->(s) { Shellwords.escape(s) }

        run(arguments: 1 .. unlimited) do |opts, args, cmd|
          if opts[:interactive] && opts[:'non-interactive']
            fail CLIError, "can't specify --interactive and --non-interactive"
          end

          interactive = opts[:interactive] ||
                        (opts[:'non-interactive'] && false)

          nodes = CLI.fetch_items(args[0], from: Node)

          # With args, we'll assume it's a non-interactive session and run them
          # in parallel, otherwise, we'll assume it's interactive and force
          # them to run one at a time.
          ssh_extra = args[1 .. -1]
          interactive ||= ssh_extra.empty?
          concurrent = interactive || nodes.size == 1 ? 1 : nil

          Cult.paramap(nodes, concurrent: concurrent) do |node|
            # Through source control, etc, these sometimes end up with improper
            # permissions.  OpenSSH won't let us use it otherwise, and there's
            # no option to disable the check.
            File.chmod(0600, node.ssh_private_key_file)

            ssh_args = 'ssh', '-i', esc.(node.ssh_private_key_file),
                       '-p', esc.(node.ssh_port.to_s),
                       '-o', "UserKnownHostsFile=#{esc.(node.ssh_known_hosts_file)}",
                       esc.("#{node.user}@#{node.host}")
            ssh_args += ssh_extra
            # We used to use exec here, but with paramap, the forked process
            # has to live to long enough to report it's return value.
            system(*ssh_args)
            exit if interactive
          end
        end
      end
      node.add_command(node_ssh)

      node_new = Cri::Command.define do
        name        'new'
        usage       'new -r /ROLE+/ [options] NAME0 NAME1 ...'
        summary     'Create a new node'
        description <<~EOD.format_description
          This command creates a new node specification and then creates it with
          your provider.

          The newly created node will have all the roles listed in --role.  If
          none are specified, it'll have the role "base".  If no name is
          provided, it will be named after its role(s).

          If multiple names are provided, a new node is created for each name
          given.  The --count option is incompatible with multiple names given
          on the command line.

          The --count option lets you create an arbitrary amount of new nodes.
          The nodes will be identical, except they'll be named with arbitrary
          random suffixes, like:

          > web-fjfowhs7, web-48pqee6v

          And so forth.
        EOD

        required :r, :role,      'Specify possibly multiple /ROLE+/',
                                  multiple: true
        required :n, :count,     'Generates <value> number of nodes'

        required :p, :provider,  'Use /PROVIDER/ to create the node'
        required :Z, :zone,      'Provider zone'
        required :I, :image,     'Provider image'
        required :S, :size,      'Provider instance size'

        run(arguments: unlimited) do |opts, args, cmd|
          random_suffix = ->(basename) do
            begin
              suffix = CLI.unique_id
              CLI.fetch_item("#{basename}-#{suffix}", from: Node, exist: false)
            rescue CLIError
              retry
            end
          end

          generate_sequenced_names = ->(name, n) do
            (0...n).map do
              random_suffix.(name)
            end
          end

          names = args.dup

          unless opts[:count].nil? || opts[:count].match(/^\d+$/)
            fail CLIError, "--count must be an integer"
          end

          if names.size > 1 && opts[:count]
            fail CLIError, "cannot specify both --count and more than one name"
          end

          roles = CLI.fetch_items(opts[:role] || 'base', from: Role)

          if names.empty?
            names.push roles.map(&:name).join("-")
            opts[:count] ||= 1
          end

          names = opts[:count] ? generate_sequenced_names.(names[0],
                                                           opts[:count].to_i)
                               : names

          # Makes sure they're all new.
          names = names.map do |name|
            CLI.fetch_item(name, from: Node, exist: false)
          end

          provider = if opts.key?(:provider)
            CLI.fetch_item(opts[:provider], from: Provider)
          else
            Cult.project.default_provider
          end

          # Use --size if it was specified, otherwise pull the
          # provider's default.
          node_spec = %i(size image zone).map do |m|
            value = opts[m] || provider.definition["default_#{m}"]
            fail CLIError, "No #{m} specified (and no default)" if value.nil?
            [m, value]
          end.to_h

          Cult.paramap(names) do |name|
            data = {
              name: name,
              roles: roles.map(&:name)
            }

            Node.from_data!(Cult.project, data).tap do |node|
              puts "Provisioning #{node.name}..."
              prov_data = provider.provision!(name: node.name,
                                              image: node_spec[:image],
                                              size: node_spec[:size],
                                              zone: node_spec[:zone],
                                              ssh_public_key: node.ssh_public_key_file)
              prov_data['provider'] = provider.name
              File.write(node.state_path, JSON.pretty_generate(prov_data))

              c = Commander.new(project: Cult.project, node: node)
              puts "Bootstrapping #{node.name}..."
              c.bootstrap!

              puts "Installing roles for #{node.name}..."
              c.install!(node)

              puts "Node installed: #{node.name}"
            end
          end

        end
      end
      node.add_command(node_new)

      node_rm = Cri::Command.define do
        name        'rm'
        usage       'rm /NODE+/ ...'
        summary     'Destroy nodes'
        description <<~EOD.format_description
          Destroys all nodes named NODE, or match the pattern described by
          NODE.

          First, the remote node is destroyed, then the local definition.

          This command respects the global --yes option, otherwise, you will
          be prompted before each destroy.
        EOD

        run(arguments: 1 .. unlimited) do |opts, args, cmd|
          nodes = CLI.fetch_items(args, from: Node)
          concurrent = CLI.yes? ? :max : 1
          Cult.paramap(nodes, concurrent: concurrent) do |node|
            if CLI.yes_no?("Destroy node `#{node}`?")
              puts "destroying #{node}"
              begin
                node.provider.destroy!(id: node.definition['id'],
                                       ssh_key_id: node.definition['ssh_key_id'])
              rescue Exception => e
                puts "Exception while remote-destroying node: #{e.to_s}\n" +
                     "#{e.backtrace}"
                puts "Continuing, though."
              end
              fail unless node.path.match(/#{Regexp.escape(node.name)}/)
              FileUtils.rm_rf(node.path)
            end
            nil
          end
        end
      end
      node.add_command(node_rm)

      node_ls = Cri::Command.define do
        name        'ls'
        summary     'List nodes'
        usage       'ls /NODE*/ ...'
        description <<~EOD.format_description
          This command lists the nodes in the project.
        EOD

        required :r, :role, 'List only nodes which include <value>',
                     multiple: true

        run(arguments: unlimited) do |opts, args, cmd|
          nodes = args.empty? ? Cult.project.nodes
                              : CLI.fetch_items(args, from: Node)

          if opts[:role]
            roles = CLI.fetch_items(opts[:role], from: Role)
            nodes = nodes.select do |n|
              roles.any? { |role| n.has_role?(role) }
            end
          end

          table = Terminal::Table.new(headings:
            ['Node', 'Provider', 'Zone', 'Public IPv4', 'Private IPv4', 'Roles']
          )

          table.rows = Cult.paramap(nodes) do |node|
            role_string = node.build_order.reject(&:node?).map do |role|
              if node.zone_leader?(role)
                Rainbow('*' + role.name).cyan
              else
                role.name
              end
            end.join(' ')

            [ node.name, node.provider&.name, node.zone,
              node.addr(:public), node.addr(:private), role_string]
          end

          puts table
        end
      end
      node.add_command(node_ls)


      node_sync = Cri::Command.define do
        name        'sync'
        usage       'sync /NODE*/ ...'
        summary     'Synchronize host information across fleet'
        description <<~EOD.format_description
          Computes, pre-processes, and executes "sync" tasks on every NODE,
          or all nodes if none are specified.

          Sync tasks are tasks that begin with 'sync-'.  They are meant to
          process dynamic information about the fleet well after a node has
          been created.  Typically, you'll run `cult node sync` to let each
          instance know about its new neighborhood after you add or remove
          new nodes.

          Sync tasks can optionally specify a "pass", with "sync-P0-..." or
          "sync-P1-...".  When `cult node sync` executes, it ensures that:

            1. On a given node, all tasks in the current pass are executed
               sequentially, in role and asciibetical order.

            2. Across the fleet, nodes which have tasks to run in a given pass
               are run concurently with each other.

            3. The entire fleet (or NODE selection) synchonizes between passes.
               "Pass 0" has run on EVERY node (across any role boundaries)
               before "Pass 1" is started on ANY node.

          Sync tasks without a specified pass are implicitly in "Pass 0".

          The sync can be restricted to a specified set of passes with the
          --pass option.  Note that this skips dependent passes.

          The sync can be restricted to a specified set of CONCRETE role tasks
          with the --role option.  No dependencies are considered: Cult
          calculates the tasks it would've ran, then removes all tasks not
          belonging to a role given to --roles
        EOD

        required :R, :role, "Skip sync tasks not in /ROLE/.  Can be specified " +
                            "more than once.",
                            multiple: true

        required :P, :pass, "Only execute PASS.  Can be specified more than " +
                            "once.",
                            multiple: true

        run(arguments: unlimited) do |opts, args, cmd|
          nodes = args.empty? ? Cult.project.nodes
                              : CLI.fetch_items(args, from: Node)
          roles = opts[:role].nil? ? Cult.project.roles
                                   : CLI.fetch_items(opts[:role], from: Role)
          c = CommanderSync.new(project: Cult.project, nodes: nodes)
          passes = opts[:pass] ? opts[:pass].map(&:to_i) : nil
          c.sync!(roles: roles, passes: passes)
        end
      end
      node.add_command(node_sync)

      node_ping = Cri::Command.define do
        name        'ping'
        summary     'Check the responsiveness of each node'
        usage       'ping /NODE*/'

        flag :d, :destroy, 'Destroy nodes that are not responding.'

        description <<~EOD.format_description
          Connects to each node and reports health information.
        EOD

        run(arguments: unlimited) do |opts, args, cmd|
          nodes = args.empty? ? Cult.project.nodes
                              : CLI.fetch_items(args, from: Node)

          table = Terminal::Table.new(headings: ["Node", "Status"])
          table.rows = Cult.paramap(nodes, quiet: true) do |node|
            c = Commander.new(project: Cult.project, node: node)
            status = c.ping
            [ node.name, status ? Rainbow(status).green
                                : Rainbow("unreachable").red ]
          end
          puts table
        end
      end
      node.add_command(node_ping)

      node_addr = Cri::Command.define do
        name        'addr'
        aliases     'ip'
        summary     'print IP address of node'
        usage       'addr [/NODE+/ ...]'
        flag        :p, :private, 'Print private address'
        flag        :'6', :ipv6, 'Print ipv6 address'
        flag        :'4', :ipv4, 'Print ipv4 address'
        description <<~EOD.format_description
        EOD

        run(arguments: unlimited) do |opts, args, cmd|
          prot = Cult.project.default_ip_protocol
          if opts[:ipv4] && opts[:ipv6]
            fail CLIError, "can't specify --ipv4 and --ipv6"
          end

          prot = :ipv6 if opts[:ipv6]
          prot = :ipv4 if opts[:ipv4]

          priv = opts[:private] ? :private: :public

          nodes = args.empty? ? Cult.project.nodes
                              : CLI.fetch_items(args, from: Node)
          nodes.each do |node|
            puts node.addr(priv, prot)
          end
        end
      end
      node.add_command(node_addr)


      return node
    end
offer_gem_install() { || ... } click to toggle source

This intercepts GemNeededError and does the installation dance. It looks a bit hairy because it has a few resumption points, e.g., attempts user gem install, and if that fails, tries the sudo gem install.

# File lib/cult/cli/common.rb, line 207
    def offer_gem_install(&block)
      prompt_install = ->(gems) do
        unless quiet?
          print <<~EOD
            This driver requires the installation of one or more gems:

              #{gems.inspect}

            Cult can install them for you.
          EOD
        end
        yes_no?("Install?")
      end

      try_install = ->(gem, sudo: false) do
        cmd = "gem install #{Shellwords.escape(gem)}"
        cmd = "sudo #{cmd}" if sudo
        puts "executing: #{cmd}"
        system cmd
        $?.success?
      end

      begin
        yield
      rescue ::Cult::Driver::GemNeededError => needed
        sudo = false
        loop do
          sudo = catch :sudo_attempt do
            # We don't want to show this again on a retry
            raise unless sudo || prompt_install.(needed.gems)

            needed.gems.each do |gem|
              success = try_install.(gem, sudo: sudo)
              if !success
                if sudo
                  puts "Nothing seemed to have worked.  Giving up."
                  puts "The gems needed are #{needed.gems.inspect}."
                  raise
                else
                  puts "It doesn't look like that went well."
                  if yes_no?("Retry with sudo?")
                    throw :sudo_attempt, true
                  end
                  raise
                end
              end
            end

            # We exit our non-loop: Everything went fine.
            break
          end
        end

        # Everything went fine, we need to retry the user-supplied block.
        Gem.refresh
        retry
      end
    end
password(prompt) click to toggle source

Disables echo to ask the user a password.

# File lib/cult/cli/common.rb, line 104
def password(prompt)
  STDIN.noecho do
    begin
      ask(prompt)
    ensure
      puts
    end
  end
end
prompt(*args) click to toggle source
# File lib/cult/cli/common.rb, line 98
def prompt(*args)
  ask(*args)
end
provider_cmd() click to toggle source
# File lib/cult/cli/provider_cmd.rb, line 8
    def provider_cmd
      provider = Cri::Command.define do
        optional_project
        name        'provider'
        aliases     'providers'
        summary     'Provider commands'
        description <<~EOD.format_description
          A provider is a VPS service.  Cult ships with drivers for quite a few
          services, (which can be listed with `cult provider drivers`).

          The commands here actually set up your environment with provider
          accounts.  Regarding terminology:

          A "driver" is an interface to a third party service you probably pay
          for, for example, "mikes-kvm-warehouse" would be a driver that knows
          how to interact with the commercial VPS provider "Mike's KVM
          Warehouse".

          A "provider" is a configured account on a service, which uses a
          driver to get things done.  For example "Bob's Account at Mike's
          KVM Warehouse".

          In a lot the common case, you'll be using one provider, which is using
          a driver of the same name.
        EOD

        run(arguments: none) do |opts, args, cmd|
          puts cmd.help
          exit
        end
      end


      provider_ls = Cri::Command.define do
        name        'ls'
        usage       'ls [/PROVIDER+/ ...]'
        summary     'List Providers'
        description <<~EOD.format_description
          Lists Providers for this project.  If --driver is specified, it only
          lists Providers which employ that driver.
        EOD
        required :d, :driver, "Restrict list to providers using DRIVER"

        run(arguments: 0 .. 1) do |opts, args, cmd|
          providers = Cult.project.providers

          # Filtering
          providers = providers.all(args[0]) if args[0]

          if opts[:driver]
            driver_cls = Cult.project.drivers[opts[:driver]]
            providers = providers.select do |p|
              p.driver.is_a?(driver_cls)
            end
          end

          providers.each do |p|
            printf "%-20s %-s\n", p.name, Cult.project.relative_path(p.path)
          end

        end
      end
      provider.add_command(provider_ls)


      provider_avail = Cri::Command.define do
        optional_project
        name       'drivers'
        summary    'list available drivers'
        description <<~EOD.format_description
          Displays a list of all available drivers, by their name, and list of
          gem dependencies.
        EOD

        run(arguments: none) do |opts, args, cmd|
          Cult::Drivers.all.each do |p|
            printf "%-20s %-s\n", p.driver_name, p.required_gems
          end
        end
      end
      provider.add_command(provider_avail)


      provider_new = Cri::Command.define do
        name        'new'
        usage       'new NAME'
        summary     'creates a new provider for your project'
        required    :d, :driver, 'Specify driver, if different than NAME'
        description <<~EOD.format_description
          Creates a new provider for the project.  There are a few ways this
          can be specified, for example

            cult provider create mikes-kvm-warehouse

          Will set up a provider account using 'mikes-kvm-warehouse' as both
          the driver type and the local provider name.

          If you need the two to be separate, for example, if you have multiple
          accounts at Mike's KVM Warehouse, you can specify a driver name with
          --driver, and an independent provider name.
        EOD

        run(arguments: 1) do |opts, args, cmd|
          name, _ = *args
          driver = CLI.fetch_item(opts[:driver] || name, from: Driver)
          name = CLI.fetch_item(name, from: Provider, exist: false)

          puts JSON.pretty_generate(driver.setup!)
          fail "FIXME"
          puts [driver, name].inspect
        end
      end
      provider.add_command(provider_new)


      provider
    end
quiet=(v) click to toggle source

Quiet mode controls how verbose `say` is

# File lib/cult/cli/common.rb, line 24
def quiet=(v)
  @quiet = v
end
quiet?() click to toggle source
# File lib/cult/cli/common.rb, line 29
def quiet?
  @quiet
end
role_cmd() click to toggle source
# File lib/cult/cli/role_cmd.rb, line 8
    def role_cmd
      role = Cri::Command.define do
        optional_project
        name        'role'
        aliases     'roles'
        summary     'Manage roles'
        description <<~EOD.format_description
          A role defines what a node does.  The easiest way to think about it
          is just a directory full of scripts (tasks).

          A role can include an arbitrary number of other roles.  For example,
          you may have two roles `rat-site' and `tempo-site', which both depend
          on a common role `web-server'.  In this case, `web-server' would be
          the set of tasks that install the web server through the package
          manager, set up a base configuration, and allow ports 80 and 443
          through the firewall.

          Both `rat-site' and `tempo-site' would declare that they depend on
          `web-server' by listing it in their `includes' array in role.json.
          Their tasks would then only consist of dropping a configuration file,
          TLS keys and certificates into `/etc/your-httpd.d`.

          Composability is the mindset behind roles.  Cult assumes, by default,
          that roles are written in a way to compose well with each other if
          they find themselves on the same node.  That is not always possible,
          (thus the `conflicts' key exists in `role.json'), but is the goal.
          You should write tasks with that in mind.  For example, dropping
          files into `/etc/your-httpd.d` instead of re-writing
          `/etc/your-httpd.conf`. With this setup, a node could include both
          `rat-site` and `tempo-site` roles and be happily serving both sites.

          By default, `cult init` generates two root roles that don't depend on
          anything else: `base` and `bootstrap`.  The `bootstrap` role exists
          to get a node from a clean OS install to a configuration to be
          managed by the settings in `base'.  Theoretically, if you're happy
          doing all deploys as the root user, you don't need a `bootstrap` role
          at all: Delete it and set the `user` key in `base/role.json` to
          "root".

          The tasks in the `base` role are considered shared amongst all roles.
          However, the only thing special about the `base` role is that Cult
          assumes roles and nodes without an explicit `includes` setting belong
          to `base`.
        EOD

        run(arguments: none) do |opts, args, cmd|
          puts cmd.help
          exit
        end
      end


      role_new = Cri::Command.define do
        name        'new'
        summary     'creates a new role'
        usage       'create [options] NAME'
        description <<~EOD.format_description
          Creates a new role names NAME, which will then be available under
          $CULT_PROJECT/roles/$NAME
        EOD

        required :r, :roles, 'this role depends on another /ROLE+/ (multiple)',
                 multiple: true

        run(arguments: 1) do |opts, args, cmd|
          name = CLI.fetch_item(args[0], from: Role, exist: false)

          role = Role.by_name(Cult.project, name)
          data = {}

          if opts[:roles]
            data[:includes] = CLI.fetch_items(opts[:roles],
                                              from: Role).map(&:name)
          end
          FileUtils.mkdir_p(role.path)
          File.write(role.role_file, JSON.pretty_generate(data))

          FileUtils.mkdir_p(File.join(role.path, "files"))
          File.write(File.join(role.path, "files", ".keep"), '')

          FileUtils.mkdir_p(File.join(role.path, "tasks"))
          File.write(File.join(role.path, "tasks", ".keep"), '')
        end
      end
      role.add_command(role_new)


      role_rm = Cri::Command.define do
        name        'rm'
        usage       'rm /ROLE+/ ...'
        summary     'Destroy role ROLE'
        description <<~EOD.format_description
          Destroys all roles specified.
        EOD

        run(arguments: 1 .. unlimited) do |opts, args, cmd|
          roles = args.map do |role_name|
            CLI.fetch_items(role_name, from: Role)
          end.flatten

          roles.each do |role|
            if CLI.yes_no?("Delete role #{role.name} (#{role.path})?",
                           default: :no)
              FileUtils.rm_rf(role.path)
            end
          end
        end
      end
      role.add_command(role_rm)


      role_ls = Cri::Command.define do
        name        'ls'
        usage       'ls [/ROLE+/ ...]'
        summary     'List existing roles'
        description <<~EOD.format_description
          Lists roles in this project.  By default, lists all roles.  If one or
          more ROLES are specified, only lists those
        EOD

        run(arguments: unlimited) do |opts, args, cmd|
          roles = Cult.project.roles
          unless args.empty?
            roles = CLI.fetch_items(*args, from: Role)
          end

          table = Terminal::Table.new(headings: ['Role', 'Build Order'])
          table.rows = roles.map do |r|
            [r.name, r.build_order.map(&:name).join(', ')]
          end
          puts table

        end
      end
      role.add_command(role_ls)

      role
    end
say(v) click to toggle source
# File lib/cult/cli/common.rb, line 34
def say(v)
  puts v unless @quiet
end
set_project(path) click to toggle source

This sets the global project based on a directory

# File lib/cult/cli/common.rb, line 15
def set_project(path)
  Cult.project = Cult::Project.locate(path)
  if Cult.project.nil?
    fail CLIError, "#{path} does not contain a valid Cult project"
  end
end
task_cmd() click to toggle source
# File lib/cult/cli/task_cmd.rb, line 4
    def task_cmd
      task = Cri::Command.define do
        optional_project
        name        'task'
        aliases     'tasks'
        summary     'Task manipulation'
        usage       'task [command]'
        description <<~EOD.format_description
          Tasks are basically shell scripts.  Or anything with a \#! line, or
          that can be executed by name.

          Each task belongs to a Role, and the collection of Tasks in a Role,
          when ran in sequence, define what the Role does.

          For example, you could have a 'database-sever' Role, which would
          include tasks with filenames like:

            000-add-postgres-apt-repo
            001-install-postgres
            002-create-roles
            003-update-hba
            004-install-tls-cert
            005-start-postgres

          All of these Tasks would be run in sequence to define what you
          consider a `database-server` should look like.  Note that a task's
          sequence is defined by a leading number, and `task resequence` will
          neatly line these up for you.
        EOD

        run(arguments: none) do |opts, args, cmd|
          puts cmd.help
          exit
        end
      end


      task_resequence = Cri::Command.define do
        name        'resequence'
        aliases     'reserial'
        summary     'Resequences task serial numbers'

        flag     :A,  :all,       'Re-sequence all roles'
        flag     :G,  :'git-add', '`git add` each change'
        required :r,  :role,      'Resequence only /NODE+/ (multiple)',
                                  multiple: true

        description <<~EOD.format_description
          Resequences the serial numbers in each task provided with --roles,
          or all roles with --all.  You cannot supply both --all and specify
          --roles.

          A resequence isn't something to do lightly once you have deployed
          nodes.  This will be elaborated on in the future.  It's probably
          a good idea to do this in a development branch and test out the
          results.

          The --git-add option will execute `git add` for each rename made.
          This will make your status contain a bunch of neat renames, instead of
          a lot of deleted and untracked files.

          This command respects the global --yes flag.
        EOD


        run(arguments: none) do |opts, args, cmd|
          if opts[:all] && Array(opts[:role]).size != 0
            fail CLIError, "can't supply -A and also a list of roles"
          end

          roles = if opts[:all]
            Cult.project.roles
          elsif opts[:role]
            CLI.fetch_items(opts[:role], from: Role)
          else
            fail CLIError, "no roles specified with --role or --all"
          end

          roles.each do |role|
            puts "Resequencing role: `#{role.name}'"
            tasks = role.build_tasks.sort_by do |task|
              # This makes sure we don't change order for duplicate serials
              [task.serial, task.name]
            end

            renames = tasks.map.with_index do |task, i|
              if task.serial != i
                new_task = Task.from_serial_and_name(role,
                                                     serial: i,
                                                     name: task.name)
                [task, new_task]
              end
            end.compact.to_h

            next if renames.empty?

            unless Cult::CLI.yes?
              renames.each do |src, dst|
                puts "rename #{Cult.project.relative_path(src.path)} " +
                     "-> #{Cult.project.relative_path(dst.path)}"
              end
            end

            if Cult::CLI.yes_no?("Execute renames?")
              renames.each do |src, dst|
                FileUtils.mv(src.path, dst.path)
                if opts[:'git-add']
                    `git add #{src.path}; git add #{dst.path}`
                end
              end
            end
          end
        end
      end
      task.add_command(task_resequence)


      task_sanity = Cri::Command.define do
        name        'sanity'
        summary     'checks task files for numbering sanity'
        description <<~EOD.format_description
          TODO: Document (and do something!)
        EOD

        run do |opts, args, cmd|
          puts 'checking sanity...'
        end
      end
      task.add_command task_sanity


      task_new = Cri::Command.define do
        name        'new'
        usage       'create [options] DESCRIPTION'
        summary     'create a new task for ROLE with a proper serial'
        description <<~EOD.format_description
        EOD

        required :r, :role, '/ROLE/ for task.  defaults to "base"'
        flag :e, :edit, 'open generated task file in your $EDITOR'

        run do |opts, args, cmd|
          english = args.join " "
          opts[:roles] ||= 'base'
          puts [english, opts[:roles], opts[:edit]].inspect
        end
      end
      task.add_command(task_new)

      task
    end
unique_id(len = 8) click to toggle source

We actually want “base 47”, so we have to generate substantially more characters than len. The method already generates 1.25*len characters, but is offset by _ and - that we discard. With the other characters we discard, we usethe minimum multiplier which makes a retry “rare” (every few thousand ids at 6 len), then handle that case.

# File lib/cult/cli/common.rb, line 135
def unique_id(len = 8)
  @uniq_id_disallowed ||= /[^abcdefhjkmnpqrtvwxyzABCDEFGHJKMNPQRTVWXY2346789]/
  candidate = SecureRandom.urlsafe_base64((len * 2.1).ceil)
                          .gsub(@uniq_id_disallowed, '')
  fail RangeError if candidate.size < len
  candidate[0...len]
rescue RangeError
  retry
end
yes=(v) click to toggle source

yes=true automatically answers yes to “yes_no” questions.

# File lib/cult/cli/common.rb, line 40
def yes=(v)
  @yes = v
end
yes?() click to toggle source
# File lib/cult/cli/common.rb, line 45
def yes?
  @yes
end
yes_no?(prompt, default: true) click to toggle source

Asks a yes or no question with promp. The prompt defaults to “Yes”. If Cli.yes=true, true is returned without showing the prompt.

# File lib/cult/cli/common.rb, line 52
def yes_no?(prompt, default: true)
  return true if yes?

  default = case default
    when :y, :yes
      true
    when :n, :no
      false
    when true, false
      default
    else
      fail ArgumentError, "invalid :default"
  end

  loop do
    y =  default ? Rainbow('Y').bright : Rainbow('y').darkgray
    n = !default ? Rainbow('N').bright : Rainbow('n').darkgray

    begin
      print "#{prompt} #{y}/#{n}: "
      case $stdin.gets.chomp
        when ''
          return default
        when /^[Yy]/
          return true
        when /^[Nn]/
          return false
        else
          $stderr.puts "Unrecognized response"
      end
    rescue Interrupt
      puts
      raise
    end
  end
end