예제 #1
0
    def __init__(self, api_home_url='', relations={}):
        self.__log = logging.getLogger(__name__)
        self.__log.debug("__init__(api_home_url=%s, relations=%s)" % (api_home_url, str(relations)))


        if "users" not in relations:
            relations["users"] = default_link_rel_users
            self.__log.warning("Using default link relation for users: %s" % default_link_rel_users)
        if "projects" not in relations:
            relations["projects"] = default_link_rel_projects
            self.__log.warning("Using default link relation for projects: %s" % default_link_rel_projects)

        self._rc = RestClient(relations)

        @self._rc.handle_content_type('application/json-home', 0.75)
        def handle_json_home0(client, resp):
                # parse JSON home document or other JSON directly
                json_home_data = resp.json()
                self.__log.debug("have JSON home data: %s" % (str(json_home_data)))
                if 'resources' in json_home_data:
                    for rel, d in json_home_data['resources'].iteritems():
                        if 'href' in d:
                            client.remember_link(resp.url, rel, d['href']) 
                    return True
                else:
                    return False

        # Fetch and handle the API Home URL
        self._rc.get(api_home_url)
예제 #2
0
 def get(self, command='', rest_args={}):
     for i in range(5):
         result, status = RestClient.get(self, command, rest_args)
         if status == 200:
             return result, status
         elif status == 429:
             print("Too fast")
             sleep(1 * i)
         else:
             print(f"status code: {status}")
             return result, status
     print("Much Too fast")
     return result, status
예제 #3
0
    def __init__(self, customer_id=None, auth_token=None, **kwargs):
        try:
            from dotenv import Dotenv
            dotenv = Dotenv(os.path.join(os.getcwd(), '.env'))
            os.environ.update(dotenv)
        except:
            sys.stderr.write('Skipping load of .env...\n')
            sys.stderr.write(traceback.format_exc())

        if not customer_id:
            customer_id = os.environ['RED_CANARY_CUSTOMER_ID']

        if not auth_token:
            auth_token = os.environ['RED_CANARY_AUTH_TOKEN']

        request_options = dict(params={'auth_token': auth_token})
        request_options.update(kwargs.pop('request_options', None) or {})

        RestClient.__init__(self,
                            'https://%s.my.redcanary.co/openapi/v2' %
                            customer_id,
                            request_options=request_options,
                            **kwargs)
예제 #4
0
    def __init__(self, oms, sso_user, sso_pwd):
        self.rest_client = RestClient(oms, sso_user, sso_pwd)
        self.logger = logging.getLogger(__name__)

        self._made_remote_dirs = []
