Skip to content

Commit

Permalink
Ethminer APIClient and MockServer for Metrics Exporter (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
onelapahead authored Jan 19, 2021
1 parent a791f34 commit c9d6674
Show file tree
Hide file tree
Showing 589 changed files with 304,096 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ethminer-exporter/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CHANGELOG.md
VERSION
build/_output/bin/
Empty file added ethminer-exporter/CHANGELOG.md
Empty file.
1 change: 1 addition & 0 deletions ethminer-exporter/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
10 changes: 10 additions & 0 deletions ethminer-exporter/build/Dockerfile
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"]
61 changes: 61 additions & 0 deletions ethminer-exporter/cmd/client.go
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
}
45 changes: 45 additions & 0 deletions ethminer-exporter/cmd/getstatdetail.go
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)
}
56 changes: 56 additions & 0 deletions ethminer-exporter/cmd/mockApiServer.go
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)
}
39 changes: 39 additions & 0 deletions ethminer-exporter/cmd/ping.go
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)
}
91 changes: 91 additions & 0 deletions ethminer-exporter/cmd/root.go
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 added ethminer-exporter/ethxporter
Binary file not shown.
17 changes: 17 additions & 0 deletions ethminer-exporter/go.mod
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
)
Loading

0 comments on commit c9d6674

Please sign in to comment.