forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services-start.go
172 lines (140 loc) · 4.36 KB
/
services-start.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package cmd
import (
"context"
"os"
"regexp"
"sync"
"github.com/spf13/cobra"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/registry"
)
var (
wg sync.WaitGroup
FilterStartTags []string
FilterStartExclude []string
)
type serviceContext struct {
ctx context.Context
cancel context.CancelFunc
}
// StartCmd represents the start command
var StartCmd = &cobra.Command{
Use: "start",
Short: "Start Cells services",
Long: `Start one or more services on this machine
### Syntax
$ ` + os.Args[0] + ` start [flags] args...
Additional arguments are regexp that can match any of the service names available (see 'list' command).
The -t/--tags flag may limit to only a certain category of services, use lowercase like broker, idm, data, etc...
The -x/--exclude flag may exclude one or more services
Both flags may be used in conjunction with the regexp arguments.
### Examples
Start only services starting with grpc
$ ` + os.Args[0] + ` start pydio.grpc
Start only services for scheduler
$ ` + os.Args[0] + ` start --tag=scheduler
Start whole plateform except the roles service
$ ` + os.Args[0] + ` start --exclude=pydio.grpc.idm.roles
`,
PreRun: func(cmd *cobra.Command, args []string) {
// Removing install services
registry.Default.Filter(func(s registry.Service) bool {
re := regexp.MustCompile(common.SERVICE_INSTALL)
if re.MatchString(s.Name()) {
return true
}
return false
})
// Filtering services by tags
registry.Default.Filter(func(s registry.Service) bool {
for _, t := range FilterStartTags {
for _, st := range s.Tags() {
if t == st {
registry.ProcessStartTags = append(registry.ProcessStartTags, "t:"+t)
return false
}
}
}
return len(FilterStartTags) > 0
})
// Filtering services by args
registry.Default.Filter(func(s registry.Service) bool {
for _, arg := range args {
reArg := regexp.MustCompile(arg)
if reArg.MatchString(s.Name()) {
registry.ProcessStartTags = append(registry.ProcessStartTags, "s:"+s.Name())
return false
}
if s.MatchesRegexp(arg) {
registry.ProcessStartTags = append(registry.ProcessStartTags, "s:"+s.Name())
return false
}
}
return len(args) > 0
})
// Filtering services that have a regexp when there is no argument to the command
registry.Default.Filter(func(s registry.Service) bool {
if len(args) == 0 && s.Regexp() != nil {
return true
}
return false
})
// Re-gather exclude flag (it is applied in root.go PersistentPreRun) for startTag
for _, x := range FilterStartExclude {
registry.ProcessStartTags = append(registry.ProcessStartTags, "x:"+x)
}
// Re-building allServices list
if s, err := registry.Default.ListServices(); err != nil {
cmd.Print("Could not retrieve list of services")
os.Exit(0)
} else {
allServices = s
}
},
Run: func(cmd *cobra.Command, args []string) {
//var err errors
if !IsFork {
if e := checkFdlimit(); e != nil {
log.Fatal(e.Error())
}
}
// Start services that have not been deregistered via flags and filtering.
for _, service := range allServices {
if !IsFork && service.RequiresFork() {
if !service.AutoStart() {
continue
}
go service.ForkStart()
} else {
go service.Start()
}
}
wg.Add(1)
wg.Wait()
},
}
func init() {
StartCmd.Flags().StringArrayVarP(&FilterStartTags, "tags", "t", []string{}, "Filter by tags")
StartCmd.Flags().StringArrayVarP(&FilterStartExclude, "exclude", "x", []string{}, "Filter")
RootCmd.AddCommand(StartCmd)
}