<commit_before>import os, time, json
import boto.ec2
from st2reactor.container.containerservice import ContainerService
from st2client.client import Client

import logging

interval = 20

st2_endpoints = {
    'action': 'http://localhost:9101',
    'reactor': 'http://localhost:9102',
    'datastore': 'http://localhost:9103'
}

class EC2InstanceStatusSensor(object):
    __container_service = None
    __log = None
    ec2 = None

    def __init__(self, container_service):
        self.__container_service = container_service
        self.__log = self.__container_service.get_logger(self.__class__.__name__)
        self.ec2 = EC2Sensor()

    def setup(self):
        self.ec2.connect('us-west-2')
        pass

    def start(self):
        while True:
          triggers = []
          data = self.ec2.getInstanceDetails()
          for i in data:
            trigger = {}
            trigger['name'] = 'st2.ec2.instance_status'
            trigger['event_id'] = time.time()
            trigger['payload'] = data[i]
            trigger['payload']['instance_id'] = i
            triggers.append(trigger)
          try:
            self.__container_service.dispatch(triggers)
          except Exception, e:
            self.__log.error('Exception %s handling st2.ec2.instance_status', e)
          time.sleep(interval)

    def stop(self):
        pass

    def get_trigger_types(self):
        return [
            {'name': 'st2.ec2.instance_status', 'description': 'EC2 Instance Status Sensor', 'payload_info': ['instance_id', 'instance_type', 'launch_time', 'tags', 'image_id', 'ip_address', 'state', 'state_code']}
        ]

class EC2VolumeStatusSensor(object):
    __container_service = None
    __log = None
    ec2 = None

    def __init__(self, container_service):
        self.__container_service = container_service
        self.__log = self.__container_service.get_logger(self.__class__.__name__)
        self.ec2 = EC2Sensor()

    def setup(self):
        self.ec2.connect('us-west-2')
        pass

    def start(self):
        while True:
          triggers = []
          data = self.ec2.getVolumeDetails()
          for i in data:
            trigger = {}
            trigger['name'] = 'st2.ec2.volume_status'
            trigger['event_id'] = time.time()
            trigger['payload'] = data[i]
            trigger['payload']['volume_id'] = i
            triggers.append(trigger)
          try:
            self.__container_service.dispatch(triggers)
          except Exception, e:
            self.__log.error('Exception %s handling st2.ec2.instance_status', e)

          time.sleep(interval)

    def stop(self):
        pass

    def get_trigger_types(self):
        return [
            {'name': 'st2.ec2.volume_status', 'description': 'EC2 Volume Status Sensor', 'payload_info': ['volume_id', 'create_time', 'region', 'size', 'status', 'tags', 'type', 'attach_time', 'device_map', 'instance_id']}
        ]


class EC2Sensor(object):

    __access_key_id = None
    __secret_Access_key = None
    __conn = None

    def __init__(self):
        try:
          client = Client(st2_endpoints)
          aws_key_id = client.key.get_by_name('aws_access_key_id')
          if not aws_key_id:
            raise Exception('Key error with aws_access_key_id.')
          aws_secret_key = client.key.get_by_name('aws_secret_access_key')
          if not aws_secret_key:
            raise Exception('Key error with aws_secret_access_key.')
          self.__access_key_id = aws_key_id.value
          self.__secret_access_key = aws_secret_key.value
        except Exception, e:
          print e

    def connect(self,region):
        try:
          self.__conn = boto.ec2.connect_to_region(region,<commit_msg>Fixed typo in ec2sensors.py<commit_after>import os, time, json
import boto.ec2
from st2reactor.container.containerservice import ContainerService
from st2client.client import Client

import logging

interval = 20

st2_endpoints = {
    'action': 'http://localhost:9101',
    'reactor': 'http://localhost:9102',
    'datastore': 'http://localhost:9103'
}

class EC2InstanceStatusSensor(object):
    __container_service = None
    __log = None
    ec2 = None

    def __init__(self, container_service):
        self.__container_service = container_service
        self.__log = self.__container_service.get_logger(self.__class__.__name__)
        self.ec2 = EC2Sensor()

    def setup(self):
        self.ec2.connect('us-west-2')
        pass

    def start(self):
        while True:
          triggers = []
          data = self.ec2.getInstanceDetails()
          for i in data:
            trigger = {}
            trigger['name'] = 'st2.ec2.instance_status'
            trigger['event_id'] = time.time()
            trigger['payload'] = data[i]
            trigger['payload']['instance_id'] = i
            triggers.append(trigger)
          try:
            self.__container_service.dispatch(triggers)
          except Exception, e:
            self.__log.error('Exception %s handling st2.ec2.instance_status', e)
          time.sleep(interval)

    def stop(self):
        pass

    def get_trigger_types(self):
        return [
            {'name': 'st2.ec2.instance_status', 'description': 'EC2 Instance Status Sensor', 'payload_info': ['instance_id', 'instance_type', 'launch_time', 'tags', 'image_id', 'ip_address', 'state', 'state_code']}
        ]

class EC2VolumeStatusSensor(object):
    __container_service = None
    __log = None
    ec2 = None

    def __init__(self, container_service):
        self.__container_service = container_service
        self.__log = self.__container_service.get_logger(self.__class__.__name__)
        self.ec2 = EC2Sensor()

    def setup(self):
        self.ec2.connect('us-west-2')
        pass

    def start(self):
        while True:
          triggers = []
          data = self.ec2.getVolumeDetails()
          for i in data:
            trigger = {}
            trigger['name'] = 'st2.ec2.volume_status'
            trigger['event_id'] = time.time()
            trigger['payload'] = data[i]
            trigger['payload']['volume_id'] = i
            triggers.append(trigger)
          try:
            self.__container_service.dispatch(triggers)
          except Exception, e:
            self.__log.error('Exception %s handling st2.ec2.instance_status', e)

          time.sleep(interval)

    def stop(self):
        pass

    def get_trigger_types(self):
        return [
            {'name': 'st2.ec2.volume_status', 'description': 'EC2 Volume Status Sensor', 'payload_info': ['volume_id', 'create_time', 'region', 'size', 'status', 'tags', 'type', 'attach_time', 'device_map', 'instance_id']}
        ]


class EC2Sensor(object):

    __access_key_id = None
    __secret_Access_key = None
    __conn = None

    def __init__(self):
        try:
          client = Client(st2_endpoints)
          aws_key_id = client.keys.get_by_name('aws_access_key_id')
          if not aws_key_id:
            raise Exception('Key error with aws_access_key_id.')
          aws_secret_key = client.keys.get_by_name('aws_secret_access_key')
          if not aws_secret_key:
            raise Exception('Key error with aws_secret_access_key.')
          self.__access_key_id = aws_key_id.value
          self.__secret_access_key = aws_secret_key.value
        except Exception, e:
          print e

    def connect(self,region):
        try:
          self.__conn = boto.ec2.connect_to_region(region,
{'fraction_non_alphanumeric': 0.11371551031342084, 'fraction_numerical': 0.01326011251004554, 'mean_word_length': 2.448960739030023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7136835', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2224, 'n_words': 450}