Esempio n. 1
0
  def post(self):                     #  Create POST endpoint
    """
    Create a VM in Azure cloud from an image
    """
    conf = config.readConfig()
    if conf == 1:
      logger.error('Missing configuration')
      return 'Internal Error : Missing configuration. Fix it at ' + request.url_root + 'config'
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Internal Error : Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['resource_group'] = api.payload['vm_resourcegroup']
    extra_vars['image'] = api.payload['vm_image']
    extra_vars['size'] = api.payload['vm_size']
    extra_vars['owner'] = api.payload['vm_owner_gaia']
    extra_vars['operating_system'] = api.payload['vm_os']
    extra_vars['target_dc'] = api.payload['vm_dc']
    extra_vars['vmname'] = api.payload['vm_name']
    payload['extra_vars'] = extra_vars

    logger.info('Creating an Azure VM from an image with the following parameters : ' + dumps(payload['extra_vars']))
    return common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='workflow_job_templates', item_name='Create Windows VM On Azure', payload=payload)
Esempio n. 2
0
  def post(self):                     #  Create POST endpoint
    """
    Create a VM on premise on VMWare
    """
    conf = config.readConfig()
    if conf == 1:
      logger.error('Missing configuration')
      return 'Internal Error : Missing configuration. Fix it at ' + request.url_root + 'config'
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Internal Error : Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['target_env'] = api.payload['target_env']
    extra_vars['site'] = api.payload['site']
    extra_vars['application_trigram'] = api.payload['application_trigram']
    extra_vars['owner'] = api.payload['owner']
    extra_vars['operating_system'] = api.payload['operating_system']
    extra_vars['cpu_count'] = int(api.payload['cpu_count'])
    extra_vars['ram_size'] = int(api.payload['ram_size'])
    extra_vars['disk_size'] = str(api.payload['disk_size'])
    extra_vars['vmname'] = api.payload['vmname']
    payload['extra_vars'] = extra_vars

    logger.info('Creating a VM on Premise with the following parameters : ' + dumps(payload['extra_vars']))
    return common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='workflow_job_templates', item_name='Create Windows VM On Premise', payload=payload)
Esempio n. 3
0
def config_awx():
  class ConfigForm(FlaskForm):
    config_token = config.readConfig()
    if config_token != 1:
      # TODO : secure access before displaying the current token in the interface
      # awx_token = StringField('AWX Token', validators=[validators.DataRequired()], default=config_token['awx_token'])
      awx_token = StringField('AWX Token', validators=[validators.DataRequired()])
    else:
      awx_token = StringField('AWX Token', validators=[validators.DataRequired()])
    save_button = SubmitField(label='Save')
  
  configform = ConfigForm()
  if configform.save_button.data:
    config.writeConfig(awx_token=configform.awx_token.data)
    awx_token = config.readConfig()['awx_token']
    return redirect('/')
  else:
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      logger.error('Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')')
      missing_config = True
      connection_ok = False
    else:
      connection_ok = True
    return render_template('config.html', form=configform, connection_ok=connection_ok, awx_url=awx_url)
Esempio n. 4
0
def checkAWXconnection(awx_url):
    """
  Check the connection to AWX 
  """
    conf = config.readConfig()
    if conf == 1:
        awx_token = ''
    else:
        awx_token = conf['awx_token']

    headers = {}
    headers['Authorization'] = "Bearer " + awx_token
    headers['Content-Type'] = "application/json"
    headers['Accept'] = "application/json"

    session = requests.Session()
    session.trust_env = False  #disable proxy
    session.verify = False  # set SSL CA path

    check_conn = session.get(awx_url + '/api/v2/workflow_job_templates/',
                             headers=headers,
                             verify=False)

    check_conn_output = {}
    check_conn_output['status_code'] = check_conn.status_code
    check_conn_output['reason'] = check_conn.reason
    return check_conn_output
Esempio n. 5
0
 class ConfigForm(FlaskForm):
   config_token = config.readConfig()
   if config_token != 1:
     # TODO : secure access before displaying the current token in the interface
     # awx_token = StringField('AWX Token', validators=[validators.DataRequired()], default=config_token['awx_token'])
     awx_token = StringField('AWX Token', validators=[validators.DataRequired()])
   else:
     awx_token = StringField('AWX Token', validators=[validators.DataRequired()])
   save_button = SubmitField(label='Save')
