Inheritance: UserDataHandler
示例#1
0
        /****
        * Contents in the unzipped directory
        * script: script file to be handled by ScriptHandler
        * powershell: powershell file to be handled by ScriptHandler
        * eucalyptus: eucalyptus file to be handled by EucalyptusParameterHandler
        * include: include file to be handled by IncludeHandler
        * and any other resource files to be used by script/powershell handlers (exe, dll, etc)
        ****/
        override protected void Handle()
        {
            if (!Directory.Exists(UnzippedDir))
            {
                EucaLogger.Error(String.Format("Can't find the unzipped directory {0}", UnzippedDir));
                return;
            }
            else if (File.Exists(UserDataFile))
            {
                try
                {
                    EucaFileUtil.Unzip(UnzippedDir, UserDataFile);
                }
                catch (Exception ex)
                {
                    EucaLogger.Exception(String.Format("Failed to unzip {0} into {1}", UserDataFile, UnzippedDir), ex);
                    return;
                }
            }

            foreach (String filePath in Directory.GetFiles(UnzippedDir))
            {
                String          fileName = Path.GetFileName(filePath).ToLower();
                UserDataHandler handler  = null;
                if (fileName.Equals("script") || fileName.Equals("powershell"))
                {
                    handler = new ScriptHandler();
                }
                else if (fileName.Equals("eucalyptus"))
                {
                    handler = new EucalyptusParameterHandler();
                }
                else if (fileName.Equals("include"))
                {
                    handler = new IncludeHandler();
                }
                else
                {
                    EucaLogger.Debug(String.Format("unknown file: {0}", fileName));
                    continue;
                }

                try
                {
                    handler.HandleUserData(filePath);
                    EucaLogger.Debug(String.Format("Successfully handled the contents in {0}", fileName));
                }
                catch (Exception ex)
                {
                    EucaLogger.Exception(String.Format("failed to handle the file {0}", fileName), ex);
                }
            }
        }
 internal UserDataHandler GetHandler(String userDataFile)
 {
     /*
      *  <script> </script>   (from ec2config)
         <powershell> </powershell>    (from ec2config)
         <eucalyptus> </eucalyptus>    : for euca-specific parameter passing
         <include> </include> : (optional) a list of URLs (that's to be processed with the same rule)
         zipped directory
      *
      */
     try
     {
         String unzippedDir = String.Format("{0}\\unzipped", CloudInit.CloudInitDirectory);
         if(Directory.Exists(unzippedDir))
             Directory.Delete(unzippedDir, true);
         Directory.CreateDirectory(unzippedDir);
         tryDecompress(userDataFile, unzippedDir);
         return new ZipHandler(unzippedDir);
     }
     catch (Exception ex)
     {
         // the user-data is not a zipped file
         UserDataHandler dataHandler = null;
         using (StreamReader sr = new StreamReader(File.OpenRead(userDataFile)))
         {
             String line = null;
             while ((line = sr.ReadLine()) != null)
             {
                 line = line.Trim();
                 if (line.Length > 0)
                 {
                     line = line.ToLower();
                     if (line.StartsWith("<script>") || line.StartsWith("<powershell>"))
                         dataHandler = new ScriptHandler();
                     else if (line.StartsWith("<eucalyptus>"))
                         dataHandler = new EucalyptusParameterHandler();
                     else if (line.StartsWith("<include>"))
                         dataHandler = new IncludeHandler();
                     else
                         dataHandler = new BogusHandler();
                     break;
                 }
             }
         }
         if (dataHandler == null)
             throw new EucaException("User-data is in unknown format");
         return dataHandler;
     }
 }
 internal UserDataHandler GetHandler(String userDataFile)
 {
     /*
      *  <script> </script>   (from ec2config)
      *  <powershell> </powershell>    (from ec2config)
      *  <eucalyptus> </eucalyptus>    : for euca-specific parameter passing
      *  <include> </include> : (optional) a list of URLs (that's to be processed with the same rule)
      *  zipped directory
      *
      */
     try
     {
         String unzippedDir = String.Format("{0}\\unzipped", CloudInit.CloudInitDirectory);
         if (Directory.Exists(unzippedDir))
         {
             Directory.Delete(unzippedDir, true);
         }
         Directory.CreateDirectory(unzippedDir);
         tryDecompress(userDataFile, unzippedDir);
         return(new ZipHandler(unzippedDir));
     }
     catch (Exception ex)
     {
         // the user-data is not a zipped file
         UserDataHandler dataHandler = null;
         using (StreamReader sr = new StreamReader(File.OpenRead(userDataFile)))
         {
             String line = null;
             while ((line = sr.ReadLine()) != null)
             {
                 line = line.Trim();
                 if (line.Length > 0)
                 {
                     line = line.ToLower();
                     if (line.StartsWith("<script>") || line.StartsWith("<powershell>"))
                     {
                         dataHandler = new ScriptHandler();
                     }
                     else if (line.StartsWith("<eucalyptus>"))
                     {
                         dataHandler = new EucalyptusParameterHandler();
                     }
                     else if (line.StartsWith("<include>"))
                     {
                         dataHandler = new IncludeHandler();
                     }
                     else
                     {
                         dataHandler = new BogusHandler();
                     }
                     break;
                 }
             }
         }
         if (dataHandler == null)
         {
             throw new EucaException("User-data is in unknown format");
         }
         return(dataHandler);
     }
 }
        /****
         * Contents in the unzipped directory
         * script: script file to be handled by ScriptHandler
         * powershell: powershell file to be handled by ScriptHandler
         * eucalyptus: eucalyptus file to be handled by EucalyptusParameterHandler
         * include: include file to be handled by IncludeHandler
         * and any other resource files to be used by script/powershell handlers (exe, dll, etc)
         ****/
        protected override void Handle()
        {
            if (!Directory.Exists(UnzippedDir))
            {
                EucaLogger.Error(String.Format("Can't find the unzipped directory {0}", UnzippedDir));
                return;
            }
            else if (File.Exists(UserDataFile))
            {
                try
                {
                    EucaFileUtil.Unzip(UnzippedDir, UserDataFile);
                }
                catch (Exception ex)
                {
                    EucaLogger.Exception(String.Format("Failed to unzip {0} into {1}", UserDataFile, UnzippedDir), ex);
                    return;
                }
            }

            foreach (String filePath in Directory.GetFiles(UnzippedDir))
            {
                String fileName = Path.GetFileName(filePath).ToLower();
                UserDataHandler handler = null;
                if (fileName.Equals("script") || fileName.Equals("powershell"))
                    handler = new ScriptHandler();
                else if (fileName.Equals("eucalyptus"))
                    handler = new EucalyptusParameterHandler();
                else if (fileName.Equals("include"))
                    handler = new IncludeHandler();
                else
                {
                    EucaLogger.Debug(String.Format("unknown file: {0}", fileName));
                    continue;
                }

                try
                {
                    handler.HandleUserData(filePath);
                    EucaLogger.Debug(String.Format("Successfully handled the contents in {0}", fileName));
                }
                catch (Exception ex)
                {
                    EucaLogger.Exception(String.Format("failed to handle the file {0}", fileName), ex);
                }
            }
        }