Skip to content

Commit

Permalink
feature(devlop): use cuisongliu/sshcmd library
Browse files Browse the repository at this point in the history
  • Loading branch information
cuisongliu committed Mar 6, 2020
1 parent 15ab293 commit 3a31321
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 405 deletions.
14 changes: 11 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@
package cmd

import (
"github.com/cuisongliu/sshcmd/pkg/sshutil"
"github.com/fanux/sealos/install"
"github.com/spf13/cobra"
)

var user, password, pkFile string

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Simplest way to init your kubernets HA cluster",
Long: `sealos init --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 --node 192.168.0.5 --user root --passwd your-server-password`,
Run: func(cmd *cobra.Command, args []string) {
install.SSHConfig = &sshutil.SSH{
User: user,
Password: password,
PkFile: pkFile,
}
c := &install.SealConfig{}
c.Dump("")
install.BuildInit()
Expand All @@ -35,9 +43,9 @@ func init() {
rootCmd.AddCommand(initCmd)

// Here you will define your flags and configuration settings.
initCmd.Flags().StringVar(&install.User, "user", "root", "servers user name for ssh")
initCmd.Flags().StringVar(&install.Passwd, "passwd", "", "password for ssh")
initCmd.Flags().StringVar(&install.PrivateKeyFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")
initCmd.Flags().StringVar(&user, "user", "root", "servers user name for ssh")
initCmd.Flags().StringVar(&password, "passwd", "", "password for ssh")
initCmd.Flags().StringVar(&pkFile, "pk", "/root/.ssh/id_rsa", "private key for ssh")

initCmd.Flags().StringVar(&install.KubeadmFile, "kubeadm-config", "", "kubeadm-config.yaml template file")

Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ module github.com/fanux/sealos
go 1.13

require (
github.com/cuisongliu/sshcmd v1.3.1
github.com/drone/drone-go v1.1.0 // indirect
github.com/fanux/sealgate v0.0.4
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/errors v0.8.1 // indirect
github.com/pkg/sftp v1.10.0
github.com/pkg/sftp v1.10.1
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.5.0
github.com/wonderivan/logger v1.0.0
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
gopkg.in/yaml.v2 v2.2.4
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cuisongliu/sshcmd v1.3.1 h1:+hSaWKQ9clEH2Z/5fy0fzyaJBcxSprYYE3EksPys1C0=
github.com/cuisongliu/sshcmd v1.3.1/go.mod h1:jAEBZhFW5kNey9B+59lR8mfQaCQsyp8b8QaI7RhESxE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -100,6 +102,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.0 h1:DGA1KlA9esU6WcicH+P8PxFZOl15O6GYtab1cIJdOlE=
github.com/pkg/sftp v1.10.0/go.mod h1:NxmoDg/QLVWluQDUYG7XBZTLUpKeFa8e3aMf1BfjyHk=
github.com/pkg/sftp v1.10.1 h1:VasscCm72135zRysgrJDKsntdmPN+OuU3+nnHYA9wyc=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -139,6 +143,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand All @@ -157,6 +162,8 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 h1:7KByu05hhLed2MO29w7p1XfZvZ13m8mub3shuVftRs0=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
4 changes: 2 additions & 2 deletions install/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func (s *SealosInstaller) CheckValid() {
logger.Error("hosts not allow empty")
os.Exit(1)
}
if User == "" {
if SSHConfig.User == "" {
s.Print("Fail")
logger.Error("user not allow empty")
os.Exit(1)
}
var session *ssh.Session
var errors []error
for _, h := range s.Hosts {
session, err := Connect(User, Passwd, PrivateKeyFile, h)
session, err := SSHConfig.Connect(h)
if err != nil {
logger.Error("[%s] ------------ check error", h)
logger.Error("[%s] ------------ error[%s]", h, err)
Expand Down
18 changes: 9 additions & 9 deletions install/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func BuildClean(beforeNodes []string) {
// 所有node节点
nodes := ParseIPs(NodeIPs)
hosts = append(masters, nodes...)
}else {
} else {
hosts = beforeNodes
}
i.Hosts = hosts
Expand All @@ -38,19 +38,19 @@ func (s *SealosInstaller) Clean() {

func clean(host string) {
cmd := "kubeadm reset -f && modprobe -r ipip && lsmod"
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = "rm -rf ~/.kube/ && rm -rf /etc/kubernetes/"
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = "rm -rf /etc/systemd/system/kubelet.service.d && rm -rf /etc/systemd/system/kubelet.service"
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = "rm -rf /usr/bin/kube* && rm -rf /usr/bin/crictl"
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = "rm -rf /etc/cni && rm -rf /opt/cni"
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = "rm -rf /var/lib/etcd && rm -rf /var/etcd"
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = fmt.Sprintf("sed -i \"/%s/d\" /etc/hosts ", ApiServer)
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
cmd = fmt.Sprint("rm -rf ~/kube")
Cmd(host, cmd)
SSHConfig.Cmd(host, cmd)
}
2 changes: 0 additions & 2 deletions install/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ func CloudInstall(c *Cluster) {
}
c.Nodes = res.VMs

Passwd = c.Passwd

//TODO wget package on master0 and scp to other nodes
logger.Info("wait few minute for download offline package on master0...")
cmd := newWgetCommand(c)
Expand Down
32 changes: 18 additions & 14 deletions install/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package install

import (
"github.com/cuisongliu/sshcmd/pkg/sshutil"
"github.com/wonderivan/logger"
"gopkg.in/yaml.v2"
"io/ioutil"
Expand All @@ -21,23 +22,23 @@ type SealConfig struct {
VIP string
PkgURL string
Version string
Repo string
Repo string
PodCIDR string
SvcCIDR string
}

//Dump is
func (c *SealConfig) Dump(path string) {
home,_ :=os.UserHomeDir()
home, _ := os.UserHomeDir()
if path == "" {
path = home + defaultConfigPath + defaultConfigFile
}

c.Masters = ParseIPs(MasterIPs)
c.Nodes = ParseIPs(NodeIPs)
c.User = User
c.Passwd = Passwd
c.PrivateKey = PrivateKeyFile
c.User = SSHConfig.User
c.Passwd = SSHConfig.Password
c.PrivateKey = SSHConfig.PkFile
c.ApiServerDomian = ApiServer
c.VIP = VIP
c.PkgURL = PkgUrl
Expand All @@ -51,24 +52,24 @@ func (c *SealConfig) Dump(path string) {
logger.Error("dump config file failed: %s", err)
}

err = os.MkdirAll(home + defaultConfigPath,os.ModePerm)
err = os.MkdirAll(home+defaultConfigPath, os.ModePerm)
if err != nil {
logger.Warn("create default sealos config dir failed, please create it by your self mkdir -p /root/.sealos && touch /root/.sealos/config.yaml")
}

ioutil.WriteFile(path, y, 0644)
}

func Dump(path string, content interface{}) error{
func Dump(path string, content interface{}) error {
y, err := yaml.Marshal(content)
if err != nil {
logger.Error("dump config file failed: %s", err)
return err
}
home,_ :=os.UserHomeDir()
err = os.MkdirAll(home + defaultConfigPath,os.ModePerm)
home, _ := os.UserHomeDir()
err = os.MkdirAll(home+defaultConfigPath, os.ModePerm)
if err != nil {
logger.Error("create dump dir failed %s",err)
logger.Error("create dump dir failed %s", err)
return err
}

Expand All @@ -79,7 +80,7 @@ func Dump(path string, content interface{}) error{
//Load is
func (c *SealConfig) Load(path string) {
if path == "" {
home,_ :=os.UserHomeDir()
home, _ := os.UserHomeDir()
path = home + defaultConfigPath + defaultConfigFile
}

Expand All @@ -97,9 +98,12 @@ func (c *SealConfig) Load(path string) {

MasterIPs = c.Masters
NodeIPs = c.Nodes
User = c.User
Passwd = c.Passwd
PrivateKeyFile = c.PrivateKey
if SSHConfig == nil {
SSHConfig = &sshutil.SSH{}
}
SSHConfig.User = c.User
SSHConfig.Password = c.Passwd
SSHConfig.PkFile = c.PrivateKey
ApiServer = c.ApiServerDomian
VIP = c.VIP
PkgUrl = c.PkgURL
Expand Down
13 changes: 9 additions & 4 deletions install/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package install

import "testing"
import (
"github.com/cuisongliu/sshcmd/pkg/sshutil"
"testing"
)

func Test_generatorKubeadmConfig(t *testing.T) {
kubeadmConfig()
Expand All @@ -9,10 +12,12 @@ func Test_generatorKubeadmConfig(t *testing.T) {
func TestTemplate(t *testing.T) {
var masters = []string{"172.20.241.205", "172.20.241.206", "172.20.241.207"}
var vip = "10.103.97.1"
User = "cuisongliu"
Passwd = "admin"
config := sshutil.SSH{
User: "cuisongliu",
Password: "admin",
}
MasterIPs = masters
VIP = vip
Cmd("127.0.0.1", "echo \""+string(Template())+"\" > ~/aa")
config.Cmd("127.0.0.1", "echo \""+string(Template())+"\" > ~/aa")
t.Log(string(Template()))
}
16 changes: 8 additions & 8 deletions install/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func BuildInit() {
nodes := ParseIPs(NodeIPs)
hosts := append(masters, nodes...)
i := &SealosInstaller{
Hosts: hosts,
Hosts: hosts,
Masters: masters,
Nodes: nodes,
Nodes: nodes,
}
i.CheckValid()
i.Print()
Expand Down Expand Up @@ -58,33 +58,33 @@ func (s *SealosInstaller) KubeadmConfigInstall() {
templateData = string(TemplateFromTemplateContent(string(fileData)))
}
cmd := "echo \"" + templateData + "\" > /root/kubeadm-config.yaml"
Cmd(s.Masters[0], cmd)
SSHConfig.Cmd(s.Masters[0], cmd)
}

//InstallMaster0 is
func (s *SealosInstaller) InstallMaster0() {
cmd := fmt.Sprintf("echo %s %s >> /etc/hosts", IpFormat(s.Masters[0]), ApiServer)
Cmd(s.Masters[0], cmd)
SSHConfig.Cmd(s.Masters[0], cmd)

cmd = s.Command(Version, InitMaster)

output := Cmd(s.Masters[0], cmd)
output := SSHConfig.Cmd(s.Masters[0], cmd)
if output == nil {
logger.Error("[%s]kubernetes install is error.please clean and uninstall.", s.Masters[0])
os.Exit(1)
}
decodeOutput(output)

cmd = `mkdir -p /root/.kube && cp /etc/kubernetes/admin.conf /root/.kube/config`
output = Cmd(s.Masters[0], cmd)
output = SSHConfig.Cmd(s.Masters[0], cmd)

if WithoutCNI {
logger.Info("--without-cni is true, so we not install calico or flannel, install it by yourself")
return
}
//cmd = `kubectl apply -f /root/kube/conf/net/calico.yaml || true`
netyaml := net.NewNetwork(Network, net.MetaData{Interface:Interface,CIDR:PodCIDR}).Manifests("")
netyaml := net.NewNetwork(Network, net.MetaData{Interface: Interface, CIDR: PodCIDR}).Manifests("")
logger.Info("calico yaml is : \n", netyaml)
cmd = fmt.Sprintf(`echo '%s' | kubectl apply -f -`, netyaml)
output = Cmd(s.Masters[0], cmd)
output = SSHConfig.Cmd(s.Masters[0], cmd)
}
4 changes: 2 additions & 2 deletions install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func AppInstall(url string) {
Exec(pkgConfig, *c)
}

func LoadRemoteFile(url string) string{
func LoadRemoteFile(url string) string {
isHttp := strings.HasPrefix(url, "http")
if !isHttp {
logger.Info("using local package %s", url)
Expand Down Expand Up @@ -220,5 +220,5 @@ func (r *RunOnMaster) Run(config SealConfig, url, pkgName string) {

func CmdWorkSpace(node, cmd, workdir string) {
command := fmt.Sprintf("cd %s && %s", workdir, cmd)
Cmd(node, command)
SSHConfig.Cmd(node, command)
}
Loading

0 comments on commit 3a31321

Please sign in to comment.