azure sdk, cli 정리

문주은·2022년 7월 7일
0

1. Resource Group

1-1. RG 생성

🖥 python sdk

def create_resource_group(self, user_id):
    # Resource group configuration 설정 셋팅
    resource_group_name = user_id + '-RG'

    # Resource group 을 생성합니다.
    rg_result = self.resource_client.resource_groups.create_or_update(
        resource_group_name,
        {
            "location": self.location
        }
    )
    return rg_result

🖥 azure cli

az group create \
  --name demoResourceGroup \
  --location westus

1-2. RG name 읽기

🖥 python sdk

def get_resource_group(self, user_id):
    # Resource group configuration 설정 셋팅
    resource_group_name = user_id + '-RG'
    BODY = {'resources': ['*']}

    # user의 Resource group 가져와서 사용
    rg_result = self.resource_client.resource_groups.begin_export_template(
        resource_group_name,
        BODY
    ).result()
    print(f'Get resource group : {rg_result.name}')

1-3. RG 나열

🖥 azure cli

az group list # 구독에서 리소스 그룹 모두 나열
az group show --name exampleGroup # 하나의 리소스 그룹 나열

1-4. 리소스 그룹 삭제

🖥 azure cli

az group delete --name exampleGroup

2. Storage

2-1. Storage 생성

🖥 python sdk

 def create_storage(self, rg_result, user_id):
    # storage 이름은 숫자 및 소문자만 사용해아하고 unique 해야함 (대문자나 특수문자 사용 불가)
    # Create a storage account
    storage_account_name = user_id + 'storage'
    storage_async_operation = self.storage_client.storage_accounts.begin_create(
      rg_result.name,
      storage_account_name,
      StorageAccountCreateParameters(
        sku=Sku(name=SkuName.standard_ragrs),
        kind=Kind.storage,
        location=self.location,
        enable_https_traffic_only=True
      )
    )
    storage_account = storage_async_operation.result()
    return storage_account

🖥 azure cli

az storage account create \
    --name <storage-account> \
    --resource-group <resource-group> \
    --location <location> \
    --sku Standard_ZRS \
    --encryption-services blob

2-2. Storage name 읽기

🖥 python sdk

def get_storage(self, rg_result, user_id):
    storage_account_name = user_id + 'storage'
    storage_account = self.storage_client.storage_accounts.get_properties(
        rg_result.name,
        storage_account_name
    )
    return storage_account

3. Blob Container

3-1. Blob container 생성

🖥 python sdk

def create_blob_container(self, storage_account, rg_result):
        # Create a blob container
        keys = self.storage_client.storage_accounts.list_keys(rg_result.name, storage_account.name)
        account_key = keys.keys[0].value

        block_blob_service = BlockBlobService(storage_account.name, account_key)
        container_name = 'dataset-name'
        block_blob_service.create_container(container_name) # container 생성
        return block_blob_service

🖥 azure cli

az storage container create \
    --account-name <storage-account> \
    --name <container> \
    --auth-mode login

3-2. Blob container에 Blob Upload

🖥 python sdk

file_list = os.listdir(file_path)
for file in file_list:
    # container에 blob upload
    block_blob_service.create_blob_from_path(
        container_name,
        file,
        os.path.join(file_path, file)
    )

🖥 azure cli

az storage blob upload \
    --account-name <storage-account> \
    --container-name <container> \
    --name helloworld \
    --file helloworld \
    --auth-mode login

3-3. 컨테이너의 Blob 나열

🖥 python sdk

def show_list_blob(self, storage_account, rg_result):
    keys = self.storage_client.storage_accounts.list_keys(rg_result.name, storage_account.name)
    account_key = keys.keys[0].value
    block_blob_service = BlockBlobService(storage_account.name, account_key)
    container_name = 'dataset-name'
    generator = block_blob_service.list_blobs(container_name)
    
    # Blob 이름 나열
    for blob in generator:
        print('\t Blob name: ' + blob.name)
      
    # 해당 blob의 container 나열
    imgs = []
    for blob in generator:
        print(f'blob : {blob.name}')
        blob_bytes = block_blob_service.get_blob_to_bytes(container_name, blob.name)
        blob_array = np.fromstring(blob_bytes.content, dtype='uint8')
        imgs.append(cv2.imdecode(blob_array, cv2.IMREAD_UNCHANGED)) # decode the array into an image
        stacked_imgs = np.stack(imgs)
        print(stacked_imgs)

