Exemple #1
0
 def pull_latest(self, now):
     log.debug('Pulling latest from github..')
     self.last_update_check_time = now
     if pull_latest():
         log.success('Pulled new changes')
         ret = True
     else:
         ret = False
     return ret
 def instance_stop():
     server_start_id = []
     cmd_start  = "nova list |grep ACTIVE|awk '{print $2}'"
     result_start  = shell.shell_cmd(cmd_start)
     for j in xrange(len(result_start)):
         pstart = result_start[j].split()[0]
         server_start_id.append(pstart)
     # nova stop server_start_id list  
     for s_stop in server_start_id:
         cmd = "nova stop %s" % s_stop
         result = shell.shell_cmd(cmd)
         logging.debug("instance[%s] already stopping... "     %s_stop)
         print "instance[%s] already stopping..." % s_stop
Exemple #3
0
 def check_for_jobs(self) -> Box:
     # TODO: Avoid polling by creating a Firestore watch and using a
     #   mutex to avoid multiple threads processing the watch.
     job_query = self.jobs_db.collection.where('instance_id', '==',
                                               self.instance_id).where(
                                                   'status', '==',
                                                   JOB_STATUS_ASSIGNED)
     jobs = list(job_query.stream())
     ret = Box()
     if len(jobs) > 1:
         # We currently only support one job per instance
         raise RuntimeError('Got more than one job for instance')
     elif not jobs:
         log.debug('No job for instance in db')
     else:
         ret = Box(jobs[0].to_dict())
     return ret
Exemple #4
0
 def updated(self) -> bool:
     """
     :return: Whether or not we updated our local repo
     """
     now = time.time()
     if not self.is_on_gcp:
         ret = False
     elif self.last_update_check_time is not None:
         log.debug('Checking for source changes')
         if now - self.last_update_check_time > 3:
             ret = self.pull_latest(now)
             self.last_update_check_time = time.time()
         else:
             ret = False
     else:
         ret = self.pull_latest(now)
         self.last_update_check_time = time.time()
     return ret
 def volume_detach():
     volume_detach_id = []
     server_detach_id = []
     ##############
     # server_detach_id
     cmd_detach_server = "nova volume-list |grep in-use|awk '{print $12}'"
     result_server = shell.shell_cmd(cmd_detach_server)
     for vo_d in xrange(len(result_server)):
         v_d = result_server[vo_d].split()[0]
         server_detach_id.append(v_d)
     #############
     # volume_detach_id
     cmd_detach_volume = "nova volume-list |grep in-use|awk '{print $2}'"
     result_volume = shell.shell_cmd(cmd_detach_volume)
     for so_d in xrange(len(result_volume)):
         s_d = result_volume[so_d].split()[0]
         volume_detach_id.append(s_d)
     stop_list = []
     for e in xrange(len(server_detach_id)):
         fd =  tuple((server_detach_id[e],volume_detach_id[e]))
         stop_list.append(fd)
     #print "SSSSSSTTTTTTOOOOPPP:%s" %stop_list
     #############
     server_list = []
     cmd_list = "nova list |grep SHUTOFF|awk '{print $2}'"
     result_list = shell.shell_cmd(cmd_list)
     for li in xrange(len(result_list)):
         lst = result_list[li].split()[0]
         server_list.append(lst)
     #print "SSSSSSEEEEEEVVVVV:%s" % server_list
     for server,volume in stop_list:
         #print  "kkkkkkkKKKKKKKKKKKKKKKKKKKKKKKKK",server
         if server in server_list:
             cmd = "nova volume-detach %s %s" %(server,volume)
             result = shell.shell_cmd(cmd)
             logging.debug("volume[%s] already detach... "     % volume)
             print "volume[%s] already detach... "     % volume
         else:
             logging.error("instance[%s] is not stop... "   %server)
        def volume_attach():
            volume_file = list_dir+list_file_volume_json
            try:
                with open(volume_file) as f:
                    json_load =json.load(f,encoding="utf-8")
                print "This is json_load:%s" %json_load
                start_list = []
                for j in xrange(len(json_load)):
                    cc = json_load[j]
                    device = str(cc['device'])
                    server = str(cc['server_id'])
                    volume = str(cc['id'])
                    ff = tuple((server,volume,device))
                    start_list.append(ff)
                print "mmmmmmMMMMMMM",start_list
                server_list = []
                ###########
                # server_list
                cmd_server = "nova list |grep SHUTOFF |awk '{print $2}'"
                server_result = shell.shell_cmd(cmd_server)
                for s in xrange(len(server_result)):
                    s_l = server_result[s].split()[0]
                    server_list.append(s_l)
                print "TTTTTTTTTTTTTTTTTTT",server_list
                 
                for server_id,volume_id,device_path in start_list:
                    if server_id in server_list:
                        cmd_v = "nova volume-attach %s %s %s" % (server,volume,device_path)
                        result = shell.shell_cmd(cmd_v)
                        logging.debug(cmd_v)
                        print "volume[%s] already detach... "     % volume_id
                    else:
                         logging.error("instance[%s] is not stop... "   %server_id)

            except IOError:
                logging.error("%s list file not found!!!" %volume_file)
                raise
        except:
            traceback.print_exc()
            logging.error("login failed....")
        finally:
             child.close(force=True) 
             logging.info("login end .......")
    
