Commit 71568e86 authored by odatskov's avatar odatskov
Browse files

Adding prelims for docker client api calls

parent e32e20b3
Pipeline #460338 canceled with stage
in 12 minutes and 9 seconds
......@@ -9,6 +9,7 @@ sync_test:
image: golang:latest
script:
- go version
- go get github.com/docker/docker/client
- go test -v
environment:
name: test
......@@ -20,23 +20,28 @@ import (
"path/filepath"
"strings"
//"net/http"
//"context"
//"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
// Global constants for available registries
const docker = "docker.registry.io"
const docker = "registry.docker.io"
const gitlab = "gitlab-registry.cern.ch"
const cvmfs_path = "/cvmfs/unpacked.cern.ch/"
//const cvmfs_path = "/cvmfs/unpacked.cern.ch/"
const cvmfs_path = "../unpacked.cern.ch/"
type Repo struct {
type Image struct {
url string // Clean repository path
registry string // Registry path for API access
target string // CVMFS target path
target string // CVMFS target folder path
file string // CVMFS target file name prefix
}
// Constructor for Repo struct
func makeRepo(path string, file string) Repo {
var url string
var registry string
func makeImage(path string, file string) Image {
var url, registry string
prefix := strings.Split(path,"/")
......@@ -48,13 +53,17 @@ func makeRepo(path string, file string) Repo {
case gitlab:
registry = gitlab ; url = strings.TrimPrefix(path,gitlab+"/")
default:
log.Printf("Unknown repository specified: %s", path)
registry = docker ; url = path
}
// Check if it is a valid repository, isValidImage
group_path := strings.Split(file,"/")
target := cvmfs_path + strings.Join(group_path[:len(group_path)-1],"/") + "/" + strings.Replace(url,":","/",1)
file_path := strings.Split(url,"/")
return Repo{url, registry, target}
target := cvmfs_path + strings.Join(group_path[:len(group_path)-1],"/") + "/"
return Image{url, registry, target,file_path[0]}
}
// Returns field value containing specified field name
......@@ -76,8 +85,8 @@ func getField(field string, path string) string {
return "na"
}
// Returns list of repositories based on input arguments to main
func getRepoList(root string, args *[]string, repos *[]Repo) {
// Returns list of valid repositories based on input arguments to main
func getImageList(root string, args *[]string, images *[]Image) {
if len(*args) == 1 {
......@@ -85,11 +94,11 @@ func getRepoList(root string, args *[]string, repos *[]Repo) {
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(path, ".yaml") {
*repos = append(*repos, makeRepo(getField("url", path),path))
*images = append(*images, makeImage(getField("url", path),path))
}
return nil
})
if len(*repos) == 0 { log.Fatalf("No Repositories found") }
if len(*images) == 0 { log.Fatalf("No Repositories found") }
} else {
var err_files []string
......@@ -100,33 +109,48 @@ func getRepoList(root string, args *[]string, repos *[]Repo) {
if os.IsNotExist(err) {
err_files = append(err_files, file)
}
} else { *repos = append(*repos, makeRepo(getField("url", file),file)) }
} else { *images = append(*images, makeImage(getField("url", file),file)) }
}
if len(err_files) > 0 { log.Fatalf("File(s) not found: %s", err_files) }
}
}
func getManifest(repo Repo) {
log.Printf("Checking out repository: %s",repo)
// Perform a GET request to the repository
// Find diffs
func isRepoValid(repo Image) bool {
return true
}
// Returns true if the repository needs to be updated
func isImageStale(image *Image, cli *client.Client) bool {
// If image already exists
// Get the latest version of the image
// Compare current version with the latest, if different IDs return true, otherwise false
// Otherwise return true
return true
}
//----------------------------------------
func main() {
var repos []Repo
getRepoList(".", &os.Args, &repos)
log.Printf("Returned list of repositories: %s", repos)
var images []Image
cli, err := client.NewEnvClient()
if err != nil { log.Fatalf(err.Error()) }
getImageList(".", &os.Args, &images)
log.Printf("Image list: %s",images)
// Check that the token still is valid
// For each file:
for _, image := range images[:0] {
// Get the manifest and check for changes
getManifest(repos[0])
// Check whether we need to update
if isImageStale(&image,cli) { log.Printf("Repository needs an update: %s",image) }
// Fetch the layers
// Unpack the structure
// Adjust permissions on the files
// Fetch the layers and adjust permissions
// Build image
}
// When done, merge changes to cvmfs
}
......@@ -9,41 +9,35 @@ import (
//"math/rand"
)
//-Utility: Return list of all yaml files-//
// Utility: Return list of all yaml files
func getFileList() []string {
//----------------------------------------//
cmd := strings.Split("find . -name *.yaml"," ")
out_cmd, _ := exec.Command(cmd[0],cmd[1:]...).Output()
out_list := strings.Split(string(out_cmd),"\n")
return out_list[:len(out_list)-1]
}
//----------------------------------------//
func Test_makeRepo(t *testing.T) {
//----------------------------------------//
func Test_makeImage(t *testing.T) {
log.Printf("One day tests shall be here")
}
//----------------------------------------//
func Test_getField(t *testing.T) {
//----------------------------------------//
log.Printf("One day tests shall be here")
}
//----------------------------------------//
func Test_getRepoList(t *testing.T) {
func Test_getImageList(t *testing.T) {
//----------------------------------------//
files := getFileList()
log.Printf("The repository contains %d files",len(files))
in_args := []string{"cmd_line"}
var repos []Repo
getRepoList(".", &in_args,&repos)
var images []Image
getImageList(".", &in_args,&images)
t.Run(fmt.Sprintf("Size test for no arguments"), func(t *testing.T) {
if len(files) != len(repos) {
t.Errorf("For all files expected size of %d, received %d",len(files),len(repos))
if len(files) != len(images) {
t.Errorf("For all files expected size of %d, received %d",len(files),len(images))
}
})
......@@ -52,12 +46,12 @@ func Test_getRepoList(t *testing.T) {
for _, path := range files[:limit] {
in_args = append(in_args,path)
repos = nil
getRepoList(".", &in_args,&repos)
images = nil
getImageList(".", &in_args,&images)
t.Run(fmt.Sprintf("Size test for %d argument(s)",len(in_args)-1), func(t *testing.T) {
if len(in_args)-1 != len(repos) {
t.Errorf("For %s expected size of %d, received %d with output: %s", in_args,len(in_args)-1,len(repos),repos)
if len(in_args)-1 != len(images) {
t.Errorf("For %s expected size of %d, received %d with output: %s", in_args,len(in_args)-1,len(images),images)
}
})
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment