cardano-up 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
 
3
3
  module CardanoUp
4
4
  ##
5
- # Start cardano-node and cardano-wallet on your system.
5
+ # Start/stop cardano-node and cardano-wallet on your system.
6
6
  #
7
7
  # For Linux and MacOS it will attempt to start separate screen sessions for
8
8
  # wallet and node respectively, therefore 'screen' tool is required on your system.
@@ -18,26 +18,29 @@ module CardanoUp
18
18
  # @see https://nssm.cc/
19
19
  # Nssm can be installed via choco package manager:
20
20
  # choco install nssm
21
- module Start
22
- # Prepare configuration for wallet and node to be started
23
- # @param env [Hash] provide env and wallet_port, e.g. { env: 'mainnet', wallet_port: '8090' }
21
+ module Launcher
22
+ # Create common set of variables for getting node and wallet up
23
+ # @param env [Hash] provide env and wallet_port,
24
+ # e.g. { env: 'mainnet', wallet_port: '8090', session_name: '0' }
24
25
  # @raise CardanoUp::EnvNotSupportedError
25
26
  # @raise CardanoUp::WalletPortError
26
- def self.prepare_configuration(opt = { env: 'mainnet', wallet_port: '8090' })
27
+ def self.setup(opt = { env: 'mainnet', wallet_port: '8090', session_name: '0' })
27
28
  env = opt[:env]
28
29
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env
29
30
 
30
31
  wallet_port = opt[:wallet_port]
31
32
  raise CardanoUp::WalletPortError if wallet_port.nil? || wallet_port.empty?
32
33
 
34
+ session_name = opt[:session_name] || '0'
35
+
33
36
  token_metadata_server = env == 'mainnet' ? CardanoUp::MAINNET_TOKEN_SERVER : CardanoUp::TESTNET_TOKEN_SERVER
34
37
 
35
38
  CardanoUp.configure_default unless CardanoUp.configured?
36
39
  configs = CardanoUp.config
37
- bin_dir = configs['bin_dir']
38
- config_dir = File.join(configs['config_dir'], env)
39
- log_dir = File.join(configs['log_dir'], env)
40
- state_dir = File.join(configs['state_dir'], env)
40
+ bin_dir = configs[:bin_dir]
41
+ config_dir = File.join(configs[:config_dir], env)
42
+ log_dir = File.join(configs[:log_dir], session_name, env)
43
+ state_dir = File.join(configs[:state_dir], session_name, env)
41
44
  wallet_db_dir = File.join(state_dir, 'wallet-db')
42
45
  node_db_dir = File.join(state_dir, 'node-db')
43
46
  [bin_dir, config_dir, log_dir, state_dir, wallet_db_dir, node_db_dir].each do |dir|
@@ -45,11 +48,11 @@ module CardanoUp
45
48
  end
46
49
 
47
50
  node_socket = if CardanoUp::Utils.win?
48
- "\\\\.\\pipe\\cardano-node-#{env}"
51
+ "\\\\.\\pipe\\cardano-node-#{env}-#{session_name}"
49
52
  else
50
53
  File.join(state_dir, 'node.socket')
51
54
  end
52
- network = env == 'mainnet' ? '--mainnet' : "--testnet #{config_dir}/byron-genesis.json"
55
+ network = env == 'mainnet' ? '--mainnet' : "--testnet #{File.join(config_dir, 'byron-genesis.json')}"
53
56
 
54
57
  {
55
58
  env: env,
@@ -62,50 +65,73 @@ module CardanoUp
62
65
  wallet_db_dir: wallet_db_dir,
63
66
  node_db_dir: node_db_dir,
64
67
  node_socket: node_socket,
65
- network: network
68
+ network: network,
69
+ session_name: session_name
66
70
  }
67
71
  end
68
72
 
69
- # @param configuration [Hash] output of prepare_configuration
70
- def self.start_node(configuration)
73
+ # @param configuration [Hash] output of setup
74
+ # @raise CardanoUp::SessionHasNodeError
75
+ # @raise CardanoUp::SessionHasWalletError
76
+ def self.node_up(configuration)
71
77
  env = configuration[:env]
72
78
  bin_dir = configuration[:bin_dir]
73
79
  config_dir = configuration[:config_dir]
74
80
  log_dir = configuration[:log_dir]
75
81
  node_db_dir = configuration[:node_db_dir]
