Python logzero.logger.warning() Examples

The following are 30 code examples of logzero.logger.warning(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logzero.logger , or try the search function .
Example #1
Source File: zipfiles.py    From geeup with Apache License 2.0 7 votes vote down vote up
def vcount(shpfile):
    df = gp.read_file(shpfile)
    if not df.size==0:
        for i, row in df.iterrows():
            # It's better to check if multigeometry
            multi = row.geometry.type.startswith("Multi")
            if multi:
                n = 0
                # iterate over all parts of multigeometry
                for part in row.geometry:
                    n += len(part.exterior.coords)
            else: # if single geometry like point, linestring or polygon
                n = len(row.geometry.exterior.coords)

            #print('Total vertices: {:,}'.format(n))
            overall.append(n)
        if all(i < 1000000 for i in overall)==True:
            return sum(overall)
        else:
            logger.warning(shpfile+' has overall max vertex of '+str(max(overall))+' with max allowed 1000000 ingest might fail')
            return sum(overall)
            #print('Total vertices per feature exceeded max. Overall vertices: {}'.format(sum(overall)))
            #return sum(overall)
    else:
        return df.size 
Example #2
Source File: main.py    From atxserver2-android-provider with MIT License 6 votes vote down vote up
def post(self, udid=None):
        """ 设备清理 """
        udid = udid or self.get_argument("udid")
        logger.info("Receive colding request for %s", udid)
        request_secret = self.get_argument("secret")
        if secret != request_secret:
            logger.warning("secret not match, expect %s, got %s", secret,
                           request_secret)
            return

        if udid not in udid2device:
            return

        device = udid2device[udid]
        await device.reset()
        await hbconn.device_update({
            "udid": udid,
            "colding": False,
            "provider": device.addrs(),
        })
        self.write({"success": True, "description": "Device colded"}) 
Example #3
Source File: python.py    From chaostoolkit-lib with Apache License 2.0 6 votes vote down vote up
def validate_python_control(control: Control):
    """
    Verify that a control block matches the specification
    """
    name = control["name"]
    provider = control["provider"]
    mod_name = provider.get("module")
    if not mod_name:
        raise InvalidActivity(
            "Control '{}' must have a module path".format(name))

    try:
        importlib.import_module(mod_name)
    except ImportError:
        logger.warning("Could not find Python module '{mod}' "
                       "in control '{name}'. Did you install the Python "
                       "module? The experiment will carry on running "
                       "nonetheless.".format(mod=mod_name, name=name)) 
Example #4
Source File: heartbeat.py    From atxserver2-android-provider with MIT License 6 votes vote down vote up
def connect(self):
        """
        Returns:
            tornado.WebSocketConnection
        """
        cnt = 0
        while True:
            try:
                ws = await self._connect()
                cnt = 0
                return ws
            except Exception as e:
                cnt = min(30, cnt + 1)
                logger.warning("WS connect error: %s, reconnect after %ds", e,
                               cnt + 1)
                await gen.sleep(cnt + 1) 
Example #5
Source File: heartbeat.py    From atxserver2-ios-provider with MIT License 6 votes vote down vote up
def connect(self):
        """
        Returns:
            tornado.WebSocketConnection
        """
        cnt = 0
        while True:
            try:
                ws = await self._connect()
                cnt = 0
                return ws
            except Exception as e:
                cnt = min(30, cnt + 1)
                logger.warning("WS connect error: %s, reconnect after %ds", e,
                               cnt + 1)
                await gen.sleep(cnt + 1) 
Example #6
Source File: check.py    From chaostoolkit with Apache License 2.0 6 votes vote down vote up
def check_newer_version(command: str):
    """
    Query for the latest release of the chaostoolkit to compare it
    with the current's version. If the former is higher then issue a warning
    inviting the user to upgrade its environment.
    """
    try:
        command = command.strip()
        r = requests.get(LATEST_RELEASE_URL, timeout=(2, 30),
                         params={"current": __version__, "command": command})
        if r.status_code == 200:
            payload = r.json()
            latest_version = payload["version"]
            if payload.get("up_to_date") is False:
                options = '--pre -U' if 'rc' in latest_version else '-U'
                logger.warning(
                    "\nThere is a new version ({v}) of the chaostoolkit "
                    "available.\n"
                    "You may upgrade by typing:\n\n"
                    "$ pip install {opt} chaostoolkit\n\n"
                    "Please review changes at {u}\n".format(
                        u=CHANGELOG_URL, v=latest_version, opt=options))
                return latest_version
    except Exception:
        pass 
Example #7
Source File: idb.py    From atxserver2-ios-provider with MIT License 6 votes vote down vote up
def wda_screenshot_ok(self):
        """
        Check if screenshot is working
        Returns:
            bool
        """
        try:
            request = httpclient.HTTPRequest(self.wda_device_url +
                                             "/screenshot",
                                             connect_timeout=3,
                                             request_timeout=15)
            client = httpclient.AsyncHTTPClient()
            resp = await client.fetch(request)
            data = json.loads(resp.body)
            raw_png_data = base64.b64decode(data['value'])
            png_header = b"\x89PNG\r\n\x1a\n"
            if not raw_png_data.startswith(png_header):
                return False
            return True
        except Exception as e:
            logger.warning("%s wda screenshot error: %s", self, e)
            return False 
Example #8
Source File: idb.py    From atxserver2-ios-provider with MIT License 6 votes vote down vote up
def wait_until_ready(self, timeout: float = 60.0) -> bool:
        """
        Returns:
            bool
        """
        deadline = time.time() + timeout
        while time.time() < deadline and not self._stop.is_set():
            quited = any([p.poll() is not None for p in self._procs])
            if quited:
                logger.warning("%s process quit %s", self,
                               [(p.pid, p.poll()) for p in self._procs])
                return False
            if await self.wda_status():
                return True
            await self._sleep(1)
        return False 
Example #9
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def run_wda_forever(self):
        """
        Args:
            callback
        """
        wda_fail_cnt = 0
        while not self._stop.is_set():
            await self._callback(self.status_preparing)
            start = time.time()
            ok = await self.run_webdriveragent()
            if not ok:
                self.destroy()

                wda_fail_cnt += 1
                if wda_fail_cnt > 3:
                    logger.error("%s Run WDA failed. -_-!", self)
                    break

                if time.time() - start < 3.0:
                    logger.error("%s WDA unable to start", self)
                    break
                logger.warning("%s wda started failed, retry after 10s", self)
                if not await self._sleep(10):
                    break
                continue

            wda_fail_cnt = 0
            logger.info("%s wda lanuched", self)

            # wda_status() result stored in __wda_info
            await self._callback(self.status_ready, self.__wda_info)
            await self.watch_wda_status()

        await self._callback(self.status_fatal)
        self.destroy()  # destroy twice to make sure no process left
        self._finished.set()  # no need await 
Example #10
Source File: probes.py    From community-playground with Apache License 2.0 5 votes vote down vote up
def ask_for_superpower(service_url: str, timeout: int = 3,
                       configuration: Configuration = None, 
                       secrets: Secrets = None) -> Dict[str, Any]:
    """
    Fetch a superpower
    """
    global session
    if not session:
        session = requests.Session()

    headers = {
        'Accept': 'application/json'
    }

    info = {}
    try:
        r = session.get(
            service_url, headers=headers, timeout=(timeout, timeout))
    except requests.exceptions.Timeout as x:
        logger.warning("Superpowers were too slow to arrive!")
        return False

    if r.status_code == 200:
        info = r.json()
        fetched_superpowers.append(info)

    return {
        "status": r.status_code,
        "headers": dict(**r.headers),
        "body": info
    } 
Example #11
Source File: deprecation.py    From chaostoolkit-lib with Apache License 2.0 5 votes vote down vote up
def warn_about_deprecated_features(experiment: Experiment):
    """
    Warn about deprecated features.

    We do it globally so that we can warn only once about each feature and
    avoid repeating the same message over and over again.
    """
    warned_deprecations = {
        DeprecatedDictArgsMessage: False,
        DeprecatedVaultMissingPathMessage: False
    }
    activities = get_all_activities_in_experiment(experiment)

    for activity in activities:
        provider = activity.get("provider")
        if not provider:
            continue

        provider_type = provider.get("type")
        if provider_type == "process":
            arguments = provider.get("arguments")
            if not warned_deprecations[DeprecatedDictArgsMessage] and \
                    isinstance(arguments, dict):
                warned_deprecations[DeprecatedDictArgsMessage] = True
                warnings.warn(DeprecatedDictArgsMessage, DeprecationWarning)
                logger.warning(DeprecatedDictArgsMessage)

    # vault now expects the path property
    # see https://github.com/chaostoolkit/chaostoolkit-lib/issues/77
    for (target, keys) in experiment.get("secrets", {}).items():
        for (key, value) in keys.items():
            if isinstance(value, dict) and value.get("type") == "vault":
                if "key" in value and "path" not in value:
                    warned_deprecations[
                        DeprecatedVaultMissingPathMessage] = True
                    warnings.warn(
                        DeprecatedVaultMissingPathMessage, DeprecationWarning)
                    logger.warning(DeprecatedVaultMissingPathMessage) 
Example #12
Source File: actions.py    From chaostoolkit-aws with Apache License 2.0 5 votes vote down vote up
def stop_instances(instance_ids: List[str] = None, az: str = None,
                   filters: List[Dict[str, Any]] = None,
                   force: bool = False, configuration: Configuration = None,
                   secrets: Secrets = None) -> List[AWSResponse]:
    """
    Stop the given EC2 instances or, if none is provided, all instances
    of the given availability zone. If you need more control, you can
    also provide a list of filters following the documentation
    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_instances
    """

    if not az and not instance_ids and not filters:
        raise FailedActivity(
            "To stop EC2 instances, you must specify either the instance ids,"
            " an AZ to pick random instances from, or a set of filters.")

    if az and not instance_ids and not filters:
        logger.warning('Based on configuration provided I am going to '
                       'stop all instances in AZ %s!' % az)

    client = aws_client('ec2', configuration, secrets)

    if not instance_ids:
        filters = deepcopy(filters) if filters else []

        if az:
            filters.append({'Name': 'availability-zone', 'Values': [az]})
        instance_types = list_instances_by_type(filters, client)

        if not instance_types:
            raise FailedActivity(
                "No instances in availability zone: {}".format(az))
    else:
        instance_types = get_instance_type_by_id(instance_ids, client)

    logger.debug(
        "Picked EC2 instances '{}' from AZ '{}' to be stopped".format(
            str(instance_types), az))

    return stop_instances_any_type(instance_types=instance_types,
                                   force=force, client=client) 
Example #13
Source File: heartbeat.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def _drain_ws_message(self):
        while True:
            message = await self._ws.read_message()
            logger.debug("WS read message: %s", message)
            if message is None:
                self._ws = None
                logger.warning("WS closed")
                self._ws = await self.connect()
                await self._queue.put(None)
            logger.info("WS receive message: %s", message) 
Example #14
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def wda_healthcheck(self):
        client = httpclient.AsyncHTTPClient()

        if not await self.is_wda_alive():
            logger.warning("%s check failed -_-!", self)
            await self._callback(self.status_preparing)
            if not await self.restart_wda():
                logger.warning("%s wda recover in healthcheck failed", self)
                return
        else:
            logger.debug("%s all check passed ^_^", self)

        await client.fetch(self.wda_device_url + "/wda/healthcheck") 
Example #15
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def watch_wda_status(self):
        """
        check WebDriverAgent all the time
        """
        # check wda_status every 30s
        fail_cnt = 0
        last_ip = self.device_ip
        while not self._stop.is_set():
            if await self.wda_status():
                if fail_cnt != 0:
                    logger.info("wda ping recovered")
                    fail_cnt = 0
                if last_ip != self.device_ip:
                    last_ip = self.device_ip
                    await self._callback(self.status_ready, self.__wda_info)
                await self._sleep(60)
                logger.debug("%s is fine", self)
            else:
                fail_cnt += 1
                logger.warning("%s wda ping error: %d", self, fail_cnt)
                if fail_cnt > 3:
                    logger.warning("ping wda fail too many times, restart wda")
                    break
                await self._sleep(10)

        self.destroy() 
Example #16
Source File: __init__.py    From chaostoolkit-kubernetes with Apache License 2.0 5 votes vote down vote up
def _log_deprecated(name: str, alt_name: str):
    logger.warning("{} function is DEPRECATED and will be removed in the next \
        releases, please use {} instead".format(
        name, alt_name)) 
Example #17
Source File: device.py    From atxserver2-android-provider with MIT License 5 votes vote down vote up
def run_forever(self):
        try:
            await self.init()
        except Exception as e:
            logger.warning("Init failed: %s", e) 
Example #18
Source File: botchecker.py    From BotListBot with MIT License 5 votes vote down vote up
def resolve_bot(self, bot: BotModel):
        if bot.chat_id:
            try:
                return self.resolve_peer(bot.chat_id)
            except:
                pass

        try:
            results = self.send(Search(bot.username, limit=3))
            if results.users:
                try:
                    return next(
                        s for s in results.users if s.username.lower() == bot.username[1:].lower())
                except StopIteration:
                    pass
        except QueryTooShort:
            pass

        if self.username_flood_until:
            if self.username_flood_until < datetime.now():
                self.username_flood_until = None
        else:
            try:
                return self.resolve_peer(bot.username)
            except FloodWait as e:
                self.username_flood_until = datetime.now() + timedelta(
                    seconds=e.x)
                log.warning("Flood wait for ResolveUsername: {}s (until {})".format(
                    e.x, self.username_flood_until))
            except UsernameInvalid as e:
                log.error(e)  # TODO

        return None 
Example #19
Source File: heartbeat.py    From atxserver2-android-provider with MIT License 5 votes vote down vote up
def _drain_ws_message(self):
        while True:
            message = await self._ws.read_message()
            logger.debug("WS read message: %s", message)
            if message is None:
                self._ws = None
                logger.warning("WS closed")
                self._ws = await self.connect()
                await self._queue.put(None)
            logger.info("WS receive message: %s", message) 
Example #20
Source File: idb.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def runcommand(*args) -> str:
    try:
        output = subprocess.check_output(args)
        return output.strip().decode('utf-8')
    except (subprocess.CalledProcessError, FileNotFoundError):
        return ""
    except Exception as e:
        logger.warning("unknown error: %s", e)
        return "" 
Example #21
Source File: device.py    From atxserver2-android-provider with MIT License 5 votes vote down vote up
def _install_apk(self, path: str):
        assert path, "Invalid %s" % path
        try:
            m = apkutils.APK(path).manifest
            info = self._device.package_info(m.package_name)
            if info and m.version_code == info[
                    'version_code'] and m.version_name == info['version_name']:
                logger.debug("%s already installed %s", self, path)
            else:
                print(info, ":", m.version_code, m.version_name)
                logger.debug("%s install %s", self, path)
                self._device.install(path, force=True)
        except Exception as e:
            traceback.print_exc()
            logger.warning("%s Install apk %s error %s", self, path, e) 
Example #22
Source File: xpath.py    From uiautomator2 with MIT License 5 votes vote down vote up
def apply_watch_from_yaml(self, data):
        """
        Examples of argument data

            ---
            - when: "@com.example.app/popup"
            then: >
                def callback(d):
                    d.click(0.5, 0.5)
            - when: 继续
            then: click
        """
        try:
            import yaml
        except ImportError:
            self.logger.warning("missing lib pyyaml")

        data = yaml.load(data, Loader=yaml.SafeLoader)
        for item in data:
            when, then = item['when'], item['then']

            trigger = lambda: None
            self.logger.info("%s, %s", when, then)
            if then == 'click':
                trigger = lambda selector: selector.get_last_match().click()
                trigger.__doc__ = "click"
            elif then.lstrip().startswith("def callback"):
                mod = ModuleType("_inner_module")
                exec(then, mod.__dict__)
                trigger = mod.callback
                trigger.__doc__ = then
            else:
                self.logger.warning("Unknown then: %r", then)

            self.logger.debug("When: %r, Trigger: %r", when, trigger.__doc__)
            self.when(when).call(trigger) 
Example #23
Source File: cli.py    From chaostoolkit with Apache License 2.0 5 votes vote down vote up
def cli(ctx: click.Context, verbose: bool = False,
        no_version_check: bool = False, change_dir: str = None,
        no_log_file: bool = False, log_file: str = "chaostoolkit.log",
        log_format: str = "string", settings: str = CHAOSTOOLKIT_CONFIG_PATH):

    if no_log_file:
        configure_logger(
            verbose=verbose, log_format=log_format,
            context_id=str(uuid.uuid4()))
    else:
        configure_logger(
            verbose=verbose, log_file=log_file, log_format=log_format,
            context_id=str(uuid.uuid4()))

    subcommand = ctx.invoked_subcommand

    # make it nicer for going through the log file
    logger.debug("#" * 79)
    logger.debug("Running command '{}'".format(subcommand))

    ctx.obj = {}
    ctx.obj["settings_path"] = click.format_filename(settings)
    logger.debug("Using settings file '{}'".format(ctx.obj["settings_path"]))

    if not no_version_check:
        check_newer_version(command=subcommand)

    if change_dir:
        logger.warning("Moving to {d}".format(d=change_dir))
        os.chdir(change_dir) 
Example #24
Source File: CThermal.py    From Thermal_Image_Analysis with MIT License 5 votes vote down vote up
def raw2temp(raw, E=0.9,OD=1,RTemp=20,ATemp=20,IRWTemp=20,IRT=1,RH=50,PR1=21106.77,PB=1501,PF=1,PO=-7340,PR2=0.012545258):
        """ convert raw values from the flir sensor to temperatures in °C """
        # this calculation has been ported to python from https://github.com/gtatters/Thermimage/blob/master/R/raw2temp.R
        # a detailed explanation of what is going on here can be found there

        # constants
        ATA1=0.006569; ATA2=0.01262; ATB1=-0.002276; ATB2=-0.00667; ATX=1.9; #RH=0
        # transmission through window (calibrated)
        emiss_wind = 1 - IRT
        refl_wind = 0
        # transmission through the air
        h2o = (RH/100)*exp(1.5587+0.06939*(ATemp)-0.00027816*(ATemp)**2+0.00000068455*(ATemp)**3)
        tau1 = ATX*exp(-sqrt(OD/2)*(ATA1+ATB1*sqrt(h2o)))+(1-ATX)*exp(-sqrt(OD/2)*(ATA2+ATB2*sqrt(h2o)))
        tau2 = ATX*exp(-sqrt(OD/2)*(ATA1+ATB1*sqrt(h2o)))+(1-ATX)*exp(-sqrt(OD/2)*(ATA2+ATB2*sqrt(h2o)))        
        # radiance from the environment
        raw_refl1 = PR1/(PR2*(exp(PB/(RTemp+273.15))-PF))-PO
        raw_refl1_attn = (1-E)/E*raw_refl1 # Reflected component

        raw_atm1 = PR1/(PR2*(exp(PB/(ATemp+273.15))-PF))-PO # Emission from atmosphere 1
        raw_atm1_attn = (1-tau1)/E/tau1*raw_atm1 # attenuation for atmospheric 1 emission

        raw_wind = PR1/(PR2*(exp(PB/(IRWTemp+273.15))-PF))-PO # Emission from window due to its own temp
        raw_wind_attn = emiss_wind/E/tau1/IRT*raw_wind # Componen due to window emissivity

        raw_refl2 = PR1/(PR2*(exp(PB/(RTemp+273.15))-PF))-PO # Reflection from window due to external objects
        raw_refl2_attn = refl_wind/E/tau1/IRT*raw_refl2 # component due to window reflectivity

        raw_atm2 = PR1/(PR2*(exp(PB/(ATemp+273.15))-PF))-PO # Emission from atmosphere 2
        raw_atm2_attn = (1-tau2)/E/tau1/IRT/tau2*raw_atm2 # attenuation for atmospheric 2 emission

        raw_obj = (raw/E/tau1/IRT/tau2-raw_atm1_attn-raw_atm2_attn-raw_wind_attn-raw_refl1_attn-raw_refl2_attn)
        val_to_log = PR1/(PR2*(raw_obj+PO))+PF
        if any(val_to_log.ravel()<0):
            logger.warning('Image seems to be corrupted')
            val_to_log = np.where(val_to_log<0, sys.float_info.min, val_to_log)
        # temperature from radiance
        temp_C = PB/np.log(val_to_log)-273.15

        return temp_C 
Example #25
Source File: CThermal.py    From Thermal_Image_Analysis with MIT License 5 votes vote down vote up
def get_contours(cls, thermal_image, contours,is_rect=False):
        temp_image = thermal_image.copy()
        point1, point2 = [[]],[[]]
        cv.namedWindow('image')
        cv.setMouseCallback('image', cls.draw_contour_area, (temp_image, contours, [is_rect, point1, point2]) )
        
        while(1):
            cv.imshow('image', temp_image)
            if is_rect:
                if len(point1[0])>0 and len(point2[0])>0:
                    temp_image  = cv.rectangle(thermal_image.copy(), point1[0], point2[0], (0,0,255))
            k=cv.waitKey(1) & 0xFF

            if k==13 or k==141:
                redraw=None
                if is_rect is True and (len(point1[0])==0 or len(point2[0])==0):
                    logger.warning('No rectangle has been drawn. Do you want to continue?')
                    redraw = input('1-Yes\t0-No,draw rectangle again\n')

                if redraw is not None and redraw == 0:
                    logger.info('Draw a rectangle')
                else:
                    if is_rect is True and redraw is not None:
                        logger.warning('Exiting function without drawing a rectangle')
                        is_rect = False
                    break
        cv.destroyWindow('image')   
        if is_rect:
            area_rect = point1[0][0], point1[0][1], abs(point1[0][0] - point2[0][0]), abs(point1[0][1] - point2[0][1])    
            return area_rect
        else:
            return None 
Example #26
Source File: CThermal.py    From Thermal_Image_Analysis with MIT License 5 votes vote down vote up
def scale_with_roi(thermal_np, thermal_roi_values):
        temp_array = thermal_np.copy()

        roi_values = thermal_roi_values.copy()
        maximum = np.amax(roi_values)
        minimum = np.amin(roi_values)
        #opt = int(input(f'Temp difference in selected area: {temp_diff}C. Proceed with scaling? 1-Yes 0-No: ' ))
        opt=1
        if opt==1:
            #print(f'New maximum Temp: {maximum}',f'\nNew minimum Temp: {minimum}\n')
            temp_array[temp_array>maximum] = maximum
            temp_array[temp_array<minimum] = minimum
        else:
            logger.warning('Returning unscaled temperature image')
        return temp_array 
Example #27
Source File: main.py    From atxserver2-ios-provider with MIT License 5 votes vote down vote up
def post(self, udid=None):
        udid = udid or self.get_argument('udid', None)
        assert udid
        d = idevices.get(udid)
        try:
            if not d:
                raise Exception("Device not found")

            d.restart_wda_proxy() # change wda public port
            wda_url = "http://{}:{}".format(current_ip(), d.public_port)
            await d.wda_healthcheck()
            await hbc.device_update({
                "udid": udid,
                "colding": False,
                "provider": {
                    "wdaUrl": wda_url,
                }
            })
            self.write({
                "success": True,
                "description": "Device successfully colded"
            })
        except Exception as e:
            logger.warning("colding procedure got error: %s", e)
            self.set_status(400)  # bad request
            self.write({
                "success": False,
                "description": "udid: %s not found" % udid
            }) 
Example #28
Source File: actions.py    From chaostoolkit-aws with Apache License 2.0 4 votes vote down vote up
def stop_instance(instance_id: str = None, az: str = None, force: bool = False,
                  filters: List[Dict[str, Any]] = None,
                  configuration: Configuration = None,
                  secrets: Secrets = None) -> List[AWSResponse]:
    """
    Stop a single EC2 instance.

    You may provide an instance id explicitly or, if you only specify the AZ,
    a random instance will be selected. If you need more control, you can
    also provide a list of filters following the documentation
    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_instances
    """

    if not az and not instance_id and not filters:
        raise FailedActivity(
            "To stop an EC2 instance, you must specify either the instance id,"
            " an AZ to pick a random instance from, or a set of filters.")

    if az and not instance_id and not filters:
        logger.warning('Based on configuration provided I am going to '
                       'stop a random instance in AZ %s!' % az)

    client = aws_client('ec2', configuration, secrets)

    if not instance_id:
        filters = deepcopy(filters) if filters else []

        if az:
            filters.append({'Name': 'availability-zone', 'Values': [az]})
        instance_types = pick_random_instance(filters, client)

        if not instance_types:
            raise FailedActivity(
                "No instances in availability zone: {}".format(az))
    else:
        instance_types = get_instance_type_by_id([instance_id], client)

    logger.debug(
        "Picked EC2 instance '{}' from AZ '{}' to be stopped".format(
            instance_types, az))

    return stop_instances_any_type(instance_types=instance_types,
                                   force=force, client=client) 
Example #29
Source File: actions.py    From chaostoolkit-aws with Apache License 2.0 4 votes vote down vote up
def terminate_instance(instance_id: str = None, az: str = None,
                       filters: List[Dict[str, Any]] = None,
                       configuration: Configuration = None,
                       secrets: Secrets = None) -> List[AWSResponse]:
    """
    Terminates a single EC2 instance.

    An instance may be targeted by specifying it by instance-id. If only the
    availability-zone is provided, a random instances in that AZ will be
    selected and terminated. For more control, please reference the available
    filters found:
    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_instances
    """

    if not any([instance_id, az, filters]):
        raise FailedActivity('To terminate an EC2, you must specify the '
                             'instance-id, an Availability Zone, or provide a '
                             'set of filters')

    if az and not any([instance_id, filters]):
        logger.warning('Based on configuration provided I am going to '
                       'terminate a random instance in AZ %s!' % az)

    client = aws_client('ec2', configuration, secrets)
    if not instance_id:
        filters = deepcopy(filters) or []

        if az:
            filters.append({'Name': 'availability-zone', 'Values': [az]})
            logger.debug('Looking for instances in AZ: %s' % az)

        # Randomly select an instance
        instance_types = pick_random_instance(filters, client)

        if not instance_types:
            raise FailedActivity(
                'No instances found matching filters: %s' % str(filters))

        logger.debug('Instance selected: %s' % str(instance_types))
    else:
        instance_types = get_instance_type_by_id([instance_id], client)

    return terminate_instances_any_type(instance_types, client) 
Example #30
Source File: actions.py    From chaostoolkit-aws with Apache License 2.0 4 votes vote down vote up
def terminate_instances(instance_ids: List[str] = None, az: str = None,
                        filters: List[Dict[str, Any]] = None,
                        configuration: Configuration = None,
                        secrets: Secrets = None) -> List[AWSResponse]:
    """
    Terminates multiple EC2 instances

    A set of instances may be targeted by providing them as the instance-ids.

    WARNING: If  only an Availability Zone is specified, all instances in
    that AZ will be terminated.

    Additional filters may be used to narrow the scope:
    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_instances
    """
    if not any([instance_ids, az, filters]):
        raise FailedActivity('To terminate instances, you must specify the '
                             'instance-id, an Availability Zone, or provide a '
                             'set of filters')

    if az and not any([instance_ids, filters]):
        logger.warning('Based on configuration provided I am going to '
                       'terminate all instances in AZ %s!' % az)

    client = aws_client('ec2', configuration, secrets)
    if not instance_ids:
        filters = deepcopy(filters) or []

        if az:
            filters.append({'Name': 'availability-zone', 'Values': [az]})
            logger.debug('Looking for instances in AZ: %s' % az)

        # Select instances based on filters
        instance_types = list_instances_by_type(filters, client)

        if not instance_types:
            raise FailedActivity(
                'No instances found matching filters: %s' % str(filters))

        logger.debug('Instances in AZ %s selected: %s}.' % (
            az, str(instance_types)))
    else:
        instance_types = get_instance_type_by_id(instance_ids, client)

    return terminate_instances_any_type(instance_types, client)