Пример #1
0
def perform_server_action(handler,
                          action,
                          server_name,
                          provider_name='',
                          action_type='',
                          args=[],
                          kwargs={}):
    """Calls required action on the server through the driver. """

    #Either we expect {action_type: ssh, action: some_action} or action: ssh/some_action  (or provider | app for the action type)
    if not action_type:
        if '/' not in action:
            raise Exception(
                'action_type argument is empty, so action is expected to be in format <action_type/<action> (example: ssh/show_processes), but instead action was : '
                + str(action))
        action_type, action = action.split('/')

    server = yield handler.datastore_handler.get_object(
        'server', server_name=server_name)

    result = None
    if action_type == 'app':
        result = yield handle_app_action(server=server,
                                         action=action,
                                         args=args,
                                         kwargs=kwargs)
    else:
        provider_name = provider_name or 'va_standalone_servers'

        provider, driver = yield providers.get_provider_and_driver(
            handler, provider_name)
        result = yield driver.server_action(provider, server_name, action)

    if type(result) != dict:
        result = {'success': True, 'message': '', 'data': result}

    result['message'] = 'Action %s completed successfuly. ' % action
    print('ACtino result is : ', result)
    raise tornado.gen.Return(result)
Пример #2
0
def validate_app_fields(handler):
    """
        description: Allows creating servers. Creates a server by going through a validation scheme similar to that for adding providers. Requires that you send a step index as an argument, whereas the specifics for the validation are based on what driver you are using. If no provider_name is sent, creates a server on the va_standalone_servers provider, which is mostly invisible, and its servers are treated as standalone. 
        arguments: TODO
        hide: True
    """
    provider, driver = yield providers.get_provider_and_driver(
        handler, handler.data.get('provider_name', 'va_standalone_servers'))

    kwargs = handler.data
    step = handler.data.pop('step')

    fields = yield driver.validate_app_fields(step, **kwargs)

    # If the state has extra fields, then there are 3 steps, otherwise just 2.
    if step == 3:
        handler.data.update(fields)
        try:
            result = yield handler.executor.submit(launch_app, handler)
        except:
            import traceback
            traceback.print_exc()
    raise tornado.gen.Return(fields)
Пример #3
0
def launch_app(handler):
    """
    Launches a server based on the data supplied. This is dependent on the specific data required by the providers. 
    The default for starting servers is using salt-cloud -p <profile> <minion_name>, where the drivers are responsible for creating the configuration files. 
    Some drivers work independent of salt though, such as libvirt. 
    If the extra_fields key is supplied, it will create a specific pillar for the server. 
    If provider_name is not sent, it will create a va_standalone server, with the invisible driver va_standalone_servers. 
    If role is sent, then the function will try to get data for the minion using salt-call mine.get  <minion_name> inventory. If it does this successfully, it will add the server to the datastore, and add a ping service for the server. 
    """

    data = handler.data
    try:
        provider, driver = yield providers.get_provider_and_driver(
            handler, data.get('provider_name', 'va_standalone_servers'))

        if data.get('extra_fields', {}):
            write_pillar(data)
    except:
        import traceback
        traceback.print_exc()

    result = yield driver.create_server(provider, data)

    if provider.get('provider_name') and provider.get(
            'provider_name', '') != 'va_standalone_servers':
        yield add_server_to_datastore(handler.datastore_handler,
                                      server_name=data['server_name'],
                                      hostname=data['server_name'],
                                      manage_type='provider',
                                      driver_name=provider['driver_name'],
                                      ip_address=data.get('ip'))

    if data.get('role', False):

        yield panels.new_panel(handler.datastore_handler, data['server_name'],
                               data['role'])
        minion_info = None

        retries = 0
        while not minion_info and retries < int(
                handler.data.get('mine_retries', '10')):
            minion_info = yield get_app_info(handler.data['server_name'])
            minion_info.update({'type': 'app'})
            retries += 1
            if not minion_info:
                yield tornado.gen.sleep(10)

        yield services.add_services_presets(minion_info, ['ping'])

        if not minion_info:
            raise tornado.gen.Return({
                "success": False,
                "message":
                "No minion_info, something probably went wrong with trying to start the instance. ",
                "data": None
            })
        else:
            yield manage_server_type(handler.datastore_handler,
                                     server_name=data['server_name'],
                                     new_type='app',
                                     role=data['role'])

    raise tornado.gen.Return(result)
Пример #4
0
def launch_app(handler, dash_user):
    """
        description: "Launches a server based on the data supplied. This is dependent on the specific data required by the providers. 
    The default for starting servers is using salt-cloud -p <profile> <minion_name>, where the drivers are responsible for creating the configuration files. 
    Some drivers work independent of salt though, such as libvirt. 
    If the extra_fields key is supplied, it will create a specific pillar for the server. 
    If provider_name is not sent, it will create a va_standalone server, with the invisible driver va_standalone_servers. 
    If role is sent, then the function will try to get data for the minion using salt-call mine.get  <minion_name> inventory. If it does this successfully, it will add the server to the datastore, and add a ping service for the server. 
Updates the subscriptions status. "
        output: None
        arguments: 
          - name: provider_name
            description: The provider which will be used to create a server. If none, the it will be added as a standalone server. 
            type: string
            required: False
            default: va_standalone_servers
          - name: server_name 
            description: The name of the server which will be used to display the server on the va-master.
            type: string
            required: True
          - name: ip
            description: The IP address of the server. If not set, then it is up to the provider to return an ip address for the server. 
            type: string
            required: False 
          - name: role
            description: The role of the server. This is typically used for salt, but in general, servers should have an app associated with them. 
            required: True
        visible: True
        event: True
        data_prefix: "kwargs"
    """

    data = handler.data
    print('Creating with data : ', data)
    try:
        provider, driver = yield providers.get_provider_and_driver(
            handler, data.get('provider_name', 'va_standalone_servers'))

        if data.get('extra_fields', {}):
            write_pillar(data)
    except:
        import traceback
        traceback.print_exc()

    result = yield driver.create_server(provider, data)

    if provider.get('provider_name') and provider.get(
            'provider_name', '') != 'va_standalone_servers':
        yield add_server_to_datastore(handler.datastore_handler,
                                      server_name=data['server_name'],
                                      hostname=data['server_name'],
                                      manage_type='provider',
                                      driver_name=provider['driver_name'],
                                      ip_address=data.get('ip'))

    if data.get('role', False):

        yield panels.new_panel(handler.datastore_handler, data['server_name'],
                               data['role'])
        minion_info = None

        retries = 0
        while not minion_info and retries < int(
                handler.data.get('mine_retries', '10')):
            minion_info = yield get_app_info(handler.data['server_name'])
            minion_info.update({'type': 'app'})

            retries += 1
            if not minion_info:
                yield tornado.gen.sleep(10)

#        yield services.add_services_presets(minion_info, ['ping'])

#        if not minion_info:
#            raise tornado.gen.Return({"success" : False, "message" : "No minion_info, something probably went wrong with trying to start the instance. ", "data" : None})
#        else:
#            yield manage_server_type(handler.datastore_handler, server_name = data['server_name'], new_type = 'app', role = data['role'])

    raise tornado.gen.Return(result)