github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/client/choria_provisionclient/client.go (about) 1 // generated code; DO NOT EDIT 2 3 package choria_provisionclient 4 5 import ( 6 "fmt" 7 "sync" 8 "time" 9 10 "context" 11 12 coreclient "github.com/choria-io/go-choria/client/client" 13 "github.com/choria-io/go-choria/config" 14 "github.com/choria-io/go-choria/inter" 15 "github.com/choria-io/go-choria/protocol" 16 rpcclient "github.com/choria-io/go-choria/providers/agent/mcorpc/client" 17 "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent" 18 ) 19 20 // Stats are the statistics for a request 21 type Stats interface { 22 Agent() string 23 Action() string 24 All() bool 25 NoResponseFrom() []string 26 UnexpectedResponseFrom() []string 27 DiscoveredCount() int 28 DiscoveredNodes() *[]string 29 FailCount() int 30 OKCount() int 31 ResponsesCount() int 32 PublishDuration() (time.Duration, error) 33 RequestDuration() (time.Duration, error) 34 DiscoveryDuration() (time.Duration, error) 35 OverrideDiscoveryTime(start time.Time, end time.Time) 36 UniqueRequestID() string 37 } 38 39 // NodeSource discovers nodes 40 type NodeSource interface { 41 Reset() 42 Discover(ctx context.Context, fw inter.Framework, filters []FilterFunc) ([]string, error) 43 } 44 45 // FilterFunc can generate a Choria filter 46 type FilterFunc func(f *protocol.Filter) error 47 48 // RenderFormat is the format used by the RenderResults helper 49 type RenderFormat int 50 51 const ( 52 // JSONFormat renders the results as a JSON document 53 JSONFormat RenderFormat = iota 54 55 // TextFormat renders the results as a Choria typical result set in line with choria req output 56 TextFormat 57 58 // TableFormat renders all successful responses in a table 59 TableFormat 60 61 // TXTFooter renders only the request summary statistics 62 TXTFooter 63 ) 64 65 // DisplayMode overrides the DDL display hints 66 type DisplayMode uint8 67 68 const ( 69 // DisplayDDL shows results based on the configuration in the DDL file 70 DisplayDDL = DisplayMode(iota) 71 // DisplayOK shows only passing results 72 DisplayOK 73 // DisplayFailed shows only failed results 74 DisplayFailed 75 // DisplayAll shows all results 76 DisplayAll 77 // DisplayNone shows no results 78 DisplayNone 79 ) 80 81 type Log interface { 82 Debugf(format string, args ...any) 83 Infof(format string, args ...any) 84 Warnf(format string, args ...any) 85 Errorf(format string, args ...any) 86 Fatalf(format string, args ...any) 87 Panicf(format string, args ...any) 88 } 89 90 // ChoriaProvisionClient to the choria_provision agent 91 type ChoriaProvisionClient struct { 92 fw inter.Framework 93 cfg *config.Config 94 ddl *agent.DDL 95 ns NodeSource 96 clientOpts *initOptions 97 clientRPCOpts []rpcclient.RequestOption 98 filters []FilterFunc 99 targets []string 100 workers int 101 exprFilter string 102 noReplies bool 103 104 sync.Mutex 105 } 106 107 // Metadata is the agent metadata 108 type Metadata struct { 109 License string `json:"license"` 110 Author string `json:"author"` 111 Timeout int `json:"timeout"` 112 Name string `json:"name"` 113 Version string `json:"version"` 114 URL string `json:"url"` 115 Description string `json:"description"` 116 } 117 118 // Must create a new client and panics on error 119 func Must(fw inter.Framework, opts ...InitializationOption) (client *ChoriaProvisionClient) { 120 c, err := New(fw, opts...) 121 if err != nil { 122 panic(err) 123 } 124 125 return c 126 } 127 128 // New creates a new client to the choria_provision agent 129 func New(fw inter.Framework, opts ...InitializationOption) (client *ChoriaProvisionClient, err error) { 130 c := &ChoriaProvisionClient{ 131 fw: fw, 132 ddl: &agent.DDL{}, 133 clientRPCOpts: []rpcclient.RequestOption{}, 134 filters: []FilterFunc{ 135 FilterFunc(coreclient.AgentFilter("choria_provision")), 136 }, 137 clientOpts: &initOptions{ 138 cfgFile: coreclient.UserConfig(), 139 }, 140 targets: []string{}, 141 } 142 143 for _, opt := range opts { 144 opt(c.clientOpts) 145 } 146 147 c.cfg = c.fw.Configuration() 148 149 if c.clientOpts.dt > 0 { 150 c.cfg.DiscoveryTimeout = int(c.clientOpts.dt.Seconds()) 151 } 152 153 if c.clientOpts.ns == nil { 154 switch c.cfg.DefaultDiscoveryMethod { 155 case "choria": 156 c.clientOpts.ns = &PuppetDBNS{} 157 default: 158 c.clientOpts.ns = &BroadcastNS{} 159 } 160 } 161 c.ns = c.clientOpts.ns 162 163 if c.clientOpts.logger == nil { 164 c.clientOpts.logger = c.fw.Logger("choria_provision") 165 } else { 166 c.fw.SetLogger(c.clientOpts.logger.Logger) 167 } 168 169 c.ddl, err = DDL() 170 if err != nil { 171 return nil, fmt.Errorf("could not parse embedded DDL: %s", err) 172 } 173 174 return c, nil 175 } 176 177 // AgentMetadata is the agent metadata this client supports 178 func (p *ChoriaProvisionClient) AgentMetadata() *Metadata { 179 return &Metadata{ 180 License: p.ddl.Metadata.License, 181 Author: p.ddl.Metadata.Author, 182 Timeout: p.ddl.Metadata.Timeout, 183 Name: p.ddl.Metadata.Name, 184 Version: p.ddl.Metadata.Version, 185 URL: p.ddl.Metadata.URL, 186 Description: p.ddl.Metadata.Description, 187 } 188 } 189 190 // DiscoverNodes performs a discovery using the configured filter and node source 191 func (p *ChoriaProvisionClient) DiscoverNodes(ctx context.Context) (nodes []string, err error) { 192 p.Lock() 193 defer p.Unlock() 194 195 return p.ns.Discover(ctx, p.fw, p.filters) 196 } 197 198 // Configure performs the configure action 199 // 200 // Description: Configure the Choria Server 201 // 202 // Required Inputs: 203 // - config (string) - The configuration to apply to this node 204 // 205 // Optional Inputs: 206 // - action_policies (map[string]any) - Map of Action Policy documents indexed by file name 207 // - ca (string) - PEM text block for the CA 208 // - certificate (string) - PEM text block for the certificate 209 // - ecdh_public (string) - Required when sending a private key 210 // - key (string) - A RSA private key 211 // - opa_policies (map[string]any) - Map of Open Policy Agent Policy documents indexed by file name 212 // - server_jwt (string) - JWT file used to identify the server to the broker for ed25519 based authentication 213 // - ssldir (string) - Directory for storing the certificate in 214 // - token (string) - Authentication token to pass to the server 215 func (p *ChoriaProvisionClient) Configure(inputConfig string) *ConfigureRequester { 216 d := &ConfigureRequester{ 217 outc: nil, 218 r: &requester{ 219 args: map[string]any{ 220 "config": inputConfig, 221 }, 222 action: "configure", 223 client: p, 224 }, 225 } 226 227 action, _ := p.ddl.ActionInterface(d.r.action) 228 action.SetDefaults(d.r.args) 229 230 return d 231 } 232 233 // Gen25519 performs the gen25519 action 234 // 235 // Description: Generates a new ED25519 keypair 236 // 237 // Required Inputs: 238 // - nonce (string) - Single use token to be signed by the private key being generated 239 // - token (string) - Authentication token to pass to the server 240 func (p *ChoriaProvisionClient) Gen25519(inputNonce string, inputToken string) *Gen25519Requester { 241 d := &Gen25519Requester{ 242 outc: nil, 243 r: &requester{ 244 args: map[string]any{ 245 "nonce": inputNonce, 246 "token": inputToken, 247 }, 248 action: "gen25519", 249 client: p, 250 }, 251 } 252 253 action, _ := p.ddl.ActionInterface(d.r.action) 254 action.SetDefaults(d.r.args) 255 256 return d 257 } 258 259 // Gencsr performs the gencsr action 260 // 261 // Description: Request a CSR from the Choria Server 262 // 263 // Required Inputs: 264 // - token (string) - Authentication token to pass to the server 265 // 266 // Optional Inputs: 267 // - C (string) - Country Code 268 // - L (string) - Locality or municipality (such as city or town name) 269 // - O (string) - Organization 270 // - OU (string) - Organizational Unit 271 // - ST (string) - State 272 // - cn (string) - The certificate Common Name to place in the CSR 273 func (p *ChoriaProvisionClient) Gencsr(inputToken string) *GencsrRequester { 274 d := &GencsrRequester{ 275 outc: nil, 276 r: &requester{ 277 args: map[string]any{ 278 "token": inputToken, 279 }, 280 action: "gencsr", 281 client: p, 282 }, 283 } 284 285 action, _ := p.ddl.ActionInterface(d.r.action) 286 action.SetDefaults(d.r.args) 287 288 return d 289 } 290 291 // Jwt performs the jwt action 292 // 293 // Description: Re-enable provision mode in a running Choria Server 294 // 295 // Required Inputs: 296 // - token (string) - Authentication token to pass to the server 297 func (p *ChoriaProvisionClient) Jwt(inputToken string) *JwtRequester { 298 d := &JwtRequester{ 299 outc: nil, 300 r: &requester{ 301 args: map[string]any{ 302 "token": inputToken, 303 }, 304 action: "jwt", 305 client: p, 306 }, 307 } 308 309 action, _ := p.ddl.ActionInterface(d.r.action) 310 action.SetDefaults(d.r.args) 311 312 return d 313 } 314 315 // Reprovision performs the reprovision action 316 // 317 // Description: Reenable provision mode in a running Choria Server 318 // 319 // Required Inputs: 320 // - token (string) - Authentication token to pass to the server 321 func (p *ChoriaProvisionClient) Reprovision(inputToken string) *ReprovisionRequester { 322 d := &ReprovisionRequester{ 323 outc: nil, 324 r: &requester{ 325 args: map[string]any{ 326 "token": inputToken, 327 }, 328 action: "reprovision", 329 client: p, 330 }, 331 } 332 333 action, _ := p.ddl.ActionInterface(d.r.action) 334 action.SetDefaults(d.r.args) 335 336 return d 337 } 338 339 // Restart performs the restart action 340 // 341 // Description: Restart the Choria Server 342 // 343 // Required Inputs: 344 // - token (string) - Authentication token to pass to the server 345 // 346 // Optional Inputs: 347 // - splay (float64) - The configuration to apply to this node 348 func (p *ChoriaProvisionClient) Restart(inputToken string) *RestartRequester { 349 d := &RestartRequester{ 350 outc: nil, 351 r: &requester{ 352 args: map[string]any{ 353 "token": inputToken, 354 }, 355 action: "restart", 356 client: p, 357 }, 358 } 359 360 action, _ := p.ddl.ActionInterface(d.r.action) 361 action.SetDefaults(d.r.args) 362 363 return d 364 } 365 366 // ReleaseUpdate performs the release_update action 367 // 368 // Description: Performs an in-place binary update and restarts Choria 369 // 370 // Required Inputs: 371 // - repository (string) - HTTP(S) server hosting the update repository 372 // - token (string) - Authentication token to pass to the server 373 // - version (string) - Package version to update to 374 func (p *ChoriaProvisionClient) ReleaseUpdate(inputRepository string, inputToken string, inputVersion string) *ReleaseUpdateRequester { 375 d := &ReleaseUpdateRequester{ 376 outc: nil, 377 r: &requester{ 378 args: map[string]any{ 379 "repository": inputRepository, 380 "token": inputToken, 381 "version": inputVersion, 382 }, 383 action: "release_update", 384 client: p, 385 }, 386 } 387 388 action, _ := p.ddl.ActionInterface(d.r.action) 389 action.SetDefaults(d.r.args) 390 391 return d 392 } 393 394 // Shutdown performs the shutdown action 395 // 396 // Description: Shut the Choria Server down cleanly 397 // 398 // Required Inputs: 399 // - token (string) - Authentication token to pass to the server 400 func (p *ChoriaProvisionClient) Shutdown(inputToken string) *ShutdownRequester { 401 d := &ShutdownRequester{ 402 outc: nil, 403 r: &requester{ 404 args: map[string]any{ 405 "token": inputToken, 406 }, 407 action: "shutdown", 408 client: p, 409 }, 410 } 411 412 action, _ := p.ddl.ActionInterface(d.r.action) 413 action.SetDefaults(d.r.args) 414 415 return d 416 }