Skip to content

Commit

Permalink
Add --static-file flag to create a static file
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
utsavanand2 committed Mar 17, 2020
1 parent 1744d89 commit 25f3918
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
66 changes: 65 additions & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -23,6 +26,8 @@ import (
const inletsControlPort = 8080
const inletsProControlPort = 8123

var staticFile bool

func init() {

inletsCmd.AddCommand(createCmd)
Expand All @@ -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.
Expand Down Expand Up @@ -164,7 +170,7 @@ func runCreate(cmd *cobra.Command, _ []string) error {
}

remoteTCP, _ := cmd.Flags().GetString("remote-tcp")
var pro bool
var pro bool = false
if len(remoteTCP) > 0 {
pro = true
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Provider string `yaml:"provider"`
Region string `yaml:"region,omitempty"`
Zone string `yaml:"zone,omitempty"`
}
type Host struct {
Metadata *Metadata `yaml:"metadata"`
IP string `yaml:"ip"`
ID string `yaml:"id"`
AuthToken string `yaml:"auth_token"`
Pro bool `yaml:"pro"`
}
type staticFile struct {
Host *Host `yaml:"host"`
}

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
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 25f3918

Please sign in to comment.