Example #1
0
        protected override bool Execute(IIntegrationResult result)
        {
            result.BuildProgressInformation.SignalStartRunTask(
                string.Format("Running virtual machine profile '{0}' declared in configuration file '{1}'.",
                              Profile, Configuration));

            string configurationFullPath = result.BaseFromWorkingDirectory(Configuration);

            XmlConfiguration xmlConfiguration = ConfigurationFileHelper.LoadConfiguration(configurationFullPath);
            XmlProfile       xmlProfile       = xmlConfiguration.GetProfileById(Profile);

            if (xmlProfile == null)
            {
                throw new BuilderException(this, string.Format("Did not find profile '{0}' in configuration file '{1}'.", Profile, configurationFullPath));
            }

            global::VMTool.Core.Profile profile = xmlProfile.ToProfile();

            using (CCNetController controller = new CCNetController(profile))
            {
                controller.ConnectionTimeout = TimeSpan.FromSeconds(ConnectionTimeout);

                using (RemoteContext remoteContext = new RemoteContext(controller))
                {
                    remoteContext.RemoteArtifactDirectory = RemoteArtifactDirectory;
                    remoteContext.RemoteWorkingDirectory  = RemoteWorkingDirectory;

                    Status status = controller.GetStatus();

                    switch (ConfiguredStartAction)
                    {
                    case StartAction.Auto:
                        if (xmlProfile.Snapshot != null)
                        {
                            Restart(controller, status);
                        }
                        else
                        {
                            StartOrResume(controller, status);
                        }
                        break;

                    case StartAction.StartOrResume:
                        StartOrResume(controller, status);
                        break;

                    case StartAction.Restart:
                        Restart(controller, status);
                        break;
                    }

                    try
                    {
                        if (Tasks != null)
                        {
                            foreach (ITask task in Tasks)
                            {
                                RunTaskAndMergeResult(task, result);

                                if (result.Status != IntegrationStatus.Success)
                                {
                                    break;
                                }
                            }
                        }

                        if (Publishers != null)
                        {
                            foreach (ITask task in Publishers)
                            {
                                RunTaskAndMergeResult(task, result);
                            }
                        }
                    }
                    finally
                    {
                        switch (ConfiguredStopAction)
                        {
                        case StopAction.Auto:
                            if (xmlProfile.Snapshot != null)
                            {
                                controller.PowerOff();
                            }
                            else
                            {
                                controller.SaveState();
                            }
                            break;

                        case StopAction.SaveState:
                            controller.SaveState();
                            break;

                        case StopAction.Pause:
                            controller.Pause();
                            break;

                        case StopAction.Shutdown:
                            controller.Shutdown();
                            break;

                        case StopAction.PowerOff:
                            controller.PowerOff();
                            break;
                        }
                    }
                }
            }

            return(true);
        }
Example #2
0
        public static int Main(string[] args)
        {
            try
            {
                var options = new ClientOptions();
                var parser  = new CommandLineParser();
                if (!parser.ParseArguments(args, options, Console.Error))
                {
                    return(1);
                }

                if (options.Values == null || options.Values.Count == 0)
                {
                    PrintErrorMessageAndHelp(options, "Must specify a command.");
                    return(1);
                }

                string  commandName = options.Values[0];
                Command command     = CreateCommand(commandName);
                if (command == null)
                {
                    PrintErrorMessageAndHelp(options, "Unrecognized command name.");
                    return(1);
                }

                bool haveMaster  = options.Master != null;
                bool haveProfile = options.Configuration != null || options.Profile != null;
                if (haveMaster && haveProfile ||
                    !haveMaster && !haveProfile ||
                    (options.Configuration != null) != (options.Profile != null))
                {
                    PrintErrorMessageAndHelp(options, "Must specify either --master or both --configuration and --profile.");
                    return(1);
                }

                Profile profile;
                if (options.Configuration != null && options.Profile != null)
                {
                    XmlConfiguration xmlConfiguration = ConfigurationFileHelper.LoadConfiguration(options.Configuration);
                    XmlProfile       xmlProfile       = xmlConfiguration.GetProfileById(options.Profile);
                    if (xmlProfile == null)
                    {
                        PrintErrorMessageAndHelp(options, "Profile not found in configuration file.");
                        return(1);
                    }

                    profile = xmlProfile.ToProfile();
                }
                else
                {
                    profile = new Profile()
                    {
                        Master     = options.Master,
                        MasterPort = options.MasterPort,
                        Slave      = options.Slave,
                        SlavePort  = options.SlavePort,
                        VM         = options.VM,
                        Snapshot   = options.Snapshot
                    };
                }

                if (!command.Validate(profile, options))
                {
                    return(1);
                }

                using (var controller = new ClientController(profile))
                {
                    controller.Quiet             = options.Quiet;
                    controller.ConnectionTimeout = TimeSpan.FromSeconds(options.ConnectionTimeout);

                    try
                    {
                        return(command.Execute(controller, options));
                    }
                    catch (OperationFailedException ex)
                    {
                        Console.Error.WriteLine("Operation failed.");
                        Console.Error.WriteLine(ex.Why);

                        if (ex.__isset.details)
                        {
                            Console.Error.WriteLine("Details:");
                            Console.Error.WriteLine(ex.Details);
                        }
                        return(1);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal exception: " + ex);
                return(1);
            }
        }