Class: Mccloud::Environment

Inherits:
Object
  • Object
show all
Includes:
EnvironmentCommand
Defined in:
lib/mccloud/environment.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EnvironmentCommand

#boot, #bootstrap, #halt, #rsync

Constructor Details

#initialize(newoptions = nil) ⇒ Environment

Returns a new instance of Environment.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mccloud/environment.rb', line 39

def initialize(newoptions=nil)
  options = {
    :cwd => nil,
    :mccloud_file => "Mccloudfile",
    :autoload => true}.merge(newoptions || {})

    # We need to set this variable before the first call to the logger object
    if options.has_key?("debug")
      ENV['MCCLOUD_LOG']="STDOUT"
      ui.info "Debugging enabled"
    end

    options.each do |key, value|
      logger.info("environment") { "Setting @#{key} to #{options[key]}" }
      self.instance_variable_set("@#{key}".to_sym, options[key])
    end

    if options[:cwd].nil?
      @cwd=computed_rootpath(".")
    else
      @cwd=computed_rootpath(options[:cwd])
    end

    logger.info("environment") { "Autoload activated? #{options[:autoload]}"}
    @autoload=options[:autoload]

    # Set the default working directory to look for the Mccloudfile
    logger.info("environment") { "Environment initialized (#{self})" }
    logger.info("environment") { " - cwd : #{cwd}" }

    @config=Config.new({:env => self})
    @generator=Generator.new(self)

    # Setting the default to path of the sshkey
    @ssh_key_path=File.join(ENV['HOME'],".ssh")

    return self
end

Instance Attribute Details

#autoloadObject

Returns the value of attribute autoload.



35
36
37
# File 'lib/mccloud/environment.rb', line 35

def autoload
  @autoload
end

#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)


27
28
29
# File 'lib/mccloud/environment.rb', line 27

def config
  @config
end

#cwdObject (readonly)

The ‘cwd` that this environment represents



18
19
20
# File 'lib/mccloud/environment.rb', line 18

def cwd
  @cwd
end

#generatorObject

A generator helper to create a Mccloud project



30
31
32
# File 'lib/mccloud/environment.rb', line 30

def generator
  @generator
end

#mccloud_fileObject (readonly)

The valid name for a Mccloudfile for this environment



21
22
23
# File 'lib/mccloud/environment.rb', line 21

def mccloud_file
  @mccloud_file
end

#ssh_key_pathObject

Default ssh keypath



33
34
35
# File 'lib/mccloud/environment.rb', line 33

def ssh_key_path
  @ssh_key_path
end

#uiUI

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

Returns:



123
124
125
# File 'lib/mccloud/environment.rb', line 123

def ui
  @ui ||=  UI.new(self)
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", "--mccloudfile", "Mccloudfile")


184
185
186
# File 'lib/mccloud/environment.rb', line 184

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

#computed_rootpath(start) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mccloud/environment.rb', line 82

def computed_rootpath(start)
  # Let's start at the start path provided
  logger.info("Calculating computed rootpath")
  logger.info("Start provided: #{start}")
  startdir=start
  prevdir="/someunknownpath"

  until File.exists?(File.join(startdir,@mccloud_file))
    prevdir=startdir
    startdir=File.expand_path(File.join(startdir,".."))
    logger.info("No #{@mccloud_file} found, going up one directory #{startdir}")

    # Check if aren't at the root dir
    if File.expand_path(prevdir)==File.expand_path(startdir)
      return start
    end
  end

  return startdir
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.



142
143
144
145
146
147
148
149
150
151
# File 'lib/mccloud/environment.rb', line 142

def load!
  if !loaded?
    @loaded = true

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

    self
  end
end

#load_config!self

Does the actual read of the configuration file

Returns:

  • (self)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mccloud/environment.rb', line 155

def load_config!

  # Read the config
  @config.load_mccloud_config()

  # Read the templates in the template sub-dir
  @config.templates.load!
  @config.definitions.load!

  # Read the vms specified inthe vm sub-dir
  @config.vms.load!

  ui.info "Loaded providers[#{@config.providers.length}]"+" vms[#{@config.vms.length}]"+" ips[#{@config.ips.length}]"+" lbs[#{@config.lbs.length}]"+" stacks[#{@config.stacks.length}]"+" templates[#{@config.templates.length}] keypairs[#{@config.keypairs.length}] keystores[#{@config.keystores.length}]"

  return self
end

#loaded?Bool

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

Returns:

  • (Bool)


135
136
137
# File 'lib/mccloud/environment.rb', line 135

def loaded?
  !!@loaded
end

#loggerLogger

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

Returns:

  • (Logger)


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

def logger
  return @logger if @logger

  # Figure out where the output should go to.
  output = nil
  if ENV["MCCLOUD_LOG"] == "STDOUT"
    output = STDOUT
  elsif ENV["MCCLOUD_LOG"] == "NULL"
    output = nil
  elsif ENV["MCCLOUD_LOG"]
    output = ENV["MCCLOUD_LOG"]
  else
    output = nil #log_path.join("#{Time.now.to_i}.log")
  end

  # Create the logger and custom formatter
  @logger = ::Logger.new(output)
  @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.



173
174
175
176
177
# File 'lib/mccloud/environment.rb', line 173

def reload_config!
  @config = nil
  load_config!
  self
end

#resourceObject



188
189
190
# File 'lib/mccloud/environment.rb', line 188

def resource
  "mccloud"
end

#root_pathObject



78
79
80
# File 'lib/mccloud/environment.rb', line 78

def root_path
  return File.expand_path(@cwd)
end