Skip to content

Commit

Permalink
Merge pull request #1 from tpepper/prepare
Browse files Browse the repository at this point in the history
Prepare: a few tweeks
  • Loading branch information
ikeydoherty authored Aug 23, 2016
2 parents 8ab5f6a + 0bd171b commit a9af65a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 74 deletions.
31 changes: 31 additions & 0 deletions ciao-scheduler/deps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import "github.com/01org/ciao/osprepare"

var schedDeps = osprepare.PackageRequirements{
"clearlinux": {
{"", ""},
},
"fedora": {
{"", ""},
},
"ubuntu": {
{"", ""},
},
}
2 changes: 1 addition & 1 deletion ciao-scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ func configSchedulerServer() (sched *ssntpSchedulerServer) {

func main() {
flag.Parse()
osprepare.PrepareOsDeps(nil)
osprepare.PrepareOsDeps(schedDeps)

sched := configSchedulerServer()
if sched == nil {
Expand Down
2 changes: 1 addition & 1 deletion osprepare/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type distro interface {
// getDistro will return a distro based on what
// is read from GetOsRelease
func getDistro() distro {
osRelease := GetOsRelease()
osRelease := getOSRelease()

if osRelease == nil {
return nil
Expand Down
17 changes: 9 additions & 8 deletions osprepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type PackageRequirement struct {
// )
type PackageRequirements map[string][]*PackageRequirement

// Required for absolutely core functionality across all Ciao components
// BootstrapRequirements lists required dependencies for absolutely core
// functionality across all Ciao components
var BootstrapRequirements = PackageRequirements{
"ubuntu": {
{"/usr/bin/cephfs", "ceph-fs-common"},
Expand All @@ -63,15 +64,15 @@ var BootstrapRequirements = PackageRequirements{

// CollectPackages returns a list of non-installed packages from
// the PackageRequirements received
func collectPackages(dist distro, reqs *PackageRequirements) []string {
func collectPackages(dist distro, reqs PackageRequirements) []string {
// For now just support keys like "ubuntu" vs "ubuntu:16.04"
var pkgsMissing []string
if reqs == nil {
return nil
}

id := dist.getID()
if pkgs, success := (*reqs)[id]; success {
if pkgs, success := reqs[id]; success {
for _, pkg := range pkgs {
// Have the path existing, skip.
if pathExists(pkg.BinaryName) {
Expand All @@ -85,14 +86,14 @@ func collectPackages(dist distro, reqs *PackageRequirements) []string {
return nil
}

// PrepareOsDeps installs all the dependencies defined in
// PackageRequirements in order to run the ciao component
func PrepareOsDeps(reqs *PackageRequirements) {
// PrepareOsDeps installs all the dependencies defined in a component
// specific PackageRequirements in order to enable running the component
func PrepareOsDeps(reqs PackageRequirements) {
distro := getDistro()

if distro == nil {
fmt.Fprintf(os.Stderr, "Running on an unsupported distro\n")
if rel := GetOsRelease(); rel != nil {
if rel := getOSRelease(); rel != nil {
fmt.Fprintf(os.Stderr, "Unsupported distro: %s %s\n", rel.Name, rel.Version)
} else {
fmt.Fprintln(os.Stderr, "No os-release found on this host")
Expand All @@ -113,5 +114,5 @@ func PrepareOsDeps(reqs *PackageRequirements) {
// Bootstrap installs all the core dependencies required to bootstrap the core
// configuration of all Ciao components
func Bootstrap() {
PrepareOsDeps(&BootstrapRequirements)
PrepareOsDeps(BootstrapRequirements)
}
30 changes: 15 additions & 15 deletions osprepare/os_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"
)

type OsRelease struct {
type osRelease struct {
Name string
ID string
PrettyName string
Expand All @@ -33,10 +33,10 @@ type OsRelease struct {

// Parse the given path and attempt to return a valid
// OsRelease for it
func ParseReleaseFile(path string) *OsRelease {
func parseReleaseFile(path string) *osRelease {
fi, err := os.Open(path)
var os_rel OsRelease
os_rel.mapping = make(map[string]string)
var r osRelease
r.mapping = make(map[string]string)

if err != nil {
return nil
Expand All @@ -57,40 +57,40 @@ func ParseReleaseFile(path string) *OsRelease {
value = strings.Replace(value, "'", "", -1)

if key == "name" {
os_rel.Name = value
r.Name = value
} else if key == "id" {
os_rel.ID = value
r.ID = value
} else if key == "pretty_name" {
os_rel.PrettyName = value
r.PrettyName = value
} else if key == "version" {
os_rel.Version = value
r.Version = value
} else if key == "version_id" {
os_rel.VersionID = value
r.VersionID = value
}

// Store it for use by Distro
os_rel.mapping[key] = value
r.mapping[key] = value
}
return &os_rel
return &r
}

// Try all known paths to get the right OsRelease instance
func GetOsRelease() *OsRelease {
func getOSRelease() *osRelease {
paths := []string{
"/etc/os-release",
"/usr/lib/os-release",
"/usr/lib64/os-release",
}

for _, item := range paths {
if os_rel := ParseReleaseFile(item); os_rel != nil {
return os_rel
if r := parseReleaseFile(item); r != nil {
return r
}
}
return nil
}

func (o *OsRelease) GetValue(key string) string {
func (o *osRelease) GetValue(key string) string {
if val, succ := o.mapping[strings.ToLower(key)]; succ {
return val
}
Expand Down
16 changes: 8 additions & 8 deletions osprepare/os_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ import (
)

const (
NON_EXISTENT_FILE = "/nonexistentpath/this/file/doesnt/exists"
nonExistentFile = "/nonexistentpath/this/file/doesnot/exists"
)

func TestGetOsRelease(t *testing.T) {
d := getDistro()
if d == nil {
t.Skip("Unknown distro, cannot test")
}
os_rel := GetOsRelease()
if os_rel == nil {
r := getOSRelease()
if r == nil {
t.Fatal("Could not get os-release file for known distro")
}
if d.getID() == "clearlinux" && !strings.Contains(os_rel.ID, "clear") {
if d.getID() == "clearlinux" && !strings.Contains(r.ID, "clear") {
t.Fatal("Invalid os-release for clearlinux")
} else if d.getID() == "ubuntu" && !strings.Contains(os_rel.ID, "ubuntu") {
} else if d.getID() == "ubuntu" && !strings.Contains(r.ID, "ubuntu") {
t.Fatal("Invalid os-release for Ubuntu")
} else if d.getID() == "fedora" && !strings.Contains(os_rel.ID, "fedora") {
} else if d.getID() == "fedora" && !strings.Contains(r.ID, "fedora") {
t.Fatal("Invalid os-release for Fedora")
}
}

func TestParseReleaseFileNonExistent(t *testing.T) {
if res := ParseReleaseFile(NON_EXISTENT_FILE); res != nil {
t.Fatal("Expected nil, got %v\n", res)
if res := parseReleaseFile(nonExistentFile); res != nil {
t.Fatalf("Expected nil, got %v\n", res)
}
}
70 changes: 37 additions & 33 deletions osprepare/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"
)

func get_command_output(command string) string {
func getCommandOutput(command string) string {
splits := strings.Split(command, " ")
c := exec.Command(splits[0], splits[1:]...)
c.Env = os.Environ()
Expand All @@ -33,73 +33,77 @@ func get_command_output(command string) string {
c.Env = append(c.Env, "LANG=C")
c.Stderr = os.Stderr

if out, err := c.Output(); err != nil {
out, err := c.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to run %s: %s\n", splits[0], err)
return ""
} else {
return string(out)
}

return string(out)
}

func GetDockerVersion() string {
ret := get_command_output("docker --version")
func getDockerVersion() string {
ret := getCommandOutput("docker --version")
var version string

if n, _ := fmt.Sscanf(ret, "Docker version %s, build", &version); n != 1 {
return ""
} else {
if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

func GetQemuVersion() string {
ret := get_command_output("qemu-system-x86_64 --version")
func getQemuVersion() string {
ret := getCommandOutput("qemu-system-x86_64 --version")
var version string

if n, _ := fmt.Sscanf(ret, "QEMU emulator version %s, Copyright (c)", &version); n != 1 {
return ""
} else {
if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

if strings.HasSuffix(version, ",") {
return string(version[0 : len(version)-1])
}
return version
}

// Determine if the given current version is less than the test version
// Note: Can only compare equal version schemas (i.e. same level of dots)
func VersionLessThan(current_version string, test_version string) bool {
cur_splits := strings.Split(current_version, ".")
test_splits := strings.Split(test_version, ".")
func versionLessThan(currentVer string, testVer string) bool {
curSplits := strings.Split(currentVer, ".")
testSplits := strings.Split(testVer, ".")

max := len(curSplits)

max_range := len(cur_splits)
if l2 := len(test_splits); l2 < max_range {
max_range = l2
if l2 := len(testSplits); l2 < max {
max = l2
}

cur_isplits := make([]int, max_range)
cur_tsplits := make([]int, max_range)
iSplits := make([]int, max)
tSplits := make([]int, max)

for i := 0; i < max_range; i++ {
cur_isplits[i], _ = strconv.Atoi(cur_splits[i])
cur_tsplits[i], _ = strconv.Atoi(test_splits[i])
for i := 0; i < max; i++ {
iSplits[i], _ = strconv.Atoi(curSplits[i])
tSplits[i], _ = strconv.Atoi(testSplits[i])
}

for i := 0; i < max_range; i++ {
for i := 0; i < max; i++ {
if i == 0 {
if cur_isplits[i] < cur_tsplits[i] {
if iSplits[i] < tSplits[i] {
return true
}
} else {
match := true
for j := 0; j < i; j++ {
if cur_isplits[j] != cur_tsplits[j] {
if iSplits[j] != tSplits[j] {
match = false
break
}
}
if match && cur_isplits[i] < cur_tsplits[i] {
if match && iSplits[i] < tSplits[i] {
return true
}
}
Expand Down
16 changes: 8 additions & 8 deletions osprepare/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGetDocker(t *testing.T) {
if pathExists("/usr/bin/docker") == false {
t.Skip("Docker not installed, cannot validate version get")
}
if vers := GetDockerVersion(); vers == "" {
if vers := getDockerVersion(); vers == "" {
t.Fatal("Cannot determine docker version")
}
}
Expand All @@ -33,7 +33,7 @@ func TestGetQemu(t *testing.T) {
if pathExists("/usr/bin/qemu-system-x86_64") == false {
t.Skip("Qemu not installed, cannot validate version get")
}
if vers := GetQemuVersion(); vers == "" {
if vers := getQemuVersion(); vers == "" {
t.Fatal("Cannot determine qemu version")
}
}
Expand All @@ -42,25 +42,25 @@ func TestGetQemu(t *testing.T) {
// false when given same version to tests. e.g: VersionLessThan("1.11.0", "1.11.0")
// this tests is expected to pass
func TestVersionLessThanEqualVersion(t *testing.T) {
if res := VersionLessThan(MinQemuVersion, MinQemuVersion); res != false {
t.Fatal("expected false, got %v\n", res)
if res := versionLessThan(MinQemuVersion, MinQemuVersion); res != false {
t.Fatalf("expected false, got %v\n", res)
}
}

// TestVersionLessThanGreaterVersion tests than VersionLessThan returns
// false when given greater version. e.g: VersionLessThan("1.11.0", "0.0.1")
// this tests is expected to pass
func TestVersionLessThanGreaterVersion(t *testing.T) {
if res := VersionLessThan(MinQemuVersion, "0.0.1"); res != false {
t.Fatal("expected false, got %v\n", res)
if res := versionLessThan(MinQemuVersion, "0.0.1"); res != false {
t.Fatalf("expected false, got %v\n", res)
}
}

// TestVersionLessThanLowerVersion tests than VersionLessThan returns
// true when given lower version. e.g: VersionLessThan("0.0.1", "99.9.9")
// this tests is expected to pass
func TestVersionLessThanLowerVersion(t *testing.T) {
if res := VersionLessThan("0.0.1", MinQemuVersion); res != true {
t.Fatal("expected true, got %v\n", res)
if res := versionLessThan("0.0.1", MinQemuVersion); res != true {
t.Fatalf("expected true, got %v\n", res)
}
}

0 comments on commit a9af65a

Please sign in to comment.