76
82
  node_socket = configuration[:node_socket]
83
+ session_name = configuration[:session_name]
77
84
 
78
85
  exe = CardanoUp::Utils.win? ? '.exe' : ''
79
- version = CardanoUp::Utils.cmd "#{bin_dir}/cardano-node#{exe} version"
80
-
86
+ cardano_node = "#{File.join(bin_dir, 'cardano-node')}#{exe}"
87
+ version = CardanoUp::Utils.cmd "#{cardano_node} version"
88
+ node_cmd = ["#{cardano_node} run",
89
+ "--config #{File.join(config_dir, 'config.json')}",
90
+ "--topology #{File.join(config_dir, 'topology.json')}",
91
+ "--database-path #{node_db_dir}",
92
+ "--socket-path #{node_socket}"].join(' ')
93
+ node_service = if CardanoUp::Utils.win?
94
+ "cardano-node-#{env}-#{session_name}"
95
+ else
96
+ "NODE_#{env}_#{session_name}"
97
+ end
98
+ logfile = File.join(log_dir, 'node.log')
99
+ service_details = {
100
+ network: env,
101
+ node: {
102
+ service: node_service,
103
+ version: version,
104
+ log: logfile,
105
+ db_dir: node_db_dir,
106
+ socket_path: node_socket,
107
+ protocol_magic: get_protocol_magic(config_dir),
108
+ bin: node_cmd.split.first,
109
+ cmd: node_cmd
110
+ }
111
+ }
112
+ CardanoUp::Session.create_or_update(session_name, service_details)
81
113
  if CardanoUp::Utils.win?
82
114
  # Turn off p2p for Windows
83
115
  # TODO: remove after https://github.com/input-output-hk/ouroboros-network/issues/3968 released
84
- config_win = JSON.parse(File.read("#{config_dir}/config.json"))
85
- config_win['EnableP2P'] = false
86
- File.write("#{config_dir}/config.json", JSON.pretty_generate(config_win))
87
- topology = %({
88
- "Producers": [
89
- {
90
- "addr": "#{env}-node.world.dev.cardano.org",
91
- "port": 30002,
92
- "valency": 2
93
- }
94
- ]
95
- })
96
- File.write("#{config_dir}/topology.json", topology)
116
+ config_win = CardanoUp::Utils.from_json("#{config_dir}/config.json")
117
+ config_win[:EnableP2P] = false
118
+ CardanoUp::Utils.to_json("#{config_dir}/config.json", config_win)
119
+ topology = {
120
+ Producers: [
121
+ {
122
+ addr: "#{env}-node.world.dev.cardano.org",
123
+ port: 30_002,
124
+ valency: 2
125
+ }
126
+ ]
127
+ }
128
+ CardanoUp::Utils.to_json("#{config_dir}/topology.json", topology)
97
129
 
98
130
  # create cardano-node.bat file
99
- node_cmd = ["#{bin_dir}/cardano-node.exe run",
100
- "--config #{config_dir}/config.json",
101
- "--topology #{config_dir}/topology.json",
102
- "--database-path #{node_db_dir}",
103
- "--socket-path #{node_socket}"].join(' ')
104
131
  File.write("#{bin_dir}/cardano-node.bat", node_cmd)
105
- node_service = "cardano-node-#{env}"
106
132
  install_node = "nssm install #{node_service} #{bin_dir}/cardano-node.bat"
107
- log_stdout_node = "nssm set #{node_service} AppStdout #{log_dir}/node.log"
108
- log_stderr_node = "nssm set #{node_service} AppStderr #{log_dir}/node.log"
133
+ log_stdout_node = "nssm set #{node_service} AppStdout #{logfile}"
134
+ log_stderr_node = "nssm set #{node_service} AppStderr #{logfile}"
109
135
  start_node = "nssm start #{node_service}"
110
136
 
111
137
  CardanoUp::Utils.cmd install_node
@@ -113,32 +139,18 @@ module CardanoUp
113
139
  CardanoUp::Utils.cmd log_stderr_node
114
140
  CardanoUp::Utils.cmd start_node
115
141
  else
