forked from OvercastNetwork/OCN-Data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
149 lines (126 loc) · 3.16 KB
/
Rakefile
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'net/http'
require 'openssl'
require 'json'
require 'yaml'
RESET = "\e[0m"
RED = "\e[31m"
YELO = "\e[33m"
GREEN = "\e[32m"
task :default => [:validate]
desc "Show a list of all valid permissions"
task :perms do
puts get(action_uri(:permissions)).body
end
desc "Validate local files with the server (but don't deploy anything)"
task :validate do
uri = action_uri(:validate)
puts "Validating models at #{uri}"
response = post(uri, {files: read_model_files})
puts YELO + response.body + RESET
if response.code.to_i == 200
puts "#{GREEN}Data looks valid#{RESET}"
else
puts "#{RED}Validation failed#{RESET}"
raise "Validation failed"
end
end
desc "Update local files to match what is currently deployed"
task :pull do
uri = action_uri(:pull)
puts "Pulling models from #{uri}"
json = JSON.load(get(uri).body)
puts YELO + json['log'] + RESET
write_model_files(json['files'])
end
def read_model_files
files = {}
each_model_file do |path|
files[path] = File.read(path)
end
files
end
def write_model_files(files)
changed = false
local = each_model_file.to_a
in_model_dir do
files.each do |path, content|
if local.include? path
old_yaml = YAML.load_file(path)
new_yaml = YAML.load(content)
if old_yaml != new_yaml
puts "Updating #{path}"
File.write(path, content)
changed = true
end
else
puts "Creating #{path}"
FileUtils.mkdir_p(File.dirname(path))
File.write(path, content)
changed = true
end
end
(local - files.keys).each do |path|
puts "Deleting #{path}"
File.delete(path)
changed = true
end
end
puts "No changes" unless changed
end
def each_model_file
if block_given?
in_model_dir do
Dir['**/*.yml'].each do |path|
yield path
end
end
else
enum_for :each_model_file
end
end
def in_model_dir
Dir.chdir model_base_path do
yield
end
end
def model_base_path
File.join(File.dirname(__FILE__), 'models')
end
def get(uri)
make_http(uri).request(make_get(uri))
end
def post(uri, json)
make_http(uri).request(make_post(uri, json))
end
def make_get(uri)
request = Net::HTTP::Get.new(uri.path)
add_headers(request)
request
end
def make_post(uri, json)
request = Net::HTTP::Post.new(uri.path)
add_headers(request)
request.body = json.to_json
request
end
def add_headers(request)
request.add_field('Content-Type', 'application/json')
request.add_field('X-OCN-Key', api_key)
end
def make_http(uri)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http
end
def action_uri(action)
URI.parse("#{base_uri}/admin/data/#{action}")
end
def base_uri
ENV['OCTC']
end
def api_key
ENV['OCN_API_KEY'] or raise "Missing OCN_API_KEY environment variable"
end