본문 바로가기

Language/Python

Openstack Instance 자동 생성 스크립트

작년 여름때 인턴을 하면서 제작한 xml인데 기존 회사에서 쓰던 xml을 수정해여 사용해서 아직 처음부터 만들 순 없다....

<host>를 2개로 나눈 이유는 생성할 인스턴스들이 각각 다른 프로젝트에 생성 시켜야하기 때문에 2개로 만들었다.

3개의 프로젝트에 생성 하려면 1개 더 만들어서 사용하면 된다.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE CyberAegis[
<!ELEMENT vCenter (#PCDATA|copy)*>
    <!ATTLIST vCenter auth_url CDATA #IMPLIED>
    <!ATTLIST vCenter project_name CDATA #REQUIRED>
    <!ATTLIST vCenter username CDATA #REQUIRED>
    <!ATTLIST vCenter password CDATA #REQUIRED>
    <!ATTLIST vCenter user_domain_id CDATA #REQUIRED>
    <!ATTLIST vCenter project_domain_id CDATA #REQUIRED>
    <!ELEMENT copy (#PCDATA)>
        <!ATTLIST copy instance_name CDATA #REQUIRED>
        <!ATTLIST copy image_name CDATA #REQUIRED>
        <!ATTLIST copy network CDATA #REQUIRED>
        <!ATTLIST copy flavor CDATA #REQUIRED>
        <!ATTLIST copy volume_size CDATA #REQUIRED>
        <!ATTLIST copy security_group CDATA #REQUIRED>
]>
<vCenter>
    <host
        auth_url="http://192.168.214.120:5000/v3"
        project_name="2-Team"
        username="user02"
        password="user02"
        user_domain_id="default"
        project_domain_id="default"
    >
        <copy instance_name="2-Team:instance-01" image_name="New-CentOS7-Minimal-origin.qcow2" network="private-01,10.0.1.20" flavor="m1.small" volume_size="10" security_group="default"> </copy>
        <copy instance_name="2-Team:instance-02" image_name="New-CentOS7-Minimal-origin.qcow2" network="private-02" flavor="m1.small" volume_size="10" security_group="default"> </copy>
    </host>

    <host
        auth_url="http://192.168.214.120:5000/v3"
        project_name="1-Team"
        username="user01"
        password="user01"
        user_domain_id="default"
        project_domain_id="default"
    >
        <copy instance_name="1-Team:instance-01" image_name="New-CentOS7-Minimal-origin.qcow2" network="private-01,10.0.1.20" flavor="m1.small" volume_size="10" security_group="default"> </copy>
        <copy instance_name="1-Team:instance-02" image_name="New-CentOS7-Minimal-origin.qcow2" network="private-02" flavor="m1.small" volume_size="10" security_group="default"> </copy>
    </host>

</vCenter>

 

만들 인스턴스가 많아지면 오래걸리기 때문에 쓰레드와 클래스를 이용하여 속도를 높였다.

그리고 첫번째 인자값에 '0'을 입력하면 실행되는 명령어가 출력이 되고,

'1'을 입력하면 명령어가 실행된다.

#!/bin/env python3

### COPY INSTANCE ###

import os,sys,subprocess
from threading import Thread
from xml.etree.ElementTree import parse

class copy_instance(Thread) :
    def __init__(self,instance_name,image_name,flavor,volume_size,sg,network) :
        Thread.__init__(self)

        self.instance_name=instance_name
        self.image_name=image_name
        self.flavor=flavor
        self.volume_size=volume_size
        self.sg=sg
        self.network=network

    def run(self) :
        def instance_create(self,cmd) :
            if sys.argv[1] == "0" :
                print(cmd)
            elif sys.argv[1] == "1" :
                os.system(cmd)
            else :
                print("Argument Error")
                exit()

        if len(self.network) == 2 :
            cmd = (f"openstack server create {self.instance_name} --image {self.image_name} --flavor {self.flavor} --security-group {self.sg} --boot-from-volume {self.volume_size} --nic net-id={self.network[0]},v4-fixed-ip={self.network[1]}")

        else :
            cmd = (f"openstack server create {self.instance_name} --image {self.image_name} --flavor {self.flavor} --security-group {self.sg} --boot-from-volume {self.volume_size} --network {self.network[0]}")
        Thread(target=instance_create, args=(self,cmd)).start()



xml_file = "/root/openstack.d/copy_instance.d/Copy_instance_Info.xml"
tree = parse(xml_file)
vcenter = tree.getroot()
hosts = vcenter.getchildren()

for host in hosts :
    os.environ['OS_AUTH_URL'] = str(host.get("auth_url"))
    os.environ['OS_PROJECT_NAME']=str(host.get("project_name"))
    os.environ['OS_USER_DOMAIN_NAME']=str(host.get("user_domain_id"))
    os.environ['OS_PROJECT_DOMAIN_ID']=str(host.get("project_domain_id"))
    os.environ['OS_USERNAME']=str(host.get("username"))
    os.environ['OS_PASSWORD']=str(host.get("password"))

    instances = host.getchildren()
    for instance_info in instances :
        instance_name = str(instance_info.get("instance_name"))
        image_name = str(instance_info.get("image_name"))
        flavor = str(instance_info.get("flavor"))
        volume_size = str(instance_info.get("volume_size"))
        sg = str(instance_info.get("security_group"))
        network = str(instance_info.get("network")).split(',')

        copy_instance(instance_name,image_name,flavor,volume_size,sg,network).start()

'Language > Python' 카테고리의 다른 글

openstack Openrc File 자동등록 스크립트  (0) 2021.06.09
heat YAML파일로 인스턴스 자동 생성  (0) 2021.05.20