-
Notifications
You must be signed in to change notification settings - Fork 79
/
service.go
135 lines (112 loc) · 3.22 KB
/
service.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cmd
import (
"fmt"
"strconv"
"github.com/fatih/color"
"github.com/luizalabs/teresa/pkg/client"
"github.com/luizalabs/teresa/pkg/client/connection"
svcpb "github.com/luizalabs/teresa/pkg/protobuf/service"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
var serviceCmd = &cobra.Command{
Use: "service",
Short: "Everything about services",
}
var serviceEnableSSLCmd = &cobra.Command{
Use: "enable-ssl",
Short: "Enable ssl for the app",
Long: `Enable SSL for the app.
To enable ssl for the app on aws:
$ teresa service enable-ssl --app myapp --cert arn:aws:iam::xxxxx:server-certificate/cert-name
To only support ssl:
$ teresa service enable-ssl --app myapp --cert arn:aws:iam::xxxxx:server-certificate/cert-name --only`,
Run: serviceEnableSSL,
}
var serviceInfoCmd = &cobra.Command{
Use: "info <name>",
Short: "Show service info",
Long: `Show service info such as ports and ssl configuration.`,
Run: serviceInfo,
}
func serviceEnableSSL(cmd *cobra.Command, args []string) {
if len(args) != 0 {
cmd.Usage()
return
}
appName, err := cmd.Flags().GetString("app")
if err != nil || appName == "" {
client.PrintErrorAndExit("Invalid app parameter")
}
cert, err := cmd.Flags().GetString("cert")
if err != nil || cert == "" {
client.PrintErrorAndExit("Invalid cert parameter")
}
only, err := cmd.Flags().GetBool("only")
if err != nil {
client.PrintErrorAndExit("Invalid only parameter")
}
conn, err := connection.New(cfgFile, cfgCluster)
if err != nil {
client.PrintErrorAndExit("Error connecting to server: %s", err)
}
defer conn.Close()
cli := svcpb.NewServiceClient(conn)
req := &svcpb.EnableSSLRequest{
AppName: appName,
Cert: cert,
Only: only,
}
if _, err := cli.EnableSSL(context.Background(), req); err != nil {
client.PrintErrorAndExit(client.GetErrorMsg(err))
}
fmt.Println("SSL enabled with success")
}
func serviceInfo(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Usage()
return
}
appName := args[0]
conn, err := connection.New(cfgFile, cfgCluster)
if err != nil {
client.PrintErrorAndExit("Error connecting to server: %v", err)
}
defer conn.Close()
cli := svcpb.NewServiceClient(conn)
info, err := cli.Info(context.Background(), &svcpb.InfoRequest{AppName: appName})
if err != nil {
client.PrintErrorAndExit(client.GetErrorMsg(err))
}
color.New(color.FgCyan, color.Bold).Printf("[%s]\n", appName)
bold := color.New(color.Bold).SprintFunc()
if len(info.ServicePorts) > 0 {
fmt.Println(bold("ports:"))
for _, port := range info.ServicePorts {
fmt.Printf(" %d\n", port.Port)
}
}
if info.Ssl != nil {
fmt.Println(bold("ssl:"))
cert := info.Ssl.Cert
if cert == "" {
cert = "n/a"
}
fmt.Printf(" cert: %s\n", cert)
if info.Ssl.ServicePort != nil {
port := strconv.Itoa(int(info.Ssl.ServicePort.Port))
if port == "0" {
port = "n/a"
}
fmt.Printf(" port: %s\n", port)
}
}
}
func init() {
RootCmd.AddCommand(serviceCmd)
serviceCmd.AddCommand(serviceEnableSSLCmd)
serviceCmd.AddCommand(serviceInfoCmd)
serviceEnableSSLCmd.Flags().String("app", "", "app name")
serviceEnableSSLCmd.Flags().String("cert", "", "certificate identifier")
serviceEnableSSLCmd.Flags().Bool("only", false, "only use SSL")
}