Esempio n. 6
0
  def get(self, id):
    """
    Get the standard output from a job
    """
    conf = config.readConfig()
    if conf == 1:
      logger.error('Missing configuration')
      return 'Internal Error : Missing configuration. Fix it at ' + request.url_root + 'config'
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Internal Error : Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'

    logger.info('Getting stdout for the following AWX job : ' + str(id))
    return Response(common.getAWXStdout(awx_url=awx_url, awx_token=conf['awx_token'], item_id=id), mimetype='text/plain') 
Esempio n. 7
0
 def get(self, id):
   """
   Get infos from a job or a workflow
   """
   conf = config.readConfig()
   if conf == 1:
     logger.error('Missing configuration')
     return 'Internal Error : Missing configuration. Fix it at ' + request.url_root + 'config'
   check_con = common.checkAWXconnection(awx_url=awx_url)
   check_status = check_con['status_code']
   if check_status != 200:
     return 'Internal Error : Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
   
   logger.info('Getting information for the following AWX job or workflow : ' + str(id))
   return common.getAWXInfos(awx_url=awx_url, awx_token=conf['awx_token'], item_id=id)
Esempio n. 8
0
  def delete(self):
    """
    Delete an Azure VM
    """
    conf = config.readConfig()
    if conf == 1:
      logger.error('Missing configuration')
      return 'Internal Error : Missing configuration. Fix it at ' + request.url_root + 'config'
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Internal Error : Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['resource_group'] = api.payload['resource_group']
    extra_vars['vm_name'] = api.payload['vm_name']
    payload['extra_vars'] = extra_vars

    logger.info('Deleting an Azure VM with the following parameters : ' + dumps(payload['extra_vars']))
    return common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='job_templates', item_name='Remove azure VM', payload=payload)
Esempio n. 9
0
  def delete(self):
    """
    Delete a VM on premise on VMWare
    """
    conf = config.readConfig()
    if conf == 1:
      logger.error('Missing configuration')
      return 'Internal Error : Missing configuration. Fix it at ' + request.url_root + 'config'
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Internal Error : Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['target_env'] = api.payload['target_env']
    extra_vars['vm_name'] = api.payload['vm_name']
    payload['extra_vars'] = extra_vars

    logger.info('Deleting a VM on Premise with the following parameters : ' + dumps(payload['extra_vars']))
    return common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='workflow_job_templates', item_name='Delete Windows VM On Premise', payload=payload)
