Skip to content

Commit c66e8c6

Browse files
committed
Node/cli: added service install features (#4)
1 parent 1ce48d3 commit c66e8c6

8 files changed

Lines changed: 284 additions & 20 deletions

File tree

Synapse.NodeService.HttpClient/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
[assembly: AssemblyVersion( "0.1.0.0" )]
36-
[assembly: AssemblyFileVersion( "0.1.17036.1" )]
36+
[assembly: AssemblyFileVersion( "0.1.17036.0" )]

Synapse.NodeService.cli/Program.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Threading.Tasks;
3+
using System.Text;
44

5-
using Synapse.Core;
6-
using Synapse.Services;
75

86
namespace Synapse.Services.NodeService.Cli
97
{
@@ -116,20 +114,32 @@ protected virtual void RunServiceAction(string[] args)
116114
case "install":
117115
{
118116
string message = string.Empty;
119-
if( !InstallUtility.InstallService( install: true, message: out message ) )
117+
bool error = false;
118+
Dictionary<string, string> values = ParseCmdLine( args, 2, ref error, true );
119+
if( !InstallUtility.InstallAndStartService( configValues: values, message: out message ) )
120+
{
120121
Console.WriteLine( message );
122+
Environment.Exit( 1 );
123+
}
124+
121125
break;
122126
}
123127
case "uninstall":
124128
{
125129
string message = string.Empty;
126-
if( !InstallUtility.InstallService( install: false, message: out message ) )
130+
if( !InstallUtility.StopAndUninstallService( out message ) )
131+
{
127132
Console.WriteLine( message );
133+
Environment.Exit( 1 );
134+
}
135+
128136
break;
129137
}
130138
default:
131139
{
132140
WriteHelpAndExit( "Unknown service action." );
141+
Environment.Exit( 1 );
142+
133143
break;
134144
}
135145
}
@@ -139,6 +149,13 @@ protected virtual void RunServiceAction(string[] args)
139149
#region Help
140150
protected override void WriteHelpAndExit(string errorMessage = null)
141151
{
152+
Dictionary<string, string> cdf = SynapseNodeConfig.GetConfigDefaultValues();
153+
StringBuilder df = new StringBuilder();
154+
df.AppendFormat( "{0,-15}- Optional install args, use argname:value. Defaults shown.\r\n", "" );
155+
foreach( string key in cdf.Keys )
156+
df.AppendLine( $" - {key}:{cdf[key]}" );
157+
df.AppendLine( $" - Run:true (Optionally Starts the Windows Service)\r\n" );
158+
142159
bool haveError = !string.IsNullOrWhiteSpace( errorMessage );
143160

144161
ConsoleColor defaultColor = Console.ForegroundColor;
@@ -155,7 +172,8 @@ protected override void WriteHelpAndExit(string errorMessage = null)
155172
Console.WriteLine( " service{0,-6}Install/Uninstall the Windows Service, or Run the Service", "" );
156173
Console.WriteLine( "{0,-15}as a cmdline-hosted daemon.", "" );
157174
Console.WriteLine( "{0,-15}- Commands: install|uninstall|run", "" );
158-
Console.WriteLine( "{0,-15}- Example: synapse.node.cli service run\r\n", "" );
175+
Console.WriteLine( "{0,-15}- Example: synapse.node.cli service run", "" );
176+
Console.WriteLine( df.ToString() );
159177
Console.WriteLine( " httpAction{0,-3}Execute a command, optionally specify URL.", "" );
160178
Console.WriteLine( "{0,-15}Parm help: synapse.node.cli {1}httpAction{2} help.\r\n", "", "{", "}" );
161179
Console.WriteLine( " - httpActions:", "" );

Synapse.NodeService.cli/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
[assembly: AssemblyVersion( "0.1.0.0" )]
36-
[assembly: AssemblyFileVersion( "0.1.17036.1" )]
36+
[assembly: AssemblyFileVersion( "0.1.17036.0" )]

Synapse.NodeService/Classes/SynapseNodeConfig.cs

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34

45
using Synapse.Core.Utilities;
@@ -18,14 +19,81 @@ public SynapseNodeConfig()
1819
public static readonly string CurrentPath = $"{Path.GetDirectoryName( typeof( SynapseNodeConfig ).Assembly.Location )}";
1920
public static readonly string FileName = $"{Path.GetDirectoryName( typeof( SynapseNodeConfig ).Assembly.Location )}\\Synapse.Node.config.yaml";
2021

21-
public int MaxServerThreads { get; set; } = 10;
22+
23+
public string ServiceName { get; set; } = "Synapse.Node";
24+
internal bool HasServiceName { get { return !string.IsNullOrWhiteSpace( ServiceName ); } }
25+
26+
public string ServiceDisplayName { get; set; } = "Synapse Node";
27+
internal bool HasServiceDisplayName { get { return !string.IsNullOrWhiteSpace( ServiceDisplayName ); } }
28+
29+
public int MaxServerThreads { get; set; } = 0;
30+
internal string MaxServerThreadsString { get; set; } = "0";
31+
internal bool TestSetMaxServerThreadsString
32+
{
33+
get
34+
{
35+
int threads = MaxServerThreads;
36+
bool ok = int.TryParse( MaxServerThreadsString, out threads );
37+
if( ok )
38+
MaxServerThreads = threads;
39+
return ok;
40+
}
41+
}
42+
2243
public string AuditLogRootPath { get; set; } = @".\Logs";
44+
internal bool HasAuditLogRootPath { get { return !string.IsNullOrWhiteSpace( AuditLogRootPath ); } }
45+
2346
public string ServiceLogRootPath { get; set; } = @".\Logs";
47+
internal bool HasServiceLogRootPath { get { return !string.IsNullOrWhiteSpace( ServiceLogRootPath ); } }
48+
2449
public string Log4NetConversionPattern { get; set; } = "%d{ISO8601}|%-5p|(%t)|%m%n";
50+
internal bool HasLog4NetConversionPattern { get { return !string.IsNullOrWhiteSpace( Log4NetConversionPattern ); } }
51+
2552
public bool SerializeResultPlan { get; set; } = true;
53+
internal string SerializeResultPlanString { get; set; } = "true";
54+
internal bool TestSetSerializeResultPlanString
55+
{
56+
get
57+
{
58+
bool v = SerializeResultPlan;
59+
bool ok = bool.TryParse( SerializeResultPlanString, out v );
60+
if( ok )
61+
SerializeResultPlan = v;
62+
return ok;
63+
}
64+
}
65+
2666
public bool ValidatePlanSignature { get; set; } = true;
67+
internal string ValidatePlanSignatureString { get; set; } = "true";
68+
internal bool TestSetValidatePlanSignatureString
69+
{
70+
get
71+
{
72+
bool v = ValidatePlanSignature;
73+
bool ok = bool.TryParse( ValidatePlanSignatureString, out v );
74+
if( ok )
75+
ValidatePlanSignature = v;
76+
return ok;
77+
}
78+
}
79+
2780
public string ControllerServiceUrl { get; set; } = "http://localhost:8008/synapse/execute";
28-
public string WebApiPort { get; set; } = "8000";
81+
internal bool HasControllerServiceUrl { get { return !string.IsNullOrWhiteSpace( ControllerServiceUrl ); } }
82+
83+
public int WebApiPort { get; set; } = 8000;
84+
internal string WebApiPortString { get; set; } = "8000";
85+
internal bool TestSetWebApiPortString
86+
{
87+
get
88+
{
89+
int port = WebApiPort;
90+
bool ok = int.TryParse( WebApiPortString, out port );
91+
if( ok )
92+
WebApiPort = port;
93+
return ok;
94+
}
95+
}
96+
2997

3098
public string GetResolvedAuditLogRootPath()
3199
{
@@ -86,5 +154,105 @@ public static SynapseNodeConfig Deserialze()
86154

87155
return YamlHelpers.DeserializeFile<SynapseNodeConfig>( FileName );
88156
}
157+
158+
public static Dictionary<string, string> GetConfigDefaultValues()
159+
{
160+
Dictionary<string, string> values = new Dictionary<string, string>();
161+
162+
SynapseNodeConfig c = new SynapseNodeConfig();
163+
values[nameof( c.ServiceName )] = c.ServiceName;
164+
values[nameof( c.ServiceDisplayName )] = c.ServiceDisplayName;
165+
values[nameof( c.MaxServerThreads )] = c.MaxServerThreads.ToString();
166+
values[nameof( c.AuditLogRootPath )] = c.AuditLogRootPath;
167+
values[nameof( c.ServiceLogRootPath )] = c.ServiceLogRootPath;
168+
values[nameof( c.Log4NetConversionPattern )] = c.Log4NetConversionPattern;
169+
values[nameof( c.SerializeResultPlan )] = c.SerializeResultPlan.ToString();
170+
values[nameof( c.ValidatePlanSignature )] = c.ValidatePlanSignature.ToString();
171+
values[nameof( c.ControllerServiceUrl )] = c.ControllerServiceUrl;
172+
values[nameof( c.WebApiPort )] = c.WebApiPort.ToString();
173+
174+
return values;
175+
}
176+
177+
public static SynapseNodeConfig Configure(Dictionary<string, string> values)
178+
{
179+
SynapseNodeConfig c = new SynapseNodeConfig();
180+
181+
if( values.ContainsKey( nameof( c.ServiceName ).ToLower() ) )
182+
c.ServiceName = values[nameof( c.ServiceName ).ToLower()];
183+
184+
if( values.ContainsKey( nameof( c.ServiceDisplayName ).ToLower() ) )
185+
c.ServiceDisplayName = values[nameof( c.ServiceDisplayName ).ToLower()];
186+
187+
if( values.ContainsKey( nameof( c.MaxServerThreads ).ToLower() ) )
188+
c.MaxServerThreadsString = values[nameof( c.MaxServerThreads ).ToLower()];
189+
190+
if( values.ContainsKey( nameof( c.AuditLogRootPath ).ToLower() ) )
191+
c.AuditLogRootPath = values[nameof( c.AuditLogRootPath ).ToLower()];
192+
193+
if( values.ContainsKey( nameof( c.ServiceLogRootPath ).ToLower() ) )
194+
c.ServiceLogRootPath = values[nameof( c.ServiceLogRootPath ).ToLower()];
195+
196+
if( values.ContainsKey( nameof( c.Log4NetConversionPattern ).ToLower() ) )
197+
c.Log4NetConversionPattern = values[nameof( c.Log4NetConversionPattern ).ToLower()];
198+
199+
if( values.ContainsKey( nameof( c.SerializeResultPlan ).ToLower() ) )
200+
c.SerializeResultPlanString = values[nameof( c.SerializeResultPlan ).ToLower()];
201+
202+
if( values.ContainsKey( nameof( c.ValidatePlanSignature ).ToLower() ) )
203+
c.ValidatePlanSignatureString = values[nameof( c.ValidatePlanSignature ).ToLower()];
204+
205+
if( values.ContainsKey( nameof( c.ControllerServiceUrl ).ToLower() ) )
206+
c.ControllerServiceUrl = values[nameof( c.ControllerServiceUrl ).ToLower()];
207+
208+
if( values.ContainsKey( nameof( c.WebApiPort ).ToLower() ) )
209+
c.WebApiPortString = values[nameof( c.WebApiPort ).ToLower()];
210+
211+
return Configure( c );
212+
}
213+
214+
public static SynapseNodeConfig Configure(SynapseNodeConfig value)
215+
{
216+
//initialize with defaults
217+
SynapseNodeConfig config = new SynapseNodeConfig();
218+
//ovrride defaults with file values
219+
if( File.Exists( FileName ) )
220+
config = YamlHelpers.DeserializeFile<SynapseNodeConfig>( FileName );
221+
222+
//configure with anything provided
223+
if( value.HasServiceName && !(value.ServiceName == config.ServiceName) )
224+
config.ServiceName = value.ServiceName;
225+
226+
if( value.HasServiceDisplayName && !(value.ServiceDisplayName == config.ServiceDisplayName) )
227+
config.ServiceDisplayName = value.ServiceDisplayName;
228+
229+
if( value.TestSetMaxServerThreadsString && !(value.MaxServerThreads == config.MaxServerThreads) )
230+
config.MaxServerThreads = value.MaxServerThreads;
231+
232+
if( value.HasAuditLogRootPath && !(value.AuditLogRootPath == config.AuditLogRootPath) )
233+
config.AuditLogRootPath = value.AuditLogRootPath;
234+
235+
if( value.HasServiceLogRootPath && !(value.ServiceLogRootPath == config.ServiceLogRootPath) )
236+
config.ServiceLogRootPath = value.ServiceLogRootPath;
237+
238+
if( value.HasLog4NetConversionPattern && !(value.Log4NetConversionPattern == config.Log4NetConversionPattern) )
239+
config.Log4NetConversionPattern = value.Log4NetConversionPattern;
240+
241+
if( value.TestSetSerializeResultPlanString && !(value.SerializeResultPlan == config.SerializeResultPlan) )
242+
config.SerializeResultPlan = value.SerializeResultPlan;
243+
244+
if( value.TestSetValidatePlanSignatureString && !(value.ValidatePlanSignature == config.ValidatePlanSignature) )
245+
config.ValidatePlanSignature = value.ValidatePlanSignature;
246+
247+
if( value.HasControllerServiceUrl && !(value.ControllerServiceUrl == config.ControllerServiceUrl) )
248+
config.ControllerServiceUrl = value.ControllerServiceUrl;
249+
250+
if( value.TestSetWebApiPortString && !(value.WebApiPort == config.WebApiPort) )
251+
config.WebApiPort = value.WebApiPort;
252+
253+
config.Serialize();
254+
255+
return config;
256+
}
89257
}
90258
}

