func (m *mod) Start() error { stdOut := log.StandardLogger().Writer() stdErr := log.StandardLogger().Writer() r := mux.NewRouter() r.Handle("/r/module/instances", handlers.LoggingHandler(stdOut, http.HandlerFunc(moduleInstHandler))) r.Handle("/r/module/instances/{id}/start", handlers.LoggingHandler(stdOut, http.HandlerFunc(moduleInstStartHandler))) r.Handle("/r/module/types", handlers.LoggingHandler(stdOut, http.HandlerFunc(moduleTypeHandler))) r.Handle("/images/rexray-banner-logo.svg", handlers.LoggingHandler(stdOut, http.HandlerFunc(imagesHandler))) r.Handle("/scripts/jquery-1.11.3.min.js", handlers.LoggingHandler(stdOut, http.HandlerFunc(scriptsHandler))) r.Handle("/styles/main.css", handlers.LoggingHandler(stdOut, http.HandlerFunc(stylesHandler))) r.Handle("/", handlers.LoggingHandler(stdOut, http.HandlerFunc(indexHandler))) _, addr, parseAddrErr := gotil.ParseAddress(m.Address()) if parseAddrErr != nil { return parseAddrErr } s := &http.Server{ Addr: addr, Handler: r, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, ErrorLog: golog.New(stdErr, "", 0), } go func() { defer stdOut.Close() defer stdErr.Close() sErr := s.ListenAndServe() if sErr != nil { panic(sErr) } }() return nil }
func init() { //tcpAddr := fmt.Sprintf("tcp://:%d", ModPort) _, fsPath, parseAddrErr := gotil.ParseAddress(modAddress) if parseAddrErr != nil { panic(parseAddrErr) } fsPathDir := filepath.Dir(fsPath) os.MkdirAll(fsPathDir, 0755) mc := &module.Config{ Address: modAddress, Config: gofig.New(), } module.RegisterModule(modName, true, newModule, []*module.Config{mc}) }
func (m *mod) Start() error { proto, addr, parseAddrErr := gotil.ParseAddress(m.Address()) if parseAddrErr != nil { return parseAddrErr } const validProtoPatt = "(?i)^unix|tcp$" isProtoValid, matchProtoErr := regexp.MatchString(validProtoPatt, proto) if matchProtoErr != nil { return goof.WithFieldsE(goof.Fields{ "protocol": proto, "validProtoPatt": validProtoPatt, }, "error matching protocol", matchProtoErr) } if !isProtoValid { return goof.WithField("protocol", proto, "invalid protocol") } if err := m.r.InitDrivers(); err != nil { return goof.WithFieldsE(goof.Fields{ "m": m, "m.r": m.r, }, "error initializing drivers", err) } if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil { return err } var specPath string var startFunc func() error mux := m.buildMux() if proto == "unix" { sockFile := addr sockFileDir := filepath.Dir(sockFile) mkSockFileDirErr := os.MkdirAll(sockFileDir, 0755) if mkSockFileDirErr != nil { return mkSockFileDirErr } _ = os.RemoveAll(sockFile) specPath = m.Address() startFunc = func() error { l, lErr := net.Listen("unix", sockFile) if lErr != nil { return lErr } defer l.Close() defer os.Remove(sockFile) return http.Serve(l, mux) } } else { specPath = addr startFunc = func() error { s := &http.Server{ Addr: addr, Handler: mux, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } return s.ListenAndServe() } } go func() { sErr := startFunc() if sErr != nil { panic(sErr) } }() writeSpecErr := ioutil.WriteFile( "/etc/docker/plugins/rexray.spec", []byte(specPath), 0644) if writeSpecErr != nil { return writeSpecErr } return nil }
func (c *CLI) initModuleCmds() { c.moduleCmd = &cobra.Command{ Use: "module", Short: "The module manager", Run: func(cmd *cobra.Command, args []string) { cmd.Usage() }, } c.serviceCmd.AddCommand(c.moduleCmd) c.moduleTypesCmd = &cobra.Command{ Use: "types", Short: "List the available module types and their IDs", Run: func(cmd *cobra.Command, args []string) { _, addr, addrErr := gotil.ParseAddress(c.host()) if addrErr != nil { panic(addrErr) } u := fmt.Sprintf("http://%s/r/module/types", addr) client := &http.Client{} resp, respErr := client.Get(u) if respErr != nil { panic(respErr) } defer resp.Body.Close() body, bodyErr := ioutil.ReadAll(resp.Body) if bodyErr != nil { panic(bodyErr) } fmt.Println(string(body)) }, } c.moduleCmd.AddCommand(c.moduleTypesCmd) c.moduleInstancesCmd = &cobra.Command{ Use: "instance", Short: "The module instance manager", Run: func(cmd *cobra.Command, args []string) { cmd.Usage() }, } c.moduleCmd.AddCommand(c.moduleInstancesCmd) c.moduleInstancesListCmd = &cobra.Command{ Use: "get", Aliases: []string{"ls", "list"}, Short: "List the running module instances", Run: func(cmd *cobra.Command, args []string) { _, addr, addrErr := gotil.ParseAddress(c.host()) if addrErr != nil { panic(addrErr) } u := fmt.Sprintf("http://%s/r/module/instances", addr) client := &http.Client{} resp, respErr := client.Get(u) if respErr != nil { panic(respErr) } defer resp.Body.Close() body, bodyErr := ioutil.ReadAll(resp.Body) if bodyErr != nil { panic(bodyErr) } fmt.Println(string(body)) }, } c.moduleInstancesCmd.AddCommand(c.moduleInstancesListCmd) c.moduleInstancesCreateCmd = &cobra.Command{ Use: "create", Aliases: []string{"new"}, Short: "Create a new module instance", Run: func(cmd *cobra.Command, args []string) { _, addr, addrErr := gotil.ParseAddress(c.host()) if addrErr != nil { panic(addrErr) } if c.moduleTypeName == "" || c.moduleInstanceAddress == "" { cmd.Usage() return } modInstStartStr := fmt.Sprintf("%v", c.moduleInstanceStart) u := fmt.Sprintf("http://%s/r/module/instances", addr) cfgJSON, cfgJSONErr := c.r.Config.ToJSON() if cfgJSONErr != nil { panic(cfgJSONErr) } log.WithFields(log.Fields{ "url": u, "name": c.moduleInstanceName, "typeName": c.moduleTypeName, "address": c.moduleInstanceAddress, "start": modInstStartStr, "config": cfgJSON}).Debug("post create module instance") client := &http.Client{} resp, respErr := client.PostForm(u, url.Values{ "name": {c.moduleInstanceName}, "typeName": {c.moduleTypeName}, "address": {c.moduleInstanceAddress}, "start": {modInstStartStr}, "config": {cfgJSON}, }) if respErr != nil { panic(respErr) } defer resp.Body.Close() body, bodyErr := ioutil.ReadAll(resp.Body) if bodyErr != nil { panic(bodyErr) } fmt.Println(string(body)) }, } c.moduleInstancesCmd.AddCommand(c.moduleInstancesCreateCmd) c.moduleInstancesStartCmd = &cobra.Command{ Use: "start", Short: "Starts a module instance", Run: func(cmd *cobra.Command, args []string) { _, addr, addrErr := gotil.ParseAddress(c.host()) if addrErr != nil { panic(addrErr) } if c.moduleInstanceName == "" { cmd.Usage() return } u := fmt.Sprintf( "http://%s/r/module/instances/%s/start", addr, c.moduleInstanceName) client := &http.Client{} resp, respErr := client.Get(u) if respErr != nil { panic(respErr) } defer resp.Body.Close() body, bodyErr := ioutil.ReadAll(resp.Body) if bodyErr != nil { panic(bodyErr) } fmt.Println(string(body)) }, } c.moduleInstancesCmd.AddCommand(c.moduleInstancesStartCmd) }
func (s *server) initEndpoints(ctx types.Context) error { endpointsObj := s.config.Get(types.ConfigEndpoints) if endpointsObj == nil { if err := s.initDefaultEndpoint(); err != nil { return goof.WithError("no endpoints defined", err) } endpointsObj = s.config.Get(types.ConfigEndpoints) } endpoints, ok := endpointsObj.(map[string]interface{}) if !ok { return goof.New("endpoints invalid type") } if len(endpoints) == 0 { if err := s.initDefaultEndpoint(); err != nil { return err } } for endpointName := range endpoints { endpoint := fmt.Sprintf("%s.%s", types.ConfigEndpoints, endpointName) address := fmt.Sprintf("%s.address", endpoint) laddr := s.config.GetString(address) if laddr == "" { return goof.WithField("endpoint", endpoint, "missing address") } laddrET := types.ParseEndpointType(laddr) switch laddrET { case types.TCPEndpoint: var tcpPort int func() { tcpPortLock.Lock() defer tcpPortLock.Unlock() tcpPort = gotil.RandomTCPPort() }() laddr = fmt.Sprintf("tcp://127.0.0.1:%d", tcpPort) s.ctx.WithField("endpoint", endpoint).Info( "initializing auto tcp endpoint") case types.UnixEndpoint: laddr = fmt.Sprintf("unix://%s", utils.GetTempSockFile()) s.ctx.WithField("endpoint", endpoint).Info( "initializing auto unix endpoint") } s.ctx.WithFields(log.Fields{ "endpoint": endpoint, "address": laddr}).Debug("endpoint info") s.addrs = append(s.addrs, laddr) proto, addr, err := gotil.ParseAddress(laddr) if err != nil { return err } logFields := map[string]interface{}{ "endpoint": endpointName, "address": laddr, } tlsConfig, err := utils.ParseTLSConfig(s.config.Scope(endpoint), logFields, endpoint) if err != nil { return err } ctx.WithFields(logFields).Info("configured endpoint") srv, err := s.newHTTPServer(proto, addr, tlsConfig) if err != nil { return err } ctx.Info("server created") s.servers = append(s.servers, srv) } return nil }
// IsLocalServerActive returns a flag indicating whether or not a local // libStorage is already running. func IsLocalServerActive( ctx apitypes.Context, config gofig.Config) (host string, running bool) { var ( isLocal bool specFile = SpecFilePath() ) if gotil.FileExists(specFile) { if h, _ := ReadSpecFile(); h != "" { host = h logHostSpec(ctx, host, "read spec file") defer func() { if running || !isLocal { return } host = "" os.RemoveAll(specFile) ctx.WithField("specFile", specFile).Info( "removed invalid spec file") }() } } if host == "" { host = config.GetString(apitypes.ConfigHost) } if host == "" { return "", false } proto, addr, err := gotil.ParseAddress(host) if err != nil { return "", false } switch proto { case "unix": isLocal = true ctx.WithField("sock", addr).Debug("is local unix server active") var sockExists, isActive bool if sockExists = gotil.FileExists(addr); sockExists { if isActive = IsAddressActive(proto, addr); !isActive { os.RemoveAll(addr) ctx.WithField("sockFile", addr).Info( "removed invalid sock file") } } return host, isActive case "tcp": m := localHostRX.FindStringSubmatch(addr) if len(m) < 3 { return "", false } isLocal = true port, err := strconv.Atoi(m[2]) if err != nil { return "", false } ctx.WithField("port", port).Debug("is local tcp server active") return host, IsAddressActive(proto, addr) } return "", false }
func (d *driver) Init(ctx types.Context, config gofig.Config) error { logFields := log.Fields{} addr := config.GetString(types.ConfigHost) d.ctx = ctx.WithValue(context.HostKey, addr) d.ctx.Debug("got configured host address") proto, lAddr, err := gotil.ParseAddress(addr) if err != nil { return err } tlsConfig, err := utils.ParseTLSConfig( config, logFields, "libstorage.client") if err != nil { return err } host := getHost(proto, lAddr, tlsConfig) lsxPath := config.GetString(types.ConfigExecutorPath) cliType := types.ParseClientType(config.GetString(types.ConfigClientType)) disableKeepAlive := config.GetBool(types.ConfigHTTPDisableKeepAlive) logFields["host"] = host logFields["lsxPath"] = lsxPath logFields["clientType"] = cliType logFields["disableKeepAlive"] = disableKeepAlive httpTransport := &http.Transport{ Dial: func(string, string) (net.Conn, error) { if tlsConfig == nil { return net.Dial(proto, lAddr) } return tls.Dial(proto, lAddr, tlsConfig) }, DisableKeepAlives: disableKeepAlive, } apiClient := apiclient.New(host, httpTransport) logReq := config.GetBool(types.ConfigLogHTTPRequests) logRes := config.GetBool(types.ConfigLogHTTPResponses) apiClient.LogRequests(logReq) apiClient.LogResponses(logRes) logFields["enableInstanceIDHeaders"] = EnableInstanceIDHeaders logFields["enableLocalDevicesHeaders"] = EnableLocalDevicesHeaders logFields["logRequests"] = logReq logFields["logResponses"] = logRes d.client = client{ APIClient: apiClient, ctx: ctx, config: config, clientType: cliType, serviceCache: &lss{Store: utils.NewStore()}, } if d.clientType == types.IntegrationClient { newIIDCache := utils.NewStore dur, err := time.ParseDuration( config.GetString(types.ConfigClientCacheInstanceID)) if err != nil { logFields["iidCacheDuration"] = dur.String() newIIDCache = func() types.Store { return utils.NewTTLStore(dur, true) } } d.lsxCache = &lss{Store: utils.NewStore()} d.supportedCache = utils.NewStore() d.instanceIDCache = &lss{Store: newIIDCache()} } d.ctx.WithFields(logFields).Info("created libStorage client") if err := d.dial(ctx); err != nil { return err } d.ctx.Info("successefully dialed libStorage server") return nil }