-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ethminer APIClient and MockServer for Metrics Exporter (#8)
- Loading branch information
1 parent
a791f34
commit c9d6674
Showing
589 changed files
with
304,096 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CHANGELOG.md | ||
VERSION | ||
build/_output/bin/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM golang:1.15 as build | ||
RUN mkdir /ethminer-exporter | ||
WORKDIR /ethminer-exporter | ||
COPY . ./ | ||
RUN GOOS=linux CGO_ENABLED=0 GO111MODULE=on go build -mod=vendor -a -tags netgo -ldflags '-w' -o /bin/ethxporter main.go | ||
|
||
FROM scratch as final | ||
COPY --from=build /bin/ethxporter / | ||
ENTRYPOINT [ "/ethxporter" ] | ||
CMD ["--help"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hfuss/ethernetes/ethminer-exporter/pkg/ethminer/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// clientCmd represents the client command | ||
var clientCmd = &cobra.Command{ | ||
Use: "client", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
} | ||
|
||
var ethapi *client.ApiClient | ||
|
||
func init() { | ||
rootCmd.AddCommand(clientCmd) | ||
networkedFlags(clientCmd) | ||
} | ||
|
||
func networkedFlags(cmd *cobra.Command) { | ||
cmd.PersistentFlags().String("hostname", "127.0.0.1", "ethminer API hostname") | ||
cmd.PersistentFlags().Int("port", 3333, "ethminer API port") | ||
} | ||
|
||
func getNetworkedFlags(cmd *cobra.Command) (string, int, error) { | ||
hostname, hostnameErr := cmd.PersistentFlags().GetString("hostname") | ||
if hostnameErr != nil { | ||
return "", 0, hostnameErr | ||
} | ||
|
||
port, portErr := cmd.PersistentFlags().GetInt("port") | ||
if portErr != nil { | ||
return "", 0, portErr | ||
} | ||
|
||
return hostname, port, nil | ||
} | ||
|
||
func initClient(ctx context.Context, cmd *cobra.Command) error { | ||
hostname, port, flagsErr := getNetworkedFlags(cmd) | ||
if flagsErr != nil { | ||
return flagsErr | ||
} | ||
|
||
ethapi = &client.ApiClient{ | ||
Host: hostname, | ||
Port: port, | ||
ConnPoolSize: 1, | ||
} | ||
ethapi.Init(ctx) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// getstatdetailCmd represents the getstatdetail command | ||
var getstatdetailCmd = &cobra.Command{ | ||
Use: "getstatdetail", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := context.TODO() | ||
initErr := initClient(ctx, cmd.Parent()) | ||
if initErr != nil { | ||
return initErr | ||
} | ||
|
||
stats, statErr := ethapi.GetStatDetail(ctx) | ||
if statErr != nil { | ||
return statErr | ||
} | ||
|
||
statsBytes, marshalErr := json.MarshalIndent(stats, "", " ") | ||
if marshalErr != nil { | ||
return marshalErr | ||
} | ||
fmt.Println(string(statsBytes)) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
clientCmd.AddCommand(getstatdetailCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Copyright © 2021 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hfuss/ethernetes/ethminer-exporter/pkg/ethminer/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// mockApiServerCmd represents the mockApiServer command | ||
var mockApiServerCmd = &cobra.Command{ | ||
Use: "mock-api-server", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
hostname, port, flagsErr := getNetworkedFlags(cmd) | ||
if flagsErr != nil { | ||
return flagsErr | ||
} | ||
|
||
server := client.MockServer{ | ||
Host: hostname, | ||
Port: port, | ||
} | ||
ctx := context.TODO() | ||
server.Init(ctx) | ||
|
||
select {} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(mockApiServerCmd) | ||
|
||
networkedFlags(mockApiServerCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// pingCmd represents the ping command | ||
var pingCmd = &cobra.Command{ | ||
Use: "ping", | ||
Short: "A brief description of your command", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := context.TODO() | ||
initErr := initClient(ctx, cmd.Parent()) | ||
if initErr != nil { | ||
return initErr | ||
} | ||
|
||
pong, pingErr := ethapi.Ping(ctx) | ||
if pingErr != nil { | ||
return pingErr | ||
} | ||
fmt.Printf("%s!\n", pong) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
clientCmd.AddCommand(pingCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright © 2021 NAME HERE <EMAIL ADDRESS> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
|
||
homedir "github.com/mitchellh/go-homedir" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "ethminer-exporter", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ethminer-exporter.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := homedir.Dir() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
// Search config in home directory with name ".ethminer-exporter" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigName(".ethminer-exporter") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Println("Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module github.com/hfuss/ethernetes/ethminer-exporter | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/avast/retry-go v3.0.0+incompatible | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/gorilla/websocket v1.4.2 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/mitchellh/go-homedir v1.1.0 | ||
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 | ||
github.com/spf13/cobra v1.1.1 | ||
github.com/spf13/viper v1.7.1 | ||
github.com/stretchr/testify v1.7.0 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
) |
Oops, something went wrong.