예제 #1
0
        static void Main(string[] args)
        {
            try
            {
                XLog.statuslog("Started");

                Ec2ConfigDir = @"C:\Program Files\Amazon\Ec2ConfigService";
                if (!System.IO.Directory.Exists(Ec2ConfigDir))
                {
                    Ec2ConfigDir = @"C:\Program Files (x86)\Amazon\Ec2ConfigSetup";
                    if (!System.IO.Directory.Exists(Ec2ConfigDir))
                    {
                        throw new System.IO.IOException(@"Unable to locate directory for Amazon\Ec2ConfigSetup");
                    }
                }

                Ec2ConfigLogsDir = Ec2ConfigDir + @"\Logs";
                if (!System.IO.Directory.Exists(Ec2ConfigLogsDir))
                {
                    Ec2ConfigLogsDir = Ec2ConfigDir;
                }

                try
                {
                    // C:\<ProgramFiles>\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt
                    // contains "Ec2RebootInstance:Windows is Ready to use"
                    // Allow thread abort and interrupt exceptions here.
                    XLog.statuslog("Waiting on EC2 instance to fully to initialize");
                    for (; ;)
                    {
                        string content = System.IO.File.ReadAllText(
                            Ec2ConfigLogsDir + @"\Ec2ConfigLog.txt");
                        if (-1 != content.IndexOf("Windows is Ready to use"))
                        {
                            break;
                        }
                        System.Threading.Thread.Sleep(1000 * 20);
                    }
                }
                catch (System.Threading.ThreadInterruptedException)
                {
                    return;
                }
                catch (System.Threading.ThreadAbortException)
                {
                    return;
                }

                XLog.statuslog("Processing user data");
                string userdata;
                for (int itries = 0; ; itries++)
                {
                    try
                    {
                        System.Net.WebClient wc = new System.Net.WebClient();
                        userdata = wc.DownloadString("http://169.254.169.254/2009-04-04/user-data");
                    }
                    catch
                    {
                        if (itries > 5)
                        {
                            throw;
                        }
                        continue;
                    }
                    break;
                }

                userdata = userdata.Trim();
                string[] options = get(userdata).Split('\t');
                if (options.Length < 1 ||
                    options[0].Length < 1 ||
                    options[0][0] != '@')
                {
                    throw new Exception("Not expected format of user data");
                }

                string mypassword = "";

                // Do the other options first.
                for (int i = 1; i < options.Length; i++)
                {
                    string opt = options[i];
                    if (opt.StartsWith("PASSWORD:"******"\t", options));
#endif

                int iarg = 0;
                if (args.Length > iarg &&
                    args[iarg].StartsWith("PASSWORD:"******"Expected new password");
                }

                if (!string.IsNullOrEmpty(mypassword))
                {
                    XLog.statuslog("Updating password");
                    // Set the password: http://support.microsoft.com/kb/149427
                    Exec.Shell("net user administrator  " + mypassword);
                }

#if RESETEC2SERVICEPASSWORD
                {
                    XLog.statuslog("Setting QizmtEC2Service permissions");

                    {
                        // Should already be stopped...
                        //System.Threading.Thread.Sleep(1000 * 2);
                        Exec.Shell(@"sc stop QizmtEC2Service");
                    }

                    {
                        System.Threading.Thread.Sleep(1000 * 2);
                        CallSC(@"sc config QizmtEC2Service obj= .\administrator password= "******"sc start QizmtEC2Service");
                    }
                }
#endif

                try
                {
                    // Note: if there's no D drive, stay on C.
                    if (System.IO.Directory.Exists(LocalQizmtDir))
                    {
                        string NewLocalQizmtParentDir = @"D:\";
                        string NewLocalQizmtDir       = @"D:\Qizmt";
                        if (!System.IO.Directory.Exists(NewLocalQizmtDir) &&
                            System.IO.Directory.Exists(NewLocalQizmtParentDir))
                        {
                            //XLog.statuslog("Copy Qizmt to " + NewLocalQizmtDir);
                            CopyDirectory(LocalQizmtDir, NewLocalQizmtDir);
                            if (!System.IO.Directory.Exists(LocalQizmtDir))
                            {
                                throw new System.IO.IOException("Not able to copy Qizmt from " + LocalQizmtDir + " to " + NewLocalQizmtDir);
                            }
                            LocalQizmtDir = NewLocalQizmtDir;
                            XLog.statuslog("Copied Qizmt to " + NewLocalQizmtDir);
                        }
                    }
                }
                catch
                {
                }

                {
                    XLog.statuslog("Setting Qizmt DistributedObjects permissions");

                    {
                        //System.Threading.Thread.Sleep(1000 * 2);
                        Exec.Shell(@"sc stop DistributedObjects");
                    }

                    {
                        System.Threading.Thread.Sleep(1000 * 2);
                        CallSC(@"sc config DistributedObjects binPath= """ + LocalQizmtDir + @"\MySpace.DataMining.DistributedObjects.exe"" obj= .\administrator password= """ + mypassword + @"""");
                    }

                    {
                        System.Threading.Thread.Sleep(1000 * 4);
                        CallSC(@"sc start DistributedObjects");
                    }
                }

                System.Threading.Thread.Sleep(1000 * 2);

                if (options[0].StartsWith("@SURROGATEWORKER:"))
                {
                    // Participating surrogate.
                    XLog.statuslog("Configuring Qizmt surrogate (participating)");
                    DoSurrogate(options[0].Substring(options[0].IndexOf(':') + 1),
                                mypassword, true);
                }
                else if (options[0].StartsWith("@SURROGATENONWORKER:"))
                {
                    // Non-participating surrogate
                    XLog.statuslog("Configuring Qizmt surrogate (non-participating)");
                    DoSurrogate(options[0].Substring(options[0].IndexOf(':') + 1),
                                mypassword, false);
                }
                else
                {
                    XLog.statuslog("Configuring Qizmt worker");
                }
            }
            catch (Exception e)
            {
                XLog.errorlog(e.ToString());
            }

            XLog.statuslog("Done");
        }
예제 #2
0
        static void DoSurrogate(string hostsinfo, string mypassword, bool participating)
        {
            string[] hinfos   = hostsinfo.Split((char)1);
            string   myipaddr = IPAddressUtil.GetIPv4Address(System.Net.Dns.GetHostName());
            string   myhost   = GetHostnameInternal(myipaddr);

            try
            {
                if (myipaddr != IPAddressUtil.GetIPv4Address(myhost))
                {
                    throw new Exception("IP addresses do not match");
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error with internal IP address and host name"
                                    + " (" + myipaddr + " != " + myhost + ")", e);
            }
            // File in the format:
            // <WindowsUser>
            // <machine>=<password>
            string logondatContent = "administrator" + Environment.NewLine;

            logondatContent += myhost + "=" + mypassword + Environment.NewLine;

            string formatmachines = "";

            if (participating)
            {
                formatmachines = myhost;
            }

            string appendhostsfiles = Environment.NewLine
                                      + myipaddr + "    " + myhost + Environment.NewLine;

            foreach (string hinfo in hinfos)
            {
                int ieq = hinfo.IndexOf('=');
                if (-1 != ieq)
                {
                    // hinfo already in form <machine>=<password>
                    //logondatContent += hinfo + Environment.NewLine;
                    // I want the hostname, not the IPaddr.

                    string rhost   = hinfo.Substring(0, ieq); // Probably an internal IP address.
                    string rpasswd = hinfo.Substring(ieq + 1);
                    {
                        if (formatmachines.Length > 0)
                        {
                            formatmachines += ";";
                        }
                        string fmhost = rhost;
                        try
                        {
                            fmhost            = GetHostnameInternal(rhost);
                            appendhostsfiles += rhost + "    " + fmhost + Environment.NewLine;
                        }
                        catch
                        {
                        }
                        formatmachines  += fmhost;
                        logondatContent += fmhost + "=" + rpasswd + Environment.NewLine;
                    }
                }
            }

            // Append my appendhostsfiles to hosts file.
            string hostsfilepath = Environment.ExpandEnvironmentVariables(
                @"%SystemRoot%\system32\drivers\etc\hosts");

            System.IO.File.Copy(hostsfilepath, "hosts.old", true); // Backup old one.
            System.IO.File.AppendAllText(ToNetworkPath(hostsfilepath, myipaddr), appendhostsfiles);
            System.Threading.Thread.Sleep(1000);

            // Write my logon.dat; and logon.
            string thisnetpath = NetworkPathForHost(myipaddr);

            if (!System.IO.Directory.Exists(thisnetpath))
            {
                System.IO.Directory.CreateDirectory(thisnetpath);
            }
            string thislogonfile = thisnetpath + @"\logon.dat";

            for (int itries = 0; ;)
            {
                try
                {
                    System.IO.File.WriteAllText(thislogonfile, logondatContent);
                }
                catch
                {
                    if (++itries > 3)
                    {
                        throw;
                    }
                    System.Threading.Thread.Sleep(1000 * itries);
                    try
                    {
                        System.IO.File.Delete(thislogonfile);
                    }
                    catch
                    {
                    }
                    continue;
                }
                break;
            }
            XLog.statuslog("Logging on all machines in cluster");
            if (!LogonMachines(thislogonfile))
            {
                throw new Exception("Was not able to LogonMachines");
            }
            XLog.statuslog("Logged on all machines in cluster");

            XLog.statuslog("Configuring machines in cluster");
            // Write logon.dat to other machines.
            // Append appendhostsfiles to other machines' hosts file.
            foreach (string hinfo in hinfos)
            {
                int ieq = hinfo.IndexOf('=');
                if (-1 != ieq)
                {
                    string rhost = hinfo.Substring(0, ieq); // Probably an internal IP address.

                    string fmhost = rhost;
                    try
                    {
                        fmhost = GetHostnameInternal(rhost);
                    }
                    catch
                    {
                    }

                    string rnetpath = NetworkPathForHost(fmhost);
                    if (!System.IO.Directory.Exists(rnetpath))
                    {
                        System.IO.Directory.CreateDirectory(rnetpath);
                    }
                    string rlogonfile = rnetpath + @"\logon.dat";

                    for (int itries = 0; ;)
                    {
                        try
                        {
                            System.IO.File.WriteAllText(rlogonfile, logondatContent);
                        }
                        catch
                        {
                            if (++itries > 3)
                            {
                                throw;
                            }
                            System.Threading.Thread.Sleep(1000 * itries);
                            try
                            {
                                System.IO.File.Delete(rlogonfile);
                            }
                            catch
                            {
                            }
                            continue;
                        }
                        break;
                    }

                    System.IO.File.AppendAllText(ToNetworkPath(hostsfilepath, fmhost), appendhostsfiles);
                }
            }
            XLog.statuslog("Configured machines in cluster");

            {
                string fmtcmd = "Qizmt format Machines=" + formatmachines;
                XLog.statuslog("Formatting Qizmt cluster: " + fmtcmd);
                Exec.Shell(fmtcmd);
            }
        }