Module: Milc::Base

Includes:
Dsl::Ansible, Dsl::Gcloud, Dsl::Mgcloud
Defined in:
lib/milc/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Ansible

#ansible_playbook

Methods included from Dsl::Mgcloud

#mgcloud

Methods included from Dsl::Gcloud

#gcloud, #json_gcloud

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



43
44
45
# File 'lib/milc/base.rb', line 43

def config
  @config
end

#projectObject (readonly)

Returns the value of attribute project.



44
45
46
# File 'lib/milc/base.rb', line 44

def project
  @project
end

Instance Method Details

#command_optionsObject



67
68
69
# File 'lib/milc/base.rb', line 67

def command_options
  "nVc:" # n と V と c: は必須
end

#dry_runObject



46
47
48
# File 'lib/milc/base.rb', line 46

def dry_run
  Milc.dry_run
end

#execute(cmd) ⇒ Object



33
34
35
36
# File 'lib/milc/base.rb', line 33

def execute(cmd)
  res = LoggerPipe.run(logger, cmd, dry_run: Milc.dry_run)
  block_given? ? yield(res) : res
end

#help_messageObject



60
61
62
63
64
65
# File 'lib/milc/base.rb', line 60

def help_message
  ## スクリプト名
  cmdname = File.basename($0) # $PROGRAM_NAME を推奨
  ## シェルスクリプトのUsage
  "Usage: #{cmdname} -c CONF_FILE"
end

#load_configObject



50
51
52
# File 'lib/milc/base.rb', line 50

def load_config
  @project = config['PROJECT'] || ENV['PROJECT']
end

#load_from_yaml(yaml_path) ⇒ Object



38
39
40
41
# File 'lib/milc/base.rb', line 38

def load_from_yaml(yaml_path)
  @config = YAML.load_file_with_erb(yaml_path)
  load_config
end

#load_options(options) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/milc/base.rb', line 71

def load_options(options)
  if options["c"]
    load_from_yaml(options["c"])
  else
    show_help_and_exit1
  end
end

#loggerObject



29
30
31
# File 'lib/milc/base.rb', line 29

def logger
  Milc.logger
end

#run(args) ⇒ Object



93
94
95
96
97
# File 'lib/milc/base.rb', line 93

def run(args)
  setup(args)
  process
  # exit 0
end

#setup(args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/milc/base.rb', line 79

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"]

  load_options(options)
end

#show_help_and_exit1Object



54
55
56
57
58
# File 'lib/milc/base.rb', line 54

def show_help_and_exit1
  ## シェルスクリプトのUsage
  $stderr.puts help_message
  exit 1
end