Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
332 lines (292 loc) · 14.8 KB

File metadata and controls

332 lines (292 loc) · 14.8 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Text;
namespace Synapse.Services.NodeService.Cli
{
class Program : Synapse.Common.CmdLine.HttpApiCliBase
{
static void Main(string[] args)
{
if( args.Length > 0 && (args[0].ToLower() == "interactive" || args[0].ToLower() == "i") )
{
Program p = new Program()
{
IsInteractive = true,
};
if( args.Length > 1 )
{
string[] s = args[1].Split( new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries );
if( s.Length == 2 )
p.BaseUrl = s[1];
}
Console.Write( "synapse> " );
string input = Console.ReadLine();
while( input.ToLower() != "exit" )
{
p.ProcessArgs( input.Split( ' ' ) );
Console.Write( "synapse> " );
input = Console.ReadLine();
}
}
else
{
new Program().ProcessArgs( args );
}
}
Dictionary<string, string> _methods = new Dictionary<string, string>();
readonly string _service = "service";
readonly string _genconfig = "genconfig";
public Program()
{
_methods.Add( "hello", "Hello" );
_methods.Add( "hi", "Hello" );
_methods.Add( "whoami", "WhoAmI" );
_methods.Add( "who", "WhoAmI" );
_methods.Add( "start", "StartPlanFile" );
_methods.Add( "s", "StartPlanFile" );
_methods.Add( "cancel", "CancelPlan" );
_methods.Add( "c", "CancelPlan" );
_methods.Add( "drainstop", "Drainstop" );
_methods.Add( "dst", "Drainstop" );
_methods.Add( "undrainstop", "Undrainstop" );
_methods.Add( "ust", "Undrainstop" );
_methods.Add( "DrainStatus", "GetIsDrainstopComplete" );
_methods.Add( "dss", "GetIsDrainstopComplete" );
_methods.Add( "QueueDepth", "GetCurrentQueueDepth" );
_methods.Add( "qd", "GetCurrentQueueDepth" );
_methods.Add( "QueueItems", "GetCurrentQueueItems" );
_methods.Add( "qi", "GetCurrentQueueItems" );
SynapseServerConfig.DeserializeOrNew( ServerRole.Node );
}
public bool IsInteractive { get; set; }
public string BaseUrl { get; set; }
void ProcessArgs(string[] args)
{
if( args.Length == 0 )
{
WriteHelpAndExit();
}
else
{
try
{
bool error = false;
Dictionary<string, string> parms = new Dictionary<string, string>( StringComparer.OrdinalIgnoreCase );
parms = ParseCmdLine( args, 0, ref error, suppressErrorMessages: true );
string configFile = null;
if( parms.ContainsKey( InstallUtility.SynapseConfigParm ) )
{
configFile = parms[InstallUtility.SynapseConfigParm];
parms.Remove( InstallUtility.SynapseConfigParm );
}
SynapseServerConfig config = SynapseServerConfig.DeserializeOrNew( ServerRole.Node, configFile );
if( string.IsNullOrWhiteSpace( BaseUrl ) )
BaseUrl = $"{config.WebApi.ToUri( isUserInteractive: true )}/synapse/node";
if( parms.ContainsKey( "url" ) )
{
BaseUrl = parms["url"];
parms.Remove( "url" );
}
string referrerurl = null;
if( parms.ContainsKey( "referrerurl" ) )
{
referrerurl = parms["referrerurl"];
parms.Remove( "referrerurl" );
}
string arg0 = args[0].ToLower();
if( _methods.ContainsKey( arg0 ) )
{
Console.WriteLine( $"Calling {_methods[arg0]} on {BaseUrl}" );
if( _methods[arg0] == "StartPlanFile" )
RunStartPlanMethod( args, parms, referrerurl );
else
RunMethod( new NodeServiceHttpApiClient( BaseUrl, referrer: referrerurl ), _methods[arg0], args );
}
else if( arg0.StartsWith( _service ) )
RunServiceAction( args );
else if( arg0.StartsWith( _genconfig ) )
RunConfigGenerator( args );
else
WriteHelpAndExit( "Unknown action." );
}
catch( Exception ex )
{
WriteHelpAndExit( Synapse.Common.WebApi.Utilities.UnwindException( ex ) );
}
}
}
protected virtual void RunStartPlanMethod(string[] args, Dictionary<string, string> parameters, string referrerurl = null)
{
string methodName = "StartPlanFile";
NodeServiceHttpApiClient instance = new NodeServiceHttpApiClient( BaseUrl, referrer: referrerurl );
bool needHelp = args.Length == 2 && args[1].ToLower().Contains( "help" );
if( needHelp )
{
Dictionary<string, Type> parms = new Dictionary<string, Type>
{
{ "filePath", typeof( string ) },
{ "planInstanceId", typeof( long ) },
{ "dryRun", typeof( bool ) }
};
Console.WriteLine( $"Parameter options for {methodName}:\r\n" );
WriteMethodParametersHelp( parms );
Console.WriteLine( $"Remaining argname:argvalue pairs will be passed as dynamic parameters.\r\n" );
}
else
{
string filePath = null;
string fp = nameof( filePath ).ToLower();
if( parameters.ContainsKey( fp ) )
{
filePath = parameters[fp];
parameters.Remove( fp );
}
else
throw new Exception( "filePath is required." );
long planInstanceId = 0;
string piid = nameof( planInstanceId ).ToLower();
if( parameters.ContainsKey( piid ) )
{
long.TryParse( parameters[piid], out planInstanceId );
parameters.Remove( piid );
}
bool dryRun = false;
string dr = nameof( dryRun ).ToLower();
if( parameters.ContainsKey( dr ) )
{
bool.TryParse( parameters[dr], out dryRun );
parameters.Remove( dr );
}
try
{
Core.ExecuteResult result = instance.StartPlanFile( filePath, planInstanceId, dryRun, parameters );
Console.WriteLine( result );
}
catch( Exception ex )
{
WriteException( ex );
}
}
}
protected virtual void RunServiceAction(string[] args)
{
if( args.Length < 2 )
WriteHelpAndExit( "Not enough arguments specified." );
string option = args[1].ToLower();
switch( option )
{
case "run":
{
string[] arguments = new string[] { };
if( args.Length > 2 )
{
arguments = new string[args.Length - 2];
for( int i = 2; i < args.Length; i++ )
arguments[i - 2] = args[i];
}
SynapseServer.RunConsole( arguments );
break;
}
case "install":
{
string message = string.Empty;
bool error = false;
Dictionary<string, string> values = ParseCmdLine( args, 2, ref error, true );
if( !InstallUtility.InstallAndStartService( serverRole: ServerRole.Node, installOptions: values, message: out message ) )
{
Console.WriteLine( message );
Environment.Exit( 1 );
}
break;
}
case "uninstall":
{
string message = string.Empty;
bool error = false;
Dictionary<string, string> values = ParseCmdLine( args, 2, ref error, true );
if( !InstallUtility.StopAndUninstallService( installOptions: values, message: out message ) )
{
Console.WriteLine( message );
Environment.Exit( 1 );
}
break;
}
default:
{
WriteHelpAndExit( "Unknown service action." );
Environment.Exit( 1 );
break;
}
}
}
#region Generate ServerConfigs
private void RunConfigGenerator(string[] args)
{
Console.WriteLine( $"Calling {nameof( GenerateConfig )}." );
RunMethod( this, nameof( GenerateConfig ), args );
}
public void GenerateConfig(string filePath)
{
if( string.IsNullOrWhiteSpace( filePath ) )
throw new ArgumentNullException( nameof( filePath ), "FilePath is required." );
SynapseServerConfig.DeserializeOrNew( ServerRole.Node, filePath ); ;
}
#endregion
#region Help
protected override void WriteHelpAndExit(string errorMessage = null)
{
bool haveError = !string.IsNullOrWhiteSpace( errorMessage );
ConsoleColor defaultColor = Console.ForegroundColor;
Console_WriteLine( $"synapse.node.cli.exe, Version: {typeof( Program ).Assembly.GetName().Version}\r\n", ConsoleColor.Green );
Console.WriteLine( "Syntax:" );
Console_WriteLine( " synapse.node.cli.exe service {0}command{1} | {0}httpAction parm:value{1}", ConsoleColor.Cyan, "{", "}" );
Console.WriteLine( " [referrerUrl:http://{1}host:port{2}/synapse/execute] |", "", "{", "}" );
Console.WriteLine( " interactive|i [url:http://{1}host:port{2}/synapse/node] |", "", "{", "}" );
Console.WriteLine( " genconfig filePath:{1}path{2}\r\n", "", "{", "}" );
Console_WriteLine( " About URLs:{0,-2}URL is an optional parameter on all commands except 'service'", ConsoleColor.Green, "" );
Console.WriteLine( "{0,-15}commands. Specify as [url:http://{1}host:port{2}/synapse/node].", "", "{", "}" );
Console.WriteLine( "{0,-15}URL default is localhost:{1}port{2} (See WebApiPort in config.yaml)", "", "{", "}" );
Console.WriteLine( "{0,-15}referrerUrl allows an override of default response location.\r\n", "", "{", "}" );
Console.WriteLine( " interactive{0,-2}Run this CLI in interactive mode, optionally specify URL.", "" );
Console.WriteLine( "{0,-15}All commands below work in standard or interactive modes.\r\n", "" );
Console.WriteLine( " service{0,-6}Install/Uninstall the Windows Service, or Run the Service", "" );
Console.WriteLine( "{0,-15}as a cmdline-hosted daemon.", "" );
Console.WriteLine( "{0,-15}- Commands: install [run:true|false] | uninstall | run", "" );
Console.WriteLine( "{0,-15}- Example: synapse.node.cli service install run:false", "" );
Console.WriteLine( "{0,-15} synapse.node.cli service run\r\n", "" );
Console.WriteLine( " {1}{0,-4}Generate a Synapse Node config file.", "", _genconfig );
Console.WriteLine( "{0,-15}- filePath: Path and filename for the config file.", "" );
Console.WriteLine( "{0,-15}- Example: synapse.node.cli genconfig filepath:custom.yaml\r\n", "" );
Console.WriteLine( "{0,-15}Note: Running synapse.node.cli with no parameters will", "" );
Console.WriteLine( "{0,-15} generate a default config if none exists. Use this option", "" );
Console.WriteLine( "{0,-15} to generate a named config file.\r\n", "" );
Console.WriteLine( " httpAction{0,-3}Execute a command, optionally specify URL.", "" );
Console.WriteLine( "{0,-15}Parm help: synapse.node.cli {1}httpAction{2} help.\r\n", "", "{", "}" );
Console.WriteLine( " - httpActions:", "" );
Console.WriteLine( " - Hello|hi Returns 'Hello, World!'.", "" );
Console.WriteLine( " - WhoAmI|who Returns NodeServer User Context.", "" );
Console.WriteLine( " - Start|s Start a new Plan Instance.", "" );
Console.WriteLine( " - Cancel|c Cancel a Plan Instance.", "" );
Console.WriteLine( " - Drainstop|dst Prevents the node from receiving incoming requests;", "" );
Console.WriteLine( " allows existing threads to complete. Optionally stops", "" );
Console.WriteLine( " the Service when queue is fully drained.", "" );
Console.WriteLine( " - DrainStatus|dss Returns true/false on whether queue is fully drained.", "" );
Console.WriteLine( " - QueueDepth|qd Returns the number of items remaining in the queue.", "" );
Console.WriteLine( " - QueueItems|qi Returns the list of items remaining in the queue.", "" );
Console.WriteLine( " - Undrainstop|ust Resumes normal request processing.\r\n", "" );
Console.WriteLine( " Examples:", "" );
Console.WriteLine( " synapse.node.cli hi url:http://somehost/synapse/node", "" );
Console.WriteLine( " synapse.node.cli s help", "" );
Console.WriteLine( " synapse.node.cli s planInstanceId:0 dryRun:false filePath:C:\\planFile.yaml", "" );
Console.WriteLine( " synapse.node.cli dst url:http://somehost/synapse/node", "" );
Console.WriteLine( " synapse.node.cli i url:http://somehost/synapse/node", "" );
Console.WriteLine( " synapse.node.cli i", "" );
if( haveError )
Console_WriteLine( $"\r\n\r\n*** Last error:\r\n{errorMessage}\r\n", ConsoleColor.Red );
Console.ForegroundColor = defaultColor;
if( !IsInteractive )
Environment.Exit( haveError ? 1 : 0 );
}
#endregion
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.