def write_to_ct(port, service_name, project_name, username): machines = database_update.get_machines() for machine in machines: cli = Client(machine, config.c_version).client tt = cli.exec_create(container='nginx', cmd='/bin/bash -c \"cd /etc/consul-templates && sh refresh.sh %s %s %s %s\"' % ( port, service_name, project_name, username)) cli.exec_start(exec_id=tt, detach=True)
def delete_from_ct(project_name, username): machines = database_update.get_machines() for machine in machines: cli = Client(machine, config.c_version).client tt = cli.exec_create(container='nginx', cmd='/bin/bash -c \"cd /etc/consul-templates && bash delete.sh %s-%s\"' % ( project_name, username)) cli.exec_start(exec_id=tt, detach=True)
def machine_monitor(): machines = database_update.get_machines() info = [] for machine in machines: ip = machine.split(":")[0] url = 'http://' + ip + ":8080/api/v1.2/containers" # url = 'http://114.212.189.147:8080/api/v1.2/containers' response = requests.get(url) true = True false = False di = eval(response.text) cur = di['stats'][-1] pre = di['stats'][-2] gap = time_gap(pre['timestamp'], cur['timestamp']) core = int(di['spec']['cpu']['mask'].split("-")[1]) + 1 print(core) print(gap) print( float(cur['cpu']['usage']['total']) - float(pre['cpu']['usage']['total'])) dic = { 'mem_usage': cur['memory']['usage'], 'mem_total': di['spec']['memory']['limit'], 'cpu_usage': (float(cur['cpu']['usage']['total']) - float(pre['cpu']['usage']['total'])) / (gap * core), 'timestamp': cur['timestamp'], 'ip': ip } usage = 0 total = 0 for item in cur['filesystem']: usage += item['usage'] total += item['capacity'] # file_dic = {'filesystem_usage': item['usage'], # 'filesystem_total': item['capacity']} # files.append(file_dic) dic['filesystem_usage'] = usage dic['filesystem_total'] = total info.append(dic) return info
def write_to_ct(port, service_name, project_name, username): machines = database_update.get_machines() for machine in machines: cli = Client(machine, config.c_version).client tt = cli.exec_create( container='nginx', cmd= '/bin/bash -c \"cd /etc/consul-templates && sh refresh.sh %s %s %s %s\"' % (port, service_name, project_name, username)) cli.exec_start(exec_id=tt, detach=True)
def delete_from_ct(project_name, username): machines = database_update.get_machines() for machine in machines: cli = Client(base_url=machine, version=config.c_version) tt = cli.exec_create( container='nginx', cmd= '/bin/bash -c \"cd /etc/consul-templates && bash delete.sh %s-%s\"' % (project_name, username)) cli.exec_start(exec_id=tt, detach=True)
def random_schedule(): machines = database_update.get_machines() # for service in service_list: # if 'host' not in service: # index = random.randint(0, len(machines)-1) # print index # machine = database_update.get_machine(index) # service['host'] = machine # return service_list index = random.randint(0, len(machines)-1) machine = database_update.get_machine(index) return machine
def random_schedule(): machines = database_update.get_machines() # for service in service_list: # if 'host' not in service: # index = random.randint(0, len(machines)-1) # print index # machine = database_update.get_machine(index) # service['host'] = machine # return service_list index = random.randint(0, len(machines) - 1) machine = database_update.get_machine(index) return machine
def from_dict(cls, username, name, service_dicts): project = cls(name, []) # for srv_dict in service_dicts: # if 'container_name' not in srv_dict: # srv_dict['container_name'] = srv_dict['name'] # srv_dict['hostname'] = srv_dict['container_name'] + config.split_mark + name + config.split_mark + username for srv_dict in service_dicts: if 'command' in srv_dict: command = srv_dict['command'] if "{{" in command: for s_dict in service_dicts: before = s_dict['name'] after = before + config.split_mark + name + config.split_mark + username before = "{{" + before + "}}" command = command.replace(before, after) srv_dict['command'] = command for service_dict in sort_service_dicts(service_dicts): log.info('from_dicts service_dict: %s', service_dict) # container_name = service_dict['container_name'] # service_dict['name'] = service_dict['name'] + config.split_mark + name + config.split_mark + username # service_dict['container_name'] = service_dict['container_name'] + config.split_mark + name + config.split_mark + username # if 'ports' in service_dict: # service_dict['ports'].append('4200') # else: # ports = ['4200'] # service_dict['ports'] = ports log.info(service_dict) print service_dict client_list = database_update.get_machines() vv = None if 'volumes' in service_dict: vv = Volume(service_dict['volumes']) # if 'host' in service_dict: # if service_dict['host'] == 'all': # no = 0 # for client in client_list: # cc = Client(client, config.c_version) # project.services.append( # Service( # name=service_dict['name'], # client=cc, # project=name, # username=username, # network=Network(service_dict['network']), # volume=vv, # options=service_dict # ) # ) # database_update.create_service(username, name, service_dict['name'], service_dict, service_dict['scale']) # no += 1 # return project # else: # ip = service_dict['host'] # else: # # orchestration algorithm # # index = random.randint(0, 1) # print 'no schedule' if 'port' in service_dict: write_to_ct(service_dict['port'], service_dict['name'], name, username) env = [] if 'environment' in service_dict: env = service_dict['environment'] env.append('SERVICE_NAME=' + service_dict['name'] + '-' + name + "-" + username) service_dict['environment'] = env if 'scale' not in service_dict: service_dict['scale'] = 1 database_update.create_service(username, name, service_dict['name'], service_dict, service_dict['scale']) project.services.append( Service(name=service_dict['name'], project=name, username=username, network=Network(service_dict['network']), volume=vv, options=service_dict)) return project
def from_dict(cls, username, name, service_dicts): project = cls(name, []) # for srv_dict in service_dicts: # if 'container_name' not in srv_dict: # srv_dict['container_name'] = srv_dict['name'] # srv_dict['hostname'] = srv_dict['container_name'] + config.split_mark + name + config.split_mark + username for srv_dict in service_dicts: if 'command' in srv_dict: command = srv_dict['command'] if "{{" in command: for s_dict in service_dicts: before = s_dict['name'] after = before + config.split_mark + name + config.split_mark + username before = "{{" + before + "}}" command = command.replace(before, after) srv_dict['command'] = command for service_dict in sort_service_dicts(service_dicts): log.info('from_dicts service_dict: %s', service_dict) # container_name = service_dict['container_name'] # service_dict['name'] = service_dict['name'] + config.split_mark + name + config.split_mark + username # service_dict['container_name'] = service_dict['container_name'] + config.split_mark + name + config.split_mark + username # if 'ports' in service_dict: # service_dict['ports'].append('4200') # else: # ports = ['4200'] # service_dict['ports'] = ports log.info(service_dict) print service_dict client_list = database_update.get_machines() vv = None if 'volumes' in service_dict: vv = Volume(service_dict['volumes']) # if 'host' in service_dict: # if service_dict['host'] == 'all': # no = 0 # for client in client_list: # cc = Client(client, config.c_version) # project.services.append( # Service( # name=service_dict['name'], # client=cc, # project=name, # username=username, # network=Network(service_dict['network']), # volume=vv, # options=service_dict # ) # ) # database_update.create_service(username, name, service_dict['name'], service_dict, service_dict['scale']) # no += 1 # return project # else: # ip = service_dict['host'] # else: # # orchestration algorithm # # index = random.randint(0, 1) # print 'no schedule' if 'port' in service_dict: write_to_ct(service_dict['port'], service_dict['name'], name, username) env = [] if 'environment' in service_dict: env = service_dict['environment'] env.append('SERVICE_NAME=' + service_dict['name'] + '-' + name + "-" + username) service_dict['environment'] = env if 'scale' not in service_dict: service_dict['scale'] = 1 database_update.create_service(username, name, service_dict['name'], service_dict, service_dict['scale']) project.services.append( Service( name=service_dict['name'], project=name, username=username, network=Network(service_dict['network']), volume=vv, options=service_dict)) return project