<commit_before>#!/usr/bin/env python

'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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 optparse
import sys
import os
import logging
import tempfile
import urllib2
import socket
import json
import base64
import time

AMBARI_HOSTNAME = None
AMBARI_PORT = 8080
CLUSTER_NAME = None
PROTOCOL = "http"
USERNAME = "admin"
PASSWORD = "<PASSWORD>"
DEFAULT_TIMEOUT = 10 # seconds

# Supported Actions
RELOCATE_ACTION = 'relocate'
ALLOWED_ACTUAL_STATES_FOR_RELOCATE = [ 'INIT', 'UNKNOWN', 'MAINTENANCE', 'UNINSTALLED' ]
ALLOWED_HOST_STATUS_FOR_RELOCATE = [ 'HEALTHY' ]
STATUS_WAIT_TIMEOUT = 120 # seconds
STATUS_CHECK_INTERVAL = 10 # seconds

# API calls
GET_CLUSTERS_URI = "/api/v1/clusters/"
GET_HOST_COMPONENTS_URI = "/api/v1/clusters/{0}/services/{1}/components/{2}" +\
                          "?fields=host_components"
GET_HOST_COMPONENT_DESIRED_STATE_URI = "/api/v1/clusters/{0}/hosts/{1}" +\
                                       "/host_components/{2}" +\
                                       "?fields=HostRoles/desired_state"
GET_HOST_COMPONENT_STATE_URI = "/api/v1/clusters/{0}/hosts/{1}" +\
                               "/host_components/{2}" +\
                               "?fields=HostRoles/state"
GET_HOST_STATE_URL = "/api/v1/clusters/{0}/hosts/{1}?fields=Hosts/host_state"
HOST_COMPONENT_URI = "/api/v1/clusters/{0}/hosts/{1}/host_components/{2}"
ADD_HOST_COMPONENT_URI = "/api/v1/clusters/{0}/hosts?Hosts/host_name={1}"

logger = logging.getLogger()



class PreemptiveBasicAuthHandler(urllib2.BaseHandler):

  def __init__(self):
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, getUrl(''), USERNAME, PASSWORD)
    self.passwd = password_mgr
    self.add_password = self.passwd.add_password

  def http_request(self, req):
    uri = req.get_full_url()
    user = USERNAME
    pw = PASSWORD
    raw = "%s:%s" % (user, pw)
    auth = 'Basic %s' % base64.b64encode(raw).strip()
    req.add_unredirected_header('Authorization', auth)
    return req


class AmbariResource:

  def __init__(self, serviceName, componentName):
    self.serviveName = serviceName
    self.componentName = componentName
    self.isInitialized = False

  def initializeResource(self):
    global CLUSTER_NAME
    if CLUSTER_NAME is None:
      CLUSTER_NAME = self.findClusterName()

    if self.serviveName is None:
      raise Exception('Service name undefined')

    if self.componentName is None:
      raise Exception('Component name undefined')

    handler = PreemptiveBasicAuthHandler()
    opener = urllib2.build_opener(handler)
    # Install opener for all requests
    urllib2.install_opener(opener)
    self.urlOpener = opener

    self.old_hostname = self.getHostname()

    self.isInitialized = True


  def relocate(self, new_hostname):
    if not self.isInitialized:
      raise Exception('Resource not initialized')

    # If old and new hostname are the same exit harmlessly
    if old_hostname == new_hostname:
      logger.error('New hostname is same as existing host name, %s' % old_hostname)
      sys.exit(2)
    pass

    try:
      self.verifyHostComponentStatus(self.old_hostname, new_hostname, self.componentName)
    except Exception, e:
      logger.error("Exception caught on verify relocate request.")
      logger.error(e.message)
      sys.exit(1)

    # Delete current host component
    self.deleteHostComponent(self.old_hostname, self.componentName)

    # Add component on the new host
    self.addHostComponent(new_hostname, self.componentName)

    # Install host component
    self.updateHostComponentStatus(new_hostname, self.componentName,
                                   "Installing", "INSTALLED")

    # Wait on install
    self.waitOnHostComponentUpdate(new_hostname, self.componentName,
                                   "INSTALLED")<commit_msg>AMBARI-3983. Create a helper script to relocate host components. Error fix. (swagle)<commit_after>#!/usr/bin/env python

