Skip to content

Commit b0c452e

Browse files
committed
Update solace script
1 parent e24ae01 commit b0c452e

File tree

3 files changed

+139
-92
lines changed

3 files changed

+139
-92
lines changed

modules/solace/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func WithEnv(env map[string]string) Option {
9797
return nil
9898
}
9999
}
100+
101+
// WithShmSize sets the size of the /dev/shm volume
100102
func WithShmSize(size int64) Option {
101103
return func(o *options) error {
102104
o.shmSize = size

modules/solace/script.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package solace
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// generateCLIScript generates a Solace CLI script for configuring VPN, users, queues, and topic subscriptions.
8+
// Reference: https://docs.solace.com/Admin-Ref/CLI-Reference/VMR_CLI_Commands.html
9+
func generateCLIScript(settings options) string {
10+
if len(settings.queues) == 0 {
11+
return ""
12+
}
13+
14+
script := `enable
15+
configure
16+
`
17+
18+
// Create VPN if not default
19+
if settings.vpn != defaultVpn {
20+
script += generateVPNConfig(settings.vpn)
21+
}
22+
23+
// Configure username and password
24+
script += generateUserConfig(settings.username, settings.password, settings.vpn)
25+
26+
// Configure VPN Basic authentication
27+
script += generateVPNAuth(settings.vpn)
28+
29+
// Configure queues and topic subscriptions
30+
script += generateQueueConfig(settings.queues, settings.vpn)
31+
32+
return script
33+
}
34+
35+
// generateVPNConfig creates the CLI commands for setting up a custom VPN
36+
func generateVPNConfig(vpn string) string {
37+
return fmt.Sprintf(`create message-vpn %s
38+
no shutdown
39+
exit
40+
client-profile default message-vpn %s
41+
message-spool
42+
allow-guaranteed-message-send
43+
allow-guaranteed-message-receive
44+
allow-guaranteed-endpoint-create
45+
allow-guaranteed-endpoint-create-durability all
46+
exit
47+
exit
48+
message-spool message-vpn %s
49+
max-spool-usage 60000
50+
exit
51+
`, vpn, vpn, vpn)
52+
}
53+
54+
// generateUserConfig creates the CLI commands for setting up user authentication
55+
func generateUserConfig(username, password, vpn string) string {
56+
return fmt.Sprintf(`create client-username %s message-vpn %s
57+
password %s
58+
no shutdown
59+
exit
60+
`, username, vpn, password)
61+
}
62+
63+
// generateVPNAuth creates the CLI commands for setting up VPN authentication
64+
func generateVPNAuth(vpn string) string {
65+
return fmt.Sprintf(`message-vpn %s
66+
authentication basic auth-type internal
67+
no shutdown
68+
end
69+
`, vpn)
70+
}
71+
72+
// generateQueueConfig creates the CLI commands for setting up queues and their topic subscriptions
73+
func generateQueueConfig(queues map[string][]string, vpn string) string {
74+
script := fmt.Sprintf(`configure
75+
message-spool message-vpn %s
76+
`, vpn)
77+
78+
for queue, topics := range queues {
79+
// Create the queue first
80+
script += fmt.Sprintf(` create queue %s
81+
access-type exclusive
82+
permission all consume
83+
no shutdown
84+
exit
85+
`, queue)
86+
87+
// Add topic subscriptions to the queue
88+
for _, topic := range topics {
89+
script += fmt.Sprintf(` queue %s
90+
subscription topic %s
91+
exit
92+
`, queue, topic)
93+
}
94+
}
95+
96+
script += ` exit
97+
exit
98+
`
99+
return script
100+
}

modules/solace/solace.go

Lines changed: 37 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -34,63 +34,6 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
3434
}
3535
}
3636

37-
// --- CLI script generation for queue/topic config ---
38-
// Reference: https://docs.solace.com/Admin-Ref/CLI-Reference/VMR_CLI_Commands.html
39-
var cliScript string
40-
if len(settings.queues) > 0 {
41-
cliScript += "enable\nconfigure\n"
42-
43-
// Create VPN if not default
44-
if settings.vpn != defaultVpn {
45-
cliScript += fmt.Sprintf("create message-vpn %s\n", settings.vpn)
46-
cliScript += "no shutdown\n"
47-
cliScript += "exit\n"
48-
cliScript += fmt.Sprintf("client-profile default message-vpn %s\n", settings.vpn)
49-
cliScript += "message-spool\n"
50-
cliScript += "allow-guaranteed-message-send\n"
51-
cliScript += "allow-guaranteed-message-receive\n"
52-
cliScript += "allow-guaranteed-endpoint-create\n"
53-
cliScript += "allow-guaranteed-endpoint-create-durability all\n"
54-
cliScript += "exit\n"
55-
cliScript += "exit\n"
56-
cliScript += fmt.Sprintf("message-spool message-vpn %s\n", settings.vpn)
57-
cliScript += "max-spool-usage 60000\n"
58-
cliScript += "exit\n"
59-
}
60-
61-
// Configure username and password
62-
cliScript += fmt.Sprintf("create client-username %s message-vpn %s\n", settings.username, settings.vpn)
63-
cliScript += fmt.Sprintf("password %s\n", settings.password)
64-
cliScript += "no shutdown\n"
65-
cliScript += "exit\n"
66-
67-
// Configure VPN Basic authentication
68-
cliScript += fmt.Sprintf("message-vpn %s\n", settings.vpn)
69-
cliScript += "authentication basic auth-type internal\n"
70-
cliScript += "no shutdown\n"
71-
cliScript += "end\n"
72-
73-
// Configure queues and topic subscriptions
74-
cliScript += "configure\n"
75-
cliScript += fmt.Sprintf("message-spool message-vpn %s\n", settings.vpn)
76-
for queue, topics := range settings.queues {
77-
// Create the queue first
78-
cliScript += fmt.Sprintf("create queue %s\n", queue)
79-
cliScript += "access-type exclusive\n"
80-
cliScript += "permission all consume\n"
81-
cliScript += "no shutdown\n"
82-
cliScript += "exit\n"
83-
84-
// Add topic subscriptions to the queue
85-
for _, topic := range topics {
86-
cliScript += fmt.Sprintf("queue %s\n", queue)
87-
cliScript += fmt.Sprintf("subscription topic %s\n", topic)
88-
cliScript += "exit\n"
89-
}
90-
}
91-
cliScript += "exit\nexit\n"
92-
}
93-
9437
req := testcontainers.ContainerRequest{
9538
Image: img,
9639
ExposedPorts: settings.exposedPorts,
@@ -118,45 +61,47 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
11861
}
11962
}
12063

