-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivnet-product-config-diff
executable file
·163 lines (138 loc) · 5.35 KB
/
pivnet-product-config-diff
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env ruby
require 'optimist'
require 'fileutils'
require 'net/http'
require 'uri'
require 'json'
require 'tty-prompt'
require 'diffy'
require 'yaml'
require 'find'
require 'byebug'
CONFIG_FILE=".pivnet-diff-config"
om_cli = ""
selected_releases = []
downloaded_releases = []
def pivnet_request(url)
uri = URI.parse(url)
request = Net::HTTP::Get.new(uri)
request["Authorization"] = $pivnet_api_token
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
return(JSON.parse(response.body))
end
opts = Optimist::options do
synopsis "Will provide a list of product tile configuration variable changes between selected release versions"
version "0.1.0"
banner <<-EOS
Tanzu Product Version Configuration Variable diff
Usage:
#{File.basename(__FILE__)} [options]
Enter no options to select from list
Example:
#{File.basename(__FILE__)} -p "elastic-runtime" -r "2.10.25 2.10.23"
where [options] are:
EOS
opt :product, "Product Slug List", short: "-p", type: :string
opt :releases, "Releases to diff", short: "-r", type: :string
opt :dont_clean_folders, "Don't Clean Temp Folder", short: "-d", type: :flag
depends :product, :releases
end
if opts[:releases]
selected_releases = opts[:releases].split(" ").sort
if selected_releases.count < 2
puts "You need to select 2 release versions to diff"
exit
end
end
puts `clear` unless opts[:product]
prompt = TTY::Prompt.new
if File.exists?(CONFIG_FILE)
$pivnet_api_token = YAML::load_file(CONFIG_FILE)
else
$pivnet_api_token = prompt.mask("What is your network.pivotal.io legacy API token")
File.open(CONFIG_FILE, "w") do |file|
file.write $pivnet_api_token.to_yaml
end
end
# Get products available in pivnet
puts "Getting product list from Tanzu Network"
products = pivnet_request("https://network.tanzu.vmware.com/api/v2/products")
products_list = []
products["products"].each do |pivnet_product|
products_list << {name: pivnet_product["name"], value: pivnet_product["slug"]}
end
if opts[:product]
selected_product = opts[:product]
else
# Select product to get diff for
selected_product = prompt.select("Select Product", products_list.sort_by!(&:values), per_page: 15, filter: true)
end
selected_product_record = products["products"].select {|product|product["slug"] == selected_product}.first
# Get product releases
if !opts[:releases]
puts "Getting product #{selected_product} releases url"
product = pivnet_request("https://network.tanzu.vmware.com/api/v2/products/#{selected_product}")
release_url = product["_links"]["releases"]["href"]
puts "Getting product #{selected_product} releases"
product_releases = pivnet_request(release_url)
# Get the releases for the selected product
releases_list = []
product_releases["releases"].each do |release|
release_name = release["version"].ljust(7)+' : '+release["release_date"]
releases_list << {id: release["id"], name: release_name, value: release["version"]}
end
# Select releases to diff
selected_releases = prompt.multi_select("Select Releases", releases_list.sort_by!{|k| [:value]}, max: 2,min: 2,per_page: 15, filter: true)
end
selected_releases.sort_by!{|k| [:value]}
# Download configuration templates
puts "Downloading configuration templates for releases #{selected_releases}"
FileUtils.mkdir 'tmp_templates' unless File.exists?('tmp_templates')
selected_releases.each do |release|
om_cli = "om config-template --output-directory tmp_templates/ --pivnet-api-token '#{$pivnet_api_token}' --pivnet-product-slug '#{selected_product}' --product-version '#{release}'"
if selected_product == "p-healthwatch" && release.split(".").first == "2"
om_cli += " --file-glob 'healthwatch-#{release}*.pivotal' "
elsif selected_product == "elastic-runtime"
om_cli += " --file-glob 'cf*.pivotal' "
end
system om_cli
# Find each release
puts "Finding release folders"
Find.find("#{Dir.pwd}/tmp_templates/") do |path|
if FileTest.directory?(path)
if File.basename(path).start_with?(release)
downloaded_releases << path
end
end
end
end
# Diff for templates
Diffy::Diff.default_format = :color
puts "Difference for Default Variables"
f1 = "#{downloaded_releases.first}/default-vars.yml"
f2 = "#{downloaded_releases.last}/default-vars.yml"
puts Diffy::Diff.new(f1, f2, :source => 'files', :context => 0)
puts "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
puts "Difference for Required Variables"
f1 = "#{downloaded_releases.first}/required-vars.yml"
f2 = "#{downloaded_releases.last}/required-vars.yml"
puts Diffy::Diff.new(f1, f2, :source => 'files', :context => 0)
puts "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
puts "Difference for Resource Variables"
f1 = "#{downloaded_releases.first}/resource-vars.yml"
f2 = "#{downloaded_releases.last}/resource-vars.yml"
puts Diffy::Diff.new(f1, f2, :source => 'files', :context => 0)
puts "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
puts "Difference for product template"
f1 = "#{downloaded_releases.first}/product.yml"
f2 = "#{downloaded_releases.last}/product.yml"
puts Diffy::Diff.new(f1, f2, :source => 'files', :context => 0)
# Clean up
if !opts[:dont_clean_folders]
FileUtils.rm_rf('tmp_templates')
end