Skip to content

Commit

Permalink
pins the docker-ce repro and adds cluster ls, cluster delete
Browse files Browse the repository at this point in the history
fix #2
  • Loading branch information
David Steiman committed Jan 26, 2018
1 parent 3512687 commit 880109e
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (cluster *Cluster) CreateWorkerNodes(template Node, count int) error {
func (cluster *Cluster) ProvisionNodes() error {
for _, node := range cluster.Nodes {
log.Printf("installing docker.io and kubeadm on node '%s'...", node.Name)
_, err := runCmd(node, "wget -cO- https://gist.githubusercontent.com/xetys/0ecfa01790debb2345c0883418dcc7c4/raw/403b6cdea6b78bc5b7209acfa3dfa810dd5f89ba/ubuntu16-kubeadm | bash -")
_, err := runCmd(node, "wget -cO- https://raw.githubusercontent.com/xetys/hetzner-kube/master/install-docker-kubeadm.sh | bash -")

if err != nil {
return err
Expand Down
102 changes: 102 additions & 0 deletions cmd/clusterDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
"errors"
"log"
)

// clusterDeleteCmd represents the clusterDelete command
var clusterDeleteCmd = &cobra.Command{
Use: "delete",
Short: "removes a cluster and deletes the associated nodes",
PreRunE: func(cmd *cobra.Command, args []string) error {

name, err := cmd.Flags().GetString("name")
if err != nil {
return nil
}

if name == "" {
return errors.New("flag --name is required")
}

idx, _ := AppConf.Config.FindClusterByName(name)

if idx == -1 {
return errors.New(fmt.Sprintf("cluster '%s' not found", name))
}

if err != nil {
return err
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {

name, _ := cmd.Flags().GetString("name")
_, cluster := AppConf.Config.FindClusterByName(name)
// first kill all nodes

for _, node := range cluster.Nodes {
server, _, err := AppConf.Client.Server.Get(AppConf.Context, node.Name)

if err != nil {
log.Fatal(err)
}

if server != nil {
_, err = AppConf.Client.Server.Delete(AppConf.Context, server)

if err != nil {
log.Fatal(err)
}

log.Printf("server '%s' deleted", node.Name)
} else {
log.Printf("server '%s' was already deleted", node.Name)
}
}

// now remove the cluster from list
if err := AppConf.Config.DeleteCluster(name); err != nil {
log.Fatal(err)
}

AppConf.Config.WriteCurrentConfig()

log.Printf("cluster '%s' deleted", name)
},
}

func init() {
clusterCmd.AddCommand(clusterDeleteCmd)
clusterDeleteCmd.Flags().StringP("name", "n", "", "Name of the cluster to delete")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// clusterDeleteCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// clusterDeleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
64 changes: 64 additions & 0 deletions cmd/clusterList.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
"text/tabwriter"
"os"
)

// clusterListCmd represents the clusterList command
var clusterListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "lists all created clusters",
Run: func(cmd *cobra.Command, args []string) {
tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintln(tw, "NAME\tNODES\tMASTER IP")

for _, cluster := range AppConf.Config.Clusters {
nodes := len(cluster.Nodes)
var masterIp string
for _, node := range cluster.Nodes {
if node.IsMaster {
masterIp = node.IPAddress
break
}
}
fmt.Fprintf(tw, "%s\t%d\t%s", cluster.Name, nodes, masterIp)
fmt.Fprintln(tw)
}

tw.Flush()
},
}

func init() {
clusterCmd.AddCommand(clusterListCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// clusterListCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// clusterListCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
25 changes: 25 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (config *HetznerConfig) DeleteSSHKey(name string) error {

return nil
}

func (config *HetznerConfig) FindSSHKeyByName(name string) (int, *SSHKey) {
index := -1
for i, v := range config.SSHKeys {
Expand All @@ -76,6 +77,30 @@ func (config *HetznerConfig) AddCluster(cluster Cluster) {
config.Clusters = append(config.Clusters, cluster)
}


func (config *HetznerConfig) DeleteCluster(name string) error {

index, _ := config.FindClusterByName(name)

if index == -1 {
return errors.New("cluster not found")
}

config.Clusters = append(config.Clusters[:index], config.Clusters[index+1:]...)

return nil
}

func (config *HetznerConfig) FindClusterByName(name string) (int, *Cluster) {
for i, cluster := range config.Clusters {
if cluster.Name == name {
return i, &cluster
}
}

return -1, nil
}

func (app *AppConfig) SwitchContextByName(name string) error {
ctx, err := app.FindContextByName(name)

Expand Down

0 comments on commit 880109e

Please sign in to comment.