| ... | ... |
@@ -590,6 +590,10 @@ func postImagesGetCache(srv *Server, w http.ResponseWriter, r *http.Request, var |
| 590 | 590 |
if err != nil {
|
| 591 | 591 |
return err |
| 592 | 592 |
} |
| 593 |
+ if image == nil {
|
|
| 594 |
+ w.WriteHeader(http.StatusNotFound) |
|
| 595 |
+ return nil |
|
| 596 |
+ } |
|
| 593 | 597 |
apiId := &ApiId{Id: image.Id}
|
| 594 | 598 |
b, err := json.Marshal(apiId) |
| 595 | 599 |
if err != nil {
|
| ... | ... |
@@ -13,8 +13,7 @@ import ( |
| 13 | 13 |
) |
| 14 | 14 |
|
| 15 | 15 |
type BuilderClient struct {
|
| 16 |
- builder *Builder |
|
| 17 |
- cli *DockerCli |
|
| 16 |
+ cli *DockerCli |
|
| 18 | 17 |
|
| 19 | 18 |
image string |
| 20 | 19 |
maintainer string |
| ... | ... |
@@ -28,17 +27,20 @@ type BuilderClient struct {
|
| 28 | 28 |
|
| 29 | 29 |
func (b *BuilderClient) clearTmp(containers, images map[string]struct{}) {
|
| 30 | 30 |
for c := range containers {
|
| 31 |
- tmp := b.builder.runtime.Get(c) |
|
| 32 |
- b.builder.runtime.Destroy(tmp) |
|
| 31 |
+ if _, _, err := b.cli.call("DELETE", "/containers/"+c, nil); err != nil {
|
|
| 32 |
+ utils.Debugf("%s", err)
|
|
| 33 |
+ } |
|
| 33 | 34 |
utils.Debugf("Removing container %s", c)
|
| 34 | 35 |
} |
| 35 | 36 |
for i := range images {
|
| 36 |
- b.builder.runtime.graph.Delete(i) |
|
| 37 |
+ if _, _, err := b.cli.call("DELETE", "/images/"+i, nil); err != nil {
|
|
| 38 |
+ utils.Debugf("%s", err)
|
|
| 39 |
+ } |
|
| 37 | 40 |
utils.Debugf("Removing image %s", i)
|
| 38 | 41 |
} |
| 39 | 42 |
} |
| 40 | 43 |
|
| 41 |
-func (b *BuilderClient) From(name string) error {
|
|
| 44 |
+func (b *BuilderClient) CmdFrom(name string) error {
|
|
| 42 | 45 |
obj, statusCode, err := b.cli.call("GET", "/images/"+name+"/json", nil)
|
| 43 | 46 |
if statusCode == 404 {
|
| 44 | 47 |
if err := b.cli.hijack("POST", "/images/create?fromImage="+name, false); err != nil {
|
| ... | ... |
@@ -53,7 +55,7 @@ func (b *BuilderClient) From(name string) error {
|
| 53 | 53 |
return err |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 |
- img := &ApiImages{}
|
|
| 56 |
+ img := &ApiId{}
|
|
| 57 | 57 |
if err := json.Unmarshal(obj, img); err != nil {
|
| 58 | 58 |
return err |
| 59 | 59 |
} |
| ... | ... |
@@ -61,17 +63,17 @@ func (b *BuilderClient) From(name string) error {
|
| 61 | 61 |
return nil |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 |
-func (b *BuilderClient) Maintainer(name string) error {
|
|
| 64 |
+func (b *BuilderClient) CmdMaintainer(name string) error {
|
|
| 65 | 65 |
b.needCommit = true |
| 66 | 66 |
b.maintainer = name |
| 67 | 67 |
return nil |
| 68 | 68 |
} |
| 69 | 69 |
|
| 70 |
-func (b *BuilderClient) Run(args string) error {
|
|
| 70 |
+func (b *BuilderClient) CmdRun(args string) error {
|
|
| 71 | 71 |
if b.image == "" {
|
| 72 | 72 |
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
| 73 | 73 |
} |
| 74 |
- config, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", args}, b.builder.runtime.capabilities)
|
|
| 74 |
+ config, _, err := ParseRun([]string{b.image, "/bin/sh", "-c", args}, nil)
|
|
| 75 | 75 |
if err != nil {
|
| 76 | 76 |
return err |
| 77 | 77 |
} |
| ... | ... |
@@ -90,8 +92,49 @@ func (b *BuilderClient) Run(args string) error {
|
| 90 | 90 |
b.image = apiId.Id |
| 91 | 91 |
return nil |
| 92 | 92 |
} |
| 93 |
+ b.commit() |
|
| 94 |
+ return nil |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+func (b *BuilderClient) CmdEnv(args string) error {
|
|
| 98 |
+ b.needCommit = true |
|
| 99 |
+ tmp := strings.SplitN(args, " ", 2) |
|
| 100 |
+ if len(tmp) != 2 {
|
|
| 101 |
+ return fmt.Errorf("Invalid ENV format")
|
|
| 102 |
+ } |
|
| 103 |
+ key := strings.Trim(tmp[0], " ") |
|
| 104 |
+ value := strings.Trim(tmp[1], " ") |
|
| 105 |
+ |
|
| 106 |
+ for i, elem := range b.config.Env {
|
|
| 107 |
+ if strings.HasPrefix(elem, key+"=") {
|
|
| 108 |
+ b.config.Env[i] = key + "=" + value |
|
| 109 |
+ return nil |
|
| 110 |
+ } |
|
| 111 |
+ } |
|
| 112 |
+ b.config.Env = append(b.config.Env, key+"="+value) |
|
| 113 |
+ return nil |
|
| 114 |
+} |
|
| 93 | 115 |
|
| 94 |
- body, _, err = b.cli.call("POST", "/containers/create", b.config)
|
|
| 116 |
+func (b *BuilderClient) CmdCmd(args string) error {
|
|
| 117 |
+ b.needCommit = true |
|
| 118 |
+ b.config.Cmd = []string{"/bin/sh", "-c", args}
|
|
| 119 |
+ return nil |
|
| 120 |
+} |
|
| 121 |
+ |
|
| 122 |
+func (b *BuilderClient) CmdExpose(args string) error {
|
|
| 123 |
+ ports := strings.Split(args, " ") |
|
| 124 |
+ b.config.PortSpecs = append(ports, b.config.PortSpecs...) |
|
| 125 |
+ return nil |
|
| 126 |
+} |
|
| 127 |
+ |
|
| 128 |
+func (b *BuilderClient) CmdInsert(args string) error {
|
|
| 129 |
+ // FIXME: Reimplement this once the remove_hijack branch gets merged. |
|
| 130 |
+ // We need to retrieve the resulting Id |
|
| 131 |
+ return fmt.Errorf("INSERT not implemented")
|
|
| 132 |
+} |
|
| 133 |
+ |
|
| 134 |
+func (b *BuilderClient) commit() error {
|
|
| 135 |
+ body, _, err := b.cli.call("POST", "/containers/create", b.config)
|
|
| 95 | 136 |
if err != nil {
|
| 96 | 137 |
return err |
| 97 | 138 |
} |
| ... | ... |
@@ -134,53 +177,11 @@ func (b *BuilderClient) Run(args string) error {
|
| 134 | 134 |
} |
| 135 | 135 |
b.tmpImages[apiId.Id] = struct{}{}
|
| 136 | 136 |
b.image = apiId.Id |
| 137 |
- b.needCommit = false |
|
| 138 | 137 |
return nil |
| 139 | 138 |
} |
| 140 | 139 |
|
| 141 |
-func (b *BuilderClient) Env(args string) error {
|
|
| 142 |
- b.needCommit = true |
|
| 143 |
- tmp := strings.SplitN(args, " ", 2) |
|
| 144 |
- if len(tmp) != 2 {
|
|
| 145 |
- return fmt.Errorf("Invalid ENV format")
|
|
| 146 |
- } |
|
| 147 |
- key := strings.Trim(tmp[0], " ") |
|
| 148 |
- value := strings.Trim(tmp[1], " ") |
|
| 149 |
- |
|
| 150 |
- for i, elem := range b.config.Env {
|
|
| 151 |
- if strings.HasPrefix(elem, key+"=") {
|
|
| 152 |
- b.config.Env[i] = key + "=" + value |
|
| 153 |
- return nil |
|
| 154 |
- } |
|
| 155 |
- } |
|
| 156 |
- b.config.Env = append(b.config.Env, key+"="+value) |
|
| 157 |
- return nil |
|
| 158 |
-} |
|
| 159 |
- |
|
| 160 |
-func (b *BuilderClient) Cmd(args string) error {
|
|
| 161 |
- b.needCommit = true |
|
| 162 |
- b.config.Cmd = []string{"/bin/sh", "-c", args}
|
|
| 163 |
- return nil |
|
| 164 |
-} |
|
| 165 |
- |
|
| 166 |
-func (b *BuilderClient) Expose(args string) error {
|
|
| 167 |
- ports := strings.Split(args, " ") |
|
| 168 |
- b.config.PortSpecs = append(ports, b.config.PortSpecs...) |
|
| 169 |
- return nil |
|
| 170 |
-} |
|
| 171 |
- |
|
| 172 |
-func (b *BuilderClient) Insert(args string) error {
|
|
| 173 |
- // FIXME: Reimplement this once the remove_hijack branch gets merged. |
|
| 174 |
- // We need to retrieve the resulting Id |
|
| 175 |
- return fmt.Errorf("INSERT not implemented")
|
|
| 176 |
-} |
|
| 177 |
- |
|
| 178 |
-func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
|
| 140 |
+func (b *BuilderClient) Build(dockerfile io.Reader) (string, error) {
|
|
| 179 | 141 |
// defer b.clearTmp(tmpContainers, tmpImages) |
| 180 |
- |
|
| 181 |
- b := &BuilderClient{
|
|
| 182 |
- cli: NewDockerCli("0.0.0.0", 4243),
|
|
| 183 |
- } |
|
| 184 | 142 |
file := bufio.NewReader(dockerfile) |
| 185 | 143 |
for {
|
| 186 | 144 |
line, err := file.ReadString('\n')
|
| ... | ... |
@@ -204,7 +205,7 @@ func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
| 204 | 204 |
|
| 205 | 205 |
fmt.Printf("%s %s\n", strings.ToUpper(instruction), arguments)
|
| 206 | 206 |
|
| 207 |
- method, exists := reflect.TypeOf(b).MethodByName(strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:])) |
|
| 207 |
+ method, exists := reflect.TypeOf(b).MethodByName("Cmd" + strings.ToUpper(instruction[:1]) + strings.ToLower(instruction[1:]))
|
|
| 208 | 208 |
if !exists {
|
| 209 | 209 |
fmt.Printf("Skipping unknown instruction %s\n", strings.ToUpper(instruction))
|
| 210 | 210 |
} |
| ... | ... |
@@ -216,49 +217,7 @@ func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
| 216 | 216 |
fmt.Printf("===> %v\n", b.image)
|
| 217 | 217 |
} |
| 218 | 218 |
if b.needCommit {
|
| 219 |
- body, _, err = b.cli.call("POST", "/containers/create", b.config)
|
|
| 220 |
- if err != nil {
|
|
| 221 |
- return err |
|
| 222 |
- } |
|
| 223 |
- |
|
| 224 |
- out := &ApiRun{}
|
|
| 225 |
- err = json.Unmarshal(body, out) |
|
| 226 |
- if err != nil {
|
|
| 227 |
- return err |
|
| 228 |
- } |
|
| 229 |
- |
|
| 230 |
- for _, warning := range out.Warnings {
|
|
| 231 |
- fmt.Fprintln(os.Stderr, "WARNING: ", warning) |
|
| 232 |
- } |
|
| 233 |
- |
|
| 234 |
- //start the container |
|
| 235 |
- _, _, err = b.cli.call("POST", "/containers/"+out.Id+"/start", nil)
|
|
| 236 |
- if err != nil {
|
|
| 237 |
- return err |
|
| 238 |
- } |
|
| 239 |
- b.tmpContainers[out.Id] = struct{}{}
|
|
| 240 |
- |
|
| 241 |
- // Wait for it to finish |
|
| 242 |
- _, _, err = b.cli.call("POST", "/containers/"+out.Id+"/wait", nil)
|
|
| 243 |
- if err != nil {
|
|
| 244 |
- return err |
|
| 245 |
- } |
|
| 246 |
- |
|
| 247 |
- // Commit the container |
|
| 248 |
- v := url.Values{}
|
|
| 249 |
- v.Set("container", out.Id)
|
|
| 250 |
- v.Set("author", b.maintainer)
|
|
| 251 |
- body, _, err = b.cli.call("POST", "/commit?"+v.Encode(), b.config)
|
|
| 252 |
- if err != nil {
|
|
| 253 |
- return err |
|
| 254 |
- } |
|
| 255 |
- apiId := &ApiId{}
|
|
| 256 |
- err = json.Unmarshal(body, apiId) |
|
| 257 |
- if err != nil {
|
|
| 258 |
- return err |
|
| 259 |
- } |
|
| 260 |
- b.tmpImages[apiId.Id] = struct{}{}
|
|
| 261 |
- b.image = apiId.Id |
|
| 219 |
+ b.commit() |
|
| 262 | 220 |
} |
| 263 | 221 |
if b.image != "" {
|
| 264 | 222 |
// The build is successful, keep the temporary containers and images |
| ... | ... |
@@ -273,3 +232,12 @@ func NewBuilderClient(dockerfile io.Reader) (string, error) {
|
| 273 | 273 |
} |
| 274 | 274 |
return "", fmt.Errorf("An error occured during the build\n")
|
| 275 | 275 |
} |
| 276 |
+ |
|
| 277 |
+func NewBuilderClient(addr string, port int) *BuilderClient {
|
|
| 278 |
+ return &BuilderClient{
|
|
| 279 |
+ cli: NewDockerCli(addr, port), |
|
| 280 |
+ config: &Config{},
|
|
| 281 |
+ tmpContainers: make(map[string]struct{}),
|
|
| 282 |
+ tmpImages: make(map[string]struct{}),
|
|
| 283 |
+ } |
|
| 284 |
+} |