cmd_net.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/asaskevich/govalidator"
  5. "github.com/cad/ovpm"
  6. "github.com/cad/ovpm/errors"
  7. "github.com/urfave/cli"
  8. )
  9. var netDefineCommand = cli.Command{
  10. Name: "def",
  11. Aliases: []string{"d"},
  12. Usage: "Define a network.",
  13. Flags: []cli.Flag{
  14. cli.StringFlag{
  15. Name: "cidr, c",
  16. Usage: "CIDR of the network",
  17. },
  18. cli.StringFlag{
  19. Name: "name, n",
  20. Usage: "name of the network",
  21. },
  22. cli.StringFlag{
  23. Name: "type, t",
  24. Usage: "type of the network (see $ovpm net types)",
  25. },
  26. cli.StringFlag{
  27. Name: "via, v",
  28. Usage: "if network type is route, via represents route's gateway",
  29. },
  30. },
  31. Action: func(c *cli.Context) error {
  32. action = "net:create"
  33. // Use default port if no port is specified.
  34. daemonPort := ovpm.DefaultDaemonPort
  35. if port := c.GlobalInt("daemon-port"); port != 0 {
  36. daemonPort = port
  37. }
  38. // Validate network name.
  39. if netName := c.String("name"); govalidator.IsNull(netName) {
  40. err := errors.EmptyValue("net", netName)
  41. exit(1)
  42. return err
  43. }
  44. // Validate network types.
  45. if netType := c.String("type"); !ovpm.IsNetworkType(netType) {
  46. err := errors.NotValidNetworkType("type", netType)
  47. exit(1)
  48. return err
  49. }
  50. // Validate if via can be set.
  51. if netVia := c.String("via"); !govalidator.IsNull(netVia) {
  52. if ovpm.NetworkTypeFromString(c.String("type")) != ovpm.ROUTE {
  53. err := errors.ConflictingDemands("--via flag can only be used with --type ROUTE")
  54. exit(1)
  55. return err
  56. }
  57. }
  58. // Validate network CIDR.
  59. if netCIDR := c.String("cidr"); !govalidator.IsCIDR(netCIDR) {
  60. err := errors.NotCIDR(netCIDR)
  61. exit(1)
  62. return err
  63. }
  64. var via *string
  65. if !govalidator.IsNull(c.String("via")) {
  66. tmp := c.String("via")
  67. via = &tmp
  68. }
  69. // If dry run, then don't call the action, just preprocess.
  70. if c.GlobalBool("dry-run") {
  71. return nil
  72. }
  73. return netDefAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("name"), c.String("cidr"), c.String("type"), via)
  74. },
  75. }
  76. var netListCommand = cli.Command{
  77. Name: "list",
  78. Aliases: []string{"l"},
  79. Usage: "List defined networks.",
  80. Action: func(c *cli.Context) error {
  81. action = "net:list"
  82. // Use default port if no port is specified.
  83. daemonPort := ovpm.DefaultDaemonPort
  84. if port := c.GlobalInt("daemon-port"); port != 0 {
  85. daemonPort = port
  86. }
  87. // If dry run, then don't call the action, just preprocess.
  88. if c.GlobalBool("dry-run") {
  89. return nil
  90. }
  91. return netListAction(fmt.Sprintf("grpc://localhost:%d", daemonPort))
  92. },
  93. }
  94. var netTypesCommand = cli.Command{
  95. Name: "types",
  96. Aliases: []string{"t"},
  97. Usage: "Show available network types.",
  98. Action: func(c *cli.Context) error {
  99. action = "net:types"
  100. // Use default port if no port is specified.
  101. daemonPort := ovpm.DefaultDaemonPort
  102. if port := c.GlobalInt("daemon-port"); port != 0 {
  103. daemonPort = port
  104. }
  105. // If dry run, then don't call the action, just preprocess.
  106. if c.GlobalBool("dry-run") {
  107. return nil
  108. }
  109. return netTypesAction(fmt.Sprintf("grpc://localhost:%d", daemonPort))
  110. },
  111. }
  112. var netUndefineCommand = cli.Command{
  113. Name: "undef",
  114. Aliases: []string{"u"},
  115. Usage: "Undefine an existing network.",
  116. Flags: []cli.Flag{
  117. cli.StringFlag{
  118. Name: "net, n",
  119. Usage: "name of the network",
  120. },
  121. },
  122. Action: func(c *cli.Context) error {
  123. action = "net:delete"
  124. // Use default port if no port is specified.
  125. daemonPort := ovpm.DefaultDaemonPort
  126. if port := c.GlobalInt("daemon-port"); port != 0 {
  127. daemonPort = port
  128. }
  129. // Validate network name.
  130. if networkName := c.String("net"); govalidator.IsNull(networkName) {
  131. err := errors.EmptyValue("net", networkName)
  132. exit(1)
  133. return err
  134. }
  135. // If dry run, then don't call the action, just preprocess.
  136. if c.GlobalBool("dry-run") {
  137. return nil
  138. }
  139. return netUndefAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"))
  140. },
  141. }
  142. var netAssociateCommand = cli.Command{
  143. Name: "assoc",
  144. Aliases: []string{"a"},
  145. Usage: "Associate a user with a network.",
  146. Flags: []cli.Flag{
  147. cli.StringFlag{
  148. Name: "net, n",
  149. Usage: "name of the network",
  150. },
  151. cli.StringFlag{
  152. Name: "user, u",
  153. Usage: "name of the user",
  154. },
  155. },
  156. Action: func(c *cli.Context) error {
  157. action = "net:associate"
  158. // Use default port if no port is specified.
  159. daemonPort := ovpm.DefaultDaemonPort
  160. if port := c.GlobalInt("daemon-port"); port != 0 {
  161. daemonPort = port
  162. }
  163. var inBulk bool
  164. // Validate username and network name.
  165. if netName := c.String("net"); govalidator.IsNull(netName) {
  166. err := errors.EmptyValue("network", netName)
  167. exit(1)
  168. return err
  169. }
  170. if username := c.String("user"); govalidator.IsNull(username) {
  171. err := errors.EmptyValue("username", username)
  172. exit(1)
  173. return err
  174. }
  175. // Mark inBulk if username is set to asterisk.
  176. if c.String("user") == "*" {
  177. inBulk = true
  178. }
  179. // If dry run, then don't call the action, just preprocess.
  180. if c.GlobalBool("dry-run") {
  181. return nil
  182. }
  183. return netAssocAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"), c.String("user"), inBulk)
  184. },
  185. }
  186. var netDissociateCommand = cli.Command{
  187. Name: "dissoc",
  188. Aliases: []string{"di"},
  189. Usage: "Dissociate a user from a network.",
  190. Flags: []cli.Flag{
  191. cli.StringFlag{
  192. Name: "net, n",
  193. Usage: "name of the network",
  194. },
  195. cli.StringFlag{
  196. Name: "user, u",
  197. Usage: "name of the user",
  198. },
  199. },
  200. Action: func(c *cli.Context) error {
  201. action = "net:dissociate"
  202. // Use default port if no port is specified.
  203. daemonPort := ovpm.DefaultDaemonPort
  204. if port := c.GlobalInt("daemon-port"); port != 0 {
  205. daemonPort = port
  206. }
  207. var inBulk bool
  208. // Validate username and network name.
  209. if netName := c.String("net"); govalidator.IsNull(netName) {
  210. err := errors.EmptyValue("network", netName)
  211. exit(1)
  212. return err
  213. }
  214. if username := c.String("user"); govalidator.IsNull(username) {
  215. err := errors.EmptyValue("username", username)
  216. exit(1)
  217. return err
  218. }
  219. // Mark inBulk if username is set to asterisk.
  220. if c.String("user") == "*" {
  221. inBulk = true
  222. }
  223. // If dry run, then don't call the action, just preprocess.
  224. if c.GlobalBool("dry-run") {
  225. return nil
  226. }
  227. return netDissocAction(fmt.Sprintf("grpc://localhost:%d", daemonPort), c.String("net"), c.String("user"), inBulk)
  228. },
  229. }
  230. func init() {
  231. app.Commands = append(app.Commands,
  232. cli.Command{
  233. Name: "net",
  234. Usage: "Network Operations",
  235. Aliases: []string{"n"},
  236. Subcommands: []cli.Command{
  237. netListCommand,
  238. netTypesCommand,
  239. netDefineCommand,
  240. netUndefineCommand,
  241. netAssociateCommand,
  242. netDissociateCommand,
  243. },
  244. },
  245. )
  246. }