예제 #1
0
/*
Test with this curl command:

*/
func TaskCreate(w http.ResponseWriter, r *http.Request) {
	var v orchestrator.Graph
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		panic(err)
	}
	if err := r.Body.Close(); err != nil {
		panic(err)
	}
	if err := json.Unmarshal(body, &v); err != nil {
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		w.WriteHeader(http.StatusBadRequest) // unprocessable entity
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
		return
	}

	uuid := uuid()
	exe := orchestrator.ExecutorBackend{
		Name:        "self",
		Url:         "https://127.0.0.1:8585/v1",
		Certificate: "orchestrator.pem",
		Key:         "orchestrator_key.pem",
		CACert:      "executor.pem",
		Ping:        "/ping",
		Client:      nil,
	}
	exe.Init()

	go v.Run([]orchestrator.ExecutorBackend{exe})
	v.Timeout = time.After(5 * time.Minute)
	tasks[uuid.ID] = &v

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusAccepted)
	if err := json.NewEncoder(w).Encode(uuid); err != nil {
		panic(err)
	}
	return
}
예제 #2
0
func togorch(t toscalib.ServiceTemplateDefinition, operations []string) orchestrator.Graph {

	e := toscaexec.GeneratePlaybook(t)
	var g orchestrator.Graph
	g.Digraph = structure.Matrix(e.AdjacencyMatrix)
	g.Name = t.Description
	for i, n := range e.Index {
		var node orchestrator.Node
		node.ID = i
		node.Name = fmt.Sprintf("%v:%v", n.NodeTemplate.Name, n.OperationName)
		if n.OperationName == "noop" {
			node.Engine = "nil"
		} else {
			node.Engine = "toscassh"
		}
		torun := false
		for _, operation := range operations {
			if operation == n.OperationName {
				torun = true
			}
		}
		if !torun {
			node.Engine = "nil"
		}

		// Sets the target

		node.Target = getComputeTarget(t, n)
		attrTarget := getAttributeTarget(t, n)

		ctxlog := log.WithFields(logrus.Fields{
			"Node":   node.Name,
			"ID":     node.ID,
			"Target": node.Target,
		})
		node.Artifact = n.NodeTemplate.Interfaces[n.InterfaceName].Operations[n.OperationName].Implementation
		// Get inputs from the node type
		for argName, argValue := range t.NodeTypes[n.NodeTemplate.Type].Interfaces[n.InterfaceName][n.OperationName].Inputs {
			for get, val := range argValue.Value {
				switch get {
				case "value":
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, val[0]))
				case "get_input":
					value := e.Inputs[val[0]]
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, value))
				case "get_property":
					tgt := val[0]
					switch val[0] {
					case "SELF":
						tgt = n.NodeTemplate.Name
					case "HOST":
						tgt = node.Target
					}
					prop, err := t.GetProperty(tgt, val[1])
					vals, err := t.EvaluateStatement(prop)
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, vals))
					if err != nil {
						ctxlog.Warnf("Cannot find property %v on %v", val[1], val[0])
					}
				case "get_attribute":
					tgt := val[0]
					switch val[0] {
					case "SELF":
						tgt = n.NodeTemplate.Name
					case "HOST":
						tgt = attrTarget
					}
					node.Args = append(node.Args, fmt.Sprintf("%v=get_attribute %v.*:%v", argName, tgt, val[1]))
				default:
					node.Args = append(node.Args, fmt.Sprintf("DEBUG: %v=%v", argName, val))

				}
			}
		}
		// Get inputs from the node template
		for argName, argValue := range n.NodeTemplate.Interfaces[n.InterfaceName].Operations[n.OperationName].Inputs {
			for get, val := range argValue {
				switch get {
				case "value":
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, val[0]))
				case "get_input":
					value := e.Inputs[val[0]]
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, value))
				case "get_property":
					prop, err := t.GetProperty(val[0], val[1])
					node.Args = append(node.Args, fmt.Sprintf("%v=%v", argName, prop))
					if err != nil {
						ctxlog.Warnf("Cannot find property %v on %v", val[1], val[0])
					}
				case "get_attribute":
					node.Args = append(node.Args, fmt.Sprintf("%v=get_attribute %v*:%v", argName, val[0], val[1]))
				default:
					node.Args = append(node.Args, fmt.Sprintf("DEBUG: %v=%v", argName, val))

				}
			}
		}
		// Sets the output
		// For every node, the attributes of the node or its type is an output
		node.Outputs = make(map[string]string, 0)
		for k, _ := range n.NodeTemplate.Refs.Type.Attributes {
			node.Outputs[k] = ""
		}
		//for k, v := range n.NodeTemplate.Attributes {
		//node.Outputs[k] = v
		//}
		g.Nodes = append(g.Nodes, node)
	}
	return g
}