def test_start_head_node(self):
        self.options = flexmock(infrastructure='public cloud',
                                group='group',
                                machine='vm image',
                                instance_type='instance type',
                                keyname='keyname',
                                table='cassandra',
                                verbose=False,
                                test=False,
                                use_spot_instances=False,
                                zone='zone',
                                static_ip=None,
                                replication=None,
                                appengine=None,
                                autoscale=None,
                                user_commands=[],
                                flower_password='',
                                max_memory='X',
                                ips=ONE_NODE_CLOUD)

        self.node_layout = NodeLayout(self.options)

        flexmock(LocalState).\
          should_receive("generate_secret_key").\
          with_args(self.options.keyname).\
          and_return('some secret key')

        flexmock(LocalState).\
          should_receive("get_key_path_from_name").\
          with_args(self.options.keyname).\
          and_return('some key path')

        flexmock(NodeLayout).should_receive('head_node').\
          and_return(Node('some IP', 'cloud'))

        fake_agent = FakeAgent()
        flexmock(factory.InfrastructureAgentFactory).\
          should_receive('create_agent').\
          with_args('public cloud').\
          and_return(fake_agent)

        self.additional_params = {}
        deployment_params = {}

        flexmock(LocalState).\
          should_receive('generate_deployment_params').\
          with_args(self.options, self.node_layout, self.additional_params).\
          and_return(deployment_params)

        flexmock(AppScaleLogger).should_receive('log').and_return()
        flexmock(AppScaleLogger).should_receive('remote_log_tools_state').\
          and_return()

        flexmock(time).should_receive('sleep').and_return()

        flexmock(RemoteHelper).\
          should_receive('copy_deployment_credentials').\
          with_args('some IP', self.options).\
          and_return()

        flexmock(RemoteHelper).\
          should_receive('run_user_commands').\
          with_args('some IP', self.options.user_commands,
                    self.options.keyname, self.options.verbose).\
          and_return()

        flexmock(RemoteHelper).\
          should_receive('start_remote_appcontroller').\
          with_args('some IP', self.options.keyname, self.options.verbose).\
          and_return()

        layout = {}
        flexmock(NodeLayout).should_receive('to_list').and_return(layout)

        flexmock(AppControllerClient).\
          should_receive('set_parameters').\
          with_args(layout, deployment_params).\
          and_return()

        RemoteHelper.start_head_node(self.options, 'an ID', self.node_layout)
  def run_instances(cls, options):
    """Starts a new AppScale deployment with the parameters given.

    Args:
      options: A Namespace that has fields for each parameter that can be
        passed in via the command-line interface.
    Raises:
      AppControllerException: If the AppController on the head node crashes.
        When this occurs, the message in the exception contains the reason why
        the AppController crashed.
      BadConfigurationException: If the user passes in options that are not
        sufficient to start an AppScale deployment (e.g., running on EC2 but
        not specifying the AMI to use), or if the user provides us
        contradictory options (e.g., running on EC2 but not specifying EC2
        credentials).
    """
    LocalState.make_appscale_directory()
    LocalState.ensure_appscale_isnt_running(options.keyname, options.force)
    node_layout = NodeLayout(options)

    if options.infrastructure:
      if (not options.test and not options.force and
          not (options.disks or node_layout.are_disks_used())):
        LocalState.ensure_user_wants_to_run_without_disks()

    reduced_version = '.'.join(x for x in APPSCALE_VERSION.split('.')[:2])
    AppScaleLogger.log("Starting AppScale " + reduced_version)

    my_id = str(uuid.uuid4())
    AppScaleLogger.remote_log_tools_state(options, my_id, "started",
      APPSCALE_VERSION)

    head_node = node_layout.head_node()
    # Start VMs in cloud via cloud agent.
    if options.infrastructure:
      node_layout = RemoteHelper.start_all_nodes(options, node_layout)

      # Enables root logins and SSH access on the head node.
      RemoteHelper.enable_root_ssh(options, head_node.public_ip)
    AppScaleLogger.verbose("Node Layout: {}".format(node_layout.to_list()))

    # Ensure all nodes are compatible.
    RemoteHelper.ensure_machine_is_compatible(
      head_node.public_ip, options.keyname)

    # Use rsync to move custom code into the deployment.
    if options.rsync_source:
      AppScaleLogger.log("Copying over local copy of AppScale from {0}".
        format(options.rsync_source))
      RemoteHelper.rsync_files(head_node.public_ip, options.keyname,
                               options.rsync_source)

    # Start services on head node.
    RemoteHelper.start_head_node(options, my_id, node_layout)

    # Write deployment metadata to disk (facilitates SSH operations, etc.)
    db_master = node_layout.db_master().private_ip
    head_node = node_layout.head_node().public_ip
    LocalState.update_local_metadata(options, db_master, head_node)

    # Copy the locations.json to the head node
    RemoteHelper.copy_local_metadata(node_layout.head_node().public_ip,
                                     options.keyname)

    # Wait for services on head node to start.
    secret_key = LocalState.get_secret_key(options.keyname)
    acc = AppControllerClient(head_node, secret_key)
    try:
      while not acc.is_initialized():
        AppScaleLogger.log('Waiting for head node to initialize...')
        # This can take some time in particular the first time around, since
        # we will have to initialize the database.
        time.sleep(cls.SLEEP_TIME*3)
    except socket.error as socket_error:
      AppScaleLogger.warn('Unable to initialize AppController: {}'.
                          format(socket_error.message))
      message = RemoteHelper.collect_appcontroller_crashlog(
        head_node, options.keyname)
      raise AppControllerException(message)

    # Set up admin account.
    try:
      # We don't need to have any exception information here: we do expect
      # some anyway while the UserAppServer is coming up.
      acc.does_user_exist("non-existent-user", True)
    except Exception:
      AppScaleLogger.log('UserAppServer not ready yet. Retrying ...')
      time.sleep(cls.SLEEP_TIME)

    if options.admin_user and options.admin_pass:
      AppScaleLogger.log("Using the provided admin username/password")
      username, password = options.admin_user, options.admin_pass
    elif options.test:
      AppScaleLogger.log("Using default admin username/password")
      username, password = LocalState.DEFAULT_USER, LocalState.DEFAULT_PASSWORD
    else:
      username, password = LocalState.get_credentials()

    RemoteHelper.create_user_accounts(username, password, head_node,
                                      options.keyname)
    acc.set_admin_role(username, 'true', cls.ADMIN_CAPABILITIES)

    # Wait for machines to finish loading and AppScale Dashboard to be deployed.
    RemoteHelper.wait_for_machines_to_finish_loading(head_node, options.keyname)

    try:
      login_host = acc.get_property('login')['login']
    except KeyError:
      raise AppControllerException('login property not found')

    RemoteHelper.sleep_until_port_is_open(
      login_host, RemoteHelper.APP_DASHBOARD_PORT)

    AppScaleLogger.success("AppScale successfully started!")
    AppScaleLogger.success(
      'View status information about your AppScale deployment at '
      'http://{}:{}'.format(login_host, RemoteHelper.APP_DASHBOARD_PORT))
    AppScaleLogger.remote_log_tools_state(options, my_id,
      "finished", APPSCALE_VERSION)
  def test_start_head_node(self):
    self.options = flexmock(
      infrastructure='public cloud',
      group='group',
      machine='vm image',
      instance_type='instance type',
      keyname='keyname',
      table='cassandra',
      verbose=False,
      test=False,
      use_spot_instances=False,
      zone='zone',
      static_ip=None,
      replication=None,
      appengine=None,
      autoscale=None,
      user_commands=[],
      flower_password='',
      max_memory='X',
    )

    self.node_layout = NodeLayout(self.options)

    flexmock(LocalState).\
      should_receive("generate_secret_key").\
      with_args(self.options.keyname).\
      and_return('some secret key')

    flexmock(LocalState).\
      should_receive("get_key_path_from_name").\
      with_args(self.options.keyname).\
      and_return('some key path')

    flexmock(NodeLayout).should_receive('head_node').\
      and_return(SimpleNode('some IP', 'cloud'))

    fake_agent = FakeAgent()
    flexmock(factory.InfrastructureAgentFactory).\
      should_receive('create_agent').\
      with_args('public cloud').\
      and_return(fake_agent)

    self.additional_params = {}
    deployment_params = {}

    flexmock(LocalState).\
      should_receive('generate_deployment_params').\
      with_args(self.options, self.node_layout, self.additional_params).\
      and_return(deployment_params)

    flexmock(AppScaleLogger).should_receive('log').and_return()
    flexmock(AppScaleLogger).should_receive('remote_log_tools_state').\
      and_return()

    flexmock(time).should_receive('sleep').and_return()

    flexmock(RemoteHelper).\
      should_receive('copy_deployment_credentials').\
      with_args('some IP', self.options).\
      and_return()

    flexmock(RemoteHelper).\
      should_receive('run_user_commands').\
      with_args('some IP', self.options.user_commands,
                self.options.keyname, self.options.verbose).\
      and_return()

    flexmock(RemoteHelper).\
      should_receive('start_remote_appcontroller').\
      with_args('some IP', self.options.keyname, self.options.verbose).\
      and_return()

    layout = {}
    flexmock(NodeLayout).should_receive('to_list').and_return(layout)

    flexmock(AppControllerClient).\
      should_receive('set_parameters').\
      with_args(layout, deployment_params).\
      and_return()

    RemoteHelper.start_head_node(self.options, 'an ID', self.node_layout)