Exemple #1
0
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 := util.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
}
Exemple #2
0
func init() {
	//tcpAddr := fmt.Sprintf("tcp://:%d", MOD_PORT)

	_, fsPath, parseAddrErr := util.ParseAddress(MOD_ADDR)
	if parseAddrErr != nil {
		panic(parseAddrErr)
	}

	fsPathDir := filepath.Dir(fsPath)
	os.MkdirAll(fsPathDir, 0755)

	mc := &module.ModuleConfig{
		Address: MOD_ADDR,
		Config:  config.New(),
	}

	module.RegisterModule(MOD_NAME, true, Init, []*module.ModuleConfig{mc})
}
func init() {
	//tcpAddr := fmt.Sprintf("tcp://:%d", ModPort)

	_, fsPath, parseAddrErr := util.ParseAddress(modAddress)
	if parseAddrErr != nil {
		panic(parseAddrErr)
	}

	fsPathDir := filepath.Dir(fsPath)
	os.MkdirAll(fsPathDir, 0755)

	mc := &module.Config{
		Address: modAddress,
		Config:  config.New(),
	}

	module.RegisterModule(modName, true, newMod, []*module.Config{mc})
}
Exemple #4
0
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 := util.ParseAddress(c.r.Config.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 := util.ParseAddress(c.r.Config.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 := util.ParseAddress(c.r.Config.Host)
			if addrErr != nil {
				panic(addrErr)
			}

			if c.moduleTypeID == -1 || c.moduleInstanceAddress == "" {
				cmd.Usage()
				return
			}

			modTypeIDStr := fmt.Sprintf("%d", c.moduleTypeID)
			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,
				"typeId":  modTypeIDStr,
				"address": c.moduleInstanceAddress,
				"start":   modInstStartStr,
				"config":  cfgJSON}).Debug("post create module instance")

			client := &http.Client{}
			resp, respErr := client.PostForm(u,
				url.Values{
					"typeId":  {modTypeIDStr},
					"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 := util.ParseAddress(c.r.Config.Host)
			if addrErr != nil {
				panic(addrErr)
			}

			if c.moduleInstanceID == -1 {
				cmd.Usage()
				return
			}

			u := fmt.Sprintf(
				"http://%s/r/module/instances/%d/start", addr, c.moduleInstanceID)

			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)
}
Exemple #5
0
func (mod *Module) Start() error {

	proto, addr, parseAddrErr := util.ParseAddress(mod.Address())
	if parseAddrErr != nil {
		return parseAddrErr
	}

	const validProtoPatt = "(?i)^unix|tcp$"
	isProtoValid, matchProtoErr := regexp.MatchString(validProtoPatt, proto)
	if matchProtoErr != nil {
		return errors.New(fmt.Sprintf(
			"Error matching protocol %s with pattern '%s' ERR: %v",
			proto, validProtoPatt, matchProtoErr))
	}
	if !isProtoValid {
		return errors.New(fmt.Sprintf("Invalid protocol %s", proto))
	}

	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
		return err
	}

	var specPath string
	var startFunc func() error

	mux := mod.buildMux()

	if proto == "unix" {
		sockFile := addr
		sockFileDir := filepath.Dir(sockFile)
		mkSockFileDirErr := os.MkdirAll(sockFileDir, 0755)
		if mkSockFileDirErr != nil {
			return mkSockFileDirErr
		}

		_ = os.RemoveAll(sockFile)

		specPath = mod.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 (m *mod) Start() error {

	proto, addr, parseAddrErr := util.ParseAddress(m.Address())
	if parseAddrErr != nil {
		return parseAddrErr
	}

	const validProtoPatt = "(?i)^unix|tcp$"
	isProtoValid, matchProtoErr := regexp.MatchString(validProtoPatt, proto)
	if matchProtoErr != nil {
		return errors.WithFieldsE(errors.Fields{
			"protocol":       proto,
			"validProtoPatt": validProtoPatt,
		}, "error matching protocol", matchProtoErr)
	}
	if !isProtoValid {
		return errors.WithField("protocol", proto, "invalid protocol")
	}

	if err := m.r.InitDrivers(); err != nil {
		return errors.WithFieldsE(errors.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
}
Exemple #7
0
	Short: "The device manager",
	Run: func(cmd *cobra.Command, args []string) {
		if isHelpFlags(cmd) {
			cmd.Usage()
		} else {
			deviceGetCmd.Run(deviceGetCmd, args)
		}
	},
}

var moduleTypesCmd = &cobra.Command{
	Use:   "types",
	Short: "List the available module types and their IDs",
	Run: func(cmd *cobra.Command, args []string) {

		_, addr, addrErr := util.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 {
Exemple #8
0
	Short: "The device manager",
	Run: func(cmd *cobra.Command, args []string) {
		if isHelpFlags(cmd) {
			cmd.Usage()
		} else {
			deviceGetCmd.Run(deviceGetCmd, args)
		}
	},
}

var moduleTypesCmd = &cobra.Command{
	Use:   "types",
	Short: "List the available module types and their IDs",
	Run: func(cmd *cobra.Command, args []string) {

		_, addr, addrErr := util.ParseAddress(r.Config.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 {