繁体   English   中英

如何在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}

这是此示例的实现(请注意,这是旧文章中的代码)。

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM