semaphore + refactoring

java_console
Adam Veldhousen 2019-01-30 23:03:18 -06:00
parent cfe17184ec
commit 816959b6e0
7 changed files with 78 additions and 16 deletions

31
.semaphore/semaphore.yml Normal file
View File

@ -0,0 +1,31 @@
version: v1.0
name: Drac Pipeline
agent:
machine:
type: e1-standard-2
os_image: ubuntu1804
blocks:
- name: "Build"
task:
env_vars:
- name: APP_ENV
value: prod
jobs:
- name: Docker build
commands:
- checkout
- make dev
- name: "Vet"
task:
jobs:
- name: Test
commands:
- checkout
- echo "make test"
- name: Lint code
commands:
- checkout
- echo "make lint"

View File

@ -1,5 +1,11 @@
# DRACLI
[![GoDoc](https://godoc.org/github.com/adamveld12/drac?status.svg)](http://godoc.org/github.com/adamveld12/drac)
[![Go Walker](http://gowalker.org/api/v1/badge)](https://gowalker.org/github.com/adamveld12/drac)
[![Gocover](http://gocover.io/_badge/github.com/adamveld12/drac)](http://gocover.io/github.com/adamveld12/drac)
[![Go Report Card](https://goreportcard.com/badge/github.com/adamveld12/drac)](https://goreportcard.com/report/github.com/adamveld12/drac)
[![Build Status](https://semaphoreci.com/api/v1/adamveld12/drac/branches/master/badge.svg)](https://semaphoreci.com/adamveld12/drac)
A quick and dirty CLI/client library for the Integrated Dell Remote Access Controller v6.
## CLI Usage

View File

@ -14,6 +14,7 @@ import (
xj "github.com/basgys/goxml2json"
)
//Client is an http client that talks to the iDRAC
type Client struct {
client *http.Client
Username string
@ -52,6 +53,7 @@ func (c *Client) doHTTP(path, qs string, body io.Reader) (string, []*http.Cookie
return jsonData.String(), res.Cookies(), nil
}
//OpenConsole downloads the jnlp that opens the console
func (c *Client) OpenConsole() error {
//https://192.168.0.228/viewer.jnlp(192.168.0.228@0@root@1548390931405)
uri := fmt.Sprintf("https://%s/viewer.jnlp(%s@0@%s@%d)", c.Host, c.Host, c.Username, time.Now().Unix())
@ -84,16 +86,19 @@ func (c *Client) OpenConsole() error {
return nil
}
//SetPowerState changes the power state of the server (ie turn off or on)
func (c *Client) SetPowerState(ps PowerState) (string, error) {
res, _, err := c.doHTTP("", fmt.Sprintf("set=pwState:%d", ps), nil)
return res, err
}
//SetBootOverride changes the boot override settings
func (c *Client) SetBootOverride(bo BootDevice, bootOnce bool) (string, error) {
res, _, err := c.doHTTP("", fmt.Sprintf("set=vmBootOnce:%v,firstBootDevice:%d", bootOnce, bo), nil)
return res, err
}
//Query queries attributes and returns them to json
func (c *Client) Query(ds ...Attribute) (string, error) {
bufs := bytes.NewBufferString("")
for idx, attr := range ds {
@ -107,6 +112,7 @@ func (c *Client) Query(ds ...Attribute) (string, error) {
return res, err
}
//NewFromCredentials creates a new client from a credentials file at the specified path
func NewFromCredentials(path string) (*Client, error) {
credential, err := LoadCredentials(path)
if err != nil {
@ -126,6 +132,7 @@ func NewFromCredentials(path string) (*Client, error) {
return c, nil
}
//NewClient creates a new Client
func NewClient(host string, skipVerify bool) (*Client, error) {
c := &http.Client{
Timeout: time.Second * 5,
@ -147,6 +154,7 @@ func NewClient(host string, skipVerify bool) (*Client, error) {
return client, nil
}
//Login sends a login http request to the iDRAC
func (c *Client) Login(username, password string) (string, error) {
body := bytes.NewBufferString(fmt.Sprintf("user=%s&password=%s", username, password))
_, cookies, err := c.doHTTP("login", "", body)
@ -219,6 +227,11 @@ var (
LocalCD = BootDevice(5)
)
//Attribute is an attribute that can be queried
type Attribute string
//PowerState represents a powerstate option
type PowerState uint8
//BootDevice represents a bootdevice option
type BootDevice uint8

View File

@ -6,12 +6,14 @@ import (
"os"
)
//Credential represents credentials for the credentials json
type Credential struct {
Host string
Username string
AuthToken string
}
//LoadCredentials from a credentials json file
func LoadCredentials(path string) (*Credential, error) {
fp := fmt.Sprintf("%s/credentials.json", path)
f, err := os.Open(fp)
@ -29,6 +31,7 @@ func LoadCredentials(path string) (*Credential, error) {
return credential, nil
}
//SaveCredentials saves credentials to a json file
func SaveCredentials(path string, c Credential) error {
f, err := os.Create(fmt.Sprintf("%s/credentials.json", path))
if err != nil {

10
main.go
View File

@ -61,7 +61,7 @@ var (
)
func main() {
c, err := ToCommand(os.Args[1:]...)
c, err := toCommand(os.Args[1:]...)
if err != nil {
log.Println(err)
os.Exit(-1)
@ -246,17 +246,17 @@ func helpAction(args map[string][]string) error {
return nil
}
type Command struct {
type command struct {
Name string
Arguments map[string][]string
}
func ToCommand(args ...string) (Command, error) {
func toCommand(args ...string) (command, error) {
if len(args) < 1 {
return Command{}, nil
return command{}, nil
}
c := Command{
c := command{
Name: args[0],
Arguments: map[string][]string{},
}

View File

@ -6,17 +6,17 @@ import (
"testing"
)
func Test_ToCommand(t *testing.T) {
func Test_toCommand(t *testing.T) {
tests := []struct {
name string
args string
want Command
want command
wantErr bool
}{
{
name: "login works",
args: "login -u root -p calvin 10.0.0.5",
want: Command{
want: command{
Name: "login",
Arguments: map[string][]string{
"u": []string{"root"},
@ -29,7 +29,7 @@ func Test_ToCommand(t *testing.T) {
{
name: "boot settings",
args: "boot_settings -once local_cd",
want: Command{
want: command{
Name: "boot_settings",
Arguments: map[string][]string{
"once": []string{"true"},
@ -41,7 +41,7 @@ func Test_ToCommand(t *testing.T) {
{
name: "query multi value",
args: "query pwState kvmEnabled voltages temperatures",
want: Command{
want: command{
Name: "query",
Arguments: map[string][]string{
"": []string{"pwState", "kvmEnabled", "voltages", "temperatures"},
@ -53,13 +53,13 @@ func Test_ToCommand(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := strings.Split(tt.args, " ")
got, err := ToCommand(args...)
got, err := toCommand(args...)
if (err != nil) != tt.wantErr {
t.Errorf("ToCommand() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("toCommand() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToCommand() = %v, want %v", got, tt.want)
t.Errorf("toCommand() = %v, want %v", got, tt.want)
}
})
}

View File

@ -1,5 +1,6 @@
.PHONY: clean debug dev test
.PHONY: check clean debug dev test
ci: clean setup lint test
clean:
@rm -rf ./dracli
@ -10,7 +11,15 @@ debug:
dlv debug --headless --api-version=2 -l 127.0.0.1:2456 -- query pwState
dracli:
@go build -o dracli
@go build -o dracli
test:
lint:
go get golang.org/x/lint/golint
golint -min_confidence 1.5 -set_exit_status
go vet -all -v
setup:
@go get -t -d -v ./...
test:
@go test -v -cover