簡體   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