| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,342 +0,0 @@ |
| 1 |
-package image |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "encoding/json" |
|
| 5 |
- "errors" |
|
| 6 |
- "github.com/dotcloud/docker/future" |
|
| 7 |
- "io" |
|
| 8 |
- "io/ioutil" |
|
| 9 |
- "os" |
|
| 10 |
- "path" |
|
| 11 |
- "path/filepath" |
|
| 12 |
- "sort" |
|
| 13 |
- "strings" |
|
| 14 |
- "time" |
|
| 15 |
-) |
|
| 16 |
- |
|
| 17 |
-type Store struct {
|
|
| 18 |
- *Index |
|
| 19 |
- Root string |
|
| 20 |
- Layers *LayerStore |
|
| 21 |
-} |
|
| 22 |
- |
|
| 23 |
-func New(root string) (*Store, error) {
|
|
| 24 |
- abspath, err := filepath.Abs(root) |
|
| 25 |
- if err != nil {
|
|
| 26 |
- return nil, err |
|
| 27 |
- } |
|
| 28 |
- if err := os.MkdirAll(abspath, 0700); err != nil && !os.IsExist(err) {
|
|
| 29 |
- return nil, err |
|
| 30 |
- } |
|
| 31 |
- layers, err := NewLayerStore(path.Join(root, "layers")) |
|
| 32 |
- if err != nil {
|
|
| 33 |
- return nil, err |
|
| 34 |
- } |
|
| 35 |
- if err := layers.Init(); err != nil {
|
|
| 36 |
- return nil, err |
|
| 37 |
- } |
|
| 38 |
- return &Store{
|
|
| 39 |
- Root: abspath, |
|
| 40 |
- Index: NewIndex(path.Join(root, "index.json")), |
|
| 41 |
- Layers: layers, |
|
| 42 |
- }, nil |
|
| 43 |
-} |
|
| 44 |
- |
|
| 45 |
-type Compression uint32 |
|
| 46 |
- |
|
| 47 |
-const ( |
|
| 48 |
- Uncompressed Compression = iota |
|
| 49 |
- Bzip2 |
|
| 50 |
- Gzip |
|
| 51 |
-) |
|
| 52 |
- |
|
| 53 |
-func (store *Store) Import(name string, archive io.Reader, stderr io.Writer, parent *Image, compression Compression) (*Image, error) {
|
|
| 54 |
- layer, err := store.Layers.AddLayer(archive, stderr, compression) |
|
| 55 |
- if err != nil {
|
|
| 56 |
- return nil, err |
|
| 57 |
- } |
|
| 58 |
- layers := []string{layer}
|
|
| 59 |
- if parent != nil {
|
|
| 60 |
- layers = append(layers, parent.Layers...) |
|
| 61 |
- } |
|
| 62 |
- var parentId string |
|
| 63 |
- if parent != nil {
|
|
| 64 |
- parentId = parent.Id |
|
| 65 |
- } |
|
| 66 |
- return store.Create(name, parentId, layers...) |
|
| 67 |
-} |
|
| 68 |
- |
|
| 69 |
-func (store *Store) Create(name string, source string, layers ...string) (*Image, error) {
|
|
| 70 |
- image, err := NewImage(name, layers, source) |
|
| 71 |
- if err != nil {
|
|
| 72 |
- return nil, err |
|
| 73 |
- } |
|
| 74 |
- if err := store.Index.Add(name, image); err != nil {
|
|
| 75 |
- return nil, err |
|
| 76 |
- } |
|
| 77 |
- return image, nil |
|
| 78 |
-} |
|
| 79 |
- |
|
| 80 |
-// Index |
|
| 81 |
- |
|
| 82 |
-type Index struct {
|
|
| 83 |
- Path string |
|
| 84 |
- ByName map[string]*History |
|
| 85 |
- ById map[string]*Image |
|
| 86 |
-} |
|
| 87 |
- |
|
| 88 |
-func NewIndex(path string) *Index {
|
|
| 89 |
- return &Index{
|
|
| 90 |
- Path: path, |
|
| 91 |
- ByName: make(map[string]*History), |
|
| 92 |
- ById: make(map[string]*Image), |
|
| 93 |
- } |
|
| 94 |
-} |
|
| 95 |
- |
|
| 96 |
-func (index *Index) Exists(id string) bool {
|
|
| 97 |
- _, exists := index.ById[id] |
|
| 98 |
- return exists |
|
| 99 |
-} |
|
| 100 |
- |
|
| 101 |
-func (index *Index) Find(idOrName string) *Image {
|
|
| 102 |
- // Load |
|
| 103 |
- if err := index.load(); err != nil {
|
|
| 104 |
- return nil |
|
| 105 |
- } |
|
| 106 |
- // Lookup by ID |
|
| 107 |
- if image, exists := index.ById[idOrName]; exists {
|
|
| 108 |
- return image |
|
| 109 |
- } |
|
| 110 |
- // Lookup by name |
|
| 111 |
- if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 {
|
|
| 112 |
- return (*history)[0] |
|
| 113 |
- } |
|
| 114 |
- return nil |
|
| 115 |
-} |
|
| 116 |
- |
|
| 117 |
-func (index *Index) Add(name string, image *Image) error {
|
|
| 118 |
- // Load |
|
| 119 |
- if err := index.load(); err != nil {
|
|
| 120 |
- return err |
|
| 121 |
- } |
|
| 122 |
- if _, exists := index.ByName[name]; !exists {
|
|
| 123 |
- index.ByName[name] = new(History) |
|
| 124 |
- } else {
|
|
| 125 |
- // If this image is already the latest version, don't add it. |
|
| 126 |
- if (*index.ByName[name])[0].Id == image.Id {
|
|
| 127 |
- return nil |
|
| 128 |
- } |
|
| 129 |
- } |
|
| 130 |
- index.ByName[name].Add(image) |
|
| 131 |
- index.ById[image.Id] = image |
|
| 132 |
- // Save |
|
| 133 |
- if err := index.save(); err != nil {
|
|
| 134 |
- return err |
|
| 135 |
- } |
|
| 136 |
- return nil |
|
| 137 |
-} |
|
| 138 |
- |
|
| 139 |
-func (index *Index) Copy(srcNameOrId, dstName string) (*Image, error) {
|
|
| 140 |
- if srcNameOrId == "" || dstName == "" {
|
|
| 141 |
- return nil, errors.New("Illegal image name")
|
|
| 142 |
- } |
|
| 143 |
- // Load |
|
| 144 |
- if err := index.load(); err != nil {
|
|
| 145 |
- return nil, err |
|
| 146 |
- } |
|
| 147 |
- src := index.Find(srcNameOrId) |
|
| 148 |
- if src == nil {
|
|
| 149 |
- return nil, errors.New("No such image: " + srcNameOrId)
|
|
| 150 |
- } |
|
| 151 |
- dst, err := NewImage(dstName, src.Layers, src.Id) |
|
| 152 |
- if err != nil {
|
|
| 153 |
- return nil, err |
|
| 154 |
- } |
|
| 155 |
- if err := index.Add(dstName, dst); err != nil {
|
|
| 156 |
- return nil, err |
|
| 157 |
- } |
|
| 158 |
- // Save |
|
| 159 |
- if err := index.save(); err != nil {
|
|
| 160 |
- return nil, err |
|
| 161 |
- } |
|
| 162 |
- return dst, nil |
|
| 163 |
-} |
|
| 164 |
- |
|
| 165 |
-func (index *Index) Rename(oldName, newName string) error {
|
|
| 166 |
- // Load |
|
| 167 |
- if err := index.load(); err != nil {
|
|
| 168 |
- return err |
|
| 169 |
- } |
|
| 170 |
- if _, exists := index.ByName[oldName]; !exists {
|
|
| 171 |
- return errors.New("Can't rename " + oldName + ": no such image.")
|
|
| 172 |
- } |
|
| 173 |
- if _, exists := index.ByName[newName]; exists {
|
|
| 174 |
- return errors.New("Can't rename to " + newName + ": name is already in use.")
|
|
| 175 |
- } |
|
| 176 |
- index.ByName[newName] = index.ByName[oldName] |
|
| 177 |
- delete(index.ByName, oldName) |
|
| 178 |
- // Change the ID of all images, since they include the name |
|
| 179 |
- for _, image := range *index.ByName[newName] {
|
|
| 180 |
- if id, err := generateImageId(newName, image.Layers); err != nil {
|
|
| 181 |
- return err |
|
| 182 |
- } else {
|
|
| 183 |
- oldId := image.Id |
|
| 184 |
- image.Id = id |
|
| 185 |
- index.ById[id] = image |
|
| 186 |
- delete(index.ById, oldId) |
|
| 187 |
- } |
|
| 188 |
- } |
|
| 189 |
- // Save |
|
| 190 |
- if err := index.save(); err != nil {
|
|
| 191 |
- return err |
|
| 192 |
- } |
|
| 193 |
- return nil |
|
| 194 |
-} |
|
| 195 |
- |
|
| 196 |
-// Delete deletes all images with the name `name` |
|
| 197 |
-func (index *Index) Delete(name string) error {
|
|
| 198 |
- // Load |
|
| 199 |
- if err := index.load(); err != nil {
|
|
| 200 |
- return err |
|
| 201 |
- } |
|
| 202 |
- if _, exists := index.ByName[name]; !exists {
|
|
| 203 |
- return errors.New("No such image: " + name)
|
|
| 204 |
- } |
|
| 205 |
- // Remove from index lookup |
|
| 206 |
- for _, image := range *index.ByName[name] {
|
|
| 207 |
- delete(index.ById, image.Id) |
|
| 208 |
- } |
|
| 209 |
- // Remove from name lookup |
|
| 210 |
- delete(index.ByName, name) |
|
| 211 |
- // Save |
|
| 212 |
- if err := index.save(); err != nil {
|
|
| 213 |
- return err |
|
| 214 |
- } |
|
| 215 |
- return nil |
|
| 216 |
-} |
|
| 217 |
- |
|
| 218 |
-func (index *Index) Names() []string {
|
|
| 219 |
- if err := index.load(); err != nil {
|
|
| 220 |
- return []string{}
|
|
| 221 |
- } |
|
| 222 |
- var names []string |
|
| 223 |
- for name := range index.ByName {
|
|
| 224 |
- names = append(names, name) |
|
| 225 |
- } |
|
| 226 |
- sort.Strings(names) |
|
| 227 |
- return names |
|
| 228 |
-} |
|
| 229 |
- |
|
| 230 |
-func (index *Index) load() error {
|
|
| 231 |
- jsonData, err := ioutil.ReadFile(index.Path) |
|
| 232 |
- if err != nil {
|
|
| 233 |
- if os.IsNotExist(err) {
|
|
| 234 |
- return nil |
|
| 235 |
- } |
|
| 236 |
- return err |
|
| 237 |
- } |
|
| 238 |
- path := index.Path |
|
| 239 |
- if err := json.Unmarshal(jsonData, index); err != nil {
|
|
| 240 |
- return err |
|
| 241 |
- } |
|
| 242 |
- index.Path = path |
|
| 243 |
- return nil |
|
| 244 |
-} |
|
| 245 |
- |
|
| 246 |
-func (index *Index) save() error {
|
|
| 247 |
- jsonData, err := json.Marshal(index) |
|
| 248 |
- if err != nil {
|
|
| 249 |
- return err |
|
| 250 |
- } |
|
| 251 |
- if err := ioutil.WriteFile(index.Path, jsonData, 0600); err != nil {
|
|
| 252 |
- return err |
|
| 253 |
- } |
|
| 254 |
- return nil |
|
| 255 |
-} |
|
| 256 |
- |
|
| 257 |
-// History wraps an array of images so they can be sorted by date (most recent first) |
|
| 258 |
- |
|
| 259 |
-type History []*Image |
|
| 260 |
- |
|
| 261 |
-func (history *History) Len() int {
|
|
| 262 |
- return len(*history) |
|
| 263 |
-} |
|
| 264 |
- |
|
| 265 |
-func (history *History) Less(i, j int) bool {
|
|
| 266 |
- images := *history |
|
| 267 |
- return images[j].Created.Before(images[i].Created) |
|
| 268 |
-} |
|
| 269 |
- |
|
| 270 |
-func (history *History) Swap(i, j int) {
|
|
| 271 |
- images := *history |
|
| 272 |
- tmp := images[i] |
|
| 273 |
- images[i] = images[j] |
|
| 274 |
- images[j] = tmp |
|
| 275 |
-} |
|
| 276 |
- |
|
| 277 |
-func (history *History) Add(image *Image) {
|
|
| 278 |
- *history = append(*history, image) |
|
| 279 |
- sort.Sort(history) |
|
| 280 |
-} |
|
| 281 |
- |
|
| 282 |
-func (history *History) Del(id string) {
|
|
| 283 |
- for idx, image := range *history {
|
|
| 284 |
- if image.Id == id {
|
|
| 285 |
- *history = append((*history)[:idx], (*history)[idx+1:]...) |
|
| 286 |
- } |
|
| 287 |
- } |
|
| 288 |
-} |
|
| 289 |
- |
|
| 290 |
-type Image struct {
|
|
| 291 |
- Id string // Globally unique identifier |
|
| 292 |
- Layers []string // Absolute paths |
|
| 293 |
- Created time.Time |
|
| 294 |
- Parent string |
|
| 295 |
-} |
|
| 296 |
- |
|
| 297 |
-func (image *Image) IdParts() (string, string) {
|
|
| 298 |
- if len(image.Id) < 8 {
|
|
| 299 |
- return "", image.Id |
|
| 300 |
- } |
|
| 301 |
- hash := image.Id[len(image.Id)-8 : len(image.Id)] |
|
| 302 |
- name := image.Id[:len(image.Id)-9] |
|
| 303 |
- return name, hash |
|
| 304 |
-} |
|
| 305 |
- |
|
| 306 |
-func (image *Image) IdIsFinal() bool {
|
|
| 307 |
- return len(image.Layers) == 1 |
|
| 308 |
-} |
|
| 309 |
- |
|
| 310 |
-func generateImageId(name string, layers []string) (string, error) {
|
|
| 311 |
- if len(layers) == 0 {
|
|
| 312 |
- return "", errors.New("No layers provided.")
|
|
| 313 |
- } |
|
| 314 |
- var hash string |
|
| 315 |
- if len(layers) == 1 {
|
|
| 316 |
- hash = path.Base(layers[0]) |
|
| 317 |
- } else {
|
|
| 318 |
- var ids string |
|
| 319 |
- for _, layer := range layers {
|
|
| 320 |
- ids += path.Base(layer) |
|
| 321 |
- } |
|
| 322 |
- if h, err := future.ComputeId(strings.NewReader(ids)); err != nil {
|
|
| 323 |
- return "", err |
|
| 324 |
- } else {
|
|
| 325 |
- hash = h |
|
| 326 |
- } |
|
| 327 |
- } |
|
| 328 |
- return name + ":" + hash, nil |
|
| 329 |
-} |
|
| 330 |
- |
|
| 331 |
-func NewImage(name string, layers []string, parent string) (*Image, error) {
|
|
| 332 |
- id, err := generateImageId(name, layers) |
|
| 333 |
- if err != nil {
|
|
| 334 |
- return nil, err |
|
| 335 |
- } |
|
| 336 |
- return &Image{
|
|
| 337 |
- Id: id, |
|
| 338 |
- Layers: layers, |
|
| 339 |
- Created: time.Now(), |
|
| 340 |
- Parent: parent, |
|
| 341 |
- }, nil |
|
| 342 |
-} |