1 #!/usr/local/bin/python2.7
3 This script is a combination of the AddComponentScript and the OnBoardingStatus
4 scripts. Depending on the arguments given, it will either post an
5 "addMachineCommand" or an "logOnBoardingInfo" request.
11 from socket import getfqdn
16 parser = argparse.ArgumentParser()
17 parser.add_argument("scribe_ip", type=str,
18 help="The IP where the Scribe can be reached.")
19 parser.add_argument("--component-type", type=str,
20 help="The component type.", required=False)
21 parser.add_argument("--component-version", type=str,
22 help="The component version.", required=False)
23 parser.add_argument("--stage", type=str, required=False)
24 parser.add_argument("--status", required=False,
25 choices=["WARNING", "INFO", "ERROR", "OK", "FAILURE"])
26 parser.add_argument("--description", type=str, required=False)
27 args = parser.parse_args()
29 add_machine_ip = "http://{0}:8084/MaveriQManager/api/Inventory/addComponent".format(args.scribe_ip)
30 log_onboarding_info_ip = "http://{0}:8084/MaveriQManager/api/Inventory/logOnBoardingInfo".format(args.scribe_ip)
40 MACHINE_TYPE = args.component_type
41 MACHINE_NAME = getfqdn()
43 ADD_COMPONENT_BODY = {}
47 """Read the instance metadata"""
54 with open('/meta.js', 'r') as json_file:
55 json_data = json.loads(json_file.read())
56 TENANT = json_data["tenant"]
57 REGION = json_data["region"]
58 CLUSTER_NAME = json_data["cluster_name"]
59 VERSION_NUMBER = json_data["version_number"]
60 OAM_DIRECT_IP = json_data["oam_private_network_ip"]
61 if MACHINE_TYPE == 'vProbe':
62 PROBE_ID = json_data["probe_id"]
65 def build_health_json():
66 """Builds the actual health status"""
67 HEALTH_STATUS["Region"] = REGION
68 HEALTH_STATUS["Tenant"] = TENANT
69 HEALTH_STATUS["MachineType"] = args.component_type
70 HEALTH_STATUS["MachineName"] = getfqdn()
71 HEALTH_STATUS["MachineIP"] = OAM_DIRECT_IP
72 HEALTH_STATUS["Time"] = long(time())
73 HEALTH_STATUS["Description"] = args.description
74 HEALTH_STATUS["Status"] = args.status
75 HEALTH_STATUS["Stage"] = args.stage
80 """Builds the actual health status"""
81 ADD_COMPONENT_BODY["region"] = REGION
82 ADD_COMPONENT_BODY["tenant"] = TENANT
83 ADD_COMPONENT_BODY["componentType"] = args.component_type
84 ADD_COMPONENT_BODY["clusterName"] = CLUSTER_NAME
85 ADD_COMPONENT_BODY["componentVersionNumber"] = VERSION_NUMBER
86 ADD_COMPONENT_BODY["machineName"] = MACHINE_NAME
87 ADD_COMPONENT_BODY["machineNetworkInterfaces"] = []
88 ADD_COMPONENT_BODY["OAM_IP"] = OAM_DIRECT_IP
90 for interface in netifaces.interfaces():
91 ADD_COMPONENT_BODY["machineNetworkInterfaces"].append({"name": interface, "value": netifaces.ifaddresses(interface)[2][0]['addr']})
93 if PROBE_ID is not "":
94 ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' +\
95 CLUSTER_NAME + '_' + MACHINE_NAME + '_' + PROBE_ID
97 ADD_COMPONENT_BODY["machineID"] = REGION + '_' + TENANT + '_' +\
98 CLUSTER_NAME + '_' + MACHINE_NAME
99 return ADD_COMPONENT_BODY
102 def send_postage(p_url, p_url_user, p_url_password, p_json_data):
103 json_header = {'Content-type': 'application/json'}
104 request = requests.post(p_url, json.dumps(p_json_data), json_header, auth=requests.auth.HTTPBasicAuth(p_url_user, p_url_password))
105 print request.status_code
106 if (request.status_code != 200):
108 return request.status_code
113 return send_postage(log_onboarding_info_ip, user, password,
117 def post_add_machine():
119 return send_postage(add_machine_ip, user, password, build_add_json())
121 if args.stage is None and args.status is None and args.description is None:
122 print "adding machine"
123 print post_add_machine()
125 print "logging health"