Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

WIP #65

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
vendor
90 changes: 90 additions & 0 deletions lib/omnibus-ctl/command/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#
# Base infrastructure for a subcommand
#

#
# An expensive require can slow the whole system down; minimize what goes here at all costs
#
require 'mixlib/cli'
require 'omnibus/mixin/recommender'
require 'omnibus/mixin/interactive'
require 'omnibus/mixin/run_command'
require 'omnibus/mixin/runit'
require 'omnibus/mixin/paths'
require 'omnibus/config'

module Omnibus
module Command
class Base
#
# Register declares
#
class << self; attr_accessor :available_commands end
available_commands = []
def self.register_command(class_name, command_name, command_groups = [])
available_commands += {name: command_name, groups: command_groups}
end

#
# Load is called by the ctl before #run is called
#
def load(basic_config = {})

# We put the requires here rather than in deps since we want
# subclasses to define deps without needing to call super.
require 'fileutils'
require 'tempfile'
require 'highline'
require 'omnibus'
require 'omnibus/log'
log_level = if basic_config[:verbose]
:debug
else
:info
end
Omnibus::Log.level = log_level
HighLine.use_color = $stdout.tty?
Omnibus.load
deps
end

#
# Called before #run
# Subclasses will override this with their own requires.
#
def deps

end

def help
puts opt_parser.to_s
if extended_help.length > 0
puts "\n#{extended_help}"
end
end
alias :show_help :help

#
# Returns a String that is appended to the end of the help
# output for the command, allowing commands to provide more help
# text than a simple list of options.
#
def extended_help
""
end

def log(msg)
$stdout.puts msg unless config[:quiet]
end

def warn(msg)
$stderr.puts HighLine.color(msg, :yellow)
end

def err(msg)
$stderr.puts HighLine.color(msg, :red)
end

end
end
end # module Omnibus
55 changes: 55 additions & 0 deletions lib/omnibus-ctl/command/runit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'omnibus/command/base'

module Omnibus
module Command
#
# RunitBase is the base class for all runit-based commands. Since
# all of the runit based commands simply run a different sv
# command, we define them all in this file.
#
class RunitBase < Base
include Omnibus::Mixin::ServiceUtils

def self.include_runitopts
include_stdopts
end

def sv_command
raise NotImplemented
end

def run(args)
parse_options(args)
services = cli_arguments
if services_exist?(services)
run_sv_command(sv_command, services)
else
recommend_services(services)
1
end
end
end

#
# Runit supports more commands that these, but these
# are the commands supported by our -ctl style commands in
# other products
#
RUNIT_COMMANDS = %w{start stop restart once hup
term int kill usr1 usr2}

RUNIT_COMMANDS.each do |cmd_name|
class_name = cmd_name.delete("-").capitalize
class_eval(<<-CLASS)
class #{class_name} < RunitBase
register_command cmd_name, [:runit]
banner "Usage: #{Omnibus.config.cmd_name} #{cmd_name} [SERVICE..] (options)"
include_runitopts
def sv_command
"#{cmd_name}"
end
end
CLASS
end
end
end
32 changes: 32 additions & 0 deletions lib/omnibus-ctl/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
#
#


# This taken from https://6ftdan.com/allyourdev/2015/03/07/configuration-with-a-singleton-instance/
# Revisit whether openstruct is the right choice, or mixlib config
require 'ostruct'
class Omnibus::OmnibusConfig < OpenStruct
method_missing(:cmd_name) || "omnibus-ctl"
method_missing(:base_path) || "/opt/omnibus"
end

module Omnibus
module Config
class << self
def config
@config ||= OmnibusConfig.new
end
end
end
end

Omnibus.Config.config

# ALL_SERVICES = %w{leaderl epmd etcd postgresql elasticsearch}
# ALL_SYSPARAMS = %w{disks}
# BASE_PATH = "/opt/opscode"
# CONFIG_PATH = "/etc/opscode/chef-server.rb"
# SERVICE_PATH = "#{BASE_PATH}/service"
# SV_PATH = "#{BASE_PATH}/sv"
# SV_INIT_PATH = "#{BASE_PATH}/init"
Loading