예제 #5
0
class OmsController(object):
    # Helper methods

    def __init__(self, oms, sso_user, sso_pwd):
        self.rest_client = RestClient(oms, sso_user, sso_pwd)
        self.logger = logging.getLogger(__name__)

        self._made_remote_dirs = []

    def login(self):
        self.rest_client.login()

    def hello(self):
        return self.rest_client.do_get('hello')

    def server_version(self):
        return self.rest_client.do_get('version')

    def server_status(self):
        return self.rest_client.do_get('status')

    def list_task(self):
        return self.rest_client.do_get('tasks')

    def list_networks(self):
        response = self.rest_client.do_get("networks")
        return response

    def list_datastores(self):
        response = self.rest_client.do_get("datastores")
        return response

    def list_deployments(self):
        clusters = self.rest_client.do_get('clusters')
        return clusters

    def list_deployment(self, name):
        api_url_template = "cluster/{}"
        url = api_url_template.format(name)
        cluster = self.rest_client.do_get(url)
        return cluster

    def delete_deployment(self, deployment_name):
        resp = self.rest_client.do_delete('cluster', deployment_name)
        return self._validate_task('Delete cluster', resp)

    def create_deployment_by_spec(self, deployment_json, timeout=5400):
        resp = self._create_deployment(deployment_json)
        return self._validate_task('Create cluster', resp, timeout=timeout)

    def _create_deployment(self, spec):
        post_body = json.dumps(spec)
        LOG.debug("Create OpenStack Cluster with spec: %s" % post_body)
        resp = self.rest_client.do_post('clusters', post_body)
        return resp

    def add_compute_vc(self, spec):
        post_body = json.dumps(spec)
        resp = self.rest_client.do_post('vc', post_body)
        return resp

    def get_vc_ip(self):
        resp = self.rest_client.do_get('vcip')
        return resp

    def cluster_config(self, spec):
        resp = self.rest_client.do_put("cluster/VIO/config", spec)
        return resp

    def get_task(self, taskid):
        task = self.rest_client.do_get('task/{}'.format(taskid))
        return json.loads(task.text)

    def del_nova_datastore(self, spec):
        resp = self.rest_client.do_put("clusters/VIO/novadatastore", spec)
        return resp

    def del_glance_datastore(self, spec):
        resp = self.rest_client.do_put("clusters/VIO/glancedatastore", spec)
        return resp

    def edit_cluster(self, cluster, spec, timeout=5400):
        api_url_template = "clusters/%s/edit"
        url = api_url_template % cluster
        put_body = json.dumps(spec)
        resp = self.rest_client.do_put(url, put_body)
        return self._validate_task('Edit cluster', resp, timeout=timeout)

    def retrieve_cluster_profile(self, cluster):
        api_url_template = "clusters/%s/profile"
        url = api_url_template % cluster
        resp = self.rest_client.do_get(url)
        return resp

    def create_deployment_plan(self, spec):
        resp = self.rest_client.do_put("clusters/plan", spec)
        return resp

    def add_nova_node_plan(self, cluster, ng):
        api_url_template = "cluster/{}/nodegroup/{}/plan"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_put(url, str(2))  # totalInstanceNum
        return resp

    def add_nova_node(self, cluster, ng, spec):
        LOG.debug('Add nova node spec: %s', spec)
        api_url_template = "cluster/{}/nodegroup/{}/scaleout"
        url = api_url_template.format(cluster, ng)
        LOG.debug('Add nova node url: %s', url)
        resp = self.rest_client.do_put(url, spec)
        return self._validate_task('Add nova node', resp)

    def add_node_group(self, cluster, spec):
        api_url_template = "clusters/{}/nodegroups"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def del_nova_node(self, cluster, ng, nd):
        api_url_template = "cluster/{}/nodegroup/{}/node"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_delete(url, nd)
        return resp

    def increase_ips(self, nw, spec):
        api_url_template = "network/{}?action=add"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def remove_ips(self, nw, spec):
        api_url_template = "network/{}?action=remove"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def update_dns(self, nw, spec):
        api_url_template = "network/{}/async"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def get_sysconf(self):
        resp = self.rest_client.do_get("conf")
        return json.loads(resp.text)

    def set_syslogserver(self, logserver, port, protocol, tag):
        url = \
            'conf?syslogserver={}&syslogserverport={}' \
            '&syslogserverprotocol={}&syslogservertag={}'
        resp = self.rest_client.do_put(
            url.format(
                logserver, port, protocol, tag), "")
        return resp

    def get_network_by_name(self, networkname):
        resp = self.rest_client.do_get("network/{}".format(networkname))
        return json.loads(resp.text)

    def create_support_bundle(self, spec):
        resp = self.rest_client.do_post("bundles", spec)
        return resp

    def get_support_bundle(self, spec, dest):
        resp = self.rest_client.do_post("bundles", spec)
        fileName = resp.text.split('/')[-1][0:-1]
        with open('%s/%s' % (dest, fileName), 'wb') as handle:
            resp = self.rest_client.do_get("bundle/{}".format(fileName))
            for block in resp.iter_content(1024):
                if not block:
                    break
                handle.write(block)
        return fileName

    def validate(self, type, spec):
        api_url_template = "validators/{}"
        url = api_url_template.format(type)
        put_body = json.dumps(spec)
        resp = self.rest_client.do_post(url, put_body)
        return resp

    def manage_openstack_services(self, cluster, service, action):
        api_url_template = "clusters/{}/services/{}?action={}"
        url = api_url_template.format(cluster, service, action)
        resp = self.rest_client.do_put(url, None)
        return resp

    def start_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=start"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def stop_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=stop"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def restart_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=restart"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def generate_csr(self, clusterName, spec):
        api_url_template = "clusters/{}/csr"
        url = api_url_template.format(clusterName)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def add_horizon(self, cluster, spec):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def del_horizon(self, cluster, title):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_delete(url, title)
        return resp

    def list_horizon(self, cluster):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        regions = self.rest_client.do_get(url)
        return regions

    def get_plugin_status(self):
        url = "plugin/status"
        resp = self.rest_client.do_get(url)
        return resp

    def check_oms_vc_connection(self):
        url = "checkOmsVCConnection"
        resp = self.rest_client.do_get(url)
        return resp

    def get_oms_vc_status(self):
        url = "connection/status"
        resp = self.rest_client.do_get(url)
        return resp

    def register_plugin(self):
        url = "plugin/register?addException=true"
        resp = self.rest_client.do_post(url, "")
        return resp

    def change_datacollector_setting(self, enable="false"):
        api_url_template = "datacollector?enabled={}"
        url = api_url_template.format(enable)
        resp = self.rest_client.do_post(url, "")
        return resp

    def get_datacollector_setting(self):
        url = "datacollector"
        resp = self.rest_client.do_get(url)
        return resp

    def get_audit_file(self):
        url = "phauditfile"
        resp = self.rest_client.do_get(url)
        return resp

    def start_cluster(self, cluster):
        api_url_template = "cluster/%s?action=start"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def stop_cluster(self, cluster):
        api_url_template = "cluster/%s?action=stop"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def retry_cluster(self, cluster, timeout=5400):
        api_url_template = "cluster/%s?action=retry"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Retry cluster', resp, timeout=timeout)

    def upgrade_provision(self, cluster, spec):
        post_body = json.dumps(spec)
        LOG.debug('Green cluster spec: %s', spec)
        api_url_template = '/clusters/%s/upgrade/provision'
        url = api_url_template % cluster
        resp = self.rest_client.do_post(url, post_body)
        return self._validate_task('Create green cluster', resp)

    def upgrade_retry(self, cluster, spec):
        put_body = json.dumps(spec)
        api_url_template = '/clusters/%s/upgrade/retry'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, put_body)
        return resp

    def upgrade_migrate_data(self, cluster):
        api_url_template = '/clusters/%s/upgrade/configure'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Migrate blue cluster data', resp)

    def upgrade_switch_to_green(self, cluster):
        api_url_template = '/clusters/%s/upgrade/switch'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Switch to green cluster', resp)

    def switch_keystone_backend(self, cluster, spec):
        put_body = json.dumps(spec)
        api_url_template = '/clusters/%s/keystonebackend'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, put_body)
        return resp

    def change_deployment_type(self, newtype):
        api_url_template = '/deploymenttype?deployment_type=%s'
        url = api_url_template % newtype
        resp = self.rest_client.do_post(url, "")
        return resp

    def unconfig_ceilometer(self, cluster):
        api_url_template = '/clusters/%s/unconfigceilometer'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Unconfig ceilometer', resp)

    @staticmethod
    def _get_task_id(url):
        LOG.debug('Grep task id from url: %s', url)
        pattern = re.compile(r'/task/(\d+)')
        result = pattern.search(url)
        if result:
            task_id = result.group(1)
            LOG.debug('Task id: %s' % task_id)
            return task_id

    def wait_for_task_completed(self, task_id, interval=60, timeout=3600):
        begin_poll = datetime.datetime.now()
        status_list = ['COMPLETED', 'STOPPING', 'STOPPED', 'FAILED']
        while (datetime.datetime.now() - begin_poll).seconds < timeout:
            task = self.get_task(task_id)
            if task['status'] in status_list:
                LOG.debug('Task %s status: %s', task_id, task['status'])
                return task['status'], task['errorMessage']
            time.sleep(interval)
        raise TimeoutError('Waited %s seconds for task %s' % (timeout,
                                                              task_id))

    def _validate_task(self, task_name, resp, interval=60, timeout=3600):
        start = time.time()
        LOG.debug('Response header: %s', resp.headers)
        LOG.debug('Response body: %s', resp.text)
        if resp.status_code != 202:
            raise OMSError('Task %s failed: %s' % (task_name, resp.text))
        url = resp.headers['Location']
        LOG.debug('Retrieve redirect url %s of task %s', url, task_name)
        task_id = OmsController._get_task_id(url)
        if not task_id:
            raise OMSError('Task id of %s not found.' % task_name)
        status, msg = self.wait_for_task_completed(task_id, interval, timeout)
        if status != 'COMPLETED':
            raise OMSError('Task %s %s: %s' % (task_name, status, msg))
        end = time.time()
        LOG.debug('Task %s took %s seconds', task_name, (end - start))
        return task_id
