예제 #1
0
func handle_node_start_event(node string) error {
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)

	var storage_node models.Node

	_ = coll.Find(bson.M{"hostname": node}).One(&storage_node)
	if storage_node.State != models.NODE_STATE_INITIALIZING {
		logger.Get().Warning(fmt.Sprintf("Node with name: %s not in activating state to update other details", node))
		return nil
	}
	asyncTask := func(t *task.Task) {
		t.UpdateStatus("started the task for InitializeNode: %s", t.ID)
		// Process the request
		if err := initializeStorageNode(node, t); err != nil {
			t.UpdateStatus("Failed")
			t.Done(models.TASK_STATUS_FAILURE)
		} else {
			t.UpdateStatus("Success")
			t.Done(models.TASK_STATUS_SUCCESS)
		}
	}
	if taskId, err := skyring.GetApp().GetTaskManager().Run(fmt.Sprintf("Initialize Node: %s", node), asyncTask, nil, nil, nil); err != nil {
		logger.Get().Error("Unable to create the task for Initialize Node: %s. error: %v", node, err)
	} else {
		logger.Get().Debug("Task created for initialize node. Task id: %s", taskId.String())
	}
	return nil
}
예제 #2
0
func sync_cluster_status(cluster models.Cluster, provider *Provider) (bool, error) {
	var result models.RpcResponse
	vars := make(map[string]string)
	vars["cluster-id"] = cluster.ClusterId.String()
	err = provider.Client.Call(provider.Name+".GetClusterStatus", models.RpcRequest{RpcRequestVars: vars, RpcRequestData: []byte{}}, &result)
	if err != nil || result.Status.StatusCode != http.StatusOK {
		logger.Get().Error("Error getting status for cluster: %s. error:%v", cluster.Name, err)
		return false, err
	} else {
		statusMsg := result.Status.StatusMessage
		sessionCopy := db.GetDatastore().Copy()
		defer sessionCopy.Close()
		coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_CLUSTERS)
		var clusterStatus int
		switch statusMsg {
		case models.STATUS_OK:
			clusterStatus = models.CLUSTER_STATUS_OK
		case models.STATUS_WARN:
			clusterStatus = models.CLUSTER_STATUS_WARN
		case models.STATUS_ERR:
			clusterStatus = models.CLUSTER_STATUS_ERROR
		}
		// Set the cluster status
		logger.Get().Info("Updating the status of the cluster: %s to %d", cluster.Name, clusterStatus)
		if err := coll.Update(bson.M{"clusterid": cluster.ClusterId}, bson.M{"$set": bson.M{"status": clusterStatus}}); err != nil {
			return false, err
		}
	}
	return true, nil
}
예제 #3
0
func initializeStorageNode(node string, t *task.Task) error {
	sProfiles, err := skyring.GetDbProvider().StorageProfileInterface().StorageProfiles(nil, models.QueryOps{})
	if err != nil {
		logger.Get().Error("Unable to get the storage profiles. May not be able to apply storage profiles for node: %v err:%v", node, err)
	}
	if storage_node, ok := saltnodemanager.GetStorageNodeInstance(node, sProfiles); ok {
		if err := updateStorageNodeToDB(*storage_node); err != nil {
			logger.Get().Error("Unable to add details of node: %s to DB. error: %v", node, err)
			t.UpdateStatus("Unable to add details of node: %s to DB. error: %v", node, err)
			updateNodeState(node, models.NODE_STATE_FAILED)
			return err
		}
		if nodeErrorMap, configureError := skyring.GetCoreNodeManager().SetUpMonitoring(node, curr_hostname); configureError != nil && len(nodeErrorMap) != 0 {
			if len(nodeErrorMap) != 0 {
				logger.Get().Error("Unable to setup collectd on %s because of %v", node, nodeErrorMap)
				t.UpdateStatus("Unable to setup collectd on %s because of %v", node, nodeErrorMap)
				updateNodeState(node, models.NODE_STATE_FAILED)
				return err
			} else {
				logger.Get().Error("Config Error during monitoring setup for node:%s Error:%v", node, configureError)
				t.UpdateStatus("Config Error during monitoring setup for node:%s Error:%v", node, configureError)
				updateNodeState(node, models.NODE_STATE_FAILED)
				return err
			}
		}
		return nil
	} else {
		logger.Get().Critical("Error getting the details for node: %s", node)
		t.UpdateStatus("Error getting the details for node: %s", node)
		updateNodeState(node, models.NODE_STATE_FAILED)
		return fmt.Errorf("Error getting the details for node: %s", node)
	}
}
예제 #4
0
func (a *App) SyncClusterDetails() {
	// Get the list of cluster
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_CLUSTERS)
	var clusters models.Clusters
	if err := coll.Find(nil).All(&clusters); err != nil {
		logger.Get().Error("Error getting the clusters list. Unable to sync details. error: %v", err)
		return
	}
	for _, cluster := range clusters {
		provider := a.getProviderFromClusterId(cluster.ClusterId)
		if provider == nil {
			logger.Get().Error("Error getting provider for the cluster: %s", cluster.Name)
			continue
		}

		// Sync the cluster status
		if ok, err := sync_cluster_status(cluster, provider); err != nil || !ok {
			logger.Get().Error("Error updating status for cluster: %s", cluster.Name)
		}
		// TODO:: Sync the nodes status
		if ok, err := sync_cluster_nodes(cluster, provider); err != nil && !ok {
			logger.Get().Error("Error syncing node details for cluster: %s. error: %v", cluster.Name, err)
		}
		// TODO:: Sync the storage entities of the cluster
		if ok, err := sync_cluster_storage_entities(cluster, provider); err != nil && !ok {
			logger.Get().Error("Error syncing storage entities for cluster: %s. error: %v", cluster.Name, err)
		}
	}
}
예제 #5
0
/*
Initialize the application
*/
func (a *App) InitializeApplication(sysConfig conf.SkyringCollection) error {

	if err := initializeNodeManager(sysConfig.NodeManagementConfig); err != nil {
		logger.Get().Error("Unable to create node manager")
		return err
	}
	/*
		TODO : This will be removed after porting all the existing things into newer scheme
	*/
	// Create DB session
	if err := db.InitDBSession(sysConfig.DBConfig); err != nil {
		logger.Get().Error("Unable to initialize DB")
		return err
	}
	if err := db.InitMonitoringDB(sysConfig.TimeSeriesDBConfig); err != nil {
		logger.Get().Error("Unable to initialize monitoring DB")
		return err
	}

	//Initialize the DB provider
	if err := initializeDb(sysConfig.DBConfig); err != nil {
		logger.Get().Error("Unable to initialize the authentication provider: %s", err)
		return err
	}

	//Initialize the auth provider
	if err := initializeAuth(sysConfig.Authentication); err != nil {
		logger.Get().Error("Unable to initialize the authentication provider: %s", err)
		return err
	}

	//Initialize the task manager
	if err := initializeTaskManager(); err != nil {
		logger.Get().Error("Unable to initialize the task manager: %s", err)
		return err
	}

	//Initialize the lock manager
	if err := initializeLockManager(); err != nil {
		logger.Get().Error("Unable to initialize the lock manager: %s", err)
		return err
	}

	//Initialize the Defaults
	if err := initializeDefaults(); err != nil {
		logger.Get().Error("Unable to initialize the Defaults: %s", err)
		return err
	}

	if err := application.initializeMonitoringManager(sysConfig.TimeSeriesDBConfig); err != nil {
		logger.Get().Error("Unable to create monitoring manager")
		os.Exit(1)
	}

	logger.Get().Info("Starting clusters syncing")
	go a.SyncClusterDetails()
	go InitSchedules()

	return nil
}
예제 #6
0
func (a *App) GET_Node(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	node_id_str := vars["node-id"]
	node_id, err := uuid.Parse(node_id_str)
	if err != nil {
		logger.Get().Error("Error parsing node id: %s", node_id_str)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing node id: %s", node_id_str))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	var node models.Node
	if err := collection.Find(bson.M{"nodeid": *node_id}).One(&node); err != nil {
		logger.Get().Error("Error getting the node detail for %v. error: %v", *node_id, err)
	}

	if node.Hostname == "" {
		util.HttpResponse(w, http.StatusBadRequest, "Node not found")
		logger.Get().Error("Node: %v not found. error: %v", *node_id, err)
		return
	} else {
		json.NewEncoder(w).Encode(node)
	}
}
예제 #7
0
func (a *App) getUser(rw http.ResponseWriter, req *http.Request) {

	vars := mux.Vars(req)

	user, err := GetAuthProvider().GetUser(vars["username"], req)
	if err != nil {
		logger.Get().Error("Unable to Get the user:%s", err)
		util.HandleHttpError(rw, err)
		return
	}

	//hide the hash field
	bytes, err := json.Marshal(struct {
		*models.User
		Hash bool `json:"hash,omitempty"`
	}{
		User: &user,
	})
	if err != nil {
		logger.Get().Error("Unable to marshal the User:%s", err)
		util.HandleHttpError(rw, err)
		return
	}
	rw.Write(bytes)

}
예제 #8
0
func AddStorageNodeToDB(hostname string, node_state int, node_status string) error {
	// Add the node details to the DB
	var storage_node models.Node
	storage_node.Hostname = hostname
	storage_node.State = node_state
	storage_node.Status = node_status

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)

	var node models.Node
	err = coll.Find(bson.M{"hostname": storage_node.Hostname}).One(&node)
	if err != mgo.ErrNotFound {
		logger.Get().Critical(fmt.Sprintf("Node with name: %v already exists", storage_node.Hostname))
		return errors.New(fmt.Sprintf("Node with name: %v already exists", storage_node.Hostname))
	}

	// Persist the node details
	if err := coll.Insert(storage_node); err != nil {
		logger.Get().Critical("Error adding the node: %s. error: %v", storage_node.Hostname, err)
		return err
	}
	return nil
}
예제 #9
0
func NewLocalAuthProvider(config io.Reader) (*Authorizer, error) {
	if config == nil {
		logger.Get().Error("missing configuration file for Local Auth provider")
		return nil, fmt.Errorf("missing configuration file for Local Auth provider")
	}

	providerCfg := LocalProviderCfg{}

	bytes, err := ioutil.ReadAll(config)
	if err != nil {
		logger.Get().Error("Error reading configuration file. error: %v", err)
		return nil, err
	}
	if err = json.Unmarshal(bytes, &providerCfg); err != nil {
		logger.Get().Error("Unable to Unmarshall the data. error: %v", err)
		return nil, err
	}
	//Create DB Backend
	userDao := skyring.GetDbProvider().UserInterface()

	//Create the Provider
	if provider, err := NewAuthorizer(userDao, providerCfg.DefaultRole, providerCfg.Roles); err != nil {
		logger.Get().Error("Unable to initialize the authorizer for localauthprovider. error: %v", err)
		panic(err)
	} else {
		return &provider, nil
	}
}
예제 #10
0
func (a *App) GET_Storages(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	cluster_id_str := vars["cluster-id"]
	cluster_id, err := uuid.Parse(cluster_id_str)
	if err != nil {
		logger.Get().Error("Error parsing the cluster id: %s. error: %v", cluster_id_str, err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing the cluster id: %s", cluster_id_str))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE)
	var storages models.Storages
	if err := collection.Find(bson.M{"clusterid": *cluster_id}).All(&storages); err != nil {
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		logger.Get().Error("Error getting the storage list for cluster: %v. error: %v", *cluster_id, err)
		return
	}
	if len(storages) == 0 {
		json.NewEncoder(w).Encode(models.Storages{})
	} else {
		json.NewEncoder(w).Encode(storages)
	}
}
예제 #11
0
func (a *App) GET_Storage(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	cluster_id_str := vars["cluster-id"]
	cluster_id, err := uuid.Parse(cluster_id_str)
	if err != nil {
		logger.Get().Error("Error parsing the cluster id: %s. error: %v", cluster_id_str, err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing the cluster id: %s", cluster_id_str))
		return
	}
	storage_id_str := vars["storage-id"]
	storage_id, err := uuid.Parse(storage_id_str)
	if err != nil {
		logger.Get().Error("Error parsing the storage id: %s. error: %v", storage_id_str, err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing the storage id: %s", storage_id_str))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE)
	var storage models.Storage
	if err := collection.Find(bson.M{"clusterid": *cluster_id, "storageid": *storage_id}).One(&storage); err != nil {
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		logger.Get().Error("Error getting the storage: %v on cluster: %v. error: %v", *storage_id, *cluster_id, err)
		return
	}
	if storage.Name == "" {
		util.HttpResponse(w, http.StatusBadRequest, "Storage not found")
		logger.Get().Error("Storage with id: %v not found for cluster: %v. error: %v", *storage_id, *cluster_id, err)
		return
	} else {
		json.NewEncoder(w).Encode(storage)
	}
}
예제 #12
0
// InitPlugin creates an instance of the named plugin.
func InitDbProvider(name string, configFilePath string) (DbInterface, error) {
	var dbprovider DbInterface

	if name == "" {
		logger.Get().Info("No providers specified.")
		return nil, nil
	}

	var err error

	if configFilePath != "" {
		config, err := os.Open(configFilePath)
		if err != nil {
			logger.Get().Critical("Couldn't open Db provider configuration %s: %v",
				configFilePath, err)
		}

		defer config.Close()
		dbprovider, err = GetDbProvider(name, config)
	} else {
		// Pass explicit nil so providers can actually check for nil. See
		// "Why is my nil error value not equal to nil?" in golang.org/doc/faq.
		dbprovider, err = GetDbProvider(name, nil)
	}

	if err != nil {
		return nil, fmt.Errorf("could not initialize Db Provider %q: %v", name, err)
	}
	if dbprovider == nil {
		return nil, fmt.Errorf("unknown Db Provider %q", name)
	}

	return dbprovider, nil
}
예제 #13
0
func (a *App) POST_StorageProfiles(w http.ResponseWriter, r *http.Request) {

	var request models.StorageProfile
	// Unmarshal the request body
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, models.REQUEST_SIZE_LIMIT))
	if err != nil {
		logger.Get().Error("Error parsing the request: %v", err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Unable to parse the request: %v", err))
		return
	}
	if err := json.Unmarshal(body, &request); err != nil {
		logger.Get().Error("Error Unmarshalling the request: %v", err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Unable to unmarshal request: %v", err))
		return
	}
	if (request == models.StorageProfile{}) {
		logger.Get().Error("Invalid request")
		util.HttpResponse(w, http.StatusBadRequest, "Invalid request")
		return
	}
	// Check if storage profile already added
	if _, err := GetDbProvider().StorageProfileInterface().StorageProfile(request.Name); err == nil {
		logger.Get().Error("Storage profile already added: %v", err)
		util.HttpResponse(w, http.StatusMethodNotAllowed, "Storage profile already added")
		return

	}

	if err := GetDbProvider().StorageProfileInterface().SaveStorageProfile(request); err != nil {
		logger.Get().Error("Storage profile add failed: %v", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}
	return
}
예제 #14
0
func (a SaltNodeManager) SyncStorageDisks(node string, sProfiles []models.StorageProfile) (bool, error) {
	disks, err := salt_backend.GetNodeDisk(node)
	if err != nil {
		return false, err
	}
	for _, disk := range disks {
		dId, err := uuid.New()
		if err != nil {
			logger.Get().Error(fmt.Sprintf("Unable to generate uuid for disk : %s. error: %v", disk.DevName, err))
			return false, err
		}
		disk.DiskId = *dId
		applyStorageProfile(&disk, sProfiles)
	}
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	if len(disks) != 0 {
		if err := coll.Update(bson.M{"hostname": node}, bson.M{"$set": bson.M{"storagedisks": disks}}); err != nil {
			logger.Get().Error("Error updating the disk details for node: %s. error: %v", node, err)
			return false, err
		}
	}
	return true, nil
}
예제 #15
0
func (a *App) GET_Nodes(w http.ResponseWriter, r *http.Request) {
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	params := r.URL.Query()
	admin_state_str := params.Get("state")

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	var nodes models.Nodes
	if admin_state_str == "" {
		if err := collection.Find(nil).All(&nodes); err != nil {
			util.HttpResponse(w, http.StatusInternalServerError, err.Error())
			logger.Get().Error("Error getting the nodes list. error: %v", err)
			return
		}
	} else {
		nodes, err = getNodesWithState(w, admin_state_str)
		if err != nil {
			util.HttpResponse(w, http.StatusInternalServerError, err.Error())
			logger.Get().Error("Error getting the nodes list. error: %v", err)
			return
		}
	}
	if len(nodes) == 0 {
		json.NewEncoder(w).Encode([]models.Node{})
	} else {
		json.NewEncoder(w).Encode(nodes)
	}
}
예제 #16
0
func NewLdapAuthProvider(config io.Reader) (*Authorizer, error) {
	if config == nil {
		errStr := "missing configuration file for Ldap Auth provider"
		logger.Get().Error(errStr)
		return nil, fmt.Errorf(errStr)
	}

	providerCfg := LdapProviderCfg{}

	bytes, err := ioutil.ReadAll(config)
	if err != nil {
		logger.Get().Error("Error reading Configuration file. error: %v", err)
		return nil, err
	}
	if err = json.Unmarshal(bytes, &providerCfg); err != nil {
		logger.Get().Error("Unable to Unmarshall the data. error: %v", err)
		return nil, err
	}
	userDao := skyring.GetDbProvider().UserInterface()
	//Create the Provider
	if provider, err := NewAuthorizer(userDao, providerCfg); err != nil {
		logger.Get().Error("Unable to initialize the authorizer for Ldapauthprovider. error: %v", err)
		panic(err)
	} else {
		return &provider, nil
	}

}
예제 #17
0
func (a *App) DELETE_Nodes(w http.ResponseWriter, r *http.Request) {
	var nodeIds []struct {
		NodeId string `json:"nodeid"`
	}

	// Unmarshal the request body
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, models.REQUEST_SIZE_LIMIT))
	if err != nil {
		logger.Get().Error("Error parsing the request. error: %v", err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Unable to parse the request. error: %v", err))
		return
	}
	if err := json.Unmarshal(body, &nodeIds); err != nil {
		logger.Get().Error("Unable to unmarshal request. error: %v", err)
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Unable to unmarshal request. error: %v", err))
		return
	}

	for _, item := range nodeIds {
		node_id, _ := uuid.Parse(item.NodeId)
		if ok, err := removeNode(w, *node_id); err != nil || !ok {
			logger.Get().Error("Error removing the node: %v. error: %v", *node_id, err)
			util.HttpResponse(w, http.StatusInternalServerError, fmt.Sprintf("Error removing the node: %v. error: %v", *node_id, err))
			return
		}
	}
}
예제 #18
0
func (a *App) getTask(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	taskId, err := uuid.Parse(vars["taskid"])
	if err != nil {
		logger.Get().Error("Unable to Parse the Id: %s. error: %v", vars["taskId"], err)
		util.HandleHttpError(rw, err)
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_TASKS)
	var task models.AppTask
	if err := coll.Find(bson.M{"id": *taskId}).One(&task); err != nil {
		logger.Get().Error("Unable to get task. error: %v", err)
		if err == mgo.ErrNotFound {
			util.HttpResponse(rw, http.StatusNotFound, err.Error())
			return
		} else {
			util.HttpResponse(rw, http.StatusInternalServerError, err.Error())
			return
		}
	}
	json.NewEncoder(rw).Encode(task)
}
예제 #19
0
func Authenticate(a Authorizer, url string, user string, passwd string) error {
	ldap, err := openldap.Initialize(url)

	if err != nil {
		logger.Get().Error("Failed to connect the server. error: %v", err)
		return err
	}

	ldap.SetOption(openldap.LDAP_OPT_PROTOCOL_VERSION, openldap.LDAP_VERSION3)
	if a.directory.Uid != "" {
		err = ldap.Bind(fmt.Sprintf("%s=%s,%s", a.directory.Uid, user, a.directory.Base), passwd)

		if err != nil {
			logger.Get().Error("Error binding to LDAP Server:%s. error: %v", url, err)
			return err
		}
	} else {
		if ldap.Bind(fmt.Sprintf("uid=%s,%s", user, a.directory.Base), passwd) != nil {
			err = ldap.Bind(fmt.Sprintf("cn=%s,%s", user, a.directory.Base), passwd)
			if err != nil {
				logger.Get().Error("Error binding to LDAP Server:%s. error: %v", url, err)
				return err
			}
		}
	}
	return nil
}
예제 #20
0
// CurrentUser returns the currently logged in user and a boolean validating
// the information.
func (a Authorizer) GetUser(u string, req *http.Request) (user models.User, e error) {
	// This should search and fetch the user name based on the given value
	// and can also check whether the available users are already imported
	// into the database or not.

	//if username is me, get the currently loggedin user
	if u == authprovider.CurrentUser {
		session, err := skyring.Store.Get(req, "session-key")
		if err != nil {
			logger.Get().Error("Error getting the session for user: %s. error: %v", u, err)
			return user, err
		}
		if val, ok := session.Values["username"]; ok {
			u = val.(string)
		} else {
			logger.Get().Error("Unable to identify the user: %s from session", u)
			return user, mkerror("Unable to identify the user from session")
		}
	}
	user, e = a.userDao.User(u)
	if e != nil {
		logger.Get().Error("Error retrieving the user: %s. error: %v", user, e)
		return user, e
	}
	return user, nil
}
예제 #21
0
func (a *App) PATCH_Disk(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	node_id_str := vars["node-id"]
	node_id, err := uuid.Parse(node_id_str)
	if err != nil {
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing node id: %s. Error: %v", node_id_str, err))
		return
	}

	disk_id_str := vars["disk-id"]
	disk_id, err := uuid.Parse(disk_id_str)
	if err != nil {
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing node id: %s. Error: %v", disk_id_str, err))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	var node models.Node
	if err := collection.Find(bson.M{"nodeid": *node_id}).One(&node); err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting the node detail for node: %s. error: %v", node_id_str, err))
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logger.Get().Error("Error parsing http request body:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}
	var m map[string]interface{}

	if err = json.Unmarshal(body, &m); err != nil {
		logger.Get().Error("Unable to Unmarshall the data:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}

	var disks []backend.Disk
	if val, ok := m["storageprofile"]; ok {
		//update the field
		for _, disk := range node.StorageDisks {
			if disk.DiskId == *disk_id {
				disk.StorageProfile = val.(string)
			}
			disks = append(disks, disk)
		}
		node.StorageDisks = disks
	}
	//Save
	err = collection.Update(bson.M{"nodeid": *node_id}, bson.M{"$set": node})
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error updating record in DB for node: %s. error: %v", node_id_str, err))
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
	}
}
예제 #22
0
func LoadAppConfiguration(configFilePath string) {
	file, err := ioutil.ReadFile(configFilePath)
	if err != nil {
		logger.Get().Critical("Error Reading SkyRing Config. error: %v", err)
	}
	err = json.Unmarshal(file, &SystemConfig)
	if err != nil {
		logger.Get().Critical("Error Unmarshalling SkyRing Config. error: %v", err)
	}
}
예제 #23
0
func (a *App) SetRoutes(router *mux.Router) error {
	// Load App specific routes
	a.LoadRoutes()

	// Create a router for defining the routes which require authenticationAuth_fix
	//For routes require auth, will be checked by a middleware which
	//return error immediately

	authReqdRouter := mux.NewRouter().StrictSlash(true)

	//create a negroni with LoginReqd middleware
	n := negroni.New(negroni.HandlerFunc(a.LoginRequired), negroni.Wrap(authReqdRouter))

	// Set routes for core which doesnot require authentication
	for _, route := range CORE_ROUTES_NOAUTH {
		if validApiVersion(route.Version) {
			urlPattern := fmt.Sprintf("%s/v%d/%s", DEFAULT_API_PREFIX, route.Version, route.Pattern)
			router.Methods(route.Method).Path(urlPattern).Name(route.Name).Handler(http.HandlerFunc(route.HandlerFunc))
		} else {
			logger.Get().Info("Skipped the route: %s as version: %d is un spported", route.Name, route.Version)
		}
	}

	// Set routes for core which require authentication
	for _, route := range CORE_ROUTES {
		if validApiVersion(route.Version) {
			urlPattern := fmt.Sprintf("%s/v%d/%s", DEFAULT_API_PREFIX, route.Version, route.Pattern)
			authReqdRouter.Methods(route.Method).Path(urlPattern).Name(route.Name).Handler(http.HandlerFunc(route.HandlerFunc))
			router.Handle(urlPattern, n)
		} else {
			logger.Get().Info("Skipped the route: %s as version: %d is un spported", route.Name, route.Version)
		}
	}

	//Set the provider specific routes here
	//All the provider specific routes are assumed to be authenticated
	for _, route := range a.routes {
		logger.Get().Debug("%s", route)
		if validApiVersion(route.Version) {
			urlPattern := fmt.Sprintf("%s/v%d/%s", DEFAULT_API_PREFIX, route.Version, route.Pattern)
			authReqdRouter.
				Methods(route.Method).
				Path(urlPattern).
				Name(route.Name).
				Handler(http.HandlerFunc(a.ProviderHandler))
			router.Handle(urlPattern, n)
		} else {
			logger.Get().Info("Skipped the route: %s as version: %d is un spported", route.Name, route.Version)
		}
	}

	return nil
}
예제 #24
0
// Update changes data for an existing user. Needs thought...
//Just added for completeness. Will revisit later
func (a Authorizer) UpdateUser(username string, m map[string]interface{}) error {
	var (
		hash    []byte
		updated bool
	)

	user, err := a.userDao.User(username)
	if err != nil {
		logger.Get().Error("Error retrieving the user: %s. error: %v", username, err)
		return err
	}

	if user.Type == authprovider.Internal {
		if val, ok := m["oldpassword"]; ok {
			op := val.(string)
			match := bcrypt.CompareHashAndPassword(user.Hash, []byte(op))
			if match != nil {
				logger.Get().Error("Old password doesnt match")
				return mkerror("Old password doesnt match" + err.Error())
			} else {
				if val, ok := m["password"]; ok {
					p := val.(string)
					hash, err = bcrypt.GenerateFromPassword([]byte(p), bcrypt.DefaultCost)
					if err != nil {
						logger.Get().Error("Error saving the password for user: %s. error: %v", username, err)
						return mkerror("couldn't save password: "******"email"]; ok {
		e := val.(string)
		user.Email = e
		updated = true
	}

	if val, ok := m["notificationenabled"]; ok {
		n := val.(bool)
		user.NotificationEnabled = n
	}

	if updated {
		err = a.userDao.SaveUser(user)
		if err != nil {
			logger.Get().Error("Error saving details for the user: %s to DB. error: %v", username, err)
			return err
		}
	}
	return nil
}
예제 #25
0
// Start event broadcaster
func StartBroadcaster(websocketPort string) {
	InitBroadcaster()
	broadcaster := GetBroadcaster()
	go broadcaster.Run()
	http.HandleFunc("/ws", ServeWs)
	go func() {
		logger.Get().Info("Websocket - start listening on %s : %v", conf.SystemConfig.Config.Host, websocketPort)
		if err := http.ListenAndServe(conf.SystemConfig.Config.Host+":"+websocketPort, nil); err != nil {
			logger.Get().Fatalf("Unable to start the websocket server.err: %v", err)
		}
	}()
}
예제 #26
0
func LdapAuth(a Authorizer, user, passwd string) bool {
	url := GetUrl(a.directory.Address, a.directory.Port)
	// Authenticating user
	err := Authenticate(a, url, user, passwd)
	if err != nil {
		logger.Get().Error("Authentication failed for user: %s. error: %v", user, err)
		return false
	} else {
		logger.Get().Info("Ldap user login success for user: %s", user)
		return true
	}
}
예제 #27
0
//Set up the indexes for the Db
//Can be called during the initialization
func (m MongoDb) InitDb() error {

	if err := m.InitUser(); err != nil {
		logger.Get().Error("Error Initilaizing User Table", err)
		return err
	}
	if err := m.InitStorageProfile(); err != nil {
		logger.Get().Error("Error Initilaizing storageprofile Table", err)
		return err
	}
	return nil
}
예제 #28
0
func (a *App) PATCH_StorageProfile(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	sprofile, err := GetDbProvider().StorageProfileInterface().StorageProfile(vars["name"])
	if err != nil {
		logger.Get().Error("Unable to Get Storage Profile:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}
	if sprofile.Default {
		logger.Get().Error("Default Storage Profile cannot be modified:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, "Default Storage Profile Cannot be Modified")
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logger.Get().Error("Error parsing http request body:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}
	var m map[string]interface{}

	if err = json.Unmarshal(body, &m); err != nil {
		logger.Get().Error("Unable to Unmarshall the data:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}
	var updated bool
	if val, ok := m["priority"]; ok {
		sprofile.Priority = int(val.(float64))
		updated = true
	}
	if val, ok := m["rule"]; ok {
		if ruleM, ok := val.(map[string]interface{}); ok {
			if ruleV, ok := ruleM["disktype"]; ok {
				sprofile.Rule.Type = models.DiskType(ruleV.(float64))
				updated = true
			}
			if ruleV, ok := ruleM["speed"]; ok {
				sprofile.Rule.Speed = int(ruleV.(float64))
				updated = true
			}
		}
	}
	if updated {
		if err := GetDbProvider().StorageProfileInterface().SaveStorageProfile(sprofile); err != nil {
			logger.Get().Error("Storage profile update failed: %v", err)
			util.HttpResponse(w, http.StatusInternalServerError, err.Error())
			return
		}
	}
}
예제 #29
0
// Logout clears an authentication session and add a logged out message.
func (a Authorizer) Logout(rw http.ResponseWriter, req *http.Request) error {
	session, err := skyring.Store.Get(req, "session-key")
	if err != nil {
		logger.Get().Error("Error getting the session. error: %v", err)
		return err
	}
	session.Options.MaxAge = -1
	if err = session.Save(req, rw); err != nil {
		logger.Get().Error("Error saving the session. error: %v", err)
		return err
	}
	return nil
}
예제 #30
0
func DeleteClusterSchedule(clusterId uuid.UUID) {
	mutex.Lock()
	defer mutex.Unlock()
	schedulerId, ok := ClusterMonitoringSchedules[clusterId]
	if !ok {
		logger.Get().Error("Cluster with id %v not scheduled", clusterId)
		return
	}
	if err := schedule.DeleteScheduler(schedulerId); err != nil {
		logger.Get().Error("Failed to delete schedule for cluster %v.Error %v", clusterId, err)
	}
	delete(ClusterMonitoringSchedules, clusterId)
}