Class: EY::Model::Environment

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ignore_bad_masterObject

Returns the value of attribute ignore_bad_master.



5
6
7
# File 'lib/engineyard/model/environment.rb', line 5

def ignore_bad_master
  @ignore_bad_master
end

Class Method Details

.from_arrayObject



17
18
19
# File 'lib/engineyard/model/environment.rb', line 17

def self.from_array(*)
  Collection::Environments[*super]
end

.from_hash(hash) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/engineyard/model/environment.rb', line 7

def self.from_hash(hash)
  super.tap do |env|
    env.username = hash['ssh_username']
    env.apps = App.from_array(env.apps, :api => env.api)
    env. = Account.from_hash(env.)
    env.instances = Instance.from_array(hash['instances'], :environment => env)
    env.app_master = Instance.from_hash(env.app_master.merge(:environment => env)) if env.app_master
  end
end

Instance Method Details

#app_master!Object



25
26
27
28
29
30
31
32
33
# File 'lib/engineyard/model/environment.rb', line 25

def app_master!
  master = app_master
  if master.nil?
    raise NoAppMasterError.new(name)
  elsif !ignore_bad_master && master.status != "running"
    raise BadAppMasterStatusError.new(master.status)
  end
  master
end

#configurationObject Also known as: config



122
123
124
# File 'lib/engineyard/model/environment.rb', line 122

def configuration
  EY.config.environments[self.name] || {}
end

#default_branchObject



127
128
129
# File 'lib/engineyard/model/environment.rb', line 127

def default_branch
  EY.config.default_branch(name)
end

#deploy(app, ref, deploy_options = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/engineyard/model/environment.rb', line 35

def deploy(app, ref, deploy_options={})
  app_master!.deploy(app,
    ref,
    migration_command(deploy_options),
    config.merge(deploy_options['extras']),
    deploy_options['verbose'])
end

#download_recipesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/engineyard/model/environment.rb', line 65

def download_recipes
  if File.exist?('cookbooks')
    raise EY::Error, "Could not download, cookbooks already exists"
  end

  require 'tempfile'
  tmp = Tempfile.new("recipes")
  tmp.write(api.request("/environments/#{id}/recipes"))
  tmp.flush
  tmp.close

  cmd = "tar xzf '#{tmp.path}' cookbooks"

  unless system(cmd)
    raise EY::Error, "Could not unarchive recipes.\nCommand `#{cmd}` exited with an error."
  end
end

#logsObject



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

def logs
  Log.from_array(api_get("/environments/#{id}/logs")["logs"])
end

#put_up_maintenance_page(app, verbose = false) ⇒ Object



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

def put_up_maintenance_page(app, verbose=false)
  app_master!.put_up_maintenance_page(app, verbose)
end

#rebuildObject



57
58
59
# File 'lib/engineyard/model/environment.rb', line 57

def rebuild
  api.request("/environments/#{id}/update_instances", :method => :put)
end

#resolve_branch(branch, allow_non_default_branch = false) ⇒ Object



115
116
117
118
119
120
# File 'lib/engineyard/model/environment.rb', line 115

def resolve_branch(branch, allow_non_default_branch=false)
  if !allow_non_default_branch && branch && default_branch && (branch != default_branch)
    raise BranchMismatchError.new(default_branch, branch)
  end
  branch || default_branch
end

#rollback(app, extra_deploy_hook_options = {}, verbose = false) ⇒ Object



43
44
45
46
47
# File 'lib/engineyard/model/environment.rb', line 43

def rollback(app, extra_deploy_hook_options={}, verbose=false)
  app_master!.rollback(app,
    config.merge(extra_deploy_hook_options),
    verbose)
end

#run_custom_recipesObject



61
62
63
# File 'lib/engineyard/model/environment.rb', line 61

def run_custom_recipes
  api.request("/environments/#{id}/run_custom_recipes", :method => :put)
end

#shorten_name_for(app) ⇒ Object



131
132
133
# File 'lib/engineyard/model/environment.rb', line 131

def shorten_name_for(app)
  name.gsub(/^#{Regexp.quote(app.name)}_/, '')
end

#take_down_maintenance_page(app, verbose = false) ⇒ Object



49
50
51
# File 'lib/engineyard/model/environment.rb', line 49

def take_down_maintenance_page(app, verbose=false)
  app_master!.take_down_maintenance_page(app, verbose)
end

#tar_and_upload_recipes_in_cookbooks_dirObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/engineyard/model/environment.rb', line 92

def tar_and_upload_recipes_in_cookbooks_dir
  require 'tempfile'
  unless File.exist?("cookbooks")
    raise EY::Error, "Could not find chef recipes. Please run from the root of your recipes repo."
  end

  recipes_file = Tempfile.new("recipes")
  cmd = "tar czf '#{recipes_file.path}' cookbooks/"

  unless system(cmd)
    raise EY::Error, "Could not archive recipes.\nCommand `#{cmd}` exited with an error."
  end

  upload_recipes(recipes_file)
end

#upload_recipes(file_to_upload) ⇒ Object



108
109
110
111
112
113
# File 'lib/engineyard/model/environment.rb', line 108

def upload_recipes(file_to_upload)
  api.request("/environments/#{id}/recipes", {
    :method => :post,
    :params => {:file => file_to_upload}
  })
end

#upload_recipes_at_path(recipes_path) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/engineyard/model/environment.rb', line 83

def upload_recipes_at_path(recipes_path)
  recipes_path = Pathname.new(recipes_path)
  if recipes_path.exist?
    upload_recipes recipes_path.open('r')
  else
    raise EY::Error, "Recipes file not found: #{recipes_path}"
  end
end