Esempio n. 10
0
def home():
  mots = ["Bonjour", "à", "toi,", "anonyme citoyen."]
  createvmform = CreateVMForm()
  createazvmform = CreateAzureVMForm()
  deletevmform = DeleteVMForm()
  deleteazvmform = DeleteAzureVMForm()
  getinfosform = GetInfosForm()
  
  missing_config = True
  conf = config.readConfig()
  if conf != 1:
    missing_config = False

  if createvmform.create_button.data:
    if missing_config:
      logger.error('Missing configuration : awx_token')
      return 'Internal Error : Missing configuration : awx_token'
    
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['target_env'] = createvmform.target_env.data
    extra_vars['site'] = createvmform.target_site.data
    extra_vars['application_trigram'] = createvmform.app_trigram.data
    extra_vars['owner'] = createvmform.vm_owner_gaia.data
    extra_vars['operating_system'] = createvmform.vm_os.data
    extra_vars['cpu_count'] = int(createvmform.vm_cpu_count.data)
    extra_vars['ram_size'] = int(createvmform.vm_ram_size.data)
    extra_vars['disk_size'] = str(createvmform.vm_disk_size.data)
    extra_vars['vmname'] = createvmform.vm_name.data
    payload['extra_vars'] = extra_vars

    logger.info('Creating a VM on Premise with the following parameters : ' + dumps(payload['extra_vars']))
    result = common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='workflow_job_templates', item_name='Create Windows VM On Premise', payload=payload)
    # flash('{}'.format(dumps(result, indent=4, sort_keys=True)))
    # return redirect('/#flash')
    return redirect('/api/v1/infos/' + str(result['id']))
  
  elif deletevmform.delete_button.data:
    if missing_config:
      logger.error('Missing configuration : awx_token')
      return 'Internal Error : Missing configuration : awx_token'
    
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['target_env'] = deletevmform.target_env.data
    extra_vars['vm_name'] = deletevmform.vm_name.data
    payload['extra_vars'] = extra_vars

    logger.info('Deleting a VM on Premise with the following parameters : ' + dumps(payload['extra_vars']))
    result = common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='workflow_job_templates', item_name='Delete Windows VM On Premise', payload=payload)
    # flash('{}'.format(dumps(result, indent=4, sort_keys=True)))
    # return redirect('/#flash')
    return redirect('/api/v1/infos/' + str(result['id']))

  elif createazvmform.createaz_button.data:
    if missing_config:
      logger.error('Missing configuration : awx_token')
      return 'Internal Error : Missing configuration : awx_token'
    
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['resource_group'] = createazvmform.vm_resourcegroup.data
    extra_vars['image'] = createazvmform.vm_image.data
    extra_vars['size'] = createazvmform.vm_size.data
    extra_vars['owner'] = createazvmform.vm_owner_gaia.data
    extra_vars['operating_system'] = createazvmform.vm_os.data
    extra_vars['target_dc'] = createazvmform.vm_dc.data
    extra_vars['vmname'] = createazvmform.vm_name.data
    payload['extra_vars'] = extra_vars

    logger.info('Creating an Azure VM with the following parameters : ' + dumps(payload['extra_vars']))
    result = common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='workflow_job_templates', item_name='Create Windows VM On Azure', payload=payload)
    # flash('{}'.format(dumps(result, indent=4, sort_keys=True)))
    # return redirect('/#flash')
    return redirect('/api/v1/infos/' + str(result['id']))

  elif deleteazvmform.deleteaz_button.data:
    if missing_config:
      logger.error('Missing configuration : awx_token')
      return 'Internal Error : Missing configuration : awx_token'
    
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    extra_vars = {}
    payload = {}
    extra_vars['resource_group'] = deleteazvmform.resource_group.data
    extra_vars['vmname'] = deleteazvmform.vmname.data
    payload['extra_vars'] = extra_vars

    logger.info('Deleting an Azure VM with the following parameters : ' + dumps(payload['extra_vars']))
    result = common.launchAWXItem(awx_url=awx_url, awx_token=conf['awx_token'], item_type='job_templates', item_name='Remove azure VM', payload=payload)
    # flash('{}'.format(dumps(result, indent=4, sort_keys=True)))
    # return redirect('/#flash')
    return redirect('/api/v1/infos/' + str(result['id']))

  elif getinfosform.getinfos_button.data:
    if missing_config:
      logger.error('Missing configuration : awx_token')
      return 'Internal Error : Missing configuration : awx_token'
    
    check_con = common.checkAWXconnection(awx_url=awx_url)
    check_status = check_con['status_code']
    if check_status != 200:
      return 'Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')'
    
    logger.info('Getting information for the following AWX workflow : ' + str(getinfosform.item_id.data))
    result = common.getAWXInfos(awx_url=awx_url, awx_token=conf['awx_token'], item_id=getinfosform.item_id.data)
    # flash('{}'.format(dumps(result, indent=4, sort_keys=True)))
    return redirect('/api/v1/infos/' + str(getinfosform.item_id.data))

  else:
    if missing_config:
      logger.error('Missing configuration : awx_token')
      flash('ERROR : Missing configuration : awx_token')
      return redirect('/config')
    else:
      check_con = common.checkAWXconnection(awx_url=awx_url)
      check_status = check_con['status_code']
      if check_status != 200:
        logger.error('Impossible to connect to AWX [' + awx_url + '] ('+ str(check_con['status_code']) + ' : ' + check_con['reason'] + ')')
        missing_config = True
        connection_ok = False
      else:
        connection_ok = True

      return render_template('index.html', 
        titre=env.upper() + " GIAC Portal",
        connection_ok=connection_ok,
        check_con=check_con,
        mots=mots,
        form=createvmform,
        createazvmform=createazvmform,
        deletevmform=deletevmform,
        deleteazvmform=deleteazvmform,
        getinfosform=getinfosform)
Esempio n. 11
0
fh = logging.FileHandler('awx_portal.log')
fh.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(name)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)

logger.info('================================================================================')
logger.info('=                               AWX PORTAL                                     =')
logger.info('================================================================================')

missing_config = True
conf = config.readConfig()
if conf != 1:
  awx_token = conf['awx_token']
  missing_config = False
else: # ONLY FOR LOCAL TESTING PURPOSE
    # awx_token = 'rDxTKN2vgQYMNvHjatvEFQdcxp8DHT' # local awx admin test token
    awx_token=''

if "AWX_URL" in os.environ:
  awx_url = os.environ['AWX_URL']
else:
  awx_url = 'http://10.20.102.6'
matching = re.match('^http(s|)://giacportal(|-)((|dev|hom)(|[0-9]))\.gem\.myengie\.com(|/)$',awx_url)
if matching:
  if matching.group(3) == '':
    env = 'PROD'