/
provision.go
79 lines (65 loc) · 2.06 KB
/
provision.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2019 Grendel Authors. All rights reserved.
//
// This file is part of Grendel.
//
// Grendel is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Grendel is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Grendel. If not, see <https://www.gnu.org/licenses/>.
package host
import (
"context"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/ubccr/grendel/cmd"
)
var (
provisionCmd = &cobra.Command{
Use: "provision",
Short: "Set hosts to provision",
Long: `Set hosts to provision`,
Args: cobra.MinimumNArgs(0),
RunE: func(command *cobra.Command, args []string) error {
if len(args) == 0 && len(tags) == 0 {
return fmt.Errorf("Please provide tags (--tags) or a nodeset")
}
if len(args) > 0 && len(tags) > 0 {
log.Warn("Using both tags (--tags) and a nodeset is not supported yet. Only nodeset is used.")
}
gc, err := cmd.NewClient()
if err != nil {
return err
}
nodes := strings.Join(args, ",")
if len(tags) > 0 && len(args) == 0 {
hostList, _, err := gc.HostApi.HostTags(context.Background(), strings.Join(tags, ","))
if err != nil {
return cmd.NewApiError("Failed to find hosts by tag", err)
}
ns, err := hostList.ToNodeSet()
if err != nil {
return cmd.NewApiError("Failed to create nodeset from host list", err)
}
nodes = ns.String()
}
_, err = gc.HostApi.HostProvision(context.Background(), nodes)
if err != nil {
return cmd.NewApiError("Failed to set hosts to provision", err)
}
cmd.Log.Info("Successfully set hosts to provision")
return nil
},
}
)
func init() {
hostCmd.AddCommand(provisionCmd)
}