Skip to content

Commit 9954891

Browse files
committed
progress sync, level structure added
1 parent c07b75a commit 9954891

7 files changed

Lines changed: 217 additions & 60 deletions

File tree

configs/samples/configuration.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ riotpot:
1313
# list of services desired to be loaded and run at start, and `emulators` with
1414
# the list of available services to the app.
1515
autod: false
16-
local_build_on: 0
16+
local_build_on: 1
1717

1818
# The name of the services is the name of the folder in which the plugin
1919
# is stored, inside the `pkg/` folder.
@@ -31,6 +31,10 @@ riotpot:
3131
- coapd
3232
- modbusd
3333

34+
images:
35+
- mqtt, eclipse-mosquitto
36+
- coap, COAP_IMAGE
37+
3438
# Contains a list of available services in the application.
3539
# This gives the user the ability to navigate or load
3640
# just the emulators that appear in this list, wether or not

internal/configuration/autopilot.go

Lines changed: 147 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1921
type 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("\nPlugins 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
140184
func (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
156194
func (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
185241
func (a *Autopilot) CheckInteractionMode() (decision string) {
186-
fmt.Print("\nSelect RIoTPot mode, Low-interaction mode, High-interaction mode or Hybrid-mode? [l,h,hy] [low, high, hybrid]")
242+
fmt.Printf("\nSelect 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
206262
func (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
218287
func (a *Autopilot) GetPluginsFromUser() (plugins []string) {
219288
for {
220-
fmt.Printf("\nPlugins 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+
}

internal/configuration/configuration.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"strings"
1010

1111
"github.com/gobuffalo/packr"
12-
"github.com/riotpot/tools/arrays"
1312
environ "github.com/riotpot/tools/environ"
1413
errors "github.com/riotpot/tools/errors"
14+
arrays "github.com/riotpot/tools/arrays"
1515

1616
"gopkg.in/yaml.v3"
1717
)
@@ -52,6 +52,15 @@ func (conf *Settings) Load() (err error) {
5252
return err
5353
}
5454

55+
// Retrieve the image name from Images tag
56+
func (conf *Settings) GetDockerImages() (images []string) {
57+
for _, val := range conf.Riotpot.Images {
58+
images = append(images, strings.TrimSuffix(arrays.StringToArray(val)[0], ","))
59+
}
60+
61+
return images
62+
}
63+
5564
// Stores the configuration into the given path in `.yml` format.
5665
func (conf *Settings) Save(path string) (err error) {
5766
// marshal the content of the configuration into a `.yaml` document
@@ -146,8 +155,12 @@ type ConfigRiotpot struct {
146155
Emulators []string
147156
// List of emulators that must be run at start
148157
Start []string
149-
// Varoable to check if the run is for local build or not
158+
// Variable to check if the run is for local build or not
150159
Local_build_on string
160+
// Available docker images along with docker name
161+
Images []string
162+
// Available docker images without docker name
163+
Images_to_run []string
151164
}
152165

153166
// Database configuration structure. It gives an interface to load and access specific databases.

pkg/plugin/coapd/coapd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/plgd-dev/go-coap/v2/mux"
2222
"github.com/riotpot/pkg/profiles"
2323
"github.com/riotpot/pkg/services"
24+
"github.com/riotpot/pkg/profiles/ports"
2425
)
2526

2627
var Name string
@@ -32,7 +33,7 @@ func init() {
3233
func Coapd() services.Service {
3334
mx := services.MixinService{
3435
Name: Name,
35-
Port: 5683,
36+
Port: ports.GetPort("coapd"),
3637
Running: make(chan bool, 1),
3738
Protocol: "udp",
3839
}

pkg/profiles/ports/ports.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ports
2+
3+
var ports = map[string]int {"coapd": 5683,
4+
"sshd": 22,
5+
"httpd": 8080,
6+
"echod": 7,
7+
"telnetd": 23,
8+
"mqttd": 1883,
9+
"modbusd": 502}
10+
11+
func GetPort(service string) (int) {
12+
return ports[service]
13+
}

0 commit comments

Comments
 (0)