Synapse.NodeService/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
// by using the '*' as shown below:
3636
// [assembly: AssemblyVersion("1.0.*")]
3737
[assembly: AssemblyVersion( "0.1.0.0" )]
38-
[assembly: AssemblyFileVersion( "0.1.17036.1" )]
38+
[assembly: AssemblyFileVersion( "0.1.17036.0" )]

Synapse.NodeService/SynapseNodeService.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.ServiceModel;
45
using System.ServiceProcess;
5-
using System.Windows.Forms;
6+
using System.Text;
67
using System.Threading;
8+
using System.Windows.Forms;
9+
10+
using log4net;
711

12+
using Synapse.Common.CmdLine;
813
using Synapse.Core.DataAccessLayer;
914
using Synapse.Services.Common;
1015

11-
using log4net;
12-
1316
namespace Synapse.Services
1417
{
1518
public partial class SynapseNodeService : ServiceBase
@@ -25,6 +28,8 @@ public SynapseNodeService()
2528
Config = SynapseNodeConfig.Deserialze();
2629

2730
InitializeComponent();
31+
32+
this.ServiceName = Config.ServiceName;
2833
}
2934

3035
public static void Main(string[] args)
@@ -55,9 +60,16 @@ static void InstallService(string[] args)
5560

5661
string arg0 = args[0].ToLower();
5762
if( arg0 == "/install" || arg0 == "/i" )
58-
ok = InstallUtility.InstallService( install: true, message: out message );
63+
{
64+
bool error = false;
65+
Dictionary<string, string> values = CmdLineUtilities.ParseCmdLine( args, 1, ref error, ref message, null );
66+
if( !error )
67+
ok = InstallUtility.InstallAndStartService( configValues: values, message: out message );
68+
}
5969
else if( arg0 == "/uninstall" || arg0 == "/u" )
60-
ok = InstallUtility.InstallService( install: false, message: out message );
70+
{
71+
ok = InstallUtility.StopAndUninstallService( out message );
72+
}
6173

