mwdd_redis.go 3.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*Package cmd is used for command line.

Copyright © 2020 Addshore

This program 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.

This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd

import (
	"github.com/spf13/cobra"
22
23
	"gitlab.wikimedia.org/releng/cli/internal/exec"
	"gitlab.wikimedia.org/releng/cli/internal/mwdd"
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
)

var mwddRedisCmd = &cobra.Command{
	Use:   "redis",
	Short: "Redis service",
	RunE:  nil,
}

var mwddRedisCreateCmd = &cobra.Command{
	Use:   "create",
	Short: "Create a Redis container",
	Run: func(cmd *cobra.Command, args []string) {
		mwdd.DefaultForUser().EnsureReady()
		mwdd.DefaultForUser().UpDetached(
			[]string{"redis"},
			exec.HandlerOptions{
Addshore's avatar
Addshore committed
40
				Verbosity: Verbosity,
41
42
43
44
45
46
47
48
49
50
51
			},
		)
	},
}

var mwddRedisDestroyCmd = &cobra.Command{
	Use:   "destroy",
	Short: "Destroy the Redis container and volumes",
	Run: func(cmd *cobra.Command, args []string) {
		mwdd.DefaultForUser().EnsureReady()
		options := exec.HandlerOptions{
Addshore's avatar
Addshore committed
52
			Verbosity: Verbosity,
53
		}
Addshore's avatar
Addshore committed
54
55
		mwdd.DefaultForUser().Rm([]string{"redis"}, options)
		mwdd.DefaultForUser().RmVolumes([]string{"redis-data"}, options)
56
57
58
59
60
61
62
63
64
	},
}

var mwddRedisSuspendCmd = &cobra.Command{
	Use:   "suspend",
	Short: "Suspend the Redis container",
	Run: func(cmd *cobra.Command, args []string) {
		mwdd.DefaultForUser().EnsureReady()
		options := exec.HandlerOptions{
Addshore's avatar
Addshore committed
65
			Verbosity: Verbosity,
66
		}
Addshore's avatar
Addshore committed
67
		mwdd.DefaultForUser().Stop([]string{"redis"}, options)
68
69
70
71
72
73
74
75
76
	},
}

var mwddRedisResumeCmd = &cobra.Command{
	Use:   "resume",
	Short: "Resume the Redis container",
	Run: func(cmd *cobra.Command, args []string) {
		mwdd.DefaultForUser().EnsureReady()
		options := exec.HandlerOptions{
Addshore's avatar
Addshore committed
77
			Verbosity: Verbosity,
78
		}
Addshore's avatar
Addshore committed
79
		mwdd.DefaultForUser().Start([]string{"redis"}, options)
80
81
82
	},
}

83
var mwddRedisExecCmd = &cobra.Command{
Addshore's avatar
Addshore committed
84
85
86
	Use:     "exec [flags] [command...]",
	Example: "  exec bash\n  exec -- bash --help\n  exec --user root bash\n  exec --user root -- bash --help",
	Short:   "Executes a command in the Redis container",
87
88
89
90
	Run: func(cmd *cobra.Command, args []string) {
		mwdd.DefaultForUser().EnsureReady()
		mwdd.DefaultForUser().DockerExec(mwdd.DockerExecCommand{
			DockerComposeService: "redis",
Addshore's avatar
Addshore committed
91
92
			Command:              args,
			User:                 User,
93
94
95
96
		})
	},
}

97
98
99
100
101
102
103
var mwddRedisCliCmd = &cobra.Command{
	Use:   "cli",
	Short: "Redis CLI for the container",
	Run: func(cmd *cobra.Command, args []string) {
		mwdd.DefaultForUser().EnsureReady()
		mwdd.DefaultForUser().DockerExec(mwdd.DockerExecCommand{
			DockerComposeService: "redis",
Addshore's avatar
Addshore committed
104
			Command:              []string{"redis-cli"},
105
106
107
108
109
110
111
112
113
114
		})
	},
}

func init() {
	mwddCmd.AddCommand(mwddRedisCmd)
	mwddRedisCmd.AddCommand(mwddRedisCreateCmd)
	mwddRedisCmd.AddCommand(mwddRedisDestroyCmd)
	mwddRedisCmd.AddCommand(mwddRedisSuspendCmd)
	mwddRedisCmd.AddCommand(mwddRedisResumeCmd)
115
116
	mwddRedisCmd.AddCommand(mwddRedisExecCmd)
	mwddRedisExecCmd.Flags().StringVarP(&User, "user", "u", mwdd.UserAndGroupForDockerExecution(), "User to run as, defaults to current OS user uid:gid")
117
118
	mwddRedisCmd.AddCommand(mwddRedisCliCmd)
}