/*
This is a *low level* function that access a MAAS Server and returns a MAASObject
referring to a single MAAS managed node.
The function takes a pointer to an already active MAASObject as well as a system_id and returns a MAASObject array
and an error code.
*/
func maasGetSingleNode(maas *gomaasapi.MAASObject, system_id string) (gomaasapi.MAASObject, error) {
	log.Printf("[DEBUG] [maasGetSingleNode] Getting a node (%s) from MAAS\n", system_id)
	nodeObject, err := maas.GetSubObject("nodes").GetSubObject(system_id).Get()
	if err != nil {
		log.Printf("[ERROR] [maasGetSingleNode] Unable to get node (%s) from MAAS\n", system_id)
		return gomaasapi.MAASObject{}, err
	}
	return nodeObject, nil
}
func maasReleaseNode(maas *gomaasapi.MAASObject, system_id string) error {
	log.Printf("[DEBUG] [maasReleaseNode] Releasing node: %s", system_id)

	_, err := maas.GetSubObject("nodes").GetSubObject(system_id).CallPost("release", url.Values{})
	if err != nil {
		log.Printf("[DEBUG] [maasReleaseNode] Unable to release node (%s)", system_id)
		return err
	}
	return nil
}
/*
This is a *low level* function that attempts to acquire a MAAS managed node for future deployment.
*/
func maasAllocateNodes(maas *gomaasapi.MAASObject, params url.Values) (gomaasapi.MAASObject, error) {
	log.Printf("[DEBUG] [maasAllocateNodes] Allocating one or more nodes with following params: %+v", params)

	nodeObject, err := maas.GetSubObject("nodes").CallPost("acquire", params)
	if err != nil {
		log.Printf("[ERROR] [maasAllocateNodes] Unable to acquire a node ... bailing")
		return gomaasapi.MAASObject{}, err
	}
	return nodeObject.GetMAASObject()
}
Ejemplo n.º 4
0
// startNode installs and boots a node.
func (environ *maasEnviron) startNode(node gomaasapi.MAASObject, series string, userdata []byte) error {
	userDataParam := base64.StdEncoding.EncodeToString(userdata)
	params := url.Values{
		"distro_series": {series},
		"user_data":     {userDataParam},
	}
	// Initialize err to a non-nil value as a sentinel for the following
	// loop.
	err := fmt.Errorf("(no error)")
	for a := shortAttempt.Start(); a.Next() && err != nil; {
		_, err = node.CallPost("start", params)
	}
	return err
}
/*
This is a *low level* function that access a MAAS Server and returns an array of MAASObject
The function takes a pointer to an already active MAASObject and returns a JSONObject array
and an error code.
*/
func maasListAllNodes(maas *gomaasapi.MAASObject) ([]gomaasapi.JSONObject, error) {
	nodeListing := maas.GetSubObject("nodes")
	log.Printf("[DEBUG] [maasListAllNodes] Fetching list of nodes...\n")
	listNodeObjects, err := nodeListing.CallGet("list", url.Values{})
	if err != nil {
		log.Printf("[ERROR] [maasListAllNodes] Unable to get list of nodes ...\n")
		return nil, err
	}

	listNodes, err := listNodeObjects.GetArray()
	if err != nil {
		log.Printf("[ERROR] [maasListAllNodes] Unable to get the node list array ...\n")
		return nil, err
	}
	return listNodes, err
}
Ejemplo n.º 6
0
func maasObjectId(maasObject *gomaasapi.MAASObject) instance.Id {
	// Use the node's 'resource_uri' value.
	return instance.Id(maasObject.URI().String())
}
/*
Convenience function to convert a MAASObject to a NodeInfo instance
The function takes a fully initialized MAASObject and returns a NodeInfo instance
with an error
*/
func toNodeInfo(nodeObject *gomaasapi.MAASObject) (*NodeInfo, error) {
	log.Println("[DEBUG] [toNodeInfo] Attempting to convert node information from MAASObject to NodeInfo")

	nodeMap := nodeObject.GetMap()

	system_id, err := nodeMap["system_id"].GetString()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get node (%s)\n", system_id)
		return nil, err
	}

	hostname, err := nodeMap["hostname"].GetString()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the node (%s) hostname\n", system_id)
		return nil, err
	}

	url := nodeObject.URL().String()
	if len(url) == 0 {
		return nil, errors.New("[ERROR] [toNodeInfo] Empty URL for node")
	}

	power_state, err := nodeMap["power_state"].GetString()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the power_state for node: %s\n", system_id)
		return nil, err
	}

	cpu_count_float, err := nodeMap["cpu_count"].GetFloat64()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the cpu_count for node: %s\n", system_id)
		log.Printf("[ERROR] [toNodeInfo] Error: %s\n", err)
		log.Printf("[ERROR] [toNodeInfo] cpu_count_float: %v\n", cpu_count_float)
		log.Println("[ERROR] [toNodeInfo] Defaulting cpu_count to 0")
		cpu_count_float = 0
	}
	cpu_count := uint16(cpu_count_float)

	architecture, err := nodeMap["architecture"].GetString()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the node (%s) architecture\n", system_id)
		return nil, err
	}

	distro_series, err := nodeMap["distro_series"].GetString()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the distro_series for node: %s\n", system_id)
		return nil, err
	}

	memory_float, err := nodeMap["memory"].GetFloat64()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the memory for node: %s\n", system_id)
		log.Printf("[ERROR] [toNodeInfo] Error: %s\n", err)
		log.Printf("[ERROR] [toNodeInfo] memory_float: %v\n", memory_float)
		log.Printf("[ERROR] [toNodeInfo] Defaulting memory to 0")
		memory_float = 0
	}
	memory := uint64(memory_float)

	osystem, err := nodeMap["osystem"].GetString()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the OS for node: %s\n", system_id)
		return nil, err
	}

	status_float, err := nodeMap["status"].GetFloat64()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the status for node: %s\n", system_id)
		return nil, err
	}
	status := uint16(status_float)

	substatus_float, err := nodeMap["substatus"].GetFloat64()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the node (%s) substatus\n", system_id)
		return nil, err
	}
	substatus := uint16(substatus_float)

	tag_names, err := nodeMap["tag_names"].GetArray()
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to get the tags for node: %s\n", system_id)
		return nil, err
	}

	tag_array := make([]string, 0, 1)
	for _, tag_object := range tag_names {
		tag_name, err := tag_object.GetString()
		if err != nil {
			log.Printf("[ERROR] [toNodeInfo] Unable to parse tag information (%v) for node (%s)", tag_object, system_id)
			return nil, err
		}
		tag_array = append(tag_array, tag_name)
	}

	prettyJSON, err := json.MarshalIndent(nodeObject, "", "    ")
	if err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to convert node (%s) information to JSON\n", system_id)
		return nil, err
	}

	log.Printf("[DEBUG] [toNodeInfo] Node (%s) JSON:\n%s\n", system_id, prettyJSON)

	var raw_data map[string]interface{}
	if err := json.Unmarshal(prettyJSON, &raw_data); err != nil {
		log.Printf("[ERROR] [toNodeInfo] Unable to Unmarshal JSON data for node: %s\n", system_id)
		return nil, err
	}

	return &NodeInfo{system_id: system_id,
		hostname:      hostname,
		url:           url,
		power_state:   power_state,
		cpu_count:     uint16(cpu_count),
		architecture:  architecture,
		distro_series: distro_series,
		memory:        memory,
		osystem:       osystem,
		status:        uint16(status),
		substatus:     uint16(substatus),
		tag_names:     tag_array,
		data:          raw_data}, nil
}