Class: OpsWorker::App

Inherits:
Object
  • Object
show all
Defined in:
lib/ops_worker/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, revision, stack, opsworks_client) ⇒ App

Returns a new instance of App.



5
6
7
8
9
10
11
# File 'lib/ops_worker/app.rb', line 5

def initialize(id, name, revision, stack, opsworks_client)
  @id = id
  @name = name
  @stack = stack
  @revision = revision
  @opsworks_client = opsworks_client
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/ops_worker/app.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ops_worker/app.rb', line 3

def name
  @name
end

#revisionObject (readonly)

Returns the value of attribute revision.



3
4
5
# File 'lib/ops_worker/app.rb', line 3

def revision
  @revision
end

#stackObject (readonly)

Returns the value of attribute stack.



3
4
5
# File 'lib/ops_worker/app.rb', line 3

def stack
  @stack
end

Instance Method Details

#deploy(revision = nil) ⇒ Object

Parameters:

  • revision (defaults to: nil)

    String revision to deploy, e.g., “feature/foo” or “d6053592ee39e64c5f092b0ba6e9cd1aa8334828”



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ops_worker/app.rb', line 43

def deploy(revision = nil)
  OpsWorker.logger.info {"Deploying app #{@name} from #{revision || @revision}"}

  if deploying?
    raise StandardError.new("#{@name} is already deploying")
  end

  existing_revision = @revision.dup()
  changing_revisions = revision && revision != existing_revision

  if changing_revisions
    update_revision(revision)
  end

  deployment_status = create_deployment(:deploy)

  if changing_revisions
    update_revision(existing_revision)
  end

  deployment_status
end

#execute_recipes(recipe_names) ⇒ Object

Parameters:

  • recipe_names

    Array of string recipe names, e.g., [“custom::recipe1”]



82
83
84
85
# File 'lib/ops_worker/app.rb', line 82

def execute_recipes(recipe_names)
  OpsWorker.logger.info {"Executing recipes #{recipe_names} on #{@name}"}
  create_deployment(:execute_recipes, {"recipes" => recipe_names}, :all)
end

#instancesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ops_worker/app.rb', line 13

def instances
  if @instances
    return @instances
  end

  instances_result = @opsworks_client.describe_instances(:stack_id => @stack.id, :app_id => @id)[:instances]
  @instances = instances_result.map do |instance_hash|
    layers = @stack.layers.select {|l| instance_hash[:layer_ids].include?(l.id)}
    Instance.new(instance_hash[:instance_id],
                 instance_hash[:hostname],
                 instance_hash[:status],
                 instance_hash[:instance_type],
                 instance_hash[:elastic_ip],
                 instance_hash[:availability_zone],
                 self,
                 layers)
  end
end

#reloadObject



91
92
93
# File 'lib/ops_worker/app.rb', line 91

def reload
  @instances = nil
end

#restartObject



71
72
73
74
# File 'lib/ops_worker/app.rb', line 71

def restart
  OpsWorker.logger.info {"Restarting #{@name}"}
  create_deployment(:restart)
end

#rollbackObject



66
67
68
69
# File 'lib/ops_worker/app.rb', line 66

def rollback
  OpsWorker.logger.info {"Rolling back #{@name}"}
  create_deployment(:rollback)
end

#to_sObject



87
88
89
# File 'lib/ops_worker/app.rb', line 87

def to_s
  "#<OpsWorker::App #{@id}, #{@name}, stack: #{@stack.name}>"
end

#update_cookbooksObject



76
77
78
79
# File 'lib/ops_worker/app.rb', line 76

def update_cookbooks
  OpsWorker.logger.info {"Updating cookbooks for #{@name}"}
  create_deployment(:update_custom_cookbooks, {}, :all)
end

#update_revision(revision) ⇒ Object

Updates the revision in this app such that all future deploys pull from this revision

Parameters:

  • revision

    String revision to update, e.g., “feature/foo” or “d6053592ee39e64c5f092b0ba6e9cd1aa8334828”



34
35
36
37
38
39
40
# File 'lib/ops_worker/app.rb', line 34

def update_revision(revision)
  OpsWorker.logger.info {"Changing revision from #{@revision} to #{revision} for app #{@name}"}

  @opsworks_client.update_app(:app_id => @id, :app_source => {:revision => revision})
  @revision = revision
  reload()
end