@@ -12,8 +12,10 @@ import (
1212
1313 "gorm.io/gorm"
1414 "github.com/riotpot/pkg/services"
15+ "github.com/riotpot/pkg/profiles/ports"
1516 "github.com/riotpot/internal/greeting"
1617 "github.com/riotpot/tools/environ"
18+ "github.com/riotpot/tools/arrays"
1719)
1820
1921type Autopilot struct {
@@ -26,7 +28,10 @@ type Autopilot struct {
2628 DB * gorm.DB
2729
2830 loaded_plugins []string
31+ loaded_containers []string
2932 plugins_to_run []string
33+ contianers_to_run [] string
34+ interaction_mode string
3035}
3136
3237// Method to start the autopilot.
@@ -41,53 +46,83 @@ func (a *Autopilot) Start() {
4146 a .wg = sync.WaitGroup {}
4247 a .wg .Add (1 )
4348
44- // register all the services
45- a .RegisterServices ()
49+ // register all the services plugins
50+ a .RegisterPlugins ()
51+ a .DiscoverImages ()
52+ _ = environ .CheckDockerExists ("mongodb" )
4653
4754 // loads the services which are available for user to run
4855 a .loaded_plugins = a .services .GetServicesNames (a .services .GetServices ())
4956
50-
5157 // set the plugins to run from the config file
5258 a .plugins_to_run = a .Settings .Riotpot .Start
5359
5460 // check if the build is local or containerized
5561 if a .Settings .Riotpot .Local_build_on == "1" {
56- // check if user want to run via config file or manually input
57- // plugins to run.
58- // _ = a.CheckInteractionMode()
59- running_mode_decision := a .CheckRunningMode ()
60-
61- // based on the user decision set the plugin running list
62- if ! running_mode_decision {
63- // user decided to provide plugins manually
64- a .plugins_to_run = a .GetPluginsFromUser ()
62+ a .interaction_mode = a .CheckInteractionMode ()
63+
64+ if a .interaction_mode == "low" {
65+ // check if user want to run via config file or manually input
66+ // plugins to run.
67+ // _ = a.CheckInteractionMode()
68+ running_mode_decision := a .CheckRunningMode ()
69+
70+ // based on the user decision set the plugin running list
71+ if ! running_mode_decision {
72+ fmt .Printf ("Plugins available to run " )
73+ fmt .Println (a .loaded_plugins )
74+
75+ // user decided to provide plugins manually
76+ a .plugins_to_run = a .GetPluginsFromUser ()
77+ }
78+ } else if a .interaction_mode == "high" {
79+ fmt .Printf ("Docker images available to run " )
80+ fmt .Println (a .loaded_containers )
81+
82+ running_mode_decision := a .CheckContainersRunMode ()
83+ if ! running_mode_decision {
84+ fmt .Printf ("Containers available to run " )
85+ fmt .Println (a .Settings .GetDockerImages ())
86+
87+ // user decided to provide contianers manually
88+ a .contianers_to_run = a .GetContainersFromUser ()
89+ }
90+
91+
92+ } else {
93+ fmt .Printf ("\n Plugins available to run " )
94+ fmt .Println (a .loaded_plugins )
95+ fmt .Printf ("\n " )
96+ fmt .Printf ("Docker images available to run " )
97+ fmt .Println (a .loaded_containers )
98+ fmt .Printf ("\n " )
99+
65100 }
66101 }
67102
68- fmt .Printf ("Plugins to run " )
69- fmt .Println (a .plugins_to_run )
103+ // fmt.Printf("Plugins to run ")
104+ // fmt.Println(a.plugins_to_run)
70105
71- // cmd, err := exec.Command("/bin/sh", "bash.sh").Output()
72- // cmd, err := exec.LookPath("go")
73- // if err != nil {
74- // log.Fatalf("[!] Error: %v", err)
75- // }
106+ // // cmd, err := exec.Command("/bin/sh", "bash.sh").Output()
107+ // // cmd, err := exec.LookPath("go")
108+ // // if err != nil {
109+ // // log.Fatalf("[!] Error: %v", err)
110+ // // }
76111
77- // output := string(cmd)
78- // fmt.Println(output)
112+ // // output := string(cmd)
113+ // // fmt.Println(output)
79114
80- server := environ .CheckPortBusy ("tcp" , ":22" )
81- if server != true {
82- fmt .Println ("Port is busy" )
83- }
84- fmt .Println (server )
115+ // server := environ.CheckPortBusy("tcp", ":22")
116+ // if server != true {
117+ // fmt.Println("Port is busy")
118+ // }
119+ // fmt.Println(server)
85120
86- servicePath , exists := environ .GetPath ("glider" )
87- if exists {
88- _ = environ . ExecuteBackgroundCmd ( servicePath , "aaa " )
89- _ = environ .ExecuteCmd (servicePath , "aaa " )
90- }
121+ // _ , exists := environ.GetPath("glider")
122+ // if exists {
123+ // a.ValidateDefaultDockerContext("docker-test ")
124+ // _ = environ.ExecuteCmd("go" , "version ")
125+ // }
91126
92127 // fmt.Println(demo1)
93128
@@ -124,8 +159,8 @@ func (a *Autopilot) available(name string, port int) (available bool) {
124159 return true
125160}
126161
127- // Register the services
128- func (a * Autopilot ) RegisterServices () {
162+ // Register the services plugins
163+ func (a * Autopilot ) RegisterPlugins () {
129164 a .services = services.Services {}
130165
131166 service_paths := a .services .Autodiscover (a .Settings .Riotpot .Local_build_on )
@@ -136,6 +171,15 @@ func (a *Autopilot) RegisterServices() {
136171 a .services .AddDB (a .DB )
137172}
138173
174+ // Discover the docker images available
175+ func (a * Autopilot ) DiscoverImages () {
176+ a .loaded_containers = a .Settings .GetDockerImages ()
177+ fmt .Printf ("[+] Found %d docker images \n " , len (a .loaded_containers ))
178+ fmt .Printf ("[+] Allowed Docker images " )
179+ fmt .Println (a .loaded_containers )
180+
181+ }
182+
139183// Load the greeting
140184func (a * Autopilot ) Greeting () {
141185 a .greeting = greeting.Greet {
@@ -146,12 +190,6 @@ func (a *Autopilot) Greeting() {
146190 a .greeting .Greeting ()
147191}
148192
149- // Converts the text separated by spaces into list items
150- func (a * Autopilot ) TextToList (in string ) (out []string ) {
151- out = strings .Fields (in )
152- return out
153- }
154-
155193// Reads the input from the terminal, returns the string
156194func (a * Autopilot ) ReadInput () (text string ) {
157195 reader := bufio .NewReader (os .Stdin )
@@ -181,9 +219,27 @@ func (a *Autopilot) CheckRunningMode() (decision bool) {
181219 }
182220}
183221
222+ // Checks if the user wants to run containers manually
223+ func (a * Autopilot ) CheckContainersRunMode () (decision bool ) {
224+ fmt .Print ("Run containers from configuation file? [y/n]" )
225+
226+ for {
227+ response := a .ReadInput ()
228+ response = strings .ToLower (strings .TrimSpace (response ))
229+
230+ if response == "y" || response == "yes" {
231+ return true
232+ } else if response == "n" || response == "no" {
233+ return false
234+ } else {
235+ fmt .Printf ("Please type Yes(y) or No(n) only\n " )
236+ }
237+ }
238+ }
239+
184240// Checks in which mode user wants to run RIoTPot
185241func (a * Autopilot ) CheckInteractionMode () (decision string ) {
186- fmt .Print ("\n Select RIoTPot mode, Low-interaction mode, High-interaction mode or Hybrid-mode? [l,h,hy] [low, high, hybrid]" )
242+ fmt .Printf ("\n Select RIoTPot mode, Low-interaction mode, High-interaction mode or Hybrid-mode? [l,h,hy] [low, high, hybrid] \n " )
187243
188244 for {
189245 response := a .ReadInput ()
@@ -201,11 +257,11 @@ func (a *Autopilot) CheckInteractionMode() (decision string) {
201257 }
202258}
203259
204- // Validates if the plugins inputed by the users match the available plugins
260+ // Validates if the plugins inputed by the user matches the available plugins
205261// TODO: print all the invalid plugins not just the first one encountered
206262func (a * Autopilot ) ValidatePlugin (input_plugins []string ) (validated bool ){
207263 for _ , plugin := range input_plugins {
208- validated := a . services . ValidatePluginByName (strings .Title (plugin ), a . loaded_plugins )
264+ validated := arrays . Contains ( a . loaded_plugins , strings . Title (strings .ToLower (plugin )) )
209265 if ! validated {
210266 fmt .Printf ("\n [-] Entered plugin \" %s\" doesn't exist, please enter plugins again... \n " , plugin )
211267 return false
@@ -214,15 +270,27 @@ func (a *Autopilot) ValidatePlugin(input_plugins []string) (validated bool){
214270 return true
215271}
216272
273+ // Validates if the contianers inputed by the users match the loaded containers
274+ // TODO: print all the invalid containers not just the first one encountered
275+ func (a * Autopilot ) ValidateContianers (input_containers []string ) (validated bool ){
276+ for _ , container := range input_containers {
277+ validated := arrays .Contains ( a .loaded_containers , strings .ToLower (container ))
278+ if ! validated {
279+ fmt .Printf ("\n [-] Entered container \" %s\" doesn't exist, please enter plugins again... \n " , container )
280+ return false
281+ }
282+ }
283+ return true
284+ }
285+
217286// Gives which plugins user wants to load in RIoTPot
218287func (a * Autopilot ) GetPluginsFromUser () (plugins []string ) {
219288 for {
220- fmt .Printf ("\n Plugins available to run " )
221- fmt .Println (a .loaded_plugins )
222289 fmt .Print ("Enter the plugins to run separated by space: " )
223290
224- text := a .ReadInput ()
225- plugins = a .TextToList (text )
291+ input := a .ReadInput ()
292+ plugins = arrays .StringToArray (input )
293+
226294 validated := a .ValidatePlugin (plugins )
227295 if ! validated {
228296 continue
@@ -231,3 +299,36 @@ func (a *Autopilot) GetPluginsFromUser() (plugins []string) {
231299 }
232300 return plugins
233301}
302+
303+ // Gives which plugins user wants to load in RIoTPot
304+ func (a * Autopilot ) GetContainersFromUser () (containers []string ) {
305+ for {
306+ fmt .Print ("Enter the contianers to run separated by space: " )
307+
308+ input := a .ReadInput ()
309+ containers = arrays .StringToArray (input )
310+ validated := a .ValidateContianers (containers )
311+
312+ if ! validated {
313+ continue
314+ }
315+ break
316+ }
317+
318+ return containers
319+ }
320+
321+
322+ // Validates if the given docker context exists and set to default
323+ func (a * Autopilot ) ValidateDefaultDockerContext (to_check string ) {
324+ cmd_output := environ .ExecuteCmd ("docker" , "context" , "ls" )
325+ cmd_out_slice := arrays .StringToArray (cmd_output )
326+ val_position := arrays .GetItemPosition (cmd_out_slice , to_check )
327+
328+ if val_position == - 1 {
329+ log .Fatalf ("Docker context %q, not found" , to_check )
330+ }
331+ if cmd_out_slice [val_position + 1 ] != "*" {
332+ log .Fatalf ("Docker context %q, is not set to default" , to_check )
333+ }
334+ }
0 commit comments