blob: 8b74d70a9ebf36111fd83e0b17a1ca6efd8941bd [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package pdu
import (
"net/http"
"strconv"
"fuchsia.googlesource.com/infra/infra/pdu/amt"
"fuchsia.googlesource.com/infra/infra/pdu/arduinorelay"
"fuchsia.googlesource.com/infra/infra/pdu/webcardlx"
)
// Config represents a PDU configuration for a particular device.
type Config struct {
// Type is the type of PDU to use.
Type string `json:"type"`
// Host is the network hostname of the PDU e.g. fuchsia-tests-pdu-001.
Host string `json:"host"`
// Username is the username used to log in to the PDU.
Username string `json:"username"`
// Password is the password used to log in to the PDU.
Password string `json:"password"`
// DevicePath is the path to the device on the local machine.
DevicePath string `json:"device_path"`
// DevicePort is the PDU-specific string which identifies the
// hardware device we're testing on in the PDU.
DevicePort string `json:"device_port"`
}
// RebootDevice uses the given configuration to reboot the device.
func RebootDevice(cfg *Config) error {
switch cfg.Type {
case "amt":
return amt.Reboot(cfg.Host, cfg.Username, cfg.Password)
case "arduinorelay":
return arduinorelay.Reboot(cfg.DevicePath, cfg.DevicePort)
case "webcardlx":
fallthrough
default:
// TODO(mknyszek): Require a type to be specified for this.
wc := webcardlx.Webcardlx{
Client: &http.Client{},
Host: cfg.Host,
Username: cfg.Username,
Password: cfg.Password,
}
if err := wc.Login(); err != nil {
return err
}
defer wc.Logout()
port, err := strconv.Atoi(cfg.DevicePort)
if err != nil {
return err
}
return wc.Loads(port, webcardlx.Cycle)
}
}