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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chapp/database.rb', line 95

def app app_id

  id = hash_to_app_id(app_id)

  app_instances = []
  app_attributes = {}
  latest_time = 0

  query = Chef::Search::Query.new
  # TODO Figure out how to get role name more appropriately
  query.search(:node, "role:chapp_#{id}") do |node|
    app_instances.push Chapp::AppInstance.new(id, node.name, node["fqdn"])

    if node.has_key?("ohai_time") && node.has_key?("chapp") && node["chapp"].has_key?("app_attributes") && node["ohai_time"] > latest_time
      latest_time = node["ohai_time"]
      app_attributes = node["chapp"]["app_attributes"].to_hash
    end
  end

  Chapp::App.new id, app_id, app_attributes, app_instances, self
end

#app_config(app_id) ⇒ Object



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

def app_config app_id

  unless app_exists? app_id
    raise AppNotExistsException.new, "App #{app_id_hash} does not exist"
  end

  AppConfig.from_hash apps_config_databag_item(hash_to_app_id(app_id)).raw_data["app"]

end

#app_exists?(app_id) ⇒ Boolean

Returns:

  • (Boolean)


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

def app_exists? app_id
  apps_config_databag.has_key? hash_to_app_id(app_id)
end

#app_idsObject



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

def app_ids
  apps_config_databag.keys
end

#connected_app_ids(app_id) ⇒ Object



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

def connected_app_ids app_id
  # TODO: Does not work
  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_id_hash) ⇒ Object



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

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

#delete_app_instance(app_instance) ⇒ Object



121
122
123
# File 'lib/chapp/database.rb', line 121

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

#write_app_config(app_config) ⇒ 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
88
# File 'lib/chapp/database.rb', line 45

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
  attributes["chapp"]["app_hash_id"] = app_config.id_hash

  # 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
  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



117
118
119
# File 'lib/chapp/database.rb', line 117

def write_app_instance app_instance
  # NOOP (uses Chef Search)
end