Class: Ey::Core::Client::Environment

Inherits:
Model
  • Object
show all
Extended by:
Associations
Defined in:
lib/ey-core/models/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Associations

assoc_accessor, assoc_coverage, assoc_reader, assoc_writer, associations, collection_reader

Methods inherited from Model

#destroy, range_parser, #save, #update!, #url

Instance Attribute Details

#application_idObject

Returns the value of attribute application_id.



32
33
34
# File 'lib/ey-core/models/environment.rb', line 32

def application_id
  @application_id
end

Instance Method Details

#applicationObject



69
70
71
72
73
# File 'lib/ey-core/models/environment.rb', line 69

def application
  if self.application_id
    connection.applications.get(self.application_id)
  end
end

#apply(type = "main", data = {}) ⇒ Object



57
58
59
# File 'lib/ey-core/models/environment.rb', line 57

def apply(type="main", data={})
  connection.requests.new(self.connection.apply_environment_updates("id" => self.id, "type" => type, "data" => data).body["request"])
end

#blueprintsObject



75
76
77
78
# File 'lib/ey-core/models/environment.rb', line 75

def blueprints
  requires :id
  self.connection.blueprints.all(environment: self.identity)
end

#boot(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ey-core/models/environment.rb', line 120

def boot(options={})
  options = Cistern::Hash.stringify_keys(options)
  if options["blueprint_id"]
    params = {
      "cluster_configuration" => {
        "blueprint_id" => options["blueprint_id"]
      }
    }

    self.connection.requests.new(self.connection.boot_environment(params.merge("id" => self.id)).body["request"])
  else
    raise "configuration is a required key" unless options["configuration"]
    raise "configuration['type'] is required" unless options["configuration"]["type"]

    missing_keys = []

    configuration = options["configuration"]
    required_keys = %w(flavor volume_size mnt_volume_size)

    if self.database_service
      raise "application_id is a required key" unless options["application_id"]

      configuration["logical_database"] = "#{self.name}_#{self.application.name}"
    end

    if configuration["type"] == 'custom'
      apps      = configuration["apps"]      ||= {}
      db_master = configuration["db_master"] ||= {}
      db_slaves = configuration["db_slaves"] ||= []
      utils     = configuration["utils"]     ||= []

      missing_keys << "apps" unless apps.any?
      (required_keys + ["count"]).each do |required|
        missing_keys << "apps[#{required}]" unless apps[required]
      end

      unless configuration["database_service_id"]
        missing_keys << "db_master" unless db_master.any?

        required_keys.each do |key|
          missing_keys << "db_master[#{key}]" unless db_master[key]
        end
      end

      db_slaves.each_with_index do |slave, i|
        (required_keys - ["volume_size", "mnt_volume_size"]).each do |key|
          missing_keys << "db_slaves[#{i}][#{key}]" unless slave[key]
        end
      end

      utils.each_with_index do |util, i|
        required_keys.each do |key|
          missing_keys << "utils[#{i}][#{key}]" unless util[key]
        end
      end
    end

    if missing_keys.any?
      raise "Invalid configuration - The following keys are missing from the configuration:\n#{missing_keys.join(",\n")}"
    end

    params = {
      "cluster_configuration" => {
        "configuration" => configuration
      }
    }

    connection.requests.new(self.connection.boot_environment(params.merge("id" => self.id)).body["request"])
  end
end

#deploy(application, opts = {}) ⇒ Object

Options Hash (opts):



36
37
38
39
40
# File 'lib/ey-core/models/environment.rb', line 36

def deploy(application, opts={})
  raise "Environment does not contain an app deployment for this application" unless self.applications.get(application.id)
  raise ":ref is a required key" unless opts[:ref]
  connection.requests.new(connection.deploy_environment_application({"deploy" => opts}.merge("id" => self.id, "application_id" => application.id)).body["request"])
end

#deprovisionObject



61
62
63
# File 'lib/ey-core/models/environment.rb', line 61

def deprovision
  connection.requests.new(self.connection.deprovision_environment("id" => self.id).body["request"])
end

#destroy!Object



65
66
67
# File 'lib/ey-core/models/environment.rb', line 65

def destroy!
  connection.requests.new(self.connection.destroy_environment("id" => self.id).body["request"])
end

#download_recipesObject



114
115
116
117
118
# File 'lib/ey-core/models/environment.rb', line 114

def download_recipes
  tempfile = Tempfile.new("cookbooks-#{self.name}.tar.gz")
  File.open(tempfile, 'wb') { |f| f.write self.connection.request(url: self.custom_recipes).body }
  tempfile
end

#maintenance(application, action) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ey-core/models/environment.rb', line 91

def maintenance(application, action)
  requires :identity

  params = {
    "id"             => self.identity,
    "application_id" => application.id,
    "maintenance"    => {
      "action" => action
    }
  }

  self.connection.requests.new(self.connection.change_environment_maintenance(params).body["request"])
end

#restart_app_serversObject



105
106
107
108
109
110
111
112
# File 'lib/ey-core/models/environment.rb', line 105

def restart_app_servers
  params = {
    "id"             => self.identity,
    "application_id" => self.applications.first.id,
  }

  self.connection.requests.new(self.connection.restart_environment_app_servers(params).body["request"])
end

#run_action(application, action, task = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ey-core/models/environment.rb', line 45

def run_action(application, action, task={})
  requires :id

  response = self.connection.run_environment_application_action(
    "environment" => self.id,
    "application" => application.id,
    "task"        => task,
    "action"      => action,
  )
  connection.requests.new(response.body["request"])
end

#save!Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ey-core/models/environment.rb', line 191

def save!
  if new_record?
    requires :application_id, :account_id, :region

    params = {
      "url"         => self.collection.url,
      "account"     => self.,
      "environment" => {
        "name"                      => self.name,
        "application_id"            => self.application_id,
        "region"                    => self.region,
      },
    }

    params["environment"].merge!("database_service" => self.database_service.id) if self.database_service

    merge_attributes(self.connection.create_environment(params).body["environment"])
  else
    requires :identity
    attributes = Cistern::Hash.slice(Cistern::Hash.stringify_keys(self.attributes), "nane", "release_label")
    connection.update_environment(
      "id"     => self.identity,
      "environment" => attributes,
    )
  end
end

#save_blueprint(options = {}) ⇒ Object



80
81
82
83
84
# File 'lib/ey-core/models/environment.rb', line 80

def save_blueprint(options={})
  requires :id
  raise "name is a required key" unless options["name"]
  self.connection.blueprints.new(self.connection.blueprint_environment("id" => self.id, "name" => options["name"]).body["blueprint"])
end

#upload_recipes(file) ⇒ Object



86
87
88
89
# File 'lib/ey-core/models/environment.rb', line 86

def upload_recipes(file)
  # file should be a StringIO
  self.connection.upload_recipes_for_environment("file" => file, "id" => self.identity)
end