Example #1
0
 def add_cash(self, value):
     self.cash_inside += value
     self.cash_banknotes += 1
     self.cash_session += value
     self.cash_last_pay_time = time.time()
     file_io.write("cash_inside_file", self.cash_inside)
     file_io.write("cash_banknotes_file", self.cash_banknotes)
Example #2
0
  def create_celery_worker_scripts(self, input_type):
    """ Creates the task worker python script. It uses
        a configuration file for setup.

    Args:
      input_type: Whether to use the config file or the 
                  database queue info. Default: config file.
    Returns: 
      The full path of the worker script.
    """
    queue_info = self._queue_info_file
    if input_type == self.QUEUE_INFO_DB:
      queue_info = self._queue_info_db 

    header_template = file_io.read(self.HEADER_LOC)
    task_template = file_io.read(self.TASK_LOC)
    header_template = header_template.replace("APP_ID", self._app_id)
    script = header_template.replace("CELERY_CONFIGURATION", 
                                     self._app_id) + '\n'
    for queue in queue_info['queue']:
      queue_name = queue['name']  
      # The queue name is used as a function name so replace invalid chars
      queue_name = queue_name.replace('-', '_')
      self.validate_queue_name(queue_name)
      new_task = task_template.replace("QUEUE_NAME", 
                     self.get_queue_function_name(queue_name))
      script += new_task + '\n'

    worker_file = self.get_celery_worker_script_path(self._app_id)
    file_io.write(worker_file, script)
    return worker_file
Example #3
0
    def create_celery_worker_scripts(self, input_type):
        """ Creates the task worker python script. It uses
        a configuration file for setup.

    Args:
      input_type: Whether to use the config file or the 
                  database queue info. Default: config file.
    Returns: 
      The full path of the worker script.
    """
        queue_info = self._queue_info_file
        if input_type == self.QUEUE_INFO_DB:
            queue_info = self._queue_info_db

        header_template = file_io.read(self.HEADER_LOC)
        task_template = file_io.read(self.TASK_LOC)
        header_template = header_template.replace("APP_ID", self._app_id)
        script = header_template.replace("CELERY_CONFIGURATION",
                                         self._app_id) + '\n'
        for queue in queue_info['queue']:
            queue_name = queue['name']
            # The queue name is used as a function name so replace invalid chars
            queue_name = queue_name.replace('-', '_')
            self.validate_queue_name(queue_name)
            new_task = task_template.replace(
                "QUEUE_NAME", self.get_queue_function_name(queue_name))
            script += new_task + '\n'

        worker_file = self.get_celery_worker_script_path(self._app_id)
        file_io.write(worker_file, script)
        return worker_file
Example #4
0
  def create_celery_worker_scripts(self):
    """ Creates the task worker python script. It uses a configuration file
    for setup.

    Returns:
      The full path of the worker script.
    """
    header_template = file_io.read(self.HEADER_LOC)
    task_template = file_io.read(self.TASK_LOC)
    header_template = header_template.replace("APP_ID", self._app_id)
    script = header_template.replace("CELERY_CONFIGURATION", self._app_id) + \
      '\n'

    for name, queue in self.queues.iteritems():
      # Celery only handles push queues.
      if not isinstance(queue, PushQueue):
        continue

      # The queue name is used as a function name so replace invalid chars
      queue_name = queue.name.replace('-', '_')
      new_task = task_template.\
        replace("QUEUE_NAME", self.get_queue_function_name(queue_name))
      # For tasks generated by mapreduce, or destined to be run by a module,
      # the hostname may have a prefix that corresponds to a different
      # subdomain.
      # AppScale does not support that type of routing currently, so the main
      # loadbalancer IP/hostname is used here for the execution of a task.
      new_task = new_task.\
        replace("PUBLIC_IP", "\"{}\"".format(self.get_public_ip()))
      script += new_task + '\n'

    worker_file = self.get_celery_worker_script_path(self._app_id)
    file_io.write(worker_file, script)
    return worker_file
