|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/charmbracelet/huh" |
| 6 | + "github.com/charmbracelet/huh/spinner" |
| 7 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 8 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain" |
| 9 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/clnode" |
| 10 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres" |
| 11 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/simple_node_set" |
| 12 | + "os" |
| 13 | + "os/signal" |
| 14 | + "sync" |
| 15 | + "syscall" |
| 16 | +) |
| 17 | + |
| 18 | +type nodeSetForm struct { |
| 19 | + Network string |
| 20 | + CLVersion string |
| 21 | + Nodes int |
| 22 | + Observability bool |
| 23 | + Blockscout bool |
| 24 | +} |
| 25 | + |
| 26 | +func createComponentsFromForm(form *nodeSetForm) error { |
| 27 | + var ( |
| 28 | + bc *blockchain.Output |
| 29 | + _ *simple_node_set.Output |
| 30 | + err error |
| 31 | + ) |
| 32 | + if err := framework.DefaultNetwork(&sync.Once{}); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + switch form.Network { |
| 36 | + case "anvil": |
| 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 | + }) |
| 45 | + } |
| 46 | + err = spinner.New(). |
| 47 | + Title("Creating anvil blockchain.."). |
| 48 | + Action(f). |
| 49 | + Run() |
| 50 | + } |
| 51 | + switch form.Nodes { |
| 52 | + case 5: |
| 53 | + nspecs := make([]*clnode.Input, 0) |
| 54 | + for i := 0; i < form.Nodes; i++ { |
| 55 | + nspecs = append(nspecs, &clnode.Input{ |
| 56 | + DbInput: &postgres.Input{ |
| 57 | + Image: "postgres:15.6", |
| 58 | + PullImage: true, |
| 59 | + }, |
| 60 | + Node: &clnode.NodeInput{ |
| 61 | + Image: form.CLVersion, |
| 62 | + PullImage: true, |
| 63 | + }, |
| 64 | + }) |
| 65 | + } |
| 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 | + } |
| 86 | + } |
| 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 |
| 118 | +} |
| 119 | + |
| 120 | +func runSetupForm() error { |
| 121 | + if !framework.IsDockerRunning() { |
| 122 | + return fmt.Errorf(`Docker daemon is not running! |
| 123 | +Please set up OrbStack (https://orbstack.dev/) |
| 124 | +or |
| 125 | +Docker Desktop (https://www.docker.com/products/docker-desktop/) |
| 126 | +`) |
| 127 | + } |
| 128 | + f := &nodeSetForm{} |
| 129 | + |
| 130 | + form := huh.NewForm( |
| 131 | + huh.NewGroup( |
| 132 | + huh.NewSelect[string](). |
| 133 | + Title("Which network would you like to connect to?"). |
| 134 | + Options( |
| 135 | + huh.NewOption("Anvil", "anvil"), |
| 136 | + ). |
| 137 | + Value(&f.Network), // stores the selected network |
| 138 | + |
| 139 | + huh.NewSelect[int](). |
| 140 | + Title("How many nodes do you need?"). |
| 141 | + Options( |
| 142 | + huh.NewOption("5", 5), |
| 143 | + ). |
| 144 | + Value(&f.Nodes), // stores the selected number of nodes |
| 145 | + |
| 146 | + huh.NewSelect[string](). |
| 147 | + Title("Choose Chainlink node version"). |
| 148 | + Options( |
| 149 | + huh.NewOption("public.ecr.aws/chainlink/chainlink:v2.17.0", "public.ecr.aws/chainlink/chainlink:v2.17.0")). |
| 150 | + Value(&f.CLVersion), |
| 151 | + huh.NewConfirm(). |
| 152 | + Title("Do you need to spin up an observability stack?"). |
| 153 | + Value(&f.Observability), // stores the observability option |
| 154 | + |
| 155 | + huh.NewConfirm(). |
| 156 | + Title("Do you need to spin up a Blockscout stack?"). |
| 157 | + Value(&f.Blockscout), // stores the Blockscout option |
| 158 | + ), |
| 159 | + ) |
| 160 | + |
| 161 | + err := form.Run() |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("failed to run form: %w", err) |
| 164 | + } |
| 165 | + if err := createComponentsFromForm(f); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + sigs := make(chan os.Signal, 1) |
| 170 | + signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) |
| 171 | + go func() { |
| 172 | + <-sigs |
| 173 | + fmt.Println("\nReceived 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 {} |
| 182 | +} |
0 commit comments