| // Copyright 2021 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| /* | |
| This file implements the client for the COS(Container-Optimized OS) Image update control plane. | |
| It is reponsible for collecting and reporting the current instance status. | |
| When run with the "monitor" command, it will perform initialization and then | |
| periodically fetch update config from the API backend by sending the instance | |
| status to the API. | |
| When run with the "show_config" command, the current config applied to the system | |
| is written to stdout. | |
| Example Output: | |
| update_strategy: "" | |
| metrics_enabled: true | |
| health_monitor_config: < | |
| enforced: false | |
| logging_enabled: false | |
| monitoring_enabled: false | |
| > | |
| */ | |
| package main | |
| import ( | |
| "flag" | |
| "fmt" | |
| "policy-manager/cmd/getstatus" | |
| "policy-manager/cmd/monitor" | |
| "policy-manager/cmd/showconfig" | |
| "github.com/golang/glog" | |
| ) | |
| // Command line flags for monitor mode. | |
| var monitorModeFlags *flag.FlagSet | |
| // Command line flags for show_config mode. | |
| var showConfigModeFlags *flag.FlagSet | |
| // Command line flags for get_status mode. | |
| var getStatusModeFlags *flag.FlagSet | |
| const ( | |
| // Command names. | |
| monitorModeCommand = "monitor" | |
| showConfigCommand = "show_config" | |
| getStatusCommand = "get_status" | |
| ) | |
| func init() { | |
| // Flags for monitor mode. | |
| monitorModeFlags = flag.NewFlagSet(monitorModeCommand, flag.ExitOnError) | |
| monitorModeFlags.Usage = func() { | |
| fmt.Println("Initialize and monitor changes to instance config periodically without terminating") | |
| } | |
| // Flags for show_config. | |
| showConfigModeFlags = flag.NewFlagSet(showConfigCommand, flag.ExitOnError) | |
| showConfigModeFlags.Usage = func() { | |
| fmt.Println("Prints the current device policy for this instance to stdout.") | |
| } | |
| // Flags for get_status. | |
| getStatusModeFlags = flag.NewFlagSet(getStatusCommand, flag.ExitOnError) | |
| getStatusModeFlags.Usage = func() { | |
| fmt.Println("Prints the status of updates available to stdout.") | |
| } | |
| // Log to stderr only by default | |
| flag.Set("logtostderr", "true") | |
| } | |
| func main() { | |
| flag.Usage = func() { | |
| fmt.Println("Usage: device_policy_manager [logging flags] command [args...]") | |
| fmt.Println("") | |
| fmt.Println("Supported logging flags:") | |
| flag.PrintDefaults() | |
| fmt.Println("") | |
| fmt.Printf("Supported commands:\n%s,%s,%s\n", | |
| monitorModeCommand, showConfigCommand, getStatusCommand) | |
| } | |
| // Parse the glog flags. | |
| flag.Parse() | |
| // If no flags are left after the glog flags are processed, print usage | |
| // message and exit. | |
| if flag.NArg() == 0 { | |
| flag.Usage() | |
| return | |
| } | |
| // Get the command and the arguments for the command. | |
| command := flag.Arg(0) | |
| switch command { | |
| case monitorModeCommand: | |
| monitor.InitDevicePolicy() | |
| monitor.HandleMonitorCmd() | |
| case showConfigCommand: | |
| showconfig.HandleShowConfigCmd() | |
| case getStatusCommand: | |
| getstatus.HandleGetStatusCmd() | |
| default: | |
| glog.Errorf("Unrecognized command: %s\n", command) | |
| flag.Usage() | |
| } | |
| } |