Class: Simp::Cli::Config::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/simp/cli/config/item.rb

Defined Under Namespace

Classes: AddLdapToHiera, AnswersYAMLFileWriter, Certificates, ClientNets, CommonRunLevelDefault, DHCP, DNSSearch, DNSServers, FailoverLogServers, Gateway, GrubPassword, Hostname, HostnameConf, IPAddress, IsMasterYumServer, LdapBaseDn, LdapBindDn, LdapBindHash, LdapBindPw, LdapMaster, LdapRootDn, LdapRootHash, LdapSyncDn, LdapSyncHash, LdapSyncPw, LdapUri, LogServers, NTPServers, Netmask, NetworkConf, NetworkInterface, PuppetAutosign, PuppetCA, PuppetCAPort, PuppetConf, PuppetDBPort, PuppetDBServer, PuppetFileServer, PuppetHostsEntry, PuppetServer, PuppetServerIP, RemoveLdapFromHiera, RenameFqdnYaml, RsyncBase, RsyncServer, RsyncTimeout, SetGrubPassword, SetupNIC, SimpYumServers, SssdDomains, UseAuditd, UseFips, UseIPtables, UseLdap, UseSELinux, YumRepositories

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, description = nil) ⇒ Item

Returns a new instance of Item.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simp/cli/config/item.rb', line 18

def initialize(key = nil, description = nil)
  @key               = key         # answers file key for the config Item
  @description       = description # A text description of the Item
  @value             = nil         # value (decided by user)
  @fact              = nil         # Facter fact to query OS value

  @skip_query        = false       # skip the query and use the default_value
  @skip_apply        = false       # skip the apply
  @skip_yaml         = false       # skip yaml output
  @silent            = false       # no output to stdout/Highline
  @die_on_apply_fail = false       # halt simp config if apply fails
  @allow_user_apply  = false       # allow non-superuser to apply
  @fail_on_missing_answer = false  # error out if @value is not pre-populated

  @config_items      = {}          # a hash of all previous Config::Items
  # a Hash of additional Items that this Item may need to add to the Queue
  # the keys of the Has are used to look up the queue
  # format:
  #   'answer1' => [ Item1, Item2, .. ]
  #   'answer2' => [ Item3, Item4, .. ]
  @next_items_tree   = {}
end

Instance Attribute Details

#allow_user_applyObject

Returns the value of attribute allow_user_apply.



13
14
15
# File 'lib/simp/cli/config/item.rb', line 13

def allow_user_apply
  @allow_user_apply
end

#config_itemsObject

Returns the value of attribute config_items.



14
15
16
# File 'lib/simp/cli/config/item.rb', line 14

def config_items
  @config_items
end

#descriptionObject

Returns the value of attribute description.



11
12
13
# File 'lib/simp/cli/config/item.rb', line 11

def description
  @description
end

#die_on_apply_failObject

Returns the value of attribute die_on_apply_fail.



13
14
15
# File 'lib/simp/cli/config/item.rb', line 13

def die_on_apply_fail
  @die_on_apply_fail
end

#factObject

Returns the value of attribute fact.



11
12
13
# File 'lib/simp/cli/config/item.rb', line 11

def fact
  @fact
end

#fail_on_missing_answerObject

Returns the value of attribute fail_on_missing_answer.



16
17
18
# File 'lib/simp/cli/config/item.rb', line 16

def fail_on_missing_answer
  @fail_on_missing_answer
end

#keyObject

Returns the value of attribute key.



11
12
13
# File 'lib/simp/cli/config/item.rb', line 11

def key
  @key
end

#next_items_treeObject

Returns the value of attribute next_items_tree.



15
16
17
# File 'lib/simp/cli/config/item.rb', line 15

def next_items_tree
  @next_items_tree
end

#silentObject

Returns the value of attribute silent.



12
13
14
# File 'lib/simp/cli/config/item.rb', line 12

def silent
  @silent
end

#skip_applyObject