Example #5
0
def create_config_file(watch,
                       start_cmd,
                       stop_cmd,
                       ports,
                       env_vars={},
                       max_memory=500,
                       syslog_server=""):
    """ Reads in a template file for monit and fills it with the 
      correct configuration. The caller is responsible for deleting 
      the created file.
  
  Args:
    watch: A string which identifies this process with monit
    start_cmd: The start command to start the process
    stop_cmd: The stop command to kill the process
    ports: A list of ports that are being watched
    env_vars: The environment variables used when starting the process
    max_memory: An int that names the maximum amount of memory that this process
      is allowed to use (in megabytes) before monit should restart it.
    syslog_server: The IP of the remote syslog server to use.
  Returns:
    The name of the created configuration file. 
  Raises: 
    TypeError with bad argument types
  """
    if not isinstance(watch, str): raise TypeError("Expected str")
    if not isinstance(start_cmd, str): raise TypeError("Expected str")
    if not isinstance(stop_cmd, str): raise TypeError("Expected str")
    if not isinstance(ports, list): raise TypeError("Expected list")
    if not isinstance(env_vars, dict): raise TypeError("Expected dict")

    env = ""
    for ii in env_vars:
        env += "export " + str(ii) + "=\"" + str(env_vars[ii]) + "\" && "

    # Convert ints to strings for template formatting
    for index, ii in enumerate(ports):
        ports[index] = str(ii)

    # 'WATCH' and 'port' are substituted here as the last two arguments
    # because the template script itself uses {}. If we do not sub for them
    # a key error is raised by template.format().
    template = ""
    for port in ports:
        if syslog_server:
            template = file_io.read(TEMPLATE_LOCATION_SYSLOG)
            template = template.format(watch, start_cmd, stop_cmd, port, env,
                                       max_memory, syslog_server)
        else:
            template = file_io.read(TEMPLATE_LOCATION)
            template = template.format(watch, start_cmd, stop_cmd, port, env,
                                       max_memory)

        temp_file_name = "/etc/monit/conf.d/appscale-" + watch + '-' + \
                         str(port) + ".cfg"
        file_io.write(temp_file_name, template)

    return
def create_config_file(watch, start_cmd, stop_cmd, ports, env_vars={},
  max_memory=500, syslog_server="", host=None):
  """ Reads in a template file for monit and fills it with the 
      correct configuration. The caller is responsible for deleting 
      the created file.
  
  Args:
    watch: A string which identifies this process with monit
    start_cmd: The start command to start the process
    stop_cmd: The stop command to kill the process
    ports: A list of ports that are being watched
    env_vars: The environment variables used when starting the process
    max_memory: An int that names the maximum amount of memory that this process
      is allowed to use (in megabytes) before monit should restart it.
    syslog_server: The IP of the remote syslog server to use.
    host: The private IP of a server that runs the appengine role; used for 
      reliably detecting a running app server process.
  Returns:
    The name of the created configuration file. 
  Raises: 
    TypeError with bad argument types
  """
  if not isinstance(watch, str): raise TypeError("Expected str")
  if not isinstance(start_cmd, str): raise TypeError("Expected str")
  if not isinstance(stop_cmd, str): raise TypeError("Expected str")
  if not isinstance(ports, list): raise TypeError("Expected list")
  if not isinstance(env_vars, dict): raise TypeError("Expected dict")

  env = ""
  for ii in env_vars:
    env += "export " + str(ii) + "=\"" + str(env_vars[ii]) + "\" && "

  # Convert ints to strings for template formatting
  for index, ii in enumerate(ports): ports[index] = str(ii) 

  # 'WATCH' and 'port' are substituted here as the last two arguments 
  # because the template script itself uses {}. If we do not sub for them 
  # a key error is raised by template.format().
  template = ""
  for port in ports:
    if syslog_server:
      template = file_io.read(TEMPLATE_LOCATION_SYSLOG)
      template = template.format(watch, start_cmd, stop_cmd, port, env,
        max_memory, syslog_server)
    else:
      template = file_io.read(TEMPLATE_LOCATION)
      template = template.format(watch, start_cmd, stop_cmd, port, env,
        max_memory)

    if host:
      template += "  if failed host {} port {} then restart\n".\
        format(host, port)

    config_file = '{}/appscale-{}-{}.cfg'.\
      format(MONIT_CONFIG_DIR, watch, port)
    file_io.write(config_file, template)

  return
Example #7
0
  def create_celery_file(self):
    """ Creates the Celery configuration file describing queues and exchanges
    for an application. Uses the queue.yaml/queue.xml input.

    Returns:
      A string representing the full path location of the 
      configuration file.
    """
    celery_queues = []
    annotations = []
    for name, queue in self.queues.iteritems():
      # Celery only handles push queues.
      if not isinstance(queue, PushQueue):
        continue

      celery_name = TaskQueueConfig.get_celery_queue_name(
        self._app_id, queue.name)
      queue_str = "Queue('{name}', Exchange('{app}'), routing_key='{key}'),"\
        .format(name=celery_name, app=self._app_id, key=celery_name)
      celery_queues.append(queue_str)

      annotation_name = TaskQueueConfig.get_celery_annotation_name(
        self._app_id, queue.name)
      annotation = "'{name}': {{'rate_limit': '{rate}'}},".format(
        name=annotation_name, rate=queue.rate)
      annotations.append(annotation)

    config = """
from kombu import Exchange
from kombu import Queue
CELERY_QUEUES = (
{queues}
)
CELERY_ANNOTATIONS = {{
{annotations}
}}
# Everytime a task is enqueued a temporary queue is created to store
# results into rabbitmq. This can be bad in a high enqueue environment
# We use the following to make sure these temp queues are not created. 
# See http://stackoverflow.com/questions/7144025/temporary-queue-made-in-celery
# for more information on this issue.
CELERY_IGNORE_RESULT = True
CELERY_STORE_ERRORS_EVEN_IF_IGNORED = False

# One month expiration date because a task can be deferred that long.
CELERY_AMQP_TASK_RESULT_EXPIRES = 2678400

# Disable prefetching for celery workers. If tasks are small in duration this
# should be set to a higher value (64-128) for increased performance.
# See: http://celery.readthedocs.org/en/latest/userguide/optimizing.html#worker-settings
CELERYD_PREFETCH_MULTIPLIER = 1
""".format(queues='\n'.join(celery_queues),
           annotations='\n'.join(annotations))

    config_file = self._app_id + ".py"
    file_io.write(self.CELERY_CONFIG_DIR + config_file, config)
    return self.CELERY_CONFIG_DIR + config_file
Example #8
0
def writeFile():
    global listScores
    names1 = []
        
    for (score, name2) in sorted(listScores.items()):
         names1.append(str(score) + "\t" + str(name2))

    ##   # Write the results to a file.
    file_io.write(names1,"listScores.csv")
Example #9
0
def create_markup(filename):
    first = True
    res = ""
    a = decode_wav(filename)
    for element in a:
        if first:
            first = False
        else:
            res = res + " "
        res = res + str(element)
    file_io.write(filename.split(".")[0] + ".mp3_markup", res)
Example #10
0
def create_config_file(watch, start_cmd, stop_cmd, ports, env_vars={}):
  """ Reads in a template file for god and fills it with the 
      correct configuration. The caller is responsible for deleting 
      the created file.
  
  Args:
    watch: A string which identifies this process with god
    start_cmd: The start command to start the process
    stop_cmd: The stop command to kill the process
    ports: A list of ports that are being watched
    env_vars: The environment variables used when starting the process
  Returns:
    The name of the created configuration file. 
  Raises: 
    TypeError with bad argument types
  """

  if not isinstance(watch, str): raise TypeError("Expected str")
  if not isinstance(start_cmd, str): raise TypeError("Expected str")
  if not isinstance(stop_cmd, str): raise TypeError("Expected str")
  if not isinstance(ports, list): raise TypeError("Expected list")
  if not isinstance(env_vars, dict): raise TypeError("Expected dict")

  template = file_io.read(TEMPLATE_LOCATION)

  env = ""
  for ii in env_vars:
    env += "          \"" + str(ii) + "\" => \"" + str(env_vars[ii]) + "\",\n" 
  if env: env = "w.env = {" + env + "}"

  # Convert ints to strings for template formatting
  for index, ii in enumerate(ports): ports[index] = str(ii) 

  # 'WATCH' and 'port' are substituted here as the last two arguments 
  # because the template script itself uses {}. If we do not sub for them 
  # a key error is raised by template.format().
  template = template.format(watch, 
                             start_cmd, 
                             stop_cmd, 
                             ', '.join(ports),
                             env,
                             "{WATCH}",
                             "{port}")

  temp_file_name = "/tmp/god-" + watch + '-' + \
                   str(random.randint(0, 9999999)) + ".conf"

  file_io.write(temp_file_name, template) 

  return temp_file_name