'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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 optparse
import sys
import os
import logging
import tempfile
import urllib2
import socket
import json
import base64
import time

AMBARI_HOSTNAME = None
AMBARI_PORT = 8080
CLUSTER_NAME = None
PROTOCOL = "http"
USERNAME = "admin"
PASSWORD = "<PASSWORD>"
DEFAULT_TIMEOUT = 10 # seconds

# Supported Actions
RELOCATE_ACTION = 'relocate'
ALLOWED_ACTUAL_STATES_FOR_RELOCATE = [ 'INIT', 'UNKNOWN', 'MAINTENANCE', 'UNINSTALLED' ]
ALLOWED_HOST_STATUS_FOR_RELOCATE = [ 'HEALTHY' ]
STATUS_WAIT_TIMEOUT = 120 # seconds
STATUS_CHECK_INTERVAL = 10 # seconds

# API calls
GET_CLUSTERS_URI = "/api/v1/clusters/"
GET_HOST_COMPONENTS_URI = "/api/v1/clusters/{0}/services/{1}/components/{2}" +\
                          "?fields=host_components"
GET_HOST_COMPONENT_DESIRED_STATE_URI = "/api/v1/clusters/{0}/hosts/{1}" +\
                                       "/host_components/{2}" +\
                                       "?fields=HostRoles/desired_state"
GET_HOST_COMPONENT_STATE_URI = "/api/v1/clusters/{0}/hosts/{1}" +\
                               "/host_components/{2}" +\
                               "?fields=HostRoles/state"
GET_HOST_STATE_URL = "/api/v1/clusters/{0}/hosts/{1}?fields=Hosts/host_state"
HOST_COMPONENT_URI = "/api/v1/clusters/{0}/hosts/{1}/host_components/{2}"
ADD_HOST_COMPONENT_URI = "/api/v1/clusters/{0}/hosts?Hosts/host_name={1}"

logger = logging.getLogger()



class PreemptiveBasicAuthHandler(urllib2.BaseHandler):

  def __init__(self):
    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, getUrl(''), USERNAME, PASSWORD)
    self.passwd = password_mgr
    self.add_password = self.passwd.add_password

  def http_request(self, req):
    uri = req.get_full_url()
    user = USERNAME
    pw = PASSWORD
    raw = "%s:%s" % (user, pw)
    auth = 'Basic %s' % base64.b64encode(raw).strip()
    req.add_unredirected_header('Authorization', auth)
    return req


class AmbariResource:

  def __init__(self, serviceName, componentName):
    self.serviveName = serviceName
    self.componentName = componentName
    self.isInitialized = False

  def initializeResource(self):
    global CLUSTER_NAME
    if CLUSTER_NAME is None:
      CLUSTER_NAME = self.findClusterName()

    if self.serviveName is None:
      raise Exception('Service name undefined')

    if self.componentName is None:
      raise Exception('Component name undefined')

    handler = PreemptiveBasicAuthHandler()
    opener = urllib2.build_opener(handler)
    # Install opener for all requests
    urllib2.install_opener(opener)
    self.urlOpener = opener

    self.old_hostname = self.getHostname()

    self.isInitialized = True


  def relocate(self, new_hostname):
    if not self.isInitialized:
      raise Exception('Resource not initialized')

    # If old and new hostname are the same exit harmlessly
    if self.old_hostname == new_hostname:
      logger.error('New hostname is same as existing host name, %s' % self.old_hostname)
      sys.exit(2)
    pass

    try:
      self.verifyHostComponentStatus(self.old_hostname, new_hostname, self.componentName)
    except Exception, e:
      logger.error("Exception caught on verify relocate request.")
      logger.error(e.message)
      sys.exit(1)

    # Delete current host component
    self.deleteHostComponent(self.old_hostname, self.componentName)

    # Add component on the new host
    self.addHostComponent(new_hostname, self.componentName)

    # Install host component
    self.updateHostComponentStatus(new_hostname, self.componentName,
                                   "Installing", "INSTALLED")

    # Wait on install
    self.waitOnHostComponentUpdate(new_hostname, self.componentName,
                                   "INSTALLED")
{'fraction_non_alphanumeric': 0.09271955599085864, 'fraction_numerical': 0.011535531613886168, 'mean_word_length': 3.40978886756238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3631924', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2748, 'n_words': 720}