in cmd/main.go [51:127]
func main() {
ctx := context.Background()
lp := log.Parameters{
OSType: runtime.GOOS,
Level: zapcore.InfoLevel,
LogToCloud: true,
}
cloudProps := &cpb.CloudProperties{}
if cp := metadataserver.FetchCloudProperties(); cp != nil {
cloudProps = &cpb.CloudProperties{
ProjectId: cp.ProjectID,
InstanceId: cp.InstanceID,
Zone: cp.Zone,
Region: cp.Region,
InstanceName: cp.InstanceName,
Image: cp.Image,
NumericProjectId: cp.NumericProjectID,
MachineType: cp.MachineType,
}
}
lp.CloudLoggingClient = log.CloudLoggingClient(ctx, cloudProps.GetProjectId())
osData, err := osinfo.ReadData(ctx, osinfo.FileReadCloser(configFileReader), osinfo.OSName, osinfo.OSReleaseFilePath)
if err != nil {
log.Logger.Errorw("Unable to read OS data. Agent services may be impacted.", "error", err)
}
rootCmd := &cobra.Command{
Use: "google_cloud_workload_agent",
Short: "Google Cloud Agent for Compute Workloads",
Long: "Google Cloud Agent for Compute Workloads",
}
rootCmd.AddCommand(version.NewCommand())
rootCmd.AddCommand(logusage.NewCommand(lp, cloudProps))
rootCmd.AddCommand(migrate.NewCommand())
rootCmd.AddCommand(configure.NewCommand(lp))
d := daemon.NewDaemon(lp, cloudProps, osData)
daemonCmd := daemon.NewDaemonSubCommand(d)
// When running on windows, the daemon is started using the winservice subcommand.
// Having both the daemon command and the winservice command will cause an error when the
// winservice tries to start the daemon, cobra will start the parent which is the winservice
// causing a loop.
if lp.OSType != "windows" {
rootCmd.AddCommand(daemonCmd)
}
// Add any additional windows or linux specific subcommands.
rootCmd.AddCommand(additionalSubcommands(ctx, daemonCmd)...)
for _, cmd := range rootCmd.Commands() {
if cmd.Name() != "startdaemon" {
onetime.Register(lp.OSType, cmd)
}
}
rootCmd.SetArgs(pflag.Args())
rc := 0
if err := rootCmd.ExecuteContext(ctx); err != nil {
log.Logger.Error(err)
rc = 1
}
// Defer cloud log flushing to ensure execution on any exit from main.
defer func() {
if lp.CloudLoggingClient != nil {
flushTimer := time.AfterFunc(5*time.Second, func() {
log.Logger.Error("Cloud logging client failed to flush logs within the 5-second deadline, exiting.")
os.Exit(rc)
})
log.FlushCloudLog()
lp.CloudLoggingClient.Close()
flushTimer.Stop()
}
}()
os.Exit(rc)
}