forked from Consensys/qubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qubernetes
executable file
·207 lines (174 loc) · 7.03 KB
/
qubernetes
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env ruby
require "yaml"
require "erb"
require 'colorize'
# setup the config file to use to generate the kubernetes API resources.
@CONFIG_FILE = "qubernetes.yaml"
@OPTIONAL_CONFIG_FILE=ARGV[0]
ARGV.clear
if @OPTIONAL_CONFIG_FILE != nil
@CONFIG_FILE = @OPTIONAL_CONFIG_FILE
end
puts "using config file: " + @CONFIG_FILE
@config = YAML.load_file(@CONFIG_FILE)
@nodes = YAML.load_file("nodes.yaml")["nodes"]
## set defaults for config if not set, else use the values from the config.
# used in deployment.yaml.erb to set quorum data dir.
@Node_DataDir = "/etc/quorum/qdata"
if @config["quorum"]["Node_DataDir"]
@Node_DataDir = @config["quorum"]["Node_DataDir"]
end
# used by quorum-shared-config.yaml.erb and quorum-keystore.yaml.erb to load keys.
@Key_Dir_Base = "out/config"
if @config["quorum"]["Key_Dir_Base"]
@Key_Dir_Base = @config["quorum"]["Key_Dir_Base"]
end
# used by quorum-shared-config.yaml.erb to load the permissioned-nodes.json in configmaps
@Permissioned_Nodes_File = "out/config/permissioned-nodes.json"
if @config["quorum"]["Permissioned_Nodes_File"]
@Permissioned_Nodes_File = @config["quorum"]["Permissioned_Nodes_File"]
end
# used by quorum-genesis-config.yaml.erb and quorum-shared-config.yaml.erb
@Genesis_File = "out/config/genesis.json"
if @config["quorum"]["Genesis_File"]
@Genesis_File = @config["quorum"]["Genesis_File"]
end
# used in quorum-shared-config.yaml.erb to make tessera config available to deployments.
@Tessera_Config_Dir = "out/config"
if @config["quorum"]["tm"]["Tessera_Config_Dir"]
@Tessera_Config_Dir = @config["quorum"]["tm"]["Tessera_Config_Dir"]
end
## default the ports if they aren't set
@Raft_Port = 50401
if @config["quorum"]["quorum"]["Raft_Port"]
@Raft_Port = @config["quorum"]["quorum"]["Raft_Port"]
end
@Tm_Port = 9001
if @config["quorum"]["tm"]["Port"]
@Tm_Port = @config["quorum"]["tm"]["Port"]
end
@Node_RPCPort = 8545
if @config["geth"] and @config["geth"]["Node_RPCPort"]
@Node_RPCPort = @config["geth"]["Node_RPCPort"]
end
@NodeP2P_ListenAddr = 30303
if @config["geth"] and @config["geth"]["NodeP2P_ListenAddr"]
@NodeP2P_ListenAdd = @config["geth"]["NodeP2P_ListenAddr"]
end
# Generate deployments in a single file, or in separate files.
@sep_deployment_files=false
if @config["sep_deployment_files"] != nil
@sep_deployment_files = @config["sep_deployment_files"]
end
# If the namespace is set in the config, create it and add it to all the resources.
@Namespace = ""
if @config["namespace"] != nil
if @config["namespace"]["name"] != ""
@Namespace = "namespace: " + @config["namespace"]["name"]
end
end
# What kind of persistent storage is desired?
# Supported storage type as of 2020 March 11 is only Persistant Volume Claims, and hostPath is no longer supported.
@Storage_Type = "PVC"
#####################
# Create config files for each node
#####################
def set_node_template_vars(values)
@Node_UserIdent = values["Node_UserIdent"]
@Node_Key_Dir = values["Key_Dir"]
return
end
# create the output directory if it doesn't exist
`mkdir -p out`
sed_string = ""
sed_string_no_escape = ""
# make all services
# set the replacement string sed command which is run on the permissioned-nodes.json,
# as the service host IPs of the nodes are not known until they are deployed, and need
# to be properly set in the permissioned-nodes.json.
#PERM_NODE_JSON=$(echo $PERM_NODE_TMPL | sed \"s/%QUORUM_DEPLOYMENT_01_SERVICE_HOST%/$QUORUM_NODE01_SERVICE_HOST/g\" | sed \"s/\\$QUORUM_DEPLOYMENT_02_SERVICE_HOST/$QUORUM_NODE02_SERVICE_HOST/g\");
@nodes.each do |node|
set_node_template_vars(node.values.first)
# puts ("#{@Node_UserIdent}")
k8_service_host_var = ("#{@Node_UserIdent}".upcase + "_SERVICE_HOST").gsub("-", "_")
permission_node_host="#{@Node_UserIdent}".upcase + "_SERVICE_HOST"
# puts (k8_service_host_var)
sed_instruction = ' sed \"s/%' + permission_node_host + '%/$' + k8_service_host_var + '/g\"'
sed_instruction_no_escape = ' sed "s/%' + permission_node_host + '%/$' + k8_service_host_var + '/g"'
sed_string = sed_string + sed_instruction + " | "
sed_string_no_escape = sed_string_no_escape + sed_instruction_no_escape + " | "
end
# used/written inside each generated deployment file.
@Sed_Set_Node_Service_Host = sed_string[0...-2]
# used inside cofigMap script
@Sed_Set_Node_Service_Host_No_Escape = sed_string_no_escape[0...-2]
@base_template_path = "templates/k8s"
#puts (sed_string)
puts "PVC"
if @Storage_Type == "PVC"
File.open("out/00-quorum-persistent-volumes.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/persistent-volumes.yaml.erb"), nil, "-").result)
end
end
File.open("out/01-quorum-genesis.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-genesis-config.yaml.erb"), nil, "-").result)
end
File.open("out/02-quorum-shared-config.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-shared-config.yaml.erb"), nil, "-").result)
end
# Create the service resources
File.open("out/03.0-quorum-services.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-services.yaml.erb"), nil, "-").result)
end
# Create the Ingress resources if they are configured
if @config["service"] and @config["service"]["Ingress"]
File.open("out/03.1-quorum-ingress.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-ingress.yaml.erb"), nil, "-").result)
end
end
# make all keystore resrouce (configMap)
File.open("out/04-quorum-keyconfigs.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-keystore.yaml.erb"), nil, "-").result)
end
# if a network policy was requested create one for the namespace (NetworkPolicy = true in yaml config)
if @config["service"] and @config["service"]["NetworkPolicy"]
File.open("out/05-quorum-network-policy.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/network-policy.yaml.erb"), nil, "-").result)
end
end
puts "deployments"
@Kubectl_Cmd=" $> kubectl apply -f out"
if @sep_deployment_files
`mkdir -p out/deployments`
ct=1
@nodes.each do |node|
set_node_template_vars(node.values.first)
# create each deployment in a separate file.
File.open("out/deployments/0" + ct.to_s + "-quorum-single-deployment.yaml", "w") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-deployment.yaml.erb"), nil, "-").result)
end
ct = ct + 1
end
@Kubectl_Cmd=" $> kubectl apply -f out -f out/deployments"
else
File.open("out/05-quorum-deployments.yaml", "w") do |f|
f.puts("---")
end
@nodes.each do |node|
set_node_template_vars(node.values.first)
# create each deployment in a separate file.
File.open("out/05-quorum-deployments.yaml", "a") do |f|
f.puts (ERB.new(File.read(@base_template_path + "/quorum-deployment.yaml.erb"), nil, "-").result)
f.puts("---")
end
end
end
puts("\n")
puts " Success! ".green
puts("\n")
puts(" Quorum Kubernetes resource files have been generated in the `out/` directory.")
puts("\n")
puts(" To deploy to kubernetes run:")
puts("\n")
puts(@Kubectl_Cmd)
puts("\n")