Exemplo n.º 1
0
def add_drop_packet(configuration: Configuration,
                    instance_id: str,
                    username: str,
                    password: str,
                    target_ip: str,
                    duration: int = 30,
                    hostname: str = None,
                    tag: bool = False):
    if hostname is None:
        service = create_ibmcloud_api_client(configuration)
        if tag:
            tag_virtual_instance(configuration=configuration,
                                 instance_id=instance_id,
                                 service=service,
                                 tagname='add_drop_packet')
        response = service.list_instance_network_interfaces(
            instance_id=instance_id).get_result()
        output_dict = [
            attachment for attachment in response['network_interfaces']
        ]
        hostname = output_dict[0]['floating_ips'][0]['address']

    with CommandExecuter(hostname=hostname,
                         username=username,
                         password=password) as client:
        # client.exec_command('ls')
        # If interface not provided pick up the first none local interface
        _, stdout, _ = client.exec_command(
            f'iptables -I  OUTPUT -j DROP -d {target_ip} -m conntrack --ctstate ESTABLISHED,RELATED;sleep {duration};iptables -D  OUTPUT -j DROP -d {target_ip} -m conntrack --ctstate ESTABLISHED,RELATED'
        )
Exemplo n.º 2
0
def create_instance_action(configuration: Configuration,
                           type: str,
                           instance_id: str,
                           force: bool = None,
                           tag: bool = False):
    service = create_ibmcloud_api_client(configuration)
    try:
        if tag:
            tag_virtual_instance(configuration=configuration,
                                 instance_id=instance_id,
                                 service=service,
                                 tagname='create_instance_action')
        service.create_instance_action(type=type,
                                       instance_id=instance_id,
                                       force=force)
    except ApiException as e:
        logger.error("Action instances failed with status code " +
                     str(e.code) + ": " + e.message)
Exemplo n.º 3
0
def start_multiple_instances(configuration: Configuration,
                             vpc_id: str,
                             zone: str = None,
                             random: bool = False,
                             tag: bool = False):
    service = create_ibmcloud_api_client(configuration)
    instances = service.list_instances(vpc_id=vpc_id).get_result()['instances']
    output_dict = [
        instance for instance in instances if instance['zone']['name'] == zone
    ]
    for instance in output_dict:
        if instance['status'] == 'stopped':
            service.create_instance_action(type='start',
                                           instance_id=instance['id'],
                                           force=False)
            if tag:
                tag_virtual_instance(configuration=configuration,
                                     instance_id=instance['id'],
                                     service=service,
                                     tagname='start_multiple_instances')
Exemplo n.º 4
0
def add_volume_to_instance(configuration: Configuration,
                           volume_id: str,
                           instance_id: str,
                           auto_delete: bool = None,
                           tag: bool = False):
    service = create_ibmcloud_api_client(configuration)
    try:
        if tag:
            tag_virtual_instance(configuration=configuration,
                                 instance_id=instance_id,
                                 service=service,
                                 tagname='add_volume_to_instance')
        identity = VolumeIdentityById(volume_id)
        service.create_instance_volume_attachment(
            instance_id=instance_id,
            volume=identity,
            delete_volume_on_instance_delete=auto_delete)
    except ApiException as e:
        logger.error("Action add_volume_to_instance failed with status code " +
                     str(e.code) + ": " + e.message)
Exemplo n.º 5
0
def add_network_latency(configuration: Configuration,
                        instance_id: str,
                        username: str,
                        password: str,
                        duration: int = 30,
                        delay: int = 300,
                        jitter: int = 50,
                        interface: str = None,
                        hostname: str = None,
                        timeout: int = 60,
                        tag: bool = False):
    if hostname is None:
        service = create_ibmcloud_api_client(configuration)
        if tag:
            tag_virtual_instance(configuration=configuration,
                                 instance_id=instance_id,
                                 service=service,
                                 tagname='add_network_latency')
        response = service.list_instance_network_interfaces(
            instance_id=instance_id).get_result()
        output_dict = [
            attachment for attachment in response['network_interfaces']
            if len(attachment['floating_ips']) > 0
        ]
        hostname = output_dict[0]['floating_ips'][0]['address']

    with CommandExecuter(hostname=hostname,
                         username=username,
                         password=password) as client:
        # client.exec_command('ls')
        # If interface not provided pick up the first none local interface
        if interface is None:
            _, stdout, _ = client.exec_command(
                "ip addr | grep UP | grep -v LOOPBACK | awk -F':' '{print $2}' | sed -e 's/^[[:space:]]*//'"
            )
            interface = next(stdout).strip()
        _, stdout, _ = client.exec_command(
            f'sudo tc qdisc add dev {interface} root netem delay {delay}ms {jitter}ms;sleep {duration};sudo tc qdisc del dev {interface} root'
        )
Exemplo n.º 6
0
def remove_volume_from_instance(configuration: Configuration,
                                volume_id: str,
                                instance_id: str,
                                tag: bool = False):
    service = create_ibmcloud_api_client(configuration)
    try:
        if tag:
            tag_virtual_instance(configuration=configuration,
                                 instance_id=instance_id,
                                 service=service,
                                 tagname='remove_volume_from_instance')
        res = service.list_instance_volume_attachments(instance_id=instance_id)
        dict = res.get_result()
        output_dict = [
            attachment for attachment in dict['volume_attachments']
            if attachment['volume']['id'] == volume_id
        ]
        volume_attachment_id = output_dict[0]['id']
        service.delete_instance_volume_attachment(instance_id=instance_id,
                                                  id=volume_attachment_id)
    except ApiException as e:
        logger.error("Action instances failed with status code " +
                     str(e.code) + ": " + e.message)