in oracle/cmd/dbdaemon_client/dbdaemon_client.go [92:343]
func main() {
klog.InitFlags(nil)
flag.Parse()
flag.Usage = usage
hostname, err := os.Hostname()
if err != nil {
klog.ErrorS(err, "Failed to retrieve the hostname")
os.Exit(exitErrorCode)
}
// There's a 5 min default timeout on a Dial and the overall
// total default timeout of 10 min, all configurable.
ctx, cancel := context.WithTimeout(context.Background(), *reqTimeout)
defer cancel()
conn, err := dbdaemonlib.DatabaseDaemonDialLocalhost(ctx, consts.DefaultDBDaemonPort, grpc.WithBlock())
if err != nil {
klog.ErrorS(err, "Failed to dial the Database Daemon")
os.Exit(exitErrorCode)
}
defer conn.Close()
client := newDatabaseDaemonClient(conn)
switch *action {
case "CheckDatabaseCDB":
klog.InfoS("checking the state of the CDB...", "container/host", hostname)
_, err := client.CheckDatabaseState(ctx, &dbdpb.CheckDatabaseStateRequest{IsCdb: true, DatabaseName: *cdbName})
if err != nil {
klog.ErrorS(err, "failed to check the state of the CDB")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: CDB is healthy", "action", *action, "CDB", *cdbName, "container/host", hostname)
case "CheckDatabasePDB":
klog.InfoS("checking the state of the PDB...", "container/host", hostname, "PDB", *databaseName)
_, err := client.CheckDatabaseState(ctx, &dbdpb.CheckDatabaseStateRequest{DatabaseName: *databaseName})
if err != nil {
klog.ErrorS(err, "failed to check the state of the PDB")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: PDB is healthy", "action", *action, "container/host", hostname, "PDB", *databaseName)
case "RunSQLPlus", "RunSQLPlusFormatted":
klog.InfoS("Executing a SQL statement...", "action", *action, "container/host", hostname)
if *commands == "" {
klog.Errorf("--action=RunSQLPlus requires --commands sub parameter, but none provided")
os.Exit(exitErrorCode)
}
var resp *dbdpb.RunCMDResponse
if *action == "RunSQLPlus" {
resp, err = client.RunSQLPlus(ctx, &dbdpb.RunSQLPlusCMDRequest{Commands: strings.Split(*commands, ";")})
} else {
resp, err = client.RunSQLPlusFormatted(ctx, &dbdpb.RunSQLPlusCMDRequest{Commands: strings.Split(*commands, ";")})
}
if err != nil {
klog.ErrorS(err, "failed to run SQL statement", "sql", *commands)
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: SQL statement successfully executed", "action", *action, "sql", *commands, "response", resp)
case "StopDatabase":
klog.InfoS("stopping a database...", "container/host", hostname, "PDB", *databaseName)
resp, err := client.BounceDatabase(ctx, &dbdpb.BounceDatabaseRequest{
Operation: dbdpb.BounceDatabaseRequest_SHUTDOWN,
DatabaseName: *cdbName,
Option: "immediate",
})
if err != nil {
klog.ErrorS(err, "failed to stop a database")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully stopped database", "action", *action, "CDB", *cdbName, "container/host", hostname, "response", resp)
case "StartDatabase":
klog.InfoS("starting a database...", "container/host", hostname, "CDB", *databaseName)
resp, err := client.BounceDatabase(ctx, &dbdpb.BounceDatabaseRequest{
Operation: dbdpb.BounceDatabaseRequest_STARTUP,
DatabaseName: *cdbName,
Option: "open",
})
if err != nil {
klog.ErrorS(err, "failed to start a database: %v")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully started database", "action", *action, "CDB", *cdbName, "host", hostname, "response", resp)
case "StopListeners":
for listenerName := range consts.ListenerNames {
klog.InfoS("stopping listeners...", "container/host", hostname, "listener", listenerName)
resp, err := client.BounceListener(ctx, &dbdpb.BounceListenerRequest{
ListenerName: listenerName,
TnsAdmin: filepath.Join(fmt.Sprintf(consts.ListenerDir, consts.DataMount), listenerName),
Operation: dbdpb.BounceListenerRequest_STOP,
})
if err != nil {
klog.ErrorS(err, "failed to stop a listener", "listener", listenerName)
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully stopped a listener", "action", *action, "listener", listenerName, "container/host", hostname, "response", resp)
}
case "StartListeners":
for listenerName := range consts.ListenerNames {
klog.InfoS("starting listeners...", "container/host", hostname, "listener", listenerName)
resp, err := client.BounceListener(ctx, &dbdpb.BounceListenerRequest{
ListenerName: listenerName,
TnsAdmin: filepath.Join(fmt.Sprintf(consts.ListenerDir, consts.DataMount), listenerName),
Operation: dbdpb.BounceListenerRequest_START,
})
if err != nil {
klog.ErrorS(err, "failed to start a listener", "listener", listenerName)
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully started a listener", "action", *action, "listener", listenerName, "container/host", hostname, "response", resp)
}
case "KnownPDBs":
klog.InfoS("getting a list of known PDBs...", "container/host", hostname)
resp, err := client.KnownPDBs(ctx, &dbdpb.KnownPDBsRequest{})
if err != nil {
klog.ErrorS(err, "failed to get a list of known PDBs")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully retrieved list of known PDBs", "action", *action, "container/host", hostname, "response", resp)
case "GetDatabaseType":
klog.InfoS("retrieving database type...", "container/host", hostname)
resp, err := client.GetDatabaseType(ctx, &dbdpb.GetDatabaseTypeRequest{})
if err != nil {
klog.ErrorS(err, "failed to retrieve database type")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully retrieved database type", "action", *action, "container/host", hostname, "response", resp)
case "GetDatabaseName":
klog.InfoS("retrieving database name...", "container/host", hostname)
resp, err := client.GetDatabaseName(ctx, &dbdpb.GetDatabaseNameRequest{})
if err != nil {
klog.ErrorS(err, "failed to retrieve database name")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully retrieved database name", "action", *action, "container/host", hostname, "response", resp)
case "DataPumpImportAsync":
klog.InfoS("starting data pump import...", "container/host", hostname, "PDB", databaseName)
resp, err := client.DataPumpImportAsync(ctx, &dbdpb.DataPumpImportAsyncRequest{
SyncRequest: &dbdpb.DataPumpImportRequest{
PdbName: *databaseName,
DbDomain: "gke",
GcsPath: *gcsPath,
GcsLogPath: *gcsLogPath,
CommandParams: []string{
"FULL=YES",
"METRICS=YES",
"LOGTIME=ALL",
},
},
})
if err != nil {
klog.ErrorS(err, "failed to start data pump import")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully started Data Pump import", "action", *action, "container/host", hostname, "response", resp)
case "DataPumpExportAsync":
klog.InfoS("starting data pump export...", "container/host", hostname, "PDB", *databaseName, "objectType", *exportObjectType, "exportObjects", *exportObjects)
resp, err := client.DataPumpExportAsync(ctx, &dbdpb.DataPumpExportAsyncRequest{
SyncRequest: &dbdpb.DataPumpExportRequest{
PdbName: *databaseName,
DbDomain: "gke",
ObjectType: *exportObjectType,
Objects: *exportObjects,
FlashbackTime: *flashbackTime,
GcsPath: *gcsPath,
GcsLogPath: *gcsLogPath,
CommandParams: []string{
"METRICS=YES",
"LOGTIME=ALL",
},
},
})
if err != nil {
klog.ErrorS(err, "failed to start data pump export")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully started data pump export", "action", *action, "container/host", hostname, "response", resp)
case "ApplyDataPatchAsync":
klog.InfoS("starting ApplyDataPatchAsync...", "container/host", hostname)
resp, err := client.ApplyDataPatchAsync(ctx, &dbdpb.ApplyDataPatchAsyncRequest{})
if err != nil {
klog.ErrorS(err, "datapatch failed")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Finished datapatch", "action", *action, "container/host", hostname, "response", resp)
case "ListOperations":
klog.InfoS("running ListOperations...")
resp, err := client.ListOperations(ctx, &lropb.ListOperationsRequest{})
if err != nil {
klog.ErrorS(err, "failed listing operations")
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully listed operations", "action", *action, "container/host", hostname, "response", resp)
case "GetOperation":
klog.InfoS("running GetOperation...")
resp, err := client.GetOperation(ctx, &lropb.GetOperationRequest{Name: *operationId})
if err != nil {
klog.ErrorS(err, "failed getting operation", "id", operationId)
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully retrieved operation", "action", *action, "container/host", hostname, "response", resp)
case "DeleteOperation":
klog.InfoS("running DeleteOperation...")
resp, err := client.DeleteOperation(ctx, &lropb.DeleteOperationRequest{Name: *operationId})
if err != nil {
klog.ErrorS(err, "failed deleting operation", "id", operationId)
os.Exit(exitErrorCode)
}
klog.InfoS("action succeeded: Successfully deleted operation", "action", *action, "container/host", hostname, "response", resp)
case "DownloadDirectoryFromGCS":
klog.InfoS("download from GCS bucket", "gcsPath", *gcsPath)
_, err := client.DownloadDirectoryFromGCS(ctx, &dbdpb.DownloadDirectoryFromGCSRequest{GcsPath: *gcsPath, LocalPath: consts.DefaultRMANDir})
if err != nil {
klog.ErrorS(err, "failed downloading directory from gcs bucket", "gcs", *gcsPath, "local path", consts.DefaultRMANDir)
os.Exit(exitErrorCode)
}
klog.InfoS("download succeeded")
case "":
flag.Usage()
default:
klog.Errorf("Unknown action: %q", *action)
os.Exit(exitErrorCode)
}
}