1+ """
2+ # This script is intended to run in your OCI Tenancy,
3+ # It uploads a file from your OCI Shell into the logging analytics upload service.
4+
5+ # @param file - the path of the file to upload
6+ # @param filename - The name with which the file will be saved in OCI
7+ # @param log-source - The log source to upload the file for
8+ # @param name - The name of the upload instance
9+ """
10+ import subprocess , json , argparse
11+
12+
13+ # CLI Arguments
14+ argParser = argparse .ArgumentParser ()
15+ argParser .add_argument ("-f" , "--file" , help = "your file" , required = True )
16+ argParser .add_argument ("-s" , "--filename" , help = "your filename" , required = True )
17+ argParser .add_argument ("-l" , "--log-source" , help = "your log source" , required = True )
18+ argParser .add_argument ("-n" , "--name" , help = "your name" , required = True )
19+
20+ args = argParser .parse_args ()
21+
22+
23+ # Functions
24+ # Lets the user choose a compartment to use the groups of
25+ def choose_compartment () -> str :
26+
27+ # Compartment Variables
28+ compartments = json .loads (
29+ subprocess .getoutput ('oci iam compartment list --all --query "data[].{name:name, id:id}" --access-level ANY --compartment-id-in-subtree true' )
30+ )
31+ compartments_names = [compartment ['name' ] for compartment in compartments ]
32+
33+ # List the compartments of the OCI tenancies
34+ print ("Here is the list of your OCI tenancy compartments: " )
35+ for name in enumerate (compartments_names ):
36+ print (* name , sep = '> ' )
37+
38+ # Prompt the user to select a value
39+ selected_input = input ("Please, Choose the index of the compartment you want to upload your files to: " )
40+
41+ while not (selected_input .isnumeric () and 0 <= int (selected_input ) < len (compartments_names )) : # re-prompt if the user selected a different value
42+ selected_input = input ("The compartment selected does not exist, Please choose a valid compartment index: " )
43+
44+ return compartments [int (selected_input )]
45+
46+
47+ # Get the default namespace label
48+ def get_namespace () -> str :
49+ namespace = subprocess .getoutput ('''oci log-analytics namespace list --compartment-id $(oci iam compartment list --all --compartment-id-in-subtree true --access-level ACCESSIBLE --include-root --raw-output --query "data[?contains(\\ "id\\ ",'tenancy')].id | [0]") --query "data.items[].{namespace: \\ "namespace-name\\ "}[0].namespace" --raw-output''' )
50+
51+ return namespace
52+
53+
54+ # Lets the user choose a compartment to use the groups of
55+ def choose_log_group (settings :dict ) -> str :
56+
57+ response = subprocess .getoutput (f'''oci log-analytics log-group list -c { settings ["compartment" ]["id" ]} --namespace-name { settings ["namespace" ]} --query "data.items[].{{name: \\ "display-name\\ ", id: id}}"''' )
58+
59+ if response == "Query returned empty result, no output to show." :
60+ # Create a new log group
61+ print ('You have no log groups in your compartment (Check your region and tenancy again)' )
62+ if not input ('Do you want to create a new log group automatically? (y|n): ' ).lower () in ('y' , 'yes' ):
63+ return
64+
65+ return create_log_group (settings )
66+
67+ # Compartment Variables
68+ log_groups = json .loads (
69+ subprocess .getoutput (f'''oci log-analytics log-group list -c { settings ["compartment" ]["id" ]} --namespace-name { settings ["namespace" ]} --query "data.items[].{{name: \\ "display-name\\ ", id: id}}"''' )
70+ )
71+
72+ log_groups_names = [log_group ['name' ] for log_group in log_groups ]
73+
74+ # List the log groups of the OCI tenancies
75+ print ("Here is the list of your OCI tenancy log groups: " )
76+ for name in enumerate (log_groups_names ):
77+ print (* name , sep = '> ' )
78+
79+ # Prompt the user to select a value
80+ selected_input = input ("Please, Choose the index of the log group you want to upload your files to: " )
81+
82+ while not (selected_input .isnumeric () and 0 <= int (selected_input ) < len (log_groups_names )) : # re-prompt if the user selected a different value
83+ selected_input = input ("The log group selected does not exist, Please choose a valid log group index: " )
84+
85+ return log_groups [int (selected_input )]
86+
87+
88+ # Create a new log group
89+ def create_log_group (settings :dict ):
90+ return json .loads (
91+ subprocess .getoutput (f"""oci log-analytics log-group create --namespace-name { settings ["namespace" ]} --display-name "Live Labs Log Group - You can delete it once you are done" --compartment-id { settings ["compartment" ]["id" ]} """ )
92+ )
93+
94+
95+ # Implementation
96+ # The bash command parameters
97+ settings = {
98+ "compartment" : choose_compartment (),
99+ "namespace" : get_namespace ()
100+ }
101+
102+ settings ["log_group" ] = choose_log_group (settings )
103+
104+ # OCI command script
105+ print (
106+ subprocess .getoutput (f'''oci log-analytics upload upload-log-file --file "{ args .file } " --filename "{ args .filename } " --log-source-name "{ args .log_source } " --namespace-name "{ settings ["namespace" ]} " --opc-meta-loggrpid "{ settings ["log_group" ]["id" ]} " --upload-name "{ args .name } "''' )
107+ )
0 commit comments