summaryrefslogtreecommitdiff
path: root/test/commandandcontrol/cc_client/main.go
blob: 674b7952102531f2258b7cd00f112eccef1daf6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"flag"
	"fmt"
	"net/url"
	"os"
	"os/signal"

	"github.com/algorand/websocket"

	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/test/commandandcontrol/lib"
)

type arrayFlags []string

func (i *arrayFlags) String() string {
	return "my string representation"
}

func (i *arrayFlags) Set(value string) error {
	*i = append(*i, value)
	return nil
}

var log = logging.NewLogger()

var targetHosts arrayFlags
var addr = flag.String("service-addr", "localhost:8080", "CC service address")
var componentName = flag.String("component", "pingpong", "name of component to control")
var componentAction = flag.String("action", "start", "action to perform (start or stop)")
var componentOptions = flag.String("options", "", "json configuration file for component action")
var listen = flag.Bool("listen", false, "keep connection to server open and tail output")

func main() {
	flag.Var(&targetHosts, "target", "target host:node, wildcards(*) are supported for host and node")
	flag.Parse()
	log.SetLevel(logging.Debug)

	if len(targetHosts) == 0 {
		log.Errorf("Use the --target flag to specify one or more target host:node pairs")
		os.Exit(1)
	}

	if *componentOptions == "" {
		log.Errorf("Use the --options flag to specify the command options")
		os.Exit(1)
	}

	options, err := os.ReadFile(*componentOptions)
	if err != nil {
		log.Errorf("failed to read options file %s", *componentOptions)
	}
	log.Infof("starting client with options %s", options)

	u := url.URL{Scheme: "ws", Host: *addr, Path: "/client"}
	log.Infof("connecting to cc service: %s", u.String())

	serverWs, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
	if err != nil {
		log.Fatal("dial:", err)
	}
	defer func() {
		log.Infof("closing service connection: %s", serverWs.RemoteAddr())
		err := serverWs.Close()
		if err != nil {
			log.Fatalf("error closing service websocket %v", err)
		}
	}()
	serverWs.Unsafe = true

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for sig := range c {
			// sig is a ^C
			log.Errorf("received signal %+v", sig)
			closeServiceConnection(serverWs)
			os.Exit(1)
		}
	}()

	ccServiceRequest := lib.CCServiceRequest{
		Component:       *componentName,
		Command:         *componentAction,
		Parameters:      fmt.Sprintf("%s", options),
		TargetAgentList: targetHosts,
	}

	log.Infof("sending service request to %+v", ccServiceRequest)
	err = serverWs.WriteJSON(ccServiceRequest)
	if err != nil {
		log.Fatalf("sending service request resulted in error: %v", err)
	}

	for {
		messageType, response, err := serverWs.ReadMessage()
		if err != nil {
			log.Fatalf("reading service response returned error: %v", err)
		} else if messageType == websocket.TextMessage {
			log.Infof("Response: %s", response)
		} else {
			log.Infof("Response: %+v", response)
		}
		if *listen == false {
			break
		}
	}
	closeServiceConnection(serverWs)
}

func closeServiceConnection(serverWs *websocket.Conn) {
	err := serverWs.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
	if err != nil {
		log.Error("write close:", err)
		return
	}
}