简介

作为提供私有云服务的第三方,在为甲方部署服务的时候,对需要对方提供机器资源,但是对方有可能不会直接提供一批机器的访问权限,有可能是给几台物理机,安装了vsphere,需要我们自己去部署vm并且安装系统。那么这样为我们的工作又带来了一份调整,如何去规划虚拟机呢?

这个脚本就是干这个事儿的。

info.txt是我们服务的一些信息,包括是啥服务,需要部署几个等等信息。 analysis.py是用来进行分析并提供

analysis.py

#coding:utf-8

import json

# 一些信息,我们的instance类型,以及对方给我们的机器的类型,看看我们机器应该如何规划
instance_types = {
    't2.micro' : {
        'cpu': 1,
        'mem': 1
    },
    't2.medium': {
        'cpu':2,
        'mem':4
    },
    'c4.xlarge':{
        'cpu': 4,
        'mem': 7.5
    },
    't2.xlarge':{
        'cpu': 4,
        'mem': 16
    }
}

mental_server_type = {
    'cpu': 12,
    'mem': 55
}

# target,初始化一百台服务器
machines = []
for i in range(50):
    a = {}
    a['cpuused'] = 0
#coding:utf-8

import json

# 一些信息,我们的instance类型,以及对方给我们的机器的类型,看看我们机器应该如何规划
instance_types = {
    't2.micro' : {
        'cpu': 1,
        'mem': 1
    },
    't2.medium': {
        'cpu':2,
        'mem':4
    },
    'c4.xlarge':{
        'cpu': 4,
        'mem': 7.5
    },
    't2.xlarge':{
        'cpu': 4,
        'mem': 16
    }
}

mental_server_type = {
    'cpu': 12,
    'mem': 55
}

# target,初始化一百台服务器
machines = []
for i in range(50):
    a = {}
    a['cpuused'] = 0
    a['memused'] = 0
    a['yw'] = []
    machines.append(a)

def main():
    with open("./info.txt") as f:
        for line in f:
            #print line
            info = line.replace("\r", "").replace("\n", "").split(",")
            yw_name = info[0]
            yw_instance_type = info[1]
            yw_instance_count = int(info[6])
            print yw_name, yw_instance_count, yw_instance_type, instance_types[yw_instance_type]['cpu'], instance_types[yw_instance_type]['mem']
            instance_used_cpu = instance_types[yw_instance_type]['cpu']
            instance_used_mem = instance_types[yw_instance_type]['mem']
            for number in range(0,yw_instance_count):
                for machine in machines:
                    if machine['cpuused'] + instance_used_cpu < mental_server_type['cpu'] and machine['memused'] + instance_used_mem < mental_server_type['mem'] and yw_name + "_" + str(instance_used_cpu) +"_" + str(instance_used_mem) not in machine['yw']:
                        machine['yw'].append(yw_name + "_" + str(instance_used_cpu) +"_" + str(instance_used_mem) )
                        machine['cpuused'] += instance_used_cpu
                        machine['memused'] += instance_used_mem
                        break
main()

# 直接打印
for machine in machines:
    if machine['memused'] !=0:
        print machine

# 详细打印

cpu_used_total = 0
mem_used_total = 0
instance_total = 0
for machine in machines:
    if machine['memused'] !=0:
        cpu_used_total += machine['cpuused']
        mem_used_total += machine['memused']
        instance_total += 1
print """一共消耗cpu: %s, mem: %s, instnaces: %s"""%(cpu_used_total,mem_used_total,instance_total)    a['memused'] = 0
    a['yw'] = []
    machines.append(a)

def main():
    with open("./info.txt") as f:
        for line in f:
            #print line
            info = line.replace("\r", "").replace("\n", "").split(",")
            yw_name = info[0]
            yw_instance_type = info[1]
            yw_instance_count = int(info[6])
            print yw_name, yw_instance_count, yw_instance_type, instance_types[yw_instance_type]['cpu'], instance_types[yw_instance_type]['mem']
            instance_used_cpu = instance_types[yw_instance_type]['cpu']
            instance_used_mem = instance_types[yw_instance_type]['mem']
            for number in range(0,yw_instance_count):
                for machine in machines:
                    if machine['cpuused'] + instance_used_cpu < mental_server_type['cpu'] and machine['memused'] + instance_used_mem < mental_server_type['mem'] and yw_name + "_" + str(instance_used_cpu) +"_" + str(instance_used_mem) not in machine['yw']:
                        machine['yw'].append(yw_name + "_" + str(instance_used_cpu) +"_" + str(instance_used_mem) )
                        machine['cpuused'] += instance_used_cpu
                        machine['memused'] += instance_used_mem
                        break
main()

# 直接打印
for machine in machines:
    if machine['memused'] !=0:
        print machine

# 详细打印

cpu_used_total = 0
mem_used_total = 0
instance_total = 0
for machine in machines:
    if machine['memused'] !=0:
        cpu_used_total += machine['cpuused']
        mem_used_total += machine['memused']
        instance_total += 1
print """一共消耗cpu: %s, mem: %s, instnaces: %s"""%(cpu_used_total,mem_used_total,instance_total)

info.txt

service1,t2.micro,30,us-west-2,ami-0dde2e6d,limm,1,22_10060_10055,public
service2,t2.micro,30,us-west-2,ami-1712d877,limm,1,3389,public
service3,t2.medium,30,us-west-2,ami-0dde2e6d,limm,2,22_10060_10055,public
service4,t2.medium,50,us-west-2,ami-0dde2e6d,limm,2,22__11086__10086,public
service5,t2.medium,50,us-west-2,ami-0dde2e6d,limm,2,10088__22__7779__6779__6879__3141,public
Copyright © opschina.org 2017 with zzlyzq@gmail.com all right reserved,powered by Gitbook该文件修订时间: 2017-07-11 11:32:48

results matching ""

    No results matching ""