def create_config_file(watch, start_cmd, stop_cmd, ports, env_vars={}):
  """ Reads in a template file for god and fills it with the 
      correct configuration. The caller is responsible for deleting 
      the created file.
  
  Args:
    watch: A string which identifies this process with god
    start_cmd: The start command to start the process
    stop_cmd: The stop command to kill the process
    ports: A list of ports that are being watched
    env_vars: The environment variables used when starting the process
  Returns:
    The name of the created configuration file. 
  Raises: 
    TypeError with bad argument types
  """
  if not isinstance(watch, str): raise TypeError("Expected str")
  if not isinstance(start_cmd, str): raise TypeError("Expected str")
  if not isinstance(stop_cmd, str): raise TypeError("Expected str")
  if not isinstance(ports, list): raise TypeError("Expected list")
  if not isinstance(env_vars, dict): raise TypeError("Expected dict")

  template = file_io.read(TEMPLATE_LOCATION)

  env = ""
  for ii in env_vars:
    env += "          \"" + str(ii) + "\" => \"" + str(env_vars[ii]) + "\",\n" 
  if env: env = "w.env = {" + env + "}"

  # Convert ints to strings for template formatting
  for index, ii in enumerate(ports): ports[index] = str(ii) 

  # 'WATCH' and 'port' are substituted here as the last two arguments 
  # because the template script itself uses {}. If we do not sub for them 
  # a key error is raised by template.format().
  template = template.format(watch, 
                             start_cmd, 
                             stop_cmd, 
                             ', '.join(ports),
                             env,
                             "{watch_name}",
                             "{port}")

  temp_file_name = "/tmp/god-" + watch + '-' + \
                   str(random.randint(0, 9999999)) + ".conf"

  file_io.write(temp_file_name, template) 

  return temp_file_name
Example #12
0
def create_test_yaml():
    file_loc = FILE_LOC
    config = \
  """
queue:
- name: default
  rate: 5/s
- name: foo
  rate: 10/m
"""
    try:
        os.mkdir("/var/apps/test_app")
        os.mkdir("/var/apps/test_app/app/")
    except OSError:
        pass
    file_io.write(file_loc, config)
Example #13
0
def create_test_yaml():
  file_loc = FILE_LOC
  config = \
"""
queue:
- name: default
  rate: 5/s
- name: foo
  rate: 10/m
"""
  try:
    os.mkdir("/var/apps/test_app")
    os.mkdir("/var/apps/test_app/app/")
  except OSError:
    pass
  file_io.write(file_loc, config)
Example #14
0
    def __init__(self, money_acceptor_object, card_dispenser_object):
        self.money_acceptor_object = money_acceptor_object
        self.card_dispenser_object = card_dispenser_object

        self.phone1 = file_io.read("gsm_phone1_file")
        if self.phone1 == 0:
            self.phone1 = dconfig.gsm_phone1_default
            file_io.write("gsm_phone1_file", self.phone1)

        self.phone2 = file_io.read("gsm_phone2_file")
        if self.phone2 == 0:
            self.phone2 = dconfig.gsm_phone2_default
            file_io.write("gsm_phone2_file", self.phone2)

        threading.Thread.__init__(self)
        self.new_ser = serial.Serial(dconfig.gsm_device, 9600)
        self.power_on()
def create_config_file(watch, start_cmd, stop_cmd, ports, env_vars={},
  max_memory=500):
  """ Reads in a template file for monit and fills it with the 
      correct configuration. The caller is responsible for deleting 
      the created file.
  
  Args:
    watch: A string which identifies this process with monit
    start_cmd: The start command to start the process
    stop_cmd: The stop command to kill the process
    ports: A list of ports that are being watched
    env_vars: The environment variables used when starting the process
    max_memory: An int that names the maximum amount of memory that this process
      is allowed to use (in megabytes) before monit should restart it.
  Returns:
    The name of the created configuration file. 
  Raises: 
    TypeError with bad argument types
  """
  if not isinstance(watch, str): raise TypeError("Expected str")
  if not isinstance(start_cmd, str): raise TypeError("Expected str")
  if not isinstance(stop_cmd, str): raise TypeError("Expected str")
  if not isinstance(ports, list): raise TypeError("Expected list")
  if not isinstance(env_vars, dict): raise TypeError("Expected dict")

  template = file_io.read(TEMPLATE_LOCATION)

  env = ""
  for ii in env_vars:
    env += "export " + str(ii) + "=\"" + str(env_vars[ii]) + "\" && "

  # Convert ints to strings for template formatting
  for index, ii in enumerate(ports): ports[index] = str(ii) 

  # 'WATCH' and 'port' are substituted here as the last two arguments 
  # because the template script itself uses {}. If we do not sub for them 
  # a key error is raised by template.format().
  for port in ports:
    template = template.format(watch, start_cmd, stop_cmd, port, env,
      max_memory)
    temp_file_name = "/etc/monit/conf.d/" + watch + '-' + \
                     str(port) + ".cfg"
    file_io.write(temp_file_name, template) 

  return
