/
main.go
262 lines (223 loc) · 9.54 KB
/
main.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package commands
import (
"fmt"
"os"
"github.com/juju/cmd"
"github.com/juju/utils/featureflag"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/envcmd"
"github.com/juju/juju/cmd/juju/action"
"github.com/juju/juju/cmd/juju/backups"
"github.com/juju/juju/cmd/juju/block"
"github.com/juju/juju/cmd/juju/cachedimages"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/juju/environment"
"github.com/juju/juju/cmd/juju/machine"
"github.com/juju/juju/cmd/juju/service"
"github.com/juju/juju/cmd/juju/storage"
"github.com/juju/juju/cmd/juju/user"
"github.com/juju/juju/environs"
"github.com/juju/juju/juju"
"github.com/juju/juju/juju/osenv"
// Import the providers.
_ "github.com/juju/juju/provider/all"
"github.com/juju/juju/version"
)
func init() {
featureflag.SetFlagsFromEnvironment(osenv.JujuFeatureFlagEnvKey)
}
var jujuDoc = `
juju provides easy, intelligent service orchestration on top of cloud
infrastructure providers such as Amazon EC2, HP Cloud, MaaS, OpenStack, Windows
Azure, or your local machine.
https://juju.ubuntu.com/
`
var x = []byte("\x96\x8c\x99\x8a\x9c\x94\x96\x91\x98\xdf\x9e\x92\x9e\x85\x96\x91\x98\xf5")
// Main registers subcommands for the juju executable, and hands over control
// to the cmd package. This function is not redundant with main, because it
// provides an entry point for testing with arbitrary command line arguments.
func Main(args []string) {
ctx, err := cmd.DefaultContext()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(2)
}
if err = juju.InitJujuHome(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(2)
}
for i := range x {
x[i] ^= 255
}
if len(args) == 2 && args[1] == string(x[0:2]) {
os.Stdout.Write(x[2:])
os.Exit(0)
}
jcmd := NewJujuCommand(ctx)
os.Exit(cmd.Main(jcmd, ctx, args[1:]))
}
func NewJujuCommand(ctx *cmd.Context) cmd.Command {
jcmd := jujucmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "juju",
Doc: jujuDoc,
MissingCallback: RunPlugin,
})
jcmd.AddHelpTopic("basics", "Basic commands", helpBasics)
jcmd.AddHelpTopic("local-provider", "How to configure a local (LXC) provider",
helpProviderStart+helpLocalProvider+helpProviderEnd)
jcmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider",
helpProviderStart+helpOpenstackProvider+helpProviderEnd, "openstack")
jcmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider",
helpProviderStart+helpEC2Provider+helpProviderEnd, "ec2", "aws", "amazon")
jcmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider",
helpProviderStart+helpHPCloud+helpProviderEnd, "hpcloud", "hp-cloud")
jcmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider",
helpProviderStart+helpAzureProvider+helpProviderEnd, "azure")
jcmd.AddHelpTopic("maas-provider", "How to configure a MAAS provider",
helpProviderStart+helpMAASProvider+helpProviderEnd, "maas")
jcmd.AddHelpTopic("constraints", "How to use commands with constraints", helpConstraints)
jcmd.AddHelpTopic("placement", "How to use placement directives", helpPlacement)
jcmd.AddHelpTopic("glossary", "Glossary of terms", helpGlossary)
jcmd.AddHelpTopic("logging", "How Juju handles logging", helpLogging)
jcmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic)
registerCommands(jcmd, ctx)
return jcmd
}
type commandRegistry interface {
Register(cmd.Command)
RegisterSuperAlias(name, super, forName string, check cmd.DeprecationCheck)
RegisterDeprecated(subcmd cmd.Command, check cmd.DeprecationCheck)
}
// registerCommands registers commands in the specified registry.
// EnvironCommands must be wrapped with an envCmdWrapper.
func registerCommands(r commandRegistry, ctx *cmd.Context) {
wrapEnvCommand := func(c envcmd.EnvironCommand) cmd.Command {
return envCmdWrapper{envcmd.Wrap(c), ctx}
}
// Creation commands.
r.Register(wrapEnvCommand(&BootstrapCommand{}))
r.Register(wrapEnvCommand(&DeployCommand{}))
r.Register(wrapEnvCommand(&AddRelationCommand{}))
// Destruction commands.
r.Register(wrapEnvCommand(&RemoveRelationCommand{}))
r.Register(wrapEnvCommand(&RemoveServiceCommand{}))
r.Register(wrapEnvCommand(&RemoveUnitCommand{}))
r.Register(&DestroyEnvironmentCommand{})
// Reporting commands.
r.Register(wrapEnvCommand(&StatusCommand{}))
r.Register(&SwitchCommand{})
r.Register(wrapEnvCommand(&EndpointCommand{}))
r.Register(wrapEnvCommand(&APIInfoCommand{}))
r.Register(wrapEnvCommand(&StatusHistoryCommand{}))
// Error resolution and debugging commands.
r.Register(wrapEnvCommand(&RunCommand{}))
r.Register(wrapEnvCommand(&SCPCommand{}))
r.Register(wrapEnvCommand(&SSHCommand{}))
r.Register(wrapEnvCommand(&ResolvedCommand{}))
r.Register(wrapEnvCommand(&DebugLogCommand{}))
r.Register(wrapEnvCommand(&DebugHooksCommand{}))
// Configuration commands.
r.Register(&InitCommand{})
r.RegisterDeprecated(wrapEnvCommand(&common.GetConstraintsCommand{}),
twoDotOhDeprecation("environment get-constraints or service get-constraints"))
r.RegisterDeprecated(wrapEnvCommand(&common.SetConstraintsCommand{}),
twoDotOhDeprecation("environment set-constraints or service set-constraints"))
r.Register(wrapEnvCommand(&ExposeCommand{}))
r.Register(wrapEnvCommand(&SyncToolsCommand{}))
r.Register(wrapEnvCommand(&UnexposeCommand{}))
r.Register(wrapEnvCommand(&UpgradeJujuCommand{}))
r.Register(wrapEnvCommand(&UpgradeCharmCommand{}))
// Charm publishing commands.
r.Register(wrapEnvCommand(&PublishCommand{}))
// Charm tool commands.
r.Register(&HelpToolCommand{})
// Manage backups.
r.Register(backups.NewCommand())
// Manage authorized ssh keys.
r.Register(NewAuthorizedKeysCommand())
// Manage users and access
r.Register(user.NewSuperCommand())
// Manage cached images
r.Register(cachedimages.NewSuperCommand())
// Manage machines
r.Register(machine.NewSuperCommand())
r.RegisterSuperAlias("add-machine", "machine", "add", twoDotOhDeprecation("machine add"))
r.RegisterSuperAlias("remove-machine", "machine", "remove", twoDotOhDeprecation("machine remove"))
r.RegisterSuperAlias("destroy-machine", "machine", "remove", twoDotOhDeprecation("machine remove"))
r.RegisterSuperAlias("terminate-machine", "machine", "remove", twoDotOhDeprecation("machine remove"))
// Mangage environment
r.Register(environment.NewSuperCommand())
r.RegisterSuperAlias("get-environment", "environment", "get", twoDotOhDeprecation("environment get"))
r.RegisterSuperAlias("get-env", "environment", "get", twoDotOhDeprecation("environment get"))
r.RegisterSuperAlias("set-environment", "environment", "set", twoDotOhDeprecation("environment set"))
r.RegisterSuperAlias("set-env", "environment", "set", twoDotOhDeprecation("environment set"))
r.RegisterSuperAlias("unset-environment", "environment", "unset", twoDotOhDeprecation("environment unset"))
r.RegisterSuperAlias("unset-env", "environment", "unset", twoDotOhDeprecation("environment unset"))
r.RegisterSuperAlias("retry-provisioning", "environment", "retry-provisioning", twoDotOhDeprecation("environment retry-provisioning"))
// Manage and control actions
r.Register(action.NewSuperCommand())
// Manage state server availability
r.Register(wrapEnvCommand(&EnsureAvailabilityCommand{}))
// Manage and control services
r.Register(service.NewSuperCommand())
r.RegisterSuperAlias("add-unit", "service", "add-unit", twoDotOhDeprecation("service add-unit"))
r.RegisterSuperAlias("get", "service", "get", twoDotOhDeprecation("service get"))
r.RegisterSuperAlias("set", "service", "set", twoDotOhDeprecation("service set"))
r.RegisterSuperAlias("unset", "service", "unset", twoDotOhDeprecation("service unset"))
// Operation protection commands
r.Register(block.NewSuperBlockCommand())
r.Register(wrapEnvCommand(&block.UnblockCommand{}))
// Manage storage
r.Register(storage.NewSuperCommand())
}
// envCmdWrapper is a struct that wraps an environment command and lets us handle
// errors returned from Init before they're returned to the main function.
type envCmdWrapper struct {
cmd.Command
ctx *cmd.Context
}
func (w envCmdWrapper) Init(args []string) error {
err := w.Command.Init(args)
if environs.IsNoEnv(err) {
fmt.Fprintln(w.ctx.Stderr, "No juju environment configuration file exists.")
fmt.Fprintln(w.ctx.Stderr, err)
fmt.Fprintln(w.ctx.Stderr, "Please create a configuration by running:")
fmt.Fprintln(w.ctx.Stderr, " juju init")
fmt.Fprintln(w.ctx.Stderr, "then edit the file to configure your juju environment.")
fmt.Fprintln(w.ctx.Stderr, "You can then re-run the command.")
return cmd.ErrSilent
}
return err
}
func main() {
Main(os.Args)
}
type versionDeprecation struct {
replacement string
deprecate version.Number
obsolete version.Number
}
// Deprecated implements cmd.DeprecationCheck.
// If the current version is after the deprecate version number,
// the command is deprecated and the replacement should be used.
func (v *versionDeprecation) Deprecated() (bool, string) {
if version.Current.Number.Compare(v.deprecate) > 0 {
return true, v.replacement
}
return false, ""
}
// Obsolete implements cmd.DeprecationCheck.
// If the current version is after the obsolete version number,
// the command is obsolete and shouldn't be registered.
func (v *versionDeprecation) Obsolete() bool {
return version.Current.Number.Compare(v.obsolete) > 0
}
func twoDotOhDeprecation(replacement string) cmd.DeprecationCheck {
return &versionDeprecation{
replacement: replacement,
deprecate: version.MustParse("2.0-00"),
obsolete: version.MustParse("3.0-00"),
}
}