예제 #1
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) addSegment(tenantId uint64, segment *Segment) error {
	var err error

	// TODO better way of getting sequence
	var segments []Segment
	mysqlStore.db.Where("tenant_id = ?", tenantId).Find(&segments)
	segment.Seq = uint64(len(segments) + 1)
	mysqlStore.db.NewRecord(*segment)
	segment.TenantId = tenantId
	mysqlStore.db.Create(segment)
	//	myId := segment.Id

	//	var segmentSeq uint64
	//
	//	for i := range segments {
	//		if segments[i].Id == myId {
	//			segmentSeq = uint64(i)
	//			segments[i].Seq = segmentSeq
	//			log.Printf("Sequence for segment %s is %d\n", segments[i].Name, segmentSeq)
	//			mysqlStore.db.Save(segment)
	//			break
	//		}
	//	}

	err = common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return err
	}
	return nil
}
예제 #2
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) addTenant(tenant *Tenant) error {
	var tenants []Tenant
	mysqlStore.db.Find(&tenants)

	//	myId := tenant.Id
	tenant.Seq = uint64(len(tenants) + 1)
	mysqlStore.db.Create(tenant)
	mysqlStore.db.NewRecord(*tenant)

	// TODO better way of getting sequence
	//
	//	var tenantSeq uint64
	//	for i := range tenants {
	//		if tenants[i].Id == myId {
	//			tenantSeq = uint64(i)
	//			tenant.Seq = tenantSeq
	//			mysqlStore.db.Save(tenant)
	//			log.Printf("Sequence for tenant %s is %d\n", tenants[i].Name, tenantSeq)
	//			break
	//		}
	//	}

	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return err
	}
	return nil
}
예제 #3
0
파일: store.go 프로젝트: romana/core
// addIPtablesRuleUnsafe is a non thread safe implementation of addIPtablesRule.
// Unsafe implementation is needed for functions which are already managing same mutex.
func (firewallStore *firewallStore) addIPtablesRuleUnsafe(rule *IPtablesRule) error {

	db := firewallStore.DbStore.Db
	// db := firewallStore.GetDb()
	log.Info("In addIPtablesRule() after GetDb")
	if db == nil {
		panic("In addIPtablesRule(), db is nil")
	}

	firewallStore.DbStore.Db.Create(rule)
	log.Info("In addIPtablesRule() after Db.Create")
	if db.Error != nil {
		return db.Error
	}
	firewallStore.DbStore.Db.NewRecord(*rule)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}
	return nil

}
예제 #4
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) createSchema(force bool) error {
	log.Println("in createSchema(", force, ")")
	// Connect to mysql database
	schemaName := mysqlStore.info.Database
	mysqlStore.info.Database = "mysql"
	mysqlStore.setConnString()
	err := mysqlStore.connect()

	if err != nil {
		return err
	}
	var sql string
	if force {
		sql = fmt.Sprintf("DROP DATABASE IF EXISTS %s", schemaName)
		res, err := mysqlStore.db.DB().Exec(sql)
		if err != nil {
			return err
		}

		rows, _ := res.RowsAffected()
		log.Println(sql, ": ", rows)
	}

	sql = fmt.Sprintf("CREATE DATABASE %s", schemaName)
	res, err := mysqlStore.db.DB().Exec(sql)
	if err != nil {
		return err
	}
	rows, _ := res.RowsAffected()
	log.Println(sql, ": ", rows)
	mysqlStore.info.Database = schemaName
	mysqlStore.setConnString()
	err = mysqlStore.connect()
	if err != nil {
		return err
	}
	log.Println("Creating vms table.")
	ipamVm := IpamVm{}
	mysqlStore.db.CreateTable(&ipamVm)
	// Sequence numbers are unique for VMs per host / tenant combination
	mysqlStore.db.Model(&IpamVm{}).AddUniqueIndex("idx_segment_host_seq", "segment_id", "host_id", "seq")

	//	log.Println("Creating hosts table.")
	//	mysqlStore.db.CreateTable(&IpamHost{})
	//
	//	log.Println("Creating segments table.")
	//	mysqlStore.db.CreateTable(&IpamHost{})

	errs := mysqlStore.db.GetErrors()
	log.Println("Errors", errs)
	err2 := common.MakeMultiError(errs)

	if err2 != nil {
		return err2
	}
	return nil

}
예제 #5
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) findHost(id uint64) (Host, error) {
	host := Host{}
	mysqlStore.db.Where("id = ?", id).First(&host)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return host, err
	}
	return host, nil
}
예제 #6
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) addHost(host *Host) (string, error) {
	mysqlStore.db.NewRecord(*host)
	mysqlStore.db.Create(host)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return "", err
	}
	return strconv.FormatUint(host.Id, 10), nil
}
예제 #7
0
파일: store.go 프로젝트: romana/core
// CreateSchemaPostProcess implements CreateSchemaPostProcess method of
// Service interface.
func (rootStore *rootStore) CreateSchemaPostProcess() error {
	passwd, err := rootStore.GetPasswordFunction()
	if err != nil {
		return err
	}
	sql := fmt.Sprintf("INSERT INTO users (username, password) VALUES (?, %s)", passwd)
	rootStore.DbStore.Db.Exec(sql, "admin", "password")
	rootStore.DbStore.Db.Exec("INSERT INTO roles (name) VALUES (admin)")
	//	rootStore.DbStore.Db.Exec("INSERT INTO roles (name) VALUES (admin)")
	return common.MakeMultiError(rootStore.DbStore.Db.GetErrors())
}
예제 #8
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) listSegments(tenantId uint64) ([]Segment, error) {
	var segments []Segment
	log.Println("In listTenants()", &segments)
	mysqlStore.db.Where("tenant_id = ?", tenantId).Find(&segments)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return nil, err
	}
	log.Println(segments)
	return segments, nil
}
예제 #9
0
파일: store.go 프로젝트: romana/core
// CreateSchemaPostProcess implements CreateSchemaPostProcess method of
// Service interface.
func (ipamStore *ipamStore) CreateSchemaPostProcess() error {
	db := ipamStore.Db
	log.Printf("ipamStore.CreateSchemaPostProcess(), DB is %v", db)
	db.Model(&Endpoint{}).AddUniqueIndex("idx_tenant_segment_host_network_id", "tenant_id", "segment_id", "host_id", "network_id")
	db.Model(&Endpoint{}).AddUniqueIndex("idx_ip", "ip")
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	return nil
}
예제 #10
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) listHosts() ([]Host, error) {
	var hosts []Host
	log.Println("In listHosts()")
	mysqlStore.db.Find(&hosts)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return nil, err
	}
	log.Println("MySQL found hosts:", hosts)
	return hosts, nil
}
예제 #11
0
파일: store.go 프로젝트: romana/core
func (tenantStore *tenantStore) listTenants() ([]Tenant, error) {
	var tenants []Tenant
	log.Println("In listTenants()", &tenants)
	tenantStore.DbStore.Db.Find(&tenants)
	err := common.MakeMultiError(tenantStore.DbStore.Db.GetErrors())
	if err != nil {
		return nil, err
	}
	log.Println(tenants)
	return tenants, nil
}
예제 #12
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) createSchema(force bool) error {
	log.Println("in createSchema(", force, ")")
	// Connect to mysql database
	schemaName := mysqlStore.info.Database
	mysqlStore.info.Database = "mysql"
	mysqlStore.setConnString()

	err := mysqlStore.connect()

	if err != nil {
		return err
	}
	var sql string

	if force {
		sql = fmt.Sprintf("DROP DATABASE IF EXISTS %s", schemaName)
		res, err := mysqlStore.db.DB().Exec(sql)
		if err != nil {
			return err
		}

		rows, _ := res.RowsAffected()
		log.Println(sql, ": ", rows)
	}

	sql = fmt.Sprintf("CREATE DATABASE %s", schemaName)
	res, err := mysqlStore.db.DB().Exec(sql)
	if err != nil {
		return err
	}
	rows, _ := res.RowsAffected()
	log.Println(sql, ": ", rows)
	mysqlStore.info.Database = schemaName
	mysqlStore.setConnString()
	err = mysqlStore.connect()
	if err != nil {
		return err
	}
	mysqlStore.db.CreateTable(&common.Datacenter{})
	mysqlStore.db.CreateTable(&Tor{})
	mysqlStore.db.CreateTable(&Host{})
	errs := mysqlStore.db.GetErrors()
	log.Println("Errors", errs)
	err2 := common.MakeMultiError(errs)

	if err2 != nil {
		return err2
	}
	return nil

}
예제 #13
0
파일: store.go 프로젝트: romana/core
// listSegments returns a list of segments for a specific tenant
// whose tenantId is specified.
func (tenantStore *tenantStore) listSegments(tenantId string) ([]Segment, error) {
	var segments []Segment
	db := tenantStore.DbStore.Db.Joins("JOIN tenants ON segments.tenant_id = tenants.id").
		Where("tenants.id = ? OR tenants.external_id = ?", tenantId, tenantId).
		Find(&segments)
	err := common.MakeMultiError(db.GetErrors())
	log.Printf("In listSegments(): %v, %v", segments, err)
	if err != nil {
		return nil, err
	}
	if db.Error != nil {
		return nil, db.Error
	}
	return segments, nil
}
예제 #14
0
파일: store.go 프로젝트: romana/core
// listIPtablesRules returns a list of all firewall rules in a database.
func (firewallStore *firewallStore) listIPtablesRules() ([]IPtablesRule, error) {
	log.Info("Acquiring store mutex for listIPtablesRules")
	firewallStore.mu.Lock()
	defer func() {
		log.Info("Releasing store mutex for listIPtablesRules")
		firewallStore.mu.Unlock()
	}()
	log.Info("Acquired store mutex for listIPtablesRules")

	var iPtablesRule []IPtablesRule
	firewallStore.DbStore.Db.Find(&iPtablesRule)
	err := common.MakeMultiError(firewallStore.DbStore.Db.GetErrors())
	if err != nil {
		return nil, err
	}
	return iPtablesRule, nil
}
예제 #15
0
파일: store.go 프로젝트: romana/core
func (agentStore *agentStore) listRoutes() ([]Route, error) {
	log.Trace(trace.Inside, "Acquiring store mutex for listRoutes")
	agentStore.mu.Lock()
	defer func() {
		log.Trace(trace.Inside, "Releasing store mutex for listRoutes")
		agentStore.mu.Unlock()
	}()
	log.Trace(trace.Inside, "Acquired store mutex for listRoutes")

	var routes []Route
	agentStore.DbStore.Db.Find(&routes)
	err := common.MakeMultiError(agentStore.DbStore.Db.GetErrors())
	if err != nil {
		return nil, err
	}
	return routes, nil
}
예제 #16
0
파일: store.go 프로젝트: romana/core
// deleteIPtablesRule deletes firewall rules from database.
func (firewallStore *firewallStore) deleteIPtablesRule(rule *IPtablesRule) error {
	log.Info("Acquiring store mutex for deleteIPtablesRule")
	firewallStore.mu.Lock()
	defer func() {
		log.Info("Releasing store mutex for deleteIPtablesRule")
		firewallStore.mu.Unlock()
	}()
	log.Info("Acquired store mutex for deleteIPtablesRule")

	db := firewallStore.DbStore.Db
	firewallStore.DbStore.Db.Delete(rule)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}

	return nil
}
예제 #17
0
파일: store.go 프로젝트: romana/core
func (agentStore *agentStore) findRouteByIface(routeIface string) (*Route, error) {
	log.Trace(trace.Inside, "Acquiring store mutex for findRoute")
	agentStore.mu.Lock()
	defer func() {
		log.Trace(trace.Inside, "Releasing store mutex for findRoute")
		agentStore.mu.Unlock()
	}()
	log.Trace(trace.Inside, "Acquired store mutex for findRoute")

	var route Route
	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Where("ip = ?", routeIface).First(&route)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return nil, err
	}
	if db.Error != nil {
		return nil, db.Error
	}
	return &route, nil
}
예제 #18
0
파일: store.go 프로젝트: romana/core
func (agentStore *agentStore) deleteRoute(route *Route) error {
	log.Trace(trace.Inside, "Acquiring store mutex for deleteRoute")
	agentStore.mu.Lock()
	defer func() {
		log.Trace(trace.Inside, "Releasing store mutex for deleteRoute")
		agentStore.mu.Unlock()
	}()
	log.Trace(trace.Inside, "Acquired store mutex for deleteRoute")

	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Delete(route)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}

	return nil
}
예제 #19
0
파일: store.go 프로젝트: romana/core
// switchIPtablesRule changes IPtablesRule state.
func (firewallStore *firewallStore) switchIPtablesRule(rule *IPtablesRule, op opSwitchIPtables) error {

	// Fast track return if nothing to be done
	if rule.State == op.String() {
		log.Infof("switchIPtablesRule nothing to be done for %s", rule.State)
		return nil
	}

	log.Info("Acquiring store mutex for switchIPtablesRule")
	firewallStore.mu.Lock()
	defer func() {
		log.Info("Releasing store mutex for switchIPtablesRule")
		firewallStore.mu.Unlock()
	}()
	log.Info("Acquired store mutex for switchIPtablesRule")

	// if toggle requested then reverse current state
	if op == toggleRule {
		if rule.State == setRuleInactive.String() {
			rule.State = setRuleActive.String()
		} else {
			rule.State = setRuleInactive.String()
		}
		// otherwise just assign op value
	} else {
		rule.State = op.String()
	}

	db := firewallStore.DbStore.Db
	firewallStore.DbStore.Db.Save(rule)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}

	return nil
}
예제 #20
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) findTenant(id uint64) (Tenant, error) {
	var tenants []Tenant
	log.Println("In findTenant()")
	mysqlStore.db.Find(&tenants)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return Tenant{}, err
	}
	for i := range tenants {
		if tenants[i].Id == id {
			return tenants[i], nil
		}
	}
	return Tenant{}, errors.New("Not found")
	//	tenant := Tenant{}
	//	mysqlStore.db.Where("id = ?", id).First(&tenant)
	//	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	//	if err != nil {
	//		return tenant, err
	//	}
	//	return tenant, nil
}
예제 #21
0
파일: store.go 프로젝트: romana/core
func (firewallStore *firewallStore) findIPtablesRules(subString string) (*[]IPtablesRule, error) {
	log.Info("Acquiring store mutex for findIPtablesRule")
	firewallStore.mu.Lock()
	defer func() {
		log.Info("Releasing store mutex for findIPtablesRule")
		firewallStore.mu.Unlock()
	}()
	log.Info("Acquired store mutex for findIPtablesRule")

	var rules []IPtablesRule
	db := firewallStore.DbStore.Db
	searchString := "%" + subString + "%"
	firewallStore.DbStore.Db.Where("body LIKE ?", searchString).Find(&rules)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return nil, err
	}
	if db.Error != nil {
		return nil, db.Error
	}
	return &rules, nil
}
예제 #22
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) findSegment(tenantId uint64, id uint64) (Segment, error) {
	var segments []Segment
	log.Println("In listSegments()")
	mysqlStore.db.Where("tenant_id = ?", tenantId).Find(&segments)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		return Segment{}, err
	}
	for i := range segments {
		if segments[i].Id == id {
			return segments[i], nil
		}
	}
	return Segment{}, errors.New("Not found")
	//	segment := Segment{}
	//	mysqlStore.db.Where("tenant_id = ? AND id = ?", tenantId, id).First(&segment)
	//	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	//	if err != nil {
	//		return segment, err
	//	}
	//	return segment, nil
}
예제 #23
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) addVm(stride uint, vm *Vm) error {
	tx := mysqlStore.db.Begin()

	row := tx.Model(IpamVm{}).Where("host_id = ? AND segment_id = ?", vm.HostId, vm.SegmentId).Select("IFNULL(MAX(seq),-1)+1").Row()
	row.Scan(&vm.Seq)
	log.Printf("New sequence is %d\n", vm.Seq)

	// vmSeq is the sequence number of VM in a given host
	effectiveVmSeq := getEffectiveSeq(vm.Seq, stride)
	log.Printf("Effective sequence for seq %d (stride %d): %d\n", vm.Seq, stride, effectiveVmSeq)
	vm.EffectiveSeq = effectiveVmSeq
	ipamVm := IpamVm{Vm: *vm}
	tx.NewRecord(ipamVm)
	tx.Create(&ipamVm)
	err := common.MakeMultiError(mysqlStore.db.GetErrors())
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return nil
}
예제 #24
0
파일: store.go 프로젝트: romana/core
// Authenticate returns a list of roles this credential
// has or an error if cannot authenticate.
func (rootStore *rootStore) Authenticate(cred common.Credential) ([]common.Role, error) {

	if !rootStore.isAuthEnabled {
		log.Println("Authentication is disabled")
		return nil, nil
	} else {
		log.Println("Authentication is enabled")
		gormDb := rootStore.DbStore.Db
		pwdFunc, err := rootStore.DbStore.GetPasswordFunction()
		if err != nil {
			return nil, err
		}
		whereClause := fmt.Sprintf("username = ? AND password = %s", pwdFunc)
		var roles []common.Role
		gormDb.Table("role").Select("role.name").Joins("JOIN user_roles ON role.id = user_roles.role_id JOIN users ON users.id = user_roles.user_id").Where(whereClause, cred.Username, cred.Password).Find(roles)
		err = common.MakeMultiError(rootStore.DbStore.Db.GetErrors())
		if err != nil {
			return nil, err
		}
		return roles, nil
	}
}
예제 #25
0
파일: store.go 프로젝트: romana/core
func (agentStore *agentStore) addRoute(route *Route) error {
	log.Info("Acquiring store mutex for addRoute")
	agentStore.mu.Lock()
	defer func() {
		log.Info("Releasing store mutex for addRoute")
		agentStore.mu.Unlock()
	}()
	log.Info("Acquired store mutex for addRoute")

	db := agentStore.DbStore.Db
	agentStore.DbStore.Db.Create(route)
	if db.Error != nil {
		return db.Error
	}
	agentStore.DbStore.Db.NewRecord(*route)
	err := common.MakeMultiError(db.GetErrors())
	if err != nil {
		return err
	}
	if db.Error != nil {
		return db.Error
	}
	return nil
}
예제 #26
0
파일: store.go 프로젝트: romana/core
// deleteEndpoint releases the IP(s) owned by the endpoint into assignable
// pool.
func (ipamStore *ipamStore) deleteEndpoint(ip string) (Endpoint, error) {
	tx := ipamStore.DbStore.Db.Begin()
	results := make([]Endpoint, 0)
	tx.Where(&Endpoint{Ip: ip}).Find(&results)
	if len(results) == 0 {
		tx.Rollback()
		return Endpoint{}, common.NewError404("endpoint", ip)
	}
	if len(results) > 1 {
		// This cannot happen by constraints...
		tx.Rollback()
		errMsg := fmt.Sprintf("Expected one result for ip %s, got %v", ip, results)
		log.Printf(errMsg)
		return Endpoint{}, common.NewError500(errors.New(errMsg))
	}
	tx = tx.Model(Endpoint{}).Where("ip = ?", ip).Update("in_use", false)
	err := common.MakeMultiError(tx.GetErrors())
	if err != nil {
		tx.Rollback()
		return Endpoint{}, err
	}
	tx.Commit()
	return results[0], nil
}
예제 #27
0
파일: mysql.go 프로젝트: yangzhares/core
func (mysqlStore *mysqlStore) createSchema(force bool) error {
	log.Println("tenant: in createSchema(", force, ")")
	// Connect to mysql database
	schemaName := mysqlStore.info.Database
	mysqlStore.info.Database = "mysql"
	mysqlStore.setConnString()
	err := mysqlStore.connect()

	if err != nil {
		return err
	}
	var sql string
	if force {
		sql = fmt.Sprintf("DROP DATABASE IF EXISTS %s", schemaName)
		res, err := mysqlStore.db.DB().Exec(sql)
		if err != nil {
			return err
		}

		rows, _ := res.RowsAffected()
		log.Println(sql, ": ", rows)
	}

	sql = fmt.Sprintf("CREATE DATABASE %s", schemaName)
	res, err := mysqlStore.db.DB().Exec(sql)
	if err != nil {
		return err
	}
	rows, _ := res.RowsAffected()
	log.Println(sql, ": ", rows)
	mysqlStore.info.Database = schemaName
	mysqlStore.setConnString()
	err = mysqlStore.connect()
	if err != nil {
		return err
	}
	log.Println("Creating segments table")
	mysqlStore.db.CreateTable(&Segment{})

	// Sequence numbers are unique for segments within a tenant.
	mysqlStore.db.Model(&Segment{}).AddUniqueIndex("idx_tenant_id_seq", "tenant_id", "seq")
	// Segment ID is unique within a tenant.
	mysqlStore.db.Model(&Segment{}).AddUniqueIndex("idx_segment_tenant_name", "tenant_id", "name")

	log.Println("Creating tenants table")
	mysqlStore.db.CreateTable(&Tenant{})
	// Tenant name is unique.
	mysqlStore.db.Model(&Tenant{}).AddUniqueIndex("idx_name", "name")
	// Sequence name is unique for tenants in a datacenter (for now, all).
	mysqlStore.db.Model(&Tenant{}).AddUniqueIndex("idx_seq", "seq")

	errs := mysqlStore.db.GetErrors()
	log.Println("Errors", errs)
	err2 := common.MakeMultiError(errs)

	if err2 != nil {
		return err2
	}
	return nil

}