This repository has been archived by the owner on Apr 7, 2018. It is now read-only.
/
provider_database_sqlite.rb
110 lines (96 loc) · 3.11 KB
/
provider_database_sqlite.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# Author:: Seth Chisamore (<schisamo@chef.io>)
# Copyright:: 2011-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/provider'
class Chef
class Provider
class Database
class Sqlite < Chef::Provider
include Chef::Mixin::ShellOut
def load_current_resource
Gem.clear_paths
begin
require 'sqlite3'
rescue LoadError
Chef::Log.info('Could not load the required sqlite3 gem. Installing now')
case node['platform_family']
when 'rhel', 'fedora'
package ['gcc', 'make', 'sqlite-devel', 'sqlite']
when 'debian', 'ubuntu'
package ['gcc', 'make', 'libsqlite3-dev', 'sqlite3']
end
# Install required gem (will be compiled)
chef_gem 'sqlite3' do
compile_time false
end
require 'sqlite3'
end
@current_resource = Chef::Resource::Database.new(@new_resource.name)
@current_resource.database_name(@new_resource.database_name)
@current_resource
end
def action_create
unless exists?
::File.open(@new_resource.database_name, 'w') {}
@new_resource.updated_by_last_action(true)
end
end
def action_query
if exists?
begin
if @new_resource.sql_query.is_a?(Array)
@new_resource.sql_query.each do |sql|
Chef::Log.debug("#{@new_resource}: Performing queries [#{sql}]")
db.execute(sql)
end
else
Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql_query}]")
db.execute(@new_resource.sql_query)
end
@new_resource.updated_by_last_action(true)
ensure
close
end
end
end
def action_drop
if exists?
begin
Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
::File.unlink(@new_resource.database_name)
@new_resource.updated_by_last_action(true)
ensure
close
end
end
end
private
def exists?
::File.exist?(@new_resource.database_name)
end
def db
@db ||= begin
::SQLite3::Database.new(@new_resource.database_name)
end
end
def close
@db = nil
end
end
end
end
end