Returns the value of attribute skip_apply.



12
13
14
# File 'lib/simp/cli/config/item.rb', line 12

def skip_apply
  @skip_apply
end

#skip_queryObject

Returns the value of attribute skip_query.



12
13
14
# File 'lib/simp/cli/config/item.rb', line 12

def skip_query
  @skip_query
end

#skip_yamlObject

Returns the value of attribute skip_yaml.



12
13
14
# File 'lib/simp/cli/config/item.rb', line 12

def skip_yaml
  @skip_yaml
end

#valueObject

Returns the value of attribute value.



11
12
13
# File 'lib/simp/cli/config/item.rb', line 11

def value
  @value
end

Instance Method Details

#applyObject



216
# File 'lib/simp/cli/config/item.rb', line 216

def apply; nil; end

#default_valueObject

returns the default answer to Item#query



163
164
165
# File 'lib/simp/cli/config/item.rb', line 163

def default_value
  @value || recommended_value
end

#highline_question_typeObject

A helper method that highline can use to cast String answers to the ask in query(). nil means don’t cast, Date casts into a date, etc. A lambda can be used for sanitization.

Descendants of Item are very likely to override this method.



194
# File 'lib/simp/cli/config/item.rb', line 194

def highline_question_type; nil; end

#next_itemsObject



180
181
182
# File 'lib/simp/cli/config/item.rb', line 180

def next_items
  @next_items_tree.fetch( @value, [] )
end

#not_valid_messageObject

optional message to show users when invalid input is entered



186
# File 'lib/simp/cli/config/item.rb', line 186

def not_valid_message; nil; end

#os_valueObject

value of item as read from OS (via Facter)



45
46
47
# File 'lib/simp/cli/config/item.rb', line 45

def os_value
  Facter.value( @fact ) unless @fact.nil?
end

Pretty stdout/stdin methods

print a pretty banner to describe an item



86
87
88
89
90
91
92
93
94
# File 'lib/simp/cli/config/item.rb', line 86

def print_banner
  return if @silent
  say_blue "=== #{@key} ===", ['BOLD']
  say_blue description
  say_blue "    - os value:          #{os_value}"          if os_value
  say_blue "    - os value:          #{puppet_value}"      if puppet_value
  say_blue "    - recommended value: #{recommended_value}" if recommended_value
  say_blue "    - chosen value:      #{@value}"            if @value
end

print a pretty summary of the Item’s key+value, printed to stdout



98
99
100
101
102
# File 'lib/simp/cli/config/item.rb', line 98

def print_summary
  return if @silent
  fail '@key is empty' if "#{@key}".empty?
  say( "#{@key} = '<%= color( %q{#{value}}, BOLD )%>'\n" )
end

#puppet_valueObject

value of Item as read from puppet (via Hiera) #TODO: not used yet



51
# File 'lib/simp/cli/config/item.rb', line 51

def puppet_value; nil; end

#queryObject

choose @value of Item



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/simp/cli/config/item.rb', line 106

def query
  extra = query_status

  if @value.nil? && @fail_on_missing_answer
    say( "<%= color(%q(FATAL: no answer for '), RED) %>" +
         "<%= color(%q{#{extra}#{@key}}, BOLD,  RED)%>" +
         "<%= color(%q('; exiting!), RED)%>\n" )
    exit 1
  end

  if !@skip_query && @value.nil?
    print_banner
    @value = query_ask
  end

  # summarize the item's status after the query is complete
  say( "#{extra}#{@key} = '<%= color( %q{#{@value}}, BOLD )%>'\n" ) unless @silent
end

#query_askObject

ask an interactive question (via stdout/stdin)



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/simp/cli/config/item.rb', line 139