예제 #6
0
    def __init__(self, oms, sso_user, sso_pwd):
        self.rest_client = RestClient(oms, sso_user, sso_pwd)
        self.logger = logging.getLogger(__name__)

        self._made_remote_dirs = []
예제 #7
0
class OmsController(object):
    # Helper methods

    def __init__(self, oms, sso_user, sso_pwd):
        self.rest_client = RestClient(oms, sso_user, sso_pwd)
        self.logger = logging.getLogger(__name__)

        self._made_remote_dirs = []

    def login(self):
        self.rest_client.login()

    def hello(self):
        return self.rest_client.do_get('hello')

    def server_version(self):
        return self.rest_client.do_get('version')

    def server_status(self):
        return self.rest_client.do_get('status')

    def list_task(self):
        return self.rest_client.do_get('tasks')

    def list_networks(self):
        response = self.rest_client.do_get("networks")
        return response

    def list_datastores(self):
        response = self.rest_client.do_get("datastores")
        return response

    def list_deployments(self):
        clusters = self.rest_client.do_get('clusters')
        return clusters

    def list_deployment(self, name):
        api_url_template = "cluster/{}"
        url = api_url_template.format(name)
        cluster = self.rest_client.do_get(url)
        return cluster

    def delete_deployment(self, deployment_name):
        resp = self.rest_client.do_delete('cluster', deployment_name)
        return self._validate_task('Delete cluster', resp)

    def create_deployment_by_spec(self, deployment_json, timeout=5400):
        resp = self._create_deployment(deployment_json)
        return self._validate_task('Create cluster', resp, timeout=timeout)

    def _create_deployment(self, spec):
        post_body = json.dumps(spec)
        LOG.debug("Create OpenStack Cluster with spec: %s" % post_body)
        resp = self.rest_client.do_post('clusters', post_body)
        return resp

    def add_compute_vc(self, spec):
        post_body = json.dumps(spec)
        resp = self.rest_client.do_post('vc', post_body)
        return resp

    def get_vc_ip(self):
        resp = self.rest_client.do_get('vcip')
        return resp

    def cluster_config(self, spec):
        resp = self.rest_client.do_put("cluster/VIO/config", spec)
        return resp

    def get_task(self, taskid):
        task = self.rest_client.do_get('task/{}'.format(taskid))
        return json.loads(task.text)

    def del_nova_datastore(self, spec):
        resp = self.rest_client.do_put("clusters/VIO/novadatastore", spec)
        return resp

    def del_glance_datastore(self, spec):
        resp = self.rest_client.do_put("clusters/VIO/glancedatastore", spec)
        return resp

    def edit_cluster(self, cluster, spec, timeout=5400):
        api_url_template = "clusters/%s/edit"
        url = api_url_template % cluster
        put_body = json.dumps(spec)
        resp = self.rest_client.do_put(url, put_body)
        return self._validate_task('Edit cluster', resp, timeout=timeout)

    def retrieve_cluster_profile(self, cluster):
        api_url_template = "clusters/%s/profile"
        url = api_url_template % cluster
        resp = self.rest_client.do_get(url)
        return resp

    def create_deployment_plan(self, spec):
        resp = self.rest_client.do_put("clusters/plan", spec)
        return resp

    def add_nova_node_plan(self, cluster, ng):
        api_url_template = "cluster/{}/nodegroup/{}/plan"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_put(url, str(2))  # totalInstanceNum
        return resp

    def add_nova_node(self, cluster, ng, spec):
        LOG.debug('Add nova node spec: %s', spec)
        api_url_template = "cluster/{}/nodegroup/{}/scaleout"
        url = api_url_template.format(cluster, ng)
        LOG.debug('Add nova node url: %s', url)
        resp = self.rest_client.do_put(url, spec)
        return self._validate_task('Add nova node', resp)

    def add_node_group(self, cluster, spec):
        api_url_template = "clusters/{}/nodegroups"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def del_nova_node(self, cluster, ng, nd):
        api_url_template = "cluster/{}/nodegroup/{}/node"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_delete(url, nd)
        return resp

    def increase_ips(self, nw, spec):
        api_url_template = "network/{}?action=add"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def remove_ips(self, nw, spec):
        api_url_template = "network/{}?action=remove"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def update_dns(self, nw, spec):
        api_url_template = "network/{}/async"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def get_sysconf(self):
        resp = self.rest_client.do_get("conf")
        return json.loads(resp.text)

    def set_syslogserver(self, logserver, port, protocol, tag):
        url = \
            'conf?syslogserver={}&syslogserverport={}' \
            '&syslogserverprotocol={}&syslogservertag={}'
        resp = self.rest_client.do_put(
            url.format(logserver, port, protocol, tag), "")
        return resp

    def get_network_by_name(self, networkname):
        resp = self.rest_client.do_get("network/{}".format(networkname))
        return json.loads(resp.text)

    def create_support_bundle(self, spec):
        resp = self.rest_client.do_post("bundles", spec)
        return resp

    def get_support_bundle(self, spec, dest):
        resp = self.rest_client.do_post("bundles", spec)
        fileName = resp.text.split('/')[-1][0:-1]
        with open('%s/%s' % (dest, fileName), 'wb') as handle:
            resp = self.rest_client.do_get("bundle/{}".format(fileName))
            for block in resp.iter_content(1024):
                if not block:
                    break
                handle.write(block)
        return fileName

    def validate(self, type, spec):
        api_url_template = "validators/{}"
        url = api_url_template.format(type)
        put_body = json.dumps(spec)
        resp = self.rest_client.do_post(url, put_body)
        return resp

    def manage_openstack_services(self, cluster, service, action):
        api_url_template = "clusters/{}/services/{}?action={}"
        url = api_url_template.format(cluster, service, action)
        resp = self.rest_client.do_put(url, None)
        return resp

    def start_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=start"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def stop_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=stop"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def restart_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=restart"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def generate_csr(self, clusterName, spec):
        api_url_template = "clusters/{}/csr"
        url = api_url_template.format(clusterName)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def add_horizon(self, cluster, spec):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def del_horizon(self, cluster, title):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_delete(url, title)
        return resp

    def list_horizon(self, cluster):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        regions = self.rest_client.do_get(url)
        return regions

    def get_plugin_status(self):
        url = "plugin/status"
        resp = self.rest_client.do_get(url)
        return resp

    def check_oms_vc_connection(self):
        url = "checkOmsVCConnection"
        resp = self.rest_client.do_get(url)
        return resp

    def get_oms_vc_status(self):
        url = "connection/status"
        resp = self.rest_client.do_get(url)
        return resp

    def register_plugin(self):
        url = "plugin/register?addException=true"
        resp = self.rest_client.do_post(url, "")
        return resp

    def change_datacollector_setting(self, enable="false"):
        api_url_template = "datacollector?enabled={}"
        url = api_url_template.format(enable)
        resp = self.rest_client.do_post(url, "")
        return resp

    def get_datacollector_setting(self):
        url = "datacollector"
        resp = self.rest_client.do_get(url)
        return resp

    def get_audit_file(self):
        url = "phauditfile"
        resp = self.rest_client.do_get(url)
        return resp

    def start_cluster(self, cluster):
        api_url_template = "cluster/%s?action=start"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def stop_cluster(self, cluster):
        api_url_template = "cluster/%s?action=stop"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def retry_cluster(self, cluster, timeout=5400):
        api_url_template = "cluster/%s?action=retry"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Retry cluster', resp, timeout=timeout)

    def upgrade_provision(self, cluster, spec):
        post_body = json.dumps(spec)
        LOG.debug('Green cluster spec: %s', spec)
        api_url_template = '/clusters/%s/upgrade/provision'
        url = api_url_template % cluster
        resp = self.rest_client.do_post(url, post_body)
        return self._validate_task('Create green cluster', resp)

    def upgrade_retry(self, cluster, spec):
        put_body = json.dumps(spec)
        api_url_template = '/clusters/%s/upgrade/retry'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, put_body)
        return resp

    def upgrade_migrate_data(self, cluster):
        api_url_template = '/clusters/%s/upgrade/configure'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Migrate blue cluster data', resp)

    def upgrade_switch_to_green(self, cluster):
        api_url_template = '/clusters/%s/upgrade/switch'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Switch to green cluster', resp)

    def switch_keystone_backend(self, cluster, spec):
        put_body = json.dumps(spec)
        api_url_template = '/clusters/%s/keystonebackend'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, put_body)
        return resp

    def change_deployment_type(self, newtype):
        api_url_template = '/deploymenttype?deployment_type=%s'
        url = api_url_template % newtype
        resp = self.rest_client.do_post(url, "")
        return resp

    def unconfig_ceilometer(self, cluster):
        api_url_template = '/clusters/%s/unconfigceilometer'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return self._validate_task('Unconfig ceilometer', resp)

    @staticmethod
    def _get_task_id(url):
        LOG.debug('Grep task id from url: %s', url)
        pattern = re.compile(r'/task/(\d+)')
        result = pattern.search(url)
        if result:
            task_id = result.group(1)
            LOG.debug('Task id: %s' % task_id)
            return task_id

    def wait_for_task_completed(self, task_id, interval=60, timeout=3600):
        begin_poll = datetime.datetime.now()
        status_list = ['COMPLETED', 'STOPPING', 'STOPPED', 'FAILED']
        while (datetime.datetime.now() - begin_poll).seconds < timeout:
            task = self.get_task(task_id)
            if task['status'] in status_list:
                LOG.debug('Task %s status: %s', task_id, task['status'])
                return task['status'], task['errorMessage']
            time.sleep(interval)
        raise TimeoutError('Waited %s seconds for task %s' %
                           (timeout, task_id))

    def _validate_task(self, task_name, resp, interval=60, timeout=3600):
        start = time.time()
        LOG.debug('Response header: %s', resp.headers)
        LOG.debug('Response body: %s', resp.text)
        if resp.status_code != 202:
            raise OMSError('Task %s failed: %s' % (task_name, resp.text))
        url = resp.headers['Location']
        LOG.debug('Retrieve redirect url %s of task %s', url, task_name)
        task_id = OmsController._get_task_id(url)
        if not task_id:
            raise OMSError('Task id of %s not found.' % task_name)
        status, msg = self.wait_for_task_completed(task_id, interval, timeout)
        if status != 'COMPLETED':
            raise OMSError('Task %s %s: %s' % (task_name, status, msg))
        end = time.time()
        LOG.debug('Task %s took %s seconds', task_name, (end - start))
        return task_id
예제 #8
0
 def __init__(self, api_key=None):
     RestClient.__init__(self, 'http://www.omdbapi.com', api_key,
                         'OMDB_API_KEY')
     self.api_key_argname = 'apikey'
예제 #9
0
class OmsController(object):
    # Helper methods

    def __init__(self, oms, sso_user, sso_pwd):
        self.rest_client = RestClient(oms, sso_user, sso_pwd)
        self.logger = logging.getLogger(__name__)

        self._made_remote_dirs = []

    def login(self):
        self.rest_client.login()

    def hello(self):
        return self.rest_client.do_get('hello')

    def server_version(self):
        return self.rest_client.do_get('version')

    def server_status(self):
        return self.rest_client.do_get('status')

    def list_task(self):
        return self.rest_client.do_get('tasks')

    def list_networks(self):
        response = self.rest_client.do_get("networks")
        return response

    def list_datastores(self):
        response = self.rest_client.do_get("datastores")
        return response

    def list_deployments(self):
        clusters = self.rest_client.do_get('clusters')
        return clusters

    def list_deployment(self, name):
        api_url_template = "cluster/{}"
        url = api_url_template.format(name)
        cluster = self.rest_client.do_get(url)
        return cluster

    def delete_deployment(self, deployment_name):
        return self.rest_client.do_delete('cluster', deployment_name)

    def create_deployment_by_spec(self, deployment_json):
        resp = self._create_deployment(deployment_json)
        return resp

    def _create_deployment(self, spec):
        post_body = json.dumps(spec)
        resp = self.rest_client.do_post('clusters', post_body)
        return resp

    def add_compute_vc(self, spec):
        post_body = json.dumps(spec)
        resp = self.rest_client.do_post('vc', post_body)
        return resp

    def get_vc_ip(self):
        resp = self.rest_client.do_get('vcip')
        return resp

    def cluster_config(self, spec):
        resp = self.rest_client.do_put("cluster/VIO/config", spec)
        return resp

    def get_task(self, taskid):
        task = self.rest_client.do_get('task/{}'.format(taskid))
        return json.loads(task.text)

    def del_nova_datastore(self, spec):
        resp = self.rest_client.do_put("clusters/VIO/novadatastore", spec)
        return resp

    def del_glance_datastore(self, spec):
        resp = self.rest_client.do_put("clusters/VIO/glancedatastore", spec)
        return resp

    def retry_cluster(self, cluster, spec):
        api_url_template = "clusters/%s?action=retry"
        url = api_url_template % cluster
        put_body = json.dumps(spec)
        resp = self.rest_client.do_put(url, put_body)
        return resp

    def retrieve_cluster_profile(self, cluster):
        api_url_template = "clusters/%s/profile"
        url = api_url_template % cluster
        resp = self.rest_client.do_get(url)
        return resp

    def create_deployment_plan(self, spec):
        resp = self.rest_client.do_put("clusters/plan", spec)
        return resp

    def add_nova_node_plan(self, cluster, ng):
        api_url_template = "cluster/{}/nodegroup/{}/plan"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_put(url, str(2))  # totalInstanceNum
        return resp

    def add_nova_node(self, cluster, ng, spec):
        api_url_template = "cluster/{}/nodegroup/{}/scaleout"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def add_node_group(self, cluster, spec):
        api_url_template = "clusters/{}/nodegroups"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def del_nova_node(self, cluster, ng, nd):
        api_url_template = "cluster/{}/nodegroup/{}/node"
        url = api_url_template.format(cluster, ng)
        resp = self.rest_client.do_delete(url, nd)
        return resp

    def increase_ips(self, nw, spec):
        api_url_template = "network/{}"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def update_dns(self, nw, spec):
        api_url_template = "network/{}/async"
        url = api_url_template.format(nw)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def get_sysconf(self):
        resp = self.rest_client.do_get("conf")
        return json.loads(resp.text)

    def set_syslogserver(self, logserver, port, protocol, tag):
        url = \
            'conf?syslogserver={}&syslogserverport={}' \
            '&syslogserverprotocol={}&syslogservertag={}'
        resp = self.rest_client.do_put(
            url.format(logserver, port, protocol, tag), "")
        return resp

    def get_network_by_name(self, networkname):
        resp = self.rest_client.do_get("network/{}".format(networkname))
        return json.loads(resp.text)

    def create_support_bundle(self, spec):
        resp = self.rest_client.do_post("bundles", spec)
        return resp

    def get_support_bundle(self, spec, dest):
        resp = self.rest_client.do_post("bundles", spec)
        fileName = resp.text.split('/')[-1][0:-1]
        with open('%s/%s' % (dest, fileName), 'wb') as handle:
            resp = self.rest_client.do_get("bundle/{}".format(fileName))
            for block in resp.iter_content(1024):
                if not block:
                    break
                handle.write(block)
        return fileName

    def validate(self, type, spec):
        api_url_template = "validators/{}"
        url = api_url_template.format(type)
        put_body = json.dumps(spec)
        resp = self.rest_client.do_post(url, put_body)
        return resp

    def manage_openstack_services(self, cluster, service, action):
        api_url_template = "clusters/{}/services/{}?action={}"
        url = api_url_template.format(cluster, service, action)
        resp = self.rest_client.do_put(url, None)
        return resp

    def start_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=start"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def stop_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=stop"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def restart_services(self, cluster, spec):
        api_url_template = "clusters/{}/services?action=restart"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_put(url, spec)
        return resp

    def generate_csr(self, clusterName, spec):
        api_url_template = "clusters/{}/csr"
        url = api_url_template.format(clusterName)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def add_horizon(self, cluster, spec):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_post(url, spec)
        return resp

    def del_horizon(self, cluster, title):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        resp = self.rest_client.do_delete(url, title)
        return resp

    def list_horizon(self, cluster):
        api_url_template = "clusters/{}/horizon"
        url = api_url_template.format(cluster)
        regions = self.rest_client.do_get(url)
        return regions

    def get_plugin_status(self):
        url = "plugin/status"
        resp = self.rest_client.do_get(url)
        return resp

    def check_oms_vc_connection(self):
        url = "checkOmsVCConnection"
        resp = self.rest_client.do_get(url)
        return resp

    def get_oms_vc_status(self):
        url = "connection/status"
        resp = self.rest_client.do_get(url)
        return resp

    def register_plugin(self):
        url = "plugin/register?addException=true"
        resp = self.rest_client.do_post(url, "")
        return resp

    def change_datacollector_setting(self, enable="false"):
        api_url_template = "datacollector?enabled={}"
        url = api_url_template.format(enable)
        resp = self.rest_client.do_post(url, "")
        return resp

    def get_datacollector_setting(self):
        url = "datacollector"
        resp = self.rest_client.do_get(url)
        return resp

    def get_audit_file(self):
        url = "phauditfile"
        resp = self.rest_client.do_get(url)
        return resp

    def start_cluster(self, cluster):
        api_url_template = "cluster/%s?action=start"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def stop_cluster(self, cluster):
        api_url_template = "cluster/%s?action=stop"
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def upgrade_provision(self, cluster, spec):
        post_body = json.dumps(spec)
        api_url_template = '/clusters/%s/upgrade/provision'
        url = api_url_template % cluster
        resp = self.rest_client.do_post(url, post_body)
        return resp

    def upgrade_retry(self, cluster, spec):
        put_body = json.dumps(spec)
        api_url_template = '/clusters/%s/upgrade/retry'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, put_body)
        return resp

    def upgrade_migrate_data(self, cluster):
        api_url_template = '/clusters/%s/upgrade/configure'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def upgrade_switch_to_green(self, cluster):
        api_url_template = '/clusters/%s/upgrade/switch'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, "")
        return resp

    def switch_keystone_backend(self, cluster, spec):
        put_body = json.dumps(spec)
        api_url_template = '/clusters/%s/keystonebackend'
        url = api_url_template % cluster
        resp = self.rest_client.do_put(url, put_body)
        return resp
