Class: AptControl::CLI::Root

Inherits:
Object
  • Object
show all
Extended by:
AptControl::ConfigDSL
Defined in:
lib/apt_control/cli.rb

Defined Under Namespace

Classes: FSListenerFactory

Constant Summary collapse

DEFAULT_CONFIG_FILE_LOCATION =
'/etc/apt_control/config.yaml'

Instance Method Summary collapse

Methods included from AptControl::ConfigDSL

configs

Instance Method Details

#apt_siteObject



161
162
163
# File 'lib/apt_control/cli.rb', line 161

def apt_site
  @apt_site ||= AptSite.new(config[:apt_site_dir], logger)
end

#build_archiveObject



169
170
171
# File 'lib/apt_control/cli.rb', line 169

def build_archive
  @build_archive ||= BuildArchive.new(config[:build_archive_dir], logger)
end

#build_configObject

Read yaml file if one exists, then apply overrides from the command line



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/apt_control/cli.rb', line 113

def build_config
  file = [options[:config_file], DEFAULT_CONFIG_FILE_LOCATION].
    compact.find {|f| File.exists?(f) }

  hash =
    if file
      YAML.load_file(file).each do |key, value|
      stderr.puts("Warn: Unknown key in config file: #{key}") unless
        self.class.configs.find {|opt| opt.first.to_s == key.to_s }
    end
    else
      {}
    end

  options[:config_option].map {|str| str.split('=') }.
    inject(hash) {|m, (k,v)| m.merge(k.to_sym => v) }
end

#configObject



106
107
108
# File 'lib/apt_control/cli.rb', line 106

def config
  @config ||= build_config
end

#control_fileObject



165
166
167
# File 'lib/apt_control/cli.rb', line 165

def control_file
  @control_file ||= ControlFile.new(config[:control_file], logger)
end

#fs_listener_factoryObject



212
213
214
215
# File 'lib/apt_control/cli.rb', line 212

def fs_listener_factory
  @fs_listener_factory ||= FSListenerFactory.new(
    disable_inotify: config[:disable_inotify].to_s == 'true')
end

#jabberObject



173
174
175
176
177
# File 'lib/apt_control/cli.rb', line 173

def jabber
  @jabber ||= Jabber.new(:jid => config[:jabber_id], :logger => logger,
    :password => config[:jabber_password], :room_jid => config[:jabber_chatroom_id],
    :enabled => jabber_enabled?)
end

#jabber_enabled?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/apt_control/cli.rb', line 179

def jabber_enabled?
  config[:jabber_enabled].to_s == 'true'
end

#loggerObject



148
149
150
151
152
153
# File 'lib/apt_control/cli.rb', line 148

def logger
  log_file = config[:log_file] || '/dev/null'
  @logger ||= Logger.new(log_file == 'STDOUT' ? STDOUT : log_file).tap do |logger|
    logger.level = Logger::DEBUG
  end
end

#new_include_cmd(options = {}) ⇒ Object



183
184
185
186
187
188
# File 'lib/apt_control/cli.rb', line 183

def new_include_cmd(options={})
  defaults = {apt_site: apt_site, build_archive: build_archive}
  options = options.merge(defaults)

  AptControl::Commands::Include.new(options)
end

#notify(message) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/apt_control/cli.rb', line 217

def notify(message)
  logger.info("notify: #{message}")
  begin
    jabber.actor.async.send_message(message)
  rescue => e
    logger.error("Unable to send notification to jabber: #{e}")
    logger.error(e)
  end
end

#package_statesObject



155
156
157
158
159
# File 'lib/apt_control/cli.rb', line 155

def package_states
  @package_states ||= PackageStates.new(apt_site: apt_site,
    build_archive: build_archive,
    control_file: control_file)
end

#validate_config!Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/apt_control/cli.rb', line 131

def validate_config!
  self.class.configs.each do |key, desc, options|
    if options[:required]
      config[key] or raise Climate::ExitException, "Error: No config supplied for #{key}"
    end
  end

  if config[:jabber_enabled]
    self.class.configs.each do |key, desc, options|
      next unless key.to_s['jabber_']
      config[key] or raise Climate::ExitException, "Error: you must supply all jabber options if jabber is enabled"
    end
  end

  Celluloid.logger = logger
end