Oracle Cloud remote batch creation of virtual machines

Source: Internet
Author: User

Objective

Due to the company's business needs, we need to create virtual machines in bulk to perform tasks that need to be destroyed after the task is run to conserve resources.
The way we create virtual in bulk is through the bash interface provided by Oracle Cloud, so our first step is to create a bash environment

One. Configure command line Interface on the Linux machine. 1. Python version number is greater than or equal to 2.7

[email protected] ~]$ python--version

2. Install the user with sudo permission

[Email protected] ~]# Visudo

Meet the criteria

2. Download and install the CLI

[Email protected] ~]$ curl-l "https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh" | Bash

View

3. Auto-Generate config CLI configuration file

This configuration file is used for authentication

Get user ocid:

Get tenant Ocid

4. Update the user key

To view the location of the key placement:

View Key Contents

Add encryption Key

4. Test whether the configuration is successful

Two. Batch deployment of node-machine scripts
#!/bin/bash# description:batch Create Oracle Cloud Virtual instance.# date:2017/11/22# Author:lirou<[email pro tected]># version:1.0.1##### set some variables. Error_create=2error_no_instance=3ierror_parameter=4#node Host Global variablesoci_path=/root/y/ocicompartment_id= Ocid1.tenancy.oc1tenancy_id=ocid1.tenancy.oc1subnet_id=ocid1.subnet.oc1.phximage_id=ocid1.image.oc1.phxshape= " Vm. Standard1.1 "#record node host create and delete variables.file_of_alived_node=/var/lib/oracle/alived.nodesfile_of_ip _number=/var/lib/oracle/ip.txtfile_of_create_node_log=/var/log/oracle/create.logfile_of_delete_node_log=/var/ log/oracle/delete.log### Make sure file is existence. [!-D $ (dirname $file _of_alived_node)] && mkdir $ (dirname $file _of_alived_node) >>/dev/null[!-D $ (Dirnam e $file _of_ip_number)] && mkdir $ (dirname $file _of_ip_number) >>/dev/nulltouch $file _of_ip_number[!-D $ ( DirName $file _of_create_node_log)] && mkdir $ (dirname $file _of_create_nOde_log) >>/dev/null[!-D $ (dirname $file _of_delete_node_log)] && mkdir $ (dirname $file _of_delete_node_ LOG) >>/dev/null#### create node host### usage:create_node instance_display_name instance_private_ip volume_ Display_name volume_size_in_mbs attachment_display_namefunction Create_node {#Create instance instance_id=$ ($ Oci_path Compute Instance Launch--availability-domain $avail _domain-c $compartment _id--image-id $image _id--shape $sha PE--display-name $--subnet-id $subnet _id--private-ip $ | grep "\" id\ "" | Cut-d "\" "-f 4) if [[-Z $instance _id]];then echo" [$ (date + '%F%T ')] [instance] [$1:$2] [Create failure] [ex It ...] " >> $file _of_create_node_log exit $ERROR _create Else echo "[$ (date + '%F%T ')] [instance] [$: $instan CE_ID:$2] [Create success] ">> $file _of_create_node_log fi # Create Volume volume_id=$ ($oci _path BV Volume Create--availability-domain $avail _domain-c $compartment _id--display-name $--size-in-mbs $4 | grep "\" id\ "" | Cut-d "\" "-f 4) if [[-Z $volume _id]];then echo" [$ (date + '%F%T ')] [volume] [$] [Create failure] [exit ...] ">> $file _of_create_node_log exit $ERROR _create Else echo" [$ (date + '%F%T ')] [volume] [$: $volume _ID:$4] [Create success] ">> $file _of_create_node_log fi # Attach Volume to Instance while True;do I        nstance_state=$ ($OCI _path Compute instance get--instance-id $instance _id | grep "lifecycle-state" |cut-d "\" "-F 4) volume_state=$ ($oci _path bv volume get--volume-id $volume _id | grep "lifecycle-state" |cut-d "\" "-f 4) if [[ $instance _state = = "RUNNING"] && [[$volume _state = = "AVAILABLE"]];then volume_attached_id=$ ($oci _pa Th compute volume-attachment Attach--display-name $--instance-id $instance _id--type iSCSI--volume-id $volume _id | grep "\" id\ "" | Cut-d "\" "-f 4) If [-Z $volume _attached_id]];then echo" [$ (date + '%F%T ')] [volume_attached] [$] [Create failure] [exit ...] ">> $file _of_create_node_log exit $ERROR _create Else echo "[$ (date + '%F%T ')] [volume_attached] [$: $volume _attached_id] [Create Success] ">> $file _of_create_node_log fi break fi sleep 5 done echo" \ "$ (d Ate + '%F%T ') \ "\" $instance _id\ "\" $volume _id\ "\" $volume _attached_id\ "" >> $file _of_alived_node}if [$#-le 3];the N echo "Error:usage $ (basename $) {create|delete} number" Exit $Error _parameterficase "in Create) STA Rt_ip_number=5 # File_of_ip_number Restore has been create maximal IP. $file _of_ip_number create_instance_number=0 # Loop Create node host while [[$create _instance_number -LT] && [[$start _ip_number-le]];d o instance_display_name=igb$ (printf "%03d" $start _ip_num ber) instance_private_ip=10.40.1. $start _ip_numbEr echo $instance _private_ip volume_display_name=vgb$ (printf "%03d" $start _ip_number) vol.            ume_size_mbs=51200 Attachment_display_name=${instance_display_name}_attached_${volume_display_name} Create_node $instance _display_name $instance _private_ip $volume _display_name $volume _size_mbs $attachment _display_ Name # alter create maximal IP start_ip_number=$ ((start_ip_number+1)) echo "Start_ip_numb        er= $start _ip_number "> $file _of_ip_number create_instance_number=$ ((create_instance_number+1)) done        echo "Create $create _instance_number instance."    ;; Delete).  $file _of_ip_number #end_delete_ip_number =$ ((start_create_ip_number-1)) delete_instance_number=0 while            [$delete _instance_number-lt];d o instance_id=$ (tail-1 $file _of_alived_node | cut-d "\" "-F 4) If [-Z $instance _id]];then echo "No has more INSTance "echo" Delete $delete _instance_number instance. "            Exit $Error _no_instance fi volume_id=$ (tail-1 $file _of_alived_node | cut-d "\" "-F 6) $OCI _path Compute instance terminate--force--instance-id $instance _id while True;do Instan                 ce_state=$ ($OCI _path Compute instance get--instance-id $instance _id |grep "lifecycle-state" |cut-d "\" "-F 4) if [[$instance _state = = "TERMINATED"] | |            [[-Z $instance _state]];then break fi sleep 5 done             $oci _path BV Volume delete--force--volume-id $volume _id delete_instance_number=$ ((delete_instance_number+1))            echo "\" $instance _id\ "\" $volume _id\ "" >> $file _of_delete_node_log # alter ALIVED hosts Sed-i "/$instance _id/d" $file _of_alived_node echo "start_ip_number=$ ((start_ip_number-1))" > $file _of _ip_number         Done echo "Delete $delete _instance_number instance."    ;; *) echo "Usage: $ (basename $) {create|delete} number" Esac

Attention:

    • Script uses
      • Create a node machine:./nodes_ocvh.sh Create 3
      • Delete node:./nodes_ocvh.sh Delete 2

Oracle Cloud remote batch creation of virtual machines

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.