Serge Bazanski | 753d63f | 2018-10-14 15:21:46 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "flag" |
| 6 | |
Serge Bazanski | 965b78a | 2018-10-25 12:22:37 +0100 | [diff] [blame] | 7 | "code.hackerspace.pl/hscloud/go/mirko" |
Serge Bazanski | 753d63f | 2018-10-14 15:21:46 -0700 | [diff] [blame] | 8 | "github.com/golang/glog" |
| 9 | "google.golang.org/grpc/codes" |
| 10 | "google.golang.org/grpc/status" |
| 11 | |
Sergiusz Bazanski | 61594bb | 2019-07-21 15:20:51 +0200 | [diff] [blame] | 12 | pb "code.hackerspace.pl/hscloud/dc/cmc-proxy/proto" |
Serge Bazanski | 753d63f | 2018-10-14 15:21:46 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | flagCMCAddress string |
| 17 | flagCMCUsername string |
| 18 | flagCMCPassword string |
| 19 | ) |
| 20 | |
| 21 | const ( |
| 22 | pathLogin = "cgi-bin/webcgi/login" |
| 23 | pathLogout = "cgi-bin/webcgi/logout" |
| 24 | pathiDRACURL = "cgi-bin/webcgi/blade_iDRAC_url" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | flag.Set("logtostderr", "true") |
| 29 | } |
| 30 | |
| 31 | type service struct { |
| 32 | cmc *cmcClient |
| 33 | } |
| 34 | |
| 35 | func (s *service) GetKVMData(ctx context.Context, req *pb.GetKVMDataRequest) (*pb.GetKVMDataResponse, error) { |
| 36 | if req.BladeNum < 1 || req.BladeNum > 16 { |
| 37 | return nil, status.Error(codes.InvalidArgument, "blade_num must be [1,16]") |
| 38 | } |
| 39 | |
| 40 | details, err := s.cmc.RequestKVMDetails(ctx, int(req.BladeNum)) |
| 41 | if err != nil { |
| 42 | glog.Errorf("RequestKVMDetails(_, %d): %v", req.BladeNum, err) |
| 43 | return nil, status.Error(codes.Unavailable, "CMC unavailable") |
| 44 | } |
| 45 | |
| 46 | return &pb.GetKVMDataResponse{ |
| 47 | Arguments: details.arguments, |
| 48 | }, nil |
| 49 | } |
| 50 | |
| 51 | func main() { |
| 52 | flag.StringVar(&flagCMCAddress, "cmc_address", "https://10.10.10.10", "URL of Dell M1000e CMC") |
| 53 | flag.StringVar(&flagCMCUsername, "cmc_username", "root", "Login username for CMC") |
| 54 | flag.StringVar(&flagCMCPassword, "cmc_password", "", "Login password for CMC") |
| 55 | flag.Parse() |
| 56 | |
| 57 | m := mirko.New() |
| 58 | if err := m.Listen(); err != nil { |
| 59 | glog.Exitf("Could not listen: %v", err) |
| 60 | } |
| 61 | |
| 62 | s := &service{ |
| 63 | cmc: NewCMCClient(), |
| 64 | } |
| 65 | pb.RegisterCMCProxyServer(m.GRPC(), s) |
| 66 | |
| 67 | if err := m.Serve(); err != nil { |
| 68 | glog.Exitf("Could not run: %v", err) |
| 69 | } |
| 70 | |
| 71 | go s.cmc.Run(m.Context()) |
| 72 | glog.Info("Running.") |
| 73 | |
| 74 | <-m.Done() |
| 75 | } |