func ContainerStop(job *engine.Job) engine.Status {
	if len(job.Args) != 1 {
		return job.Errorf("Usage: %s CONTAINER\n", job.Name)
	}
	var (
		name = job.Args[0]
	)
	v := url.Values{}

	if job.EnvExists("t") {
		v.Set("t", strconv.Itoa(job.GetenvInt("t")))
	}

	ship := shipWithContainerId(job, name)

	cli := client.NewKraneClientApi(*ship, false, job)

	if ship != nil {
		body, statusCode, err := readBody(cli.Call("POST", "/containers/"+name+"/stop?"+v.Encode(), nil, false))
		if statusCode == 304 {
			return job.Errorf("Container already stopped")
		} else if statusCode == 404 {
			return job.Errorf("No such container: %s\n", name)
		} else if statusCode == 404 {
			return job.Errorf("Cannot stop container %s: %s\n", name, err)
		} else if (statusCode >= 200) && (statusCode < 300) {
			fmt.Printf("%s", body)
		} else {
			return job.Errorf("Cannot stop container %s: %s\n", name, err)
		}
	} else {
		return job.Errorf("No such container: %s\n", name)
	}
	return engine.StatusOK
}
func ContainerCreate(job *engine.Job) engine.Status {
	configuration := job.Eng.Hack_GetGlobalVar("configuration").(types.KraneConfiguration)
	config := runconfig.ContainerConfigFromJob(job)

	if len(job.Args) != 2 {
		return job.Errorf("Usage: %s CONTAINER\n", job.Name)
	}

	ship := configuration.Production.Fleet.Find(job.Args[1])

	containerValues := url.Values{}
	containerValues.Set("name", job.Args[0])

	cli := client.NewKraneClientApi(ship, false, job)

	stream, statusCode, err := cli.Call("POST", "/containers/create?"+containerValues.Encode(), config, false)

	if statusCode == 404 {
		job.Printf("Unable to find image '%s' in %s://%s:%d\n", config.Image, ship.Schema, ship.Fqdn, ship.Port)

		if err = pullImage(job, config.Image, ship); err != nil {
			return job.Errorf("Cannot pull image %s: %s\n", config.Image, err)
		}
		if stream, _, err = cli.Call("POST", "/containers/create?"+containerValues.Encode(), config, false); err != nil {
			return job.Errorf("Cannot create container: %s\n", err)
		}
	} else if err != nil {
		return job.Errorf("Cannot create container: %s\n", err)
	}

	var runResult engine.Env

	if err := runResult.Decode(stream); err != nil {
		return job.Errorf("Error with container: %s\n", err)
	}

	for _, warning := range runResult.GetList("Warnings") {
		job.Stdout.Write([]byte(fmt.Sprintf("WARNING: %s\n", warning)))
	}

	job.Stdout.Write([]byte(runResult.Get("Id")))

	return engine.StatusOK
}
Exemple #3
0
func pullImage(job *dockerEngine.Job, image string, ship types.Ship) error {
	v := url.Values{}
	repos, tag := dockerPkgParser.ParseRepositoryTag(image)
	// pull only the image tagged 'latest' if no tag was specified
	if tag == "" {
		tag = "latest"
	}
	v.Set("fromImage", repos)
	v.Set("tag", tag)

	// // Resolve the Repository name from fqn to hostname + name
	// hostname, _, err := registry.ResolveRepositoryName(repos)
	// if err != nil {
	// 	return err
	// }

	// // Load the auth config file, to be able to pull the image
	// cli.LoadConfigFile()

	// // Resolve the Auth config relevant for this server
	// authConfig := cli.configFile.ResolveAuthConfig(hostname)
	// buf, err := json.Marshal(authConfig)
	// if err != nil {
	// 	return err
	// }

	// registryAuthHeader := []string{
	// 	base64.URLEncoding.EncodeToString(buf),
	// }
	//

	cli := client.NewKraneClientApi(ship, false, job)

	if err := cli.Stream("POST", "/images/create?"+v.Encode(), nil, job.Stdout, nil); err != nil {
		return err
	}
	return nil

}
Exemple #4
0
func listContainer(job *engine.Job, configuration types.KraneConfiguration) <-chan *types.Ship {
	v := url.Values{}
	if all := job.GetenvBool("all"); all {
		v.Set("all", strconv.FormatBool(all))
	}

	ch := make(chan *types.Ship, len(configuration.Production.Fleet.Ships()))
	for _, ship := range configuration.Production.Fleet.Available() {
		go func(ship types.Ship) {

			cli := client.NewKraneClientApi(ship, false, job)
			body, statusCode, err := readBody(cli.Call("GET", "/containers/json?"+v.Encode(), nil, false))

			job.Logf("(%d) %s\n", statusCode, body)

			if err != nil {
				job.Logf("Error: %s", err.Error())
			}
			var resultShip types.Ship
			if (statusCode >= 200) && (statusCode < 300) {
				var containerList []types.Containers
				json.Unmarshal(body, &containerList)
				resultShip.Name = ship.Name
				resultShip.Fqdn = ship.Fqdn
				resultShip.Port = ship.Port
				resultShip.State = "operational"
				resultShip.Containers = containerList
				fmt.Printf("%#v", resultShip)
				ch <- &resultShip
			} else {
				ch <- nil
			}
		}(ship)
	}
	return ch
}