116
- node_cmd = ["#{bin_dir}/cardano-node run",
117
- "--config #{config_dir}/config.json",
118
- "--topology #{config_dir}/topology.json",
119
- "--database-path #{node_db_dir}",
120
- "--socket-path #{node_socket}"].join(' ')
121
- node_service = "NODE_#{env}"
122
- CardanoUp::Utils.cmd "screen -dmS #{node_service} -L -Logfile #{log_dir}/node.log #{node_cmd}"
142
+ screen_cmd = "screen -dmS #{node_service} -L -Logfile #{logfile} #{node_cmd}"
143
+ CardanoUp::Utils.cmd screen_cmd
123
144
  end
124
-
125
- {
126
- node: {
127
- service: node_service,
128
- network: env,
129
- version: version,
130
- log: "#{log_dir}/node.log",
131
- db_dir: node_db_dir,
132
- socket_path: node_socket,
133
- protocol_magic: get_protocol_magic(config_dir),
134
- bin: node_cmd.split.first,
135
- cmd: node_cmd
136
- }
137
- }
145
+ service_details
138
146
  end
139
147
 
140
- # @param configuration [Hash] output of prepare_configuration
141
- def self.start_wallet(configuration)
148
+ # @param configuration [Hash] output of setup
149
+ def self.wallet_up(configuration)
150
+ if CardanoUp::Utils.port_used?(configuration[:wallet_port].to_i)
151
+ raise CardanoUp::WalletPortUsedError, configuration[:wallet_port]
152
+ end
153
+
142
154
  env = configuration[:env]
143
155
  wallet_port = configuration[:wallet_port]
144
156
  token_metadata_server = configuration[:token_metadata_server]
@@ -147,24 +159,44 @@ module CardanoUp
147
159
  wallet_db_dir = configuration[:wallet_db_dir]
148
160
  node_socket = configuration[:node_socket]
149
161
  network = configuration[:network]
162
+ session_name = configuration[:session_name]
150
163
 
151
164
  exe = CardanoUp::Utils.win? ? '.exe' : ''
165
+ cardano_wallet = "#{File.join(bin_dir, 'cardano-wallet')}#{exe}"
166
+ wallet_cmd = ["#{cardano_wallet} serve",
167
+ "--port #{wallet_port}",
168
+ "--node-socket #{node_socket}",
169
+ network.to_s,
170
+ "--database #{wallet_db_dir}",
171
+ "--token-metadata-server #{token_metadata_server}"].join(' ')
152
172
  version = CardanoUp::Utils.cmd "#{bin_dir}/cardano-wallet#{exe} version"
173
+ wallet_service = if CardanoUp::Utils.win?
174
+ "cardano-wallet-#{env}-#{session_name}"
175
+ else
176
+ "WALLET_#{env}_#{session_name}"
177
+ end
178
+ logfile = File.join(log_dir, 'wallet.log')
179
+ service_details = {
180
+ network: env,
181
+ wallet: {
182
+ service: wallet_service,
183
+ version: version,
184
+ log: logfile,
185
+ db_dir: wallet_db_dir,
186
+ port: wallet_port.to_i,
187
+ url: "http://localhost:#{wallet_port}/v2",
188
+ bin: wallet_cmd.split.first,
189
+ cmd: wallet_cmd
190
+ }
191
+ }
153
192
 
193
+ CardanoUp::Session.create_or_update(session_name, service_details)
154
194
  if CardanoUp::Utils.win?
155
-
156
195
  # create cardano-wallet.bat file
157
- wallet_cmd = ["#{bin_dir}/cardano-wallet.exe serve",
158
- "--port #{wallet_port}",
159
- "--node-socket #{node_socket}",
160
- network.to_s,
161
- "--database #{wallet_db_dir}",
162
- "--token-metadata-server #{token_metadata_server}"].join(' ')
163
196
  File.write("#{bin_dir}/cardano-wallet.bat", wallet_cmd)
164
- wallet_service = "cardano-wallet-#{env}"
165
197
  install_wallet = "nssm install #{wallet_service} #{bin_dir}/cardano-wallet.bat"
166
- log_stdout_wallet = "nssm set #{wallet_service} AppStdout #{log_dir}/wallet.log"
167
- log_stderr_wallet = "nssm set #{wallet_service} AppStderr #{log_dir}/wallet.log"
198
+ log_stdout_wallet = "nssm set #{wallet_service} AppStdout #{logfile}"
199
+ log_stderr_wallet = "nssm set #{wallet_service} AppStderr #{logfile}"
168
200
  start_wallet = "nssm start #{wallet_service}"