Example #16
0
def create_test_yaml():
    file_loc = FILE_LOC
    config = \
  """
queue:
- name: default
  rate: 5/s
- name: foo
  rate: 10/m
"""
    FILE = file_io.write(config, file_loc)
Example #17
0
def request_data(sub, size, fields, epoch, initial):
    url = 'https://api.pushshift.io/reddit/search/comment/?subreddit=' + str(
        sub) + '&size=' + str(size) + '&fields=' + str(
            fields) + '&before=' + str(epoch)
    #print(url)
    r = url_request(url)
    data = json.loads(r.text)
    obj_num = len(data["data"])

    if obj_num == 0:
        return epoch, -1

    epoch = data["data"][obj_num - 1]["created_utc"]

    if not initial:
        data = file_io.update(data)

    file_io.write(data)

    return epoch, obj_num
Example #18
0
def create_test_yaml():
  file_loc = FILE_LOC
  config = \
"""
queue:
- name: default
  rate: 5/s
- name: foo
  rate: 10/m
"""
  FILE = file_io.write(config, file_loc)
Example #19
0
    def __start_working_action(self):
        print self.new_ser.name
        while True:
            time.sleep(0.01)
            if self.new_ser.inWaiting():
                response = self.read()

                if response == 'a':
                    self.send_status("normal")
                elif response == 'b':
                    self.money_acceptor_object.reset()
                    self.send_reset_banknotes()
                elif response == 'c':
                    self.card_dispenser_object.reset()
                    self.send_reset_cards()
                elif response == 'd':
                    self.money_acceptor_object.set_capacity(self.get_number('e'))
                    self.money_acceptor_object.reset()
                    self.send_banknotes_limit_set()
                elif response == 'f':
                    self.card_dispenser_object.set_capacity(self.get_number('g'))
                    self.card_dispenser_object.reset()
                    self.send_reset_cards()
                elif response == 'h':
                    self.money_acceptor_object.set_price(self.get_number('i'))
                    self.send_price_set()
                elif response == 'j':
                    self.phone1 = (self.get_number('k'))
                    file_io.write("gsm_phone1_file", self.phone1)
                    self.send_number(1, self.phone1, True)
                elif response == 'l':
                    self.phone2 = (self.get_number('m'))
                    file_io.write("gsm_phone2_file", self.phone2)
                    self.send_number(2, self.phone2, True)
                else:
                    pass
Example #20
0
 def set_capacity(self, capacity):
     self.capacity = capacity
     file_io.write("cash_capacity_file", capacity)
Example #21
0
    def create_celery_file(self, input_type):
        """ Creates the Celery configuration file describing 
        queues and exchanges for an application. Uses either
        the queue.yaml/queue.xml input or what was stored in the 
        datastore to create the celery file. 

    Args:
      input_type: Whether to use the config file or the 
                  database queue info. Default: config file.
    Returns:
      A string representing the full path location of the 
      configuration file.
    """
        queue_info = self._queue_info_file
        if input_type == self.QUEUE_INFO_DB:
            queue_info = self._queue_info_db

        celery_queues = []
        celery_annotations = []
        for queue in queue_info['queue']:
            if 'mode' in queue and queue['mode'] == "pull":
                continue  # celery does not handle pull queues
            celery_queue_name = \
              TaskQueueConfig.get_celery_queue_name(self._app_id, queue['name'])
            celery_queues.append("Queue('" + celery_queue_name + \
               "', Exchange('" + self._app_id + \
               "'), routing_key='" + celery_queue_name  + "'),")

            rate_limit = self.DEFAULT_RATE
            if 'rate' in queue:
                rate_limit = queue['rate']

            annotation_name = \
              TaskQueueConfig.get_celery_annotation_name(self._app_id,
                                                         queue['name'])
            celery_annotations.append("'" + annotation_name + \
               "': {'rate_limit': '" + rate_limit + "'},")

        celery_queues = '\n'.join(celery_queues)
        celery_annotations = '\n'.join(celery_annotations)
        config = \
    """
from kombu import Exchange
from kombu import Queue
CELERY_QUEUES = (
"""
        config += celery_queues
        config += \
    """
)
CELERY_ANNOTATIONS = {
"""
        config += celery_annotations
        config += \
    """
}
# Everytime a task is enqueued a temporary queue is created to store
# results into rabbitmq. This can be bad in a high enqueue environment
# We use the following to make sure these temp queues are not created. 
# See http://stackoverflow.com/questions/7144025/temporary-queue-made-in-celery
# for more information on this issue.
CELERY_IGNORE_RESULT = True
CELERY_STORE_ERRORS_EVEN_IF_IGNORED = False

# One month expiration date because a task can be deferred that long.
CELERY_AMQP_TASK_RESULT_EXPIRES = 2678400
"""
        config_file = self._app_id + ".py"
        file_io.write(self.CELERY_CONFIG_DIR + config_file, config)
        return self.CELERY_CONFIG_DIR + config_file
