示例#1
0
func (s *Server) Start() error {
	c, _ := projects.ConfigDir()
	socketpath := filepath.Join(c, "projects.socket")

	addr, err := net.ResolveUnixAddr("unix", socketpath)

	if err != nil {
		return err
	}

	listener, e := net.ListenUnix("unix", addr)
	if e != nil {
		return e
	}

	var opts []grpc.ServerOption

	grpcServer := grpc.NewServer(opts...)
	messages.RegisterProjectsServer(grpcServer, &projectServer{s.core})
	messages.RegisterResourcesServer(grpcServer, &resourcesServer{s.core})
	messages.RegisterEventsServer(grpcServer, &eventsServer{s.core})
	//s.core.Log.Infof("project daemon started and listening on %s", socketpath)
	s.server = grpcServer
	s.listener = listener

	s.tomb.Go(func() error { return grpcServer.Serve(listener) })
	return nil
}
示例#2
0
func loadConfig() (*Config, error) {
	var config Config
	configfile, err := projects.ConfigFile()

	if err != nil {
		return nil, err
	}

	file, err := os.Open(configfile)

	if err != nil {
		if !os.IsNotExist(err) {
			return nil, err
		}

		return &config, nil
	}

	defer file.Close()

	err = decodeConfig(file, &config)

	configdir, _ := projects.ConfigDir()
	config.Path = configdir
	return &config, err
}
示例#3
0
func wrappedMain() int {

	if os.Getenv("GOMAXPROCS") == "" {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	log.SetOutput(os.Stderr)
	log.SetPrefix("[BOOTSTRAP] ")
	//setupStdin()

	log.Println("Initializing configuration")
	_, err := loadConfig()

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading configuration\n%s\n", err.Error())
		return 1
	}

	var configDir string
	configDir, err = projects.ConfigDir()

	if err != nil {
		return 1
	}

	log.Println("Initializing datastore")
	db, err := database.NewMongoDatastore() //database.NewDatabase(filepath.Join(configDir, "database"))

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error initializing database\n%s\n", err.Error())
		return 1
	}

	log.Println("Initializing core")
	core, e := projects.NewCore(projects.CoreConfig{
		Db:         db,
		ConfigPath: configDir,
	})

	if e != nil {
		fmt.Fprintf(os.Stderr, "Error while initializing core\n  %s\n", e.Error())
		return 1
	}

	defer core.Close()

	log.Println("Initializing server")
	server := server2.NewServer(core)

	err = run_server(server)

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error %v\n", err)
		return 1
	}

	return 0
}
示例#4
0
func getContext(client *server.Client) (*projects.DirectoryResource, error) {

	cur, err := os.Getwd()

	if err != nil {
		return nil, err
	}

	p := filepath.Join(cur, ".projects")

	configDir, _ := projects.ConfigDir()

	if p == configDir {
		return nil, nil
	}

	if !utils.FileExists(p) {
		return nil, nil
	}

	pFile := filepath.Join(p, "projects.toml")

	if !utils.FileExists(pFile) {
		return nil, nil
	}

	/*file, err := os.Open(pFile)

	if err != nil {
		return nil, err
	}*/

	/*defer file.Close()

	var conf projects.DirectoryResourceConfig
	_, err = toml.DecodeReader(file, &conf)

	pp := client.Projects()
	q := messages.ProjectQuery{
		Id: conf.Project.Id,
	}

	ppp, _ := pp.Get(context.Background(), &q)

	conf.Project = ppp

	return &projects.DirectoryResource{
		Config: &conf,
	}, err*/
	return nil, nil

}
示例#5
0
func init() {

	configDir, err := projects.ConfigDir()

	if err != nil {
		panic(err)
	}

	os.Setenv("PROJECTS_PATH", configDir)

	if !utils.FileExists(configDir) {

		err := os.MkdirAll(configDir, 0755)
		if err != nil {
			panic(err)
		}
	}

}
示例#6
0
func NewClient() (*Client, error) {

	c, e := projects.ConfigDir()

	if e != nil {
		return nil, e
	}

	path := filepath.Join(c, "projects.socket")

	trans := http.Transport{
		Dial: func(proto, addr string) (conn net.Conn, err error) {
			return net.Dial("unix", path)
		},
	}
	client := &http.Client{
		Transport: &trans,
	}
	return &Client{client}, nil
}
示例#7
0
func NewClient() (*Client, error) {
	var opts []grpc.DialOption
	opts = append(opts, grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
		return net.DialTimeout("unix", addr, timeout)
	}), grpc.WithInsecure())
	//addr, _ := net.ResolveUnixAddr("unix", "socket.unix")

	c, _ := projects.ConfigDir()

	path := filepath.Join(c, "projects.socket")

	conn, err := grpc.Dial(path, opts...)
	if err != nil {
		//grpclog.Fatalf("fail to dial: %v", err)
		return nil, err
	}

	err = waitForConnection(conn)

	return &Client{conn}, err
}
示例#8
0
func (self *Server) Listen() error {

	configPath, err := projects.ConfigDir()

	if err != nil {
		return err
	}
	var unixPath *net.UnixAddr
	var unixl *net.UnixListener
	unixPath, err = net.ResolveUnixAddr("unix", filepath.Join(configPath, "projects.socket"))
	if err != nil {
		return err
	}

	unixl, err = net.ListenUnix("unix", unixPath)
	if err != nil {
		return fmt.Errorf("cannot listen on unix socket: %v", err)
	}
	self.unixl = unixl

	self.tomb.Go(func() error { return http.Serve(self.unixl, self.mux) })
	return nil
}