Beispiel #1
0
func (s *Service) regSrv(config vulcan.Config) {
	for {
		registry := vulcan.NewRegistry(config, s.options.EtcdNodes)
		entry, err := vulcan.NewEndpointWithID(s.options.ApplicationId,
			s.options.ApplicationId,
			s.options.Interface,
			s.options.Port)
		if err != nil {
			log.Errorf(err.Error())
		}
		log.Debugf(entry.String())

		if err := registry.RegisterBackend(entry); err != nil {
			log.Errorf(err.Error())
		}
		if err := registry.RegisterServer(entry); err != nil {
			log.Errorf(err.Error())
		}
		loc := vulcan.NewLocation(s.options.HostLimit,
			[]string{},
			s.options.RegisterPath,
			s.options.ApplicationId,
			[]middleware.Middleware{})
		if err := registry.RegisterFrontend(loc); err != nil {
			log.Errorf(err.Error())
		}
		log.Debugf("Syncing up Vulcan")
		time.Sleep(4000 * time.Millisecond)
	}
}
Beispiel #2
0
// RegisterApp adds a new backend and a single server with Vulcand.
func (s *LeaderRegistry) RegisterApp(registration *AppRegistration) error {
	log.Infof("Registering app: %v", registration)

	endpoint, err := vulcan.NewEndpointWithID(s.Group, registration.Name, registration.Host, registration.Port)
	if err != nil {
		return err
	}

	err = s.client.RegisterBackend(endpoint)
	if err != nil {
		log.Errorf("Failed to register backend for endpoint: %v, %s", endpoint, err)
		return err
	}

	if s.IsMaster {
		err = s.maintainLeader(endpoint)
	} else {
		err = s.initLeader(endpoint)
	}

	if err != nil {
		log.Errorf("Failed to register server for endpoint: %v, %s", endpoint, err)
		return err
	}

	return nil
}
Beispiel #3
0
// Subscribe watches etcd changes and generates structured events telling vulcand to add or delete frontends, hosts etc.
// It is a blocking function.
func (n *ng) Subscribe(changes chan interface{}, cancelC chan bool) error {
	// This index helps us to get changes in sequence, as they were performed by clients.
	waitIndex := uint64(0)
	for {
		response, err := n.client.Watch(n.etcdKey, waitIndex, true, nil, cancelC)
		if err != nil {
			switch err {
			case etcd.ErrWatchStoppedByUser:
				log.Infof("Stop watching: graceful shutdown")
				return nil
			default:
				log.Errorf("unexpected error: %s, stop watching", err)
				return err
			}
		}
		waitIndex = response.Node.ModifiedIndex + 1
		log.Infof("%s", responseToString(response))
		change, err := n.parseChange(response)
		if err != nil {
			log.Warningf("Ignore '%s', error: %s", responseToString(response), err)
			continue
		}
		if change != nil {
			log.Infof("%v", change)
			select {
			case changes <- change:
			case <-cancelC:
				return nil
			}
		}
	}
}
Beispiel #4
0
func (n *ng) GetUserByEmail(email string) (MinimalUserRecord, error) {
	user := MinimalUserRecord{}
	session, _ := n.Database.CreateSession()
	defer session.Close()
	log.Debugf("===> Querying Database for email: " + email)
	if err := session.Query(`SELECT id,
    email,
    hashed_password,
    password_salt,
    encryption_keys,
    meta_storage,
    is_active
    FROM access_users
    WHERE email = ?
    LIMIT 1 ALLOW FILTERING`,
		email).Consistency(gocql.One).Scan(&user.Id,
		&user.Email,
		&user.HashedPassword,
		&user.PasswordSalt,
		&user.EncryptionKeys,
		&user.MetaStorage,
		&user.IsActive); err != nil {
		log.Errorf("===> Got a database error while querying for specific user")
		return MinimalUserRecord{}, err
	}
	return user, nil
}
Beispiel #5
0
// registerLocationForScope registers a location with a specified scope.
func (app *App) registerLocationForScope(methods []string, path string, scope Scope, middlewares []middleware.Middleware) {
	host, err := app.apiHostForScope(scope)
	if err != nil {
		log.Errorf("Failed to register a location: %v", err)
		return
	}
	app.registerLocationForHost(methods, path, host, middlewares)
}
Beispiel #6
0
// RegisterHandler registers the frontends and middlewares with Vulcand.
func (s *LeaderRegistry) RegisterHandler(registration *HandlerRegistration) error {
	log.Infof("Registering handler: %v", registration)

	location := vulcan.NewLocation(registration.Host, registration.Methods, registration.Path, registration.Name, registration.Middlewares)
	err := s.client.RegisterFrontend(location)
	if err != nil {
		log.Errorf("Failed to register frontend for location: %v, %s", location, err)
		return err
	}

	err = s.client.RegisterMiddleware(location)
	if err != nil {
		log.Errorf("Failed to register middleware for location: %v, %s", location, err)
		return err
	}

	return nil
}
Beispiel #7
0
// RegisterApp adds a new backend and a single server with Vulcand.
func (s *LBRegistry) RegisterApp(registration *AppRegistration) error {
	log.Infof("Registering app: %v", registration)

	endpoint, err := vulcan.NewEndpoint(registration.Name, registration.Host, registration.Port)
	if err != nil {
		return err
	}

	err = s.client.RegisterBackend(endpoint)
	if err != nil {
		log.Errorf("Failed to register backend for endpoint: %v, %s", endpoint, err)
		return err
	}

	err = s.client.UpsertServer(endpoint, s.TTL)
	if err != nil {
		log.Errorf("Failed to register server for endpoint: %v, %s", endpoint, err)
		return err
	}

	return nil
}
Beispiel #8
0
func Run() error {
	options, err := ParseCommandLine()
	if err != nil {
		return fmt.Errorf("failed to parse command line: %s", err)
	}
	service := NewService(options)
	if err := service.Start(); err != nil {
		log.Errorf("Failed to start service: %v", err)
		return fmt.Errorf("service start failure: %s", err)
	} else {
		log.Infof("Service exited gracefully")
	}
	return nil
}
Beispiel #9
0
func (n *ng) GetJwtToken(token string) ([]byte, error) {
	//log.Debugf(validTime)
	var key string
	session, _ := n.Database.CreateSession()
	defer session.Close()
	if err := session.Query(`SELECT key
    FROM tokens
    WHERE jwt = ?
    LIMIT 1`,
		token).Consistency(gocql.One).Scan(&key); err != nil {
		log.Errorf("===> Got a database error while querying for a specific key")
		return []byte(""), err
	}
	return []byte(key), nil
}
Beispiel #10
0
// Helper that replies with the 500 code and happened error message.
func ReplyInternalError(w http.ResponseWriter, message string) {
	log.Errorf("Internal server error: %v", message)
	Reply(w, Response{"message": message}, http.StatusInternalServerError)
}
Beispiel #11
0
func main() {
	app := cli.NewApp()
	app.Name = "XTOOLS for SITREP"
	app.Usage = "Tools for the SITREP Server Environment"
	app.Commands = []cli.Command{
		{
			Name:    "index",
			Aliases: []string{"i"},
			Usage:   "Re-Index everything into Elasticsearch",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "cassandra, c",
					Value: "127.0.0.1",
					Usage: "Cassandra Endpoint",
				},
				cli.StringFlag{
					Name:  "keyspace, k",
					Value: "sitrep_dev",
					Usage: "SITREP ENDPOINT",
				},
				cli.StringFlag{
					Name:  "elastichost, e",
					Value: "elasticsearch.dev.docker",
					Usage: "Elasticsearch Host",
				},
			},
			Action: func(c *cli.Context) {
				db := gocql.NewCluster(c.String("cassandra"))
				db.Keyspace = "sitrep_dev"
				db.DiscoverHosts = true
				db.Discovery = gocql.DiscoveryConfig{
					DcFilter:   "",
					RackFilter: "",
					Sleep:      30 * time.Second,
				}
				session, _ := db.CreateSession()
				defer session.Close()

				news := NewsRecord{}
				cl := elastigo.NewConn()
				cl.Domain = c.String("elastichost")

				iter := session.Query(`SELECT url, newsid, content, media, postedat, previewcontent, title FROM newsstoriestable`).Iter()
				for iter.Scan(&news.Url,
					&news.NewsId,
					&news.Content,
					&news.Media,
					&news.PostedAt,
					&news.PreviewContent,
					&news.Title) {
					item := &Record{
						Title:    news.Title,
						Preview:  news.PreviewContent,
						Image:    *news.Media["preview"],
						Link:     "/news/flobama/" + news.Url,
						Content:  news.Content,
						PostedAt: news.PostedAt,
						Media:    news.Media,
					}
					cl.Index("sites", "news", news.Url, nil, item)
					fmt.Println("Indexed:", news.Url)
				}
				if err := iter.Close(); err != nil {
					log.Errorf(err.Error())
				}
				page := IntellipediaPage{}
				iter2 := session.Query(`SELECT url, content, image, mapimg, name FROM intellipediapagestable`).Iter()
				for iter2.Scan(&page.Url,
					&page.Content,
					&page.Image,
					&page.PreviewContent,
					&page.Title) {
					item := &Record{
						Title:   page.Title,
						Preview: page.PreviewContent,
						Image:   page.Image,
						Link:    "/intellipedia/" + page.Url,
						Content: page.Content,
					}
					cl.Index("sites", "intellipedia", page.Url, nil, item)
					fmt.Println("Indexed:", page.Url)
				}
				if err := iter2.Close(); err != nil {
					log.Errorf(err.Error())
				}
				site := SocialSite{}
				iter3 := session.Query(`SELECT name, link, tile FROM categoriestable`).Iter()
				for iter3.Scan(&site.Name,
					&site.Link,
					&site.Tile) {
					item := &Record{
						Title:   site.Name,
						Preview: "",
						Image:   site.Tile,
						Link:    site.Link,
					}
					cl.Index("sites", "sites", strings.Replace(site.Name, " ", "-", -1), nil, item)
					fmt.Println("Indexed:", site.Name)
				}
				if err := iter3.Close(); err != nil {
					log.Errorf(err.Error())
				}
			},
		},
		{
			Name:    "complete",
			Aliases: []string{"c"},
			Usage:   "complete a task on the list",
			Action: func(c *cli.Context) {
				println("completed task: ", c.Args().First())
			},
		},
		{
			Name:    "template",
			Aliases: []string{"r"},
			Usage:   "options for task templates",
			Subcommands: []cli.Command{
				{
					Name:  "add",
					Usage: "add a new template",
					Action: func(c *cli.Context) {
						println("new task template: ", c.Args().First())
					},
				},
				{
					Name:  "remove",
					Usage: "remove an existing template",
					Action: func(c *cli.Context) {
						println("removed task template: ", c.Args().First())
					},
				},
			},
		},
	}

	app.Run(os.Args)
}