Example #22
0
 def __give_card_action(self):
     GPIO.output(dconfig.card_dispenser_pin, 0)
     self.cards_given += 1
     file_io.write("card_dispenser_file", self.cards_given)
     time.sleep(1)
     GPIO.output(dconfig.card_dispenser_pin, 1)
Example #23
0
  def create_celery_file(self, input_type):
    """ Creates the Celery configuration file describing 
        queues and exchanges for an application. Uses either
        the queue.yaml/queue.xml input or what was stored in the 
        datastore to create the celery file. 

    Args:
      input_type: Whether to use the config file or the 
                  database queue info. Default: config file.
    Returns:
      A string representing the full path location of the 
      configuration file.
    """
    queue_info = self._queue_info_file
    if input_type == self.QUEUE_INFO_DB:
      queue_info = self._queue_info_db 
 
    celery_queues = []
    celery_annotations = []
    for queue in queue_info['queue']:
      if 'mode' in queue and queue['mode'] == "pull":
        continue # celery does not handle pull queues
      celery_queue_name = \
        TaskQueueConfig.get_celery_queue_name(self._app_id, queue['name'])
      celery_queues.append("Queue('" + celery_queue_name + \
         "', Exchange('" + self._app_id + \
         "'), routing_key='" + celery_queue_name  + "'),")

      rate_limit = self.DEFAULT_RATE
      if 'rate' in queue:
        rate_limit = queue['rate']

      annotation_name = \
        TaskQueueConfig.get_celery_annotation_name(self._app_id,
                                                   queue['name'])
      celery_annotations.append("'" + annotation_name + \
         "': {'rate_limit': '" + rate_limit + "'},")

    celery_queues = '\n'.join(celery_queues)
    celery_annotations = '\n'.join(celery_annotations)
    config = \
"""
from kombu import Exchange
from kombu import Queue
CELERY_QUEUES = (
"""
    config += celery_queues
    config += \
"""
)
CELERY_ANNOTATIONS = {
"""
    config += celery_annotations
    config += \
"""
}
# Everytime a task is enqueued a temporary queue is created to store
# results into rabbitmq. This can be bad in a high enqueue environment
# We use the following to make sure these temp queues are not created. 
# See http://stackoverflow.com/questions/7144025/temporary-queue-made-in-celery
# for more information on this issue.
CELERY_IGNORE_RESULT = True
CELERY_STORE_ERRORS_EVEN_IF_IGNORED = False

# One month expiration date because a task can be deferred that long.
CELERY_AMQP_TASK_RESULT_EXPIRES = 2678400
"""
    config_file = self._app_id + ".py" 
    file_io.write(self.CELERY_CONFIG_DIR + config_file, config)
    return self.CELERY_CONFIG_DIR + config_file
Example #24
0
 def reset(self):
     self.cards_given = 0
     file_io.write("card_dispenser_file", self.cards_given)
     self.card_send_warning = False
