Class: Migrant::Environment

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

Overview

Holds all the state for this run of Migrant

Constant Summary collapse

DEFAULT_DATA_PATH =
'data'
DEFAULT_BOXES_PATH =
File.join(DEFAULT_DATA_PATH,'migrant_boxes.yml')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment_name, config = nil) ⇒ Environment

Returns a new instance of Environment.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/migrant/environment.rb', line 11

def initialize(environment_name,config=nil)
  @ui = Migrant::UI.new(::Thor::Base.shell.new)
  @environment_name = environment_name
  if config
    @config = config
  else
    Kernel.load 'Migrantfile'
    @config = Configuration.for('migrant')
  end
  if @environment_name.nil?
    @ui.notice "Using default environment"
  elsif @config.include?(@environment_name.to_sym)
    @ui.notice "Using #{@environment_name} environment"
  else
    @ui.error "#{@environment_name} environment is not defined in the configuration file"
    raise "Environment #{@environment_name} not found"
  end

  cloud_class = Migrant::Clouds::Base.registered(setting('provider.name'))
  raise "Cannot find cloud '#{setting('provider.name')}'" if cloud_class.nil?
  @cloud = cloud_class.new(self)
  @bootstrapper = Migrant::Bootstrappers::Base.default.new(self)
  @provisioner = Migrant::Provisioners::Base.registered(:chef_solo).new(self)
  FileUtils.mkdir_p DEFAULT_DATA_PATH unless File.exists?(DEFAULT_DATA_PATH)
  @boxes = Boxes.new(DEFAULT_BOXES_PATH).load
end

Instance Attribute Details

#cloudObject (readonly)

Returns the value of attribute cloud.



126
127
128
# File 'lib/migrant/environment.rb', line 126

def cloud
  @cloud
end

#serverObject

Returns the value of attribute server.



124
125
126
# File 'lib/migrant/environment.rb', line 124

def server
  @server
end

#uiObject (readonly)

Returns the value of attribute ui.



125
126
127
# File 'lib/migrant/environment.rb', line 125

def ui
  @ui
end

Instance Method Details

#bootstrapObject



101
102
103
# File 'lib/migrant/environment.rb', line 101

def bootstrap
  @bootstrapper.run unless @bootstrapper.bootstrapped?
end

#connectObject



66
67
68
69
# File 'lib/migrant/environment.rb', line 66

def connect
  ui.warn "Connecting to #{setting('provider.name')}"
  @cloud.connect
end

#destroyObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/migrant/environment.rb', line 111

def destroy
  box = @boxes[@environment_name]

  if box.nil?
    @ui.error "There are currently no boxes running"
  else
    if @cloud.destroy(box)
      @boxes.remove @environment_name
      @boxes.save
    end
  end
end

#infoObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/migrant/environment.rb', line 90

def info
  box = @boxes[@environment_name]
  if box.nil?
    @ui.info "There are no boxes running"
  else
    connect
    @cloud.connect_to_server(box)
    @cloud.log_server_info
  end
end

#init_serverObject

If the server exists, connect to it. If not, bootstrap it



81
82
83
84
85
86
87
88
# File 'lib/migrant/environment.rb', line 81

def init_server
  box = @boxes[@environment_name]
  if box.nil?
    launch!
  else
    @cloud.connect_to_server(box)
  end
end

#launch!Object



71
72
73
74
75
76
77
78
# File 'lib/migrant/environment.rb', line 71

def launch!
  @cloud.bootstrap_server
  @boxes.add(@environment_name,setting('provider.name'),@server.id)
  @boxes.save

  # persist metadata about server so we can load it later
  @cloud.execute ['pwd']
end

#provisionObject



105
106
107
108
109
# File 'lib/migrant/environment.rb', line 105

def provision
  @provisioner.upload
  @provisioner.prepare
  @provisioner.run
end

#setting(name) ⇒ Object

Retrieve a setting by name. First try within the context of the current environment. If the property does not exist there, look in the default property definitions



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/migrant/environment.rb', line 40

def setting(name)
  roots = []
  if @environment_name
    roots << @config.send(@environment_name)
  end
  roots << @config
  roots.each do |root|
    begin
      setting_value = name.split('.').inject(root) { |node,prop| if node.include?(prop.to_sym) then node.send(prop) else raise "Undefined property #{prop} for #{node.name}" end}
      return setting_value
    rescue => e
      # Fall through to next case
    end
  end
  # If we get here the property does not exist
  #XXX - should probably ask for a default, and if one is not provided, raise an error
  nil
end

#upObject



59
60
61
62
63
64
# File 'lib/migrant/environment.rb', line 59

def up
  connect
  init_server
  bootstrap
  provision
end