# coding: utf-8 require "milc" require 'json' require 'erb' require 'yaml' require 'optparse' require 'shellwords' require 'logger_pipe' def YAML.load_file_with_erb(yaml_path) # config file example # https://github.com/tengine/magellan-infra/blob/master/ansible/magellan-production_nebula-001/files/conf/nebula-staging/common.conf erb = ERB.new(IO.read(yaml_path)) erb.filename = yaml_path text = erb.result YAML.load(text) end module Milc module Base include Milc::Dsl::Gcloud include Milc::Dsl::Mgcloud include Milc::Dsl::Ansible def logger Milc.logger end def execute(cmd) res = LoggerPipe.run(logger, cmd, dry_run: Milc.dry_run) block_given? ? yield(res) : res end def load_from_yaml(yaml_path) @config = YAML.load_file_with_erb(yaml_path) load_config end attr_reader :config attr_reader :project, :nebula attr_reader :network_base, :region, :new_relic_license_key def dry_run Milc.dry_run end def load_config @project = config['PROJECT'] || ENV['PROJECT'] @nebula = config['NEBULA'] || ENV['NEBULA'] @network_base = config['NETWORK_BASE'] || ENV['NETWORK_BASE'] @region =config['REGION'] || ENV['REGION'] @new_relic_license_key = config['NEW_RELIC_LICENSE_KEY'] || ENV['NEW_RELIC_LICENSE_KEY'] end def show_help_and_exit1 ## シェルスクリプトのUsage $stderr.puts help_message exit 1 end def help_message ## スクリプト名 cmdname = File.basename($0) # $PROGRAM_NAME を推奨 ## シェルスクリプトのUsage "Usage: #{cmdname} -c CONF_FILE" end def command_options "nVc:" # n と V と c: は必須 end def load_options(options) end def setup(args) # ARGV.getopts については以下を参照 # http://d.hatena.ne.jp/zariganitosh/20140819/ruby_optparser_true_power # http://docs.ruby-lang.org/ja/2.1.0/method/OptionParser=3a=3aArguable/i/getopts.html args.extend(OptionParser::Arguable) unless args.is_a?(OptionParser::Arguable) options = args.getopts(command_options) show_help_and_exit1 unless args.empty? Milc.dry_run = !!options["n"] Milc.verbose = !!options["V"] show_help_and_exit1 unless options["c"] load_from_yaml(options["c"]) load_options(options) end def run(args) setup(args) process exit 0 end end end