8000 Digital Ocean implementation of `StartNodes` and `StopTestnet` by sergio-mena · Pull Request #846 · cometbft/cometbft · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Digital Ocean implementation of StartNodes and StopTestnet #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions test/e2e/pkg/infra/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package digitalocean

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

e2e "github.com/cometbft/cometbft/test/e2e/pkg"
"github.com/cometbft/cometbft/test/e2e/pkg/exec"
"github.com/cometbft/cometbft/test/e2e/pkg/infra"
)

Expand All @@ -19,11 +24,68 @@ func (p *Provider) Setup() error {
return nil
}

func (p Provider) StartNodes(_ context.Context, _ ...*e2e.Node) error {
//TODO Not implemented (next PR)
return nil
const ymlSystemd = "systemd-action.yml"

func (p Provider) StartNodes(ctx context.Context, nodes ...*e2e.Node) error {
nodeIPs := make([]string, len(nodes))
for i, n := range nodes {
nodeIPs[i] = n.ExternalIP.String()
}
if err := p.writePlaybook(ymlSystemd, true); err != nil {
return err
}

return execAnsible(ctx, p.Testnet.Dir, ymlSystemd, nodeIPs)
}
func (p Provider) StopTestnet(_ context.Context) error {
//TODO Not implemented (next PR)
func (p Provider) StopTestnet(ctx context.Context) error {
nodeIPs := make([]string, len(p.Testnet.Nodes))
for i, n := range p.Testnet.Nodes {
nodeIPs[i] = n.ExternalIP.String()
}

if err := p.writePlaybook(ymlSystemd, false); err != nil {
return err
}
return execAnsible(ctx, p.Testnet.Dir, ymlSystemd, nodeIPs)
}

func (p Provider) writePlaybook(yaml string, starting bool) error {
playbook := ansibleSystemdBytes(starting)
//nolint: gosec
// G306: Expect WriteFile permissions to be 0600 or less
err := os.WriteFile(filepath.Join(p.Testnet.Dir, yaml), []byte(playbook), 0o644)
if err != nil {
return err
}< 7FB1 /span>
return nil
}

// file as bytes to be written out to disk.
// ansibleStartBytes generates an Ansible playbook to start the network
func ansibleSystemdBytes(starting bool) string {
startStop := "stopped"
if starting {
startStop = "started"
}
playbook := fmt.Sprintf(`- name: start/stop testapp
hosts: all
gather_facts: yes
vars:
ansible_host_key_checking: false

tasks:
- name: operate on the systemd-unit
ansible.builtin.systemd:
name: testappd
state: %s
enabled: yes`, startStop)
return playbook
}

// ExecCompose runs a Docker Compose command for a testnet.
func execAnsible(ctx context.Context, dir, playbook string, nodeIPs []string, args ...string) error {
playbook = filepath.Join(dir, playbook)
return exec.CommandVerbose(ctx, append(
[]string{"ansible-playbook", playbook, "-f", "50", "-u", "root", "--inventory", strings.Join(nodeIPs, ",") + ","},
args...)...)
}
0