def start_app(config):
  """ Starts a Google App Engine application on this machine. It 
      will start it up and then proceed to fetch the main page.
  
  Args:
    config: a dictionary that contains 
       app_name: Name of the application to start
       app_port: Port to start on 
       language: What language the app is written in
       load_balancer_ip: Public ip of load balancer
       load_balancer_port: Port of load balancer
       xmpp_ip: IP of XMPP service 
       dblocations: List of database locations 
       env_vars: A dict of environment variables that should be passed to the
        app.
  Returns:
    PID of process on success, -1 otherwise
  """
  config = convert_config_from_json(config)
  if config == None:
    logging.error("Invalid configuration for application")
    return BAD_PID 
  
  if not misc.is_app_name_valid(config['app_name']):
    logging.error("Invalid app name for application: " +\
                  config['app_name'])
    return BAD_PID
  logging.info("Starting %s application %s"%(config['language'], 
                                             config['app_name']))

  start_cmd = ""
  stop_cmd = ""
  env_vars = config['env_vars']
  watch = "app___" + config['app_name']
 
  if config['language'] == constants.PYTHON or \
        config['language'] == constants.PYTHON27 or \
        config['language'] == constants.GO:
    start_cmd = create_python_start_cmd(config['app_name'],
                            config['load_balancer_ip'],
                            config['app_port'],
                            config['load_balancer_ip'],
                            config['load_balancer_port'],
                            config['xmpp_ip'],
                            config['dblocations'],
                            config['language'])
    logging.info(start_cmd)
    stop_cmd = create_python_stop_cmd(config['app_port'], config['language'])
    env_vars.update(create_python_app_env(config['load_balancer_ip'],
                            config['load_balancer_port'], 
                            config['app_name']))
  elif config['language'] == constants.JAVA:
    copy_successful = copy_modified_jars(config['app_name'])
    if not copy_successful:
      return BAD_PID
    start_cmd = create_java_start_cmd(config['app_name'],
                            config['app_port'],
                            config['load_balancer_ip'],
                            config['load_balancer_port'],
                            config['dblocations'])
    stop_cmd = create_java_stop_cmd(config['app_port'])
    env_vars.update(create_java_app_env())
  else:
    logging.error("Unknown application language %s for appname %s"\
                  %(config['language'], config['app_name'])) 
    return BAD_PID

  logging.info("Start command: " + str(start_cmd))
  logging.info("Stop command: " + str(stop_cmd))
  logging.info("Environment variables: " +str(env_vars))

  config_file_loc = god_app_configuration.create_config_file(str(watch),
                                                     str(start_cmd), 
                                                     str(stop_cmd), 
                                                     [config['app_port']],
                                                     env_vars)

  if not god_interface.start(config_file_loc, watch):
    logging.error("Unable to start application server with god")
    return BAD_PID

  if not wait_on_app(int(config['app_port'])):
    logging.error("Application server did not come up in time, " + \
                   "removing god watch")
    god_interface.stop(watch)
    return BAD_PID

  pid = get_pid_from_port(config['app_port'])
  pid_file = constants.APP_PID_DIR + config['app_name'] + '-' +\
             str(config['app_port'])
  file_io.write(pid_file, str(pid))
      
  return pid
Example #26
0
def start_app(config):
  """ Starts a Google App Engine application on this machine. It 
      will start it up and then proceed to fetch the main page.
  
  Args:
    config: a dictionary that contains 
       app_name: Name of the application to start
       app_port: Port to start on 
       language: What language the app is written in
       load_balancer_ip: Public ip of load balancer
       load_balancer_port: Port of load balancer
       xmpp_ip: IP of XMPP service 
       dblocations: List of database locations 
  Returns:
    PID of process on success, -1 otherwise
  """

  config = convert_config_from_json(config)
  if config == None:
    logging.error("Invalid configuration for application")
    return BAD_PID 
  
  logging.info("Starting %s application %s"%(config['language'], 
                                             config['app_name']))

  start_cmd = ""
  stop_cmd = ""
  env_vars = {}
  watch = "app___" + config['app_name']
 
  if config['language'] == constants.PYTHON or \
     config['language'] == constants.GO:
    start_cmd = create_python_start_cmd(config['app_name'],
                            config['load_balancer_ip'],
                            config['app_port'],
                            config['load_balancer_ip'],
                            config['load_balancer_port'],
                            config['xmpp_ip'],
                            config['dblocations'])
    stop_cmd = create_python_stop_cmd(config['app_port'])
    env_vars = create_python_app_env(config['load_balancer_ip'], 
                            config['load_balancer_port'], 
                            config['app_name'])
  elif config['language'] == constants.JAVA:
    start_cmd = create_java_start_cmd(config['app_name'],
                            config['app_port'],
                            config['load_balancer_ip'],
                            config['load_balancer_port'],
                            config['dblocations'])
    stop_cmd = create_java_stop_cmd(config['app_port'])
    env_vars = create_java_app_env()
  else:
    logging.error("Unknown application language %s for appname %s"\
                  %(config['language'], config['app_name'])) 
    return BAD_PID

  logging.info("Start command: " + str(start_cmd))
  logging.info("Stop command: " + str(stop_cmd))
  logging.info("Environment variables: " +str(env_vars))

  config_file_loc = god_app_interface.create_config_file(str(watch),
                                                     str(start_cmd), 
                                                     str(stop_cmd), 
                                                     [config['app_port']],
                                                     env_vars)

  if not god_interface.start(config_file_loc, watch):
    logging.error("Unable to start application server with god")
    return BAD_PID

  if not wait_on_app(int(config['app_port'])):
    logging.error("Application server did not come up in time, " + \
                   "removing god watch")
    god_interface.stop(watch)
    return BAD_PID

  pid = get_pid_from_port(config['app_port'])
  pid_file = constants.APP_PID_DIR + config['app_name'] + '-' +\
             str(config['app_port'])
  file_io.write(pid_file, str(pid))
      
  return pid
            tok = tok.replace(',','')
            if tok in syn:
                tok = syn[tok]
            tok = lemmatizer.lemmatize(tok)
            lemmatized_data.append(tok)    

        #count occurances of each word
        count = Counter(lemmatized_data)
        count = count.most_common()

        identities_attacked = []

        for pair in count:
            w = pair[0]
            c = pair[1]
            if w in slurs:
                identities_attacked.append(slurs[w])
                if w in word_count:
                    word_count[w] += c
                else:
                    word_count[w] = c

        datum['identities'] = identities_attacked
        sub_data.append(datum)

    data = {'data': sub_data}
    file_io.write(data)

    with open(slur_count_file,'w',encoding='utf-8') as f:
        for w in sorted(word_count, key=word_count.get, reverse=True):
            f.write('{},{},{}\n'.format(w,word_count[w],slurs[w]))          
