示例#1
0
def create_swapfile(fname: str, size: str) -> None:
    """Size is in MiB."""

    errmsg = "Failed to create swapfile '%s' of size %sMB via %s: %s"

    def create_swap(fname, size, method):
        LOG.debug(
            "Creating swapfile in '%s' on fstype '%s' using '%s'",
            fname,
            fstype,
            method,
        )

        if method == "fallocate":
            cmd = ["fallocate", "-l", "%sM" % size, fname]
        elif method == "dd":
            cmd = [
                "dd",
                "if=/dev/zero",
                "of=%s" % fname,
                "bs=1M",
                "count=%s" % size,
            ]

        try:
            subp.subp(cmd, capture=True)
        except subp.ProcessExecutionError as e:
            LOG.info(errmsg, fname, size, method, e)
            util.del_file(fname)
            raise

    swap_dir = os.path.dirname(fname)
    util.ensure_dir(swap_dir)

    fstype = util.get_mount_info(swap_dir)[1]

    if (fstype == "xfs" and util.kernel_version() <
        (4, 18)) or fstype == "btrfs":
        create_swap(fname, size, "dd")
    else:
        try:
            create_swap(fname, size, "fallocate")
        except subp.ProcessExecutionError:
            LOG.info("fallocate swap creation failed, will attempt with dd")
            create_swap(fname, size, "dd")

    if os.path.exists(fname):
        util.chmod(fname, 0o600)
    try:
        subp.subp(["mkswap", fname])
    except subp.ProcessExecutionError:
        util.del_file(fname)
        raise
示例#2
0
def create_swapfile(fname, size):
    """Size is in MiB."""

    errmsg = "Failed to create swapfile '%s' of size %dMB via %s: %s"

    def create_swap(fname, size, method):
        LOG.debug("Creating swapfile in '%s' on fstype '%s' using '%s'", fname,
                  fstype, method)

        if method == "fallocate":
            cmd = ['fallocate', '-l', '%dM' % size, fname]
        elif method == "dd":
            cmd = [
                'dd', 'if=/dev/zero',
                'of=%s' % fname, 'bs=1M',
                'count=%d' % size
            ]

        try:
            util.subp(cmd, capture=True)
        except util.ProcessExecutionError as e:
            LOG.warning(errmsg, fname, size, method, e)
            util.del_file(fname)

    swap_dir = os.path.dirname(fname)
    util.ensure_dir(swap_dir)

    fstype = util.get_mount_info(swap_dir)[1]

    if fstype in ("xfs", "btrfs"):
        create_swap(fname, size, "dd")
    else:
        try:
            create_swap(fname, size, "fallocate")
        except util.ProcessExecutionError as e:
            LOG.warning(errmsg, fname, size, "dd", e)
            LOG.warning("Will attempt with dd.")
            create_swap(fname, size, "dd")

    util.chmod(fname, 0o600)
    try:
        util.subp(['mkswap', fname])
    except util.ProcessExecutionError:
        util.del_file(fname)
        raise
示例#3
0
def handle(name, cfg, cloud, log, _args):
    try:

        # Go Agent Config Prefix
        go_agent_prefix = "goagent_"
        go_agent_default = "/etc/default/go-agent"

        go_server_prefix = "goserver_"
        go_server_config_dir = "/var/lib/go-agent/config"
        if not os.path.exists(go_server_config_dir):
            os.makedirs(go_server_config_dir)
            util.chownbyname(go_server_config_dir, "go", "go")
        go_server_config = go_server_config_dir + "/autoregister.properties"

        # Make sure Go Agent is not running
        util.subp(['service', 'go-agent', 'stop'])

        md = cloud.datasource.metadata
        for key in md['meta']:
            if key.startswith(go_agent_prefix):
                replace_config(go_agent_default, key[len(go_agent_prefix):],
                               md['meta'][key])
            elif key.startswith(go_server_prefix):
                replace_config(go_server_config, key[len(go_server_prefix):],
                               md['meta'][key])

        # Use Openstack Instance ID as Go Elastic Agent ID for auto registraton
        replace_config(go_server_config,
                       "agent.auto.register.elasticAgent.agentId", md['uuid'])

        if os.path.exists(go_server_config):
            util.chownbyname(go_server_config, "go", "go")

        if os.path.exists(go_agent_default):
            util.chmod(go_agent_default, '0644')

        # Start Go Agent
        util.subp(['service', 'go-agent', 'start'])

    except:
        log.error("Error configuring Go Agent")
        return
def handle(name, cfg, cloud, log, _args):
  try:

    # Make sure Go Agent is not running
    util.subp(['service', 'go-agent', 'stop'])

    # Go Agent Config Prefix
    go_agent_prefix = "goagent_"
    go_agent_default = "/etc/default/go-agent"
    
    go_server_prefix = "goserver_"
    go_server_config_dir = "/var/lib/go-agent/config"
    if not os.path.exists(go_server_config_dir):
      os.makedirs(go_server_config_dir)
      util.chownbyname(go_server_config_dir,"go","go")
    go_server_config = go_server_config_dir + "/autoregister.properties"


    md = cloud.datasource.metadata
    for key in md['meta']:
      if key.startswith(go_agent_prefix):
        replace_config(go_agent_default,key[len(go_agent_prefix):],md['meta'][key])
      elif key.startswith(go_server_prefix):
        replace_config(go_server_config,key[len(go_server_prefix):],md['meta'][key])
    
    replace_config(go_server_config,"agent.auto.register.elasticAgent.agentId",md['instance-id'])
        
    if os.path.exists(go_server_config):
      util.chownbyname(go_server_config,"go","go")

    if os.path.exists(go_agent_default):
      util.chmod(go_agent_default,0644)
    
    # Start Go Agent
    util.subp(['service', 'go-agent', 'start'])

  except:
    log.debug("Error configuring Go Agent")
    return