Class: Chapp::Database

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

Defined Under Namespace

Classes: AppAttributesNotExistsException, AppNotExistsException

Constant Summary collapse

APPS_CONFIG_DATA_BAG_NAME =
"chapp_apps_config"
APP_ATTRIBUTES_DATA_BAG_NAME =
"chapp_app_attributes"

Instance Method Summary collapse

Instance Method Details

#app_attributes(app_id) ⇒ Object



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

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

#app_config(app_id) ⇒ Object



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

def app_config app_id
  
  unless app_exists? app_id
    raise AppNotExistsException.new, "App [#{app_id}] does not exist"
  end
  
  AppConfig.from_hash apps_config_databag_item(app_id).raw_data["app"]
end

#app_exists?(app_id) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/chapp/database.rb', line 27

def app_exists? app_id
  apps_config_databag.has_key? app_id
end

#app_idsObject



14
15
16
# File 'lib/chapp/database.rb', line 14

def app_ids
  apps_config_databag.keys
end

#app_instances(app_id) ⇒ Object



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

def app_instances app_id
  app_instances = Array.new
  
  query = Chef::Search::Query.new
  # TODO Figure out how to get role name more appropriately
  query.search(:node, "role:chapp_#{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



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

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

#delete_app(app_name) ⇒ Object



87
88
89
90
# File 'lib/chapp/database.rb', line 87

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

#delete_app_attributes(app_id) ⇒ Object



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

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



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

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

#write_app_attributes(app_id, app_attributes) ⇒ Object



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

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_config(app_config) ⇒ Object



43
44
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
# File 'lib/chapp/database.rb', line 43

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

#write_app_instance(app_instance) ⇒ Object



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

def write_app_instance app_instance
  # NOOP (uses Chef Search)
end