if __name__ == '__main__':
    while True:
        team0_cmd='fping 192.168.204.2'
        team0_active = shell_cmd(team0_cmd)
        team1_cmd='fping 192.168.203.2'
        team1_active = shell_cmd(team1_cmd)
        print team0_active
        print team1_active
        if 'alive' not in team0_active[0]:
	    port = '17'
            logging.debug("this is team0")
            print "this is tema0"
            rest_team_port(port)
        if 'alive' not in team1_active[0]:
	    port = '12'
            print "this is tema1"
            logging.debug("this is team0")
            rest_team_port(port)
	time.sleep(10)
	continue

		
		
	def tenant_do_work(self,tenant_name,cmd):
		#tenant_name = 'kycloudprod'
		list_dir = '/var/lib/platform-manager/'
		cmd_date = "date  '+%Y-%m-%d'"
		date = shell.shell_cmd(cmd_date)[0].split()[0]
		list_file_nova='list_'+tenant_name+'_'+date
		list_file_volume ='list_'+tenant_name+'_volume'
		list_file_volume_flag =list_file_volume+'_flag'
		list_file_volume_json = list_file_volume+'.json'
		list_file_volume_back = list_file_volume+'.bak'
		if not os.path.exists(list_dir):
    			os.makedirs(list_dir,0o755)
			#os.chdir(list_dir)
		

		os.chdir(home_path)

		#TENANT_NAME_LIST =
		env_set_func(tenant_name)
		logging.info(os.environ.data)
		#####################
		# cmd_list 
		######################
		cmd_list = "nova list"
		#######################
		# cmd_volume_list
		############################
		cmd_volume_list = "nova volume-list"
		######################
		# cmd_stop  
		#######################
		############################
		cmd_stop = []
		if cmd == "stop":
			server_running_id = []
			server_cmd = "nova list |grep Running|awk '{print $2}'"
			cmd_result = shell.shell_cmd(server_cmd)
		#
			for i in xrange(len(cmd_result)):
                		lo = cmd_result[i].split()[0]
                        	server_running_id.append(lo)
		#
		#
			for server_stop in server_running_id:
				cmd_lop = "nova stop %s" %server_stop
				cmd_stop.append(cmd_lop)
			logging.info("this is Server_runing_id: %s" % server_running_id)
			# print "this is Server_runing_id: %s" % server_running_id
		##########################################
		# cmd_start
		#################################
		cmd_start = []
		if cmd == "start":
			server_shutdown_id = []
			server_stop_cmd = " nova list|grep Shutdown |awk '{print $2}'"
			cmd_result_stop = shell.shell_cmd(server_stop_cmd)
		#
			for j in xrange(len(cmd_result_stop)):
				po = cmd_result_stop[j].split()[0]
				server_shutdown_id.append(po)
		
		##
			for server_start in server_shutdown_id:
				cmd_pop = "nova start %s" %server_start
				cmd_start.append(cmd_pop)
		# print "this is Server_shutdown_id: %s" % server_shutdown_id
		####################################
		# cmd_volume_attach
		#######################################
		cmd_volume_attach = []
		if cmd == "volume_attach" :
			start_list = []
			#print "This is json_load:%s" %(list_dir+list_file_volume_json)
			f = open(list_dir+list_file_volume_json)
			json_load =json.load(f,encoding="utf-8")
			f.close()
			print "This is json_load:%s" %json_load
			for j in xrange(len(json_load)):
				cc = json_load[j]
				device = str(cc['device'])
				server_id = str(cc['server_id'])
				volume_id = str(cc['id'])
				ff = tuple((server_id,volume_id,device))
				start_list.append(ff)
			
			for server,volume,device_path in start_list:
				cmd_v = "nova volume-attach %s %s %s" % (server,volume,device_path)
				cmd_volume_attach.append(cmd_v)
			# print "This is server_volume_id: %s" % server_volume_id
			logging.info("This is start_list: %s" % start_list)
		##################################
		# cmd_volume_detach
		###########################
		cmd_volume_detach = []
		if cmd == "volume_detach":
			server_volume_detach_id = []
			volume_detach_id = []
			cmd_detach_server = "nova volume-list |grep in-use|awk '{print $(NF-1)}'"
			cmd_result_server = shell.shell_cmd(cmd_detach_server)
			for de in xrange(len(cmd_result_server)):
				sd = cmd_result_server[de].split()[0]
				server_volume_detach_id.append(sd)
			cmd_detach_volume = "nova volume-list |grep in-use|awk '{print $2}'"
			cmd_result_volume = shell.shell_cmd(cmd_detach_volume)
			for dv in xrange(len(cmd_result_volume)):
				vd = cmd_result_volume[dv].split()[0]
				volume_detach_id.append(vd)
			stop_list = []
			for e in xrange(len(server_volume_detach_id)):
                        	fd = tuple((server_volume_detach_id[e],volume_detach_id[e]))
                        	stop_list.append(fd)
                	for server_volume_01,volume_01 in stop_list:
                        	cmd_d = "nova volume-detach %s %s" % (server_volume_01,volume_01)
                        	cmd_volume_detach.append(cmd_d)
			#print "This is volume_detach_id:%s" % volume_detach_id
			#print "This is server_volume_detach_id:%s" %server_volume_detach_id
		##############################################
		#############################
		# volume_json
		############################
		if cmd == "stop" :
			volume_json_key = []
			#volume_key_cmd = "grep in-use %s |awk '{print $2}'" %(list_dir+list_file_volume)
			volume_key_cmd = "nova volume-list|grep in-use |awk '{print $2}'"
			volume_json_key_result = shell.shell_cmd(volume_key_cmd)
			for k in xrange(len(volume_json_key_result)):
				jk = volume_json_key_result[k].split()[0]
				volume_json_key.append(jk)
			volume_json = []
			for js in xrange(len(volume_json_key)):
				volume_value_cmd = "nova volume-show %s |grep device |awk '{print $4,$5,$6,$7,$12,$13}'" %volume_json_key[js]
				reuslt = shell.shell_cmd(volume_value_cmd)
				reuslt_ev = eval(reuslt[0].split('\n')[0])[0]
				volume_json.append(reuslt_ev)
			# json 
			fp = open(list_dir+list_file_volume_json,'w')
			json.dump(volume_json,fp)
			fp.close()
			logging.info(volume_json)
		
		##############################################
		content_list = []
		cmd_name = {
			"list":cmd_list,
			"stop":cmd_stop,
			"volume_list":cmd_volume_list,
			"start":cmd_start, 
			"volume_attach":cmd_volume_attach,
			"volume_detach":cmd_volume_detach
		}
		print "This is cmdname : %s" % cmd_name
		logging.debug("This is cmd_name:%s" %cmd_name)
		print cmd
		cmd_list = []
		cmd_result = cmd_name.get(cmd)
		if isinstance(cmd_result,str):
			cmd_list.insert(0,cmd_result)
		if isinstance(cmd_result,list):
			cmd_list = cmd_result
		#print "This is cmd_list: %s" % cmd_list
		logging.debug("This is cmd_list: %s" % cmd_list)
		####################
		in_use_check_id = []
		in_use_check_server_cmd = "nova volume-list|grep in-use|awk '{print $(NF-1)}'"
		in_use_check_result = shell.shell_cmd(in_use_check_server_cmd)
		for in_use in xrange(len(in_use_check_result)):
			iu = in_use_check_result[in_use].split()[0]
			in_use_check_id.append(iu)
			 
		for cmd_l in cmd_laist:
			#print "this is cmd_l:%s" % cmd_l
			logging.debug("this is cmd_l:%s" % cmd_l)
			if cmd_l == "nova volume-list":
				content = shell.shell_cmd(cmd_l)
				#content = os.system('nova list')
				back_cmd = "cp -f %s %s" %(list_dir+list_file_volume,list_dir+list_file_volume_back)
				shell.shell_cmd(back_cmd)
				f = open(list_dir+list_file_volume,'w')
				f.writelines(content)
				f.close()
				logging.info(content)
				content_list.append(content)
			else:
				if "nova start" in cmd_l:
					server_id = cmd_l.split()[2] 
					if  server_id in in_use_check_id:  
						content = shell.shell_cmd(cmd_l)
						#content = os.system('nova list')
                                        	f = open(list_dir+list_file_nova,'a')
                                        	f.writelines(content)
                                        	f.close()
                                        	logging.info(content)
                                        	content_list.append(content)

				else:
					content = shell.shell_cmd(cmd_l)
					#content = os.system('nova list')
					f = open(list_dir+list_file_nova,'a')
					f.writelines(content)
					f.close()
					logging.info(content)
					content_list.append(content)
		#print "This is content: %s" % content
		time.sleep(10)
		print "\033[1;31;40m Tenant: %s perform the %s action ,please later.....\033[0m" % (tenant_name,cmd)
		##############################
		# check_result_instance and check_result_volume
		##################################
		# check_result_instance
		cmd_check = "nova list |awk '{print $6}'|grep -v Status|grep -v ^$"
		check_cmd_result = shell.shell_cmd(cmd_check)
		check_result = []
		for c in xrange(len(check_cmd_result)):
			check = check_cmd_result[c].split()[0]
			check_result.append(check)
		num = len(check_result)
		###########################3
		# check_result_volume
		cmd_check_volume = "nova volume-list|grep -v Status|awk '{print $4}'|grep -v ^$"
		check_volume_result = shell.shell_cmd(cmd_check)
		check_volume_result = []
		for v in xrange(len(check_volume_result)):
			check_v = check_cmd_result[v].split()[0]
			check_volume_result.append(check_v)
		num_volume = len(check_volume_result)
		#print "I'am herer"
		if cmd == "stop" or cmd == "volume_detach":
			if num == check_result.count("SHUTOFF") and num_volume == check_volume_result.count("available"):
				logging.info("%s %s" %(check_result,check_volume_result))
				logging.info("Tenant: %s All instance stop successfully!" %tenant_name)
				logging.info("Tenant: %s All volume deattch successfully!" %tenant_name)
				print "\033[1;31;40m Tenant: %s all instance stop successfully!\033[0m" % tenant_name
				print "\033[1;31;40m Tenant: %s all volume deattch successfully!\033[0m" % tenant_name
			elif num == check_result.count("SHUTOFF"):
				logging.info(check_result)
				logging.info("Tenant: %s All instance stop successfully!" %tenant_name)
				print "\033[1;31;40m Tenant: %s all instance stop successfully!\033[0m" % tenant_name
			elif 0 == check_result.count("SHUTOFF"):
				logging.info(check_result)
				logging.critical("Tenant: %s All instance stop failure!" %tenant_name)
			elif num != check_result.count("SHUTOFF"):
				logging.info(check_result)
				logging.error("Tenant:%s All your stoped operating withou success!" %tenant_name)
			elif num_volume == check_volume_result.count("available"):
				logging.info(check_volume_result)
				logging.info("Tenant: %s All volume deattch successfully!" %tenant_name)
				print "\033[1;31;40m Tenant: %s all volume deattch successfully!\033[0m" % tenant_name
			elif 0 == check_volume_result.count("available"):
				logging.info(check_volume_result)
				logging.critical("Tenant: %s All volume deattch failure!" %tenant_name)
			elif num != check_result.count("SHUTOFF") or num_volume != check_volume_result.count("available"):
				logging.info("There are info %s,%s" %(check_volume_result,check_result))
				logging.error("Tenant:%s All your stoped operating withou success!" %tenant_name)
		if cmd == "start":
			#######################################
			if num == check_result.count("ACTIVE"):
				logging.info(check_result)
				logging.info("Tenant:%s All instance started successfully!" %tenant_name)
				print "\033[1;31;40m Tenant:%s All instance started successfully!\033[0m" %tenant_name
			elif 0 == check_result.count("ACTIVE"):
				logging.info(check_result)
				logging.critical("Tenant: %s All instance start failure!" %tenant_name)
			elif num != check_result.count("ACTIVE"):
				logging.info(check_result)
				logging.error("Tenant:%s All your started operating withou success!" %tenant_name)
		if cmd == "volume_attach":
			check_volume_name_old = []
			cmd_volume_name_old ="grep  in-use %s |awk '{print $2}'" %(list_file_volume)
			name_old_result = shell.shell_cmd(cmd_volume_name_old)
			for o in xrange(len(name_old_result)):
				vo = name_old_result[o].split()[0]
				check_volume_name_old.append(vo)
			check_volume_name = []
			cmd_volume_name ="nova volume-list |grep in-use|awk '{print $2}'"
			name_result = shell.shell_cmd(cmd_volume_name)
			for l in xrange(len(name_result)):
				vl =  name_result[l].split()[0]
				check_volume_name.append(vl)
			#######################################
			if len(check_volume_name) == len(check_volume_name_old):
				logging.info((check_volume_result))
				logging.info("Tenant:%s All volume attach successfully!" %tenant_name)
				print "\033[1;31;40m Tenant:%s All volume attach successfully!\033[0m" %tenant_name
				flag = open(list_dir+list_file_volume_flag,'w')
				flag.writelines('0')
				flag.close()
				return 0
			elif len(check_volume_name) == 0:
				logging.info(check_volume_result)
				logging.critical("Tenant: %s All volume attach failure!" %tenant_name)
				flag = open(list_dir+list_file_volume_flag,'w')
				flag.writelines('1')
				flag.close()
				return 1
			else:
				logging.info(check_volume_result)
				logging.error("Tenant:%s All your started operating withou success!" %tenant_name)
				flag = open(list_dir+list_file_volume_flag,'w')
				flag.writelines('-1')
				flag.close()
				return -1
        def instance_start():
            check_old_id = []
            check_id = []
            server_shutdown_id = []
            cmd_stop = "nova list|grep Shutdown |awk '{print $2}'"
            result_stop = shell.shell_cmd(cmd_stop)
            for j in xrange(len(result_stop)):
                pstop = result_stop[j].split()[0]
                server_shutdown_id.append(pstop)
            ########################
            # instnace_id_list
            #################################
            instance_id_list = server_shutdown_id
            #####################
            # volume_id_list
            ##########################
            volume_id_list = []
            volume_list = list_dir+list_file_volume
            cmd_volume = "grep in-use %s |awk '{print $12}'" %volume_list
            result_volume = shell.shell_cmd(cmd_volume)
            for v in xrange(len(result_volume)):
                v_volume = result_volume[v].split()[0]
                volume_id_list.append(v_volume)
            #############################
            # instnace_id_list and volume_id_list difference
            #########################
            difference=set(instance_id_list).difference(set(volume_id_list))
            # No instances of mount cloud drive
            for di in difference:
                cmd_start = "nova start %s" % di
                result_start = shell.shell_cmd(cmd_start)
                logging.debug("instance[%s no mount cloud drive] already starting..." %di )
                print "instance[%s no mount cloud drive] already starting...     " %di
                    

            if server_shutdown_id:
                if os.path.exists(volume_list) and not os.path.getsize(volume_list):
                    for s_start in server_shutdown_id:
                        # old volume id
                        check_cmd_old = "grep %s |awk '{print $2}'" % s_start
                        check_old_result = shell.shell_cmd(check_cmd_old)
                        for x in xrange(len(check_old_result)):
                            old = check_old_result[x].split()[0]
                            check_old_id.append(old)
                        # current volume id
                        check_cmd = "nova volume-list |grep %s|awk '{print $2}'" % s_start
                        check_result = shell.shell_cmd(check_cmd)
                        for y in xrange(len(check_result)):
                            current = check_result[y].split()[0]
                            check_id.append(current)
                        # intersection
                        check_intersection = set(check_old_oid).intersection(set(check_id))
                            
                        
                        for id in check_intersection:
                             
                            cmd = "nova start %s" % s_start
                            result = shell.shell_cmd(cmd)
                            logging.debug("instance[%s] already starting... " %s_start )
                            print "instance[%s] already starting... " %s_start 
                else:
                    logging.warning("%s:%s file does not exit or is empty,without the basis of a instance to start!!!" %(tenant_name,volume_list))
            else:
                logging.warning("%s:Instace information is empty,please check the relevant configuration" %tenant_name) 
 def drop_course(self, section):
     if section not in self._student.enrolled_courses:
         log.debug(
             "Attempted to drop a course that student was not enrolled in.")
     else:
         self._student.enrolled_courses.remove(section)