def query_ask
  # NOTE: This trailing space at the end of the String obliquely instructs
  # Highline to keep the prompt on the same line as the question.  If the
  # String did not end with a space or tab, Highline would move the input
  # prompt to the next line (which, for our purposes, looks confusing)
  value = ask( "<%= color('#{@key}', WHITE, BOLD) %>: ",
              highline_question_type ) do |q|
    q.default = default_value unless default_value.to_s.empty?

    # validate input via the validate() method
    q.validate = lambda{ |x| validate( x )}

    # if the answer is not valid, construct a reply:
    q.responses[:not_valid] =  "<%= color( %q{Invalid answer!}, RED ) %>\n"
    q.responses[:not_valid] += "<%= color( %q{#{ (not_valid_message || description) }}, YELLOW) %>\n"
    q.responses[:not_valid] += "#{q.question}  |#{q.default}|"

    query_extras q
  end
  value
end

#query_extras(q) ⇒ Object



168
# File 'lib/simp/cli/config/item.rb', line 168

def query_extras( q ); q; end

#query_statusObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/simp/cli/config/item.rb', line 126

def query_status
  extra = ''
  if !@value.nil?
    extra = "<%= color( %q{(answered)}, CYAN, BOLD)%> "
  elsif @skip_query
    extra = "<%= color( %q{(noninteractive)}, CYAN, BOLD)%> "
    @value = default_value
  end
  extra
end

value of Item as recommended by Very Clever Logic (tm)



55
# File 'lib/simp/cli/config/item.rb', line 55

def recommended_value; nil; end

#safe_applyObject



215
# File 'lib/simp/cli/config/item.rb', line 215

def safe_apply; nil; end

#say_blue(msg, options = []) ⇒ Object

convenience_method to print formatted information



197
198
199
200
# File 'lib/simp/cli/config/item.rb', line 197

def say_blue( msg, options=[] )
  options = options.unshift( '' ) unless options.empty?
  say "<%= color(%q{#{msg}}, CYAN #{options.join(', ')}) %>\n" unless @silent
end

#say_green(msg, options = []) ⇒ Object



209
210
211
212
# File 'lib/simp/cli/config/item.rb', line 209

def say_green( msg, options=[] )
  options = options.unshift( '' ) unless options.empty?
  say "<%= color(%q{#{msg}}, GREEN #{options.join(', ')}) %>\n" unless @silent
end

#say_red(msg, options = []) ⇒ Object



205
206
207
208
# File 'lib/simp/cli/config/item.rb', line 205

def say_red( msg, options=[] )
  options = options.unshift( '' ) unless options.empty?
  say "<%= color(%q{#{msg}}, RED #{options.join(', ')}) %>\n" unless @silent
end

#say_yellow(msg, options = []) ⇒ Object



201
202
203
204
# File 'lib/simp/cli/config/item.rb', line 201

def say_yellow( msg, options=[] )
  options = options.unshift( '' ) unless options.empty?
  say "<%= color(%q{#{msg}}, YELLOW #{options.join(', ')}) %>\n" unless @silent
end

#to_yaml_sObject

String in yaml answer file format, with comments (if any)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/simp/cli/config/item.rb', line 60

def to_yaml_s
  fail '@key is empty' if "#{@key}".empty?

  x =  "=== #{@key} ===\n"
  x += "#{(description || 'FIXME: NO DESCRIPTION GIVEN')}\n"

  # comment every line that describes the item:
  x =  x.each_line.map{ |y| "# #{y}" }.join

  # add yaml (but stripped of frontmatter and first indent)
  # TODO: should we be using SafeYAML?  http://danieltao.com/safe_yaml/
  x += { @key => @value }.to_yaml.gsub(/^---\s*\n/m, '').gsub(/^  /, '' )
  x += "\n"

  if @skip_yaml
    x.gsub( /^/, '### ' )
  else
    x
  end
end

#validate(_x) ⇒ Object

returns true if x is a valid value



172
173
174
175
176
177
# File 'lib/simp/cli/config/item.rb', line 172

def validate( _x )
  msg =  'ERROR: Item.validate() not implemented!'
  msg += "\nTODO: cover common type-based validations?"
  msg += "\nTODO: Offer validation objects?"
  fail msg
end