forked from solo-io/capstan
/
repository.go
154 lines (142 loc) · 3.38 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
package util
import (
"errors"
"fmt"
"github.com/cloudius-systems/capstan/core"
"github.com/cloudius-systems/capstan/image"
"gopkg.in/yaml.v1"
"io/ioutil"
"os"
"path/filepath"
)
type Repo struct {
URL string
Path string
}
func NewRepo(url string) *Repo {
root := os.Getenv("CAPSTAN_ROOT")
if root == "" {
root = filepath.Join(ConfigDir(), "repository")
}
return &Repo{
URL: url,
Path: root,
}
}
type ImageInfo struct {
FormatVersion string `yaml:"format_version"`
Version string
Created string
Description string
Build string
}
func (r *Repo) ImportImage(imageName string, file string, version string, created string, description string, build string) error {
format, err := image.Probe(file)
if err != nil {
return err
}
var hypervisor string
switch format {
case image.VDI:
hypervisor = "vbox"
case image.QCOW2:
hypervisor = "qemu"
case image.VMDK:
hypervisor = "vmware"
default:
return fmt.Errorf("%s: unsupported image format", file)
}
if _, err := os.Stat(file); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("%s: no such file", file))
}
fmt.Printf("Importing %s...\n", imageName)
dir := filepath.Dir(r.ImagePath(hypervisor, imageName))
err = os.MkdirAll(dir, 0775)
if err != nil {
return errors.New(fmt.Sprintf("%s: mkdir failed", dir))
}
dst := r.ImagePath(hypervisor, imageName)
cmd := CopyFile(file, dst)
_, err = cmd.Output()
if err != nil {
return err
}
info := ImageInfo{
FormatVersion: "1",
Version: version,
Created: created,
Description: description,
Build: build,
}
value, err := yaml.Marshal(info)
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Join(dir, "index.yaml"), value, 0644)
if err != nil {
return err
}
return nil
}
func (r *Repo) ImageExists(hypervisor, image string) bool {
file := r.ImagePath(hypervisor, image)
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}
func (r *Repo) RemoveImage(image string) error {
path := filepath.Join(r.Path, image)
if _, err := os.Stat(path); os.IsNotExist(err) {
return errors.New(fmt.Sprintf("%s: no such image\n", image))
}
fmt.Printf("Removing %s...\n", image)
err := os.RemoveAll(path)
return err
}
func (r *Repo) ImagePath(hypervisor string, image string) string {
return filepath.Join(r.Path, image, fmt.Sprintf("%s.%s", filepath.Base(image), hypervisor))
}
func (r *Repo) ListImages() {
fmt.Println(FileInfoHeader())
namespaces, _ := ioutil.ReadDir(r.Path)
for _, n := range namespaces {
images, _ := ioutil.ReadDir(filepath.Join(r.Path, n.Name()))
nrImages := 0
nrFiles := 0
for _, i := range images {
if i.IsDir() {
info := MakeFileInfo(r.Path, n.Name(), i.Name())
if info == nil {
fmt.Println(n.Name() + "/" + i.Name())
} else {
fmt.Println(info.String())
}
nrImages++
} else {
nrFiles++
}
}
// Image is directly at repository root with no namespace:
if nrImages == 0 && nrFiles != 0 {
fmt.Println(n.Name())
}
}
}
func (r *Repo) DefaultImage() string {
if !core.IsTemplateFile("Capstanfile") {
return ""
}
pwd, err := os.Getwd()
if err != nil {
return ""
}
image := filepath.Base(pwd)
return image
}