예제 #10
0

import logging

from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
import requests
from restclient import RestClient
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import json
import re
logger = logging.getLogger(__name__)

rest_client = RestClient("10.154.9.156", "*****@*****.**", "VMware1!")

class Omsnode(APIView):
    def post(self, request, cluster, ng):
        api_url_template = "cluster/{}/nodegroup/{}/scaleout"
        url = api_url_template.format(cluster, ng)
        try:
            resp = rest_client.do_put(url, request.body)    
        except Exception as e:
            return Response(data={'error': str(e)},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return Response(data={'oms': resp},
                        status=status.HTTP_202_ACCEPTED)


    def delete(self, request, cluster, ng, nd):
예제 #11
0
def main():

    #Do requests SSL stuff
    #requests.packages.urllib3.disable_warnings()

    #Get configuration  
    parser = SafeConfigParser()
    parser.read('/etc/dinofactsbot/config.ini')
    config = parser._sections['defaults']

    #Configure logging
    logger = logging.getLogger('DINOFACTSBOT')
    logger.setLevel(logging.INFO)
    handler = TimedRotatingFileHandler(config['log_file'],when='midnight')
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    #Initialize rest client
    client = RestClient(config['api_host'])

    #Initialize reddit client
    logger.info('Initializing reddit API client')
    r = praw.Reddit(user_agent = config['user_agent'])

    #login
    logger.info('Attempting to log in to reddit with credentials from config.ini')
    r.login(config['bot_user'], config['bot_pass'])

    #Get reddit user
    logger.info('Retrieving subreddit: ' + config['subreddit'])
    subreddit = r.get_subreddit(config['subreddit'])

    #Main loop of program
    logger.info('Beginning monitor..')
    logger.info('Retrieving comments with limit of ' + str(config['limit']))
    
    while True:
        
        try:
        
            comments = subreddit.get_comments(limit=int(config['limit']))

            for comment in comments:

                if client.isCommentProcessed(comment.id) is 0:
                
                    logger.info('Processing ' + comment.id + ' by user ' + comment.author.name)
                    record = client.insertRecord(comment)
                    logger.info('New log record created: ' + str(record['id']))


                    if (config['trigger'] in comment.body):
                    
                        logger.info('Found trigger in ' + comment.id)
                        fact = client.getRandom()

                        logger.info('Retrieved fact:{fact} '.format(fact=fact['id']))
                        comment.reply(fact['fact'])

                        logger.info('Updating record field')                                    
                        client.updateRecord(record['id'], fact['id'])

                    else:         

                        logger.info('No trigger found. Ignorinng comment')

            sleep(int(config['poll_time']))

        except Exception as e:
                    
            logger.error(e)
예제 #12
0
 def query_string(self, command='', args={}):
     string = RestClient.query_string(self, command, {
         **args,
         **self.language_arg()
     })
     return string
예제 #13
0
 def __init__(self, api_key=None):
     RestClient.__init__(self, 'https://api.themoviedb.org/3', api_key,
                         'TMDB_API_KEY')
     self.api_key_argname = 'api_key'
예제 #14
0
import sys


def getInput(string, l, ex=False):
    while True:
        val = int(input(string + "\n"))
        if (ex == True and val == 5):
            sys.exit()
        if val not in l:
            print('Invalid Input')
        else:
            break
    return val


a = RestClient("http://127.0.0.1:5000")
tables = {1: 'employees', 2: 'student', 3: 'cricket'}
employeeDict = {
    "LastName": "",
    "FirstName": "",
    "Title": "",
    "ReportsTo": "",
    "BirthDate": "",
    "HireDate": "",
    "Address": "",
    "City": "",
    "State": "",
    "Country": "",
    "PostalCode": "",
    "Phone": "",
    "Fax": "",