169
201
 
170
202
  CardanoUp::Utils.cmd install_wallet
@@ -172,75 +204,45 @@ module CardanoUp
172
204
  CardanoUp::Utils.cmd log_stderr_wallet
173
205
  CardanoUp::Utils.cmd start_wallet
174
206
  else
175
- wallet_cmd = ["#{bin_dir}/cardano-wallet serve",
176
- "--port #{wallet_port}",
177
- "--node-socket #{node_socket}",
178
- network.to_s,
179
- "--database #{wallet_db_dir}",
180
- "--token-metadata-server #{token_metadata_server}"].join(' ')
181
- wallet_service = "WALLET_#{env}"
182
- CardanoUp::Utils.cmd "screen -dmS #{wallet_service} -L -Logfile #{log_dir}/wallet.log #{wallet_cmd}"
207
+ CardanoUp::Utils.cmd "screen -dmS #{wallet_service} -L -Logfile #{logfile} #{wallet_cmd}"
183
208
  end
184
209
 
185
- {
186
- wallet: {
187
- service: wallet_service,
188
- network: env,
189
- version: version,
190
- log: "#{log_dir}/wallet.log",
191
- db_dir: wallet_db_dir,
192
- port: wallet_port.to_i,
193
- host: "http://localhost:#{wallet_port}/v2",
194
- bin: wallet_cmd.split.first,
195
- cmd: wallet_cmd
196
- }
197
- }
198
- end
199
-
200
- # @param configuration [Hash] output of prepare_configuration
201
- def self.start_node_and_wallet(configuration)
202
- cn = start_node(configuration)
203
- cw = start_wallet(configuration)
204
- cn.merge(cw)
210
+ service_details
205
211
  end
206
212
 
207
213
  # @raise CardanoUp::EnvNotSupportedError
208
- def self.stop_node(env)
214
+ def self.node_down(env, session_name = '0')
209
215
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env
210
216
 
211
217
  if CardanoUp::Utils.win?
212
- CardanoUp::Utils.cmd "nssm stop cardano-node-#{env}"
213
- CardanoUp::Utils.cmd "nssm remove cardano-node-#{env} confirm"
218
+ CardanoUp::Utils.cmd "nssm stop cardano-node-#{env}-#{session_name}"
219
+ CardanoUp::Utils.cmd "nssm remove cardano-node-#{env}-#{session_name} confirm"
214
220
  else
215
- CardanoUp::Utils.cmd "screen -S NODE_#{env} -X at '0' stuff '^C'"
216
- CardanoUp::Utils.cmd "screen -XS NODE_#{env} quit"
221
+ CardanoUp::Utils.cmd "screen -S NODE_#{env}_#{session_name} -X at '0' stuff '^C'"
222
+ CardanoUp::Utils.cmd "screen -XS NODE_#{env}_#{session_name} quit"
217
223
  end
224
+ CardanoUp::Session.remove(session_name, { network: env, service: 'node' })
218
225
  end
219
226
 
220
227
  # @raise CardanoUp::EnvNotSupportedError
221
- def self.stop_wallet(env)
228
+ def self.wallet_down(env, session_name = '0')
222
229
  raise CardanoUp::EnvNotSupportedError, env unless CardanoUp::ENVS.include? env
223
230
 
224
231
  if CardanoUp::Utils.win?
225
- CardanoUp::Utils.cmd "nssm stop cardano-wallet-#{env}"
226
- CardanoUp::Utils.cmd "nssm remove cardano-wallet-#{env} confirm"
232
+ CardanoUp::Utils.cmd "nssm stop cardano-wallet-#{env}-#{session_name}"
233
+ CardanoUp::Utils.cmd "nssm remove cardano-wallet-#{env}-#{session_name} confirm"
227
234
  else
228
- CardanoUp::Utils.cmd "screen -S WALLET_#{env} -X at '0' stuff '^C'"
229
- CardanoUp::Utils.cmd "screen -XS WALLET_#{env} quit"
235
+ CardanoUp::Utils.cmd "screen -S WALLET_#{env}_#{session_name} -X at '0' stuff '^C'"
236
+ CardanoUp::Utils.cmd "screen -XS WALLET_#{env}_#{session_name} quit"
230
237
  end
231
- end
232
-
233
- # @raise CardanoUp::EnvNotSupportedError
234
- def self.stop_node_and_wallet(env)
235
- stop_wallet(env)
236
- stop_node(env)
238
+ CardanoUp::Session.remove(session_name, { network: env, service: 'wallet' })
237
239
  end
238
240
 
239
241
  ##
240
242
  # Get protocol magic from config's byron-genesis.json
241
243
  def get_protocol_magic(config)
242
- byron_genesis = JSON.parse(File.read(File.join(config, 'byron-genesis.json')))
243
- byron_genesis['protocolConsts']['protocolMagic'].to_i
244
+ byron_genesis = CardanoUp::Utils.from_json(File.join(config, 'byron-genesis.json'))
245
+ byron_genesis[:protocolConsts][:protocolMagic].to_i
244
246
  end
245
247
  module_function :get_protocol_magic
246
248
  private_class_method :get_protocol_magic
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardanoUp
4
+ # Check status (ping) of running node and wallet services
5
+ module Ping
6
+ # @param session_name [String]
7
+ # @param env [String]
8
+ # @raise CardanoUp::EnvNotSupportedError
9
+ # @raise CardanoUp::SessionNotExistsError
10
+ # @raise CardanoUp::SessionServiceNotUpError
11
+ # @raise CardanoUp::SessionEnvNotUpError
12
+ def self.wallet(session_name, env)
13
+ CardanoUp::Configs.exist?(env)
14
+ CardanoUp::Session.network_or_raise?(session_name, env.to_sym)
15
+ CardanoUp::Session.wallet_or_raise?(session_name, env.to_sym)
16
+ session = CardanoUp::Session.get(session_name)
17
+
18
+ url = session[env.to_sym][:wallet][:url]
19
+ r = HTTParty.get("#{url}/network/information")
20
+ [r.parsed_response, r.code]
21
+ end
22
+
23
+ # @param session_name [String]
24
+ # @param env [String]
25
+ # @raise CardanoUp::EnvNotSupportedError
26
+ # @raise CardanoUp::SessionNotExistsError
27
+ # @raise CardanoUp::SessionServiceNotUpError
28
+ # @raise CardanoUp::SessionEnvNotUpError
29
+ def self.node(session_name, env)
30
+ CardanoUp::Configs.exist?(env)
31
+ CardanoUp::Session.network_or_raise?(session_name, env.to_sym)
32
+ CardanoUp::Session.node_or_raise?(session_name, env.to_sym)
33
+ session = CardanoUp::Session.get(session_name)
34
+ bin_dir = CardanoUp.config[:bin_dir]
35
+
36
+ ENV['CARDANO_NODE_SOCKET_PATH'] = session[env.to_sym][:node][:socket_path]
37
+ protocol_magic = session[env.to_sym][:node][:protocol_magic]
38
+ network_arg = env == 'mainnet' ? '--mainnet' : "--testnet-magic #{protocol_magic}"
39
+ exe = CardanoUp::Utils.win? ? '.exe' : ''
40
+ cmd = "#{File.join(bin_dir, 'cardano-cli')}#{exe} query tip #{network_arg}"
41
+ res = CardanoUp::Utils.cmd cmd
42
+ begin
43
+ r = JSON.parse(res)
44
+ c = 200
45
+ rescue JSON::ParserError
46
+ r = 'Not responding.'
47
+ c = 404
48
+ end
49
+ [r, c]
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CardanoUp
4
+ # Basic sessions management
5
+ module Session
6
+ # Check session file exists
7
+ def self.exists?(session_name)
8
+ CardanoUp.configure_default unless CardanoUp.configured?
9
+ File.exist?(session_file_path(session_name))
10
+ end
11
+
12
+ # get session contents
13
+ def self.get(session_name)
14
+ raise CardanoUp::SessionNotExistsError, session_name unless exists?(session_name)
15
+
16
+ CardanoUp.configure_default unless CardanoUp.configured?
17
+ session = session_file_path(session_name)
18
+ CardanoUp::Utils.from_json(session)
19
+ end
20
+
21
+ def self.destroy!(session_name)
22
+ path = session_file_path(session_name)
23
+ FileUtils.rm_f(path)
24
+ end
25
+
26
+ def self.destroy_all!
27
+ list_all.each do |path|
28
+ FileUtils.rm_f(path)
29
+ end
30
+ end
31
+
32
+ def self.list_all
33
+ Dir[File.join(CardanoUp.base_dir, '.session*')]
34
+ end
35
+
36
+ # create or update session with details of launched service
37
+ # @param session_name [String] session name
38
+ # @param service_details [Hash] service details
39
+ # @raise ArgumentError
40
+ # @raise CardanoUp::SessionHasNodeError
41
+ # @raise CardanoUp::SessionHasWalletError
42
+ def self.create_or_update(session_name, service_details)
43
+ raise ArgumentError, 'service_details should be Hash' unless service_details.is_a?(Hash)
44
+ raise ArgumentError, 'service_details shoud have :network' if service_details[:network].nil?
45
+
46
+ service_network = service_details[:network].to_sym || :unknown
47
+
48
+ if !exists?(session_name) || empty?(session_name)
49
+ # if session is not set then just create new with just service_details
50
+ content = { service_network => service_details }
51
+ put_content(session_name, content)
52
+ elsif !network?(session_name, service_network)
53
+ # if session exists but has no network, add new network
54
+ existing_services = get(session_name)
55
+ new_service = { service_network => service_details }
56
+ put_content(session_name, existing_services.merge(new_service))
57
+ else
58
+ # if session has network and node already, you can add only wallet
59
+ if service_details.key?(:node) && node?(session_name, service_network)
60
+ raise CardanoUp::SessionHasNodeError.new(session_name, service_network)
61
+ end
62
+
63
+ # if session has network and wallet already, you can add only node
64
+ if service_details.key?(:wallet) && wallet?(session_name, service_network)
65
+ raise CardanoUp::SessionHasWalletError.new(session_name,
66
+ service_network)
67
+ end
68
+
69
+ existing_services = get(session_name)
70
+ updated_network = existing_services[service_network].merge(service_details)
71
+ existing_services[service_network] = updated_network
72
+ put_content(session_name, existing_services)
73
+ end
74
+ end
75
+
76
+ def self.node_or_raise?(session_name, env)
77
+ raise CardanoUp::SessionServiceNotUpError.new(session_name, env, :node) unless node?(session_name, env)
78
+
79
+ true
80
+ end
81
+
82
+ def self.wallet_or_raise?(session_name, env)
83
+ raise CardanoUp::SessionServiceNotUpError.new(session_name, env, :wallet) unless wallet?(session_name, env)
84
+
85
+ true
86
+ end
87
+
88
+ def self.network_or_raise?(session_name, env)
89
+ raise CardanoUp::SessionEnvNotUpError.new(session_name, env) unless network?(session_name, env)
90
+
91
+ true
92
+ end
93
+
94
+ # remove entry from session
95
+ # @param session_name [String] session name
96
+ # @param service_details [Hash] service details ({ network: 'preprod', service: 'wallet' })
97
+ def self.remove(session_name, service_details)
98
+ existing_services = get(session_name)
99
+ service = service_details[:service].to_sym
100
+ network = service_details[:network].to_sym
101
+ if existing_services && existing_services[network]
102
+ existing_services[network].delete(service)
103
+
104
+ unless existing_services[network].key?(:node) || existing_services[network].key?(:wallet)
105
+ existing_services.delete(network)
106
+ end
107
+ put_content(session_name, existing_services)
108
+ end
109
+
110
+ destroy!(session_name) if empty?(session_name)
111
+ end
112
+
113
+ def session_file_path(session_name)
114
+ File.join(CardanoUp.base_dir, ".session-#{session_name}.json")
115
+ end
116
+ module_function :session_file_path
117
+ private_class_method :session_file_path
118
+
119
+ def put_content(session_name, contents)
120
+ raise ArgumentError, 'contents should be Hash' unless contents.is_a?(Hash)
121
+
122
+ path = session_file_path(session_name)
123
+ File.write(path, JSON.pretty_generate(contents))
124
+ path
125
+ end
126
+ module_function :put_content
127
+ private_class_method :put_content
128
+
129
+ def empty?(session_name)
130
+ get(session_name) == {}
131
+ end
132
+ module_function :empty?
133
+ private_class_method :empty?
134
+
135
+ def network?(session_name, env)
136
+ get(session_name).key?(env.to_sym)
137
+ end
138
+ module_function :network?
139
+ private_class_method :network?
140
+
141
+ def node?(session_name, env)
142
+ get(session_name)[env.to_sym].key?(:node)
143
+ end
144
+ module_function :node?
145
+ private_class_method :node?
146
+
147
+ def wallet?(session_name, env)
148
+ get(session_name)[env.to_sym].key?(:wallet)
149
+ end
150
+ module_function :wallet?
151
+ private_class_method :wallet?
152
+ end
153
+ end
@@ -8,7 +8,7 @@ module CardanoUp
8
8
  res = `#{cmd}`
