Class: Veewee::Environment

Inherits:
Object show all
Defined in:
lib/veewee/environment.rb

Overview

Represents a single Veewee environment. A “Veewee environment” is defined as basically a folder with a “Veeweefile”. This class allows access to the VMs, CLI, etc. all in the scope of this environment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment

Returns a new instance of Environment


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/veewee/environment.rb', line 57

def initialize(options = {})
  # symbolify commandline options
  options = options.inject({}) {|result,(key,value)| result.update({key.to_sym => value})}

  # If a cwd was provided as option it overrules the default
  # cwd is again merged later with all options but it has to merged here
  # because several defaults are generated from it
  cwd = options[:cwd] || Veewee::Environment.workdir

  defaults = {
    :cwd => cwd,
    :veewee_filename => "Veeweefile",
    :template_path => ["templates"],
    :validation_dir => File.join(File.expand_path(File.join(File.dirname(__FILE__), "..", "..")), "validation"),
  }

  options = defaults.merge(options)

  @config_filepath = File.join(options[:cwd], options[:veewee_filename])

  veeweefile_config = defaults.keys.inject({}) do |memo, obj|
    if config.env.methods.include?(obj) && !config.env.send(obj).nil?
      memo.merge({ obj => config.env.send(obj) })
    else
      memo
    end
  end
  options = options.merge(veeweefile_config)

  logger.info("environment") { "Environment initialized (#{self})" }

  # Injecting all variables of the options and assign the variables
  options.each do |key, value|
    instance_variable_set("@#{key}".to_sym, options[key])
    logger.info("environment") { " - #{key} : #{options[key]}" }
  end

  # Definitions
  @definitions = Veewee::Definitions.new(self)
  @templates = Veewee::Templates.new(self)
  @providers = Veewee::Providers.new(self, options)

  # Read ostypes
  yamlfile = File.join(File.dirname(__FILE__), "config", "ostypes.yml")
  logger.info "Reading ostype yamlfile #{yamlfile}"
  @ostypes = YAML.load_file(yamlfile)

  return self
end

Instance Attribute Details

#configConfig::Top

The configuration object represented by this environment. This will trigger the environment to load if it hasn't loaded yet (see #load!).

Returns:

  • (Config::Top)

38
39
40
# File 'lib/veewee/environment.rb', line 38

def config
  @config
end

#config_filepathObject (readonly)

Path to the config file


53
54
55
# File 'lib/veewee/environment.rb', line 53

def config_filepath
  @config_filepath
end

#current_providerObject

Returns the value of attribute current_provider


55
56
57
# File 'lib/veewee/environment.rb', line 55

def current_provider
  @current_provider
end

#cwdObject

The `cwd` that this environment represents


16
17
18
# File 'lib/veewee/environment.rb', line 16

def cwd
  @cwd
end

#definition_dirObject


107
108
109
# File 'lib/veewee/environment.rb', line 107

def definition_dir
  @definition_dir ||= File.join(cwd, "definitions")
end

#definitionsObject

Hash element of all definitions available


41
42
43
# File 'lib/veewee/environment.rb', line 41

def definitions
  @definitions
end

#iso_dirObject


110
111
112
# File 'lib/veewee/environment.rb', line 110

def iso_dir
  @iso_dir ||= File.join(cwd, "iso")
end

#ostypesObject (readonly)

Hash element of all OS types


50
51
52
# File 'lib/veewee/environment.rb', line 50

def ostypes
  @ostypes
end

#providersObject

Hash element of all providers available


47
48
49
# File 'lib/veewee/environment.rb', line 47

def providers
  @providers
end

#template_pathObject

This initializes a new Veewee Environment settings argument is a hash with the following options

  • :definition_dir : where definitions are located

  • :template_path : paths that contains the template definitions that come with the veewee gem, defaults to the path relative to the gemfiles

  • :iso_dir : directory to look for iso files, defaults to $environment_dir/iso

  • :validation_dir : directory that contains a list of validation tests, that can be run after building a box

  • :tmp_dir : directory that will be used for creating temporary files, needs to be rewritable, default to $environment_dir/tmp


