IcsEc2 Common Library

IcsEc2: Library for EC2

This is the IcsEc2 common library.
class opslib.icsec2.IcsEc2(region, **kwargs)[source]

ICS Library for EC2

__init__(region, **kwargs)[source]
__module__ = 'opslib.icsec2'
add_instance_tags(instance_id, tags)[source]

Add tags to the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
bind_eip(eip, instance_id)[source]

Bind EIP address to the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:bool
Returns:success or raise IcsEc2Exception
clean_snapshots(tags, duration)[source]

Clean up snapshots by specific tags and duration

Parameters:tags (dict) – snapshot tags like
{
  "Name": "XXX"
}
Parameters:duration (int) – seconds
Return type:list
Returns:list of cleaned snapshot ids
del_instance_tags(instance_id, tags)[source]

Remove tags of the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
del_snapshot(snapshot_id)[source]

Delete snapshots by snapshot_id

Parameters:snapshot_id (string) – snapshot Id like ‘snap-xxxxxx’
Return type:boolean
Returns:true, false, exception
fetch_imageid_by_tags(**tags)[source]

Fetch the Image Id by specific tags

Parameters:tags (dict) – AMI tags like {‘Name’: ‘XXX’}
Return type:string
Returns:Image Id
fetch_latest_snapshot(snapshots)[source]

Find the latest Snapshot

fetch_snapid_by_tags(**tags)[source]

Find the Snapshot Id by specific tags

Parameters:tags (dict) – snapshot tags like {‘Name’: ‘XXX’}
Return type:string
Returns:Snapshot Id
find_ami_by_tags(tags)[source]

Find AMI by specific tags

Parameters:tags (dict) – AMI tags like {‘Name’: ‘XXX’}
Return type:list
Returns:list of boto image objects
find_snapshot_by_tags(tags)[source]

Find a snapshot by specific tags

Parameters:tags (dict) – snapshot tags like {‘Name’: ‘XXX’}
Return type:list
Returns:list of boto snapshot objects
static format_tags(tags)[source]

Convert {“Name”: “XXX”} to {“tag:Name”: “XXX”}

free_eip(eip, instance_id)[source]

Free EIP address to the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:bool
Returns:success or raise IcsEc2Exception
get_all_zones()[source]

Get all Availability Zones under this region

Return type:list
Returns:list of availability zones in this region
get_az_from_subnet_id(subnet_id=None)[source]

Get the name of Availability Zone by its Subnet Id

Parameters:subnet_id (string) – subnet id
Return type:string
Returns:availability zone name
get_eips_from_addr(eip_list)[source]

Get EIP objects via the list of EIP addresses

Parameters:eip_list (list) – the list of EIP addresses
Return type:class
Returns:EIP objects in boto
get_eips_from_instance(instance_id)[source]

Get EIP objects via the instance id

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:class
Returns:EIP objects in boto
get_instance_attribute(instance_id, attr_name)[source]

Get the attribute value of an instance.

Parameters:
  • instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
  • attr_name (string) – the name of the instance attribute, details shown as below:
