如何从 ec2 实例中的用户数据编辑 xml 行?

How can I edit an xml line from user data in ec2 instance?

我在从后端启动的多个 ec2 实例中使用 Wowza 流媒体引擎。我需要一种将边缘实例与原点链接起来的方法,为此我需要更新 xml 文件中特定行的值。当边缘旋转时,我已将 ip 加载到用户数据中。 我需要什么来启动一个脚本来编辑这个来自用户数据的标签中的值? 谢谢

您可以让服务器侦听器将 IP 地址加载到自定义变量中。然后您可以让您的配置文件引用这个自定义变量。这与填充 Wowza EC2 变量的方法相同,例如 "com.wowza.amazonaws.ec2.AWSEC2_METADATA_LOCAL_IPV4"。这些可以作为

添加到 Wowza 配置文件中
${com.wowza.amazonaws.ec2.AWSEC2_METADATA_LOCAL_IPV4}

这是一个示例实现(请注意,这是来自 old post 的代码)。

package com.wowza.wms.plugin.amazonaws.ec2.env;

import com.wowza.util.*;
import com.wowza.wms.server.*;
import com.wowza.wms.vhost.HostPort;
import com.wowza.wms.logging.*;

public class ServerListenerEC2Variables implements IServerNotify2
{
    public static final String VARPREFIX = "com.wowza.amazonaws.ec2";

    public static final String VAR_AMI_ID = "AWSEC2_METADATA_AMI_ID";
    public static final String VAR_AMI_LAUNCH_INDEX = "AWSEC2_METADATA_AMI_LAUNCH_INDEX";
    public static final String VAR_AMI_MANIFEST_PATH = "AWSEC2_METADATA_AMI_MANIFEST_PATH";
    public static final String VAR_INSTANCE_ID = "AWSEC2_METADATA_INSTANCE_ID";
    public static final String VAR_INSTANCE_TYPE = "AWSEC2_METADATA_INSTANCE_TYPE";
    public static final String VAR_HOSTNAME = "AWSEC2_METADATA_HOSTNAME";
    public static final String VAR_LOCAL_HOSTNAME = "AWSEC2_METADATA_LOCAL_HOSTNAME";
    public static final String VAR_LOCAL_IPV4 = "AWSEC2_METADATA_LOCAL_IPV4";
    public static final String VAR_PUBLIC_HOSTNAME = "AWSEC2_METADATA_PUBLIC_HOSTNAME";
    public static final String VAR_PUBLIC_IPV4 = "AWSEC2_METADATA_PUBLIC_IPV4";
    public static final String VAR_RESERVATION_ID = "AWSEC2_METADATA_RESERVATION_ID";
    public static final String VAR_SECURITY_GROUPS = "AWSEC2_METADATA_SECURITY_GROUPS";
    public static final String VAR_PRODUCT_CODES = "AWSEC2_METADATA_PRODUCT_CODES";

    private void populateEnvironment()
    {
        try
        {
            String[] urls = {
                    "http://169.254.169.254/latest/meta-data/ami-id",
                    "http://169.254.169.254/latest/meta-data/ami-launch-index",
                    "http://169.254.169.254/latest/meta-data/ami-manifest-path",
                    "http://169.254.169.254/latest/meta-data/instance-id",
                    "http://169.254.169.254/latest/meta-data/instance-type",
                    "http://169.254.169.254/latest/meta-data/hostname",
                    "http://169.254.169.254/latest/meta-data/local-hostname",
                    "http://169.254.169.254/latest/meta-data/local-ipv4",
                    "http://169.254.169.254/latest/meta-data/public-hostname",
                    "http://169.254.169.254/latest/meta-data/public-ipv4",
                    /* "http://169.254.169.254/2007-03-01/meta-data/public-keys/", */
                    "http://169.254.169.254/latest/meta-data/reservation-id",
                    "http://169.254.169.254/latest/meta-data/security-groups",
                    "http://169.254.169.254/latest/meta-data/product-codes",
                };

            for(int i=0;i<urls.length;i++)
            {
                String url = urls[i];
                byte[] data = HTTPUtils.HTTPRequestToByteArray(url, "GET", null, null);
                if (data != null)
                {
                    String[] urlParts = url.split("/");
                    if (urlParts.length > 1)
                    {
                        try
                        {
                            String key = VARPREFIX+"."+"AWSEC2_METADATA_"+urlParts[urlParts.length-1].replace("-", "_").toUpperCase();
                            String value = new String(data, "UTF-8");

                            System.setProperty(key, value);
                            WMSLoggerFactory.getLogger(ServerListenerEC2Variables.class).info(key+": "+value);
                        }
                        catch (Exception e)
                        {
                            WMSLoggerFactory.getLogger(ServerListenerEC2Variables.class).error("ServerListenerEC2Variables.populateEnvironment: conversion to string: "+e.toString());
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            WMSLoggerFactory.getLogger(ServerListenerEC2Variables.class).error("ServerListenerEC2Variables.populateEnvironment: "+e.toString());
        }
    }

    public void onServerConfigLoaded(IServer server)
    {
        populateEnvironment();

        Server inServer = (Server)server;
        JMXRemoteConfig remoteConfig = inServer.getJmxRemoteConfig();

        while(true)
        {
            String ipAddress = remoteConfig.getIpAddress();
            if (ipAddress == null)
                break;

            ipAddress = SystemUtils.expandEnvironmentVariables(ipAddress);

            WMSLoggerFactory.getLogger(ServerListenerEC2Variables.class).info("Updating JMX IpAddress: "+ipAddress);
            remoteConfig.setIpAddress(ipAddress);
            break;
        }

        while(true)
        {
            String ipAddress = remoteConfig.getRmiServerHostName();
            if (ipAddress == null)
                break;

            ipAddress = SystemUtils.expandEnvironmentVariables(ipAddress);

            WMSLoggerFactory.getLogger(ServerListenerEC2Variables.class).info("Updating JMX RMIServerHostName: "+ipAddress);
            remoteConfig.setRmiServerHostName(ipAddress);
            break;
        }

        while(true)
        {
            String propName = "loadBalancerSenderRedirectAddress";
            String ipAddress = server.getProperties().getPropertyStr(propName);
            if (ipAddress == null)
                break;

            ipAddress = SystemUtils.expandEnvironmentVariables(ipAddress);

            WMSLoggerFactory.getLogger(ServerListenerEC2Variables.class).info("Updating Server.property."+propName+": "+ipAddress);
            server.getProperties().setProperty(propName, ipAddress);
            break;
        }       
    }

    public void onServerCreate(IServer server)
    {
    }

    public void onServerInit(IServer server)
    {
    }

    public void onServerShutdownStart(IServer server)
    {
    }

    public void onServerShutdownComplete(IServer server)
    {
    }

}

假设定义了origin_ip:

script = "#!/bin/bash
          sed -i -e '/<Name>loadbalanceServerIP<\/Name>/,/<Value>/s/<Value>[^<]*/<Value>#{origin_ip}/' /home/wowza/conf/Server.xml
          sudo service WowzaStreamingEngine restart"