@@ -5,20 +5,23 @@ import * as fs from "fs";
55import { WebSocketClient } from "./nodeclient/WebSocketClient" ;
66import { SigninMessage , Message } from "./nodeclient/Message" ;
77import { Config } from "./Config" ;
8- import { logger , StopService , StartService , loadenv , copyenv , envfilename , servicename , isOpenFlow } from "./nodeclient/cliutil" ;
8+ import { logger , StopService , StartService , RemoveService , InstallService , RunService , loadenv , envfilename , envfilepathname , servicename , isOpenFlow } from "./nodeclient/cliutil" ;
99
1010const optionDefinitions = [
1111 { name : 'verbose' , alias : 'v' , type : Boolean } ,
12- { name : 'config' , alias : 'c' , type : Boolean } ,
12+ { name : 'authenticate' , alias : 'a' , type : Boolean } ,
13+ { name : 'init' , type : Boolean } ,
1314 { name : 'install' , alias : 'i' , type : Boolean } ,
1415 { name : 'uninstall' , alias : 'u' , type : Boolean } ,
1516 { name : 'start' , type : Boolean } ,
1617 { name : 'stop' , type : Boolean } ,
1718 { name : 'run' , type : Boolean } ,
18- { name : 'name' , type : String , defaultOption : true }
19+ { name : 'name' , type : String , defaultOption : true } ,
20+ { name : 'config' , type : String } ,
21+ { name : 'inspect' , type : String }
1922]
2023const commandLineArgs = require ( 'command-line-args' ) ;
21- const service = require ( "os-service" ) ;
24+
2225const path = require ( 'path' ) ;
2326const readlineSync = require ( 'readline-sync' ) ;
2427const envfile = require ( 'envfile' )
@@ -28,9 +31,14 @@ const envfile = require('envfile')
2831var options = null ;
2932try {
3033 options = commandLineArgs ( optionDefinitions ) ;
31- if ( options . name == null || options . name == "" ) throw new Error ( "Name is mandatory" ) ;
32- ( servicename as any ) = options . name ;
33- ( envfilename as any ) = options . name + ".env" ;
34+ if ( ! options . init ) {
35+ if ( options . name == null || options . name == "" ) throw new Error ( "Name is mandatory" ) ;
36+ if ( options . name . endsWith ( ".env" ) ) options . name = options . name . substring ( 0 , options . name . length - 4 ) ;
37+ ( servicename as any ) = options . name ;
38+ ( envfilename as any ) = options . name + ".env" ;
39+ ( envfilepathname as any ) = path . join ( process . cwd ( ) , envfilename ) ;
40+ if ( options . config != null && options . config != "" ) ( envfilepathname as any ) = options . config ;
41+ }
3442} catch ( error ) {
3543 logger . info ( error . message ) ;
3644 printusage ( ) ;
@@ -71,46 +79,50 @@ function getToken(): Promise<string> {
7179
7280
7381async function doit ( ) {
74- if ( options . config == true ) {
75- StopService ( servicename ) ;
76- service . remove ( servicename , function ( error ) {
77- } ) ;
82+ if ( options . init ) {
83+ var files = fs . readdirSync ( path . join ( __dirname , ".." ) )
84+ for ( var i = 0 ; i < files . length ; i ++ ) {
85+ var filename = files [ i ] ;
86+ if ( path . extname ( filename ) == '.env' ) {
87+ var target = path . join ( process . cwd ( ) , filename ) ;
88+ if ( ! fs . existsSync ( target ) ) {
89+ console . log ( "Creating " + filename ) ;
90+ filename = path . join ( __dirname , ".." , filename ) ;
91+ fs . copyFileSync ( filename , target ) ;
92+
93+ let parsedFile = envfile . parse ( fs . readFileSync ( target ) ) ;
94+ parsedFile . logpath = process . cwd ( ) ;
95+ fs . writeFileSync ( target , envfile . stringify ( parsedFile ) ) ;
7896
97+ } else {
98+ console . log ( "Skipping " + filename + " already exists." ) ;
99+ }
100+ }
101+ }
102+ } else if ( options . authenticate == true ) {
103+ StopService ( servicename ) ;
104+ RemoveService ( servicename ) ;
79105 try {
80- var dir = __dirname . toLowerCase ( ) ;
81- logger . error ( __dirname ) ;
82- // Request a token, to add into the config
83106 logger . info ( "isOpenFlow: " + isOpenFlow ( ) ) ;
84107 if ( ! isOpenFlow ( ) ) {
85- copyenv ( ) ;
108+ loadenv ( ) ;
86109 let jwt = await getToken ( ) ;
87- logger . info ( "Received token, update " + envfilename + " file" ) ;
88- let localenv = path . join ( process . cwd ( ) , envfilename ) ;
89- let parsedFile = envfile . parse ( fs . readFileSync ( localenv ) ) ;
110+ let parsedFile = envfile . parse ( fs . readFileSync ( envfilepathname ) ) ;
90111 parsedFile . jwt = jwt ;
91- fs . writeFileSync ( localenv , envfile . stringify ( parsedFile ) ) ;
112+ fs . writeFileSync ( envfilepathname , envfile . stringify ( parsedFile ) ) ;
92113 }
93- copyenv ( ) ;
94- logger . info ( "Install service " + servicename ) ;
95- service . add ( servicename , { programArgs : [ "--run" , options . name ] } , function ( error ) {
96- if ( error ) logger . info ( error . message ) ;
97- StartService ( servicename ) ;
98- } ) ;
114+ loadenv ( ) ;
115+ InstallService ( servicename , envfilepathname ) ;
99116 logger . info ( "Quit" ) ;
100117 } catch ( error ) {
101118 logger . error ( error ) ;
102119 }
103120 } else if ( options . install == true ) {
104121 loadenv ( ) ;
105- service . add ( servicename , { programArgs : [ "--run" , options . name ] } , function ( error ) {
106- if ( error ) logger . info ( error . message ) ;
107- StartService ( servicename ) ;
108- } ) ;
122+ InstallService ( servicename , envfilepathname ) ;
109123 } else if ( options . uninstall == true ) {
110124 StopService ( servicename ) ;
111- service . remove ( servicename , function ( error ) {
112- if ( error ) logger . info ( error . message ) ;
113- } ) ;
125+ RemoveService ( servicename ) ;
114126 } else if ( options . start == true ) {
115127 loadenv ( ) ;
116128 StartService ( servicename ) ;
@@ -119,16 +131,12 @@ async function doit() {
119131 } else if ( options . run == true ) {
120132 loadenv ( ) ;
121133 logger . info ( "Starting as service " + servicename ) ;
122- service . run ( function ( ) {
123- logger . info ( "Service" + servicename + " stopping" ) ;
124- service . stop ( 0 ) ;
125- } ) ;
126- logger . info ( "Run " + servicename ) ;
134+ RunService ( null ) ;
127135 var index = index = path . join ( __dirname , "/index.js" ) ;
128136 if ( ! fs . existsSync ( index ) ) {
129137 index = path . join ( __dirname , "dist" , "/index.js" ) ;
130138 }
131- logger . info ( "run: " + index )
139+ logger . info ( "run: " + index ) ;
132140 require ( index ) ;
133141 } else {
134142 printusage ( ) ;
@@ -139,7 +147,8 @@ async function doit() {
139147
140148function printusage ( ) {
141149 if ( ! isOpenFlow ( ) ) {
142- console . log ( "openflow-nodered-cli [--install][--uninstall][--config][--start][--stop] name" ) ;
150+ console . log ( "openflow-nodered-cli [--init][--install][--uninstall][--config][--start][--stop] name" ) ;
151+ console . log ( " --init - Create sample environment files for running nodered" ) ;
143152 console . log ( " --install - Install openflow as an service that runs at boot" ) ;
144153 console . log ( " --uninstall - Uninstalls service, if openflow has been installed as an service" ) ;
145154 console . log ( " --config - Prompt for credentials and create config" ) ;
@@ -150,7 +159,8 @@ function printusage() {
150159 console . log ( "source directory" ) ;
151160 return ;
152161 }
153- console . log ( "openflow-cli [--install][--uninstall][--start][--stop] name" ) ;
162+ console . log ( "openflow-cli [--init][--install][--uninstall][--start][--stop] name" ) ;
163+ console . log ( " --init - Create a sample environment file for running openflow" ) ;
154164 console . log ( " --install - Install openflow as an service that runs at boot" ) ;
155165 console . log ( " --uninstall - Uninstalls service, if openflow has been installed as an service" ) ;
156166 console . log ( " --start - Will start the service with the given name" ) ;
0 commit comments