9
9
  puts cmd if display_result
10
10
  puts res if display_result
11
- res.gsub("\n", '')
11
+ res.gsub("\n", ' ').strip
12
12
  end
13
13
 
14
14
  def self.wget(url, file = nil)
@@ -17,6 +17,31 @@ module CardanoUp
17
17
  File.binwrite(file, resp.body)
18
18
  end
19
19
 
20
+ # Check if port is already used
21
+ def self.port_used?(port)
22
+ begin
23
+ Timeout.timeout(1) do
24
+ s = TCPSocket.new('localhost', port)
25
+ s.close
26
+ return true
27
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
28
+ return false
29
+ end
30
+ rescue Timeout::Error
31
+ # do nothing
32
+ end
33
+
34
+ false
35
+ end
36
+
37
+ def self.from_json(file)
38
+ JSON.parse(File.read(file), { symbolize_names: true })
39
+ end
40
+
41
+ def self.to_json(file, hash)
42
+ File.write(file, JSON.pretty_generate(hash))
43
+ end
44
+
20
45
  def self.win?
21
46
  RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
22
47
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Version
4
4
  module CardanoUp
5
- VERSION ||= '0.1.1'
5
+ VERSION ||= '0.1.2'
6
6
  end
data/lib/cardano-up.rb CHANGED
@@ -8,13 +8,18 @@ require 'file-tail'
8
8
  require 'json'