121-
// Copy and execute CLI script inside the container if it was generated
122-
if cliScript != "" {
123-
// Write the CLI script to a temp file
124-
tmpFile, err := os.CreateTemp("", "solace-queue-setup-*.cli")
125-
if err != nil {
126-
return nil, fmt.Errorf("failed to create temp CLI script: %w", err)
127-
}
128-
defer os.Remove(tmpFile.Name())
129-
if _, err := tmpFile.Write([]byte(cliScript)); err != nil {
130-
return nil, fmt.Errorf("failed to write CLI script: %w", err)
131-
}
132-
if err := tmpFile.Close(); err != nil {
133-
return nil, fmt.Errorf("failed to close CLI script: %w", err)
134-
}
64+
// Generate CLI script for queue/topic configuration
65+
cliScript := generateCLIScript(settings)
66+
if cliScript == "" {
67+
return c, nil
68+
}
69+
// Write the CLI script to a temp file
70+
tmpFile, err := os.CreateTemp("", "solace-queue-setup-*.cli")
71+
if err != nil {
72+
return nil, fmt.Errorf("failed to create temp CLI script: %w", err)
73+
}
74+
defer os.Remove(tmpFile.Name())
75+
if _, err := tmpFile.Write([]byte(cliScript)); err != nil {
76+
return nil, fmt.Errorf("failed to write CLI script: %w", err)
77+
}
78+
if err := tmpFile.Close(); err != nil {
79+
return nil, fmt.Errorf("failed to close CLI script: %w", err)
80+
}
13581

136-
// Copy the script into the container at the correct location
137-
err = c.CopyFileToContainer(ctx, tmpFile.Name(), "/usr/sw/jail/cliscripts/script.cli", 0o644)
138-
if err != nil {
139-
return nil, fmt.Errorf("failed to copy CLI script to container: %w", err)
140-
}
82+
// Copy the script into the container at the correct location
83+
err = c.CopyFileToContainer(ctx, tmpFile.Name(), "/usr/sw/jail/cliscripts/script.cli", 0o644)
84+
if err != nil {
85+
return nil, fmt.Errorf("failed to copy CLI script to container: %w", err)
86+
}
14187

142-
// Execute the script
143-
code, out, err := c.Exec(ctx, []string{"/usr/sw/loads/currentload/bin/cli", "-A", "-es", "script.cli"})
144-
output := ""
145-
if out != nil {
146-
bytes, readErr := io.ReadAll(out)
147-
if readErr == nil {
148-
output = string(bytes)
149-
} else {
150-
output = fmt.Sprintf("[ERROR reading CLI output: %v]", readErr)
151-
}
152-
}
153-
if err != nil {
154-
return nil, fmt.Errorf("failed to execute CLI script for queue/topic setup: %w", err)
155-
}
156-
if code != 0 {
157-
return nil, fmt.Errorf("CLI script execution failed with exit code %d: %s", code, output)
88+
// Execute the script
89+
code, out, err := c.Exec(ctx, []string{"/usr/sw/loads/currentload/bin/cli", "-A", "-es", "script.cli"})
90+
output := ""
91+
if out != nil {
92+
bytes, readErr := io.ReadAll(out)
93+
if readErr == nil {
94+
output = string(bytes)
95+
} else {
96+
output = fmt.Sprintf("[ERROR reading CLI output: %v]", readErr)
15897
}
15998
}
99+
if err != nil {
100+
return nil, fmt.Errorf("failed to execute CLI script for queue/topic setup: %w", err)
101+
}
102+
if code != 0 {
103+
return nil, fmt.Errorf("CLI script execution failed with exit code %d: %s", code, output)
104+
}
160105

161106
return c, nil
162107
}

0 commit comments

Comments
 (0)