Ejemplo n.º 1
0
/*
* Delete command kills the container by talking to swarm cluster and giving
* the container ID.
*
 */
func (i *Docker) Delete(assembly *global.AssemblyWithComponents, id string) (string, error) {

	pair_endpoint, perrscm := global.ParseKeyValuePair(assembly.Inputs, "endpoint")
	if perrscm != nil {
		log.Error("Failed to get the endpoint value : %s", perrscm)
	}

	pair_id, iderr := global.ParseKeyValuePair(assembly.Components[0].Outputs, "id")
	if iderr != nil {
		log.Error("Failed to get the endpoint value : %s", iderr)
	}

	var endpoint string
	if pair_endpoint.Value == BAREMETAL {

		api_host, _ := config.GetString("swarm:host")
		endpoint = api_host

	} else {
		endpoint = pair_endpoint.Value
	}

	client, _ := docker.NewClient(endpoint)
	kerr := client.KillContainer(docker.KillContainerOptions{ID: pair_id.Value})
	if kerr != nil {
		log.Error("Failed to kill the container : %s", kerr)
		return "", kerr
	}
	log.Info("Container is killed")
	return "", nil
}
Ejemplo n.º 2
0
func dockerStateHandler(chann []byte) {

	Msg := &Message{}
	parse_err := json.Unmarshal(chann, &Msg)
	log.Info(parse_err)
	if parse_err != nil {
		log.Error("Error: Message parsing error:\n%s.", parse_err)
		return
	}

	apprequest := global.AppRequest{Id: Msg.Id}
	req, err := apprequest.Get(Msg.Id)
	log.Info(req)
	if err != nil {
		log.Error("Error: Riak didn't cooperate:\n%s.", err)
		return
	}

	assembly := global.Assembly{Id: req.AppId}
	asm, err := assembly.GetAssemblyWithComponents(req.AppId)
	if err != nil {
		log.Error("Error: Riak didn't cooperate:\n%s.", err)
		return
	}

	cont_id, perrscm := global.ParseKeyValuePair(asm.Components[0].Outputs, "id")
	if perrscm != nil {
		log.Error("Failed to get the container id : %s", perrscm)
	}
	endpoint, perrscm := global.ParseKeyValuePair(asm.Components[0].Outputs, "endpoint")
	if perrscm != nil {
		log.Error("Failed to get the container id : %s", perrscm)
	}

	pair_cpu, perrscm := global.ParseKeyValuePair(asm.Inputs, "cpu")
	if perrscm != nil {
		log.Error("Failed to get the cpu value : %s", perrscm)
	}

	pair_memory, iderr := global.ParseKeyValuePair(asm.Inputs, "ram")
	if iderr != nil {
		log.Error("Failed to get the memory value : %s", iderr)
	}

	switch req.Action {
	case "start":
		log.Info("Starting Container")
		go docker.StartContainer(&global.Container{ContainerID: cont_id.Value, Cpu: pair_cpu.Value, Ram: pair_memory.Value}, endpoint.Value)
		break
	case "stop":
		log.Info("Stopping Container")
		go docker.StopContainer(&global.Container{ContainerID: cont_id.Value}, endpoint.Value)
		break
	case "restart":
		log.Info("Restarting container")
		go docker.RestartContainer(&global.Container{ContainerID: cont_id.Value}, endpoint.Value)
		break
	}
}
Ejemplo n.º 3
0
/*
* delete the machine from megam server using knife opennebula plugin
 */
