Class: Chapp::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/chapp/database.rb

Constant Summary collapse

APPS_DATA_BAG_NAME =
"chapp_apps"
APP_ATTRIBUTES_DATA_BAG_NAME =
"chapp_app_attributes"
PREFIX =
"chapp"

Instance Method Summary collapse

Instance Method Details

#app(app_id) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/chapp/database.rb', line 20

def app app_id
  
  unless app_exists? app_id
    raise Chapp::DB::AppNotExistsException.new, "App [#{app_id}] does not exist"
  end
  
  App.from_hash app_databag_item(app_id).raw_data["app"]
end

#app_attributes(app_id) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/chapp/database.rb', line 113

def app_attributes app_id
  databag = app_attributes_databag
  
  unless databag.has_key? app_id
    raise Chapp::DB::AppAttributesNotExistsException.new, "Attributes for app [#{app_id}] do not exist"
  end
  
  app_attributes_databag_item(app_id).raw_data["app_attributes"]
end

#app_exists?(app_id) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/chapp/database.rb', line 29

def app_exists? app_id
  apps_databag.has_key? app_id
end

#app_idsObject



16
17
18
# File 'lib/chapp/database.rb', line 16

def app_ids
  apps_databag.keys
end

#app_instances(app_id) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/chapp/database.rb', line 94

def app_instances app_id
  app_instances = Array.new
  
  query = Chef::Search::Query.new
  query.search(:node, "role:#{role_name app_id}") do |node|
    app_instances.push Chapp::AppInstance.new(app_id, node.name, node["fqdn"])
  end
  
  app_instances
end

#connected_app_ids(app_id) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/chapp/database.rb', line 33

def connected_app_ids app_id
  app_ids = Array.new
  
  query = Chef::Search::Query.new
  query.search APPS_DATA_BAG_NAME, "used_apps:#{app_id}" do |databag_item|
    app = App.from_hash databag_item.raw_data["app"]
    app_ids.push app.id
  end
  
  app_ids
end

#delete_app(app_name) ⇒ Object



89
90
91
92
# File 'lib/chapp/database.rb', line 89

def delete_app app_name
  # TODO (not necessary for now)
  # Delete role/environment, app databag item, app attribute databag item, app instances
end

#delete_app_attributes(app_id) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/chapp/database.rb', line 129

def delete_app_attributes app_id
  databag = app_attributes_databag
  
  if databag.has_key? app_id
    app_attributes_databag_item(app_id).destroy
  end
  
end

#delete_app_instance(app_instance) ⇒ Object



109
110
111
# File 'lib/chapp/database.rb', line 109

def delete_app_instance app_instance
  # TODO: Delete node/client (not necessary for now)
end

#write_app(app) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chapp/database.rb', line 45

def write_app app
  
  # TODO Verify used_apps exist
  # TODO Verify cookbooks exist
  
  app_role_name = role_name app.id
  
  attributes = app.node
  attributes["chapp"]["app_id"] = app.id
  
  # Build/Save role
  role = Chef::Role.new
  role.name app_role_name
  role.description "Used to define the App [#{app.id}]"
  role.default_attributes attributes.to_hash
  role.run_list "recipe[chapp]", *app.pre_run_list, "recipe[#{app.cookbook}::#{app.recipe}]", *app.post_run_list, "recipe[chapp::publish_app_attributes]"
  role.save
  
  # Set run_list of app to role
  app.run_list ["role[#{app_role_name}]"]
  
  environment_name = "#{PREFIX}_env_#{app.id}"
  
  # Build/Save environment
  environment = Chef::Environment.new
  environment.name  environment_name
  environment.description "Used to define the dependencies of App [#{app.id}]"
  
  app.dependencies.each do |cookbook, version|
    environment.cookbook cookbook, version
  end
  
  environment.save
  
   # Set environment of app
  app.environment environment_name
  
  # Update app databag item
  databag_item = app_databag_item app.id
  databag_item.raw_data["app"] = app.to_hash
  databag_item.save
  
end

#write_app_attributes(app_id, app_attributes) ⇒ Object



123
124
125
126
127
# File 'lib/chapp/database.rb', line 123

def write_app_attributes app_id, app_attributes
  databag_item = app_attributes_databag_item app_id
  databag_item.raw_data["app_attributes"] = app_attributes
  databag_item.save
end

#write_app_instance(app_instance) ⇒ Object



105
106
107
# File 'lib/chapp/database.rb', line 105

def write_app_instance app_instance
  # NOOP (uses Chef Search)
end