From 4397695694703b0bf3857cf878dbbba3048c62f3 Mon Sep 17 00:00:00 2001 From: Utsav Anand Date: Tue, 17 Mar 2020 19:28:25 +0530 Subject: [PATCH] Add --static-file flag to create a static file The PR adds feature to create a static file with the machine ID, auth, IP and other metadata when the `--static-file` flag is passed Closes #28 Signed-off-by: Utsav Anand --- cmd/create.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++- go.sum | 2 ++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/cmd/create.go b/cmd/create.go index c53382f3..888fe4c2 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -6,10 +6,13 @@ package cmd import ( "encoding/base64" "fmt" + "io/ioutil" "strconv" "strings" "time" + "gopkg.in/yaml.v2" + "github.com/inlets/inletsctl/pkg/env" "github.com/inlets/inletsctl/pkg/names" @@ -23,6 +26,8 @@ import ( const inletsControlPort = 8080 const inletsProControlPort = 8123 +var staticFile bool + func init() { inletsCmd.AddCommand(createCmd) @@ -44,6 +49,7 @@ func init() { createCmd.Flags().StringP("remote-tcp", "c", "", `Remote host for inlets-pro to use for forwarding TCP connections`) createCmd.Flags().DurationP("poll", "n", time.Second*2, "poll every N seconds, use a higher value if you encounter rate-limiting") + createCmd.Flags().BoolVarP(&staticFile, "static-file", "", false, "Create a static file for the provisioned machine") } // clientCmd represents the client sub command. @@ -204,6 +210,18 @@ func runCreate(cmd *cobra.Command, _ []string) error { i+1, max, hostStatus.ID, hostStatus.Status) if hostStatus.Status == "active" { + + // Creaye a static file named host-name-inletsctl.yaml + staticFile, _ := cmd.Flags().GetBool("static-file") + + // If user has passed a fileName to the `-f` flag + if staticFile { + err := createStaticFile(provider, hostReq, hostStatus, inletsToken, pro) + if err != nil { + return err + } + } + if !pro { fmt.Printf(`Inlets OSS exit-node summary: IP: %s @@ -397,3 +415,49 @@ curl -sLO https://raw.githubusercontent.com/inlets/inlets/master/hack/inlets-pro systemctl start inlets-pro && \ systemctl enable inlets-pro` } + +func createStaticFile(provider string, reqHost *provision.BasicHost, provisionedHost *provision.ProvisionedHost, inletsToken string, pro bool) error { + type Metadata struct { + CreatedAt string `yaml:"created_at,omitempty"` + Provider string `yaml:"provider,omitempty"` + Region string `yaml:"region,omitempty"` + Zone string `yaml:"zone,omitempty"` + } + type Host struct { + Metadata *Metadata `yaml:"metadata,omitempty"` + IP string `yaml:"ip,omitempty"` + ID string `yaml:"id,omitempty"` + AuthToken string `yaml:"auth_token,omitempty"` + pro bool `yaml:"pro,omitempty"` + } + type staticFile struct { + Host *Host `yaml:"host,omitempty"` + } + + fileStruct := &staticFile{ + Host: &Host{ + Metadata: &Metadata{ + CreatedAt: time.Now().String(), + Provider: provider, + Region: reqHost.Region, + Zone: reqHost.Additional["zone"], + }, + IP: provisionedHost.IP, + ID: provisionedHost.ID, + AuthToken: inletsToken, + pro: pro, + }, + } + + out, err := yaml.Marshal(fileStruct) + if err != nil { + return fmt.Errorf("cannot marshal struct: %v", err) + } + + fileName := fmt.Sprintf("%s-inletsctl.yaml", provisionedHost.ID) + err = ioutil.WriteFile(fileName, out, 0666) + if err != nil { + return fmt.Errorf("cannot write to file: %s Error: %v", fileName, err) + } + return nil +} diff --git a/go.mod b/go.mod index 015f8419..b1e7d3f0 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/alexellis/go-execute v0.0.0-20191029181357-d17947259f74 github.com/aws/aws-sdk-go v1.26.8 github.com/digitalocean/godo v1.27.0 + github.com/go-yaml/yaml v2.1.0+incompatible github.com/golang/groupcache v0.0.0-20191027212112-611e8accdfc9 // indirect github.com/google/uuid v1.1.1 github.com/morikuni/aec v1.0.0 @@ -29,5 +30,5 @@ require ( google.golang.org/appengine v1.6.5 // indirect google.golang.org/genproto v0.0.0-20191115221424-83cc0476cb11 // indirect google.golang.org/grpc v1.25.1 // indirect - gopkg.in/yaml.v2 v2.2.7 // indirect + gopkg.in/yaml.v2 v2.2.7 ) diff --git a/go.sum b/go.sum index 29593b28..63538e15 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= +github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= +github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=