os_validator.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package system
  14. import (
  15. "os/exec"
  16. "strings"
  17. "github.com/pkg/errors"
  18. )
  19. var _ Validator = &OSValidator{}
  20. // OSValidator validates OS.
  21. type OSValidator struct {
  22. Reporter Reporter
  23. }
  24. // Name is part of the system.Validator interface.
  25. func (o *OSValidator) Name() string {
  26. return "os"
  27. }
  28. // Validate is part of the system.Validator interface.
  29. func (o *OSValidator) Validate(spec SysSpec) (error, error) {
  30. os, err := exec.Command("uname").CombinedOutput()
  31. if err != nil {
  32. return nil, errors.Wrap(err, "failed to get os name")
  33. }
  34. return nil, o.validateOS(strings.TrimSpace(string(os)), spec.OS)
  35. }
  36. func (o *OSValidator) validateOS(os, specOS string) error {
  37. if os != specOS {
  38. o.Reporter.Report("OS", os, bad)
  39. return errors.Errorf("unsupported operating system: %s", os)
  40. }
  41. o.Reporter.Report("OS", os, good)
  42. return nil
  43. }