6274
if( !ok )
6375
WriteHelpAndExit( message );
@@ -219,7 +231,14 @@ static void WriteHelpAndExit(string errorMessage = null)
219231
bool haveError = !string.IsNullOrWhiteSpace( errorMessage );
220232

221233
MessageBoxIcon icon = MessageBoxIcon.Information;
222-
string msg = $"synapse.node.exe, Version: {typeof( SynapseNodeService ).Assembly.GetName().Version}\r\nSyntax:\r\n synapse.node.exe /install | /uninstall";
234+
Dictionary<string, string> cdf = SynapseNodeConfig.GetConfigDefaultValues();
235+
StringBuilder df = new StringBuilder();
236+
df.AppendLine( $"Optional args for configuring /install, use argname:value. Defaults shown." );
237+
foreach( string key in cdf.Keys )
238+
df.AppendLine( $" - {key}:{cdf[key]}" );
239+
df.AppendLine( $" - Run:true (Optionally Starts the Windows Service)" );
240+
241+
string msg = $"synapse.node.exe, Version: {typeof( SynapseNodeService ).Assembly.GetName().Version}\r\nSyntax:\r\n synapse.node.exe /install | /uninstall\r\n\r\n{df.ToString()}";
223242

224243
if( haveError )
225244
{

0 commit comments

Comments
 (0)