@@ -3,12 +3,16 @@ package main
33import (
44 "fmt"
55 "github.com/charmbracelet/huh"
6+ "github.com/charmbracelet/huh/spinner"
67 "github.com/smartcontractkit/chainlink-testing-framework/framework"
78 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
89 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode"
910 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
1011 "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set"
12+ "os"
13+ "os/signal"
1114 "sync"
15+ "syscall"
1216)
1317
1418type nodeSetForm struct {
@@ -19,7 +23,7 @@ type nodeSetForm struct {
1923 Blockscout bool
2024}
2125
22- func createComponentsFromForm (f * nodeSetForm ) error {
26+ func createComponentsFromForm (form * nodeSetForm ) error {
2327 var (
2428 bc * blockchain.Output
2529 _ * simple_node_set.Output
@@ -28,42 +32,89 @@ func createComponentsFromForm(f *nodeSetForm) error {
2832 if err := framework .DefaultNetwork (& sync.Once {}); err != nil {
2933 return err
3034 }
31-
32- switch f .Network {
35+ switch form .Network {
3336 case "anvil" :
34- bc , err = blockchain .NewBlockchainNetwork (& blockchain.Input {
35- Type : "anvil" ,
36- Image : "f4hrenh9it/foundry" ,
37- PullImage : true ,
38- Port : "8545" ,
39- ChainID : "31337" ,
40- })
41- if err != nil {
42- return err
37+ f := func () {
38+ bc , err = blockchain .NewBlockchainNetwork (& blockchain.Input {
39+ Type : "anvil" ,
40+ Image : "f4hrenh9it/foundry" ,
41+ PullImage : true ,
42+ Port : "8545" ,
43+ ChainID : "31337" ,
44+ })
4345 }
46+ err = spinner .New ().
47+ Title ("Creating anvil blockchain.." ).
48+ Action (f ).
49+ Run ()
4450 }
45- switch f .Nodes {
51+ switch form .Nodes {
4652 case 5 :
4753 nspecs := make ([]* clnode.Input , 0 )
48- for i := 0 ; i < f .Nodes ; i ++ {
54+ for i := 0 ; i < form .Nodes ; i ++ {
4955 nspecs = append (nspecs , & clnode.Input {
5056 DbInput : & postgres.Input {
5157 Image : "postgres:15.6" ,
5258 PullImage : true ,
5359 },
5460 Node : & clnode.NodeInput {
55- Image : f .CLVersion ,
61+ Image : form .CLVersion ,
5662 PullImage : true ,
5763 },
5864 })
5965 }
60- _ , err = simple_node_set .NewSharedDBNodeSet (& simple_node_set.Input {
61- Nodes : 5 ,
62- OverrideMode : "all" ,
63- NodeSpecs : nspecs ,
64- }, bc , "" )
66+ f := func () {
67+ _ , err = simple_node_set .NewSharedDBNodeSet (& simple_node_set.Input {
68+ Nodes : 5 ,
69+ OverrideMode : "all" ,
70+ NodeSpecs : nspecs ,
71+ }, bc , "" )
72+ }
73+ err = spinner .New ().
74+ Title ("Creating node set.." ).
75+ Action (f ).
76+ Run ()
77+ }
78+ switch form .Observability {
79+ case true :
80+ if err = framework .NewPromtail (); err != nil {
81+ return err
82+ }
83+ if err := observabilityUp (); err != nil {
84+ return err
85+ }
6586 }
66- return nil
87+ switch form .Blockscout {
88+ case true :
89+ if err := blockscoutUp (); err != nil {
90+ return err
91+ }
92+ }
93+ return err
94+ }
95+
96+ func cleanup (form * nodeSetForm ) error {
97+ var err error
98+ f := func () {
99+ err = cleanDockerResources ()
100+ }
101+ err = spinner .New ().
102+ Title ("Removing docker resources.." ).
103+ Action (f ).
104+ Run ()
105+ switch form .Observability {
106+ case true :
107+ if err := observabilityDown (); err != nil {
108+ return err
109+ }
110+ }
111+ switch form .Blockscout {
112+ case true :
113+ if err := blockscoutDown (); err != nil {
114+ return err
115+ }
116+ }
117+ return err
67118}
68119
69120func runSetupForm () error {
@@ -111,13 +162,21 @@ Docker Desktop (https://www.docker.com/products/docker-desktop/)
111162 if err != nil {
112163 return fmt .Errorf ("failed to run form: %w" , err )
113164 }
165+ if err := createComponentsFromForm (f ); err != nil {
166+ return err
167+ }
114168
115- fmt .Println ("Configuration Summary:" )
116- fmt .Printf ("Network: %s\n " , f .Network )
117- fmt .Printf ("Chainlink version: %d\n " , f .Nodes )
118- fmt .Printf ("Number of Nodes: %d\n " , f .Nodes )
119- fmt .Printf ("Observability Stack: %v\n " , f .Observability )
120- fmt .Printf ("Blockscout Stack: %v\n " , f .Blockscout )
121-
122- return createComponentsFromForm (f )
169+ sigs := make (chan os.Signal , 1 )
170+ signal .Notify (sigs , os .Interrupt , syscall .SIGTERM )
171+ go func () {
172+ <- sigs
173+ fmt .Println ("\n Received Ctrl+C, starting custom cleanup..." )
174+ err := cleanup (f )
175+ if err != nil {
176+ panic (err )
177+ }
178+ os .Exit (0 )
179+ }()
180+ framework .L .Info ().Msg ("Press Ctrl+C to remove the stack.." )
181+ select {}
123182}
0 commit comments