Skip to content

Commit

Permalink
Retrieve kubeconfig file
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Aug 15, 2019
1 parent 0375e12 commit 0b7c634
Show file tree
Hide file tree
Showing 12 changed files with 737 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
k3sup
bin/**
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Version := $(shell git describe --tags --dirty)
GitCommit := $(shell git rev-parse HEAD)
LDFLAGS := "-s -w -X main.Version=$(Version) -X main.GitCommit=$(GitCommit)"
PLATFORM := $(shell ./hack/platform-tag.sh)

.PHONY: all

.PHONY: dist
dist:
mkdir -p bin
CGO_ENABLED=0 GOOS=linux go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/k3sup
CGO_ENABLED=0 GOOS=darwin go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/k3sup-darwin
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=6 go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/k3sup-armhf
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags $(LDFLAGS) -a -installsuffix cgo -o bin/k3sup-arm64
19 changes: 19 additions & 0 deletions hack/platform-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

getPackage() {
suffix=""
arch=$(uname -m)
case $arch in
"aarch64")
suffix="-arm64"
;;
esac
case $arch in
"armv6l" | "armv7l")
suffix="-armhf"
;;
esac
}

getPackage
echo ${suffix}
125 changes: 119 additions & 6 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package cmd

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
)
Expand All @@ -23,15 +28,18 @@ func MakeInstall() *cobra.Command {
command.Flags().String("user", "root", "Username for SSH login")

command.Flags().String("ssh-key", "~/.ssh/id_rsa", "The ssh key to use for remote login")
command.Flags().Int("ssh-port", 22, "The port on which to connect for ssh")
command.Flags().Bool("skip-install", false, "Skip the k3s installer")
command.Flags().String("local-path", "kubeconfig", "Local path to save the kubeconfig file")

command.Run = func(command *cobra.Command, args []string) {
command.RunE = func(command *cobra.Command, args []string) error {

localKubeconfig, _ := command.Flags().GetString("local-path")

skipInstall, _ := command.Flags().GetBool("skip-install")

port, _ := command.Flags().GetInt("ssh-port")

ip, _ := command.Flags().GetIP("ip")
fmt.Println("Public SAN: " + ip.String())

Expand All @@ -40,36 +48,70 @@ func MakeInstall() *cobra.Command {
sshKeyPath := expandPath(sshKey)
fmt.Printf("ssh -i %s %s@%s\n", sshKeyPath, user, ip.String())

clientConfig := ssh.ClientConfig{
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
loadPublickey(sshKeyPath),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
fmt.Println(clientConfig)

address := fmt.Sprintf("%s:%d", ip.String(), port)
operator, err := NewSSHOperator(address, config)

if err != nil {
return errors.Wrapf(err, "unable to connect to %s over ssh", address)
}
defer operator.Close()

if !skipInstall {
installK3scommand := fmt.Sprintf("curl -sLS https://get.k3s.io | INSTALL_K3S_EXEC='server --tls-san %s' sh -\n", ip)
fmt.Printf("ssh: %s\n", installK3scommand)
res, err := operator.Execute(installK3scommand)

if err != nil {
return fmt.Errorf("Error received processing command: %s", err)
}

fmt.Printf("Result: %s %s\n", string(res.StdOut), string(res.StdErr))
}

getConfigcommand := fmt.Sprintf("sudo cat /etc/rancher/k3s/k3s.yaml\n")
fmt.Printf("ssh: %s\n", getConfigcommand)

res, err := operator.Execute(getConfigcommand)

if err != nil {
return fmt.Errorf("Error received processing command: %s", err)
}

fmt.Printf("Result: %s %s\n", string(res.StdOut), string(res.StdErr))

absPath, _ := filepath.Abs(localKubeconfig)
fmt.Printf("Saving file to: %s\n", absPath)

kubeconfig := strings.Replace(string(res.StdOut), "localhost", ip.String(), -1)

fmt.Println(res)

writeErr := ioutil.WriteFile(absPath, []byte(kubeconfig), 0600)
if writeErr != nil {
return writeErr
}

return nil
}

command.PreRunE = func(command *cobra.Command, args []string) error {
// if val, _ := command.Flags().getip .GetString("ip"); len(val) == 0 {
// return fmt.Errorf(`give --ip or install --help`)
// }
_, ipErr := command.Flags().GetIP("ip")
if ipErr != nil {
return ipErr
}

_, sshPortErr := command.Flags().GetInt("ssh-port")
if sshPortErr != nil {
return sshPortErr
}
return nil
}
return command
Expand All @@ -92,3 +134,74 @@ func loadPublickey(path string) ssh.AuthMethod {
}
return ssh.PublicKeys(signer)
}

type commandRes struct {
StdOut []byte
StdErr []byte
}

func executeCommand(cmd string) (commandRes, error) {

return commandRes{}, nil
}

type SSHOperator struct {
conn *ssh.Client
}

func (s *SSHOperator) Close() error {

return s.conn.Close()
}

func NewSSHOperator(address string, config *ssh.ClientConfig) (*SSHOperator, error) {
conn, err := ssh.Dial("tcp", address, config)
if err != nil {
return nil, err
}

operator := SSHOperator{
conn: conn,
}

return &operator, nil
}

func (s *SSHOperator) Execute(command string) (commandRes, error) {

sess, err := s.conn.NewSession()
if err != nil {
return commandRes{}, err
}

defer sess.Close()

sessStdOut, err := sess.StdoutPipe()
if err != nil {
return commandRes{}, err
}

output := bytes.Buffer{}

stdOutWriter := io.MultiWriter(os.Stdout, &output)
go io.Copy(stdOutWriter, sessStdOut)

sessStderr, err := sess.StderrPipe()
if err != nil {
return commandRes{}, err
}

errorOutput := bytes.Buffer{}
stdErrWriter := io.MultiWriter(os.Stderr, &errorOutput)
go io.Copy(stdErrWriter, sessStderr)

err = sess.Run(command)
if err != nil {
return commandRes{}, err
}

return commandRes{
StdErr: errorOutput.Bytes(),
StdOut: output.Bytes(),
}, nil
}
24 changes: 24 additions & 0 deletions vendor/github.com/pkg/errors/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/pkg/errors/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/pkg/errors/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions vendor/github.com/pkg/errors/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions vendor/github.com/pkg/errors/appveyor.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0b7c634

Please sign in to comment.