text: #!/usr/bin/python
  
  # *****************************************************************************
  #
  # Copyright (c) 2016, EPAM SYSTEMS INC
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #    http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  # ******************************************************************************
  
  import logging
  import json
  import sys
  from dlab.fab import *
  from dlab.meta_lib import *
  from dlab.actions_lib import *
  import os
  import argparse
  
  parser = argparse.ArgumentParser()
  parser.add_argument('--uuid', type=str, default='')
  args = parser.parse_args()
  
  if __name__ == "__main__":
      instance_class = 'notebook'
      local_log_filename = "{}_{}_{}.log".format(os.environ['conf_resource'], os.environ['edge_user_name'],
                                                 os.environ['request_id'])
      local_log_filepath = "/logs/" + os.environ['conf_resource'] + "/" + local_log_filename
      logging.basicConfig(format='%(levelname)-8s [%(asctime)s]  %(message)s',
                          level=logging.DEBUG,
                          filename=local_log_filepath)
  
      # generating variables dictionary
      create_aws_config_files()
      edge_status = get_instance_status(
          os.environ['conf_service_base_name'] + '-' + os.environ['edge_user_name'] + '-edge')
      if edge_status != 'running':
          logging.info('ERROR: Edge node is unavailable! Aborting...')
          print 'ERROR: Edge node is unavailable! Aborting...'
          put_resource_status('edge', 'Unavailable', os.environ['ssn_dlab_path'], os.environ['conf_os_user'])
          append_result("Edge node is unavailable")
          sys.exit(1)
      print 'Generating infrastructure names and tags'
      notebook_config = dict()
      try:
          notebook_config['exploratory_name'] = os.environ['exploratory_name']
      except:
          notebook_config['exploratory_name'] = ''
      notebook_config['service_base_name'] = os.environ['conf_service_base_name']
      notebook_config['instance_type'] = os.environ['aws_notebook_instance_type']
      notebook_config['key_name'] = os.environ['conf_key_name']
      notebook_config['instance_name'] = os.environ['conf_service_base_name'] + "-" + os.environ[
          'edge_user_name'] + "-nb-" + notebook_config['exploratory_name'] + "-" + args.uuid
      if os.environ['application'] == 'deeplearning':
          notebook_config['primary_disk_size'] = '50'
      else:
          notebook_config['primary_disk_size'] = '12'
      if os.environ['application'] == 'zeppelin':
          if os.environ['notebook_multiple_emrs'] == 'true':
              notebook_config['expected_ami_name'] = os.environ['conf_service_base_name'] + "-" + os.environ[
                  'edge_user_name'] + '-' + os.environ['application'] + '-livy-notebook-image'
          else:
              notebook_config['expected_ami_name'] = os.environ['conf_service_base_name'] + "-" + os.environ[
                  'edge_user_name'] + '-' + os.environ['application'] + '-spark-notebook-image'
      elif os.environ['application'] == 'deeplearning':
          notebook_config['expected_ami_name'] = os.environ['notebook_deeplearning_ami_name']
      else:
          notebook_config['expected_ami_name'] = os.environ['conf_service_base_name'] + "-" + os.environ[
              'edge_user_name'] + '-' + os.environ['application'] + '-notebook-image'
      notebook_config['role_profile_name'] = os.environ['conf_service_base_name'].lower().replace('-', '_') + "-" + \
                                             os.environ['edge_user_name'] + "-nb-Profile"
      notebook_config['security_group_name'] = os.environ['conf_service_base_name'] + "-" + os.environ[
          'edge_user_name'] + "-nb-SG"
      notebook_config['tag_name'] = notebook_config['service_base_name'] + '-Tag'
  
      print 'Searching preconfigured images'
      ami_id = get_ami_id_by_name(notebook_config['expected_ami_name'], 'available')
      if ami_id != '':
          print 'Preconfigured image found. Using: ' + ami_id
          notebook_config['ami_id'] = ami_id
      else:
          if os.environ['application'] == 'deeplearning':
              print "ERROR: Deep Learning image wasn't found. Aborting..."
              append_result('ERROR: Deep Learning image was not found.')
              sys.exit(1)
          else:
              notebook_config['ami_id'] = get_ami_id(os.environ['aws_' + os.environ['conf_os_family'] + '_ami_name'])
              print 'No preconfigured image found. Using default one: ' + notebook_config['ami_id']
  
      tag = {"Key": notebook_config['tag_name'],
             "Value": "{}-{}-subnet".format(notebook_config['service_base_name'], os.environ['edge_user_name'])}
      notebook_config['subnet_cidr'] = get_subnet_by_tag(tag)
  
      # launching instance for notebook server
      try:
          logging.info('[CREATE NOTEBOOK INSTANCE]')
          print '[CREATE NOTEBOOK INSTANCE]'
          params = "--node_name {} --ami_id {} --instance_type {} --key_name {} --security_group_ids {} --subnet_id {} --iam_profile {} --infra_tag_name {} --infra_tag_value {} --instance_class {} --instance_disk_size {} --primary_disk_size {}" \
              .format(notebook_config['instance_name'], notebook_config['ami_id'], notebook_config['instance_type'],
                      notebook_config['key_name'], get_security_group_by_name(notebook_config['security_group_name']),
                      get_subnet_by_cidr(notebook_config['subnet_cidr']), notebook_config['role_profile_name'],
                      notebook_config['tag_name'], notebook_config['instance_name'], instance_class,
                      os.environ['notebook_disk_size'], notebook_config['primary_disk_size'])
          try:
              local("~/scripts/{}.py {}".format('common_create_instance', params))
          except:
              traceback.print_exc()
              raise Exception
      except Exception as err:
          append_result("Failed to create instance.", str(err))
          sys.exit(1)