func (i *MegamIaaS) DeleteMachine(pdc *global.PredefClouds, assembly *global.AssemblyWithComponents) (string, error) {

	accesskey, err_accesskey := config.GetString("opennebula:access_key")
	if err_accesskey != nil {
		return "", err_accesskey
	}

	secretkey, err_secretkey := config.GetString("opennebula:secret_key")
	if err_secretkey != nil {
		return "", err_secretkey
	}

	str, err := buildDelCommand(iaas.GetPlugins("opennebula"), pdc, "delete")
	if err != nil {
		return "", err
	}
	//str = str + " -P " + " -y "
	pair, perr := global.ParseKeyValuePair(assembly.Inputs, "domain")
	if perr != nil {
		log.Error("Failed to get the domain value : %s", perr)
	}
	str = str + " -N " + assembly.Name + "." + pair.Value
	str = str + " -A " + accesskey
	str = str + " -K " + secretkey

	knifePath, kerr := config.GetString("knife:path")
	if kerr != nil {
		return "", kerr
	}
	str = strings.Replace(str, " -c ", " -c "+knifePath+" ", -1)
	str = strings.Replace(str, "<node_name>", assembly.Name+"."+pair.Value, -1)

	return str, nil
}
Ejemplo n.º 4
0
func CommandExecutor(app *global.AssemblyWithComponents) (action.Result, error) {
	var e exec.OsExecutor
	var commandWords []string
	appName := ""
	commandWords = strings.Fields(app.Command)
	log.Debug("Command Executor entry: %s\n", app)
	megam_home, ckberr := config.GetString("megam_home")
	if ckberr != nil {
		return nil, ckberr
	}
	pair, perr := global.ParseKeyValuePair(app.Inputs, "domain")
	if perr != nil {
		log.Error("Failed to get the domain value : %s", perr)
	}

	appName = app.Name + "." + pair.Value

	basePath := megam_home + "logs"
	dir := path.Join(basePath, appName)

	fileOutPath := path.Join(dir, appName+"_out")
	fileErrPath := path.Join(dir, appName+"_err")
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		log.Info("Creating directory: %s\n", dir)
		if errm := os.MkdirAll(dir, 0777); errm != nil {
			return nil, errm
		}
	}
	// open output file
	fout, outerr := os.OpenFile(fileOutPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if outerr != nil {
		return nil, outerr
	}
	defer fout.Close()
	// open Error file
	ferr, errerr := os.OpenFile(fileErrPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if errerr != nil {
		return nil, errerr
	}
	defer ferr.Close()

	foutwriter := bufio.NewWriterSize(fout, 1)
	ferrwriter := bufio.NewWriterSize(ferr, 1)
	log.Debug(commandWords)
	log.Debug("Length: %s", len(commandWords))

	defer ferrwriter.Flush()
	defer foutwriter.Flush()

	if len(commandWords) > 0 {
		if err := e.Execute(commandWords[0], commandWords[1:], nil, foutwriter, ferrwriter); err != nil {
			return nil, err
		}
	}

	return &app, nil
}
Ejemplo n.º 5
0
func LauncherHelper(asm *global.AssemblyWithComponents, id string, instance bool, act_id string) error {
	pair_host, perrscm := global.ParseKeyValuePair(asm.Inputs, "provider")
	if perrscm != nil {
		log.Error("Failed to get the host value : %s", perrscm)
	}

	if pair_host.Value == "docker" {

		log.Debug("Docker provisioner entry")
		// Provisioner
		p, err := provisioner.GetProvisioner("docker")
		if err != nil {
			return err
		}
		log.Info("Provisioner: %v", p)
		_, perr := p.Create(asm, id, instance, act_id)
		if perr != nil {
			return perr
		}
	}

	if pair_host.Value == "chef" {

		p, err := provisioner.GetProvisioner("chef")
		if err != nil {
			return err
		}

		str, perr := p.Create(asm, id, instance, act_id)
		if perr != nil {
			return perr
		}
		asm.Command = str
		actions := []*action.Action{&launchedApp}

		pipeline := action.NewPipeline(actions...)
		aerr := pipeline.Execute(asm)
		if aerr != nil {
			return aerr
		}
	}
	return nil
}
Ejemplo n.º 6
0
func cioperation(asm *global.AssemblyWithComponents, ci *global.Operations, com *global.Component) error {
	pair_scm, perrscm := global.ParseKeyValuePair(ci.OperationRequirements, "ci-scm")
	if perrscm != nil {
		log.Error("Failed to get the domain value : %s", perrscm)
	}

	pair_enable, perrenable := global.ParseKeyValuePair(ci.OperationRequirements, "ci-enable")
	if perrenable != nil {
		log.Error("Failed to get the domain value : %s", perrenable)
	}

	if pair_scm.Value == GITHUB && pair_enable.Value == ENABLE {
		log.Info("Github is working")

		pair_token, perrtoken := global.ParseKeyValuePair(ci.OperationRequirements, "ci-token")
		if perrtoken != nil {
			log.Error("Failed to get the domain value : %s", perrtoken)
		}
		t := &oauth.Transport{
			Token: &oauth.Token{AccessToken: pair_token.Value},
		}

		client := git.NewClient(t.Client())

		api_host, apierr := config.GetString("api:host")
		if apierr != nil {
			return apierr
		}

		api_version, apiverr := config.GetString("api:version")
		if apiverr != nil {
			return apiverr
		}
		trigger_url := "http://" + api_host + "/" + api_version + "/assembly/build/" + asm.Id + "/" + com.Id

		byt12 := []byte(`{"url": "","content_type": "json"}`)
		var postData map[string]interface{}
		if perr := json.Unmarshal(byt12, &postData); perr != nil {
			return perr
		}
		postData["url"] = trigger_url

		byt1 := []byte(`{"name": "web", "active": true, "events": [ "push" ]}`)
		postHook := git.Hook{Config: postData}
		if perr := json.Unmarshal(byt1, &postHook); perr != nil {
			log.Info(perr)
		}

		pair_source, perrsource := global.ParseKeyValuePair(com.Inputs, "source")
		if perrsource != nil {
			log.Error("Failed to get the domain value : %s", perrsource)
		}

		pair_owner, perrowner := global.ParseKeyValuePair(ci.OperationRequirements, "ci-owner")
		if perrowner != nil {
			log.Error("Failed to get the domain value : %s", perrowner)
		}

		source := strings.Split(pair_source.Value, "/")
		_, _, err := client.Repositories.CreateHook(pair_owner.Value, strings.TrimRight(source[len(source)-1], ".git"), &postHook)

		if err != nil {
			return err
		}

	} else {
		log.Info("Github is skipped")
	}
	return nil
}
Ejemplo n.º 7
0
/*
* create the machine into megam server using knife opennebula plugin
 */
func (i *MegamIaaS) CreateMachine(pdc *global.PredefClouds, assembly *global.AssemblyWithComponents, act_id string) (string, error) {
	log.Info("Megam provider create entry")
	accesskey, err_accesskey := config.GetString("opennebula:access_key")
	if err_accesskey != nil {
		return "", err_accesskey
	}

	secretkey, err_secretkey := config.GetString("opennebula:secret_key")
	if err_secretkey != nil {
		return "", err_secretkey
	}

	str, err := buildCommand(assembly)
	if err != nil {
		return "", err
	}

	pair, perr := global.ParseKeyValuePair(assembly.Inputs, "domain")
	if perr != nil {
		log.Error("Failed to get the domain value : %s", perr)
	}

	knifePath, kerr := config.GetString("knife:path")
	if kerr != nil {
		return "", kerr
	}

	str = str + " -c " + knifePath
	str = str + " -N " + assembly.Name + "." + pair.Value
	str = str + " -A " + accesskey
	str = str + " -K " + secretkey

	recipe, err_recipe := config.GetString("knife:recipe")
	if err_recipe != nil {
		return "", err_recipe
	}

	riakHost, err_riakHost := config.GetString("launch:riak")
	if err_riakHost != nil {
		return "", err_riakHost
	}

	rabbitmqHost, err_rabbitmq := config.GetString("launch:rabbitmq")
	if err_rabbitmq != nil {
		return "", err_rabbitmq
	}

	monitor, err_monitor := config.GetString("launch:monitor")
	if err_monitor != nil {
		return "", err_monitor
	}

	kibana, err_kibana := config.GetString("launch:kibana")
	if err_kibana != nil {
		return "", err_kibana
	}

	etcdHost, err_etcd := config.GetString("launch:etcd")
	if err_etcd != nil {
		return "", err_etcd
	}

	str = str + " --run-list recipe[" + recipe + "]"
	attributes := &iaas.Attributes{RiakHost: riakHost, AccountID: act_id, AssemblyID: assembly.Id, RabbitMQ: rabbitmqHost, MonitorHost: monitor, KibanaHost: kibana, EtcdHost: etcdHost}
	b, aerr := json.Marshal(attributes)
	if aerr != nil {
		return "", aerr
	}
	str = str + " --json-attributes " + string(b)

	return str, nil

}
Ejemplo n.º 8
0
/*
* Build the knife opennebula server create command
 */
func buildCommand(assembly *global.AssemblyWithComponents) (string, error) {
	var buffer bytes.Buffer
	buffer.WriteString("knife ")
	buffer.WriteString("opennebula ")
	buffer.WriteString("server ")
	buffer.WriteString("create")

	templatekey := ""
	if len(assembly.Components) > 0 {
		megamtemplatekey, err_templatekey := config.GetString("opennebula:default_template_name")
		if err_templatekey != nil {
			return "", err_templatekey
		}
		templatekey = megamtemplatekey
	} else {
		atype := make([]string, 3)
		atype = strings.Split(assembly.ToscaType, ".")
		pair, perr := global.ParseKeyValuePair(assembly.Inputs, "version")
		if perr != nil {
			log.Error("Failed to get the version : %s", perr)
		}
		templatekey = "megam_" + atype[2] + "_" + pair.Value
	}

	if len(templatekey) > 0 {
		buffer.WriteString(" --template-name " + templatekey)
	} else {
		return "", fmt.Errorf("Template doesn't loaded")
	}

	sshuserkey, err_sshuserkey := config.GetString("opennebula:ssh_user")
	if err_sshuserkey != nil {
		return "", err_sshuserkey
	}
	if len(sshuserkey) > 0 {
		buffer.WriteString(" -x " + sshuserkey)
	} else {
		return "", fmt.Errorf("Ssh user value doesn't loaded")
	}

	identityfilekey, err_identityfilekey := config.GetString("opennebula:identity_file")
	if err_identityfilekey != nil {
		return "", err_identityfilekey
	}
	if len(identityfilekey) > 0 {
		buffer.WriteString(" --identity-file " + identityfilekey)
	} else {
		return "", fmt.Errorf("Identity file doesn't loaded")
	}

	zonekey, err_zonekey := config.GetString("opennebula:zone")
	if err_zonekey != nil {
		return "", err_zonekey
	}
	if len(zonekey) > 0 {
		buffer.WriteString(" --endpoint " + zonekey)
	} else {
		return "", fmt.Errorf("Zone doesn't loaded")
	}

	return buffer.String(), nil
}
Ejemplo n.º 9
0
func (i *Docker) Create(assembly *global.AssemblyWithComponents, id string, instance bool, act_id string) (string, error) {
	log.Info("%q", assembly)
	pair_endpoint, perrscm := global.ParseKeyValuePair(assembly.Inputs, "endpoint")
	if perrscm != nil {
		log.Error("Failed to get the endpoint value : %s", perrscm)
		return "", perrscm
	}

	pair_img, perrscm := global.ParseKeyValuePair(assembly.Components[0].Inputs, "source")
	if perrscm != nil {
		log.Error("Failed to get the image value : %s", perrscm)
		return "", perrscm
	}

	pair_domain, perrdomain := global.ParseKeyValuePair(assembly.Components[0].Inputs, "domain")
	if perrdomain != nil {
		log.Error("Failed to get the image value : %s", perrdomain)
		return "", perrdomain
	}

	var endpoint string
	if pair_endpoint.Value == BAREMETAL {

		/*
		 * swarm host is obtained from conf file. Swarm host is considered
		 * only when the 'endpoint' is baremetal in the Component JSON
		 */
		api_host, _ := config.GetString("swarm:host")
		endpoint = api_host
	} else {
		endpoint = pair_endpoint.Value
	}
	/*
	 * Docker API client to connect to swarm. Swarm supports all docker API endpoints
	 */
	client, _ := docker.NewClient(endpoint)

	opts := docker.PullImageOptions{
		Repository: pair_img.Value,
	}
	pullerr := client.PullImage(opts, docker.AuthConfiguration{})
	if pullerr != nil {
		log.Error(pullerr)
	}

	/*
	 * Inspect image to get the default internal port to ExposedPorts
	 * the running internal port to external port
	 *
	 */

	img, err := client.InspectImage(pair_img.Value)
	if err != nil {
		log.Error("Inspect image failed : %s", err)
	}
	InspectImg := &docker.Image{}
	mapFP, _ := json.Marshal(img)
	json.Unmarshal([]byte(string(mapFP)), InspectImg)
	conf := InspectImg.Config

	var Iport string

	for k, _ := range conf.ExposedPorts {
		port := strings.Split(string(k), "/")
		Iport = port[0]

	}

	/*	var exposedPorts map[docker.Port]struct{}
		exposedPorts = map[docker.Port]struct{}{
			docker.Port(Iport + "/tcp"): {},
		} */

	config := docker.Config{Image: pair_img.Value}
	copts := docker.CreateContainerOptions{Name: fmt.Sprint(assembly.Components[0].Name, ".", pair_domain.Value), Config: &config}

	/*
	 * Creation of the container with copts.
	 */

	container, conerr := client.CreateContainer(copts)
	if conerr != nil {
		log.Error("Container creation failed : %s", conerr)
		return "", conerr
	}

	cont := &docker.Container{}
	mapP, _ := json.Marshal(container)
	json.Unmarshal([]byte(string(mapP)), cont)

	/*
	 * hostConfig{} stuct for portbindings - to expose visible ports
	 *  Also for specfying the container configurations (memory, cpuquota etc)
	 */

	hostConfig := docker.HostConfig{
	//Memory: GetMemory(),
	//MemorySwap: GetMemory() + GetSwap(),
	//CPUQuota:  GetCpuQuota(),
	//CPUPeriod: GetCpuPeriod(),
	}
	hostConfig.PortBindings = map[docker.Port][]docker.PortBinding{
		docker.Port(Iport + "/tcp"): {{HostIP: "", HostPort: ""}},
	}

	/*
	 *   Starting container once the container is created - container ID &
	 *   hostConfig is proivided to start the container.
	 *
	 */
	serr := client.StartContainer(cont.ID, &hostConfig)
	if serr != nil {
		log.Error("Start container was failed : %s", serr)
		return "", serr
	}

	/*
	 * Inspect API is called to fetch the data about the launched container
	 *
	 */
	inscontainer, _ := client.InspectContainer(cont.ID)
	contain := &docker.Container{}
	mapC, _ := json.Marshal(inscontainer)
	json.Unmarshal([]byte(string(mapC)), contain)

	container_network := &docker.NetworkSettings{}
	mapN, _ := json.Marshal(contain.NetworkSettings)
	json.Unmarshal([]byte(string(mapN)), container_network)

	configs := &docker.Config{}
	mapPort, _ := json.Marshal(contain.Config)
	json.Unmarshal([]byte(string(mapPort)), configs)

	var port string

	for k, _ := range container_network.Ports {
		porti := strings.Split(string(k), "/")
		port = porti[0]
	}
	fmt.Println(port)

	updatecomponent(assembly, container_network.IPAddress, cont.ID, port)

	herr := setHostName(fmt.Sprint(assembly.Components[0].Name, ".", pair_domain.Value), container_network.IPAddress)
	if herr != nil {
		log.Error("Failed to set the host name : %s", herr)
		return "", herr
	}

	return "", nil
}
Ejemplo n.º 10
0
func cioperation(asm *global.AssemblyWithComponents, ci *global.Operations, com *global.Component) error {
	pair_scm, perrscm := global.ParseKeyValuePair(ci.OperationRequirements, "ci-scm")
	if perrscm != nil {
		log.Error("Failed to get the domain value : %s", perrscm)
	}

	pair_enable, perrenable := global.ParseKeyValuePair(ci.OperationRequirements, "ci-enable")
	if perrenable != nil {
		log.Error("Failed to get the domain value : %s", perrenable)
	}

	if pair_scm.Value == GOGS && pair_enable.Value == ENABLE {
		log.Info("gogs process started...")

		api_host, apierr := config.GetString("api:host")
		if apierr != nil {
			return apierr
		}

		api_version, apiverr := config.GetString("api:version")
		if apiverr != nil {
			return apiverr
		}

		trigger_url := "http://" + api_host + "/" + api_version + "/assembly/build/" + asm.Id + "/" + com.Id

		url, herr := config.GetString("scm:gogs:url")
		if herr != nil {
			return herr
		}

		pair_token, perrtoken := global.ParseKeyValuePair(ci.OperationRequirements, "ci-token")
		if perrtoken != nil {
			log.Error("Failed to get the domain value : %s", perrtoken)
		}

		client := gogs.NewClient(url, pair_token.Value)
		log.Info("Gogs api client created")

		var postData = make(map[string]string)
		postData["url"] = trigger_url
		postData["content_type"] = "json"

		postHook := gogs.CreateHookOption{Type: "gogs", Config: postData, Active: true}

		pair_source, perrsource := global.ParseKeyValuePair(com.Inputs, "source")
		if perrsource != nil {
			log.Error("Failed to get the domain value : %s", perrsource)
		}

		source := strings.Split(pair_source.Value, "/")
		log.Info(strings.Replace(source[len(source)-1], ".git", "", -1))

		pair_owner, perrowner := global.ParseKeyValuePair(ci.OperationRequirements, "ci-owner")
		if perrowner != nil {
			log.Error("Failed to get the domain value : %s", perrowner)
		}

		s, hook_err := client.CreateRepoHook(pair_owner.Value, strings.Replace(source[len(source)-1], ".git", "", -1), postHook)
		if hook_err != nil {
			return hook_err
		}
		//s, _ := client.ListRepoHooks(ci.Owner, strings.Replace(source[len(source)-1], ".git", "", -1))

		log.Info("Hook created")
		log.Info(s)
	} else {
		log.Info("gogs is skipped")
	}

	return nil
}
Ejemplo n.º 11
0
func cioperation(asm *global.AssemblyWithComponents, ci *global.Operations, com *global.Component) error {

	pair_scm, perrscm := global.ParseKeyValuePair(ci.OperationRequirements, "ci-scm")
	if perrscm != nil {
		log.Error("Failed to get the domain value : %s", perrscm)
	}

	pair_enable, perrenable := global.ParseKeyValuePair(ci.OperationRequirements, "ci-enable")
	if perrenable != nil {
		log.Error("Failed to get the domain value : %s", perrenable)
	}

	if pair_scm.Value == GITLAB && pair_enable.Value == ENABLE {
		log.Info("GitLab is working..")

		pair_token, perrtoken := global.ParseKeyValuePair(ci.OperationRequirements, "ci-token")
		if perrtoken != nil {
			log.Error("Failed to get the ci-token value : %s", perrtoken)

		}

		pair_url, perrtoken := global.ParseKeyValuePair(ci.OperationRequirements, "ci-url")
		if perrtoken != nil {
			log.Error("Failed to get the ci-url value : %s", perrtoken)

		}
		pair_apiversion, perrtoken := global.ParseKeyValuePair(ci.OperationRequirements, "ci-apiversion")
		if perrtoken != nil {
			log.Error("Failed to get the ci-apiversion value : %s", perrtoken)

		}

		pair_owner, perrowner := global.ParseKeyValuePair(ci.OperationRequirements, "ci-owner")
		if perrowner != nil {
			log.Error("Failed to get the ci-owner value : %s", perrowner)
		}

		api_host, apierr := config.GetString("api:host")
		if apierr != nil {
			return apierr
		}

		api_version, apiverr := config.GetString("api:version")
		if apiverr != nil {
			return apiverr
		}

		trigger_url := "http://" + api_host + "/" + api_version + "/assembly/build/" + asm.Id + "/" + com.Id

		client := gogitlab.NewGitlab(pair_url.Value, pair_apiversion.Value, pair_token.Value)

		err := client.AddProjectHook(pair_owner.Value, trigger_url, false, false, false)
		if err != nil {
			return err
		}

	} else {
		log.Info("GitLab is skipped")
	}
	return nil

}
Ejemplo n.º 12
0
func (i *Docker) Create(assembly *global.AssemblyWithComponents, id string, instance bool, act_id string) (string, error) {
	log.Info("%q", assembly)
	pair_endpoint, perrscm := global.ParseKeyValuePair(assembly.Inputs, "endpoint")
	if perrscm != nil {
		log.Error("Failed to get the endpoint value : %s", perrscm)
		return "", perrscm
	}

	var endpoint string
	if pair_endpoint.Value == BAREMETAL {

		/*
		 * swarm host is obtained from conf file. Swarm host is considered
		 * only when the 'endpoint' is baremetal in the Component JSON
		 */
		api_host, _ := config.GetString("docker:swarm_host")
		endpoint = api_host

		container, cerr := create(assembly, endpoint)
		if cerr != nil {
			log.Error("container creation was failed : %s", cerr)
			return "", cerr
		}

		pair_cpu, perrscm := global.ParseKeyValuePair(assembly.Inputs, "cpu")
		if perrscm != nil {
			log.Error("Failed to get the endpoint value : %s", perrscm)
		}

		pair_memory, iderr := global.ParseKeyValuePair(assembly.Inputs, "ram")
		if iderr != nil {
			log.Error("Failed to get the endpoint value : %s", iderr)
		}

		container.Cpu = pair_cpu.Value
		container.Ram = pair_memory.Value

		serr := StartContainer(container, endpoint)
		if serr != nil {
			log.Error("container starting error : %s", serr)
			return "", serr
		}

		ipaddress, iperr := setContainerNAL(container)
		if iperr != nil {
			log.Error("set container network was failed : %s", iperr)
			return "", iperr
		}

		container.IPAddress = ipaddress

		herr := setHostName(container)
		if herr != nil {
			log.Error("set host name error : %s", herr)
		}

		updateContainerJSON(assembly, container, endpoint)
	} else {
		endpoint = pair_endpoint.Value
		create(assembly, endpoint)
	}

	return "", nil
}
Ejemplo n.º 13
0
/*
* Docker API client to connect to swarm/docker VM.
* Swarm supports all docker API endpoints
 */
func create(assembly *global.AssemblyWithComponents, endpoint string) (*global.Container, error) {

	pair_img, perrscm := global.ParseKeyValuePair(assembly.Components[0].Inputs, "source")
	if perrscm != nil {
		log.Error("Failed to get the image value : %s", perrscm)
		return &global.Container{}, perrscm
	}

	pair_domain, perrdomain := global.ParseKeyValuePair(assembly.Components[0].Inputs, "domain")
	if perrdomain != nil {
		log.Error("Failed to get the image value : %s", perrdomain)
		return &global.Container{}, perrdomain
	}

	client, _ := docker.NewClient(endpoint)

	opts := docker.PullImageOptions{
		Repository: pair_img.Value,
	}
	pullerr := client.PullImage(opts, docker.AuthConfiguration{})
	if pullerr != nil {
		log.Error("Image pulled failed : %s", pullerr)
		return &global.Container{}, pullerr
	}

	dconfig := docker.Config{Image: pair_img.Value, NetworkDisabled: true}
	copts := docker.CreateContainerOptions{Name: fmt.Sprint(assembly.Components[0].Name, ".", pair_domain.Value), Config: &dconfig}

	/*
	 * Creation of the container with copts.
	 */

	container, conerr := client.CreateContainer(copts)
	if conerr != nil {
		log.Error("Container creation failed : %s", conerr)
		return &global.Container{}, conerr
	}

	cont := &docker.Container{}
	mapP, _ := json.Marshal(container)
	json.Unmarshal([]byte(string(mapP)), cont)

	inspect, _ := client.InspectContainer(cont.ID)
	contI := &docker.Container{}
	mapInsp, _ := json.Marshal(inspect)
	json.Unmarshal([]byte(string(mapInsp)), contI)

	ins, _ := client.InspectImage(pair_img.Value)
	conti := &docker.Image{}
	mapins, _ := json.Marshal(ins)
	json.Unmarshal([]byte(string(mapins)), conti)

	confi := &docker.Config{}
	mapc, _ := json.Marshal(conti.ContainerConfig)
	json.Unmarshal([]byte(string(mapc)), confi)

	//ports := make([]string, len(confi.ExposedPorts))
	var ports string
	for k, _ := range confi.ExposedPorts {
		//i := 0
		porti := strings.Split(string(k), "/")
		//ports[i] = porti[0]
		ports = porti[0] + ", " + ports
		//i++
	}

	swarmNode := &docker.SwarmNode{}
	mapSwarm, _ := json.Marshal(contI.Node)
	json.Unmarshal([]byte(string(mapSwarm)), swarmNode)

	return &global.Container{ContainerID: cont.ID, ContainerName: cont.Name, SwarmNode: swarmNode.IP, Ports: ports}, nil
}