create.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright © 2021 NAME HERE <EMAIL ADDRESS>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "context"
  17. "net/http"
  18. "io/ioutil"
  19. nodepb "github.com/gravitl/netmaker/grpc"
  20. "github.com/spf13/cobra"
  21. )
  22. // createCmd represents the create command
  23. var createCmd = &cobra.Command{
  24. Use: "create",
  25. Short: "Create a new node",
  26. Long: `hi`,
  27. SilenceUsage: true,
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. // Get the data from our flags
  30. nodegroup, err := cmd.Flags().GetString("nodegroup")
  31. password, err := cmd.Flags().GetString("password")
  32. macaddress, err := cmd.Flags().GetString("macaddress")
  33. name, err := cmd.Flags().GetString("name")
  34. listenport, err := cmd.Flags().GetInt32("listenport")
  35. publickey, err := cmd.Flags().GetString("publickey")
  36. endpoint, err := cmd.Flags().GetString("endpoint")
  37. if err != nil {
  38. return err
  39. }
  40. if endpoint == "" {
  41. resp, err := http.Get("https://ifconfig.me")
  42. if err != nil {
  43. return err
  44. }
  45. defer resp.Body.Close()
  46. if resp.StatusCode == http.StatusOK {
  47. bodyBytes, err := ioutil.ReadAll(resp.Body)
  48. if err != nil {
  49. return err
  50. }
  51. endpoint = string(bodyBytes)
  52. }
  53. }
  54. // Create a blog protobuffer message
  55. node := &nodepb.Node{
  56. Password: password,
  57. Macaddress: macaddress,
  58. Nodegroup: nodegroup,
  59. Listenport: listenport,
  60. Publickey: publickey,
  61. Name: name,
  62. Endpoint: endpoint,
  63. }
  64. // RPC call
  65. res, err := client.CreateNode(
  66. context.TODO(),
  67. // wrap the blog message in a CreateBlog request message
  68. &nodepb.CreateNodeReq{
  69. Node: node,
  70. },
  71. )
  72. if err != nil {
  73. return err
  74. }
  75. fmt.Printf("Node created: %s\n", res.Node.Id)
  76. return err
  77. },
  78. }
  79. func init() {
  80. createCmd.Flags().StringP("name", "n", "", "The node name")
  81. createCmd.Flags().StringP("listenport", "l", "", "The wireguard port")
  82. createCmd.Flags().StringP("endpoint", "e", "", "The public IP")
  83. createCmd.Flags().StringP("macaddress", "m", "", "The local macaddress")
  84. createCmd.Flags().StringP("password", "p", "", "The password")
  85. createCmd.Flags().StringP("nodegroup", "g", "", "The group this will be added to")
  86. createCmd.Flags().StringP("publickey", "k", "", "The wireguard public key")
  87. createCmd.MarkFlagRequired("nodegroup")
  88. createCmd.MarkFlagRequired("password")
  89. createCmd.MarkFlagRequired("macaddress")
  90. rootCmd.AddCommand(createCmd)
  91. }