🖥 azure cli

az storage blob list \
    --account-name <storage-account> \
    --container-name <container> \
    --output table \
    --auth-mode login

3-4. Blob 다운로드

🖥 azure cli

az storage blob download \
    --account-name <storage-account> \
    --container-name <container> \
    --name helloworld \
    --file ~/destination/path/for/file \
    --auth-mode login

4. VM

4-1. VM 생성

🖥 azure cli

az vm create \
    --name vmname\
    --resource-group rgname \
    --admin-username azureuser \
    --admin-password tensorflow123!

🖥 python sdk

compute_client = ComputeManagementClient(credentials, subscription_id)
network_client = NetworkManagementClient(credentials, subscription_id)

# VM 생성에 필요한 NIC 생성
def create_nic(network_client):
    """Create a Network Interface for a VM.
    """
    # Create VNet (Virtual Network)
    async_vnet_creation = network_client.virtual_networks.begin_create_or_update(
        GROUP_NAME,
        VNET_NAME,
        {
            'location': LOCATION,
            'address_space': {
                'address_prefixes': ['10.0.0.0/16']
            }
        }
    )
    async_vnet_creation.wait()

    # Create Subnet
    async_subnet_creation = network_client.subnets.begin_create_or_update(
        GROUP_NAME,
        VNET_NAME,
        SUBNET_NAME,
        {'address_prefix': '10.0.0.0/24'}
    )
    subnet_info = async_subnet_creation.result()
    
    # Create IP address
    async_ip_creation = network_client.public_ip_addresses.begin_create_or_update(
        GROUP_NAME,
        IP_NAME,
        {
            'location': LOCATION,
            'sku': {"name": "Standard"},
            "public_ip_allocation_method": "Static",
            "public_ip_address_version" : "IPV4"
        }
    )
    ip_info = async_ip_creation.result()

    # Create NIC
    async_nic_creation = network_client.network_interfaces.begin_create_or_update(
        GROUP_NAME,
        NIC_NAME,
        {
            'location': LOCATION,
            'ip_configurations': [{
                'name': IP_CONFIG_NAME,
                'subnet': { 'id': subnet_info.id },
                'public_ip_address': {'id': ip_info.id}
            }]
        }
    )
    return async_nic_creation.result()

nic = create_nic(network_client) 

VM_NAME = "myVM"
USERNAME = "jueun"
PASSWORD = "tensorflow123!@#"

# VM 생성에 필요한 파라미터
VM_PARAMETERS={
    'location': LOCATION,
    'os_profile': {
        'computer_name': VM_NAME,
        'admin_username': USERNAME,
        'admin_password': PASSWORD
    },
    'hardware_profile': {
        'vm_size': 'Standard_DS1_v2'
    },
    'storage_profile': {
        'image_reference': {
            'publisher': 'Canonical',
            'offer': 'UbuntuServer',
            'sku': '18.04-LTS', # 리눅스 우분투 버전(우분투 18-LTS)
            'version': 'latest'
        },
    },
    'network_profile': {
        'network_interfaces': [{
            'id': nic.id,
        }]
    },
}

vm_result = compute_client.virtual_machines.begin_create_or_update(
    RESOURCE_GROUP_NAME, VM_NAME, VM_PARAMETERS)

4-2. 실행중인 VM 리스트 나열

🖥 azure cli

!az vm list -d -o table

4-3. VM stop

🖥 python sdk

vm_stop = compute_client.virtual_machines.begin_power_off(
    RESOURCE_GROUP_NAME, VM_NAME)

🖥 azure cli

az vm stop --name myVM --resource-group rgname

4-4. VM 리소스 삭제

🖥 python sdk

vm_delete = compute_client.virtual_machines.begin_delete(RESOURCE_GROUP_NAME, VM_NAME)
vm_delete.wait()
profile
Data Engineer

0개의 댓글