Variables:
  • id – The unique ID of the Instance.
  • groups – A list of Group objects representing the security groups associated with the instance.
  • public_dns_name – The public dns name of the instance.
  • private_dns_name – The private dns name of the instance.
  • state – The string representation of the instance’s current state.
  • state_code – An integer representation of the instance’s current state.
  • previous_state – The string representation of the instance’s previous state.
  • previous_state_code – An integer representation of the instance’s current state.
  • key_name – The name of the SSH key associated with the instance.
  • instance_type – The type of instance (e.g. m1.small).
  • launch_time – The time the instance was launched.
  • image_id – The ID of the AMI used to launch this instance.
  • placement – The availability zone in which the instance is running.
  • placement_group – The name of the placement group the instance is in (for cluster compute instances).
  • placement_tenancy – The tenancy of the instance, if the instance is running within a VPC. An instance with a tenancy of dedicated runs on a single-tenant hardware.
  • kernel – The kernel associated with the instance.
  • ramdisk – The ramdisk associated with the instance.
  • architecture – The architecture of the image (i386|x86_64).
  • hypervisor – The hypervisor used.
  • virtualization_type – The type of virtualization used.
  • product_codes – A list of product codes associated with this instance.
  • ami_launch_index – This instances position within it’s launch group.
  • monitored – A boolean indicating whether monitoring is enabled or not.
  • monitoring_state – A string value that contains the actual value of the monitoring element returned by EC2.
  • spot_instance_request_id – The ID of the spot instance request if this is a spot instance.
  • subnet_id – The VPC Subnet ID, if running in VPC.
  • vpc_id – The VPC ID, if running in VPC.
  • private_ip_address – The private IP address of the instance.
  • ip_address – The public IP address of the instance.
  • platform – Platform of the instance (e.g. Windows)
  • root_device_name – The name of the root device.
  • root_device_type – The root device type (ebs|instance-store).
  • block_device_mapping – The Block Device Mapping for the instance.
  • state_reason – The reason for the most recent state transition.
  • groups – List of security Groups associated with the instance.
  • interfaces – List of Elastic Network Interfaces associated with this instance.
  • ebs_optimized – Whether instance is using optimized EBS volumes or not.
  • instance_profile – A Python dict containing the instance profile id and arn associated with this instance.
get_instance_event(instance_id)[source]

Get the event of the specified instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
get_instance_status(instance_id)[source]
Get the instance status and system status
of the specified instance
Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:tuple
Returns:a tuple contains (instance_status, system_status)
get_instance_tags(instance_id)[source]

Get tags of the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:dict
Returns:a dictionary containing the tags of this instance
get_private_address(instance_id)[source]

Get the private IPv4 address of the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:string
Returns:a string containing the private IPv4 address
get_private_dns(instance_id)[source]

Get the private dns address of the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:string
Returns:a string containing the private IPv4 address
get_public_address(instance_id)[source]

Get the public IPv4 address of the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:string
Returns:a string containing the public IPv4 address
get_public_dns(instance_id)[source]

Get the public dns address of the instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:string
Returns:a string containing the public dns address
get_security_group_id(name, vpc_id=None)[source]

Get security group id

Parameters:
  • name (string) – security group name
  • vpc_id (string) – vpc id
Return type:

string

Returns:

security group id

get_sgroup(name, vpc_id=None)[source]

Get Security Group Name (if Ec2) / Id (if Vpc)

Parameters:
  • name (string) – security group name
  • vpc_id (string) – vpc id
Return type:

string

Returns:

security group id

get_volumes_by_instance(instance_id)[source]

Get boto Volume Objects by instance Id

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:list
Returns:list of boto volume objects
get_zone_index_for_cassandra(index)[source]

Get the index of Availability Zone for Cassandra

Parameters:index (int) – the index of cassandra instance
Return type:string
Returns:zone index like “1”
get_zone_name_for_cassandra(index)[source]

Get the name of Availability Zone for Cassandra

Parameters:index (int) – the index of cassandra instance
Return type:string
Returns:zone name like “us-west-2a”
get_zone_suffix_for_cassandra(index)[source]

Get the suffix of Availability Zone for Cassandra

Parameters:index (int) – the index of cassandra instance
Return type:string
Returns:zone suffix like “a-1”
is_eip_free(eip)[source]

check the availability of the specified EIP address: free or not

Parameters:eip (string) – one EIP address
Return type:tuple
Returns:(True/False, EIP object/None)
is_instance_healthy(instance_id)[source]

check the health of the specified instance

Parameters:instance_id (string) – EC2 instance id startwith ‘i-xxxxxxx’
Return type:boolean
Returns:True/False
size_of_all_zones()[source]

Get the number of all Availability Zones under this region

Return type:int
Returns:number of availability zones in this region
take_snapshot(volume_id, description=None, tags=None)[source]

Take a snapshot to existing volume with specific tags

Parameters:
  • volume_id (string) – EC2 volume id startwith ‘vol-xxxxxxx’
  • description (string) – words to describe the usage of this snapshot
  • tags (dict) – snapshot tags like {‘Name’: ‘XXX’}
Return type:

class

Returns:

boto snapshot object

Indices and tables

Read the Docs v: latest
Versions
latest
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.