Skip to content

Commit

Permalink
add "/v1/machine/{id}/power/cycle" (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwennrich authored Jul 21, 2021
1 parent 394ee66 commit dc3d50c
Show file tree
Hide file tree
Showing 22 changed files with 306 additions and 82 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'

- name: Docker Login
uses: docker/login-action@v1
with:
Expand Down Expand Up @@ -41,6 +46,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'

- name: Run integration tests
run: |
go test -tags=integration -timeout 600s -p 1 ./...
11 changes: 11 additions & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'

- name: Figure out if running fork PR
id: fork
run: '["${{ secrets.DOCKER_REGISTRY_TOKEN }}" == ""] && echo "::set-output name=is_fork_pr::true" || echo "::set-output name=is_fork_pr::false"'
Expand Down Expand Up @@ -44,6 +49,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'

- name: Run integration tests
run: |
go test -tags=integration -timeout 600s -p 1 ./...
5 changes: 5 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.16
uses: actions/[email protected]
with:
go-version: '1.16.x'

- name: Docker Login
uses: docker/login-action@v1
with:
Expand Down
9 changes: 5 additions & 4 deletions cmd/metal-api/internal/datastore/migrate.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package datastore

import (
"errors"
"fmt"
"sync"

"github.com/pkg/errors"
"go4.org/sort"
"sort"

r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)

Expand Down Expand Up @@ -142,14 +143,14 @@ func (rs *RethinkStore) Migrate(targetVersion *int, dry bool) error {
rs.SugaredLogger.Infow("running database migration", "version", m.Version, "name", m.Name)
err = m.Up(rs.db(), rs.session, rs)
if err != nil {
return errors.Wrap(err, "error running database migration")
return fmt.Errorf("error running database migration: %w", err)
}

_, err := rs.migrationTable().Insert(MigrationVersionEntry{Version: m.Version, Name: m.Name}, r.InsertOpts{
Conflict: "replace",
}).RunWrite(rs.session)
if err != nil {
return errors.Wrap(err, "error updating database migration version")
return fmt.Errorf("error updating database migration version: %w", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/metal-api/internal/grpc/grpc-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"os"
"time"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
Expand Down Expand Up @@ -128,11 +128,11 @@ func (s *Server) Serve() error {
}

if s.tlsEnabled {
cert, err := ioutil.ReadFile(s.serverCertFile)
cert, err := os.ReadFile(s.serverCertFile)
if err != nil {
s.logger.Fatalw("failed to serve gRPC", "error", err)
}
key, err := ioutil.ReadFile(s.serverKeyFile)
key, err := os.ReadFile(s.serverKeyFile)
if err != nil {
s.logger.Fatalw("failed to serve gRPC", "error", err)
}
Expand All @@ -141,7 +141,7 @@ func (s *Server) Serve() error {
return err
}

caCert, err := ioutil.ReadFile(s.caCertFile)
caCert, err := os.ReadFile(s.caCertFile)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/metal-api/internal/grpc/supwd-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package grpc

import (
"context"
"io/ioutil"
"os"
"strings"

v1 "github.com/metal-stack/metal-api/pkg/api/v1"
Expand Down Expand Up @@ -30,7 +30,7 @@ func (s *SupwdService) FetchSuperUserPassword(ctx context.Context, req *v1.Super
return resp, nil
}

bb, err := ioutil.ReadFile(s.pwdFile)
bb, err := os.ReadFile(s.pwdFile)
if err != nil {
s.logger.Errorw("failed to lookup BMC superuser password", "password file", s.pwdFile, "error", err)
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions cmd/metal-api/internal/grpc/wait-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

mathrand "math/rand"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
Expand Down Expand Up @@ -42,7 +41,7 @@ type Datasource interface {
func NewWaitService(cfg *ServerConfig) (*WaitService, error) {
c, err := bus.NewConsumer(cfg.Logger.Desugar(), cfg.NsqTlsConfig, cfg.NsqlookupdHttpAddress)
if err != nil {
return nil, errors.Wrap(err, "cannot connect to NSQ")
return nil, fmt.Errorf("cannot connect to NSQ: %w", err)
}

s := &WaitService{
Expand Down
3 changes: 1 addition & 2 deletions cmd/metal-api/internal/metal/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package metal

import (
"errors"
"testing"

"github.com/pkg/errors"
)

func TestNotFound(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions cmd/metal-api/internal/metal/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ const (
MachineOnCmd MachineCommand = "ON"
MachineOffCmd MachineCommand = "OFF"
MachineResetCmd MachineCommand = "RESET"
MachineCycleCmd MachineCommand = "CYCLE"
MachineBiosCmd MachineCommand = "BIOS"
MachineDiskCmd MachineCommand = "DISK"
MachinePxeCmd MachineCommand = "PXE"
Expand Down
4 changes: 2 additions & 2 deletions cmd/metal-api/internal/service/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package service

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -37,7 +37,7 @@ func webRequest(t *testing.T, method string, service *restful.WebService, user *

jsonBody, err := json.Marshal(request)
require.NoError(t, err)
body := ioutil.NopCloser(strings.NewReader(string(jsonBody)))
body := io.NopCloser(strings.NewReader(string(jsonBody)))
createReq := httptest.NewRequest(method, path, body)
createReq.Header.Set("Content-Type", "application/json")

Expand Down
6 changes: 3 additions & 3 deletions cmd/metal-api/internal/service/firmware-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package service

import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/service/s3"
s3server "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/s3client"
"net/http"
"strings"

"github.com/pkg/errors"
"github.com/aws/aws-sdk-go/service/s3"
s3server "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/s3client"

"github.com/metal-stack/metal-lib/httperrors"
"go.uber.org/zap"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package service
import (
"context"
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestGetImagesIntegration(t *testing.T) {

ji, err := json.Marshal(newImage)
require.NoError(t, err)
body := ioutil.NopCloser(strings.NewReader(string(ji)))
body := io.NopCloser(strings.NewReader(string(ji)))
createReq := httptest.NewRequest(http.MethodPut, "/v1/image", body)
createReq.Header.Set("Content-Type", "application/json")

Expand Down
21 changes: 18 additions & 3 deletions cmd/metal-api/internal/service/machine-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/metal-stack/security"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/grpc"
"github.com/pkg/errors"

"golang.org/x/crypto/ssh"

Expand Down Expand Up @@ -361,6 +361,17 @@ func (r machineResource) webService() *restful.WebService {
Returns(http.StatusOK, "OK", v1.MachineResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.POST("/{id}/power/cycle").
To(editor(r.machineCycle)).
Operation("machineCycle").
Doc("sends a power cycle to the machine").
Param(ws.PathParameter("id", "identifier of the machine").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Reads(v1.EmptyBody{}).
Writes(v1.MachineResponse{}).
Returns(http.StatusOK, "OK", v1.MachineResponse{}).
DefaultReturns("Error", httperrors.HTTPErrorResponse{}))

ws.Route(ws.POST("/{id}/power/bios").
To(editor(r.machineBios)).
Operation("machineBios").
Expand Down Expand Up @@ -1256,7 +1267,7 @@ func gatherNetworks(ds *datastore.RethinkStore, allocationSpec *machineAllocatio
boolTrue := true
err = ds.SearchNetworks(&datastore.NetworkSearchQuery{PrivateSuper: &boolTrue}, &privateSuperNetworks)
if err != nil {
return nil, errors.Wrap(err, "partition has no private super network")
return nil, fmt.Errorf("partition has no private super network: %w", err)
}

specNetworks, err := gatherNetworksFromSpec(ds, allocationSpec, partition, privateSuperNetworks)
Expand Down Expand Up @@ -2118,6 +2129,10 @@ func (r machineResource) machineReset(request *restful.Request, response *restfu
r.machineCmd("machineReset", metal.MachineResetCmd, request, response)
}

func (r machineResource) machineCycle(request *restful.Request, response *restful.Response) {
r.machineCmd("machineCycle", metal.MachineCycleCmd, request, response)
}

func (r machineResource) machineBios(request *restful.Request, response *restful.Response) {
r.machineCmd("machineBios", metal.MachineBiosCmd, request, response)
}
Expand Down Expand Up @@ -2195,7 +2210,7 @@ func (r machineResource) machineCmd(op string, cmd metal.MachineCommand, request
}

switch op {
case "machineReset", "machineOff":
case "machineReset", "machineOff", "machineCycle":
event := string(metal.ProvisioningEventPlannedReboot)
_, err = r.provisioningEventForMachine(id, v1.MachineProvisioningEvent{Time: time.Now(), Event: event, Message: op})
if checkError(request, response, utils.CurrentFuncName(), err) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/metal-api/internal/service/machine-service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,10 @@ func TestOnMachine(t *testing.T) {
cmd: metal.MachineResetCmd,
endpoint: "reset",
},
{
cmd: metal.MachineCycleCmd,
endpoint: "cycle",
},
{
cmd: metal.MachineBiosCmd,
endpoint: "bios",
Expand Down
6 changes: 3 additions & 3 deletions cmd/metal-api/internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package service
import (
"bytes"
"context"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -37,10 +37,10 @@ func injectUser(u security.User, container *restful.Container, rq *http.Request)
container.Filter(rest.UserAuth(usergetter))
var body []byte
if rq.Body != nil {
data, _ := ioutil.ReadAll(rq.Body)
data, _ := io.ReadAll(rq.Body)
body = data
rq.Body.Close()
rq.Body = ioutil.NopCloser(bytes.NewReader(data))
rq.Body = io.NopCloser(bytes.NewReader(data))
}
hma.AddAuth(rq, time.Now(), body)
return container
Expand Down
6 changes: 3 additions & 3 deletions cmd/metal-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
httppprof "net/http/pprof"
Expand All @@ -24,7 +25,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"

nsq2 "github.com/nsqio/go-nsq"
"github.com/pkg/errors"

"github.com/metal-stack/metal-lib/jwt/grp"
"github.com/metal-stack/metal-lib/jwt/sec"
Expand Down Expand Up @@ -781,7 +781,7 @@ func resurrectDeadMachines() error {
}
err = service.ResurrectMachines(ds, p, ep, ipamer, logger)
if err != nil {
return errors.Wrap(err, "unable to resurrect machines")
return fmt.Errorf("unable to resurrect machines: %w", err)
}

return nil
Expand All @@ -795,7 +795,7 @@ func evaluateLiveliness() error {

err = service.MachineLiveliness(ds, logger)
if err != nil {
return errors.Wrap(err, "unable to evaluate machine liveliness")
return fmt.Errorf("unable to evaluate machine liveliness: %w", err)
}

return nil
Expand Down
Loading

0 comments on commit dc3d50c

Please sign in to comment.