28
29
30
# File 'lib/veewee/environment.rb', line 28

def template_path
  @template_path
end

#templatesObject

Hash element of all templates available


44
45
46
# File 'lib/veewee/environment.rb', line 44

def templates
  @templates
end

#tmp_dirObject


113
114
115
# File 'lib/veewee/environment.rb', line 113

def tmp_dir
  tmp_dir ||= File.join(cwd, "tmp")
end

#uiUI

Returns the UI for the environment, which is responsible for talking with the outside world.

Returns:


139
140
141
# File 'lib/veewee/environment.rb', line 139

def ui
  @ui ||= UI.new(self)
end

#validation_dirObject

Returns the value of attribute validation_dir


31
32
33
# File 'lib/veewee/environment.rb', line 31

def validation_dir
  @validation_dir
end

#veewee_filenameObject

The valid name for a Veeweefile for this environment


19
20
21
# File 'lib/veewee/environment.rb', line 19

def veewee_filename
  @veewee_filename
end

Class Method Details

.workdirObject


117
118
119
# File 'lib/veewee/environment.rb', line 117

def self.workdir
  ENV['VEEWEE_DIR'] || Dir.pwd
end

Instance Method Details

#cli(*args) ⇒ Object

Makes a call to the CLI with the given arguments as if they came from the real command line (sometimes they do!). An example:

env.cli("package", "--veeweefile", "Veeweefie")

186
187
188
# File 'lib/veewee/environment.rb', line 186

def cli(*args)
  CLI.start(args.flatten, :env => self)
end

#get_box(name) ⇒ Object

Get box from current provider


221
222
223
224
225
226
227
# File 'lib/veewee/environment.rb', line 221

def get_box(name)
  if current_provider.nil?
    raise "Provider is unset in the environment."
  else
    providers[current_provider].get_box(name)
  end
end

#load!Object

Loads this entire environment, setting up the instance variables such as `vm`, `config`, etc. on this environment. The order this method calls its other methods is very particular.


158
159
160
161
162
163
164
165
166
167
# File 'lib/veewee/environment.rb', line 158

def load!
  if !loaded?
    @loaded = true

    logger.info("environment") { "Loading configuration..." }
    load_config!

    self
  end
end

#load_config!Object


169
170
171
172
# File 'lib/veewee/environment.rb', line 169

def load_config!
  @config = Config.new({ :env => self }).load_veewee_config()
  return self
end

#loaded?Bool

Returns a boolean representing if the environment has been loaded or not.

Returns:

  • (Bool)

151
152
153
# File 'lib/veewee/environment.rb', line 151

def loaded?
  !!@loaded
end

#loggerLogger

Accesses the logger for Veewee. This logger is a detailed logger which should be used to log internals only. For outward facing information, use #ui.

Returns:

  • (Logger)

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/veewee/environment.rb', line 199

def logger
  return @logger if @logger

  output = nil
  loglevel = Logger::ERROR

  # Figure out where the output should go to.
  if ENV["VEEWEE_LOG"]
    output = STDOUT
    loglevel = Logger.const_get(ENV["VEEWEE_LOG"].upcase)
  end

    # Create the logger and custom formatter
  @logger = ::Logger.new(output)
  @logger.level = loglevel
  @logger.formatter = Proc.new do |severity, datetime, progname, msg|
    "#{datetime} - #{progname} - [#{resource}] #{msg}\n"
  end
  @logger
end

#reload_config!Object

Reloads the configuration of this environment.


175
176
177
178
179
# File 'lib/veewee/environment.rb', line 175

def reload_config!
  @config = nil
  load_config!
  self
end

#resourceObject


190
191
192
# File 'lib/veewee/environment.rb', line 190

def resource
  "veewee"
end