def _uninstall_sanity_app():
    if not _is_sanity_dep_exist():
        return

    data = {
        'deployment_id': DEPLOYMENT_ID,
        'workflow_id': 'uninstall'
    }
    headers = utils.create_maintenance_headers()
    headers.update({'content-type': 'application/json'})

    utils.http_request(
        '{0}/executions'.format(_get_url_prefix()),
        method='POST',
        data=json.dumps(data),
        headers=headers)

    # Waiting for installation to complete
    utils.repetitive(
        utils.wait_for_workflow,
        timeout=5*60,
        interval=30,
        deployment_id=DEPLOYMENT_ID,
        workflow_id='uninstall',
        url_prefix=_get_url_prefix(),
        timeout_msg='Timed out while waiting for '
                    'deployment {0} to uninstall.'.format(DEPLOYMENT_ID))
示例#2
0
def _deploy_app():
    if _is_sanity_dep_exist(should_fail=True):
        return

    dep_inputs = {
        'server_ip': manager_ip,
        'agent_user': manager_user,
        'agent_private_key_path': manager_remote_key_path
    }
    data = {'blueprint_id': BLUEPRINT_ID, 'inputs': dep_inputs}
    headers = utils.create_maintenance_headers()
    headers.update({'content-type': 'application/json'})

    utils.http_request('{0}/deployments/{1}'.format(_get_url_prefix(),
                                                    DEPLOYMENT_ID),
                       data=json.dumps(data),
                       headers=headers)

    # Waiting for create deployment env to end
    utils.repetitive(utils.wait_for_workflow,
                     deployment_id=DEPLOYMENT_ID,
                     workflow_id='create_deployment_environment',
                     url_prefix=_get_url_prefix(),
                     timeout=60,
                     timeout_msg='Timed out while waiting for '
                     'deployment {0} to be created'.format(DEPLOYMENT_ID))
def _deploy_app():
    if _is_sanity_dep_exist(should_fail=True):
        return

    dep_inputs = {'server_ip': manager_ip,
                  'agent_user': manager_user,
                  'agent_private_key_path': manager_remote_key_path}
    data = {
        'blueprint_id': BLUEPRINT_ID,
        'inputs': dep_inputs
    }
    headers = utils.create_maintenance_headers()
    headers.update({'content-type': 'application/json'})

    utils.http_request(
            '{0}/deployments/{1}'.format(_get_url_prefix(), DEPLOYMENT_ID),
            data=json.dumps(data),
            headers=headers)

    # Waiting for create deployment env to end
    utils.repetitive(
        utils.wait_for_workflow,
        deployment_id=DEPLOYMENT_ID,
        workflow_id='create_deployment_environment',
        url_prefix=_get_url_prefix(),
        timeout_msg='Timed out while waiting for '
                    'deployment {0} to be created'.format(DEPLOYMENT_ID))
def _install_sanity_app():
    data = {
        'deployment_id': DEPLOYMENT_ID,
        'workflow_id': 'install'
    }
    headers = utils.create_maintenance_headers()
    headers.update({'content-type': 'application/json'})

    resp = utils.http_request(
            '{0}/executions'.format(_get_url_prefix()),
            method='POST',
            data=json.dumps(data),
            headers=headers)

    # Waiting for installation to complete
    utils.repetitive(
        utils.wait_for_workflow,
        timeout=5*60,
        interval=30,
        deployment_id=DEPLOYMENT_ID,
        workflow_id='install',
        url_prefix=_get_url_prefix(),
        timeout_msg='Timed out while waiting for '
                    'deployment {0} to install'.format(DEPLOYMENT_ID))

    resp_content = resp.readlines()
    json_resp = json.loads(resp_content[0])
    return json_resp['id']
示例#5
0
def _is_sanity_blueprint_exist(should_fail=False):
    headers = utils.create_maintenance_headers()
    res = utils.http_request('{0}/blueprints/{1}'.format(
        _get_url_prefix(), BLUEPRINT_ID),
                             method='GET',
                             headers=headers,
                             should_fail=should_fail)
    if not res:
        return False
    return res.code == 200
def _is_sanity_blueprint_exist(should_fail=False):
    headers = utils.create_maintenance_headers()
    res = utils.http_request(
            '{0}/blueprints/{1}'.format(_get_url_prefix(), BLUEPRINT_ID),
            method='GET',
            headers=headers,
            should_fail=should_fail)
    if not res:
        return False
    return res.code == 200
def _delete_sanity_blueprint():
    if not _is_sanity_blueprint_exist():
        return
    headers = utils.create_maintenance_headers()
    resp = utils.http_request(
        '{0}/blueprints/{1}'.format(_get_url_prefix(), BLUEPRINT_ID),
        method='DELETE',
        headers=headers)

    if resp.code != 200:
        ctx.abort_operation('Failed deleting '
                            'deployment {0}: {1}'.format(BLUEPRINT_ID,
                                                         resp.reason))
示例#8
0
def _delete_sanity_blueprint():
    if not _is_sanity_blueprint_exist():
        return
    headers = utils.create_maintenance_headers()
    resp = utils.http_request('{0}/blueprints/{1}'.format(
        _get_url_prefix(), BLUEPRINT_ID),
                              method='DELETE',
                              headers=headers)

    if resp.code != 200:
        ctx.abort_operation('Failed deleting '
                            'deployment {0}: {1}'.format(
                                BLUEPRINT_ID, resp.reason))
def _upload_app_blueprint(app_tar):
    if _is_sanity_blueprint_exist(should_fail=True):
        return

    with open(app_tar, 'rb') as f:
        app_data = f.read()
    length = os.path.getsize(app_tar)

    headers = utils.create_maintenance_headers()
    headers['Content-Length'] = length
    headers['Content-Type'] = 'application/octet-stream'
    params = urllib.urlencode(
        dict(application_file_name='singlehost-blueprint.yaml'))

    endpoint = '{0}/blueprints/{1}'.format(_get_url_prefix(), BLUEPRINT_ID)
    url = endpoint + '?' + params
    utils.http_request(url, data=app_data, headers=headers)
示例#10
0
文件: start.py 项目: ptanX/cloudify
def _assert_logs_and_events(execution_id):
    headers = utils.create_maintenance_headers()
    params = urllib.urlencode((
        ('execution_id', execution_id),
        ('type', 'cloudify_event'),
        ('type', 'cloudify_log'),
        ('_sort', '@timestamp'),
        ('_size', 100),
        ('_offset', 0),
    ))

    endpoint = '{0}/events'.format(_get_url_prefix())
    url = endpoint + '?' + params
    ctx.logger.debug('Sending request to url: {0}, with the following '
                     'headers: {1}'.format(url, headers))
    resp = utils.http_request(url, method='GET', headers=headers, timeout=30)
    if not resp:
        ctx.abort_operation("Can't connect to Cloudify's rest service")
    if resp.code != 200:
        ctx.abort_operation('Failed to retrieve logs/events')

    resp_content = resp.readlines()
    json_resp = json.loads(resp_content[0])

    if 'items' not in json_resp or not json_resp['items']:
        ctx.logger.debug('No items received. The response is: '
                         '{0}'.format(json_resp))
        ctx.abort_operation('No logs/events received')

    db_name = 'cloudify_db'
    for table_name in ['logs', 'events']:
        proc = utils.run([
            'sudo',
            '-u',
            'postgres',
            'psql',
            db_name,
            '-t',
            '-c',
            'SELECT COUNT(*) FROM {0}'.format(table_name),
        ])
        count = int(proc.aggr_stdout)
        if count <= 0:
            ctx.abort_operation(
                'Failed to retrieve {0} from PostgreSQL'.format(table_name))
示例#11
0
def _assert_logs_and_events(execution_id):
    headers = utils.create_maintenance_headers()
    params = urllib.urlencode(
        dict(execution_id=execution_id, type='cloudify_log'))

    endpoint = '{0}/events'.format(_get_url_prefix())
    url = endpoint + '?' + params
    resp = utils.http_request(url, method='GET', headers=headers, timeout=30)
    if not resp:
        ctx.abort_operation("Can't connect to elasticsearch")
    if resp.code != 200:
        ctx.abort_operation('Failed to retrieve logs/events')

    resp_content = resp.readlines()
    json_resp = json.loads(resp_content[0])

    if 'items' not in json_resp or not json_resp['items']:
        ctx.abort_operation('No logs/events received')
def _assert_logs_and_events(execution_id):
    headers = utils.create_maintenance_headers()
    params = urllib.urlencode(
            dict(execution_id=execution_id,
                 type='cloudify_log'))

    endpoint = '{0}/events'.format(_get_url_prefix())
    url = endpoint + '?' + params
    resp = utils.http_request(url, method='GET', headers=headers, timeout=30)
    if not resp:
        ctx.abort_operation("Can't connect to elasticsearch")
    if resp.code != 200:
        ctx.abort_operation('Failed to retrieve logs/events')

    resp_content = resp.readlines()
    json_resp = json.loads(resp_content[0])

    if 'items' not in json_resp or not json_resp['items']:
        ctx.abort_operation('No logs/events received')
示例#13
0
def verify_restservice(url):
    """To verify that the REST service is working, GET the blueprints list.

    There's nothing special about the blueprints endpoint, it's simply one
    that also requires the storage backend to be up, so if it works, there's
    a good chance everything is configured correctly.
    """
    if utils.is_upgrade or utils.is_rollback:
        # if we're doing an upgrade, we're in maintenance mode - this request
        # is safe to perform in maintenance mode, so let's bypass the check
        headers = utils.create_maintenance_headers()
    else:
        headers = utils.get_auth_headers(True)
        headers['tenant'] = 'default_tenant'

    utils.verify_service_http(REST_SERVICE_NAME, url, headers=headers)

    blueprints_url = urlparse.urljoin(url, 'api/v2.1/blueprints')
    req = urllib2.Request(blueprints_url, headers=headers)

    try:
        response = urllib2.urlopen(req)
    # keep an erroneous HTTP response to examine its status code, but still
    # abort on fatal errors like being unable to connect at all
    except urllib2.HTTPError as e:
        response = e
    except urllib2.URLError as e:
        ctx.abort_operation(
            'REST service returned an invalid response: {0}'.format(e))
    if response.code == 401:
        ctx.abort_operation('Could not connect to the REST service: '
                            '401 unauthorized. Possible access control '
                            'misconfiguration')
    if response.code != 200:
        ctx.abort_operation(
            'REST service returned an unexpected response: {0}'.format(
                response.code))

    try:
        json.load(response)
    except ValueError as e:
        ctx.abort_operation(
            'REST service returned malformed JSON: {0}'.format(e))
def _upload_app_blueprint(app_tar):
    if _is_sanity_blueprint_exist(should_fail=True):
        return

    with open(app_tar, 'rb') as f:
        app_data = f.read()
    length = os.path.getsize(app_tar)

    headers = utils.create_maintenance_headers()
    headers['Content-Length'] = length
    headers['Content-Type'] = 'application/octet-stream'
    params = urllib.urlencode(
            dict(application_file_name='singlehost-blueprint.yaml'))

    endpoint = '{0}/blueprints/{1}'.format(_get_url_prefix(), BLUEPRINT_ID)
    url = endpoint + '?' + params
    utils.http_request(url,
                       data=app_data,
                       headers=headers)
def verify_restservice(url):
    """To verify that the REST service is working, GET the blueprints list.

    There's nothing special about the blueprints endpoint, it's simply one
    that also requires the storage backend to be up, so if it works, there's
    a good chance everything is configured correctly.
    """
    if utils.is_upgrade or utils.is_rollback:
        # if we're doing an upgrade, we're in maintenance mode - this request
        # is safe to perform in maintenance mode, so let's bypass the check
        headers = utils.create_maintenance_headers()
    else:
        headers = utils.get_auth_headers(True)
        headers['tenant'] = 'default_tenant'

    utils.verify_service_http(SERVICE_NAME, url, headers=headers)

    blueprints_url = urlparse.urljoin(url, 'api/v2.1/blueprints')
    req = urllib2.Request(blueprints_url, headers=headers)

    try:
        response = urllib2.urlopen(req)
    # keep an erroneous HTTP response to examine its status code, but still
    # abort on fatal errors like being unable to connect at all
    except urllib2.HTTPError as e:
        response = e
    except urllib2.URLError as e:
        ctx.abort_operation('REST service returned an invalid response: {0}'
                            .format(e))
    if response.code == 401:
        ctx.abort_operation('Could not connect to the REST service: '
                            '401 unauthorized. Possible access control '
                            'misconfiguration')
    if response.code != 200:
        ctx.abort_operation('REST service returned an unexpected response: {0}'
                            .format(response.code))

    try:
        json.load(response)
    except ValueError as e:
        ctx.abort_operation('REST service returned malformed JSON: {0}'
                            .format(e))
def _assert_logs_and_events(execution_id):
    headers = utils.create_maintenance_headers()
    params = urllib.urlencode((
        ('execution_id', execution_id),
        ('type', 'cloudify_event'),
        ('type', 'cloudify_log'),
        ('_sort', '@timestamp'),
        ('_size', 100),
        ('_offset', 0),
    ))

    endpoint = '{0}/events'.format(_get_url_prefix())
    url = endpoint + '?' + params
    ctx.logger.debug('Sending request to url: {0}, with the following '
                     'headers: {1}'.format(url, headers))
    resp = utils.http_request(url, method='GET', headers=headers, timeout=30)
    if not resp:
        ctx.abort_operation("Can't connect to Cloudify's rest service")
    if resp.code != 200:
        ctx.abort_operation('Failed to retrieve logs/events')

    resp_content = resp.readlines()
    json_resp = json.loads(resp_content[0])

    if 'items' not in json_resp or not json_resp['items']:
        ctx.logger.debug('No items received. The response is: '
                         '{0}'.format(json_resp))
        ctx.abort_operation('No logs/events received')

    db_name = 'cloudify_db'
    for table_name in ['logs', 'events']:
        proc = utils.run([
            'sudo', '-u', 'postgres',
            'psql', db_name, '-t', '-c',
            'SELECT COUNT(*) FROM {0}'.format(table_name),
        ])
        count = int(proc.aggr_stdout)
        if count <= 0:
            ctx.abort_operation(
                'Failed to retrieve {0} from PostgreSQL'.format(table_name))
示例#17
0
def _uninstall_sanity_app():
    if not _is_sanity_dep_exist():
        return

    data = {'deployment_id': DEPLOYMENT_ID, 'workflow_id': 'uninstall'}
    headers = utils.create_maintenance_headers()
    headers.update({'content-type': 'application/json'})

    utils.http_request('{0}/executions'.format(_get_url_prefix()),
                       method='POST',
                       data=json.dumps(data),
                       headers=headers)

    # Waiting for installation to complete
    utils.repetitive(utils.wait_for_workflow,
                     timeout=5 * 60,
                     interval=30,
                     deployment_id=DEPLOYMENT_ID,
                     workflow_id='uninstall',
                     url_prefix=_get_url_prefix(),
                     timeout_msg='Timed out while waiting for '
                     'deployment {0} to uninstall.'.format(DEPLOYMENT_ID))
示例#18
0
def _install_sanity_app():
    data = {'deployment_id': DEPLOYMENT_ID, 'workflow_id': 'install'}
    headers = utils.create_maintenance_headers()
    headers.update({'content-type': 'application/json'})

    resp = utils.http_request('{0}/executions'.format(_get_url_prefix()),
                              method='POST',
                              data=json.dumps(data),
                              headers=headers)

    # Waiting for installation to complete
    utils.repetitive(utils.wait_for_workflow,
                     timeout=5 * 60,
                     interval=30,
                     deployment_id=DEPLOYMENT_ID,
                     workflow_id='install',
                     url_prefix=_get_url_prefix(),
                     timeout_msg='Timed out while waiting for '
                     'deployment {0} to install'.format(DEPLOYMENT_ID))

    resp_content = resp.readlines()
    json_resp = json.loads(resp_content[0])
    return json_resp['id']
示例#19
0
文件: start.py 项目: ptanX/cloudify
import utils  # NOQA

runtime_props = ctx.instance.runtime_properties
SERVICE_NAME = runtime_props['service_name']


def check_response(response):
    """Check if the response looks like a correct REST service response.

    We can get a 200, or a 401 in case auth is enabled. We don't expect a
    502, though, as this would mean nginx isn't correctly proxying to
    the REST service.
    """
    return response.code in {200, 401}


utils.start_service(SERVICE_NAME, append_prefix=False)
utils.systemd.verify_alive(SERVICE_NAME, append_prefix=False)

nginx_url = '{0}://127.0.0.1/api/v2.1/version'.format(
    runtime_props['external_rest_protocol'])

headers = {}
if utils.is_upgrade or utils.is_rollback:
    headers = utils.create_maintenance_headers()

utils.verify_service_http(SERVICE_NAME,
                          nginx_url,
                          check_response,
                          headers=headers)
runtime_props = ctx.instance.runtime_properties
SERVICE_NAME = runtime_props['service_name']


def check_response(response):
    """Check if the response looks like a correct REST service response.

    We can get a 200, or a 401 in case auth is enabled. We don't expect a
    502, though, as this would mean nginx isn't correctly proxying to
    the REST service.
    """
    return response.code in {200, 401}


utils.start_service(SERVICE_NAME, append_prefix=False)
utils.systemd.verify_alive(SERVICE_NAME, append_prefix=False)

nginx_url = '{0}://127.0.0.1/api/v2.1/version'.format(
    runtime_props['external_rest_protocol'])

headers = {}
if utils.is_upgrade or utils.is_rollback:
    headers = utils.create_maintenance_headers()

utils.verify_service_http(
    SERVICE_NAME,
    nginx_url,
    check_response,
    headers=headers
)