Example #28
0
def create_config_file(watch,
                       start_cmd,
                       stop_cmd,
                       ports,
                       env_vars={},
                       max_memory=500,
                       syslog_server="",
                       host=None,
                       upgrade_flag=False,
                       match_cmd=""):
    """ Reads in a template file for monit and fills it with the 
      correct configuration. The caller is responsible for deleting 
      the created file.
  
  Args:
    watch: A string which identifies this process with monit
    start_cmd: The start command to start the process
    stop_cmd: The stop command to kill the process
    ports: A list of ports that are being watched
    env_vars: The environment variables used when starting the process
    max_memory: An int that names the maximum amount of memory that this process
      is allowed to use (in megabytes) before monit should restart it.
    syslog_server: The IP of the remote syslog server to use.
    host: The private IP of a server that runs the appengine role; used for 
      reliably detecting a running app server process.
  Returns:
    The name of the created configuration file. 
  Raises: 
    TypeError with bad argument types
  """
    if not isinstance(watch, str): raise TypeError("Expected str")
    if not isinstance(start_cmd, str): raise TypeError("Expected str")
    if not isinstance(stop_cmd, str): raise TypeError("Expected str")
    if not isinstance(ports, list): raise TypeError("Expected list")
    if not isinstance(env_vars, dict): raise TypeError("Expected dict")

    env = ""
    for ii in env_vars:
        env += "export " + str(ii) + "=\"" + str(env_vars[ii]) + "\" && "

    # Convert ints to strings for template formatting
    for index, ii in enumerate(ports):
        ports[index] = str(ii)

    # 'WATCH' and 'port' are substituted here as the last two arguments
    # because the template script itself uses {}. If we do not sub for them
    # a key error is raised by template.format().
    template = ""
    for port in ports:
        if syslog_server:
            template = file_io.read(TEMPLATE_LOCATION_SYSLOG)
            template = template.format(watch, start_cmd, stop_cmd, port, env,
                                       max_memory, syslog_server)
        else:
            if upgrade_flag:
                template = file_io.read(TEMPLATE_LOCATION_FOR_UPGRADE)
                template = template.format(watch=watch,
                                           start=start_cmd,
                                           stop=stop_cmd,
                                           port=port,
                                           match=match_cmd,
                                           env=env,
                                           memory=max_memory)
            else:
                template = file_io.read(TEMPLATE_LOCATION)
                template = template.format(watch, start_cmd, stop_cmd, port,
                                           env, max_memory)

        if host:
            template += "  if failed host {} port {} then restart\n".\
              format(host, port)

        config_file = '{}/appscale-{}-{}.cfg'.\
          format(MONIT_CONFIG_DIR, watch, port)
        file_io.write(config_file, template)

    return
Example #29
0
 def reset(self):
     self.cash_inside = 0
     self.cash_banknotes = 0
     file_io.write("cash_inside_file", self.cash_inside)
     file_io.write("cash_banknotes_file", self.cash_banknotes)
     self.money_send_warning = False
Example #30
0
 def set_price(self, price):
     self.price = price
     file_io.write("cash_price_file", price)