9
9
  require 'zlib'
10
10
  require 'zip'
11
+ require 'socket'
12
+ require 'timeout'
11
13
 
12
14
  require 'cardano-up/version'
13
15
  require 'cardano-up/err'
14
16
  require 'cardano-up/utils'
15
17
  require 'cardano-up/tar'
16
- require 'cardano-up/install'
17
- require 'cardano-up/start'
18
+ require 'cardano-up/bins'
19
+ require 'cardano-up/configs'
20
+ require 'cardano-up/launcher'
21
+ require 'cardano-up/session'
22
+ require 'cardano-up/ping'
18
23
  require 'cardano-up/tail'
19
24
 
20
25
  # Cardano Up!
@@ -74,24 +79,24 @@ module CardanoUp
74
79
  c = if configured?
75
80
  CardanoUp.config
76
81
  else
77
- { 'bin_dir' => File.join(@base_dir, 'bins'),
78
- 'state_dir' => File.join(@base_dir, 'state'),
79
- 'log_dir' => File.join(@base_dir, 'state'),
80
- 'config_dir' => File.join(@base_dir, 'configs') }
82
+ { bin_dir: File.join(@base_dir, 'bins'),
83
+ state_dir: File.join(@base_dir, 'state'),
84
+ log_dir: File.join(@base_dir, 'state'),
85
+ config_dir: File.join(@base_dir, 'configs') }
81
86
  end
82
- c['bin_dir'] = bin_dir if bin_dir
83
- c['state_dir'] = state_dir if state_dir
84
- c['log_dir'] = logs_dir if logs_dir
85
- c['config_dir'] = config_dir if config_dir
86
- File.write(@cardano_up_config, JSON.pretty_generate(c))
87
- JSON.parse(File.read(@cardano_up_config))
87
+ c[:bin_dir] = bin_dir if bin_dir
88
+ c[:state_dir] = state_dir if state_dir
89
+ c[:log_dir] = logs_dir if logs_dir
90
+ c[:config_dir] = config_dir if config_dir
91
+ CardanoUp::Utils.to_json(@cardano_up_config, c)
92
+ CardanoUp::Utils.from_json(@cardano_up_config)
88
93
  end
89
94
 
90
95
  # Get config values
91
96
  def self.config
92
97
  raise CardanoUp::ConfigNotSetError unless configured?
93
98
 
94
- JSON.parse(File.read(@cardano_up_config))
99
+ CardanoUp::Utils.from_json(@cardano_up_config)
95
100
  end
96
101
 
97
102
  # Remove configuration file