forked from BrianMMcClain/mysql-concourse-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
out.rb
executable file
·49 lines (38 loc) · 1.25 KB
/
out.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
#!/usr/bin/env ruby
require 'mysql2'
require 'json'
require 'fileutils'
sourceDir = ARGV[0]
# Read in and parse the credentials from STDIN
sSource = STDIN.read
source = JSON.parse(sSource)
host = source["source"]["host"]
user = source["source"]["user"]
password = source["source"]["password"]
database = source["source"]["database"]
table = source["source"]["table"]
column = source["params"]["column"]
value = source["params"]["value"]
sqlPath = source["params"]["sql_path"]
# Read in the JSON-ified SQL row from the indicated path
sourceFile = File.join(sourceDir, sqlPath)
outSQL = File.read(sourceFile)
sqlSource = JSON.parse(outSQL)
version = sqlSource["id"]
# Connect to the MySQL server and update the specified row
client = Mysql2::Client.new(:host => host, :username => user, :password => password, :database => database)
client.query("UPDATE #{table} SET #{column}=#{value} WHERE id = #{version};")
# Compile the metadata to make public to the Concourse users
metadata = []
sqlSource.keys.each do |k|
metadata << {"name": k, "value": sqlSource[k].to_s}
end
metadata << {"name": "timestamp", "value": Time.now.to_s}
ret = {
"version": {
"id": version.to_s
},
"metadata": metadata
}
# Return the expected JSON to STDOUT
puts ret.to_json