forked from appc/docker2aci
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
163 lines (138 loc) · 4.4 KB
/
main.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
155
156
157
158
159
160
161
162
163
// Copyright 2015 CoreOS, Inc.
//
// 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 (
"flag"
"fmt"
"net/url"
"os"
"strings"
"github.com/appc/docker2aci/lib"
"github.com/appc/docker2aci/lib/util"
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
)
var (
flagNoSquash = flag.Bool("nosquash", false, "Don't squash layers and output every layer as ACI")
flagImage = flag.String("image", "", "When converting a local file, it selects a particular image to convert. Format: IMAGE_NAME[:TAG]")
flagDebug = flag.Bool("debug", false, "Enables debug messages")
)
func runDocker2ACI(arg string, flagNoSquash bool, flagImage string, flagDebug bool) error {
if flagDebug {
util.InitDebug()
}
squash := !flagNoSquash
var aciLayerPaths []string
// try to convert a local file
u, err := url.Parse(arg)
if err != nil {
return fmt.Errorf("error parsing argument: %v", err)
}
if u.Scheme == "docker" {
if flagImage != "" {
return fmt.Errorf("flag --image works only with files.")
}
dockerURL := strings.TrimPrefix(arg, "docker://")
indexServer := docker2aci.GetIndexName(dockerURL)
var username, password string
username, password, err = docker2aci.GetDockercfgAuth(indexServer)
if err != nil {
return fmt.Errorf("error reading .dockercfg file: %v", err)
}
aciLayerPaths, err = docker2aci.Convert(dockerURL, squash, ".", os.TempDir(), username, password)
} else {
aciLayerPaths, err = docker2aci.ConvertFile(flagImage, arg, squash, ".", os.TempDir())
}
if err != nil {
return fmt.Errorf("conversion error: %v", err)
}
// we get last layer's manifest, this will include all the elements in the
// previous layers. If we're squashing, the last element of aciLayerPaths
// will be the squashed image.
manifest, err := getManifest(aciLayerPaths[len(aciLayerPaths)-1])
if err != nil {
return err
}
if err := printConvertedVolumes(*manifest); err != nil {
return err
}
if err := printConvertedPorts(*manifest); err != nil {
return err
}
fmt.Printf("\nGenerated ACI(s):\n")
for _, aciFile := range aciLayerPaths {
fmt.Println(aciFile)
}
return nil
}
func printConvertedVolumes(manifest schema.ImageManifest) error {
if manifest.App != nil && manifest.App.MountPoints != nil {
mps := manifest.App.MountPoints
if len(mps) > 0 {
fmt.Printf("\nConverted volumes:\n")
for _, mp := range mps {
fmt.Printf("\tname: %q, path: %q, readOnly: %v\n", mp.Name, mp.Path, mp.ReadOnly)
}
}
}
return nil
}
func printConvertedPorts(manifest schema.ImageManifest) error {
if manifest.App != nil && manifest.App.Ports != nil {
ports := manifest.App.Ports
if len(ports) > 0 {
fmt.Printf("\nConverted ports:\n")
for _, port := range ports {
fmt.Printf("\tname: %q, protocol: %q, port: %v, count: %v, socketActivated: %v\n",
port.Name, port.Protocol, port.Port, port.Count, port.SocketActivated)
}
}
}
return nil
}
func getManifest(aciPath string) (*schema.ImageManifest, error) {
f, err := os.Open(aciPath)
if err != nil {
return nil, fmt.Errorf("error opening converted image: %v", err)
}
defer f.Close()
manifest, err := aci.ManifestFromImage(f)
if err != nil {
return nil, fmt.Errorf("error reading manifest from converted image: %v", err)
}
return manifest, nil
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "docker2aci [--debug] [--nosquash] IMAGE\n")
fmt.Fprintf(os.Stderr, " Where IMAGE is\n")
fmt.Fprintf(os.Stderr, " [--image=IMAGE_NAME[:TAG]] FILEPATH\n")
fmt.Fprintf(os.Stderr, " or\n")
fmt.Fprintf(os.Stderr, " docker://[REGISTRYURL/]IMAGE_NAME[:TAG]\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
usage()
return
}
if err := runDocker2ACI(args[0], *flagNoSquash, *flagImage, *flagDebug); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}