Exemple #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);
                }
            }
        }
        /*
         * <include>
         *    http://myhost/user-data1
         *    http://myhost/user-data2
         * </include>
         *
         */
        override protected void Handle()
        {
            var urls      = this.AsMultiLinesWithoutTag;
            var validUrls = urls.Where(url =>
                                       url.ToLower().StartsWith("http://"));
            List <String> localUserData = new List <String>();

            foreach (String url in validUrls)
            {
                String filePath = String.Format("{0}\\{1}.txt",
                                                CloudInit.CloudInitDirectory, Guid.NewGuid().ToString().Substring(0, 8));
                try{
                    EucaUtil.Curl(url, filePath);
                    localUserData.Add(filePath);
                }catch (Exception ex) {
                    EucaLogger.Exception(String.Format("Failed to download from the include url {0}", url), ex);
                    continue;
                }
            }

            foreach (String userDataFile in localUserData)
            {
                UserDataHandler handler = null;
                try
                {
                    handler = UserDataHandlerFactory.Instance.GetHandler(userDataFile);
                }
                catch (Exception ex)
                {
                    EucaLogger.Exception("Unable to find the right handler for include file", ex);
                }

                try
                {
                    handler.HandleUserData(userDataFile);
                }
                catch (Exception ex)
                {
                    EucaLogger.Exception("Failed to handle the user data", ex);
                }
            }

            foreach (String userDataFile in localUserData)
            {
                File.Delete(userDataFile);
            }
        }
Exemple #3
0
        public void Init()
        {
            string userDataFile = null;

            try
            {
                userDataFile = CloudInit.CloudInitDirectory + "\\user-data";
                EucaUtil.GetUserData(userDataFile);
                if (!File.Exists(userDataFile))
                {
                    throw new EucaException("User data file not found");
                }
                if ((new FileInfo(userDataFile)).Length <= 0)
                {
                    throw new EucaException("Invalid user data file");
                }
            }
            catch (Exception ex)
            {
                EucaLogger.Debug("Unable to download the user-data");
                throw ex;
            }

            // detect the contents
            UserDataHandler handler = null;

            try
            {
                handler = UserDataHandlerFactory.Instance.GetHandler(userDataFile);
            }
            catch (Exception ex)
            {
                EucaLogger.Exception("Unable to find the handler for matching user-data contents", ex);
                return;
            }
            // invoke handler
            try
            {
                handler.HandleUserData(userDataFile);
            }
            catch (Exception e)
            {
                EucaLogger.Exception("User data handler threw exception", e);
            }
            // return
        }
 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);
     }
 }